blob: 03681013d804a7d030b268fd0555fec3b5643044 [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.
624 *
625 * This routine will issue the INIT_LINK mailbox command call.
626 * It is available to other drivers through the lpfc_hba data
627 * structure for use as a delayed link up mechanism with the
628 * module parameter lpfc_suppress_link_up.
629 *
630 * Return code
631 * 0 - success
632 * Any other value - error
633 **/
634int
635lpfc_hba_init_link(struct lpfc_hba *phba)
636{
637 struct lpfc_vport *vport = phba->pport;
638 LPFC_MBOXQ_t *pmb;
639 MAILBOX_t *mb;
640 int rc;
641
642 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
643 if (!pmb) {
644 phba->link_state = LPFC_HBA_ERROR;
645 return -ENOMEM;
646 }
647 mb = &pmb->u.mb;
648 pmb->vport = vport;
649
650 lpfc_init_link(phba, pmb, phba->cfg_topology,
651 phba->cfg_link_speed);
652 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
653 lpfc_set_loopback_flag(phba);
654 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
655 if (rc != MBX_SUCCESS) {
656 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
657 "0498 Adapter failed to init, mbxCmd x%x "
658 "INIT_LINK, mbxStatus x%x\n",
659 mb->mbxCommand, mb->mbxStatus);
660 /* Clear all interrupt enable conditions */
661 writel(0, phba->HCregaddr);
662 readl(phba->HCregaddr); /* flush */
663 /* Clear all pending interrupts */
664 writel(0xffffffff, phba->HAregaddr);
665 readl(phba->HAregaddr); /* flush */
666 phba->link_state = LPFC_HBA_ERROR;
667 if (rc != MBX_BUSY)
668 mempool_free(pmb, phba->mbox_mem_pool);
669 return -EIO;
670 }
James Smarte40a02c2010-02-26 14:13:54 -0500671 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart84d1b002010-02-12 14:42:33 -0500672
673 return 0;
674}
675
676/**
677 * lpfc_hba_down_link - this routine downs the FC link
678 *
679 * This routine will issue the DOWN_LINK mailbox command call.
680 * It is available to other drivers through the lpfc_hba data
681 * structure for use to stop the link.
682 *
683 * Return code
684 * 0 - success
685 * Any other value - error
686 **/
687int
688lpfc_hba_down_link(struct lpfc_hba *phba)
689{
690 LPFC_MBOXQ_t *pmb;
691 int rc;
692
693 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
694 if (!pmb) {
695 phba->link_state = LPFC_HBA_ERROR;
696 return -ENOMEM;
697 }
698
699 lpfc_printf_log(phba,
700 KERN_ERR, LOG_INIT,
701 "0491 Adapter Link is disabled.\n");
702 lpfc_down_link(phba, pmb);
703 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
704 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
705 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
706 lpfc_printf_log(phba,
707 KERN_ERR, LOG_INIT,
708 "2522 Adapter failed to issue DOWN_LINK"
709 " mbox command rc 0x%x\n", rc);
710
711 mempool_free(pmb, phba->mbox_mem_pool);
712 return -EIO;
713 }
714 return 0;
715}
716
717/**
James Smart3621a712009-04-06 18:47:14 -0400718 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400719 * @phba: pointer to lpfc HBA data structure.
720 *
721 * This routine will do LPFC uninitialization before the HBA is reset when
722 * bringing down the SLI Layer.
723 *
724 * Return codes
725 * 0 - success.
726 * Any other value - error.
727 **/
dea31012005-04-17 16:05:31 -0500728int
James Smart2e0fef82007-06-17 19:56:36 -0500729lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500730{
James Smart1b32f6a2008-02-08 18:49:39 -0500731 struct lpfc_vport **vports;
732 int i;
James Smart3772a992009-05-22 14:50:54 -0400733
734 if (phba->sli_rev <= LPFC_SLI_REV3) {
735 /* Disable interrupts */
736 writel(0, phba->HCregaddr);
737 readl(phba->HCregaddr); /* flush */
738 }
dea31012005-04-17 16:05:31 -0500739
James Smart1b32f6a2008-02-08 18:49:39 -0500740 if (phba->pport->load_flag & FC_UNLOADING)
741 lpfc_cleanup_discovery_resources(phba->pport);
742 else {
743 vports = lpfc_create_vport_work_array(phba);
744 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400745 for (i = 0; i <= phba->max_vports &&
746 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500747 lpfc_cleanup_discovery_resources(vports[i]);
748 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500749 }
750 return 0;
dea31012005-04-17 16:05:31 -0500751}
752
James Smarte59058c2008-08-24 21:49:00 -0400753/**
James Smart3772a992009-05-22 14:50:54 -0400754 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400755 * @phba: pointer to lpfc HBA data structure.
756 *
757 * This routine will do uninitialization after the HBA is reset when bring
758 * down the SLI Layer.
759 *
760 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200761 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400762 * Any other value - error.
763 **/
James Smart3772a992009-05-22 14:50:54 -0400764static int
765lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500766{
767 struct lpfc_sli *psli = &phba->sli;
768 struct lpfc_sli_ring *pring;
769 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500770 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500771 int i;
772
James Smart92d7f7b2007-06-17 19:56:38 -0500773 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
774 lpfc_sli_hbqbuf_free_all(phba);
775 else {
776 /* Cleanup preposted buffers on the ELS ring */
777 pring = &psli->ring[LPFC_ELS_RING];
778 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
779 list_del(&mp->list);
780 pring->postbufq_cnt--;
781 lpfc_mbuf_free(phba, mp->virt, mp->phys);
782 kfree(mp);
783 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500784 }
785
James Smart09372822008-01-11 01:52:54 -0500786 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500787 for (i = 0; i < psli->num_rings; i++) {
788 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500789
790 /* At this point in time the HBA is either reset or DOA. Either
791 * way, nothing should be on txcmplq as it will NEVER complete.
792 */
793 list_splice_init(&pring->txcmplq, &completions);
794 pring->txcmplq_cnt = 0;
795 spin_unlock_irq(&phba->hbalock);
796
James Smarta257bf92009-04-06 18:48:10 -0400797 /* Cancel all the IOCBs from the completions list */
798 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
799 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500800
Jamie Wellnitz41415862006-02-28 19:25:27 -0500801 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500802 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500803 }
James Smart09372822008-01-11 01:52:54 -0500804 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500805
806 return 0;
807}
James Smartda0436e2009-05-22 14:51:39 -0400808/**
809 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
810 * @phba: pointer to lpfc HBA data structure.
811 *
812 * This routine will do uninitialization after the HBA is reset when bring
813 * down the SLI Layer.
814 *
815 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200816 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400817 * Any other value - error.
818 **/
819static int
820lpfc_hba_down_post_s4(struct lpfc_hba *phba)
821{
822 struct lpfc_scsi_buf *psb, *psb_next;
823 LIST_HEAD(aborts);
824 int ret;
825 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500826 struct lpfc_sglq *sglq_entry = NULL;
827
James Smartda0436e2009-05-22 14:51:39 -0400828 ret = lpfc_hba_down_post_s3(phba);
829 if (ret)
830 return ret;
831 /* At this point in time the HBA is either reset or DOA. Either
832 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
833 * on the lpfc_sgl_list so that it can either be freed if the
834 * driver is unloading or reposted if the driver is restarting
835 * the port.
836 */
837 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
838 /* scsl_buf_list */
839 /* abts_sgl_list_lock required because worker thread uses this
840 * list.
841 */
842 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500843 list_for_each_entry(sglq_entry,
844 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
845 sglq_entry->state = SGL_FREED;
846
James Smartda0436e2009-05-22 14:51:39 -0400847 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
848 &phba->sli4_hba.lpfc_sgl_list);
849 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
850 /* abts_scsi_buf_list_lock required because worker thread uses this
851 * list.
852 */
853 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
854 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
855 &aborts);
856 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
857 spin_unlock_irq(&phba->hbalock);
858
859 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
860 psb->pCmd = NULL;
861 psb->status = IOSTAT_SUCCESS;
862 }
863 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
864 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
865 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
866 return 0;
867}
868
869/**
870 * lpfc_hba_down_post - Wrapper func for hba down post routine
871 * @phba: pointer to lpfc HBA data structure.
872 *
873 * This routine wraps the actual SLI3 or SLI4 routine for performing
874 * uninitialization after the HBA is reset when bring down the SLI Layer.
875 *
876 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200877 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400878 * Any other value - error.
879 **/
880int
881lpfc_hba_down_post(struct lpfc_hba *phba)
882{
883 return (*phba->lpfc_hba_down_post)(phba);
884}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500885
James Smarte59058c2008-08-24 21:49:00 -0400886/**
James Smart3621a712009-04-06 18:47:14 -0400887 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400888 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
889 *
890 * This is the HBA-timer timeout handler registered to the lpfc driver. When
891 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
892 * work-port-events bitmap and the worker thread is notified. This timeout
893 * event will be used by the worker thread to invoke the actual timeout
894 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
895 * be performed in the timeout handler and the HBA timeout event bit shall
896 * be cleared by the worker thread after it has taken the event bitmap out.
897 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100898static void
James Smart858c9f62007-06-17 19:56:39 -0500899lpfc_hb_timeout(unsigned long ptr)
900{
901 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400902 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500903 unsigned long iflag;
904
905 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400906
907 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500908 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400909 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
910 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500911 phba->pport->work_port_events |= WORKER_HB_TMO;
912 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
913
James Smart93996272008-08-24 21:50:30 -0400914 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400915 if (!tmo_posted)
916 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500917 return;
918}
919
James Smarte59058c2008-08-24 21:49:00 -0400920/**
James Smart3621a712009-04-06 18:47:14 -0400921 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400922 * @phba: pointer to lpfc hba data structure.
923 * @pmboxq: pointer to the driver internal queue element for mailbox command.
924 *
925 * This is the callback function to the lpfc heart-beat mailbox command.
926 * If configured, the lpfc driver issues the heart-beat mailbox command to
927 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
928 * heart-beat mailbox command is issued, the driver shall set up heart-beat
929 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
930 * heart-beat outstanding state. Once the mailbox command comes back and
931 * no error conditions detected, the heart-beat mailbox command timer is
932 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
933 * state is cleared for the next heart-beat. If the timer expired with the
934 * heart-beat outstanding state set, the driver will put the HBA offline.
935 **/
James Smart858c9f62007-06-17 19:56:39 -0500936static void
937lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
938{
939 unsigned long drvr_flag;
940
941 spin_lock_irqsave(&phba->hbalock, drvr_flag);
942 phba->hb_outstanding = 0;
943 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
944
James Smart93996272008-08-24 21:50:30 -0400945 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500946 mempool_free(pmboxq, phba->mbox_mem_pool);
947 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
948 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400949 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500950 mod_timer(&phba->hb_tmofunc,
951 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
952 return;
953}
954
James Smarte59058c2008-08-24 21:49:00 -0400955/**
James Smart3621a712009-04-06 18:47:14 -0400956 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400957 * @phba: pointer to lpfc hba data structure.
958 *
959 * This is the actual HBA-timer timeout handler to be invoked by the worker
960 * thread whenever the HBA timer fired and HBA-timeout event posted. This
961 * handler performs any periodic operations needed for the device. If such
962 * periodic event has already been attended to either in the interrupt handler
963 * or by processing slow-ring or fast-ring events within the HBA-timer
964 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
965 * the timer for the next timeout period. If lpfc heart-beat mailbox command
966 * is configured and there is no heart-beat mailbox command outstanding, a
967 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
968 * has been a heart-beat mailbox command outstanding, the HBA shall be put
969 * to offline.
970 **/
James Smart858c9f62007-06-17 19:56:39 -0500971void
972lpfc_hb_timeout_handler(struct lpfc_hba *phba)
973{
James Smart45ed1192009-10-02 15:17:02 -0400974 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -0500975 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500976 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -0400977 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -0500978 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500979 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500980
James Smart45ed1192009-10-02 15:17:02 -0400981 vports = lpfc_create_vport_work_array(phba);
982 if (vports != NULL)
983 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
984 lpfc_rcv_seq_check_edtov(vports[i]);
985 lpfc_destroy_vport_work_array(phba, vports);
986
James Smart858c9f62007-06-17 19:56:39 -0500987 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400988 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500989 (phba->pport->fc_flag & FC_OFFLINE_MODE))
990 return;
991
992 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -0500993
994 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
995 jiffies)) {
996 spin_unlock_irq(&phba->pport->work_port_lock);
997 if (!phba->hb_outstanding)
998 mod_timer(&phba->hb_tmofunc,
999 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1000 else
1001 mod_timer(&phba->hb_tmofunc,
1002 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1003 return;
1004 }
1005 spin_unlock_irq(&phba->pport->work_port_lock);
1006
James Smart0ff10d42008-01-11 01:52:36 -05001007 if (phba->elsbuf_cnt &&
1008 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1009 spin_lock_irq(&phba->hbalock);
1010 list_splice_init(&phba->elsbuf, &completions);
1011 phba->elsbuf_cnt = 0;
1012 phba->elsbuf_prev_cnt = 0;
1013 spin_unlock_irq(&phba->hbalock);
1014
1015 while (!list_empty(&completions)) {
1016 list_remove_head(&completions, buf_ptr,
1017 struct lpfc_dmabuf, list);
1018 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1019 kfree(buf_ptr);
1020 }
1021 }
1022 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1023
James Smart858c9f62007-06-17 19:56:39 -05001024 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001025 if (phba->cfg_enable_hba_heartbeat) {
1026 if (!phba->hb_outstanding) {
1027 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
1028 if (!pmboxq) {
1029 mod_timer(&phba->hb_tmofunc,
1030 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1031 return;
1032 }
1033
1034 lpfc_heart_beat(phba, pmboxq);
1035 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1036 pmboxq->vport = phba->pport;
1037 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
1038
1039 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
1040 mempool_free(pmboxq, phba->mbox_mem_pool);
1041 mod_timer(&phba->hb_tmofunc,
1042 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1043 return;
1044 }
James Smart858c9f62007-06-17 19:56:39 -05001045 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001046 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1047 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -05001048 return;
James Smart13815c82008-01-11 01:52:48 -05001049 } else {
1050 /*
1051 * If heart beat timeout called with hb_outstanding set
1052 * we need to take the HBA offline.
1053 */
1054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1055 "0459 Adapter heartbeat failure, "
1056 "taking this port offline.\n");
1057
1058 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001059 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart13815c82008-01-11 01:52:48 -05001060 spin_unlock_irq(&phba->hbalock);
1061
1062 lpfc_offline_prep(phba);
1063 lpfc_offline(phba);
1064 lpfc_unblock_mgmt_io(phba);
1065 phba->link_state = LPFC_HBA_ERROR;
1066 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -05001067 }
James Smart858c9f62007-06-17 19:56:39 -05001068 }
1069}
1070
James Smarte59058c2008-08-24 21:49:00 -04001071/**
James Smart3621a712009-04-06 18:47:14 -04001072 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001073 * @phba: pointer to lpfc hba data structure.
1074 *
1075 * This routine is called to bring the HBA offline when HBA hardware error
1076 * other than Port Error 6 has been detected.
1077 **/
James Smart09372822008-01-11 01:52:54 -05001078static void
1079lpfc_offline_eratt(struct lpfc_hba *phba)
1080{
1081 struct lpfc_sli *psli = &phba->sli;
1082
1083 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001084 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001085 spin_unlock_irq(&phba->hbalock);
1086 lpfc_offline_prep(phba);
1087
1088 lpfc_offline(phba);
1089 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001090 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001091 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001092 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001093 lpfc_hba_down_post(phba);
1094 lpfc_sli_brdready(phba, HS_MBRDY);
1095 lpfc_unblock_mgmt_io(phba);
1096 phba->link_state = LPFC_HBA_ERROR;
1097 return;
1098}
1099
James Smarte59058c2008-08-24 21:49:00 -04001100/**
James Smartda0436e2009-05-22 14:51:39 -04001101 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1102 * @phba: pointer to lpfc hba data structure.
1103 *
1104 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1105 * other than Port Error 6 has been detected.
1106 **/
1107static void
1108lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1109{
1110 lpfc_offline_prep(phba);
1111 lpfc_offline(phba);
1112 lpfc_sli4_brdreset(phba);
1113 lpfc_hba_down_post(phba);
1114 lpfc_sli4_post_status_check(phba);
1115 lpfc_unblock_mgmt_io(phba);
1116 phba->link_state = LPFC_HBA_ERROR;
1117}
1118
1119/**
James Smarta257bf92009-04-06 18:48:10 -04001120 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1121 * @phba: pointer to lpfc hba data structure.
1122 *
1123 * This routine is invoked to handle the deferred HBA hardware error
1124 * conditions. This type of error is indicated by HBA by setting ER1
1125 * and another ER bit in the host status register. The driver will
1126 * wait until the ER1 bit clears before handling the error condition.
1127 **/
1128static void
1129lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1130{
1131 uint32_t old_host_status = phba->work_hs;
1132 struct lpfc_sli_ring *pring;
1133 struct lpfc_sli *psli = &phba->sli;
1134
James Smartf4b4c682009-05-22 14:53:12 -04001135 /* If the pci channel is offline, ignore possible errors,
1136 * since we cannot communicate with the pci card anyway.
1137 */
1138 if (pci_channel_offline(phba->pcidev)) {
1139 spin_lock_irq(&phba->hbalock);
1140 phba->hba_flag &= ~DEFER_ERATT;
1141 spin_unlock_irq(&phba->hbalock);
1142 return;
1143 }
1144
James Smarta257bf92009-04-06 18:48:10 -04001145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1146 "0479 Deferred Adapter Hardware Error "
1147 "Data: x%x x%x x%x\n",
1148 phba->work_hs,
1149 phba->work_status[0], phba->work_status[1]);
1150
1151 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001152 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001153 spin_unlock_irq(&phba->hbalock);
1154
1155
1156 /*
1157 * Firmware stops when it triggred erratt. That could cause the I/Os
1158 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1159 * SCSI layer retry it after re-establishing link.
1160 */
1161 pring = &psli->ring[psli->fcp_ring];
1162 lpfc_sli_abort_iocb_ring(phba, pring);
1163
1164 /*
1165 * There was a firmware error. Take the hba offline and then
1166 * attempt to restart it.
1167 */
1168 lpfc_offline_prep(phba);
1169 lpfc_offline(phba);
1170
1171 /* Wait for the ER1 bit to clear.*/
1172 while (phba->work_hs & HS_FFER1) {
1173 msleep(100);
1174 phba->work_hs = readl(phba->HSregaddr);
1175 /* If driver is unloading let the worker thread continue */
1176 if (phba->pport->load_flag & FC_UNLOADING) {
1177 phba->work_hs = 0;
1178 break;
1179 }
1180 }
1181
1182 /*
1183 * This is to ptrotect against a race condition in which
1184 * first write to the host attention register clear the
1185 * host status register.
1186 */
1187 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1188 phba->work_hs = old_host_status & ~HS_FFER1;
1189
James Smart3772a992009-05-22 14:50:54 -04001190 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001191 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001192 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001193 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1194 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1195}
1196
James Smart3772a992009-05-22 14:50:54 -04001197static void
1198lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1199{
1200 struct lpfc_board_event_header board_event;
1201 struct Scsi_Host *shost;
1202
1203 board_event.event_type = FC_REG_BOARD_EVENT;
1204 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1205 shost = lpfc_shost_from_vport(phba->pport);
1206 fc_host_post_vendor_event(shost, fc_get_event_number(),
1207 sizeof(board_event),
1208 (char *) &board_event,
1209 LPFC_NL_VENDOR_ID);
1210}
1211
James Smarta257bf92009-04-06 18:48:10 -04001212/**
James Smart3772a992009-05-22 14:50:54 -04001213 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001214 * @phba: pointer to lpfc hba data structure.
1215 *
1216 * This routine is invoked to handle the following HBA hardware error
1217 * conditions:
1218 * 1 - HBA error attention interrupt
1219 * 2 - DMA ring index out of range
1220 * 3 - Mailbox command came back as unknown
1221 **/
James Smart3772a992009-05-22 14:50:54 -04001222static void
1223lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001224{
James Smart2e0fef82007-06-17 19:56:36 -05001225 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001226 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001227 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001228 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001229 unsigned long temperature;
1230 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001231 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001232
Linas Vepstas8d63f372007-02-14 14:28:36 -06001233 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001234 * since we cannot communicate with the pci card anyway.
1235 */
1236 if (pci_channel_offline(phba->pcidev)) {
1237 spin_lock_irq(&phba->hbalock);
1238 phba->hba_flag &= ~DEFER_ERATT;
1239 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001240 return;
James Smart3772a992009-05-22 14:50:54 -04001241 }
1242
James Smart13815c82008-01-11 01:52:48 -05001243 /* If resets are disabled then leave the HBA alone and return */
1244 if (!phba->cfg_enable_hba_reset)
1245 return;
dea31012005-04-17 16:05:31 -05001246
James Smartea2151b2008-09-07 11:52:10 -04001247 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001248 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001249
James Smarta257bf92009-04-06 18:48:10 -04001250 if (phba->hba_flag & DEFER_ERATT)
1251 lpfc_handle_deferred_eratt(phba);
1252
James Smart97eab632008-04-07 10:16:05 -04001253 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -05001254 /* Re-establishing Link */
1255 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001256 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -05001257 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001258 phba->work_hs,
dea31012005-04-17 16:05:31 -05001259 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001260
James Smart92d7f7b2007-06-17 19:56:38 -05001261 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001262 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001263 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001264
1265 /*
1266 * Firmware stops when it triggled erratt with HS_FFER6.
1267 * That could cause the I/Os dropped by the firmware.
1268 * Error iocb (I/O) on txcmplq and let the SCSI layer
1269 * retry it after re-establishing link.
1270 */
1271 pring = &psli->ring[psli->fcp_ring];
1272 lpfc_sli_abort_iocb_ring(phba, pring);
1273
dea31012005-04-17 16:05:31 -05001274 /*
1275 * There was a firmware error. Take the hba offline and then
1276 * attempt to restart it.
1277 */
James Smart46fa3112007-04-25 09:51:45 -04001278 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001279 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001280 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001281 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001282 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001283 return;
1284 }
James Smart46fa3112007-04-25 09:51:45 -04001285 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001286 } else if (phba->work_hs & HS_CRIT_TEMP) {
1287 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1288 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1289 temp_event_data.event_code = LPFC_CRIT_TEMP;
1290 temp_event_data.data = (uint32_t)temperature;
1291
1292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001293 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001294 "(%ld), taking this port offline "
1295 "Data: x%x x%x x%x\n",
1296 temperature, phba->work_hs,
1297 phba->work_status[0], phba->work_status[1]);
1298
1299 shost = lpfc_shost_from_vport(phba->pport);
1300 fc_host_post_vendor_event(shost, fc_get_event_number(),
1301 sizeof(temp_event_data),
1302 (char *) &temp_event_data,
1303 SCSI_NL_VID_TYPE_PCI
1304 | PCI_VENDOR_ID_EMULEX);
1305
James Smart7af67052007-10-27 13:38:11 -04001306 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001307 phba->over_temp_state = HBA_OVER_TEMP;
1308 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001309 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001310
dea31012005-04-17 16:05:31 -05001311 } else {
1312 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001313 * failure is a value other than FFER6. Do not call the offline
1314 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001315 */
1316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001317 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001318 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001319 phba->work_hs,
dea31012005-04-17 16:05:31 -05001320 phba->work_status[0], phba->work_status[1]);
1321
James Smartd2873e42006-08-18 17:46:43 -04001322 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001323 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001324 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001325 sizeof(event_data), (char *) &event_data,
1326 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1327
James Smart09372822008-01-11 01:52:54 -05001328 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001329 }
James Smart93996272008-08-24 21:50:30 -04001330 return;
dea31012005-04-17 16:05:31 -05001331}
1332
James Smarte59058c2008-08-24 21:49:00 -04001333/**
James Smartda0436e2009-05-22 14:51:39 -04001334 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1335 * @phba: pointer to lpfc hba data structure.
1336 *
1337 * This routine is invoked to handle the SLI4 HBA hardware error attention
1338 * conditions.
1339 **/
1340static void
1341lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1342{
1343 struct lpfc_vport *vport = phba->pport;
1344 uint32_t event_data;
1345 struct Scsi_Host *shost;
1346
1347 /* If the pci channel is offline, ignore possible errors, since
1348 * we cannot communicate with the pci card anyway.
1349 */
1350 if (pci_channel_offline(phba->pcidev))
1351 return;
1352 /* If resets are disabled then leave the HBA alone and return */
1353 if (!phba->cfg_enable_hba_reset)
1354 return;
1355
1356 /* Send an internal error event to mgmt application */
1357 lpfc_board_errevt_to_mgmt(phba);
1358
1359 /* For now, the actual action for SLI4 device handling is not
1360 * specified yet, just treated it as adaptor hardware failure
1361 */
1362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1363 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1364 phba->work_status[0], phba->work_status[1]);
1365
1366 event_data = FC_REG_DUMP_EVENT;
1367 shost = lpfc_shost_from_vport(vport);
1368 fc_host_post_vendor_event(shost, fc_get_event_number(),
1369 sizeof(event_data), (char *) &event_data,
1370 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1371
1372 lpfc_sli4_offline_eratt(phba);
1373}
1374
1375/**
1376 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1377 * @phba: pointer to lpfc HBA data structure.
1378 *
1379 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1380 * routine from the API jump table function pointer from the lpfc_hba struct.
1381 *
1382 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001383 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001384 * Any other value - error.
1385 **/
1386void
1387lpfc_handle_eratt(struct lpfc_hba *phba)
1388{
1389 (*phba->lpfc_handle_eratt)(phba);
1390}
1391
1392/**
James Smart3621a712009-04-06 18:47:14 -04001393 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001394 * @phba: pointer to lpfc hba data structure.
1395 *
1396 * This routine is invoked from the worker thread to handle a HBA host
1397 * attention link event.
1398 **/
dea31012005-04-17 16:05:31 -05001399void
James Smart2e0fef82007-06-17 19:56:36 -05001400lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001401{
James Smart2e0fef82007-06-17 19:56:36 -05001402 struct lpfc_vport *vport = phba->pport;
1403 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001404 LPFC_MBOXQ_t *pmb;
1405 volatile uint32_t control;
1406 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001407 int rc = 0;
dea31012005-04-17 16:05:31 -05001408
1409 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001410 if (!pmb) {
1411 rc = 1;
dea31012005-04-17 16:05:31 -05001412 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001413 }
dea31012005-04-17 16:05:31 -05001414
1415 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001416 if (!mp) {
1417 rc = 2;
dea31012005-04-17 16:05:31 -05001418 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001419 }
dea31012005-04-17 16:05:31 -05001420
1421 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001422 if (!mp->virt) {
1423 rc = 3;
dea31012005-04-17 16:05:31 -05001424 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001425 }
dea31012005-04-17 16:05:31 -05001426
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001427 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001428 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001429
1430 psli->slistat.link_event++;
1431 lpfc_read_la(phba, pmb, mp);
1432 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001433 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001434 /* Block ELS IOCBs until we have processed this mbox command */
1435 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001436 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001437 if (rc == MBX_NOT_FINISHED) {
1438 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001439 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001440 }
dea31012005-04-17 16:05:31 -05001441
1442 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001443 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001444 writel(HA_LATT, phba->HAregaddr);
1445 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001446 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001447
1448 return;
1449
James Smart14691152006-12-02 13:34:28 -05001450lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001451 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001452 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001453lpfc_handle_latt_free_mp:
1454 kfree(mp);
1455lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001456 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001457lpfc_handle_latt_err_exit:
1458 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001459 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001460 psli->sli_flag |= LPFC_PROCESS_LA;
1461 control = readl(phba->HCregaddr);
1462 control |= HC_LAINT_ENA;
1463 writel(control, phba->HCregaddr);
1464 readl(phba->HCregaddr); /* flush */
1465
1466 /* Clear Link Attention in HA REG */
1467 writel(HA_LATT, phba->HAregaddr);
1468 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001469 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001470 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001471 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001472
James Smart09372822008-01-11 01:52:54 -05001473 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1474 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001475
1476 return;
1477}
1478
James Smarte59058c2008-08-24 21:49:00 -04001479/**
James Smart3621a712009-04-06 18:47:14 -04001480 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001481 * @phba: pointer to lpfc hba data structure.
1482 * @vpd: pointer to the vital product data.
1483 * @len: length of the vital product data in bytes.
1484 *
1485 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1486 * an array of characters. In this routine, the ModelName, ProgramType, and
1487 * ModelDesc, etc. fields of the phba data structure will be populated.
1488 *
1489 * Return codes
1490 * 0 - pointer to the VPD passed in is NULL
1491 * 1 - success
1492 **/
James Smart3772a992009-05-22 14:50:54 -04001493int
James Smart2e0fef82007-06-17 19:56:36 -05001494lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001495{
1496 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001497 int Length;
dea31012005-04-17 16:05:31 -05001498 int i, j;
1499 int finished = 0;
1500 int index = 0;
1501
1502 if (!vpd)
1503 return 0;
1504
1505 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001506 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001507 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001508 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1509 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001510 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001511 switch (vpd[index]) {
1512 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001513 case 0x91:
dea31012005-04-17 16:05:31 -05001514 index += 1;
1515 lenlo = vpd[index];
1516 index += 1;
1517 lenhi = vpd[index];
1518 index += 1;
1519 i = ((((unsigned short)lenhi) << 8) + lenlo);
1520 index += i;
1521 break;
1522 case 0x90:
1523 index += 1;
1524 lenlo = vpd[index];
1525 index += 1;
1526 lenhi = vpd[index];
1527 index += 1;
1528 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001529 if (Length > len - index)
1530 Length = len - index;
dea31012005-04-17 16:05:31 -05001531 while (Length > 0) {
1532 /* Look for Serial Number */
1533 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1534 index += 2;
1535 i = vpd[index];
1536 index += 1;
1537 j = 0;
1538 Length -= (3+i);
1539 while(i--) {
1540 phba->SerialNumber[j++] = vpd[index++];
1541 if (j == 31)
1542 break;
1543 }
1544 phba->SerialNumber[j] = 0;
1545 continue;
1546 }
1547 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1548 phba->vpd_flag |= VPD_MODEL_DESC;
1549 index += 2;
1550 i = vpd[index];
1551 index += 1;
1552 j = 0;
1553 Length -= (3+i);
1554 while(i--) {
1555 phba->ModelDesc[j++] = vpd[index++];
1556 if (j == 255)
1557 break;
1558 }
1559 phba->ModelDesc[j] = 0;
1560 continue;
1561 }
1562 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1563 phba->vpd_flag |= VPD_MODEL_NAME;
1564 index += 2;
1565 i = vpd[index];
1566 index += 1;
1567 j = 0;
1568 Length -= (3+i);
1569 while(i--) {
1570 phba->ModelName[j++] = vpd[index++];
1571 if (j == 79)
1572 break;
1573 }
1574 phba->ModelName[j] = 0;
1575 continue;
1576 }
1577 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1578 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1579 index += 2;
1580 i = vpd[index];
1581 index += 1;
1582 j = 0;
1583 Length -= (3+i);
1584 while(i--) {
1585 phba->ProgramType[j++] = vpd[index++];
1586 if (j == 255)
1587 break;
1588 }
1589 phba->ProgramType[j] = 0;
1590 continue;
1591 }
1592 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1593 phba->vpd_flag |= VPD_PORT;
1594 index += 2;
1595 i = vpd[index];
1596 index += 1;
1597 j = 0;
1598 Length -= (3+i);
1599 while(i--) {
1600 phba->Port[j++] = vpd[index++];
1601 if (j == 19)
1602 break;
1603 }
1604 phba->Port[j] = 0;
1605 continue;
1606 }
1607 else {
1608 index += 2;
1609 i = vpd[index];
1610 index += 1;
1611 index += i;
1612 Length -= (3 + i);
1613 }
1614 }
1615 finished = 0;
1616 break;
1617 case 0x78:
1618 finished = 1;
1619 break;
1620 default:
1621 index ++;
1622 break;
1623 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001624 }
dea31012005-04-17 16:05:31 -05001625
1626 return(1);
1627}
1628
James Smarte59058c2008-08-24 21:49:00 -04001629/**
James Smart3621a712009-04-06 18:47:14 -04001630 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001631 * @phba: pointer to lpfc hba data structure.
1632 * @mdp: pointer to the data structure to hold the derived model name.
1633 * @descp: pointer to the data structure to hold the derived description.
1634 *
1635 * This routine retrieves HBA's description based on its registered PCI device
1636 * ID. The @descp passed into this function points to an array of 256 chars. It
1637 * shall be returned with the model name, maximum speed, and the host bus type.
1638 * The @mdp passed into this function points to an array of 80 chars. When the
1639 * function returns, the @mdp will be filled with the model name.
1640 **/
dea31012005-04-17 16:05:31 -05001641static void
James Smart2e0fef82007-06-17 19:56:36 -05001642lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001643{
1644 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001645 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001646 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001647 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001648 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001649 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001650 char *name;
1651 char *bus;
1652 char *function;
1653 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001654
1655 if (mdp && mdp[0] != '\0'
1656 && descp && descp[0] != '\0')
1657 return;
1658
1659 if (phba->lmt & LMT_10Gb)
1660 max_speed = 10;
1661 else if (phba->lmt & LMT_8Gb)
1662 max_speed = 8;
1663 else if (phba->lmt & LMT_4Gb)
1664 max_speed = 4;
1665 else if (phba->lmt & LMT_2Gb)
1666 max_speed = 2;
1667 else
1668 max_speed = 1;
dea31012005-04-17 16:05:31 -05001669
1670 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001671
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001672 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001673 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001674 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001675 break;
dea31012005-04-17 16:05:31 -05001676 case PCI_DEVICE_ID_SUPERFLY:
1677 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001678 m = (typeof(m)){"LP7000", "PCI",
1679 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001680 else
James Smarta747c9c2009-11-18 15:41:10 -05001681 m = (typeof(m)){"LP7000E", "PCI",
1682 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001683 break;
1684 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001685 m = (typeof(m)){"LP8000", "PCI",
1686 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001687 break;
1688 case PCI_DEVICE_ID_CENTAUR:
1689 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001690 m = (typeof(m)){"LP9002", "PCI",
1691 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001692 else
James Smarta747c9c2009-11-18 15:41:10 -05001693 m = (typeof(m)){"LP9000", "PCI",
1694 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001695 break;
1696 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001697 m = (typeof(m)){"LP952", "PCI",
1698 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001699 break;
1700 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001701 m = (typeof(m)){"LP9802", "PCI-X",
1702 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001703 break;
1704 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001705 m = (typeof(m)){"LP10000", "PCI-X",
1706 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001707 break;
1708 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001709 m = (typeof(m)){"LPX1000", "PCI-X",
1710 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001711 break;
1712 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001713 m = (typeof(m)){"LP982", "PCI-X",
1714 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001715 break;
1716 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001717 m = (typeof(m)){"LP1050", "PCI-X",
1718 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001719 break;
1720 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001721 m = (typeof(m)){"LP11000", "PCI-X2",
1722 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001723 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001724 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001725 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1726 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001727 break;
1728 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001729 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1730 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001731 break;
1732 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001733 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001734 break;
1735 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001736 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001737 break;
1738 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001739 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001740 break;
dea31012005-04-17 16:05:31 -05001741 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001742 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001743 break;
1744 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001745 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001746 break;
1747 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001748 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001749 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001750 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001751 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001752 break;
1753 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001754 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001755 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001756 break;
dea31012005-04-17 16:05:31 -05001757 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001758 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001759 break;
1760 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001761 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001762 break;
1763 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001764 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001765 break;
1766 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001767 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001768 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001769 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001770 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001771 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001772 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001773 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001774 break;
James Smartb87eab32007-04-25 09:53:28 -04001775 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001776 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001777 break;
1778 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001779 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001780 break;
1781 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001782 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001783 break;
1784 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001785 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001786 break;
1787 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001788 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001789 break;
1790 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001791 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001792 break;
James Smart84774a42008-08-24 21:50:06 -04001793 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001794 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001795 GE = 1;
1796 break;
1797 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001798 m = (typeof(m)){"LPev12000", "PCIe IOV",
1799 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001800 break;
1801 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001802 m = (typeof(m)){"LPev12000", "PCIe IOV",
1803 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001804 break;
1805 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001806 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1807 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001808 break;
James Smartda0436e2009-05-22 14:51:39 -04001809 case PCI_DEVICE_ID_TIGERSHARK:
1810 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001811 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04001812 break;
James Smarta747c9c2009-11-18 15:41:10 -05001813 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04001814 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001815 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1816 break;
1817 case PCI_DEVICE_ID_FALCON:
1818 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1819 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04001820 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001821 default:
James Smarta747c9c2009-11-18 15:41:10 -05001822 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001823 break;
dea31012005-04-17 16:05:31 -05001824 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001825
1826 if (mdp && mdp[0] == '\0')
1827 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001828 /* oneConnect hba requires special processing, they are all initiators
1829 * and we put the port number on the end
1830 */
1831 if (descp && descp[0] == '\0') {
1832 if (oneConnect)
1833 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05001834 "Emulex OneConnect %s, %s Initiator, Port %s",
1835 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04001836 phba->Port);
1837 else
1838 snprintf(descp, 255,
1839 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05001840 m.name, max_speed, (GE) ? "GE" : "Gb",
1841 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04001842 }
dea31012005-04-17 16:05:31 -05001843}
1844
James Smarte59058c2008-08-24 21:49:00 -04001845/**
James Smart3621a712009-04-06 18:47:14 -04001846 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001847 * @phba: pointer to lpfc hba data structure.
1848 * @pring: pointer to a IOCB ring.
1849 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1850 *
1851 * This routine posts a given number of IOCBs with the associated DMA buffer
1852 * descriptors specified by the cnt argument to the given IOCB ring.
1853 *
1854 * Return codes
1855 * The number of IOCBs NOT able to be posted to the IOCB ring.
1856 **/
dea31012005-04-17 16:05:31 -05001857int
James Smart495a7142008-06-14 22:52:59 -04001858lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001859{
1860 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001861 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001862 struct lpfc_dmabuf *mp1, *mp2;
1863
1864 cnt += pring->missbufcnt;
1865
1866 /* While there are buffers to post */
1867 while (cnt > 0) {
1868 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001869 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001870 if (iocb == NULL) {
1871 pring->missbufcnt = cnt;
1872 return cnt;
1873 }
dea31012005-04-17 16:05:31 -05001874 icmd = &iocb->iocb;
1875
1876 /* 2 buffers can be posted per command */
1877 /* Allocate buffer to post */
1878 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1879 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001880 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1881 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001882 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001883 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001884 pring->missbufcnt = cnt;
1885 return cnt;
1886 }
1887
1888 INIT_LIST_HEAD(&mp1->list);
1889 /* Allocate buffer to post */
1890 if (cnt > 1) {
1891 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1892 if (mp2)
1893 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1894 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001895 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001896 kfree(mp2);
dea31012005-04-17 16:05:31 -05001897 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1898 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001899 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001900 pring->missbufcnt = cnt;
1901 return cnt;
1902 }
1903
1904 INIT_LIST_HEAD(&mp2->list);
1905 } else {
1906 mp2 = NULL;
1907 }
1908
1909 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1910 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1911 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1912 icmd->ulpBdeCount = 1;
1913 cnt--;
1914 if (mp2) {
1915 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1916 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1917 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1918 cnt--;
1919 icmd->ulpBdeCount = 2;
1920 }
1921
1922 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1923 icmd->ulpLe = 1;
1924
James Smart3772a992009-05-22 14:50:54 -04001925 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1926 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001927 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1928 kfree(mp1);
1929 cnt++;
1930 if (mp2) {
1931 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1932 kfree(mp2);
1933 cnt++;
1934 }
James Bottomley604a3e32005-10-29 10:28:33 -05001935 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001936 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001937 return cnt;
1938 }
dea31012005-04-17 16:05:31 -05001939 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001940 if (mp2)
dea31012005-04-17 16:05:31 -05001941 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001942 }
1943 pring->missbufcnt = 0;
1944 return 0;
1945}
1946
James Smarte59058c2008-08-24 21:49:00 -04001947/**
James Smart3621a712009-04-06 18:47:14 -04001948 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001949 * @phba: pointer to lpfc hba data structure.
1950 *
1951 * This routine posts initial receive IOCB buffers to the ELS ring. The
1952 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1953 * set to 64 IOCBs.
1954 *
1955 * Return codes
1956 * 0 - success (currently always success)
1957 **/
dea31012005-04-17 16:05:31 -05001958static int
James Smart2e0fef82007-06-17 19:56:36 -05001959lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001960{
1961 struct lpfc_sli *psli = &phba->sli;
1962
1963 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001964 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001965 /* Ring 2 - FCP no buffers needed */
1966
1967 return 0;
1968}
1969
1970#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1971
James Smarte59058c2008-08-24 21:49:00 -04001972/**
James Smart3621a712009-04-06 18:47:14 -04001973 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001974 * @HashResultPointer: pointer to an array as hash table.
1975 *
1976 * This routine sets up the initial values to the array of hash table entries
1977 * for the LC HBAs.
1978 **/
dea31012005-04-17 16:05:31 -05001979static void
1980lpfc_sha_init(uint32_t * HashResultPointer)
1981{
1982 HashResultPointer[0] = 0x67452301;
1983 HashResultPointer[1] = 0xEFCDAB89;
1984 HashResultPointer[2] = 0x98BADCFE;
1985 HashResultPointer[3] = 0x10325476;
1986 HashResultPointer[4] = 0xC3D2E1F0;
1987}
1988
James Smarte59058c2008-08-24 21:49:00 -04001989/**
James Smart3621a712009-04-06 18:47:14 -04001990 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001991 * @HashResultPointer: pointer to an initial/result hash table.
1992 * @HashWorkingPointer: pointer to an working hash table.
1993 *
1994 * This routine iterates an initial hash table pointed by @HashResultPointer
1995 * with the values from the working hash table pointeed by @HashWorkingPointer.
1996 * The results are putting back to the initial hash table, returned through
1997 * the @HashResultPointer as the result hash table.
1998 **/
dea31012005-04-17 16:05:31 -05001999static void
2000lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2001{
2002 int t;
2003 uint32_t TEMP;
2004 uint32_t A, B, C, D, E;
2005 t = 16;
2006 do {
2007 HashWorkingPointer[t] =
2008 S(1,
2009 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2010 8] ^
2011 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2012 } while (++t <= 79);
2013 t = 0;
2014 A = HashResultPointer[0];
2015 B = HashResultPointer[1];
2016 C = HashResultPointer[2];
2017 D = HashResultPointer[3];
2018 E = HashResultPointer[4];
2019
2020 do {
2021 if (t < 20) {
2022 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2023 } else if (t < 40) {
2024 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2025 } else if (t < 60) {
2026 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2027 } else {
2028 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2029 }
2030 TEMP += S(5, A) + E + HashWorkingPointer[t];
2031 E = D;
2032 D = C;
2033 C = S(30, B);
2034 B = A;
2035 A = TEMP;
2036 } while (++t <= 79);
2037
2038 HashResultPointer[0] += A;
2039 HashResultPointer[1] += B;
2040 HashResultPointer[2] += C;
2041 HashResultPointer[3] += D;
2042 HashResultPointer[4] += E;
2043
2044}
2045
James Smarte59058c2008-08-24 21:49:00 -04002046/**
James Smart3621a712009-04-06 18:47:14 -04002047 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002048 * @RandomChallenge: pointer to the entry of host challenge random number array.
2049 * @HashWorking: pointer to the entry of the working hash array.
2050 *
2051 * This routine calculates the working hash array referred by @HashWorking
2052 * from the challenge random numbers associated with the host, referred by
2053 * @RandomChallenge. The result is put into the entry of the working hash
2054 * array and returned by reference through @HashWorking.
2055 **/
dea31012005-04-17 16:05:31 -05002056static void
2057lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2058{
2059 *HashWorking = (*RandomChallenge ^ *HashWorking);
2060}
2061
James Smarte59058c2008-08-24 21:49:00 -04002062/**
James Smart3621a712009-04-06 18:47:14 -04002063 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002064 * @phba: pointer to lpfc hba data structure.
2065 * @hbainit: pointer to an array of unsigned 32-bit integers.
2066 *
2067 * This routine performs the special handling for LC HBA initialization.
2068 **/
dea31012005-04-17 16:05:31 -05002069void
2070lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2071{
2072 int t;
2073 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002074 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002075
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002076 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002077 if (!HashWorking)
2078 return;
2079
dea31012005-04-17 16:05:31 -05002080 HashWorking[0] = HashWorking[78] = *pwwnn++;
2081 HashWorking[1] = HashWorking[79] = *pwwnn;
2082
2083 for (t = 0; t < 7; t++)
2084 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2085
2086 lpfc_sha_init(hbainit);
2087 lpfc_sha_iterate(hbainit, HashWorking);
2088 kfree(HashWorking);
2089}
2090
James Smarte59058c2008-08-24 21:49:00 -04002091/**
James Smart3621a712009-04-06 18:47:14 -04002092 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002093 * @vport: pointer to a virtual N_Port data structure.
2094 *
2095 * This routine performs the necessary cleanups before deleting the @vport.
2096 * It invokes the discovery state machine to perform necessary state
2097 * transitions and to release the ndlps associated with the @vport. Note,
2098 * the physical port is treated as @vport 0.
2099 **/
James Smart87af33f2007-10-27 13:37:43 -04002100void
James Smart2e0fef82007-06-17 19:56:36 -05002101lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002102{
James Smart87af33f2007-10-27 13:37:43 -04002103 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002104 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002105 int i = 0;
dea31012005-04-17 16:05:31 -05002106
James Smart87af33f2007-10-27 13:37:43 -04002107 if (phba->link_state > LPFC_LINK_DOWN)
2108 lpfc_port_link_failure(vport);
2109
2110 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002111 if (!NLP_CHK_NODE_ACT(ndlp)) {
2112 ndlp = lpfc_enable_node(vport, ndlp,
2113 NLP_STE_UNUSED_NODE);
2114 if (!ndlp)
2115 continue;
2116 spin_lock_irq(&phba->ndlp_lock);
2117 NLP_SET_FREE_REQ(ndlp);
2118 spin_unlock_irq(&phba->ndlp_lock);
2119 /* Trigger the release of the ndlp memory */
2120 lpfc_nlp_put(ndlp);
2121 continue;
2122 }
2123 spin_lock_irq(&phba->ndlp_lock);
2124 if (NLP_CHK_FREE_REQ(ndlp)) {
2125 /* The ndlp should not be in memory free mode already */
2126 spin_unlock_irq(&phba->ndlp_lock);
2127 continue;
2128 } else
2129 /* Indicate request for freeing ndlp memory */
2130 NLP_SET_FREE_REQ(ndlp);
2131 spin_unlock_irq(&phba->ndlp_lock);
2132
James Smart58da1ff2008-04-07 10:15:56 -04002133 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2134 ndlp->nlp_DID == Fabric_DID) {
2135 /* Just free up ndlp with Fabric_DID for vports */
2136 lpfc_nlp_put(ndlp);
2137 continue;
2138 }
2139
James Smart87af33f2007-10-27 13:37:43 -04002140 if (ndlp->nlp_type & NLP_FABRIC)
2141 lpfc_disc_state_machine(vport, ndlp, NULL,
2142 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002143
James Smart87af33f2007-10-27 13:37:43 -04002144 lpfc_disc_state_machine(vport, ndlp, NULL,
2145 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002146
James Smart87af33f2007-10-27 13:37:43 -04002147 }
2148
James Smarta8adb832007-10-27 13:37:53 -04002149 /* At this point, ALL ndlp's should be gone
2150 * because of the previous NLP_EVT_DEVICE_RM.
2151 * Lets wait for this to happen, if needed.
2152 */
James Smart87af33f2007-10-27 13:37:43 -04002153 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002154 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002155 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002156 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002157 list_for_each_entry_safe(ndlp, next_ndlp,
2158 &vport->fc_nodes, nlp_listp) {
2159 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2160 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002161 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002162 "usgmap:x%x refcnt:%d\n",
2163 ndlp->nlp_DID, (void *)ndlp,
2164 ndlp->nlp_usg_map,
2165 atomic_read(
2166 &ndlp->kref.refcount));
2167 }
James Smarta8adb832007-10-27 13:37:53 -04002168 break;
James Smart87af33f2007-10-27 13:37:43 -04002169 }
James Smarta8adb832007-10-27 13:37:53 -04002170
2171 /* Wait for any activity on ndlps to settle */
2172 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002173 }
dea31012005-04-17 16:05:31 -05002174}
2175
James Smarte59058c2008-08-24 21:49:00 -04002176/**
James Smart3621a712009-04-06 18:47:14 -04002177 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002178 * @vport: pointer to a virtual N_Port data structure.
2179 *
2180 * This routine stops all the timers associated with a @vport. This function
2181 * is invoked before disabling or deleting a @vport. Note that the physical
2182 * port is treated as @vport 0.
2183 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002184void
2185lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002186{
James Smart92d7f7b2007-06-17 19:56:38 -05002187 del_timer_sync(&vport->els_tmofunc);
2188 del_timer_sync(&vport->fc_fdmitmo);
2189 lpfc_can_disctmo(vport);
2190 return;
dea31012005-04-17 16:05:31 -05002191}
2192
James Smarte59058c2008-08-24 21:49:00 -04002193/**
James Smartecfd03c2010-02-12 14:41:27 -05002194 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2195 * @phba: pointer to lpfc hba data structure.
2196 *
2197 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2198 * caller of this routine should already hold the host lock.
2199 **/
2200void
2201__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2202{
James Smartfc2b9892010-02-26 14:15:29 -05002203 /* Clear pending FCF rediscovery wait and failover in progress flags */
2204 phba->fcf.fcf_flag &= ~(FCF_REDISC_PEND |
James Smart0c9ab6f2010-02-26 14:15:57 -05002205 FCF_DEAD_DISC |
2206 FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002207 /* Now, try to stop the timer */
2208 del_timer(&phba->fcf.redisc_wait);
2209}
2210
2211/**
2212 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2213 * @phba: pointer to lpfc hba data structure.
2214 *
2215 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2216 * checks whether the FCF rediscovery wait timer is pending with the host
2217 * lock held before proceeding with disabling the timer and clearing the
2218 * wait timer pendig flag.
2219 **/
2220void
2221lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2222{
2223 spin_lock_irq(&phba->hbalock);
2224 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2225 /* FCF rediscovery timer already fired or stopped */
2226 spin_unlock_irq(&phba->hbalock);
2227 return;
2228 }
2229 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2230 spin_unlock_irq(&phba->hbalock);
2231}
2232
2233/**
James Smart3772a992009-05-22 14:50:54 -04002234 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002235 * @phba: pointer to lpfc hba data structure.
2236 *
2237 * This routine stops all the timers associated with a HBA. This function is
2238 * invoked before either putting a HBA offline or unloading the driver.
2239 **/
James Smart3772a992009-05-22 14:50:54 -04002240void
2241lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002242{
James Smart51ef4c22007-08-02 11:10:31 -04002243 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002244 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002245 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002246 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002247 del_timer_sync(&phba->hb_tmofunc);
2248 phba->hb_outstanding = 0;
2249
2250 switch (phba->pci_dev_grp) {
2251 case LPFC_PCI_DEV_LP:
2252 /* Stop any LightPulse device specific driver timers */
2253 del_timer_sync(&phba->fcp_poll_timer);
2254 break;
2255 case LPFC_PCI_DEV_OC:
2256 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002257 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002258 break;
2259 default:
2260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2261 "0297 Invalid device group (x%x)\n",
2262 phba->pci_dev_grp);
2263 break;
2264 }
James Smart2e0fef82007-06-17 19:56:36 -05002265 return;
dea31012005-04-17 16:05:31 -05002266}
2267
James Smarte59058c2008-08-24 21:49:00 -04002268/**
James Smart3621a712009-04-06 18:47:14 -04002269 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002270 * @phba: pointer to lpfc hba data structure.
2271 *
2272 * This routine marks a HBA's management interface as blocked. Once the HBA's
2273 * management interface is marked as blocked, all the user space access to
2274 * the HBA, whether they are from sysfs interface or libdfc interface will
2275 * all be blocked. The HBA is set to block the management interface when the
2276 * driver prepares the HBA interface for online or offline.
2277 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002278static void
2279lpfc_block_mgmt_io(struct lpfc_hba * phba)
2280{
2281 unsigned long iflag;
2282
2283 spin_lock_irqsave(&phba->hbalock, iflag);
2284 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2285 spin_unlock_irqrestore(&phba->hbalock, iflag);
2286}
2287
James Smarte59058c2008-08-24 21:49:00 -04002288/**
James Smart3621a712009-04-06 18:47:14 -04002289 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002290 * @phba: pointer to lpfc hba data structure.
2291 *
2292 * This routine initializes the HBA and brings a HBA online. During this
2293 * process, the management interface is blocked to prevent user space access
2294 * to the HBA interfering with the driver initialization.
2295 *
2296 * Return codes
2297 * 0 - successful
2298 * 1 - failed
2299 **/
dea31012005-04-17 16:05:31 -05002300int
James Smart2e0fef82007-06-17 19:56:36 -05002301lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002302{
Julia Lawall372bd282008-12-16 16:15:08 +01002303 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002304 struct lpfc_vport **vports;
2305 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002306
dea31012005-04-17 16:05:31 -05002307 if (!phba)
2308 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002309 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002310
James Smart2e0fef82007-06-17 19:56:36 -05002311 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002312 return 0;
2313
James Smarted957682007-06-17 19:56:37 -05002314 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002315 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002316
James Smart46fa3112007-04-25 09:51:45 -04002317 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002318
James Smart46fa3112007-04-25 09:51:45 -04002319 if (!lpfc_sli_queue_setup(phba)) {
2320 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002321 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002322 }
2323
James Smartda0436e2009-05-22 14:51:39 -04002324 if (phba->sli_rev == LPFC_SLI_REV4) {
2325 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2326 lpfc_unblock_mgmt_io(phba);
2327 return 1;
2328 }
2329 } else {
2330 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2331 lpfc_unblock_mgmt_io(phba);
2332 return 1;
2333 }
James Smart46fa3112007-04-25 09:51:45 -04002334 }
dea31012005-04-17 16:05:31 -05002335
James Smart549e55c2007-08-02 11:09:51 -04002336 vports = lpfc_create_vport_work_array(phba);
2337 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002338 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002339 struct Scsi_Host *shost;
2340 shost = lpfc_shost_from_vport(vports[i]);
2341 spin_lock_irq(shost->host_lock);
2342 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2343 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2344 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002345 if (phba->sli_rev == LPFC_SLI_REV4)
2346 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002347 spin_unlock_irq(shost->host_lock);
2348 }
James Smart09372822008-01-11 01:52:54 -05002349 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002350
James Smart46fa3112007-04-25 09:51:45 -04002351 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002352 return 0;
2353}
2354
James Smarte59058c2008-08-24 21:49:00 -04002355/**
James Smart3621a712009-04-06 18:47:14 -04002356 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002357 * @phba: pointer to lpfc hba data structure.
2358 *
2359 * This routine marks a HBA's management interface as not blocked. Once the
2360 * HBA's management interface is marked as not blocked, all the user space
2361 * access to the HBA, whether they are from sysfs interface or libdfc
2362 * interface will be allowed. The HBA is set to block the management interface
2363 * when the driver prepares the HBA interface for online or offline and then
2364 * set to unblock the management interface afterwards.
2365 **/
James Smart46fa3112007-04-25 09:51:45 -04002366void
James Smart46fa3112007-04-25 09:51:45 -04002367lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2368{
2369 unsigned long iflag;
2370
James Smart2e0fef82007-06-17 19:56:36 -05002371 spin_lock_irqsave(&phba->hbalock, iflag);
2372 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2373 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002374}
2375
James Smarte59058c2008-08-24 21:49:00 -04002376/**
James Smart3621a712009-04-06 18:47:14 -04002377 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002378 * @phba: pointer to lpfc hba data structure.
2379 *
2380 * This routine is invoked to prepare a HBA to be brought offline. It performs
2381 * unregistration login to all the nodes on all vports and flushes the mailbox
2382 * queue to make it ready to be brought offline.
2383 **/
James Smart46fa3112007-04-25 09:51:45 -04002384void
2385lpfc_offline_prep(struct lpfc_hba * phba)
2386{
James Smart2e0fef82007-06-17 19:56:36 -05002387 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002388 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002389 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002390 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002391 int i;
dea31012005-04-17 16:05:31 -05002392
James Smart2e0fef82007-06-17 19:56:36 -05002393 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002394 return;
dea31012005-04-17 16:05:31 -05002395
James Smart46fa3112007-04-25 09:51:45 -04002396 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002397
2398 lpfc_linkdown(phba);
2399
James Smart87af33f2007-10-27 13:37:43 -04002400 /* Issue an unreg_login to all nodes on all vports */
2401 vports = lpfc_create_vport_work_array(phba);
2402 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002403 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002404 if (vports[i]->load_flag & FC_UNLOADING)
2405 continue;
James Smart72100cc2010-02-12 14:43:01 -05002406 shost = lpfc_shost_from_vport(vports[i]);
2407 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002408 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002409 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2410 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002411 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002412
James Smart87af33f2007-10-27 13:37:43 -04002413 shost = lpfc_shost_from_vport(vports[i]);
2414 list_for_each_entry_safe(ndlp, next_ndlp,
2415 &vports[i]->fc_nodes,
2416 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002417 if (!NLP_CHK_NODE_ACT(ndlp))
2418 continue;
James Smart87af33f2007-10-27 13:37:43 -04002419 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2420 continue;
2421 if (ndlp->nlp_type & NLP_FABRIC) {
2422 lpfc_disc_state_machine(vports[i], ndlp,
2423 NULL, NLP_EVT_DEVICE_RECOVERY);
2424 lpfc_disc_state_machine(vports[i], ndlp,
2425 NULL, NLP_EVT_DEVICE_RM);
2426 }
2427 spin_lock_irq(shost->host_lock);
2428 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2429 spin_unlock_irq(shost->host_lock);
2430 lpfc_unreg_rpi(vports[i], ndlp);
2431 }
2432 }
2433 }
James Smart09372822008-01-11 01:52:54 -05002434 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002435
James Smart04c68492009-05-22 14:52:52 -04002436 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002437}
2438
James Smarte59058c2008-08-24 21:49:00 -04002439/**
James Smart3621a712009-04-06 18:47:14 -04002440 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002441 * @phba: pointer to lpfc hba data structure.
2442 *
2443 * This routine actually brings a HBA offline. It stops all the timers
2444 * associated with the HBA, brings down the SLI layer, and eventually
2445 * marks the HBA as in offline state for the upper layer protocol.
2446 **/
James Smart46fa3112007-04-25 09:51:45 -04002447void
James Smart2e0fef82007-06-17 19:56:36 -05002448lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002449{
James Smart549e55c2007-08-02 11:09:51 -04002450 struct Scsi_Host *shost;
2451 struct lpfc_vport **vports;
2452 int i;
James Smart46fa3112007-04-25 09:51:45 -04002453
James Smart549e55c2007-08-02 11:09:51 -04002454 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002455 return;
James Smart688a8862006-07-06 15:49:56 -04002456
James Smartda0436e2009-05-22 14:51:39 -04002457 /* stop port and all timers associated with this hba */
2458 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002459 vports = lpfc_create_vport_work_array(phba);
2460 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002461 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002462 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002463 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002464 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002465 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002466 /* Bring down the SLI Layer and cleanup. The HBA is offline
2467 now. */
2468 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002469 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002470 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002471 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002472 vports = lpfc_create_vport_work_array(phba);
2473 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002474 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002475 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002476 spin_lock_irq(shost->host_lock);
2477 vports[i]->work_port_events = 0;
2478 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2479 spin_unlock_irq(shost->host_lock);
2480 }
James Smart09372822008-01-11 01:52:54 -05002481 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002482}
2483
James Smarte59058c2008-08-24 21:49:00 -04002484/**
James Smart3621a712009-04-06 18:47:14 -04002485 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002486 * @phba: pointer to lpfc hba data structure.
2487 *
2488 * This routine is to free all the SCSI buffers and IOCBs from the driver
2489 * list back to kernel. It is called from lpfc_pci_remove_one to free
2490 * the internal resources before the device is removed from the system.
2491 *
2492 * Return codes
2493 * 0 - successful (for now, it always returns 0)
2494 **/
dea31012005-04-17 16:05:31 -05002495static int
James Smart2e0fef82007-06-17 19:56:36 -05002496lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002497{
2498 struct lpfc_scsi_buf *sb, *sb_next;
2499 struct lpfc_iocbq *io, *io_next;
2500
James Smart2e0fef82007-06-17 19:56:36 -05002501 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002502 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002503 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002504 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2505 list_del(&sb->list);
2506 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002507 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002508 kfree(sb);
2509 phba->total_scsi_bufs--;
2510 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002511 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002512
2513 /* Release all the lpfc_iocbq entries maintained by this host. */
2514 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2515 list_del(&io->list);
2516 kfree(io);
2517 phba->total_iocbq_bufs--;
2518 }
James Smart2e0fef82007-06-17 19:56:36 -05002519 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002520 return 0;
2521}
2522
James Smarte59058c2008-08-24 21:49:00 -04002523/**
James Smart3621a712009-04-06 18:47:14 -04002524 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002525 * @phba: pointer to lpfc hba data structure.
2526 * @instance: a unique integer ID to this FC port.
2527 * @dev: pointer to the device data structure.
2528 *
2529 * This routine creates a FC port for the upper layer protocol. The FC port
2530 * can be created on top of either a physical port or a virtual port provided
2531 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2532 * and associates the FC port created before adding the shost into the SCSI
2533 * layer.
2534 *
2535 * Return codes
2536 * @vport - pointer to the virtual N_Port data structure.
2537 * NULL - port create failed.
2538 **/
James Smart2e0fef82007-06-17 19:56:36 -05002539struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002540lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002541{
James Smart2e0fef82007-06-17 19:56:36 -05002542 struct lpfc_vport *vport;
2543 struct Scsi_Host *shost;
2544 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002545
James Smart3de2a652007-08-02 11:09:59 -04002546 if (dev != &phba->pcidev->dev)
2547 shost = scsi_host_alloc(&lpfc_vport_template,
2548 sizeof(struct lpfc_vport));
2549 else
2550 shost = scsi_host_alloc(&lpfc_template,
2551 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002552 if (!shost)
2553 goto out;
James Smart47a86172007-04-25 09:53:22 -04002554
James Smart2e0fef82007-06-17 19:56:36 -05002555 vport = (struct lpfc_vport *) shost->hostdata;
2556 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002557 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002558 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002559 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002560
James Smart3de2a652007-08-02 11:09:59 -04002561 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002562 shost->unique_id = instance;
2563 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002564 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002565 shost->this_id = -1;
2566 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002567 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002568 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04002569 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04002570 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2571 }
James Smart81301a92008-12-04 22:39:46 -05002572
James Smart47a86172007-04-25 09:53:22 -04002573 /*
James Smart2e0fef82007-06-17 19:56:36 -05002574 * Set initial can_queue value since 0 is no longer supported and
2575 * scsi_add_host will fail. This will be adjusted later based on the
2576 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002577 */
James Smart2e0fef82007-06-17 19:56:36 -05002578 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002579 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002580 shost->transportt = lpfc_vport_transport_template;
2581 vport->port_type = LPFC_NPIV_PORT;
2582 } else {
2583 shost->transportt = lpfc_transport_template;
2584 vport->port_type = LPFC_PHYSICAL_PORT;
2585 }
James Smart47a86172007-04-25 09:53:22 -04002586
James Smart2e0fef82007-06-17 19:56:36 -05002587 /* Initialize all internally managed lists. */
2588 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002589 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002590 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002591
James Smart2e0fef82007-06-17 19:56:36 -05002592 init_timer(&vport->fc_disctmo);
2593 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002594 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002595
James Smart2e0fef82007-06-17 19:56:36 -05002596 init_timer(&vport->fc_fdmitmo);
2597 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002598 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002599
James Smart2e0fef82007-06-17 19:56:36 -05002600 init_timer(&vport->els_tmofunc);
2601 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002602 vport->els_tmofunc.data = (unsigned long)vport;
James Bottomleyd139b9b2009-11-05 13:33:12 -06002603 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002604 if (error)
2605 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002606
James Smart549e55c2007-08-02 11:09:51 -04002607 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002608 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002609 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002610 return vport;
James Smart47a86172007-04-25 09:53:22 -04002611
James Smart2e0fef82007-06-17 19:56:36 -05002612out_put_shost:
2613 scsi_host_put(shost);
2614out:
2615 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002616}
2617
James Smarte59058c2008-08-24 21:49:00 -04002618/**
James Smart3621a712009-04-06 18:47:14 -04002619 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002620 * @vport: pointer to an lpfc virtual N_Port data structure.
2621 *
2622 * This routine destroys a FC port from the upper layer protocol. All the
2623 * resources associated with the port are released.
2624 **/
James Smart2e0fef82007-06-17 19:56:36 -05002625void
2626destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002627{
James Smart92d7f7b2007-06-17 19:56:38 -05002628 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2629 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002630
James Smart858c9f62007-06-17 19:56:39 -05002631 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002632 fc_remove_host(shost);
2633 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002634
James Smart92d7f7b2007-06-17 19:56:38 -05002635 spin_lock_irq(&phba->hbalock);
2636 list_del_init(&vport->listentry);
2637 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002638
James Smart92d7f7b2007-06-17 19:56:38 -05002639 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002640 return;
James Smart47a86172007-04-25 09:53:22 -04002641}
2642
James Smarte59058c2008-08-24 21:49:00 -04002643/**
James Smart3621a712009-04-06 18:47:14 -04002644 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002645 *
2646 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2647 * uses the kernel idr facility to perform the task.
2648 *
2649 * Return codes:
2650 * instance - a unique integer ID allocated as the new instance.
2651 * -1 - lpfc get instance failed.
2652 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002653int
2654lpfc_get_instance(void)
2655{
2656 int instance = 0;
2657
2658 /* Assign an unused number */
2659 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2660 return -1;
2661 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2662 return -1;
2663 return instance;
2664}
2665
James Smarte59058c2008-08-24 21:49:00 -04002666/**
James Smart3621a712009-04-06 18:47:14 -04002667 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002668 * @shost: pointer to SCSI host data structure.
2669 * @time: elapsed time of the scan in jiffies.
2670 *
2671 * This routine is called by the SCSI layer with a SCSI host to determine
2672 * whether the scan host is finished.
2673 *
2674 * Note: there is no scan_start function as adapter initialization will have
2675 * asynchronously kicked off the link initialization.
2676 *
2677 * Return codes
2678 * 0 - SCSI host scan is not over yet.
2679 * 1 - SCSI host scan is over.
2680 **/
James Smart47a86172007-04-25 09:53:22 -04002681int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2682{
James Smart2e0fef82007-06-17 19:56:36 -05002683 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2684 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002685 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002686
James Smart858c9f62007-06-17 19:56:39 -05002687 spin_lock_irq(shost->host_lock);
2688
James Smart51ef4c22007-08-02 11:10:31 -04002689 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002690 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002691 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002692 }
James Smart2e0fef82007-06-17 19:56:36 -05002693 if (time >= 30 * HZ) {
2694 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002695 "0461 Scanning longer than 30 "
2696 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002697 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002698 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002699 }
2700 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2701 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002702 "0465 Link down longer than 15 "
2703 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002704 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002705 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002706 }
2707
James Smart2e0fef82007-06-17 19:56:36 -05002708 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002709 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002710 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002711 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002712 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002713 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002714 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002715 goto finished;
2716
2717 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002718
2719finished:
James Smart858c9f62007-06-17 19:56:39 -05002720 spin_unlock_irq(shost->host_lock);
2721 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002722}
2723
James Smarte59058c2008-08-24 21:49:00 -04002724/**
James Smart3621a712009-04-06 18:47:14 -04002725 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002726 * @shost: pointer to SCSI host data structure.
2727 *
2728 * This routine initializes a given SCSI host attributes on a FC port. The
2729 * SCSI host can be either on top of a physical port or a virtual port.
2730 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002731void lpfc_host_attrib_init(struct Scsi_Host *shost)
2732{
2733 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2734 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002735 /*
James Smart2e0fef82007-06-17 19:56:36 -05002736 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002737 */
2738
James Smart2e0fef82007-06-17 19:56:36 -05002739 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2740 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002741 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2742
2743 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002744 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002745 fc_host_supported_fc4s(shost)[2] = 1;
2746 fc_host_supported_fc4s(shost)[7] = 1;
2747
James Smart92d7f7b2007-06-17 19:56:38 -05002748 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2749 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002750
2751 fc_host_supported_speeds(shost) = 0;
2752 if (phba->lmt & LMT_10Gb)
2753 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002754 if (phba->lmt & LMT_8Gb)
2755 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002756 if (phba->lmt & LMT_4Gb)
2757 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2758 if (phba->lmt & LMT_2Gb)
2759 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2760 if (phba->lmt & LMT_1Gb)
2761 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2762
2763 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002764 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2765 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002766
2767 /* This value is also unchanging */
2768 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002769 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002770 fc_host_active_fc4s(shost)[2] = 1;
2771 fc_host_active_fc4s(shost)[7] = 1;
2772
James Smart92d7f7b2007-06-17 19:56:38 -05002773 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002774 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002775 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002776 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002777}
dea31012005-04-17 16:05:31 -05002778
James Smarte59058c2008-08-24 21:49:00 -04002779/**
James Smartda0436e2009-05-22 14:51:39 -04002780 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002781 * @phba: pointer to lpfc hba data structure.
2782 *
James Smartda0436e2009-05-22 14:51:39 -04002783 * This routine is invoked to stop an SLI3 device port, it stops the device
2784 * from generating interrupts and stops the device driver's timers for the
2785 * device.
James Smarte59058c2008-08-24 21:49:00 -04002786 **/
James Smartdb2378e2008-02-08 18:49:51 -05002787static void
James Smartda0436e2009-05-22 14:51:39 -04002788lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002789{
James Smartda0436e2009-05-22 14:51:39 -04002790 /* Clear all interrupt enable conditions */
2791 writel(0, phba->HCregaddr);
2792 readl(phba->HCregaddr); /* flush */
2793 /* Clear all pending interrupts */
2794 writel(0xffffffff, phba->HAregaddr);
2795 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002796
James Smartda0436e2009-05-22 14:51:39 -04002797 /* Reset some HBA SLI setup states */
2798 lpfc_stop_hba_timers(phba);
2799 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002800}
2801
James Smarte59058c2008-08-24 21:49:00 -04002802/**
James Smartda0436e2009-05-22 14:51:39 -04002803 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002804 * @phba: pointer to lpfc hba data structure.
2805 *
James Smartda0436e2009-05-22 14:51:39 -04002806 * This routine is invoked to stop an SLI4 device port, it stops the device
2807 * from generating interrupts and stops the device driver's timers for the
2808 * device.
2809 **/
2810static void
2811lpfc_stop_port_s4(struct lpfc_hba *phba)
2812{
2813 /* Reset some HBA SLI4 setup states */
2814 lpfc_stop_hba_timers(phba);
2815 phba->pport->work_port_events = 0;
2816 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04002817}
2818
2819/**
2820 * lpfc_stop_port - Wrapper function for stopping hba port
2821 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002822 *
James Smartda0436e2009-05-22 14:51:39 -04002823 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2824 * the API jump table function pointer from the lpfc_hba struct.
2825 **/
2826void
2827lpfc_stop_port(struct lpfc_hba *phba)
2828{
2829 phba->lpfc_stop_port(phba);
2830}
2831
2832/**
2833 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2834 * @phba: pointer to lpfc hba data structure.
2835 *
2836 * This routine is invoked to remove the driver default fcf record from
2837 * the port. This routine currently acts on FCF Index 0.
2838 *
2839 **/
2840void
2841lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2842{
2843 int rc = 0;
2844 LPFC_MBOXQ_t *mboxq;
2845 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2846 uint32_t mbox_tmo, req_len;
2847 uint32_t shdr_status, shdr_add_status;
2848
2849 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2850 if (!mboxq) {
2851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2852 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2853 return;
2854 }
2855
2856 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2857 sizeof(struct lpfc_sli4_cfg_mhdr);
2858 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2859 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2860 req_len, LPFC_SLI4_MBX_EMBED);
2861 /*
2862 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2863 * supports multiple FCF indices.
2864 */
2865 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2866 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2867 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
James Smartecfd03c2010-02-12 14:41:27 -05002868 phba->fcf.current_rec.fcf_indx);
James Smartda0436e2009-05-22 14:51:39 -04002869
2870 if (!phba->sli4_hba.intr_enable)
2871 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2872 else {
2873 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2874 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2875 }
2876 /* The IOCTL status is embedded in the mailbox subheader. */
2877 shdr_status = bf_get(lpfc_mbox_hdr_status,
2878 &del_fcf_record->header.cfg_shdr.response);
2879 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2880 &del_fcf_record->header.cfg_shdr.response);
2881 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2882 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2883 "2516 DEL FCF of default FCF Index failed "
2884 "mbx status x%x, status x%x add_status x%x\n",
2885 rc, shdr_status, shdr_add_status);
2886 }
2887 if (rc != MBX_TIMEOUT)
2888 mempool_free(mboxq, phba->mbox_mem_pool);
2889}
2890
2891/**
James Smartecfd03c2010-02-12 14:41:27 -05002892 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2893 * @phba: Pointer to hba for which this call is being executed.
2894 *
2895 * This routine starts the timer waiting for the FCF rediscovery to complete.
2896 **/
2897void
2898lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2899{
2900 unsigned long fcf_redisc_wait_tmo =
2901 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2902 /* Start fcf rediscovery wait period timer */
2903 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2904 spin_lock_irq(&phba->hbalock);
2905 /* Allow action to new fcf asynchronous event */
2906 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2907 /* Mark the FCF rediscovery pending state */
2908 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2909 spin_unlock_irq(&phba->hbalock);
2910}
2911
2912/**
2913 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2914 * @ptr: Map to lpfc_hba data structure pointer.
2915 *
2916 * This routine is invoked when waiting for FCF table rediscover has been
2917 * timed out. If new FCF record(s) has (have) been discovered during the
2918 * wait period, a new FCF event shall be added to the FCOE async event
2919 * list, and then worker thread shall be waked up for processing from the
2920 * worker thread context.
2921 **/
2922void
2923lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2924{
2925 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2926
2927 /* Don't send FCF rediscovery event if timer cancelled */
2928 spin_lock_irq(&phba->hbalock);
2929 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2930 spin_unlock_irq(&phba->hbalock);
2931 return;
2932 }
2933 /* Clear FCF rediscovery timer pending flag */
2934 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2935 /* FCF rediscovery event to worker thread */
2936 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2937 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002938 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2939 "2776 FCF rediscover wait timer expired, post "
2940 "a worker thread event for FCF table scan\n");
James Smartecfd03c2010-02-12 14:41:27 -05002941 /* wake up worker thread */
2942 lpfc_worker_wake_up(phba);
2943}
2944
2945/**
James Smart6669f9b2009-10-02 15:16:45 -04002946 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2947 * @phba: pointer to lpfc hba data structure.
2948 *
2949 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2950 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2951 * was successful and the firmware supports FCoE. Any other return indicates
2952 * a error. It is assumed that this function will be called before interrupts
2953 * are enabled.
2954 **/
2955static int
2956lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2957{
2958 int rc = 0;
2959 LPFC_MBOXQ_t *mboxq;
2960 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2961 uint32_t length;
2962 uint32_t shdr_status, shdr_add_status;
2963
2964 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2965 if (!mboxq) {
2966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2967 "2621 Failed to allocate mbox for "
2968 "query firmware config cmd\n");
2969 return -ENOMEM;
2970 }
2971 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2972 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2973 sizeof(struct lpfc_sli4_cfg_mhdr));
2974 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2975 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2976 length, LPFC_SLI4_MBX_EMBED);
2977 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2978 /* The IOCTL status is embedded in the mailbox subheader. */
2979 shdr_status = bf_get(lpfc_mbox_hdr_status,
2980 &query_fw_cfg->header.cfg_shdr.response);
2981 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2982 &query_fw_cfg->header.cfg_shdr.response);
2983 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2985 "2622 Query Firmware Config failed "
2986 "mbx status x%x, status x%x add_status x%x\n",
2987 rc, shdr_status, shdr_add_status);
2988 return -EINVAL;
2989 }
2990 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2992 "2623 FCoE Function not supported by firmware. "
2993 "Function mode = %08x\n",
2994 query_fw_cfg->function_mode);
2995 return -EINVAL;
2996 }
2997 if (rc != MBX_TIMEOUT)
2998 mempool_free(mboxq, phba->mbox_mem_pool);
2999 return 0;
3000}
3001
3002/**
James Smartda0436e2009-05-22 14:51:39 -04003003 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3004 * @phba: pointer to lpfc hba data structure.
3005 * @acqe_link: pointer to the async link completion queue entry.
3006 *
3007 * This routine is to parse the SLI4 link-attention link fault code and
3008 * translate it into the base driver's read link attention mailbox command
3009 * status.
3010 *
3011 * Return: Link-attention status in terms of base driver's coding.
3012 **/
3013static uint16_t
3014lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3015 struct lpfc_acqe_link *acqe_link)
3016{
3017 uint16_t latt_fault;
3018
3019 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3020 case LPFC_ASYNC_LINK_FAULT_NONE:
3021 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3022 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3023 latt_fault = 0;
3024 break;
3025 default:
3026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3027 "0398 Invalid link fault code: x%x\n",
3028 bf_get(lpfc_acqe_link_fault, acqe_link));
3029 latt_fault = MBXERR_ERROR;
3030 break;
3031 }
3032 return latt_fault;
3033}
3034
3035/**
3036 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3037 * @phba: pointer to lpfc hba data structure.
3038 * @acqe_link: pointer to the async link completion queue entry.
3039 *
3040 * This routine is to parse the SLI4 link attention type and translate it
3041 * into the base driver's link attention type coding.
3042 *
3043 * Return: Link attention type in terms of base driver's coding.
3044 **/
3045static uint8_t
3046lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3047 struct lpfc_acqe_link *acqe_link)
3048{
3049 uint8_t att_type;
3050
3051 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3052 case LPFC_ASYNC_LINK_STATUS_DOWN:
3053 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3054 att_type = AT_LINK_DOWN;
3055 break;
3056 case LPFC_ASYNC_LINK_STATUS_UP:
3057 /* Ignore physical link up events - wait for logical link up */
3058 att_type = AT_RESERVED;
3059 break;
3060 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3061 att_type = AT_LINK_UP;
3062 break;
3063 default:
3064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3065 "0399 Invalid link attention type: x%x\n",
3066 bf_get(lpfc_acqe_link_status, acqe_link));
3067 att_type = AT_RESERVED;
3068 break;
3069 }
3070 return att_type;
3071}
3072
3073/**
3074 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3075 * @phba: pointer to lpfc hba data structure.
3076 * @acqe_link: pointer to the async link completion queue entry.
3077 *
3078 * This routine is to parse the SLI4 link-attention link speed and translate
3079 * it into the base driver's link-attention link speed coding.
3080 *
3081 * Return: Link-attention link speed in terms of base driver's coding.
3082 **/
3083static uint8_t
3084lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3085 struct lpfc_acqe_link *acqe_link)
3086{
3087 uint8_t link_speed;
3088
3089 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3090 case LPFC_ASYNC_LINK_SPEED_ZERO:
3091 link_speed = LA_UNKNW_LINK;
3092 break;
3093 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3094 link_speed = LA_UNKNW_LINK;
3095 break;
3096 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3097 link_speed = LA_UNKNW_LINK;
3098 break;
3099 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3100 link_speed = LA_1GHZ_LINK;
3101 break;
3102 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3103 link_speed = LA_10GHZ_LINK;
3104 break;
3105 default:
3106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3107 "0483 Invalid link-attention link speed: x%x\n",
3108 bf_get(lpfc_acqe_link_speed, acqe_link));
3109 link_speed = LA_UNKNW_LINK;
3110 break;
3111 }
3112 return link_speed;
3113}
3114
3115/**
3116 * lpfc_sli4_async_link_evt - Process the asynchronous link event
3117 * @phba: pointer to lpfc hba data structure.
3118 * @acqe_link: pointer to the async link completion queue entry.
3119 *
3120 * This routine is to handle the SLI4 asynchronous link event.
3121 **/
3122static void
3123lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3124 struct lpfc_acqe_link *acqe_link)
3125{
3126 struct lpfc_dmabuf *mp;
3127 LPFC_MBOXQ_t *pmb;
3128 MAILBOX_t *mb;
3129 READ_LA_VAR *la;
3130 uint8_t att_type;
3131
3132 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3133 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3134 return;
James Smart32b97932009-07-19 10:01:21 -04003135 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003136 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3137 if (!pmb) {
3138 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3139 "0395 The mboxq allocation failed\n");
3140 return;
3141 }
3142 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3143 if (!mp) {
3144 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3145 "0396 The lpfc_dmabuf allocation failed\n");
3146 goto out_free_pmb;
3147 }
3148 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3149 if (!mp->virt) {
3150 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3151 "0397 The mbuf allocation failed\n");
3152 goto out_free_dmabuf;
3153 }
3154
3155 /* Cleanup any outstanding ELS commands */
3156 lpfc_els_flush_all_cmd(phba);
3157
3158 /* Block ELS IOCBs until we have done process link event */
3159 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3160
3161 /* Update link event statistics */
3162 phba->sli.slistat.link_event++;
3163
3164 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3165 lpfc_read_la(phba, pmb, mp);
3166 pmb->vport = phba->pport;
3167
3168 /* Parse and translate status field */
3169 mb = &pmb->u.mb;
3170 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3171
3172 /* Parse and translate link attention fields */
3173 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3174 la->eventTag = acqe_link->event_tag;
3175 la->attType = att_type;
3176 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3177
3178 /* Fake the the following irrelvant fields */
3179 la->topology = TOPOLOGY_PT_PT;
3180 la->granted_AL_PA = 0;
3181 la->il = 0;
3182 la->pb = 0;
3183 la->fa = 0;
3184 la->mm = 0;
3185
3186 /* Keep the link status for extra SLI4 state machine reference */
3187 phba->sli4_hba.link_state.speed =
3188 bf_get(lpfc_acqe_link_speed, acqe_link);
3189 phba->sli4_hba.link_state.duplex =
3190 bf_get(lpfc_acqe_link_duplex, acqe_link);
3191 phba->sli4_hba.link_state.status =
3192 bf_get(lpfc_acqe_link_status, acqe_link);
3193 phba->sli4_hba.link_state.physical =
3194 bf_get(lpfc_acqe_link_physical, acqe_link);
3195 phba->sli4_hba.link_state.fault =
3196 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003197 phba->sli4_hba.link_state.logical_speed =
3198 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003199
3200 /* Invoke the lpfc_handle_latt mailbox command callback function */
3201 lpfc_mbx_cmpl_read_la(phba, pmb);
3202
3203 return;
3204
3205out_free_dmabuf:
3206 kfree(mp);
3207out_free_pmb:
3208 mempool_free(pmb, phba->mbox_mem_pool);
3209}
3210
3211/**
James Smartfc2b9892010-02-26 14:15:29 -05003212 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3213 * @vport: pointer to vport data structure.
3214 *
3215 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3216 * response to a CVL event.
3217 *
3218 * Return the pointer to the ndlp with the vport if successful, otherwise
3219 * return NULL.
3220 **/
3221static struct lpfc_nodelist *
3222lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3223{
3224 struct lpfc_nodelist *ndlp;
3225 struct Scsi_Host *shost;
3226 struct lpfc_hba *phba;
3227
3228 if (!vport)
3229 return NULL;
3230 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3231 if (!ndlp)
3232 return NULL;
3233 phba = vport->phba;
3234 if (!phba)
3235 return NULL;
3236 if (phba->pport->port_state <= LPFC_FLOGI)
3237 return NULL;
3238 /* If virtual link is not yet instantiated ignore CVL */
3239 if (vport->port_state <= LPFC_FDISC)
3240 return NULL;
3241 shost = lpfc_shost_from_vport(vport);
3242 if (!shost)
3243 return NULL;
3244 lpfc_linkdown_port(vport);
3245 lpfc_cleanup_pending_mbox(vport);
3246 spin_lock_irq(shost->host_lock);
3247 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3248 spin_unlock_irq(shost->host_lock);
3249
3250 return ndlp;
3251}
3252
3253/**
3254 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3255 * @vport: pointer to lpfc hba data structure.
3256 *
3257 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3258 * response to a FCF dead event.
3259 **/
3260static void
3261lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3262{
3263 struct lpfc_vport **vports;
3264 int i;
3265
3266 vports = lpfc_create_vport_work_array(phba);
3267 if (vports)
3268 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3269 lpfc_sli4_perform_vport_cvl(vports[i]);
3270 lpfc_destroy_vport_work_array(phba, vports);
3271}
3272
3273/**
James Smartda0436e2009-05-22 14:51:39 -04003274 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3275 * @phba: pointer to lpfc hba data structure.
3276 * @acqe_link: pointer to the async fcoe completion queue entry.
3277 *
3278 * This routine is to handle the SLI4 asynchronous fcoe event.
3279 **/
3280static void
3281lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3282 struct lpfc_acqe_fcoe *acqe_fcoe)
3283{
3284 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3285 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003286 struct lpfc_vport *vport;
3287 struct lpfc_nodelist *ndlp;
3288 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003289 int active_vlink_present;
3290 struct lpfc_vport **vports;
3291 int i;
James Smartda0436e2009-05-22 14:51:39 -04003292
James Smart4d9ab992009-10-02 15:16:39 -04003293 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003294 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003295 switch (event_type) {
3296 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
James Smartecfd03c2010-02-12 14:41:27 -05003297 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
James Smart999d8132010-03-15 11:24:56 -04003298 if (event_type == LPFC_FCOE_EVENT_TYPE_NEW_FCF)
3299 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3300 LOG_DISCOVERY,
3301 "2546 New FCF found event: "
3302 "evt_tag:x%x, fcf_index:x%x\n",
3303 acqe_fcoe->event_tag,
3304 acqe_fcoe->index);
3305 else
3306 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3307 LOG_DISCOVERY,
3308 "2788 FCF parameter modified event: "
3309 "evt_tag:x%x, fcf_index:x%x\n",
3310 acqe_fcoe->event_tag,
3311 acqe_fcoe->index);
James Smartda0436e2009-05-22 14:51:39 -04003312 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003313 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3314 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3315 /*
3316 * If the current FCF is in discovered state or
3317 * FCF discovery is in progress, do nothing.
3318 */
3319 spin_unlock_irq(&phba->hbalock);
3320 break;
3321 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003322
James Smartecfd03c2010-02-12 14:41:27 -05003323 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3324 /*
3325 * If fast FCF failover rescan event is pending,
3326 * do nothing.
3327 */
James Smartda0436e2009-05-22 14:51:39 -04003328 spin_unlock_irq(&phba->hbalock);
3329 break;
3330 }
3331 spin_unlock_irq(&phba->hbalock);
3332
James Smart0c9ab6f2010-02-26 14:15:57 -05003333 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
3334 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
3335 /*
3336 * During period of FCF discovery, read the FCF
3337 * table record indexed by the event to update
3338 * FCF round robin failover eligible FCF bmask.
3339 */
3340 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3341 LOG_DISCOVERY,
3342 "2779 Read new FCF record with "
3343 "fcf_index:x%x for updating FCF "
3344 "round robin failover bmask\n",
3345 acqe_fcoe->index);
3346 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fcoe->index);
3347 }
3348
3349 /* Otherwise, scan the entire FCF table and re-discover SAN */
3350 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3351 "2770 Start FCF table scan due to new FCF "
3352 "event: evt_tag:x%x, fcf_index:x%x\n",
3353 acqe_fcoe->event_tag, acqe_fcoe->index);
3354 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3355 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003356 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003357 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3358 "2547 Issue FCF scan read FCF mailbox "
3359 "command failed 0x%x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003360 break;
3361
3362 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3363 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003364 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003365 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3366 acqe_fcoe->event_tag);
3367 break;
3368
3369 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003370 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003371 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003372 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003373 acqe_fcoe->event_tag);
3374 /* If the event is not for currently used fcf do nothing */
James Smartecfd03c2010-02-12 14:41:27 -05003375 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003376 break;
James Smartfc2b9892010-02-26 14:15:29 -05003377 /* We request port to rediscover the entire FCF table for
3378 * a fast recovery from case that the current FCF record
James Smart0c9ab6f2010-02-26 14:15:57 -05003379 * is no longer valid if we are not in the middle of FCF
3380 * failover process already.
James Smartda0436e2009-05-22 14:51:39 -04003381 */
James Smartfc2b9892010-02-26 14:15:29 -05003382 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003383 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003384 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003385 /* Update FLOGI FCF failover eligible FCF bmask */
3386 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fcoe->index);
James Smartfc2b9892010-02-26 14:15:29 -05003387 break;
3388 }
3389 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003390 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003391 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003392 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3393 "2771 Start FCF fast failover process due to "
3394 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3395 "\n", acqe_fcoe->event_tag, acqe_fcoe->index);
James Smartfc2b9892010-02-26 14:15:29 -05003396 rc = lpfc_sli4_redisc_fcf_table(phba);
3397 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003398 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3399 LOG_DISCOVERY,
3400 "2772 Issue FCF rediscover mabilbox "
3401 "command failed, fail through to FCF "
3402 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003403 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003404 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003405 spin_unlock_irq(&phba->hbalock);
3406 /*
3407 * Last resort will fail over by treating this
3408 * as a link down to FCF registration.
3409 */
3410 lpfc_sli4_fcf_dead_failthrough(phba);
3411 } else
3412 /* Handling fast FCF failover to a DEAD FCF event
3413 * is considered equalivant to receiving CVL to all
3414 * vports.
3415 */
3416 lpfc_sli4_perform_all_vport_cvl(phba);
James Smartda0436e2009-05-22 14:51:39 -04003417 break;
James Smart6669f9b2009-10-02 15:16:45 -04003418 case LPFC_FCOE_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003419 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003420 "2718 Clear Virtual Link Received for VPI 0x%x"
3421 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3422 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003423 acqe_fcoe->index - phba->vpi_base);
James Smartfc2b9892010-02-26 14:15:29 -05003424 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003425 if (!ndlp)
3426 break;
James Smart695a8142010-01-26 23:08:03 -05003427 active_vlink_present = 0;
3428
3429 vports = lpfc_create_vport_work_array(phba);
3430 if (vports) {
3431 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3432 i++) {
3433 if ((!(vports[i]->fc_flag &
3434 FC_VPORT_CVL_RCVD)) &&
3435 (vports[i]->port_state > LPFC_FDISC)) {
3436 active_vlink_present = 1;
3437 break;
3438 }
3439 }
3440 lpfc_destroy_vport_work_array(phba, vports);
3441 }
3442
3443 if (active_vlink_present) {
3444 /*
3445 * If there are other active VLinks present,
3446 * re-instantiate the Vlink using FDISC.
3447 */
James Smart6669f9b2009-10-02 15:16:45 -04003448 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003449 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003450 spin_lock_irq(shost->host_lock);
3451 ndlp->nlp_flag |= NLP_DELAY_TMO;
3452 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003453 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3454 vport->port_state = LPFC_FDISC;
3455 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003456 /*
3457 * Otherwise, we request port to rediscover
3458 * the entire FCF table for a fast recovery
3459 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003460 * is no longer valid if we are not already
3461 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003462 */
James Smartfc2b9892010-02-26 14:15:29 -05003463 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003464 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003465 spin_unlock_irq(&phba->hbalock);
3466 break;
3467 }
3468 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003469 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003470 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003471 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3472 LOG_DISCOVERY,
3473 "2773 Start FCF fast failover due "
3474 "to CVL event: evt_tag:x%x\n",
3475 acqe_fcoe->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003476 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003477 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003478 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3479 LOG_DISCOVERY,
3480 "2774 Issue FCF rediscover "
3481 "mabilbox command failed, "
3482 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003483 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003484 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003485 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003486 /*
3487 * Last resort will be re-try on the
3488 * the current registered FCF entry.
3489 */
3490 lpfc_retry_pport_discovery(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003491 }
James Smart6669f9b2009-10-02 15:16:45 -04003492 }
3493 break;
James Smartda0436e2009-05-22 14:51:39 -04003494 default:
3495 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3496 "0288 Unknown FCoE event type 0x%x event tag "
3497 "0x%x\n", event_type, acqe_fcoe->event_tag);
3498 break;
3499 }
3500}
3501
3502/**
3503 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3504 * @phba: pointer to lpfc hba data structure.
3505 * @acqe_link: pointer to the async dcbx completion queue entry.
3506 *
3507 * This routine is to handle the SLI4 asynchronous dcbx event.
3508 **/
3509static void
3510lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3511 struct lpfc_acqe_dcbx *acqe_dcbx)
3512{
James Smart4d9ab992009-10-02 15:16:39 -04003513 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3515 "0290 The SLI4 DCBX asynchronous event is not "
3516 "handled yet\n");
3517}
3518
3519/**
James Smartb19a0612010-04-06 14:48:51 -04003520 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3521 * @phba: pointer to lpfc hba data structure.
3522 * @acqe_link: pointer to the async grp5 completion queue entry.
3523 *
3524 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3525 * is an asynchronous notified of a logical link speed change. The Port
3526 * reports the logical link speed in units of 10Mbps.
3527 **/
3528static void
3529lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3530 struct lpfc_acqe_grp5 *acqe_grp5)
3531{
3532 uint16_t prev_ll_spd;
3533
3534 phba->fc_eventTag = acqe_grp5->event_tag;
3535 phba->fcoe_eventtag = acqe_grp5->event_tag;
3536 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3537 phba->sli4_hba.link_state.logical_speed =
3538 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3539 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3540 "2789 GRP5 Async Event: Updating logical link speed "
3541 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3542 (phba->sli4_hba.link_state.logical_speed*10));
3543}
3544
3545/**
James Smartda0436e2009-05-22 14:51:39 -04003546 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3547 * @phba: pointer to lpfc hba data structure.
3548 *
3549 * This routine is invoked by the worker thread to process all the pending
3550 * SLI4 asynchronous events.
3551 **/
3552void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3553{
3554 struct lpfc_cq_event *cq_event;
3555
3556 /* First, declare the async event has been handled */
3557 spin_lock_irq(&phba->hbalock);
3558 phba->hba_flag &= ~ASYNC_EVENT;
3559 spin_unlock_irq(&phba->hbalock);
3560 /* Now, handle all the async events */
3561 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3562 /* Get the first event from the head of the event queue */
3563 spin_lock_irq(&phba->hbalock);
3564 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3565 cq_event, struct lpfc_cq_event, list);
3566 spin_unlock_irq(&phba->hbalock);
3567 /* Process the asynchronous event */
3568 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3569 case LPFC_TRAILER_CODE_LINK:
3570 lpfc_sli4_async_link_evt(phba,
3571 &cq_event->cqe.acqe_link);
3572 break;
3573 case LPFC_TRAILER_CODE_FCOE:
3574 lpfc_sli4_async_fcoe_evt(phba,
3575 &cq_event->cqe.acqe_fcoe);
3576 break;
3577 case LPFC_TRAILER_CODE_DCBX:
3578 lpfc_sli4_async_dcbx_evt(phba,
3579 &cq_event->cqe.acqe_dcbx);
3580 break;
James Smartb19a0612010-04-06 14:48:51 -04003581 case LPFC_TRAILER_CODE_GRP5:
3582 lpfc_sli4_async_grp5_evt(phba,
3583 &cq_event->cqe.acqe_grp5);
3584 break;
James Smartda0436e2009-05-22 14:51:39 -04003585 default:
3586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3587 "1804 Invalid asynchrous event code: "
3588 "x%x\n", bf_get(lpfc_trailer_code,
3589 &cq_event->cqe.mcqe_cmpl));
3590 break;
3591 }
3592 /* Free the completion event processed to the free pool */
3593 lpfc_sli4_cq_event_release(phba, cq_event);
3594 }
3595}
3596
3597/**
James Smartecfd03c2010-02-12 14:41:27 -05003598 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3599 * @phba: pointer to lpfc hba data structure.
3600 *
3601 * This routine is invoked by the worker thread to process FCF table
3602 * rediscovery pending completion event.
3603 **/
3604void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3605{
3606 int rc;
3607
3608 spin_lock_irq(&phba->hbalock);
3609 /* Clear FCF rediscovery timeout event */
3610 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3611 /* Clear driver fast failover FCF record flag */
3612 phba->fcf.failover_rec.flag = 0;
3613 /* Set state for FCF fast failover */
3614 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3615 spin_unlock_irq(&phba->hbalock);
3616
3617 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05003618 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3619 "2777 Start FCF table scan after FCF "
3620 "rediscovery quiescent period over\n");
3621 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05003622 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003623 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3624 "2747 Issue FCF scan read FCF mailbox "
3625 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05003626}
3627
3628/**
James Smartda0436e2009-05-22 14:51:39 -04003629 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3630 * @phba: pointer to lpfc hba data structure.
3631 * @dev_grp: The HBA PCI-Device group number.
3632 *
3633 * This routine is invoked to set up the per HBA PCI-Device group function
3634 * API jump table entries.
3635 *
3636 * Return: 0 if success, otherwise -ENODEV
3637 **/
3638int
3639lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003640{
3641 int rc;
3642
James Smartda0436e2009-05-22 14:51:39 -04003643 /* Set up lpfc PCI-device group */
3644 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003645
James Smartda0436e2009-05-22 14:51:39 -04003646 /* The LPFC_PCI_DEV_OC uses SLI4 */
3647 if (dev_grp == LPFC_PCI_DEV_OC)
3648 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003649
James Smartda0436e2009-05-22 14:51:39 -04003650 /* Set up device INIT API function jump table */
3651 rc = lpfc_init_api_table_setup(phba, dev_grp);
3652 if (rc)
3653 return -ENODEV;
3654 /* Set up SCSI API function jump table */
3655 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3656 if (rc)
3657 return -ENODEV;
3658 /* Set up SLI API function jump table */
3659 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3660 if (rc)
3661 return -ENODEV;
3662 /* Set up MBOX API function jump table */
3663 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3664 if (rc)
3665 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003666
James Smartda0436e2009-05-22 14:51:39 -04003667 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003668}
3669
3670/**
James Smart3621a712009-04-06 18:47:14 -04003671 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003672 * @phba: pointer to lpfc hba data structure.
3673 * @intr_mode: active interrupt mode adopted.
3674 *
3675 * This routine it invoked to log the currently used active interrupt mode
3676 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003677 **/
3678static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003679{
3680 switch (intr_mode) {
3681 case 0:
3682 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3683 "0470 Enable INTx interrupt mode.\n");
3684 break;
3685 case 1:
3686 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3687 "0481 Enabled MSI interrupt mode.\n");
3688 break;
3689 case 2:
3690 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3691 "0480 Enabled MSI-X interrupt mode.\n");
3692 break;
3693 default:
3694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3695 "0482 Illegal interrupt mode.\n");
3696 break;
3697 }
3698 return;
3699}
3700
James Smart5b75da22008-12-04 22:39:35 -05003701/**
James Smart3772a992009-05-22 14:50:54 -04003702 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003703 * @phba: pointer to lpfc hba data structure.
3704 *
James Smart3772a992009-05-22 14:50:54 -04003705 * This routine is invoked to enable the PCI device that is common to all
3706 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003707 *
3708 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003709 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003710 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003711 **/
James Smart3772a992009-05-22 14:50:54 -04003712static int
3713lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003714{
James Smart3772a992009-05-22 14:50:54 -04003715 struct pci_dev *pdev;
3716 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003717
James Smart3772a992009-05-22 14:50:54 -04003718 /* Obtain PCI device reference */
3719 if (!phba->pcidev)
3720 goto out_error;
3721 else
3722 pdev = phba->pcidev;
3723 /* Select PCI BARs */
3724 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3725 /* Enable PCI device */
3726 if (pci_enable_device_mem(pdev))
3727 goto out_error;
3728 /* Request PCI resource for the device */
3729 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3730 goto out_disable_device;
3731 /* Set up device as PCI master and save state for EEH */
3732 pci_set_master(pdev);
3733 pci_try_set_mwi(pdev);
3734 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003735
James Smart3772a992009-05-22 14:50:54 -04003736 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003737
James Smart3772a992009-05-22 14:50:54 -04003738out_disable_device:
3739 pci_disable_device(pdev);
3740out_error:
3741 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003742}
3743
3744/**
James Smart3772a992009-05-22 14:50:54 -04003745 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003746 * @phba: pointer to lpfc hba data structure.
3747 *
James Smart3772a992009-05-22 14:50:54 -04003748 * This routine is invoked to disable the PCI device that is common to all
3749 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003750 **/
3751static void
James Smart3772a992009-05-22 14:50:54 -04003752lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003753{
James Smart3772a992009-05-22 14:50:54 -04003754 struct pci_dev *pdev;
3755 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003756
James Smart3772a992009-05-22 14:50:54 -04003757 /* Obtain PCI device reference */
3758 if (!phba->pcidev)
3759 return;
3760 else
3761 pdev = phba->pcidev;
3762 /* Select PCI BARs */
3763 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3764 /* Release PCI resource and disable PCI device */
3765 pci_release_selected_regions(pdev, bars);
3766 pci_disable_device(pdev);
3767 /* Null out PCI private reference to driver */
3768 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003769
3770 return;
3771}
3772
3773/**
James Smart3772a992009-05-22 14:50:54 -04003774 * lpfc_reset_hba - Reset a hba
3775 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003776 *
James Smart3772a992009-05-22 14:50:54 -04003777 * This routine is invoked to reset a hba device. It brings the HBA
3778 * offline, performs a board restart, and then brings the board back
3779 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3780 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003781 **/
James Smart3772a992009-05-22 14:50:54 -04003782void
3783lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003784{
James Smart3772a992009-05-22 14:50:54 -04003785 /* If resets are disabled then set error state and return. */
3786 if (!phba->cfg_enable_hba_reset) {
3787 phba->link_state = LPFC_HBA_ERROR;
3788 return;
3789 }
3790 lpfc_offline_prep(phba);
3791 lpfc_offline(phba);
3792 lpfc_sli_brdrestart(phba);
3793 lpfc_online(phba);
3794 lpfc_unblock_mgmt_io(phba);
3795}
dea31012005-04-17 16:05:31 -05003796
James Smart3772a992009-05-22 14:50:54 -04003797/**
3798 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3799 * @phba: pointer to lpfc hba data structure.
3800 *
3801 * This routine is invoked to set up the driver internal resources specific to
3802 * support the SLI-3 HBA device it attached to.
3803 *
3804 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003805 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003806 * other values - error
3807 **/
3808static int
3809lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3810{
3811 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003812
James Smart3772a992009-05-22 14:50:54 -04003813 /*
3814 * Initialize timers used by driver
3815 */
dea31012005-04-17 16:05:31 -05003816
James Smart3772a992009-05-22 14:50:54 -04003817 /* Heartbeat timer */
3818 init_timer(&phba->hb_tmofunc);
3819 phba->hb_tmofunc.function = lpfc_hb_timeout;
3820 phba->hb_tmofunc.data = (unsigned long)phba;
3821
3822 psli = &phba->sli;
3823 /* MBOX heartbeat timer */
3824 init_timer(&psli->mbox_tmo);
3825 psli->mbox_tmo.function = lpfc_mbox_timeout;
3826 psli->mbox_tmo.data = (unsigned long) phba;
3827 /* FCP polling mode timer */
3828 init_timer(&phba->fcp_poll_timer);
3829 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3830 phba->fcp_poll_timer.data = (unsigned long) phba;
3831 /* Fabric block timer */
3832 init_timer(&phba->fabric_block_timer);
3833 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3834 phba->fabric_block_timer.data = (unsigned long) phba;
3835 /* EA polling mode timer */
3836 init_timer(&phba->eratt_poll);
3837 phba->eratt_poll.function = lpfc_poll_eratt;
3838 phba->eratt_poll.data = (unsigned long) phba;
3839
3840 /* Host attention work mask setup */
3841 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3842 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3843
3844 /* Get all the module params for configuring this host */
3845 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04003846 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
3847 phba->menlo_flag |= HBA_MENLO_SUPPORT;
3848 /* check for menlo minimum sg count */
3849 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
3850 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
3851 }
3852
James Smart3772a992009-05-22 14:50:54 -04003853 /*
3854 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3855 * used to create the sg_dma_buf_pool must be dynamically calculated.
3856 * 2 segments are added since the IOCB needs a command and response bde.
3857 */
3858 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3859 sizeof(struct fcp_rsp) +
3860 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3861
3862 if (phba->cfg_enable_bg) {
3863 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3864 phba->cfg_sg_dma_buf_size +=
3865 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3866 }
3867
3868 /* Also reinitialize the host templates with new values. */
3869 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3870 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3871
3872 phba->max_vpi = LPFC_MAX_VPI;
3873 /* This will be set to correct value after config_port mbox */
3874 phba->max_vports = 0;
3875
3876 /*
3877 * Initialize the SLI Layer to run with lpfc HBAs.
3878 */
3879 lpfc_sli_setup(phba);
3880 lpfc_sli_queue_setup(phba);
3881
3882 /* Allocate device driver memory */
3883 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3884 return -ENOMEM;
3885
3886 return 0;
3887}
3888
3889/**
3890 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3891 * @phba: pointer to lpfc hba data structure.
3892 *
3893 * This routine is invoked to unset the driver internal resources set up
3894 * specific for supporting the SLI-3 HBA device it attached to.
3895 **/
3896static void
3897lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3898{
3899 /* Free device driver memory allocated */
3900 lpfc_mem_free_all(phba);
3901
3902 return;
3903}
3904
3905/**
James Smartda0436e2009-05-22 14:51:39 -04003906 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3907 * @phba: pointer to lpfc hba data structure.
3908 *
3909 * This routine is invoked to set up the driver internal resources specific to
3910 * support the SLI-4 HBA device it attached to.
3911 *
3912 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003913 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04003914 * other values - error
3915 **/
3916static int
3917lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3918{
3919 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05003920 LPFC_MBOXQ_t *mboxq;
3921 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3922 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3923 struct lpfc_mqe *mqe;
James Smart0c9ab6f2010-02-26 14:15:57 -05003924 int longs;
James Smartda0436e2009-05-22 14:51:39 -04003925
3926 /* Before proceed, wait for POST done and device ready */
3927 rc = lpfc_sli4_post_status_check(phba);
3928 if (rc)
3929 return -ENODEV;
3930
3931 /*
3932 * Initialize timers used by driver
3933 */
3934
3935 /* Heartbeat timer */
3936 init_timer(&phba->hb_tmofunc);
3937 phba->hb_tmofunc.function = lpfc_hb_timeout;
3938 phba->hb_tmofunc.data = (unsigned long)phba;
3939
3940 psli = &phba->sli;
3941 /* MBOX heartbeat timer */
3942 init_timer(&psli->mbox_tmo);
3943 psli->mbox_tmo.function = lpfc_mbox_timeout;
3944 psli->mbox_tmo.data = (unsigned long) phba;
3945 /* Fabric block timer */
3946 init_timer(&phba->fabric_block_timer);
3947 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3948 phba->fabric_block_timer.data = (unsigned long) phba;
3949 /* EA polling mode timer */
3950 init_timer(&phba->eratt_poll);
3951 phba->eratt_poll.function = lpfc_poll_eratt;
3952 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05003953 /* FCF rediscover timer */
3954 init_timer(&phba->fcf.redisc_wait);
3955 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3956 phba->fcf.redisc_wait.data = (unsigned long)phba;
3957
James Smartda0436e2009-05-22 14:51:39 -04003958 /*
3959 * We need to do a READ_CONFIG mailbox command here before
3960 * calling lpfc_get_cfgparam. For VFs this will report the
3961 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3962 * All of the resources allocated
3963 * for this Port are tied to these values.
3964 */
3965 /* Get all the module params for configuring this host */
3966 lpfc_get_cfgparam(phba);
3967 phba->max_vpi = LPFC_MAX_VPI;
3968 /* This will be set to correct value after the read_config mbox */
3969 phba->max_vports = 0;
3970
3971 /* Program the default value of vlan_id and fc_map */
3972 phba->valid_vlan = 0;
3973 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3974 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3975 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3976
3977 /*
3978 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3979 * used to create the sg_dma_buf_pool must be dynamically calculated.
3980 * 2 segments are added since the IOCB needs a command and response bde.
3981 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05003982 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04003983 */
James Smart28baac72010-02-12 14:42:03 -05003984 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
3985 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
3986 /* Feature Level 1 hardware is limited to 2 pages */
3987 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
3988 LPFC_SLI_INTF_FEATURELEVEL1_1))
3989 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04003990 else
James Smart28baac72010-02-12 14:42:03 -05003991 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
3992 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
3993 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
3994 dma_buf_size = dma_buf_size << 1)
3995 ;
3996 if (dma_buf_size == max_buf_size)
3997 phba->cfg_sg_seg_cnt = (dma_buf_size -
3998 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
3999 (2 * sizeof(struct sli4_sge))) /
4000 sizeof(struct sli4_sge);
4001 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004002
4003 /* Initialize buffer queue management fields */
4004 hbq_count = lpfc_sli_hbq_count();
4005 for (i = 0; i < hbq_count; ++i)
4006 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4007 INIT_LIST_HEAD(&phba->rb_pend_list);
4008 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4009 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4010
4011 /*
4012 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4013 */
4014 /* Initialize the Abort scsi buffer list used by driver */
4015 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4016 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4017 /* This abort list used by worker thread */
4018 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4019
4020 /*
4021 * Initialize dirver internal slow-path work queues
4022 */
4023
4024 /* Driver internel slow-path CQ Event pool */
4025 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4026 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004027 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004028 /* Asynchronous event CQ Event work queue list */
4029 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4030 /* Fast-path XRI aborted CQ Event work queue list */
4031 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4032 /* Slow-path XRI aborted CQ Event work queue list */
4033 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4034 /* Receive queue CQ Event work queue list */
4035 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4036
4037 /* Initialize the driver internal SLI layer lists. */
4038 lpfc_sli_setup(phba);
4039 lpfc_sli_queue_setup(phba);
4040
4041 /* Allocate device driver memory */
4042 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4043 if (rc)
4044 return -ENOMEM;
4045
4046 /* Create the bootstrap mailbox command */
4047 rc = lpfc_create_bootstrap_mbox(phba);
4048 if (unlikely(rc))
4049 goto out_free_mem;
4050
4051 /* Set up the host's endian order with the device. */
4052 rc = lpfc_setup_endian_order(phba);
4053 if (unlikely(rc))
4054 goto out_free_bsmbx;
4055
James Smart6669f9b2009-10-02 15:16:45 -04004056 rc = lpfc_sli4_fw_cfg_check(phba);
4057 if (unlikely(rc))
4058 goto out_free_bsmbx;
4059
James Smartda0436e2009-05-22 14:51:39 -04004060 /* Set up the hba's configuration parameters. */
4061 rc = lpfc_sli4_read_config(phba);
4062 if (unlikely(rc))
4063 goto out_free_bsmbx;
4064
4065 /* Perform a function reset */
4066 rc = lpfc_pci_function_reset(phba);
4067 if (unlikely(rc))
4068 goto out_free_bsmbx;
4069
James Smartcb5172e2010-03-15 11:25:07 -04004070 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4071 GFP_KERNEL);
4072 if (!mboxq) {
4073 rc = -ENOMEM;
4074 goto out_free_bsmbx;
4075 }
4076
4077 /* Get the Supported Pages. It is always available. */
4078 lpfc_supported_pages(mboxq);
4079 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4080 if (unlikely(rc)) {
4081 rc = -EIO;
4082 mempool_free(mboxq, phba->mbox_mem_pool);
4083 goto out_free_bsmbx;
4084 }
4085
4086 mqe = &mboxq->u.mqe;
4087 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4088 LPFC_MAX_SUPPORTED_PAGES);
4089 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4090 switch (pn_page[i]) {
4091 case LPFC_SLI4_PARAMETERS:
4092 phba->sli4_hba.pc_sli4_params.supported = 1;
4093 break;
4094 default:
4095 break;
4096 }
4097 }
4098
4099 /* Read the port's SLI4 Parameters capabilities if supported. */
4100 if (phba->sli4_hba.pc_sli4_params.supported)
4101 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4102 mempool_free(mboxq, phba->mbox_mem_pool);
4103 if (rc) {
4104 rc = -EIO;
4105 goto out_free_bsmbx;
4106 }
James Smartda0436e2009-05-22 14:51:39 -04004107 /* Create all the SLI4 queues */
4108 rc = lpfc_sli4_queue_create(phba);
4109 if (rc)
4110 goto out_free_bsmbx;
4111
4112 /* Create driver internal CQE event pool */
4113 rc = lpfc_sli4_cq_event_pool_create(phba);
4114 if (rc)
4115 goto out_destroy_queue;
4116
4117 /* Initialize and populate the iocb list per host */
4118 rc = lpfc_init_sgl_list(phba);
4119 if (rc) {
4120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4121 "1400 Failed to initialize sgl list.\n");
4122 goto out_destroy_cq_event_pool;
4123 }
4124 rc = lpfc_init_active_sgl_array(phba);
4125 if (rc) {
4126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4127 "1430 Failed to initialize sgl list.\n");
4128 goto out_free_sgl_list;
4129 }
4130
4131 rc = lpfc_sli4_init_rpi_hdrs(phba);
4132 if (rc) {
4133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4134 "1432 Failed to initialize rpi headers.\n");
4135 goto out_free_active_sgl;
4136 }
4137
James Smart0c9ab6f2010-02-26 14:15:57 -05004138 /* Allocate eligible FCF bmask memory for FCF round robin failover */
4139 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4140 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4141 GFP_KERNEL);
4142 if (!phba->fcf.fcf_rr_bmask) {
4143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4144 "2759 Failed allocate memory for FCF round "
4145 "robin failover bmask\n");
4146 goto out_remove_rpi_hdrs;
4147 }
4148
James Smartda0436e2009-05-22 14:51:39 -04004149 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4150 phba->cfg_fcp_eq_count), GFP_KERNEL);
4151 if (!phba->sli4_hba.fcp_eq_hdl) {
4152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4153 "2572 Failed allocate memory for fast-path "
4154 "per-EQ handle array\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004155 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04004156 }
4157
4158 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4159 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4160 if (!phba->sli4_hba.msix_entries) {
4161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4162 "2573 Failed allocate memory for msi-x "
4163 "interrupt vector entries\n");
4164 goto out_free_fcp_eq_hdl;
4165 }
4166
4167 return rc;
4168
4169out_free_fcp_eq_hdl:
4170 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004171out_free_fcf_rr_bmask:
4172 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004173out_remove_rpi_hdrs:
4174 lpfc_sli4_remove_rpi_hdrs(phba);
4175out_free_active_sgl:
4176 lpfc_free_active_sgl(phba);
4177out_free_sgl_list:
4178 lpfc_free_sgl_list(phba);
4179out_destroy_cq_event_pool:
4180 lpfc_sli4_cq_event_pool_destroy(phba);
4181out_destroy_queue:
4182 lpfc_sli4_queue_destroy(phba);
4183out_free_bsmbx:
4184 lpfc_destroy_bootstrap_mbox(phba);
4185out_free_mem:
4186 lpfc_mem_free(phba);
4187 return rc;
4188}
4189
4190/**
4191 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4192 * @phba: pointer to lpfc hba data structure.
4193 *
4194 * This routine is invoked to unset the driver internal resources set up
4195 * specific for supporting the SLI-4 HBA device it attached to.
4196 **/
4197static void
4198lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4199{
4200 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4201
4202 /* unregister default FCFI from the HBA */
4203 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4204
4205 /* Free the default FCR table */
4206 lpfc_sli_remove_dflt_fcf(phba);
4207
4208 /* Free memory allocated for msi-x interrupt vector entries */
4209 kfree(phba->sli4_hba.msix_entries);
4210
4211 /* Free memory allocated for fast-path work queue handles */
4212 kfree(phba->sli4_hba.fcp_eq_hdl);
4213
4214 /* Free the allocated rpi headers. */
4215 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004216 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004217
James Smart0c9ab6f2010-02-26 14:15:57 -05004218 /* Free eligible FCF index bmask */
4219 kfree(phba->fcf.fcf_rr_bmask);
4220
James Smartda0436e2009-05-22 14:51:39 -04004221 /* Free the ELS sgl list */
4222 lpfc_free_active_sgl(phba);
4223 lpfc_free_sgl_list(phba);
4224
4225 /* Free the SCSI sgl management array */
4226 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4227
4228 /* Free the SLI4 queues */
4229 lpfc_sli4_queue_destroy(phba);
4230
4231 /* Free the completion queue EQ event pool */
4232 lpfc_sli4_cq_event_release_all(phba);
4233 lpfc_sli4_cq_event_pool_destroy(phba);
4234
4235 /* Reset SLI4 HBA FCoE function */
4236 lpfc_pci_function_reset(phba);
4237
4238 /* Free the bsmbx region. */
4239 lpfc_destroy_bootstrap_mbox(phba);
4240
4241 /* Free the SLI Layer memory with SLI4 HBAs */
4242 lpfc_mem_free_all(phba);
4243
4244 /* Free the current connect table */
4245 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004246 &phba->fcf_conn_rec_list, list) {
4247 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004248 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004249 }
James Smartda0436e2009-05-22 14:51:39 -04004250
4251 return;
4252}
4253
4254/**
James Smart3772a992009-05-22 14:50:54 -04004255 * lpfc_init_api_table_setup - Set up init api fucntion jump table
4256 * @phba: The hba struct for which this call is being executed.
4257 * @dev_grp: The HBA PCI-Device group number.
4258 *
4259 * This routine sets up the device INIT interface API function jump table
4260 * in @phba struct.
4261 *
4262 * Returns: 0 - success, -ENODEV - failure.
4263 **/
4264int
4265lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4266{
James Smart84d1b002010-02-12 14:42:33 -05004267 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4268 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart3772a992009-05-22 14:50:54 -04004269 switch (dev_grp) {
4270 case LPFC_PCI_DEV_LP:
4271 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4272 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4273 phba->lpfc_stop_port = lpfc_stop_port_s3;
4274 break;
James Smartda0436e2009-05-22 14:51:39 -04004275 case LPFC_PCI_DEV_OC:
4276 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4277 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4278 phba->lpfc_stop_port = lpfc_stop_port_s4;
4279 break;
James Smart3772a992009-05-22 14:50:54 -04004280 default:
4281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4282 "1431 Invalid HBA PCI-device group: 0x%x\n",
4283 dev_grp);
4284 return -ENODEV;
4285 break;
4286 }
4287 return 0;
4288}
4289
4290/**
4291 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4292 * @phba: pointer to lpfc hba data structure.
4293 *
4294 * This routine is invoked to set up the driver internal resources before the
4295 * device specific resource setup to support the HBA device it attached to.
4296 *
4297 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004298 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004299 * other values - error
4300 **/
4301static int
4302lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4303{
4304 /*
4305 * Driver resources common to all SLI revisions
4306 */
James Smartea2151b2008-09-07 11:52:10 -04004307 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004308 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004309
James Smarte47c9092008-02-08 18:49:26 -05004310 /* Initialize ndlp management spinlock */
4311 spin_lock_init(&phba->ndlp_lock);
4312
James Smart2e0fef82007-06-17 19:56:36 -05004313 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004314 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004315 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004316
James Smart3772a992009-05-22 14:50:54 -04004317 /* Initialize the wait queue head for the kernel thread */
4318 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004319
James Smart3772a992009-05-22 14:50:54 -04004320 /* Initialize the scsi buffer list used by driver for scsi IO */
4321 spin_lock_init(&phba->scsi_buf_list_lock);
4322 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004323
James Smart3772a992009-05-22 14:50:54 -04004324 /* Initialize the fabric iocb list */
4325 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004326
James Smart3772a992009-05-22 14:50:54 -04004327 /* Initialize list to save ELS buffers */
4328 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004329
James Smart3772a992009-05-22 14:50:54 -04004330 /* Initialize FCF connection rec list */
4331 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004332
James Smart3772a992009-05-22 14:50:54 -04004333 return 0;
4334}
dea31012005-04-17 16:05:31 -05004335
James Smart3772a992009-05-22 14:50:54 -04004336/**
4337 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4338 * @phba: pointer to lpfc hba data structure.
4339 *
4340 * This routine is invoked to set up the driver internal resources after the
4341 * device specific resource setup to support the HBA device it attached to.
4342 *
4343 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004344 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004345 * other values - error
4346 **/
4347static int
4348lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4349{
4350 int error;
4351
4352 /* Startup the kernel thread for this host adapter. */
4353 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4354 "lpfc_worker_%d", phba->brd_no);
4355 if (IS_ERR(phba->worker_thread)) {
4356 error = PTR_ERR(phba->worker_thread);
4357 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004358 }
4359
James Smart3772a992009-05-22 14:50:54 -04004360 return 0;
4361}
4362
4363/**
4364 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4365 * @phba: pointer to lpfc hba data structure.
4366 *
4367 * This routine is invoked to unset the driver internal resources set up after
4368 * the device specific resource setup for supporting the HBA device it
4369 * attached to.
4370 **/
4371static void
4372lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4373{
4374 /* Stop kernel worker thread */
4375 kthread_stop(phba->worker_thread);
4376}
4377
4378/**
4379 * lpfc_free_iocb_list - Free iocb list.
4380 * @phba: pointer to lpfc hba data structure.
4381 *
4382 * This routine is invoked to free the driver's IOCB list and memory.
4383 **/
4384static void
4385lpfc_free_iocb_list(struct lpfc_hba *phba)
4386{
4387 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4388
4389 spin_lock_irq(&phba->hbalock);
4390 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4391 &phba->lpfc_iocb_list, list) {
4392 list_del(&iocbq_entry->list);
4393 kfree(iocbq_entry);
4394 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004395 }
James Smart3772a992009-05-22 14:50:54 -04004396 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004397
James Smart3772a992009-05-22 14:50:54 -04004398 return;
4399}
dea31012005-04-17 16:05:31 -05004400
James Smart3772a992009-05-22 14:50:54 -04004401/**
4402 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4403 * @phba: pointer to lpfc hba data structure.
4404 *
4405 * This routine is invoked to allocate and initizlize the driver's IOCB
4406 * list and set up the IOCB tag array accordingly.
4407 *
4408 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004409 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004410 * other values - error
4411 **/
4412static int
4413lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4414{
4415 struct lpfc_iocbq *iocbq_entry = NULL;
4416 uint16_t iotag;
4417 int i;
dea31012005-04-17 16:05:31 -05004418
4419 /* Initialize and populate the iocb list per host. */
4420 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004421 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004422 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004423 if (iocbq_entry == NULL) {
4424 printk(KERN_ERR "%s: only allocated %d iocbs of "
4425 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004426 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004427 goto out_free_iocbq;
4428 }
4429
James Bottomley604a3e32005-10-29 10:28:33 -05004430 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4431 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004432 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004433 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004434 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004435 goto out_free_iocbq;
4436 }
James Smart3772a992009-05-22 14:50:54 -04004437 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004438
4439 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004440 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4441 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004442 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004443 }
4444
James Smart3772a992009-05-22 14:50:54 -04004445 return 0;
4446
4447out_free_iocbq:
4448 lpfc_free_iocb_list(phba);
4449
4450 return -ENOMEM;
4451}
4452
4453/**
James Smartda0436e2009-05-22 14:51:39 -04004454 * lpfc_free_sgl_list - Free sgl list.
4455 * @phba: pointer to lpfc hba data structure.
4456 *
4457 * This routine is invoked to free the driver's sgl list and memory.
4458 **/
4459static void
4460lpfc_free_sgl_list(struct lpfc_hba *phba)
4461{
4462 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4463 LIST_HEAD(sglq_list);
4464 int rc = 0;
4465
4466 spin_lock_irq(&phba->hbalock);
4467 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4468 spin_unlock_irq(&phba->hbalock);
4469
4470 list_for_each_entry_safe(sglq_entry, sglq_next,
4471 &sglq_list, list) {
4472 list_del(&sglq_entry->list);
4473 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4474 kfree(sglq_entry);
4475 phba->sli4_hba.total_sglq_bufs--;
4476 }
4477 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4478 if (rc) {
4479 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004480 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004481 }
4482 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4483}
4484
4485/**
4486 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4487 * @phba: pointer to lpfc hba data structure.
4488 *
4489 * This routine is invoked to allocate the driver's active sgl memory.
4490 * This array will hold the sglq_entry's for active IOs.
4491 **/
4492static int
4493lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4494{
4495 int size;
4496 size = sizeof(struct lpfc_sglq *);
4497 size *= phba->sli4_hba.max_cfg_param.max_xri;
4498
4499 phba->sli4_hba.lpfc_sglq_active_list =
4500 kzalloc(size, GFP_KERNEL);
4501 if (!phba->sli4_hba.lpfc_sglq_active_list)
4502 return -ENOMEM;
4503 return 0;
4504}
4505
4506/**
4507 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4508 * @phba: pointer to lpfc hba data structure.
4509 *
4510 * This routine is invoked to walk through the array of active sglq entries
4511 * and free all of the resources.
4512 * This is just a place holder for now.
4513 **/
4514static void
4515lpfc_free_active_sgl(struct lpfc_hba *phba)
4516{
4517 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4518}
4519
4520/**
4521 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4522 * @phba: pointer to lpfc hba data structure.
4523 *
4524 * This routine is invoked to allocate and initizlize the driver's sgl
4525 * list and set up the sgl xritag tag array accordingly.
4526 *
4527 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004528 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004529 * other values - error
4530 **/
4531static int
4532lpfc_init_sgl_list(struct lpfc_hba *phba)
4533{
4534 struct lpfc_sglq *sglq_entry = NULL;
4535 int i;
4536 int els_xri_cnt;
4537
4538 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4539 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4540 "2400 lpfc_init_sgl_list els %d.\n",
4541 els_xri_cnt);
4542 /* Initialize and populate the sglq list per host/VF. */
4543 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4544 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4545
4546 /* Sanity check on XRI management */
4547 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4549 "2562 No room left for SCSI XRI allocation: "
4550 "max_xri=%d, els_xri=%d\n",
4551 phba->sli4_hba.max_cfg_param.max_xri,
4552 els_xri_cnt);
4553 return -ENOMEM;
4554 }
4555
4556 /* Allocate memory for the ELS XRI management array */
4557 phba->sli4_hba.lpfc_els_sgl_array =
4558 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4559 GFP_KERNEL);
4560
4561 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4562 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4563 "2401 Failed to allocate memory for ELS "
4564 "XRI management array of size %d.\n",
4565 els_xri_cnt);
4566 return -ENOMEM;
4567 }
4568
4569 /* Keep the SCSI XRI into the XRI management array */
4570 phba->sli4_hba.scsi_xri_max =
4571 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4572 phba->sli4_hba.scsi_xri_cnt = 0;
4573
4574 phba->sli4_hba.lpfc_scsi_psb_array =
4575 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4576 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4577
4578 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4579 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4580 "2563 Failed to allocate memory for SCSI "
4581 "XRI management array of size %d.\n",
4582 phba->sli4_hba.scsi_xri_max);
4583 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4584 return -ENOMEM;
4585 }
4586
4587 for (i = 0; i < els_xri_cnt; i++) {
4588 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4589 if (sglq_entry == NULL) {
4590 printk(KERN_ERR "%s: only allocated %d sgls of "
4591 "expected %d count. Unloading driver.\n",
4592 __func__, i, els_xri_cnt);
4593 goto out_free_mem;
4594 }
4595
4596 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4597 if (sglq_entry->sli4_xritag == NO_XRI) {
4598 kfree(sglq_entry);
4599 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4600 "Unloading driver.\n", __func__);
4601 goto out_free_mem;
4602 }
4603 sglq_entry->buff_type = GEN_BUFF_TYPE;
4604 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4605 if (sglq_entry->virt == NULL) {
4606 kfree(sglq_entry);
4607 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4608 "Unloading driver.\n", __func__);
4609 goto out_free_mem;
4610 }
4611 sglq_entry->sgl = sglq_entry->virt;
4612 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4613
4614 /* The list order is used by later block SGL registraton */
4615 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05004616 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04004617 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4618 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4619 phba->sli4_hba.total_sglq_bufs++;
4620 spin_unlock_irq(&phba->hbalock);
4621 }
4622 return 0;
4623
4624out_free_mem:
4625 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4626 lpfc_free_sgl_list(phba);
4627 return -ENOMEM;
4628}
4629
4630/**
4631 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4632 * @phba: pointer to lpfc hba data structure.
4633 *
4634 * This routine is invoked to post rpi header templates to the
4635 * HBA consistent with the SLI-4 interface spec. This routine
4636 * posts a PAGE_SIZE memory region to the port to hold up to
4637 * PAGE_SIZE modulo 64 rpi context headers.
4638 * No locks are held here because this is an initialization routine
4639 * called only from probe or lpfc_online when interrupts are not
4640 * enabled and the driver is reinitializing the device.
4641 *
4642 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004643 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004644 * ENOMEM - No availble memory
4645 * EIO - The mailbox failed to complete successfully.
4646 **/
4647int
4648lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4649{
4650 int rc = 0;
4651 int longs;
4652 uint16_t rpi_count;
4653 struct lpfc_rpi_hdr *rpi_hdr;
4654
4655 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4656
4657 /*
4658 * Provision an rpi bitmask range for discovery. The total count
4659 * is the difference between max and base + 1.
4660 */
4661 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4662 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4663
4664 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4665 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4666 GFP_KERNEL);
4667 if (!phba->sli4_hba.rpi_bmask)
4668 return -ENOMEM;
4669
4670 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4671 if (!rpi_hdr) {
4672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4673 "0391 Error during rpi post operation\n");
4674 lpfc_sli4_remove_rpis(phba);
4675 rc = -ENODEV;
4676 }
4677
4678 return rc;
4679}
4680
4681/**
4682 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4683 * @phba: pointer to lpfc hba data structure.
4684 *
4685 * This routine is invoked to allocate a single 4KB memory region to
4686 * support rpis and stores them in the phba. This single region
4687 * provides support for up to 64 rpis. The region is used globally
4688 * by the device.
4689 *
4690 * Returns:
4691 * A valid rpi hdr on success.
4692 * A NULL pointer on any failure.
4693 **/
4694struct lpfc_rpi_hdr *
4695lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4696{
4697 uint16_t rpi_limit, curr_rpi_range;
4698 struct lpfc_dmabuf *dmabuf;
4699 struct lpfc_rpi_hdr *rpi_hdr;
4700
4701 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4702 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4703
4704 spin_lock_irq(&phba->hbalock);
4705 curr_rpi_range = phba->sli4_hba.next_rpi;
4706 spin_unlock_irq(&phba->hbalock);
4707
4708 /*
4709 * The port has a limited number of rpis. The increment here
4710 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4711 * and to allow the full max_rpi range per port.
4712 */
4713 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4714 return NULL;
4715
4716 /*
4717 * First allocate the protocol header region for the port. The
4718 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4719 */
4720 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4721 if (!dmabuf)
4722 return NULL;
4723
4724 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4725 LPFC_HDR_TEMPLATE_SIZE,
4726 &dmabuf->phys,
4727 GFP_KERNEL);
4728 if (!dmabuf->virt) {
4729 rpi_hdr = NULL;
4730 goto err_free_dmabuf;
4731 }
4732
4733 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4734 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4735 rpi_hdr = NULL;
4736 goto err_free_coherent;
4737 }
4738
4739 /* Save the rpi header data for cleanup later. */
4740 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4741 if (!rpi_hdr)
4742 goto err_free_coherent;
4743
4744 rpi_hdr->dmabuf = dmabuf;
4745 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4746 rpi_hdr->page_count = 1;
4747 spin_lock_irq(&phba->hbalock);
4748 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4749 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4750
4751 /*
4752 * The next_rpi stores the next module-64 rpi value to post
4753 * in any subsequent rpi memory region postings.
4754 */
4755 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4756 spin_unlock_irq(&phba->hbalock);
4757 return rpi_hdr;
4758
4759 err_free_coherent:
4760 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4761 dmabuf->virt, dmabuf->phys);
4762 err_free_dmabuf:
4763 kfree(dmabuf);
4764 return NULL;
4765}
4766
4767/**
4768 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4769 * @phba: pointer to lpfc hba data structure.
4770 *
4771 * This routine is invoked to remove all memory resources allocated
4772 * to support rpis. This routine presumes the caller has released all
4773 * rpis consumed by fabric or port logins and is prepared to have
4774 * the header pages removed.
4775 **/
4776void
4777lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4778{
4779 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4780
4781 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4782 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4783 list_del(&rpi_hdr->list);
4784 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4785 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4786 kfree(rpi_hdr->dmabuf);
4787 kfree(rpi_hdr);
4788 }
4789
4790 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4791 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4792}
4793
4794/**
James Smart3772a992009-05-22 14:50:54 -04004795 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4796 * @pdev: pointer to pci device data structure.
4797 *
4798 * This routine is invoked to allocate the driver hba data structure for an
4799 * HBA device. If the allocation is successful, the phba reference to the
4800 * PCI device data structure is set.
4801 *
4802 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004803 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004804 * NULL - error
4805 **/
4806static struct lpfc_hba *
4807lpfc_hba_alloc(struct pci_dev *pdev)
4808{
4809 struct lpfc_hba *phba;
4810
4811 /* Allocate memory for HBA structure */
4812 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4813 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004814 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004815 return NULL;
4816 }
4817
4818 /* Set reference to PCI device in HBA structure */
4819 phba->pcidev = pdev;
4820
4821 /* Assign an unused board number */
4822 phba->brd_no = lpfc_get_instance();
4823 if (phba->brd_no < 0) {
4824 kfree(phba);
4825 return NULL;
4826 }
4827
James Smart4fede782010-01-26 23:08:55 -05004828 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04004829 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4830
James Smart3772a992009-05-22 14:50:54 -04004831 return phba;
4832}
4833
4834/**
4835 * lpfc_hba_free - Free driver hba data structure with a device.
4836 * @phba: pointer to lpfc hba data structure.
4837 *
4838 * This routine is invoked to free the driver hba data structure with an
4839 * HBA device.
4840 **/
4841static void
4842lpfc_hba_free(struct lpfc_hba *phba)
4843{
4844 /* Release the driver assigned board number */
4845 idr_remove(&lpfc_hba_index, phba->brd_no);
4846
4847 kfree(phba);
4848 return;
4849}
4850
4851/**
4852 * lpfc_create_shost - Create hba physical port with associated scsi host.
4853 * @phba: pointer to lpfc hba data structure.
4854 *
4855 * This routine is invoked to create HBA physical port and associate a SCSI
4856 * host with it.
4857 *
4858 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004859 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004860 * other values - error
4861 **/
4862static int
4863lpfc_create_shost(struct lpfc_hba *phba)
4864{
4865 struct lpfc_vport *vport;
4866 struct Scsi_Host *shost;
4867
4868 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004869 phba->fc_edtov = FF_DEF_EDTOV;
4870 phba->fc_ratov = FF_DEF_RATOV;
4871 phba->fc_altov = FF_DEF_ALTOV;
4872 phba->fc_arbtov = FF_DEF_ARBTOV;
4873
James Smart3de2a652007-08-02 11:09:59 -04004874 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004875 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004876 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004877
4878 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004879 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004880 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004881 /* Put reference to SCSI host to driver's device private data */
4882 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004883
James Smart3772a992009-05-22 14:50:54 -04004884 return 0;
4885}
dea31012005-04-17 16:05:31 -05004886
James Smart3772a992009-05-22 14:50:54 -04004887/**
4888 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4889 * @phba: pointer to lpfc hba data structure.
4890 *
4891 * This routine is invoked to destroy HBA physical port and the associated
4892 * SCSI host.
4893 **/
4894static void
4895lpfc_destroy_shost(struct lpfc_hba *phba)
4896{
4897 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004898
James Smart3772a992009-05-22 14:50:54 -04004899 /* Destroy physical port that associated with the SCSI host */
4900 destroy_port(vport);
4901
4902 return;
4903}
4904
4905/**
4906 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4907 * @phba: pointer to lpfc hba data structure.
4908 * @shost: the shost to be used to detect Block guard settings.
4909 *
4910 * This routine sets up the local Block guard protocol settings for @shost.
4911 * This routine also allocates memory for debugging bg buffers.
4912 **/
4913static void
4914lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4915{
4916 int pagecnt = 10;
4917 if (lpfc_prot_mask && lpfc_prot_guard) {
4918 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4919 "1478 Registering BlockGuard with the "
4920 "SCSI layer\n");
4921 scsi_host_set_prot(shost, lpfc_prot_mask);
4922 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004923 }
James Smart81301a92008-12-04 22:39:46 -05004924 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004925 while (pagecnt) {
4926 spin_lock_init(&_dump_buf_lock);
4927 _dump_buf_data =
4928 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4929 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004930 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4931 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004932 "_dump_buf_data at 0x%p\n",
4933 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004934 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004935 memset(_dump_buf_data, 0,
4936 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004937 break;
James Smart3772a992009-05-22 14:50:54 -04004938 } else
James Smart81301a92008-12-04 22:39:46 -05004939 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004940 }
James Smart81301a92008-12-04 22:39:46 -05004941 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004942 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4943 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004944 "memory for hexdump\n");
4945 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004946 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4947 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004948 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004949 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004950 while (pagecnt) {
4951 _dump_buf_dif =
4952 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4953 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004954 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4955 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004956 "_dump_buf_dif at 0x%p\n",
4957 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004958 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004959 memset(_dump_buf_dif, 0,
4960 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004961 break;
James Smart3772a992009-05-22 14:50:54 -04004962 } else
James Smart81301a92008-12-04 22:39:46 -05004963 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004964 }
James Smart81301a92008-12-04 22:39:46 -05004965 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004966 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4967 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004968 "memory for hexdump\n");
4969 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004970 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4971 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004972 _dump_buf_dif);
4973}
4974
4975/**
4976 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4977 * @phba: pointer to lpfc hba data structure.
4978 *
4979 * This routine is invoked to perform all the necessary post initialization
4980 * setup for the device.
4981 **/
4982static void
4983lpfc_post_init_setup(struct lpfc_hba *phba)
4984{
4985 struct Scsi_Host *shost;
4986 struct lpfc_adapter_event_header adapter_event;
4987
4988 /* Get the default values for Model Name and Description */
4989 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4990
4991 /*
4992 * hba setup may have changed the hba_queue_depth so we need to
4993 * adjust the value of can_queue.
4994 */
4995 shost = pci_get_drvdata(phba->pcidev);
4996 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4997 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4998 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004999
5000 lpfc_host_attrib_init(shost);
5001
James Smart2e0fef82007-06-17 19:56:36 -05005002 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5003 spin_lock_irq(shost->host_lock);
5004 lpfc_poll_start_timer(phba);
5005 spin_unlock_irq(shost->host_lock);
5006 }
James Smart8f6d98d2006-08-01 07:34:00 -04005007
James Smart93996272008-08-24 21:50:30 -04005008 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5009 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005010 /* Send board arrival event to upper layer */
5011 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5012 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5013 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005014 sizeof(adapter_event),
5015 (char *) &adapter_event,
5016 LPFC_NL_VENDOR_ID);
5017 return;
5018}
5019
5020/**
5021 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5022 * @phba: pointer to lpfc hba data structure.
5023 *
5024 * This routine is invoked to set up the PCI device memory space for device
5025 * with SLI-3 interface spec.
5026 *
5027 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005028 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005029 * other values - error
5030 **/
5031static int
5032lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5033{
5034 struct pci_dev *pdev;
5035 unsigned long bar0map_len, bar2map_len;
5036 int i, hbq_count;
5037 void *ptr;
5038 int error = -ENODEV;
5039
5040 /* Obtain PCI device reference */
5041 if (!phba->pcidev)
5042 return error;
5043 else
5044 pdev = phba->pcidev;
5045
5046 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005047 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5048 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5049 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5050 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005051 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005052 }
5053 }
James Smart3772a992009-05-22 14:50:54 -04005054
5055 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5056 * required by each mapping.
5057 */
5058 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5059 bar0map_len = pci_resource_len(pdev, 0);
5060
5061 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5062 bar2map_len = pci_resource_len(pdev, 2);
5063
5064 /* Map HBA SLIM to a kernel virtual address. */
5065 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5066 if (!phba->slim_memmap_p) {
5067 dev_printk(KERN_ERR, &pdev->dev,
5068 "ioremap failed for SLIM memory.\n");
5069 goto out;
5070 }
5071
5072 /* Map HBA Control Registers to a kernel virtual address. */
5073 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5074 if (!phba->ctrl_regs_memmap_p) {
5075 dev_printk(KERN_ERR, &pdev->dev,
5076 "ioremap failed for HBA control registers.\n");
5077 goto out_iounmap_slim;
5078 }
5079
5080 /* Allocate memory for SLI-2 structures */
5081 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5082 SLI2_SLIM_SIZE,
5083 &phba->slim2p.phys,
5084 GFP_KERNEL);
5085 if (!phba->slim2p.virt)
5086 goto out_iounmap;
5087
5088 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5089 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005090 phba->mbox_ext = (phba->slim2p.virt +
5091 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005092 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5093 phba->IOCBs = (phba->slim2p.virt +
5094 offsetof(struct lpfc_sli2_slim, IOCBs));
5095
5096 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5097 lpfc_sli_hbq_size(),
5098 &phba->hbqslimp.phys,
5099 GFP_KERNEL);
5100 if (!phba->hbqslimp.virt)
5101 goto out_free_slim;
5102
5103 hbq_count = lpfc_sli_hbq_count();
5104 ptr = phba->hbqslimp.virt;
5105 for (i = 0; i < hbq_count; ++i) {
5106 phba->hbqs[i].hbq_virt = ptr;
5107 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5108 ptr += (lpfc_hbq_defs[i]->entry_count *
5109 sizeof(struct lpfc_hbq_entry));
5110 }
5111 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5112 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5113
5114 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5115
5116 INIT_LIST_HEAD(&phba->rb_pend_list);
5117
5118 phba->MBslimaddr = phba->slim_memmap_p;
5119 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5120 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5121 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5122 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005123
dea31012005-04-17 16:05:31 -05005124 return 0;
5125
dea31012005-04-17 16:05:31 -05005126out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005127 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5128 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005129out_iounmap:
5130 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005131out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005132 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005133out:
5134 return error;
5135}
5136
James Smarte59058c2008-08-24 21:49:00 -04005137/**
James Smart3772a992009-05-22 14:50:54 -04005138 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5139 * @phba: pointer to lpfc hba data structure.
5140 *
5141 * This routine is invoked to unset the PCI device memory space for device
5142 * with SLI-3 interface spec.
5143 **/
5144static void
5145lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5146{
5147 struct pci_dev *pdev;
5148
5149 /* Obtain PCI device reference */
5150 if (!phba->pcidev)
5151 return;
5152 else
5153 pdev = phba->pcidev;
5154
5155 /* Free coherent DMA memory allocated */
5156 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5157 phba->hbqslimp.virt, phba->hbqslimp.phys);
5158 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5159 phba->slim2p.virt, phba->slim2p.phys);
5160
5161 /* I/O memory unmap */
5162 iounmap(phba->ctrl_regs_memmap_p);
5163 iounmap(phba->slim_memmap_p);
5164
5165 return;
5166}
5167
5168/**
James Smartda0436e2009-05-22 14:51:39 -04005169 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5170 * @phba: pointer to lpfc hba data structure.
5171 *
5172 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5173 * done and check status.
5174 *
5175 * Return 0 if successful, otherwise -ENODEV.
5176 **/
5177int
5178lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5179{
James Smart28baac72010-02-12 14:42:03 -05005180 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
James Smartda0436e2009-05-22 14:51:39 -04005181 int i, port_error = -ENODEV;
5182
5183 if (!phba->sli4_hba.STAregaddr)
5184 return -ENODEV;
5185
James Smartda0436e2009-05-22 14:51:39 -04005186 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5187 for (i = 0; i < 3000; i++) {
5188 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
5189 /* Encounter fatal POST error, break out */
5190 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
5191 port_error = -ENODEV;
5192 break;
5193 }
5194 if (LPFC_POST_STAGE_ARMFW_READY ==
5195 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
5196 port_error = 0;
5197 break;
5198 }
5199 msleep(10);
5200 }
5201
5202 if (port_error)
5203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5204 "1408 Failure HBA POST Status: sta_reg=0x%x, "
5205 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5206 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5207 bf_get(lpfc_hst_state_perr, &sta_reg),
5208 bf_get(lpfc_hst_state_sfi, &sta_reg),
5209 bf_get(lpfc_hst_state_nip, &sta_reg),
5210 bf_get(lpfc_hst_state_ipc, &sta_reg),
5211 bf_get(lpfc_hst_state_xrom, &sta_reg),
5212 bf_get(lpfc_hst_state_dl, &sta_reg),
5213 bf_get(lpfc_hst_state_port_status, &sta_reg));
5214
5215 /* Log device information */
James Smart28baac72010-02-12 14:42:03 -05005216 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5217 if (bf_get(lpfc_sli_intf_valid,
5218 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5219 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5220 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5221 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5222 bf_get(lpfc_sli_intf_sli_family,
5223 &phba->sli4_hba.sli_intf),
5224 bf_get(lpfc_sli_intf_slirev,
5225 &phba->sli4_hba.sli_intf),
5226 bf_get(lpfc_sli_intf_featurelevel1,
5227 &phba->sli4_hba.sli_intf),
5228 bf_get(lpfc_sli_intf_featurelevel2,
5229 &phba->sli4_hba.sli_intf));
5230 }
James Smarta747c9c2009-11-18 15:41:10 -05005231 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5232 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04005233 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05005234 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5235 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5236 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5237 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5239 "1422 HBA Unrecoverable error: "
5240 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5241 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5242 uerrlo_reg.word0, uerrhi_reg.word0,
5243 phba->sli4_hba.ue_mask_lo,
5244 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04005245 return -ENODEV;
5246 }
5247
James Smartda0436e2009-05-22 14:51:39 -04005248 return port_error;
5249}
5250
5251/**
5252 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5253 * @phba: pointer to lpfc hba data structure.
5254 *
5255 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5256 * memory map.
5257 **/
5258static void
5259lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5260{
5261 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5262 LPFC_UERR_STATUS_LO;
5263 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5264 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05005265 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5266 LPFC_UE_MASK_LO;
5267 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5268 LPFC_UE_MASK_HI;
James Smart28baac72010-02-12 14:42:03 -05005269 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5270 LPFC_SLI_INTF;
James Smartda0436e2009-05-22 14:51:39 -04005271}
5272
5273/**
5274 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5275 * @phba: pointer to lpfc hba data structure.
5276 *
5277 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5278 * memory map.
5279 **/
5280static void
5281lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5282{
5283
5284 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5285 LPFC_HST_STATE;
5286 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5287 LPFC_HST_ISR0;
5288 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5289 LPFC_HST_IMR0;
5290 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5291 LPFC_HST_ISCR0;
5292 return;
5293}
5294
5295/**
5296 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5297 * @phba: pointer to lpfc hba data structure.
5298 * @vf: virtual function number
5299 *
5300 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5301 * based on the given viftual function number, @vf.
5302 *
5303 * Return 0 if successful, otherwise -ENODEV.
5304 **/
5305static int
5306lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5307{
5308 if (vf > LPFC_VIR_FUNC_MAX)
5309 return -ENODEV;
5310
5311 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5312 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5313 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5314 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5315 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5316 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5317 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5318 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5319 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5320 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5321 return 0;
5322}
5323
5324/**
5325 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5326 * @phba: pointer to lpfc hba data structure.
5327 *
5328 * This routine is invoked to create the bootstrap mailbox
5329 * region consistent with the SLI-4 interface spec. This
5330 * routine allocates all memory necessary to communicate
5331 * mailbox commands to the port and sets up all alignment
5332 * needs. No locks are expected to be held when calling
5333 * this routine.
5334 *
5335 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005336 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005337 * ENOMEM - could not allocated memory.
5338 **/
5339static int
5340lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5341{
5342 uint32_t bmbx_size;
5343 struct lpfc_dmabuf *dmabuf;
5344 struct dma_address *dma_address;
5345 uint32_t pa_addr;
5346 uint64_t phys_addr;
5347
5348 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5349 if (!dmabuf)
5350 return -ENOMEM;
5351
5352 /*
5353 * The bootstrap mailbox region is comprised of 2 parts
5354 * plus an alignment restriction of 16 bytes.
5355 */
5356 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5357 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5358 bmbx_size,
5359 &dmabuf->phys,
5360 GFP_KERNEL);
5361 if (!dmabuf->virt) {
5362 kfree(dmabuf);
5363 return -ENOMEM;
5364 }
5365 memset(dmabuf->virt, 0, bmbx_size);
5366
5367 /*
5368 * Initialize the bootstrap mailbox pointers now so that the register
5369 * operations are simple later. The mailbox dma address is required
5370 * to be 16-byte aligned. Also align the virtual memory as each
5371 * maibox is copied into the bmbx mailbox region before issuing the
5372 * command to the port.
5373 */
5374 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5375 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5376
5377 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5378 LPFC_ALIGN_16_BYTE);
5379 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5380 LPFC_ALIGN_16_BYTE);
5381
5382 /*
5383 * Set the high and low physical addresses now. The SLI4 alignment
5384 * requirement is 16 bytes and the mailbox is posted to the port
5385 * as two 30-bit addresses. The other data is a bit marking whether
5386 * the 30-bit address is the high or low address.
5387 * Upcast bmbx aphys to 64bits so shift instruction compiles
5388 * clean on 32 bit machines.
5389 */
5390 dma_address = &phba->sli4_hba.bmbx.dma_address;
5391 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5392 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5393 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5394 LPFC_BMBX_BIT1_ADDR_HI);
5395
5396 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5397 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5398 LPFC_BMBX_BIT1_ADDR_LO);
5399 return 0;
5400}
5401
5402/**
5403 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5404 * @phba: pointer to lpfc hba data structure.
5405 *
5406 * This routine is invoked to teardown the bootstrap mailbox
5407 * region and release all host resources. This routine requires
5408 * the caller to ensure all mailbox commands recovered, no
5409 * additional mailbox comands are sent, and interrupts are disabled
5410 * before calling this routine.
5411 *
5412 **/
5413static void
5414lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5415{
5416 dma_free_coherent(&phba->pcidev->dev,
5417 phba->sli4_hba.bmbx.bmbx_size,
5418 phba->sli4_hba.bmbx.dmabuf->virt,
5419 phba->sli4_hba.bmbx.dmabuf->phys);
5420
5421 kfree(phba->sli4_hba.bmbx.dmabuf);
5422 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5423}
5424
5425/**
5426 * lpfc_sli4_read_config - Get the config parameters.
5427 * @phba: pointer to lpfc hba data structure.
5428 *
5429 * This routine is invoked to read the configuration parameters from the HBA.
5430 * The configuration parameters are used to set the base and maximum values
5431 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5432 * allocation for the port.
5433 *
5434 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005435 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005436 * ENOMEM - No availble memory
5437 * EIO - The mailbox failed to complete successfully.
5438 **/
5439static int
5440lpfc_sli4_read_config(struct lpfc_hba *phba)
5441{
5442 LPFC_MBOXQ_t *pmb;
5443 struct lpfc_mbx_read_config *rd_config;
5444 uint32_t rc = 0;
5445
5446 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5447 if (!pmb) {
5448 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5449 "2011 Unable to allocate memory for issuing "
5450 "SLI_CONFIG_SPECIAL mailbox command\n");
5451 return -ENOMEM;
5452 }
5453
5454 lpfc_read_config(phba, pmb);
5455
5456 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5457 if (rc != MBX_SUCCESS) {
5458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5459 "2012 Mailbox failed , mbxCmd x%x "
5460 "READ_CONFIG, mbxStatus x%x\n",
5461 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5462 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5463 rc = -EIO;
5464 } else {
5465 rd_config = &pmb->u.mqe.un.rd_config;
5466 phba->sli4_hba.max_cfg_param.max_xri =
5467 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5468 phba->sli4_hba.max_cfg_param.xri_base =
5469 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5470 phba->sli4_hba.max_cfg_param.max_vpi =
5471 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5472 phba->sli4_hba.max_cfg_param.vpi_base =
5473 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5474 phba->sli4_hba.max_cfg_param.max_rpi =
5475 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5476 phba->sli4_hba.max_cfg_param.rpi_base =
5477 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5478 phba->sli4_hba.max_cfg_param.max_vfi =
5479 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5480 phba->sli4_hba.max_cfg_param.vfi_base =
5481 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5482 phba->sli4_hba.max_cfg_param.max_fcfi =
5483 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5484 phba->sli4_hba.max_cfg_param.fcfi_base =
5485 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5486 phba->sli4_hba.max_cfg_param.max_eq =
5487 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5488 phba->sli4_hba.max_cfg_param.max_rq =
5489 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5490 phba->sli4_hba.max_cfg_param.max_wq =
5491 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5492 phba->sli4_hba.max_cfg_param.max_cq =
5493 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5494 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5495 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5496 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5497 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5498 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005499 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5500 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005501 phba->max_vports = phba->max_vpi;
5502 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5503 "2003 cfg params XRI(B:%d M:%d), "
5504 "VPI(B:%d M:%d) "
5505 "VFI(B:%d M:%d) "
5506 "RPI(B:%d M:%d) "
5507 "FCFI(B:%d M:%d)\n",
5508 phba->sli4_hba.max_cfg_param.xri_base,
5509 phba->sli4_hba.max_cfg_param.max_xri,
5510 phba->sli4_hba.max_cfg_param.vpi_base,
5511 phba->sli4_hba.max_cfg_param.max_vpi,
5512 phba->sli4_hba.max_cfg_param.vfi_base,
5513 phba->sli4_hba.max_cfg_param.max_vfi,
5514 phba->sli4_hba.max_cfg_param.rpi_base,
5515 phba->sli4_hba.max_cfg_param.max_rpi,
5516 phba->sli4_hba.max_cfg_param.fcfi_base,
5517 phba->sli4_hba.max_cfg_param.max_fcfi);
5518 }
5519 mempool_free(pmb, phba->mbox_mem_pool);
5520
5521 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5522 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5523 phba->cfg_hba_queue_depth =
5524 phba->sli4_hba.max_cfg_param.max_xri;
5525 return rc;
5526}
5527
5528/**
5529 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5530 * @phba: pointer to lpfc hba data structure.
5531 *
5532 * This routine is invoked to setup the host-side endian order to the
5533 * HBA consistent with the SLI-4 interface spec.
5534 *
5535 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005536 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005537 * ENOMEM - No availble memory
5538 * EIO - The mailbox failed to complete successfully.
5539 **/
5540static int
5541lpfc_setup_endian_order(struct lpfc_hba *phba)
5542{
5543 LPFC_MBOXQ_t *mboxq;
5544 uint32_t rc = 0;
5545 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5546 HOST_ENDIAN_HIGH_WORD1};
5547
5548 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5549 if (!mboxq) {
5550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5551 "0492 Unable to allocate memory for issuing "
5552 "SLI_CONFIG_SPECIAL mailbox command\n");
5553 return -ENOMEM;
5554 }
5555
5556 /*
5557 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5558 * words to contain special data values and no other data.
5559 */
5560 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5561 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5562 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5563 if (rc != MBX_SUCCESS) {
5564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5565 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5566 "status x%x\n",
5567 rc);
5568 rc = -EIO;
5569 }
5570
5571 mempool_free(mboxq, phba->mbox_mem_pool);
5572 return rc;
5573}
5574
5575/**
5576 * lpfc_sli4_queue_create - Create all the SLI4 queues
5577 * @phba: pointer to lpfc hba data structure.
5578 *
5579 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5580 * operation. For each SLI4 queue type, the parameters such as queue entry
5581 * count (queue depth) shall be taken from the module parameter. For now,
5582 * we just use some constant number as place holder.
5583 *
5584 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005585 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005586 * ENOMEM - No availble memory
5587 * EIO - The mailbox failed to complete successfully.
5588 **/
5589static int
5590lpfc_sli4_queue_create(struct lpfc_hba *phba)
5591{
5592 struct lpfc_queue *qdesc;
5593 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5594 int cfg_fcp_wq_count;
5595 int cfg_fcp_eq_count;
5596
5597 /*
5598 * Sanity check for confiugred queue parameters against the run-time
5599 * device parameters
5600 */
5601
5602 /* Sanity check on FCP fast-path WQ parameters */
5603 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5604 if (cfg_fcp_wq_count >
5605 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5606 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5607 LPFC_SP_WQN_DEF;
5608 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5610 "2581 Not enough WQs (%d) from "
5611 "the pci function for supporting "
5612 "FCP WQs (%d)\n",
5613 phba->sli4_hba.max_cfg_param.max_wq,
5614 phba->cfg_fcp_wq_count);
5615 goto out_error;
5616 }
5617 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5618 "2582 Not enough WQs (%d) from the pci "
5619 "function for supporting the requested "
5620 "FCP WQs (%d), the actual FCP WQs can "
5621 "be supported: %d\n",
5622 phba->sli4_hba.max_cfg_param.max_wq,
5623 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5624 }
5625 /* The actual number of FCP work queues adopted */
5626 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5627
5628 /* Sanity check on FCP fast-path EQ parameters */
5629 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5630 if (cfg_fcp_eq_count >
5631 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5632 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5633 LPFC_SP_EQN_DEF;
5634 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5636 "2574 Not enough EQs (%d) from the "
5637 "pci function for supporting FCP "
5638 "EQs (%d)\n",
5639 phba->sli4_hba.max_cfg_param.max_eq,
5640 phba->cfg_fcp_eq_count);
5641 goto out_error;
5642 }
5643 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5644 "2575 Not enough EQs (%d) from the pci "
5645 "function for supporting the requested "
5646 "FCP EQs (%d), the actual FCP EQs can "
5647 "be supported: %d\n",
5648 phba->sli4_hba.max_cfg_param.max_eq,
5649 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5650 }
5651 /* It does not make sense to have more EQs than WQs */
5652 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5653 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005654 "2593 The FCP EQ count(%d) cannot be greater "
5655 "than the FCP WQ count(%d), limiting the "
5656 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005657 phba->cfg_fcp_wq_count,
5658 phba->cfg_fcp_wq_count);
5659 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5660 }
5661 /* The actual number of FCP event queues adopted */
5662 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5663 /* The overall number of event queues used */
5664 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5665
5666 /*
5667 * Create Event Queues (EQs)
5668 */
5669
5670 /* Get EQ depth from module parameter, fake the default for now */
5671 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5672 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5673
5674 /* Create slow path event queue */
5675 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5676 phba->sli4_hba.eq_ecount);
5677 if (!qdesc) {
5678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5679 "0496 Failed allocate slow-path EQ\n");
5680 goto out_error;
5681 }
5682 phba->sli4_hba.sp_eq = qdesc;
5683
5684 /* Create fast-path FCP Event Queue(s) */
5685 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5686 phba->cfg_fcp_eq_count), GFP_KERNEL);
5687 if (!phba->sli4_hba.fp_eq) {
5688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5689 "2576 Failed allocate memory for fast-path "
5690 "EQ record array\n");
5691 goto out_free_sp_eq;
5692 }
5693 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5694 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5695 phba->sli4_hba.eq_ecount);
5696 if (!qdesc) {
5697 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5698 "0497 Failed allocate fast-path EQ\n");
5699 goto out_free_fp_eq;
5700 }
5701 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5702 }
5703
5704 /*
5705 * Create Complete Queues (CQs)
5706 */
5707
5708 /* Get CQ depth from module parameter, fake the default for now */
5709 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5710 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5711
5712 /* Create slow-path Mailbox Command Complete Queue */
5713 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5714 phba->sli4_hba.cq_ecount);
5715 if (!qdesc) {
5716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5717 "0500 Failed allocate slow-path mailbox CQ\n");
5718 goto out_free_fp_eq;
5719 }
5720 phba->sli4_hba.mbx_cq = qdesc;
5721
5722 /* Create slow-path ELS Complete Queue */
5723 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5724 phba->sli4_hba.cq_ecount);
5725 if (!qdesc) {
5726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5727 "0501 Failed allocate slow-path ELS CQ\n");
5728 goto out_free_mbx_cq;
5729 }
5730 phba->sli4_hba.els_cq = qdesc;
5731
James Smartda0436e2009-05-22 14:51:39 -04005732
5733 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5734 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5735 phba->cfg_fcp_eq_count), GFP_KERNEL);
5736 if (!phba->sli4_hba.fcp_cq) {
5737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5738 "2577 Failed allocate memory for fast-path "
5739 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005740 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005741 }
5742 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5743 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5744 phba->sli4_hba.cq_ecount);
5745 if (!qdesc) {
5746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5747 "0499 Failed allocate fast-path FCP "
5748 "CQ (%d)\n", fcp_cqidx);
5749 goto out_free_fcp_cq;
5750 }
5751 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5752 }
5753
5754 /* Create Mailbox Command Queue */
5755 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5756 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5757
5758 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5759 phba->sli4_hba.mq_ecount);
5760 if (!qdesc) {
5761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5762 "0505 Failed allocate slow-path MQ\n");
5763 goto out_free_fcp_cq;
5764 }
5765 phba->sli4_hba.mbx_wq = qdesc;
5766
5767 /*
5768 * Create all the Work Queues (WQs)
5769 */
5770 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5771 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5772
5773 /* Create slow-path ELS Work Queue */
5774 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5775 phba->sli4_hba.wq_ecount);
5776 if (!qdesc) {
5777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5778 "0504 Failed allocate slow-path ELS WQ\n");
5779 goto out_free_mbx_wq;
5780 }
5781 phba->sli4_hba.els_wq = qdesc;
5782
5783 /* Create fast-path FCP Work Queue(s) */
5784 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5785 phba->cfg_fcp_wq_count), GFP_KERNEL);
5786 if (!phba->sli4_hba.fcp_wq) {
5787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5788 "2578 Failed allocate memory for fast-path "
5789 "WQ record array\n");
5790 goto out_free_els_wq;
5791 }
5792 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5793 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5794 phba->sli4_hba.wq_ecount);
5795 if (!qdesc) {
5796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5797 "0503 Failed allocate fast-path FCP "
5798 "WQ (%d)\n", fcp_wqidx);
5799 goto out_free_fcp_wq;
5800 }
5801 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5802 }
5803
5804 /*
5805 * Create Receive Queue (RQ)
5806 */
5807 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5808 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5809
5810 /* Create Receive Queue for header */
5811 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5812 phba->sli4_hba.rq_ecount);
5813 if (!qdesc) {
5814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5815 "0506 Failed allocate receive HRQ\n");
5816 goto out_free_fcp_wq;
5817 }
5818 phba->sli4_hba.hdr_rq = qdesc;
5819
5820 /* Create Receive Queue for data */
5821 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5822 phba->sli4_hba.rq_ecount);
5823 if (!qdesc) {
5824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5825 "0507 Failed allocate receive DRQ\n");
5826 goto out_free_hdr_rq;
5827 }
5828 phba->sli4_hba.dat_rq = qdesc;
5829
5830 return 0;
5831
5832out_free_hdr_rq:
5833 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5834 phba->sli4_hba.hdr_rq = NULL;
5835out_free_fcp_wq:
5836 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5837 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5838 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5839 }
5840 kfree(phba->sli4_hba.fcp_wq);
5841out_free_els_wq:
5842 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5843 phba->sli4_hba.els_wq = NULL;
5844out_free_mbx_wq:
5845 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5846 phba->sli4_hba.mbx_wq = NULL;
5847out_free_fcp_cq:
5848 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5849 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5850 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5851 }
5852 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005853out_free_els_cq:
5854 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5855 phba->sli4_hba.els_cq = NULL;
5856out_free_mbx_cq:
5857 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5858 phba->sli4_hba.mbx_cq = NULL;
5859out_free_fp_eq:
5860 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5861 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5862 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5863 }
5864 kfree(phba->sli4_hba.fp_eq);
5865out_free_sp_eq:
5866 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5867 phba->sli4_hba.sp_eq = NULL;
5868out_error:
5869 return -ENOMEM;
5870}
5871
5872/**
5873 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5874 * @phba: pointer to lpfc hba data structure.
5875 *
5876 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5877 * operation.
5878 *
5879 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005880 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005881 * ENOMEM - No availble memory
5882 * EIO - The mailbox failed to complete successfully.
5883 **/
5884static void
5885lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5886{
5887 int fcp_qidx;
5888
5889 /* Release mailbox command work queue */
5890 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5891 phba->sli4_hba.mbx_wq = NULL;
5892
5893 /* Release ELS work queue */
5894 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5895 phba->sli4_hba.els_wq = NULL;
5896
5897 /* Release FCP work queue */
5898 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5899 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5900 kfree(phba->sli4_hba.fcp_wq);
5901 phba->sli4_hba.fcp_wq = NULL;
5902
5903 /* Release unsolicited receive queue */
5904 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5905 phba->sli4_hba.hdr_rq = NULL;
5906 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5907 phba->sli4_hba.dat_rq = NULL;
5908
James Smartda0436e2009-05-22 14:51:39 -04005909 /* Release ELS complete queue */
5910 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5911 phba->sli4_hba.els_cq = NULL;
5912
5913 /* Release mailbox command complete queue */
5914 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5915 phba->sli4_hba.mbx_cq = NULL;
5916
5917 /* Release FCP response complete queue */
5918 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5919 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5920 kfree(phba->sli4_hba.fcp_cq);
5921 phba->sli4_hba.fcp_cq = NULL;
5922
5923 /* Release fast-path event queue */
5924 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5925 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5926 kfree(phba->sli4_hba.fp_eq);
5927 phba->sli4_hba.fp_eq = NULL;
5928
5929 /* Release slow-path event queue */
5930 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5931 phba->sli4_hba.sp_eq = NULL;
5932
5933 return;
5934}
5935
5936/**
5937 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5938 * @phba: pointer to lpfc hba data structure.
5939 *
5940 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5941 * operation.
5942 *
5943 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005944 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005945 * ENOMEM - No availble memory
5946 * EIO - The mailbox failed to complete successfully.
5947 **/
5948int
5949lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5950{
5951 int rc = -ENOMEM;
5952 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5953 int fcp_cq_index = 0;
5954
5955 /*
5956 * Set up Event Queues (EQs)
5957 */
5958
5959 /* Set up slow-path event queue */
5960 if (!phba->sli4_hba.sp_eq) {
5961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5962 "0520 Slow-path EQ not allocated\n");
5963 goto out_error;
5964 }
5965 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5966 LPFC_SP_DEF_IMAX);
5967 if (rc) {
5968 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5969 "0521 Failed setup of slow-path EQ: "
5970 "rc = 0x%x\n", rc);
5971 goto out_error;
5972 }
5973 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5974 "2583 Slow-path EQ setup: queue-id=%d\n",
5975 phba->sli4_hba.sp_eq->queue_id);
5976
5977 /* Set up fast-path event queue */
5978 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5979 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5981 "0522 Fast-path EQ (%d) not "
5982 "allocated\n", fcp_eqidx);
5983 goto out_destroy_fp_eq;
5984 }
5985 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5986 phba->cfg_fcp_imax);
5987 if (rc) {
5988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5989 "0523 Failed setup of fast-path EQ "
5990 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5991 goto out_destroy_fp_eq;
5992 }
5993 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5994 "2584 Fast-path EQ setup: "
5995 "queue[%d]-id=%d\n", fcp_eqidx,
5996 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5997 }
5998
5999 /*
6000 * Set up Complete Queues (CQs)
6001 */
6002
6003 /* Set up slow-path MBOX Complete Queue as the first CQ */
6004 if (!phba->sli4_hba.mbx_cq) {
6005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6006 "0528 Mailbox CQ not allocated\n");
6007 goto out_destroy_fp_eq;
6008 }
6009 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6010 LPFC_MCQ, LPFC_MBOX);
6011 if (rc) {
6012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6013 "0529 Failed setup of slow-path mailbox CQ: "
6014 "rc = 0x%x\n", rc);
6015 goto out_destroy_fp_eq;
6016 }
6017 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6018 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6019 phba->sli4_hba.mbx_cq->queue_id,
6020 phba->sli4_hba.sp_eq->queue_id);
6021
6022 /* Set up slow-path ELS Complete Queue */
6023 if (!phba->sli4_hba.els_cq) {
6024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6025 "0530 ELS CQ not allocated\n");
6026 goto out_destroy_mbx_cq;
6027 }
6028 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6029 LPFC_WCQ, LPFC_ELS);
6030 if (rc) {
6031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6032 "0531 Failed setup of slow-path ELS CQ: "
6033 "rc = 0x%x\n", rc);
6034 goto out_destroy_mbx_cq;
6035 }
6036 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6037 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6038 phba->sli4_hba.els_cq->queue_id,
6039 phba->sli4_hba.sp_eq->queue_id);
6040
James Smartda0436e2009-05-22 14:51:39 -04006041 /* Set up fast-path FCP Response Complete Queue */
6042 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6043 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6044 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6045 "0526 Fast-path FCP CQ (%d) not "
6046 "allocated\n", fcp_cqidx);
6047 goto out_destroy_fcp_cq;
6048 }
6049 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
6050 phba->sli4_hba.fp_eq[fcp_cqidx],
6051 LPFC_WCQ, LPFC_FCP);
6052 if (rc) {
6053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6054 "0527 Failed setup of fast-path FCP "
6055 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6056 goto out_destroy_fcp_cq;
6057 }
6058 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6059 "2588 FCP CQ setup: cq[%d]-id=%d, "
6060 "parent eq[%d]-id=%d\n",
6061 fcp_cqidx,
6062 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6063 fcp_cqidx,
6064 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
6065 }
6066
6067 /*
6068 * Set up all the Work Queues (WQs)
6069 */
6070
6071 /* Set up Mailbox Command Queue */
6072 if (!phba->sli4_hba.mbx_wq) {
6073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6074 "0538 Slow-path MQ not allocated\n");
6075 goto out_destroy_fcp_cq;
6076 }
6077 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6078 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6079 if (rc) {
6080 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6081 "0539 Failed setup of slow-path MQ: "
6082 "rc = 0x%x\n", rc);
6083 goto out_destroy_fcp_cq;
6084 }
6085 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6086 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6087 phba->sli4_hba.mbx_wq->queue_id,
6088 phba->sli4_hba.mbx_cq->queue_id);
6089
6090 /* Set up slow-path ELS Work Queue */
6091 if (!phba->sli4_hba.els_wq) {
6092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6093 "0536 Slow-path ELS WQ not allocated\n");
6094 goto out_destroy_mbx_wq;
6095 }
6096 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6097 phba->sli4_hba.els_cq, LPFC_ELS);
6098 if (rc) {
6099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6100 "0537 Failed setup of slow-path ELS WQ: "
6101 "rc = 0x%x\n", rc);
6102 goto out_destroy_mbx_wq;
6103 }
6104 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6105 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6106 phba->sli4_hba.els_wq->queue_id,
6107 phba->sli4_hba.els_cq->queue_id);
6108
6109 /* Set up fast-path FCP Work Queue */
6110 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6111 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6112 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6113 "0534 Fast-path FCP WQ (%d) not "
6114 "allocated\n", fcp_wqidx);
6115 goto out_destroy_fcp_wq;
6116 }
6117 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6118 phba->sli4_hba.fcp_cq[fcp_cq_index],
6119 LPFC_FCP);
6120 if (rc) {
6121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6122 "0535 Failed setup of fast-path FCP "
6123 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6124 goto out_destroy_fcp_wq;
6125 }
6126 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6127 "2591 FCP WQ setup: wq[%d]-id=%d, "
6128 "parent cq[%d]-id=%d\n",
6129 fcp_wqidx,
6130 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6131 fcp_cq_index,
6132 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6133 /* Round robin FCP Work Queue's Completion Queue assignment */
6134 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
6135 }
6136
6137 /*
6138 * Create Receive Queue (RQ)
6139 */
6140 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6142 "0540 Receive Queue not allocated\n");
6143 goto out_destroy_fcp_wq;
6144 }
6145 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006146 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006147 if (rc) {
6148 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6149 "0541 Failed setup of Receive Queue: "
6150 "rc = 0x%x\n", rc);
6151 goto out_destroy_fcp_wq;
6152 }
6153 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6154 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6155 "parent cq-id=%d\n",
6156 phba->sli4_hba.hdr_rq->queue_id,
6157 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006158 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006159 return 0;
6160
6161out_destroy_fcp_wq:
6162 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6163 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6164 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6165out_destroy_mbx_wq:
6166 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6167out_destroy_fcp_cq:
6168 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6169 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04006170 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6171out_destroy_mbx_cq:
6172 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6173out_destroy_fp_eq:
6174 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6175 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6176 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6177out_error:
6178 return rc;
6179}
6180
6181/**
6182 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6183 * @phba: pointer to lpfc hba data structure.
6184 *
6185 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6186 * operation.
6187 *
6188 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006189 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006190 * ENOMEM - No availble memory
6191 * EIO - The mailbox failed to complete successfully.
6192 **/
6193void
6194lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6195{
6196 int fcp_qidx;
6197
6198 /* Unset mailbox command work queue */
6199 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6200 /* Unset ELS work queue */
6201 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6202 /* Unset unsolicited receive queue */
6203 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6204 /* Unset FCP work queue */
6205 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6206 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6207 /* Unset mailbox command complete queue */
6208 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6209 /* Unset ELS complete queue */
6210 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006211 /* Unset FCP response complete queue */
6212 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6213 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6214 /* Unset fast-path event queue */
6215 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6216 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6217 /* Unset slow-path event queue */
6218 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6219}
6220
6221/**
6222 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6223 * @phba: pointer to lpfc hba data structure.
6224 *
6225 * This routine is invoked to allocate and set up a pool of completion queue
6226 * events. The body of the completion queue event is a completion queue entry
6227 * CQE. For now, this pool is used for the interrupt service routine to queue
6228 * the following HBA completion queue events for the worker thread to process:
6229 * - Mailbox asynchronous events
6230 * - Receive queue completion unsolicited events
6231 * Later, this can be used for all the slow-path events.
6232 *
6233 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006234 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006235 * -ENOMEM - No availble memory
6236 **/
6237static int
6238lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6239{
6240 struct lpfc_cq_event *cq_event;
6241 int i;
6242
6243 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6244 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6245 if (!cq_event)
6246 goto out_pool_create_fail;
6247 list_add_tail(&cq_event->list,
6248 &phba->sli4_hba.sp_cqe_event_pool);
6249 }
6250 return 0;
6251
6252out_pool_create_fail:
6253 lpfc_sli4_cq_event_pool_destroy(phba);
6254 return -ENOMEM;
6255}
6256
6257/**
6258 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6259 * @phba: pointer to lpfc hba data structure.
6260 *
6261 * This routine is invoked to free the pool of completion queue events at
6262 * driver unload time. Note that, it is the responsibility of the driver
6263 * cleanup routine to free all the outstanding completion-queue events
6264 * allocated from this pool back into the pool before invoking this routine
6265 * to destroy the pool.
6266 **/
6267static void
6268lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6269{
6270 struct lpfc_cq_event *cq_event, *next_cq_event;
6271
6272 list_for_each_entry_safe(cq_event, next_cq_event,
6273 &phba->sli4_hba.sp_cqe_event_pool, list) {
6274 list_del(&cq_event->list);
6275 kfree(cq_event);
6276 }
6277}
6278
6279/**
6280 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6281 * @phba: pointer to lpfc hba data structure.
6282 *
6283 * This routine is the lock free version of the API invoked to allocate a
6284 * completion-queue event from the free pool.
6285 *
6286 * Return: Pointer to the newly allocated completion-queue event if successful
6287 * NULL otherwise.
6288 **/
6289struct lpfc_cq_event *
6290__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6291{
6292 struct lpfc_cq_event *cq_event = NULL;
6293
6294 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6295 struct lpfc_cq_event, list);
6296 return cq_event;
6297}
6298
6299/**
6300 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6301 * @phba: pointer to lpfc hba data structure.
6302 *
6303 * This routine is the lock version of the API invoked to allocate a
6304 * completion-queue event from the free pool.
6305 *
6306 * Return: Pointer to the newly allocated completion-queue event if successful
6307 * NULL otherwise.
6308 **/
6309struct lpfc_cq_event *
6310lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6311{
6312 struct lpfc_cq_event *cq_event;
6313 unsigned long iflags;
6314
6315 spin_lock_irqsave(&phba->hbalock, iflags);
6316 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6317 spin_unlock_irqrestore(&phba->hbalock, iflags);
6318 return cq_event;
6319}
6320
6321/**
6322 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6323 * @phba: pointer to lpfc hba data structure.
6324 * @cq_event: pointer to the completion queue event to be freed.
6325 *
6326 * This routine is the lock free version of the API invoked to release a
6327 * completion-queue event back into the free pool.
6328 **/
6329void
6330__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6331 struct lpfc_cq_event *cq_event)
6332{
6333 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6334}
6335
6336/**
6337 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6338 * @phba: pointer to lpfc hba data structure.
6339 * @cq_event: pointer to the completion queue event to be freed.
6340 *
6341 * This routine is the lock version of the API invoked to release a
6342 * completion-queue event back into the free pool.
6343 **/
6344void
6345lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6346 struct lpfc_cq_event *cq_event)
6347{
6348 unsigned long iflags;
6349 spin_lock_irqsave(&phba->hbalock, iflags);
6350 __lpfc_sli4_cq_event_release(phba, cq_event);
6351 spin_unlock_irqrestore(&phba->hbalock, iflags);
6352}
6353
6354/**
6355 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6356 * @phba: pointer to lpfc hba data structure.
6357 *
6358 * This routine is to free all the pending completion-queue events to the
6359 * back into the free pool for device reset.
6360 **/
6361static void
6362lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6363{
6364 LIST_HEAD(cqelist);
6365 struct lpfc_cq_event *cqe;
6366 unsigned long iflags;
6367
6368 /* Retrieve all the pending WCQEs from pending WCQE lists */
6369 spin_lock_irqsave(&phba->hbalock, iflags);
6370 /* Pending FCP XRI abort events */
6371 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6372 &cqelist);
6373 /* Pending ELS XRI abort events */
6374 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6375 &cqelist);
6376 /* Pending asynnc events */
6377 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6378 &cqelist);
6379 spin_unlock_irqrestore(&phba->hbalock, iflags);
6380
6381 while (!list_empty(&cqelist)) {
6382 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6383 lpfc_sli4_cq_event_release(phba, cqe);
6384 }
6385}
6386
6387/**
6388 * lpfc_pci_function_reset - Reset pci function.
6389 * @phba: pointer to lpfc hba data structure.
6390 *
6391 * This routine is invoked to request a PCI function reset. It will destroys
6392 * all resources assigned to the PCI function which originates this request.
6393 *
6394 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006395 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006396 * ENOMEM - No availble memory
6397 * EIO - The mailbox failed to complete successfully.
6398 **/
6399int
6400lpfc_pci_function_reset(struct lpfc_hba *phba)
6401{
6402 LPFC_MBOXQ_t *mboxq;
6403 uint32_t rc = 0;
6404 uint32_t shdr_status, shdr_add_status;
6405 union lpfc_sli4_cfg_shdr *shdr;
6406
6407 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6408 if (!mboxq) {
6409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6410 "0494 Unable to allocate memory for issuing "
6411 "SLI_FUNCTION_RESET mailbox command\n");
6412 return -ENOMEM;
6413 }
6414
6415 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6416 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6417 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6418 LPFC_SLI4_MBX_EMBED);
6419 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6420 shdr = (union lpfc_sli4_cfg_shdr *)
6421 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6422 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6423 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6424 if (rc != MBX_TIMEOUT)
6425 mempool_free(mboxq, phba->mbox_mem_pool);
6426 if (shdr_status || shdr_add_status || rc) {
6427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6428 "0495 SLI_FUNCTION_RESET mailbox failed with "
6429 "status x%x add_status x%x, mbx status x%x\n",
6430 shdr_status, shdr_add_status, rc);
6431 rc = -ENXIO;
6432 }
6433 return rc;
6434}
6435
6436/**
6437 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6438 * @phba: pointer to lpfc hba data structure.
6439 * @cnt: number of nop mailbox commands to send.
6440 *
6441 * This routine is invoked to send a number @cnt of NOP mailbox command and
6442 * wait for each command to complete.
6443 *
6444 * Return: the number of NOP mailbox command completed.
6445 **/
6446static int
6447lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6448{
6449 LPFC_MBOXQ_t *mboxq;
6450 int length, cmdsent;
6451 uint32_t mbox_tmo;
6452 uint32_t rc = 0;
6453 uint32_t shdr_status, shdr_add_status;
6454 union lpfc_sli4_cfg_shdr *shdr;
6455
6456 if (cnt == 0) {
6457 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6458 "2518 Requested to send 0 NOP mailbox cmd\n");
6459 return cnt;
6460 }
6461
6462 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6463 if (!mboxq) {
6464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6465 "2519 Unable to allocate memory for issuing "
6466 "NOP mailbox command\n");
6467 return 0;
6468 }
6469
6470 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6471 length = (sizeof(struct lpfc_mbx_nop) -
6472 sizeof(struct lpfc_sli4_cfg_mhdr));
6473 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6474 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6475
6476 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6477 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6478 if (!phba->sli4_hba.intr_enable)
6479 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6480 else
6481 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6482 if (rc == MBX_TIMEOUT)
6483 break;
6484 /* Check return status */
6485 shdr = (union lpfc_sli4_cfg_shdr *)
6486 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6487 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6488 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6489 &shdr->response);
6490 if (shdr_status || shdr_add_status || rc) {
6491 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6492 "2520 NOP mailbox command failed "
6493 "status x%x add_status x%x mbx "
6494 "status x%x\n", shdr_status,
6495 shdr_add_status, rc);
6496 break;
6497 }
6498 }
6499
6500 if (rc != MBX_TIMEOUT)
6501 mempool_free(mboxq, phba->mbox_mem_pool);
6502
6503 return cmdsent;
6504}
6505
6506/**
6507 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6508 * @phba: pointer to lpfc hba data structure.
6509 * @fcfi: fcf index.
6510 *
6511 * This routine is invoked to unregister a FCFI from device.
6512 **/
6513void
6514lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6515{
6516 LPFC_MBOXQ_t *mbox;
6517 uint32_t mbox_tmo;
6518 int rc;
6519 unsigned long flags;
6520
6521 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6522
6523 if (!mbox)
6524 return;
6525
6526 lpfc_unreg_fcfi(mbox, fcfi);
6527
6528 if (!phba->sli4_hba.intr_enable)
6529 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6530 else {
6531 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6532 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6533 }
6534 if (rc != MBX_TIMEOUT)
6535 mempool_free(mbox, phba->mbox_mem_pool);
6536 if (rc != MBX_SUCCESS)
6537 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6538 "2517 Unregister FCFI command failed "
6539 "status %d, mbxStatus x%x\n", rc,
6540 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6541 else {
6542 spin_lock_irqsave(&phba->hbalock, flags);
6543 /* Mark the FCFI is no longer registered */
6544 phba->fcf.fcf_flag &=
James Smartecfd03c2010-02-12 14:41:27 -05006545 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
James Smartda0436e2009-05-22 14:51:39 -04006546 spin_unlock_irqrestore(&phba->hbalock, flags);
6547 }
6548}
6549
6550/**
6551 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6552 * @phba: pointer to lpfc hba data structure.
6553 *
6554 * This routine is invoked to set up the PCI device memory space for device
6555 * with SLI-4 interface spec.
6556 *
6557 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006558 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006559 * other values - error
6560 **/
6561static int
6562lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6563{
6564 struct pci_dev *pdev;
6565 unsigned long bar0map_len, bar1map_len, bar2map_len;
6566 int error = -ENODEV;
6567
6568 /* Obtain PCI device reference */
6569 if (!phba->pcidev)
6570 return error;
6571 else
6572 pdev = phba->pcidev;
6573
6574 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006575 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6576 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6577 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6578 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006579 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006580 }
6581 }
James Smartda0436e2009-05-22 14:51:39 -04006582
6583 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6584 * number of bytes required by each mapping. They are actually
James Smart1dfb5a42010-02-12 14:40:50 -05006585 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006586 */
James Smart1dfb5a42010-02-12 14:40:50 -05006587 if (pci_resource_start(pdev, 0)) {
6588 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6589 bar0map_len = pci_resource_len(pdev, 0);
6590 } else {
6591 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6592 bar0map_len = pci_resource_len(pdev, 1);
6593 }
6594 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6595 bar1map_len = pci_resource_len(pdev, 2);
James Smartda0436e2009-05-22 14:51:39 -04006596
James Smart1dfb5a42010-02-12 14:40:50 -05006597 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6598 bar2map_len = pci_resource_len(pdev, 4);
James Smartda0436e2009-05-22 14:51:39 -04006599
6600 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6601 phba->sli4_hba.conf_regs_memmap_p =
6602 ioremap(phba->pci_bar0_map, bar0map_len);
6603 if (!phba->sli4_hba.conf_regs_memmap_p) {
6604 dev_printk(KERN_ERR, &pdev->dev,
6605 "ioremap failed for SLI4 PCI config registers.\n");
6606 goto out;
6607 }
6608
6609 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6610 phba->sli4_hba.ctrl_regs_memmap_p =
6611 ioremap(phba->pci_bar1_map, bar1map_len);
6612 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6613 dev_printk(KERN_ERR, &pdev->dev,
6614 "ioremap failed for SLI4 HBA control registers.\n");
6615 goto out_iounmap_conf;
6616 }
6617
6618 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6619 phba->sli4_hba.drbl_regs_memmap_p =
6620 ioremap(phba->pci_bar2_map, bar2map_len);
6621 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6622 dev_printk(KERN_ERR, &pdev->dev,
6623 "ioremap failed for SLI4 HBA doorbell registers.\n");
6624 goto out_iounmap_ctrl;
6625 }
6626
6627 /* Set up BAR0 PCI config space register memory map */
6628 lpfc_sli4_bar0_register_memmap(phba);
6629
6630 /* Set up BAR1 register memory map */
6631 lpfc_sli4_bar1_register_memmap(phba);
6632
6633 /* Set up BAR2 register memory map */
6634 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6635 if (error)
6636 goto out_iounmap_all;
6637
6638 return 0;
6639
6640out_iounmap_all:
6641 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6642out_iounmap_ctrl:
6643 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6644out_iounmap_conf:
6645 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6646out:
6647 return error;
6648}
6649
6650/**
6651 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6652 * @phba: pointer to lpfc hba data structure.
6653 *
6654 * This routine is invoked to unset the PCI device memory space for device
6655 * with SLI-4 interface spec.
6656 **/
6657static void
6658lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6659{
6660 struct pci_dev *pdev;
6661
6662 /* Obtain PCI device reference */
6663 if (!phba->pcidev)
6664 return;
6665 else
6666 pdev = phba->pcidev;
6667
6668 /* Free coherent DMA memory allocated */
6669
6670 /* Unmap I/O memory space */
6671 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6672 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6673 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6674
6675 return;
6676}
6677
6678/**
James Smart3772a992009-05-22 14:50:54 -04006679 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6680 * @phba: pointer to lpfc hba data structure.
6681 *
6682 * This routine is invoked to enable the MSI-X interrupt vectors to device
6683 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6684 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6685 * invoked, enables either all or nothing, depending on the current
6686 * availability of PCI vector resources. The device driver is responsible
6687 * for calling the individual request_irq() to register each MSI-X vector
6688 * with a interrupt handler, which is done in this function. Note that
6689 * later when device is unloading, the driver should always call free_irq()
6690 * on all MSI-X vectors it has done request_irq() on before calling
6691 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6692 * will be left with MSI-X enabled and leaks its vectors.
6693 *
6694 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006695 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006696 * other values - error
6697 **/
6698static int
6699lpfc_sli_enable_msix(struct lpfc_hba *phba)
6700{
6701 int rc, i;
6702 LPFC_MBOXQ_t *pmb;
6703
6704 /* Set up MSI-X multi-message vectors */
6705 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6706 phba->msix_entries[i].entry = i;
6707
6708 /* Configure MSI-X capability structure */
6709 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6710 ARRAY_SIZE(phba->msix_entries));
6711 if (rc) {
6712 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6713 "0420 PCI enable MSI-X failed (%d)\n", rc);
6714 goto msi_fail_out;
6715 }
6716 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6717 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6718 "0477 MSI-X entry[%d]: vector=x%x "
6719 "message=%d\n", i,
6720 phba->msix_entries[i].vector,
6721 phba->msix_entries[i].entry);
6722 /*
6723 * Assign MSI-X vectors to interrupt handlers
6724 */
6725
6726 /* vector-0 is associated to slow-path handler */
6727 rc = request_irq(phba->msix_entries[0].vector,
6728 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6729 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6730 if (rc) {
6731 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6732 "0421 MSI-X slow-path request_irq failed "
6733 "(%d)\n", rc);
6734 goto msi_fail_out;
6735 }
6736
6737 /* vector-1 is associated to fast-path handler */
6738 rc = request_irq(phba->msix_entries[1].vector,
6739 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6740 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6741
6742 if (rc) {
6743 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6744 "0429 MSI-X fast-path request_irq failed "
6745 "(%d)\n", rc);
6746 goto irq_fail_out;
6747 }
6748
6749 /*
6750 * Configure HBA MSI-X attention conditions to messages
6751 */
6752 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6753
6754 if (!pmb) {
6755 rc = -ENOMEM;
6756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6757 "0474 Unable to allocate memory for issuing "
6758 "MBOX_CONFIG_MSI command\n");
6759 goto mem_fail_out;
6760 }
6761 rc = lpfc_config_msi(phba, pmb);
6762 if (rc)
6763 goto mbx_fail_out;
6764 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6765 if (rc != MBX_SUCCESS) {
6766 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6767 "0351 Config MSI mailbox command failed, "
6768 "mbxCmd x%x, mbxStatus x%x\n",
6769 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6770 goto mbx_fail_out;
6771 }
6772
6773 /* Free memory allocated for mailbox command */
6774 mempool_free(pmb, phba->mbox_mem_pool);
6775 return rc;
6776
6777mbx_fail_out:
6778 /* Free memory allocated for mailbox command */
6779 mempool_free(pmb, phba->mbox_mem_pool);
6780
6781mem_fail_out:
6782 /* free the irq already requested */
6783 free_irq(phba->msix_entries[1].vector, phba);
6784
6785irq_fail_out:
6786 /* free the irq already requested */
6787 free_irq(phba->msix_entries[0].vector, phba);
6788
6789msi_fail_out:
6790 /* Unconfigure MSI-X capability structure */
6791 pci_disable_msix(phba->pcidev);
6792 return rc;
6793}
6794
6795/**
6796 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6797 * @phba: pointer to lpfc hba data structure.
6798 *
6799 * This routine is invoked to release the MSI-X vectors and then disable the
6800 * MSI-X interrupt mode to device with SLI-3 interface spec.
6801 **/
6802static void
6803lpfc_sli_disable_msix(struct lpfc_hba *phba)
6804{
6805 int i;
6806
6807 /* Free up MSI-X multi-message vectors */
6808 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6809 free_irq(phba->msix_entries[i].vector, phba);
6810 /* Disable MSI-X */
6811 pci_disable_msix(phba->pcidev);
6812
6813 return;
6814}
6815
6816/**
6817 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6818 * @phba: pointer to lpfc hba data structure.
6819 *
6820 * This routine is invoked to enable the MSI interrupt mode to device with
6821 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6822 * enable the MSI vector. The device driver is responsible for calling the
6823 * request_irq() to register MSI vector with a interrupt the handler, which
6824 * is done in this function.
6825 *
6826 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006827 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006828 * other values - error
6829 */
6830static int
6831lpfc_sli_enable_msi(struct lpfc_hba *phba)
6832{
6833 int rc;
6834
6835 rc = pci_enable_msi(phba->pcidev);
6836 if (!rc)
6837 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6838 "0462 PCI enable MSI mode success.\n");
6839 else {
6840 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6841 "0471 PCI enable MSI mode failed (%d)\n", rc);
6842 return rc;
6843 }
6844
6845 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6846 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6847 if (rc) {
6848 pci_disable_msi(phba->pcidev);
6849 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6850 "0478 MSI request_irq failed (%d)\n", rc);
6851 }
6852 return rc;
6853}
6854
6855/**
6856 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6857 * @phba: pointer to lpfc hba data structure.
6858 *
6859 * This routine is invoked to disable the MSI interrupt mode to device with
6860 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6861 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6862 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6863 * its vector.
6864 */
6865static void
6866lpfc_sli_disable_msi(struct lpfc_hba *phba)
6867{
6868 free_irq(phba->pcidev->irq, phba);
6869 pci_disable_msi(phba->pcidev);
6870 return;
6871}
6872
6873/**
6874 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6875 * @phba: pointer to lpfc hba data structure.
6876 *
6877 * This routine is invoked to enable device interrupt and associate driver's
6878 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6879 * spec. Depends on the interrupt mode configured to the driver, the driver
6880 * will try to fallback from the configured interrupt mode to an interrupt
6881 * mode which is supported by the platform, kernel, and device in the order
6882 * of:
6883 * MSI-X -> MSI -> IRQ.
6884 *
6885 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006886 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006887 * other values - error
6888 **/
6889static uint32_t
6890lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6891{
6892 uint32_t intr_mode = LPFC_INTR_ERROR;
6893 int retval;
6894
6895 if (cfg_mode == 2) {
6896 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6897 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6898 if (!retval) {
6899 /* Now, try to enable MSI-X interrupt mode */
6900 retval = lpfc_sli_enable_msix(phba);
6901 if (!retval) {
6902 /* Indicate initialization to MSI-X mode */
6903 phba->intr_type = MSIX;
6904 intr_mode = 2;
6905 }
6906 }
6907 }
6908
6909 /* Fallback to MSI if MSI-X initialization failed */
6910 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6911 retval = lpfc_sli_enable_msi(phba);
6912 if (!retval) {
6913 /* Indicate initialization to MSI mode */
6914 phba->intr_type = MSI;
6915 intr_mode = 1;
6916 }
6917 }
6918
6919 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6920 if (phba->intr_type == NONE) {
6921 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6922 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6923 if (!retval) {
6924 /* Indicate initialization to INTx mode */
6925 phba->intr_type = INTx;
6926 intr_mode = 0;
6927 }
6928 }
6929 return intr_mode;
6930}
6931
6932/**
6933 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6934 * @phba: pointer to lpfc hba data structure.
6935 *
6936 * This routine is invoked to disable device interrupt and disassociate the
6937 * driver's interrupt handler(s) from interrupt vector(s) to device with
6938 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6939 * release the interrupt vector(s) for the message signaled interrupt.
6940 **/
6941static void
6942lpfc_sli_disable_intr(struct lpfc_hba *phba)
6943{
6944 /* Disable the currently initialized interrupt mode */
6945 if (phba->intr_type == MSIX)
6946 lpfc_sli_disable_msix(phba);
6947 else if (phba->intr_type == MSI)
6948 lpfc_sli_disable_msi(phba);
6949 else if (phba->intr_type == INTx)
6950 free_irq(phba->pcidev->irq, phba);
6951
6952 /* Reset interrupt management states */
6953 phba->intr_type = NONE;
6954 phba->sli.slistat.sli_intr = 0;
6955
6956 return;
6957}
6958
6959/**
James Smartda0436e2009-05-22 14:51:39 -04006960 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6961 * @phba: pointer to lpfc hba data structure.
6962 *
6963 * This routine is invoked to enable the MSI-X interrupt vectors to device
6964 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6965 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6966 * enables either all or nothing, depending on the current availability of
6967 * PCI vector resources. The device driver is responsible for calling the
6968 * individual request_irq() to register each MSI-X vector with a interrupt
6969 * handler, which is done in this function. Note that later when device is
6970 * unloading, the driver should always call free_irq() on all MSI-X vectors
6971 * it has done request_irq() on before calling pci_disable_msix(). Failure
6972 * to do so results in a BUG_ON() and a device will be left with MSI-X
6973 * enabled and leaks its vectors.
6974 *
6975 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006976 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006977 * other values - error
6978 **/
6979static int
6980lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6981{
6982 int rc, index;
6983
6984 /* Set up MSI-X multi-message vectors */
6985 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6986 phba->sli4_hba.msix_entries[index].entry = index;
6987
6988 /* Configure MSI-X capability structure */
6989 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6990 phba->sli4_hba.cfg_eqn);
6991 if (rc) {
6992 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6993 "0484 PCI enable MSI-X failed (%d)\n", rc);
6994 goto msi_fail_out;
6995 }
6996 /* Log MSI-X vector assignment */
6997 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6998 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6999 "0489 MSI-X entry[%d]: vector=x%x "
7000 "message=%d\n", index,
7001 phba->sli4_hba.msix_entries[index].vector,
7002 phba->sli4_hba.msix_entries[index].entry);
7003 /*
7004 * Assign MSI-X vectors to interrupt handlers
7005 */
7006
7007 /* The first vector must associated to slow-path handler for MQ */
7008 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7009 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7010 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7011 if (rc) {
7012 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7013 "0485 MSI-X slow-path request_irq failed "
7014 "(%d)\n", rc);
7015 goto msi_fail_out;
7016 }
7017
7018 /* The rest of the vector(s) are associated to fast-path handler(s) */
7019 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
7020 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7021 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7022 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7023 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7024 LPFC_FP_DRIVER_HANDLER_NAME,
7025 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7026 if (rc) {
7027 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7028 "0486 MSI-X fast-path (%d) "
7029 "request_irq failed (%d)\n", index, rc);
7030 goto cfg_fail_out;
7031 }
7032 }
7033
7034 return rc;
7035
7036cfg_fail_out:
7037 /* free the irq already requested */
7038 for (--index; index >= 1; index--)
7039 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7040 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7041
7042 /* free the irq already requested */
7043 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7044
7045msi_fail_out:
7046 /* Unconfigure MSI-X capability structure */
7047 pci_disable_msix(phba->pcidev);
7048 return rc;
7049}
7050
7051/**
7052 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7053 * @phba: pointer to lpfc hba data structure.
7054 *
7055 * This routine is invoked to release the MSI-X vectors and then disable the
7056 * MSI-X interrupt mode to device with SLI-4 interface spec.
7057 **/
7058static void
7059lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7060{
7061 int index;
7062
7063 /* Free up MSI-X multi-message vectors */
7064 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7065
7066 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
7067 free_irq(phba->sli4_hba.msix_entries[index].vector,
7068 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7069 /* Disable MSI-X */
7070 pci_disable_msix(phba->pcidev);
7071
7072 return;
7073}
7074
7075/**
7076 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7077 * @phba: pointer to lpfc hba data structure.
7078 *
7079 * This routine is invoked to enable the MSI interrupt mode to device with
7080 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7081 * to enable the MSI vector. The device driver is responsible for calling
7082 * the request_irq() to register MSI vector with a interrupt the handler,
7083 * which is done in this function.
7084 *
7085 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007086 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007087 * other values - error
7088 **/
7089static int
7090lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7091{
7092 int rc, index;
7093
7094 rc = pci_enable_msi(phba->pcidev);
7095 if (!rc)
7096 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7097 "0487 PCI enable MSI mode success.\n");
7098 else {
7099 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7100 "0488 PCI enable MSI mode failed (%d)\n", rc);
7101 return rc;
7102 }
7103
7104 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7105 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7106 if (rc) {
7107 pci_disable_msi(phba->pcidev);
7108 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7109 "0490 MSI request_irq failed (%d)\n", rc);
7110 }
7111
7112 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7113 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7114 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7115 }
7116
7117 return rc;
7118}
7119
7120/**
7121 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7122 * @phba: pointer to lpfc hba data structure.
7123 *
7124 * This routine is invoked to disable the MSI interrupt mode to device with
7125 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7126 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7127 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7128 * its vector.
7129 **/
7130static void
7131lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7132{
7133 free_irq(phba->pcidev->irq, phba);
7134 pci_disable_msi(phba->pcidev);
7135 return;
7136}
7137
7138/**
7139 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7140 * @phba: pointer to lpfc hba data structure.
7141 *
7142 * This routine is invoked to enable device interrupt and associate driver's
7143 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7144 * interface spec. Depends on the interrupt mode configured to the driver,
7145 * the driver will try to fallback from the configured interrupt mode to an
7146 * interrupt mode which is supported by the platform, kernel, and device in
7147 * the order of:
7148 * MSI-X -> MSI -> IRQ.
7149 *
7150 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007151 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007152 * other values - error
7153 **/
7154static uint32_t
7155lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7156{
7157 uint32_t intr_mode = LPFC_INTR_ERROR;
7158 int retval, index;
7159
7160 if (cfg_mode == 2) {
7161 /* Preparation before conf_msi mbox cmd */
7162 retval = 0;
7163 if (!retval) {
7164 /* Now, try to enable MSI-X interrupt mode */
7165 retval = lpfc_sli4_enable_msix(phba);
7166 if (!retval) {
7167 /* Indicate initialization to MSI-X mode */
7168 phba->intr_type = MSIX;
7169 intr_mode = 2;
7170 }
7171 }
7172 }
7173
7174 /* Fallback to MSI if MSI-X initialization failed */
7175 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7176 retval = lpfc_sli4_enable_msi(phba);
7177 if (!retval) {
7178 /* Indicate initialization to MSI mode */
7179 phba->intr_type = MSI;
7180 intr_mode = 1;
7181 }
7182 }
7183
7184 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7185 if (phba->intr_type == NONE) {
7186 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7187 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7188 if (!retval) {
7189 /* Indicate initialization to INTx mode */
7190 phba->intr_type = INTx;
7191 intr_mode = 0;
7192 for (index = 0; index < phba->cfg_fcp_eq_count;
7193 index++) {
7194 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7195 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7196 }
7197 }
7198 }
7199 return intr_mode;
7200}
7201
7202/**
7203 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7204 * @phba: pointer to lpfc hba data structure.
7205 *
7206 * This routine is invoked to disable device interrupt and disassociate
7207 * the driver's interrupt handler(s) from interrupt vector(s) to device
7208 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7209 * will release the interrupt vector(s) for the message signaled interrupt.
7210 **/
7211static void
7212lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7213{
7214 /* Disable the currently initialized interrupt mode */
7215 if (phba->intr_type == MSIX)
7216 lpfc_sli4_disable_msix(phba);
7217 else if (phba->intr_type == MSI)
7218 lpfc_sli4_disable_msi(phba);
7219 else if (phba->intr_type == INTx)
7220 free_irq(phba->pcidev->irq, phba);
7221
7222 /* Reset interrupt management states */
7223 phba->intr_type = NONE;
7224 phba->sli.slistat.sli_intr = 0;
7225
7226 return;
7227}
7228
7229/**
James Smart3772a992009-05-22 14:50:54 -04007230 * lpfc_unset_hba - Unset SLI3 hba device initialization
7231 * @phba: pointer to lpfc hba data structure.
7232 *
7233 * This routine is invoked to unset the HBA device initialization steps to
7234 * a device with SLI-3 interface spec.
7235 **/
7236static void
7237lpfc_unset_hba(struct lpfc_hba *phba)
7238{
7239 struct lpfc_vport *vport = phba->pport;
7240 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7241
7242 spin_lock_irq(shost->host_lock);
7243 vport->load_flag |= FC_UNLOADING;
7244 spin_unlock_irq(shost->host_lock);
7245
7246 lpfc_stop_hba_timers(phba);
7247
7248 phba->pport->work_port_events = 0;
7249
7250 lpfc_sli_hba_down(phba);
7251
7252 lpfc_sli_brdrestart(phba);
7253
7254 lpfc_sli_disable_intr(phba);
7255
7256 return;
7257}
7258
7259/**
James Smartda0436e2009-05-22 14:51:39 -04007260 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7261 * @phba: pointer to lpfc hba data structure.
7262 *
7263 * This routine is invoked to unset the HBA device initialization steps to
7264 * a device with SLI-4 interface spec.
7265 **/
7266static void
7267lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7268{
7269 struct lpfc_vport *vport = phba->pport;
7270 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7271
7272 spin_lock_irq(shost->host_lock);
7273 vport->load_flag |= FC_UNLOADING;
7274 spin_unlock_irq(shost->host_lock);
7275
7276 phba->pport->work_port_events = 0;
7277
7278 lpfc_sli4_hba_down(phba);
7279
7280 lpfc_sli4_disable_intr(phba);
7281
7282 return;
7283}
7284
7285/**
7286 * lpfc_sli4_hba_unset - Unset the fcoe hba
7287 * @phba: Pointer to HBA context object.
7288 *
7289 * This function is called in the SLI4 code path to reset the HBA's FCoE
7290 * function. The caller is not required to hold any lock. This routine
7291 * issues PCI function reset mailbox command to reset the FCoE function.
7292 * At the end of the function, it calls lpfc_hba_down_post function to
7293 * free any pending commands.
7294 **/
7295static void
7296lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7297{
7298 int wait_cnt = 0;
7299 LPFC_MBOXQ_t *mboxq;
7300
7301 lpfc_stop_hba_timers(phba);
7302 phba->sli4_hba.intr_enable = 0;
7303
7304 /*
7305 * Gracefully wait out the potential current outstanding asynchronous
7306 * mailbox command.
7307 */
7308
7309 /* First, block any pending async mailbox command from posted */
7310 spin_lock_irq(&phba->hbalock);
7311 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7312 spin_unlock_irq(&phba->hbalock);
7313 /* Now, trying to wait it out if we can */
7314 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7315 msleep(10);
7316 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7317 break;
7318 }
7319 /* Forcefully release the outstanding mailbox command if timed out */
7320 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7321 spin_lock_irq(&phba->hbalock);
7322 mboxq = phba->sli.mbox_active;
7323 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7324 __lpfc_mbox_cmpl_put(phba, mboxq);
7325 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7326 phba->sli.mbox_active = NULL;
7327 spin_unlock_irq(&phba->hbalock);
7328 }
7329
7330 /* Tear down the queues in the HBA */
7331 lpfc_sli4_queue_unset(phba);
7332
7333 /* Disable PCI subsystem interrupt */
7334 lpfc_sli4_disable_intr(phba);
7335
7336 /* Stop kthread signal shall trigger work_done one more time */
7337 kthread_stop(phba->worker_thread);
7338
7339 /* Stop the SLI4 device port */
7340 phba->pport->work_port_events = 0;
7341}
7342
James Smart28baac72010-02-12 14:42:03 -05007343 /**
7344 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7345 * @phba: Pointer to HBA context object.
7346 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7347 *
7348 * This function is called in the SLI4 code path to read the port's
7349 * sli4 capabilities.
7350 *
7351 * This function may be be called from any context that can block-wait
7352 * for the completion. The expectation is that this routine is called
7353 * typically from probe_one or from the online routine.
7354 **/
7355int
7356lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7357{
7358 int rc;
7359 struct lpfc_mqe *mqe;
7360 struct lpfc_pc_sli4_params *sli4_params;
7361 uint32_t mbox_tmo;
7362
7363 rc = 0;
7364 mqe = &mboxq->u.mqe;
7365
7366 /* Read the port's SLI4 Parameters port capabilities */
7367 lpfc_sli4_params(mboxq);
7368 if (!phba->sli4_hba.intr_enable)
7369 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7370 else {
7371 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7372 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7373 }
7374
7375 if (unlikely(rc))
7376 return 1;
7377
7378 sli4_params = &phba->sli4_hba.pc_sli4_params;
7379 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7380 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7381 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7382 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7383 &mqe->un.sli4_params);
7384 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7385 &mqe->un.sli4_params);
7386 sli4_params->proto_types = mqe->un.sli4_params.word3;
7387 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7388 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7389 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7390 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7391 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7392 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7393 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7394 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7395 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7396 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7397 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7398 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7399 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7400 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7401 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7402 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7403 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7404 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7405 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7406 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7407 return rc;
7408}
7409
James Smartda0436e2009-05-22 14:51:39 -04007410/**
James Smart3772a992009-05-22 14:50:54 -04007411 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7412 * @pdev: pointer to PCI device
7413 * @pid: pointer to PCI device identifier
7414 *
7415 * This routine is to be called to attach a device with SLI-3 interface spec
7416 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7417 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7418 * information of the device and driver to see if the driver state that it can
7419 * support this kind of device. If the match is successful, the driver core
7420 * invokes this routine. If this routine determines it can claim the HBA, it
7421 * does all the initialization that it needs to do to handle the HBA properly.
7422 *
7423 * Return code
7424 * 0 - driver can claim the device
7425 * negative value - driver can not claim the device
7426 **/
7427static int __devinit
7428lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7429{
7430 struct lpfc_hba *phba;
7431 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007432 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007433 int error;
7434 uint32_t cfg_mode, intr_mode;
7435
7436 /* Allocate memory for HBA structure */
7437 phba = lpfc_hba_alloc(pdev);
7438 if (!phba)
7439 return -ENOMEM;
7440
7441 /* Perform generic PCI device enabling operation */
7442 error = lpfc_enable_pci_dev(phba);
7443 if (error) {
7444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7445 "1401 Failed to enable pci device.\n");
7446 goto out_free_phba;
7447 }
7448
7449 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7450 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7451 if (error)
7452 goto out_disable_pci_dev;
7453
7454 /* Set up SLI-3 specific device PCI memory space */
7455 error = lpfc_sli_pci_mem_setup(phba);
7456 if (error) {
7457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7458 "1402 Failed to set up pci memory space.\n");
7459 goto out_disable_pci_dev;
7460 }
7461
7462 /* Set up phase-1 common device driver resources */
7463 error = lpfc_setup_driver_resource_phase1(phba);
7464 if (error) {
7465 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7466 "1403 Failed to set up driver resource.\n");
7467 goto out_unset_pci_mem_s3;
7468 }
7469
7470 /* Set up SLI-3 specific device driver resources */
7471 error = lpfc_sli_driver_resource_setup(phba);
7472 if (error) {
7473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7474 "1404 Failed to set up driver resource.\n");
7475 goto out_unset_pci_mem_s3;
7476 }
7477
7478 /* Initialize and populate the iocb list per host */
7479 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7480 if (error) {
7481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7482 "1405 Failed to initialize iocb list.\n");
7483 goto out_unset_driver_resource_s3;
7484 }
7485
7486 /* Set up common device driver resources */
7487 error = lpfc_setup_driver_resource_phase2(phba);
7488 if (error) {
7489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7490 "1406 Failed to set up driver resource.\n");
7491 goto out_free_iocb_list;
7492 }
7493
7494 /* Create SCSI host to the physical port */
7495 error = lpfc_create_shost(phba);
7496 if (error) {
7497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7498 "1407 Failed to create scsi host.\n");
7499 goto out_unset_driver_resource;
7500 }
7501
7502 /* Configure sysfs attributes */
7503 vport = phba->pport;
7504 error = lpfc_alloc_sysfs_attr(vport);
7505 if (error) {
7506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7507 "1476 Failed to allocate sysfs attr\n");
7508 goto out_destroy_shost;
7509 }
7510
James Smart6669f9b2009-10-02 15:16:45 -04007511 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04007512 /* Now, trying to enable interrupt and bring up the device */
7513 cfg_mode = phba->cfg_use_msi;
7514 while (true) {
7515 /* Put device to a known state before enabling interrupt */
7516 lpfc_stop_port(phba);
7517 /* Configure and enable interrupt */
7518 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7519 if (intr_mode == LPFC_INTR_ERROR) {
7520 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7521 "0431 Failed to enable interrupt.\n");
7522 error = -ENODEV;
7523 goto out_free_sysfs_attr;
7524 }
7525 /* SLI-3 HBA setup */
7526 if (lpfc_sli_hba_setup(phba)) {
7527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7528 "1477 Failed to set up hba\n");
7529 error = -ENODEV;
7530 goto out_remove_device;
7531 }
7532
7533 /* Wait 50ms for the interrupts of previous mailbox commands */
7534 msleep(50);
7535 /* Check active interrupts on message signaled interrupts */
7536 if (intr_mode == 0 ||
7537 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7538 /* Log the current active interrupt mode */
7539 phba->intr_mode = intr_mode;
7540 lpfc_log_intr_mode(phba, intr_mode);
7541 break;
7542 } else {
7543 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7544 "0447 Configure interrupt mode (%d) "
7545 "failed active interrupt test.\n",
7546 intr_mode);
7547 /* Disable the current interrupt mode */
7548 lpfc_sli_disable_intr(phba);
7549 /* Try next level of interrupt mode */
7550 cfg_mode = --intr_mode;
7551 }
7552 }
7553
7554 /* Perform post initialization setup */
7555 lpfc_post_init_setup(phba);
7556
7557 /* Check if there are static vports to be created. */
7558 lpfc_create_static_vport(phba);
7559
7560 return 0;
7561
7562out_remove_device:
7563 lpfc_unset_hba(phba);
7564out_free_sysfs_attr:
7565 lpfc_free_sysfs_attr(vport);
7566out_destroy_shost:
7567 lpfc_destroy_shost(phba);
7568out_unset_driver_resource:
7569 lpfc_unset_driver_resource_phase2(phba);
7570out_free_iocb_list:
7571 lpfc_free_iocb_list(phba);
7572out_unset_driver_resource_s3:
7573 lpfc_sli_driver_resource_unset(phba);
7574out_unset_pci_mem_s3:
7575 lpfc_sli_pci_mem_unset(phba);
7576out_disable_pci_dev:
7577 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007578 if (shost)
7579 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007580out_free_phba:
7581 lpfc_hba_free(phba);
7582 return error;
7583}
7584
7585/**
7586 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007587 * @pdev: pointer to PCI device
7588 *
James Smart3772a992009-05-22 14:50:54 -04007589 * This routine is to be called to disattach a device with SLI-3 interface
7590 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7591 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7592 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007593 **/
dea31012005-04-17 16:05:31 -05007594static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007595lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007596{
James Smart2e0fef82007-06-17 19:56:36 -05007597 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7598 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007599 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007600 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007601 int i;
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05007602 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7603
James Smart549e55c2007-08-02 11:09:51 -04007604 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007605 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007606 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007607
James Smart858c9f62007-06-17 19:56:39 -05007608 lpfc_free_sysfs_attr(vport);
7609
James Smarteada2722008-12-04 22:39:13 -05007610 /* Release all the vports against this physical port */
7611 vports = lpfc_create_vport_work_array(phba);
7612 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007613 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007614 fc_vport_terminate(vports[i]->fc_vport);
7615 lpfc_destroy_vport_work_array(phba, vports);
7616
7617 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007618 fc_remove_host(shost);
7619 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007620 lpfc_cleanup(vport);
7621
James Smart2e0fef82007-06-17 19:56:36 -05007622 /*
7623 * Bring down the SLI Layer. This step disable all interrupts,
7624 * clears the rings, discards all mailbox commands, and resets
7625 * the HBA.
7626 */
James Smarta257bf92009-04-06 18:48:10 -04007627
7628 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007629 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007630 /* Stop kthread signal shall trigger work_done one more time */
7631 kthread_stop(phba->worker_thread);
7632 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007633 lpfc_sli_brdrestart(phba);
7634
James Smart3772a992009-05-22 14:50:54 -04007635 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007636 spin_lock_irq(&phba->hbalock);
7637 list_del_init(&vport->listentry);
7638 spin_unlock_irq(&phba->hbalock);
7639
James Smart858c9f62007-06-17 19:56:39 -05007640 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007641
James Smart5b75da22008-12-04 22:39:35 -05007642 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007643 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007644
7645 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007646 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007647
7648 /*
7649 * Call scsi_free before mem_free since scsi bufs are released to their
7650 * corresponding pools here.
7651 */
7652 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007653 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007654
James Smart34b02dc2008-08-24 21:49:55 -04007655 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7656 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007657
James Smart2e0fef82007-06-17 19:56:36 -05007658 /* Free resources associated with SLI2 interface */
7659 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007660 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007661
7662 /* unmap adapter SLIM and Control Registers */
7663 iounmap(phba->ctrl_regs_memmap_p);
7664 iounmap(phba->slim_memmap_p);
7665
James Smart3772a992009-05-22 14:50:54 -04007666 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007667
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05007668 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007669 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007670}
7671
Linas Vepstas8d63f372007-02-14 14:28:36 -06007672/**
James Smart3772a992009-05-22 14:50:54 -04007673 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007674 * @pdev: pointer to PCI device
7675 * @msg: power management message
7676 *
James Smart3772a992009-05-22 14:50:54 -04007677 * This routine is to be called from the kernel's PCI subsystem to support
7678 * system Power Management (PM) to device with SLI-3 interface spec. When
7679 * PM invokes this method, it quiesces the device by stopping the driver's
7680 * worker thread for the device, turning off device's interrupt and DMA,
7681 * and bring the device offline. Note that as the driver implements the
7682 * minimum PM requirements to a power-aware driver's PM support for the
7683 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7684 * to the suspend() method call will be treated as SUSPEND and the driver will
7685 * fully reinitialize its device during resume() method call, the driver will
7686 * set device to PCI_D3hot state in PCI config space instead of setting it
7687 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007688 *
7689 * Return code
James Smart3772a992009-05-22 14:50:54 -04007690 * 0 - driver suspended the device
7691 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007692 **/
7693static int
James Smart3772a992009-05-22 14:50:54 -04007694lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007695{
7696 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7697 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7698
7699 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7700 "0473 PCI device Power Management suspend.\n");
7701
7702 /* Bring down the device */
7703 lpfc_offline_prep(phba);
7704 lpfc_offline(phba);
7705 kthread_stop(phba->worker_thread);
7706
7707 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007708 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007709
7710 /* Save device state to PCI config space */
7711 pci_save_state(pdev);
7712 pci_set_power_state(pdev, PCI_D3hot);
7713
7714 return 0;
7715}
7716
7717/**
James Smart3772a992009-05-22 14:50:54 -04007718 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007719 * @pdev: pointer to PCI device
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 PM
7723 * invokes this method, it restores the device's PCI config space state and
7724 * fully reinitializes the device and brings it online. Note that as the
7725 * driver implements the minimum PM requirements to a power-aware driver's
7726 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7727 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7728 * driver will fully reinitialize its device during resume() method call,
7729 * the device will be set to PCI_D0 directly in PCI config space before
7730 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007731 *
7732 * Return code
James Smart3772a992009-05-22 14:50:54 -04007733 * 0 - driver suspended the device
7734 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007735 **/
7736static int
James Smart3772a992009-05-22 14:50:54 -04007737lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007738{
7739 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7740 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007741 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007742 int error;
7743
7744 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7745 "0452 PCI device Power Management resume.\n");
7746
7747 /* Restore device state from PCI config space */
7748 pci_set_power_state(pdev, PCI_D0);
7749 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007750
James Smart1dfb5a42010-02-12 14:40:50 -05007751 /*
7752 * As the new kernel behavior of pci_restore_state() API call clears
7753 * device saved_state flag, need to save the restored state again.
7754 */
7755 pci_save_state(pdev);
7756
James Smart3a55b532008-12-04 22:38:54 -05007757 if (pdev->is_busmaster)
7758 pci_set_master(pdev);
7759
7760 /* Startup the kernel thread for this host adapter. */
7761 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7762 "lpfc_worker_%d", phba->brd_no);
7763 if (IS_ERR(phba->worker_thread)) {
7764 error = PTR_ERR(phba->worker_thread);
7765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7766 "0434 PM resume failed to start worker "
7767 "thread: error=x%x.\n", error);
7768 return error;
7769 }
7770
James Smart5b75da22008-12-04 22:39:35 -05007771 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007772 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007773 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007775 "0430 PM resume Failed to enable interrupt\n");
7776 return -EIO;
7777 } else
7778 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007779
7780 /* Restart HBA and bring it online */
7781 lpfc_sli_brdrestart(phba);
7782 lpfc_online(phba);
7783
James Smart5b75da22008-12-04 22:39:35 -05007784 /* Log the current active interrupt mode */
7785 lpfc_log_intr_mode(phba, phba->intr_mode);
7786
James Smart3a55b532008-12-04 22:38:54 -05007787 return 0;
7788}
7789
7790/**
James Smart891478a2009-11-18 15:40:23 -05007791 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7792 * @phba: pointer to lpfc hba data structure.
7793 *
7794 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04007795 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05007796 **/
7797static void
7798lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7799{
James Smarte2af0d22010-03-15 11:25:32 -04007800 struct lpfc_sli *psli = &phba->sli;
7801 struct lpfc_sli_ring *pring;
7802
James Smart891478a2009-11-18 15:40:23 -05007803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7804 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04007805
7806 /*
7807 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
7808 * and let the SCSI mid-layer to retry them to recover.
7809 */
7810 pring = &psli->ring[psli->fcp_ring];
7811 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05007812}
7813
7814/**
James Smart0d878412009-10-02 15:16:56 -04007815 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7816 * @phba: pointer to lpfc hba data structure.
7817 *
7818 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7819 * disables the device interrupt and pci device, and aborts the internal FCP
7820 * pending I/Os.
7821 **/
7822static void
7823lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7824{
James Smart0d878412009-10-02 15:16:56 -04007825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007826 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04007827
7828 /* Block all SCSI devices' I/Os on the host */
7829 lpfc_scsi_dev_block(phba);
7830
7831 /* stop all timers */
7832 lpfc_stop_hba_timers(phba);
7833
James Smart0d878412009-10-02 15:16:56 -04007834 /* Disable interrupt and pci device */
7835 lpfc_sli_disable_intr(phba);
7836 pci_disable_device(phba->pcidev);
James Smarte2af0d22010-03-15 11:25:32 -04007837 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
7838 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04007839}
7840
7841/**
7842 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7843 * @phba: pointer to lpfc hba data structure.
7844 *
7845 * This routine is called to prepare the SLI3 device for PCI slot permanently
7846 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7847 * pending I/Os.
7848 **/
7849static void
7850lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7851{
7852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007853 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04007854 /* Block all SCSI devices' I/Os on the host */
7855 lpfc_scsi_dev_block(phba);
7856
7857 /* stop all timers */
7858 lpfc_stop_hba_timers(phba);
7859
James Smart0d878412009-10-02 15:16:56 -04007860 /* Clean up all driver's outstanding SCSI I/Os */
7861 lpfc_sli_flush_fcp_rings(phba);
7862}
7863
7864/**
James Smart3772a992009-05-22 14:50:54 -04007865 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007866 * @pdev: pointer to PCI device.
7867 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007868 *
James Smart3772a992009-05-22 14:50:54 -04007869 * This routine is called from the PCI subsystem for I/O error handling to
7870 * device with SLI-3 interface spec. This function is called by the PCI
7871 * subsystem after a PCI bus error affecting this device has been detected.
7872 * When this function is invoked, it will need to stop all the I/Os and
7873 * interrupt(s) to the device. Once that is done, it will return
7874 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7875 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007876 *
7877 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007878 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007879 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7880 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007881 **/
James Smart3772a992009-05-22 14:50:54 -04007882static pci_ers_result_t
7883lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007884{
James Smart51ef4c22007-08-02 11:10:31 -04007885 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7886 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007887
James Smart0d878412009-10-02 15:16:56 -04007888 switch (state) {
7889 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007890 /* Non-fatal error, prepare for recovery */
7891 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007892 return PCI_ERS_RESULT_CAN_RECOVER;
7893 case pci_channel_io_frozen:
7894 /* Fatal error, prepare for slot reset */
7895 lpfc_sli_prep_dev_for_reset(phba);
7896 return PCI_ERS_RESULT_NEED_RESET;
7897 case pci_channel_io_perm_failure:
7898 /* Permanent failure, prepare for device down */
7899 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007900 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007901 default:
7902 /* Unknown state, prepare and request slot reset */
7903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7904 "0472 Unknown PCI error state: x%x\n", state);
7905 lpfc_sli_prep_dev_for_reset(phba);
7906 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007907 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007908}
7909
7910/**
James Smart3772a992009-05-22 14:50:54 -04007911 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007912 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007913 *
James Smart3772a992009-05-22 14:50:54 -04007914 * This routine is called from the PCI subsystem for error handling to
7915 * device with SLI-3 interface spec. This is called after PCI bus has been
7916 * reset to restart the PCI card from scratch, as if from a cold-boot.
7917 * During the PCI subsystem error recovery, after driver returns
7918 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7919 * recovery and then call this routine before calling the .resume method
7920 * to recover the device. This function will initialize the HBA device,
7921 * enable the interrupt, but it will just put the HBA to offline state
7922 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007923 *
7924 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007925 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7926 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007927 */
James Smart3772a992009-05-22 14:50:54 -04007928static pci_ers_result_t
7929lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007930{
James Smart51ef4c22007-08-02 11:10:31 -04007931 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7932 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007933 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007934 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007935
7936 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007937 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007938 printk(KERN_ERR "lpfc: Cannot re-enable "
7939 "PCI device after reset.\n");
7940 return PCI_ERS_RESULT_DISCONNECT;
7941 }
7942
James Smart97207482008-12-04 22:39:19 -05007943 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05007944
7945 /*
7946 * As the new kernel behavior of pci_restore_state() API call clears
7947 * device saved_state flag, need to save the restored state again.
7948 */
7949 pci_save_state(pdev);
7950
James Smart97207482008-12-04 22:39:19 -05007951 if (pdev->is_busmaster)
7952 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007953
James Smart92d7f7b2007-06-17 19:56:38 -05007954 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007955 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007956 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007957
James Smart5b75da22008-12-04 22:39:35 -05007958 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007959 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007960 if (intr_mode == LPFC_INTR_ERROR) {
7961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7962 "0427 Cannot re-enable interrupt after "
7963 "slot reset.\n");
7964 return PCI_ERS_RESULT_DISCONNECT;
7965 } else
7966 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007967
7968 /* Take device offline; this will perform cleanup */
7969 lpfc_offline(phba);
7970 lpfc_sli_brdrestart(phba);
7971
James Smart5b75da22008-12-04 22:39:35 -05007972 /* Log the current active interrupt mode */
7973 lpfc_log_intr_mode(phba, phba->intr_mode);
7974
Linas Vepstas8d63f372007-02-14 14:28:36 -06007975 return PCI_ERS_RESULT_RECOVERED;
7976}
7977
7978/**
James Smart3772a992009-05-22 14:50:54 -04007979 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007980 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007981 *
James Smart3772a992009-05-22 14:50:54 -04007982 * This routine is called from the PCI subsystem for error handling to device
7983 * with SLI-3 interface spec. It is called when kernel error recovery tells
7984 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7985 * error recovery. After this call, traffic can start to flow from this device
7986 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007987 */
James Smart3772a992009-05-22 14:50:54 -04007988static void
7989lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007990{
James Smart51ef4c22007-08-02 11:10:31 -04007991 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7992 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007993
James Smarte2af0d22010-03-15 11:25:32 -04007994 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04007995 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04007996
7997 /* Clean up Advanced Error Reporting (AER) if needed */
7998 if (phba->hba_flag & HBA_AER_ENABLED)
7999 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008000}
8001
James Smart3772a992009-05-22 14:50:54 -04008002/**
James Smartda0436e2009-05-22 14:51:39 -04008003 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8004 * @phba: pointer to lpfc hba data structure.
8005 *
8006 * returns the number of ELS/CT IOCBs to reserve
8007 **/
8008int
8009lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8010{
8011 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8012
James Smartf1126682009-06-10 17:22:44 -04008013 if (phba->sli_rev == LPFC_SLI_REV4) {
8014 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04008015 return 10;
James Smartf1126682009-06-10 17:22:44 -04008016 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04008017 return 25;
James Smartf1126682009-06-10 17:22:44 -04008018 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04008019 return 50;
James Smartf1126682009-06-10 17:22:44 -04008020 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04008021 return 100;
James Smartf1126682009-06-10 17:22:44 -04008022 else
James Smart6a9c52c2009-10-02 15:16:51 -04008023 return 150;
James Smartf1126682009-06-10 17:22:44 -04008024 } else
8025 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008026}
8027
8028/**
8029 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
8030 * @pdev: pointer to PCI device
8031 * @pid: pointer to PCI device identifier
8032 *
8033 * This routine is called from the kernel's PCI subsystem to device with
8034 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8035 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8036 * information of the device and driver to see if the driver state that it
8037 * can support this kind of device. If the match is successful, the driver
8038 * core invokes this routine. If this routine determines it can claim the HBA,
8039 * it does all the initialization that it needs to do to handle the HBA
8040 * properly.
8041 *
8042 * Return code
8043 * 0 - driver can claim the device
8044 * negative value - driver can not claim the device
8045 **/
8046static int __devinit
8047lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8048{
8049 struct lpfc_hba *phba;
8050 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008051 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008052 int error;
8053 uint32_t cfg_mode, intr_mode;
8054 int mcnt;
8055
8056 /* Allocate memory for HBA structure */
8057 phba = lpfc_hba_alloc(pdev);
8058 if (!phba)
8059 return -ENOMEM;
8060
8061 /* Perform generic PCI device enabling operation */
8062 error = lpfc_enable_pci_dev(phba);
8063 if (error) {
8064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8065 "1409 Failed to enable pci device.\n");
8066 goto out_free_phba;
8067 }
8068
8069 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
8070 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
8071 if (error)
8072 goto out_disable_pci_dev;
8073
8074 /* Set up SLI-4 specific device PCI memory space */
8075 error = lpfc_sli4_pci_mem_setup(phba);
8076 if (error) {
8077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8078 "1410 Failed to set up pci memory space.\n");
8079 goto out_disable_pci_dev;
8080 }
8081
8082 /* Set up phase-1 common device driver resources */
8083 error = lpfc_setup_driver_resource_phase1(phba);
8084 if (error) {
8085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8086 "1411 Failed to set up driver resource.\n");
8087 goto out_unset_pci_mem_s4;
8088 }
8089
8090 /* Set up SLI-4 Specific device driver resources */
8091 error = lpfc_sli4_driver_resource_setup(phba);
8092 if (error) {
8093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8094 "1412 Failed to set up driver resource.\n");
8095 goto out_unset_pci_mem_s4;
8096 }
8097
8098 /* Initialize and populate the iocb list per host */
8099 error = lpfc_init_iocb_list(phba,
8100 phba->sli4_hba.max_cfg_param.max_xri);
8101 if (error) {
8102 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8103 "1413 Failed to initialize iocb list.\n");
8104 goto out_unset_driver_resource_s4;
8105 }
8106
8107 /* Set up common device driver resources */
8108 error = lpfc_setup_driver_resource_phase2(phba);
8109 if (error) {
8110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8111 "1414 Failed to set up driver resource.\n");
8112 goto out_free_iocb_list;
8113 }
8114
8115 /* Create SCSI host to the physical port */
8116 error = lpfc_create_shost(phba);
8117 if (error) {
8118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8119 "1415 Failed to create scsi host.\n");
8120 goto out_unset_driver_resource;
8121 }
8122
8123 /* Configure sysfs attributes */
8124 vport = phba->pport;
8125 error = lpfc_alloc_sysfs_attr(vport);
8126 if (error) {
8127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8128 "1416 Failed to allocate sysfs attr\n");
8129 goto out_destroy_shost;
8130 }
8131
James Smart6669f9b2009-10-02 15:16:45 -04008132 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04008133 /* Now, trying to enable interrupt and bring up the device */
8134 cfg_mode = phba->cfg_use_msi;
8135 while (true) {
8136 /* Put device to a known state before enabling interrupt */
8137 lpfc_stop_port(phba);
8138 /* Configure and enable interrupt */
8139 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
8140 if (intr_mode == LPFC_INTR_ERROR) {
8141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8142 "0426 Failed to enable interrupt.\n");
8143 error = -ENODEV;
8144 goto out_free_sysfs_attr;
8145 }
James Smartdef9c7a2009-12-21 17:02:28 -05008146 /* Default to single FCP EQ for non-MSI-X */
8147 if (phba->intr_type != MSIX)
8148 phba->cfg_fcp_eq_count = 1;
James Smartda0436e2009-05-22 14:51:39 -04008149 /* Set up SLI-4 HBA */
8150 if (lpfc_sli4_hba_setup(phba)) {
8151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8152 "1421 Failed to set up hba\n");
8153 error = -ENODEV;
8154 goto out_disable_intr;
8155 }
8156
8157 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
8158 if (intr_mode != 0)
8159 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
8160 LPFC_ACT_INTR_CNT);
8161
8162 /* Check active interrupts received only for MSI/MSI-X */
8163 if (intr_mode == 0 ||
8164 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
8165 /* Log the current active interrupt mode */
8166 phba->intr_mode = intr_mode;
8167 lpfc_log_intr_mode(phba, intr_mode);
8168 break;
8169 }
8170 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8171 "0451 Configure interrupt mode (%d) "
8172 "failed active interrupt test.\n",
8173 intr_mode);
8174 /* Unset the preivous SLI-4 HBA setup */
8175 lpfc_sli4_unset_hba(phba);
8176 /* Try next level of interrupt mode */
8177 cfg_mode = --intr_mode;
8178 }
8179
8180 /* Perform post initialization setup */
8181 lpfc_post_init_setup(phba);
8182
James Smart1c6834a2009-07-19 10:01:26 -04008183 /* Check if there are static vports to be created. */
8184 lpfc_create_static_vport(phba);
8185
James Smartda0436e2009-05-22 14:51:39 -04008186 return 0;
8187
8188out_disable_intr:
8189 lpfc_sli4_disable_intr(phba);
8190out_free_sysfs_attr:
8191 lpfc_free_sysfs_attr(vport);
8192out_destroy_shost:
8193 lpfc_destroy_shost(phba);
8194out_unset_driver_resource:
8195 lpfc_unset_driver_resource_phase2(phba);
8196out_free_iocb_list:
8197 lpfc_free_iocb_list(phba);
8198out_unset_driver_resource_s4:
8199 lpfc_sli4_driver_resource_unset(phba);
8200out_unset_pci_mem_s4:
8201 lpfc_sli4_pci_mem_unset(phba);
8202out_disable_pci_dev:
8203 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008204 if (shost)
8205 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04008206out_free_phba:
8207 lpfc_hba_free(phba);
8208 return error;
8209}
8210
8211/**
8212 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8213 * @pdev: pointer to PCI device
8214 *
8215 * This routine is called from the kernel's PCI subsystem to device with
8216 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8217 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8218 * device to be removed from the PCI subsystem properly.
8219 **/
8220static void __devexit
8221lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8222{
8223 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8224 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8225 struct lpfc_vport **vports;
8226 struct lpfc_hba *phba = vport->phba;
8227 int i;
8228
8229 /* Mark the device unloading flag */
8230 spin_lock_irq(&phba->hbalock);
8231 vport->load_flag |= FC_UNLOADING;
8232 spin_unlock_irq(&phba->hbalock);
8233
8234 /* Free the HBA sysfs attributes */
8235 lpfc_free_sysfs_attr(vport);
8236
8237 /* Release all the vports against this physical port */
8238 vports = lpfc_create_vport_work_array(phba);
8239 if (vports != NULL)
8240 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8241 fc_vport_terminate(vports[i]->fc_vport);
8242 lpfc_destroy_vport_work_array(phba, vports);
8243
8244 /* Remove FC host and then SCSI host with the physical port */
8245 fc_remove_host(shost);
8246 scsi_remove_host(shost);
8247
8248 /* Perform cleanup on the physical port */
8249 lpfc_cleanup(vport);
8250
8251 /*
8252 * Bring down the SLI Layer. This step disables all interrupts,
8253 * clears the rings, discards all mailbox commands, and resets
8254 * the HBA FCoE function.
8255 */
8256 lpfc_debugfs_terminate(vport);
8257 lpfc_sli4_hba_unset(phba);
8258
8259 spin_lock_irq(&phba->hbalock);
8260 list_del_init(&vport->listentry);
8261 spin_unlock_irq(&phba->hbalock);
8262
8263 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
8264 * buffers are released to their corresponding pools here.
8265 */
8266 lpfc_scsi_free(phba);
8267 lpfc_sli4_driver_resource_unset(phba);
8268
8269 /* Unmap adapter Control and Doorbell registers */
8270 lpfc_sli4_pci_mem_unset(phba);
8271
8272 /* Release PCI resources and disable device's PCI function */
8273 scsi_host_put(shost);
8274 lpfc_disable_pci_dev(phba);
8275
8276 /* Finally, free the driver's device data structure */
8277 lpfc_hba_free(phba);
8278
8279 return;
8280}
8281
8282/**
8283 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
8284 * @pdev: pointer to PCI device
8285 * @msg: power management message
8286 *
8287 * This routine is called from the kernel's PCI subsystem to support system
8288 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8289 * this method, it quiesces the device by stopping the driver's worker
8290 * thread for the device, turning off device's interrupt and DMA, and bring
8291 * the device offline. Note that as the driver implements the minimum PM
8292 * requirements to a power-aware driver's PM support for suspend/resume -- all
8293 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8294 * method call will be treated as SUSPEND and the driver will fully
8295 * reinitialize its device during resume() method call, the driver will set
8296 * device to PCI_D3hot state in PCI config space instead of setting it
8297 * according to the @msg provided by the PM.
8298 *
8299 * Return code
8300 * 0 - driver suspended the device
8301 * Error otherwise
8302 **/
8303static int
8304lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
8305{
8306 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8307 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8308
8309 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8310 "0298 PCI device Power Management suspend.\n");
8311
8312 /* Bring down the device */
8313 lpfc_offline_prep(phba);
8314 lpfc_offline(phba);
8315 kthread_stop(phba->worker_thread);
8316
8317 /* Disable interrupt from device */
8318 lpfc_sli4_disable_intr(phba);
8319
8320 /* Save device state to PCI config space */
8321 pci_save_state(pdev);
8322 pci_set_power_state(pdev, PCI_D3hot);
8323
8324 return 0;
8325}
8326
8327/**
8328 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8329 * @pdev: pointer to PCI device
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 spac. When PM invokes
8333 * this method, it restores the device's PCI config space state and fully
8334 * reinitializes the device and brings it online. Note that as the driver
8335 * implements the minimum PM requirements to a power-aware driver's PM for
8336 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8337 * to the suspend() method call will be treated as SUSPEND and the driver
8338 * will fully reinitialize its device during resume() method call, the device
8339 * will be set to PCI_D0 directly in PCI config space before restoring the
8340 * state.
8341 *
8342 * Return code
8343 * 0 - driver suspended the device
8344 * Error otherwise
8345 **/
8346static int
8347lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8348{
8349 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8350 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8351 uint32_t intr_mode;
8352 int error;
8353
8354 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8355 "0292 PCI device Power Management resume.\n");
8356
8357 /* Restore device state from PCI config space */
8358 pci_set_power_state(pdev, PCI_D0);
8359 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008360
8361 /*
8362 * As the new kernel behavior of pci_restore_state() API call clears
8363 * device saved_state flag, need to save the restored state again.
8364 */
8365 pci_save_state(pdev);
8366
James Smartda0436e2009-05-22 14:51:39 -04008367 if (pdev->is_busmaster)
8368 pci_set_master(pdev);
8369
8370 /* Startup the kernel thread for this host adapter. */
8371 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8372 "lpfc_worker_%d", phba->brd_no);
8373 if (IS_ERR(phba->worker_thread)) {
8374 error = PTR_ERR(phba->worker_thread);
8375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8376 "0293 PM resume failed to start worker "
8377 "thread: error=x%x.\n", error);
8378 return error;
8379 }
8380
8381 /* Configure and enable interrupt */
8382 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8383 if (intr_mode == LPFC_INTR_ERROR) {
8384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8385 "0294 PM resume Failed to enable interrupt\n");
8386 return -EIO;
8387 } else
8388 phba->intr_mode = intr_mode;
8389
8390 /* Restart HBA and bring it online */
8391 lpfc_sli_brdrestart(phba);
8392 lpfc_online(phba);
8393
8394 /* Log the current active interrupt mode */
8395 lpfc_log_intr_mode(phba, phba->intr_mode);
8396
8397 return 0;
8398}
8399
8400/**
8401 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
8402 * @pdev: pointer to PCI device.
8403 * @state: the current PCI connection state.
8404 *
8405 * This routine is called from the PCI subsystem for error handling to device
8406 * with SLI-4 interface spec. This function is called by the PCI subsystem
8407 * after a PCI bus error affecting this device has been detected. When this
8408 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8409 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8410 * for the PCI subsystem to perform proper recovery as desired.
8411 *
8412 * Return codes
8413 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8414 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8415 **/
8416static pci_ers_result_t
8417lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8418{
8419 return PCI_ERS_RESULT_NEED_RESET;
8420}
8421
8422/**
8423 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
8424 * @pdev: pointer to PCI device.
8425 *
8426 * This routine is called from the PCI subsystem for error handling to device
8427 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8428 * restart the PCI card from scratch, as if from a cold-boot. During the
8429 * PCI subsystem error recovery, after the driver returns
8430 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8431 * recovery and then call this routine before calling the .resume method to
8432 * recover the device. This function will initialize the HBA device, enable
8433 * the interrupt, but it will just put the HBA to offline state without
8434 * passing any I/O traffic.
8435 *
8436 * Return codes
8437 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8438 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8439 */
8440static pci_ers_result_t
8441lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8442{
8443 return PCI_ERS_RESULT_RECOVERED;
8444}
8445
8446/**
8447 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8448 * @pdev: pointer to PCI device
8449 *
8450 * This routine is called from the PCI subsystem for error handling to device
8451 * with SLI-4 interface spec. It is called when kernel error recovery tells
8452 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8453 * error recovery. After this call, traffic can start to flow from this device
8454 * again.
8455 **/
8456static void
8457lpfc_io_resume_s4(struct pci_dev *pdev)
8458{
8459 return;
8460}
8461
8462/**
James Smart3772a992009-05-22 14:50:54 -04008463 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8464 * @pdev: pointer to PCI device
8465 * @pid: pointer to PCI device identifier
8466 *
8467 * This routine is to be registered to the kernel's PCI subsystem. When an
8468 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8469 * at PCI device-specific information of the device and driver to see if the
8470 * driver state that it can support this kind of device. If the match is
8471 * successful, the driver core invokes this routine. This routine dispatches
8472 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8473 * do all the initialization that it needs to do to handle the HBA device
8474 * properly.
8475 *
8476 * Return code
8477 * 0 - driver can claim the device
8478 * negative value - driver can not claim the device
8479 **/
8480static int __devinit
8481lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8482{
8483 int rc;
James Smart8fa38512009-07-19 10:01:03 -04008484 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04008485
James Smart28baac72010-02-12 14:42:03 -05008486 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04008487 return -ENODEV;
8488
James Smart8fa38512009-07-19 10:01:03 -04008489 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05008490 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04008491 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008492 else
James Smart3772a992009-05-22 14:50:54 -04008493 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008494
James Smart3772a992009-05-22 14:50:54 -04008495 return rc;
8496}
8497
8498/**
8499 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8500 * @pdev: pointer to PCI device
8501 *
8502 * This routine is to be registered to the kernel's PCI subsystem. When an
8503 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8504 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8505 * remove routine, which will perform all the necessary cleanup for the
8506 * device to be removed from the PCI subsystem properly.
8507 **/
8508static void __devexit
8509lpfc_pci_remove_one(struct pci_dev *pdev)
8510{
8511 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8512 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8513
8514 switch (phba->pci_dev_grp) {
8515 case LPFC_PCI_DEV_LP:
8516 lpfc_pci_remove_one_s3(pdev);
8517 break;
James Smartda0436e2009-05-22 14:51:39 -04008518 case LPFC_PCI_DEV_OC:
8519 lpfc_pci_remove_one_s4(pdev);
8520 break;
James Smart3772a992009-05-22 14:50:54 -04008521 default:
8522 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8523 "1424 Invalid PCI device group: 0x%x\n",
8524 phba->pci_dev_grp);
8525 break;
8526 }
8527 return;
8528}
8529
8530/**
8531 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8532 * @pdev: pointer to PCI device
8533 * @msg: power management message
8534 *
8535 * This routine is to be registered to the kernel's PCI subsystem to support
8536 * system Power Management (PM). When PM invokes this method, it dispatches
8537 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8538 * suspend the device.
8539 *
8540 * Return code
8541 * 0 - driver suspended the device
8542 * Error otherwise
8543 **/
8544static int
8545lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8546{
8547 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8548 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8549 int rc = -ENODEV;
8550
8551 switch (phba->pci_dev_grp) {
8552 case LPFC_PCI_DEV_LP:
8553 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8554 break;
James Smartda0436e2009-05-22 14:51:39 -04008555 case LPFC_PCI_DEV_OC:
8556 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8557 break;
James Smart3772a992009-05-22 14:50:54 -04008558 default:
8559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8560 "1425 Invalid PCI device group: 0x%x\n",
8561 phba->pci_dev_grp);
8562 break;
8563 }
8564 return rc;
8565}
8566
8567/**
8568 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8569 * @pdev: pointer to PCI device
8570 *
8571 * This routine is to be registered to the kernel's PCI subsystem to support
8572 * system Power Management (PM). When PM invokes this method, it dispatches
8573 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8574 * resume the device.
8575 *
8576 * Return code
8577 * 0 - driver suspended the device
8578 * Error otherwise
8579 **/
8580static int
8581lpfc_pci_resume_one(struct pci_dev *pdev)
8582{
8583 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8584 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8585 int rc = -ENODEV;
8586
8587 switch (phba->pci_dev_grp) {
8588 case LPFC_PCI_DEV_LP:
8589 rc = lpfc_pci_resume_one_s3(pdev);
8590 break;
James Smartda0436e2009-05-22 14:51:39 -04008591 case LPFC_PCI_DEV_OC:
8592 rc = lpfc_pci_resume_one_s4(pdev);
8593 break;
James Smart3772a992009-05-22 14:50:54 -04008594 default:
8595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8596 "1426 Invalid PCI device group: 0x%x\n",
8597 phba->pci_dev_grp);
8598 break;
8599 }
8600 return rc;
8601}
8602
8603/**
8604 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8605 * @pdev: pointer to PCI device.
8606 * @state: the current PCI connection state.
8607 *
8608 * This routine is registered to the PCI subsystem for error handling. This
8609 * function is called by the PCI subsystem after a PCI bus error affecting
8610 * this device has been detected. When this routine is invoked, it dispatches
8611 * the action to the proper SLI-3 or SLI-4 device error detected handling
8612 * routine, which will perform the proper error detected operation.
8613 *
8614 * Return codes
8615 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8616 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8617 **/
8618static pci_ers_result_t
8619lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8620{
8621 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8622 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8623 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8624
8625 switch (phba->pci_dev_grp) {
8626 case LPFC_PCI_DEV_LP:
8627 rc = lpfc_io_error_detected_s3(pdev, state);
8628 break;
James Smartda0436e2009-05-22 14:51:39 -04008629 case LPFC_PCI_DEV_OC:
8630 rc = lpfc_io_error_detected_s4(pdev, state);
8631 break;
James Smart3772a992009-05-22 14:50:54 -04008632 default:
8633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8634 "1427 Invalid PCI device group: 0x%x\n",
8635 phba->pci_dev_grp);
8636 break;
8637 }
8638 return rc;
8639}
8640
8641/**
8642 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8643 * @pdev: pointer to PCI device.
8644 *
8645 * This routine is registered to the PCI subsystem for error handling. This
8646 * function is called after PCI bus has been reset to restart the PCI card
8647 * from scratch, as if from a cold-boot. When this routine is invoked, it
8648 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8649 * routine, which will perform the proper device reset.
8650 *
8651 * Return codes
8652 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8653 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8654 **/
8655static pci_ers_result_t
8656lpfc_io_slot_reset(struct pci_dev *pdev)
8657{
8658 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8659 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8660 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8661
8662 switch (phba->pci_dev_grp) {
8663 case LPFC_PCI_DEV_LP:
8664 rc = lpfc_io_slot_reset_s3(pdev);
8665 break;
James Smartda0436e2009-05-22 14:51:39 -04008666 case LPFC_PCI_DEV_OC:
8667 rc = lpfc_io_slot_reset_s4(pdev);
8668 break;
James Smart3772a992009-05-22 14:50:54 -04008669 default:
8670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8671 "1428 Invalid PCI device group: 0x%x\n",
8672 phba->pci_dev_grp);
8673 break;
8674 }
8675 return rc;
8676}
8677
8678/**
8679 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8680 * @pdev: pointer to PCI device
8681 *
8682 * This routine is registered to the PCI subsystem for error handling. It
8683 * is called when kernel error recovery tells the lpfc driver that it is
8684 * OK to resume normal PCI operation after PCI bus error recovery. When
8685 * this routine is invoked, it dispatches the action to the proper SLI-3
8686 * or SLI-4 device io_resume routine, which will resume the device operation.
8687 **/
8688static void
8689lpfc_io_resume(struct pci_dev *pdev)
8690{
8691 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8692 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8693
8694 switch (phba->pci_dev_grp) {
8695 case LPFC_PCI_DEV_LP:
8696 lpfc_io_resume_s3(pdev);
8697 break;
James Smartda0436e2009-05-22 14:51:39 -04008698 case LPFC_PCI_DEV_OC:
8699 lpfc_io_resume_s4(pdev);
8700 break;
James Smart3772a992009-05-22 14:50:54 -04008701 default:
8702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8703 "1429 Invalid PCI device group: 0x%x\n",
8704 phba->pci_dev_grp);
8705 break;
8706 }
8707 return;
8708}
8709
dea31012005-04-17 16:05:31 -05008710static struct pci_device_id lpfc_id_table[] = {
8711 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8712 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008713 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8714 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008715 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8716 PCI_ANY_ID, PCI_ANY_ID, },
8717 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8718 PCI_ANY_ID, PCI_ANY_ID, },
8719 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8720 PCI_ANY_ID, PCI_ANY_ID, },
8721 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8722 PCI_ANY_ID, PCI_ANY_ID, },
8723 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8724 PCI_ANY_ID, PCI_ANY_ID, },
8725 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8726 PCI_ANY_ID, PCI_ANY_ID, },
8727 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8728 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008729 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8730 PCI_ANY_ID, PCI_ANY_ID, },
8731 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8732 PCI_ANY_ID, PCI_ANY_ID, },
8733 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8734 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008735 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8736 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008737 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8738 PCI_ANY_ID, PCI_ANY_ID, },
8739 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8740 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008741 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8742 PCI_ANY_ID, PCI_ANY_ID, },
8743 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8744 PCI_ANY_ID, PCI_ANY_ID, },
8745 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8746 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008747 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8748 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008749 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8750 PCI_ANY_ID, PCI_ANY_ID, },
8751 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8752 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008753 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8754 PCI_ANY_ID, PCI_ANY_ID, },
8755 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8756 PCI_ANY_ID, PCI_ANY_ID, },
8757 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8758 PCI_ANY_ID, PCI_ANY_ID, },
8759 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8760 PCI_ANY_ID, PCI_ANY_ID, },
8761 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8762 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008763 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8764 PCI_ANY_ID, PCI_ANY_ID, },
8765 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8766 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008767 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8768 PCI_ANY_ID, PCI_ANY_ID, },
8769 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8770 PCI_ANY_ID, PCI_ANY_ID, },
8771 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8772 PCI_ANY_ID, PCI_ANY_ID, },
8773 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8774 PCI_ANY_ID, PCI_ANY_ID, },
8775 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8776 PCI_ANY_ID, PCI_ANY_ID, },
8777 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8778 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008779 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8780 PCI_ANY_ID, PCI_ANY_ID, },
8781 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8782 PCI_ANY_ID, PCI_ANY_ID, },
8783 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8784 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008785 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8786 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05008787 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8788 PCI_ANY_ID, PCI_ANY_ID, },
8789 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04008790 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008791 { 0 }
8792};
8793
8794MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8795
Linas Vepstas8d63f372007-02-14 14:28:36 -06008796static struct pci_error_handlers lpfc_err_handler = {
8797 .error_detected = lpfc_io_error_detected,
8798 .slot_reset = lpfc_io_slot_reset,
8799 .resume = lpfc_io_resume,
8800};
8801
dea31012005-04-17 16:05:31 -05008802static struct pci_driver lpfc_driver = {
8803 .name = LPFC_DRIVER_NAME,
8804 .id_table = lpfc_id_table,
8805 .probe = lpfc_pci_probe_one,
8806 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008807 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008808 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008809 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008810};
8811
James Smarte59058c2008-08-24 21:49:00 -04008812/**
James Smart3621a712009-04-06 18:47:14 -04008813 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008814 *
8815 * This routine is to be invoked when the lpfc module is loaded into the
8816 * kernel. The special kernel macro module_init() is used to indicate the
8817 * role of this routine to the kernel as lpfc module entry point.
8818 *
8819 * Return codes
8820 * 0 - successful
8821 * -ENOMEM - FC attach transport failed
8822 * all others - failed
8823 */
dea31012005-04-17 16:05:31 -05008824static int __init
8825lpfc_init(void)
8826{
8827 int error = 0;
8828
8829 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008830 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008831
James Smart7ee5d432007-10-27 13:37:17 -04008832 if (lpfc_enable_npiv) {
8833 lpfc_transport_functions.vport_create = lpfc_vport_create;
8834 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8835 }
dea31012005-04-17 16:05:31 -05008836 lpfc_transport_template =
8837 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008838 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008839 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008840 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008841 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008842 fc_attach_transport(&lpfc_vport_transport_functions);
8843 if (lpfc_vport_transport_template == NULL) {
8844 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008845 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008846 }
James Smart7ee5d432007-10-27 13:37:17 -04008847 }
dea31012005-04-17 16:05:31 -05008848 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008849 if (error) {
dea31012005-04-17 16:05:31 -05008850 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008851 if (lpfc_enable_npiv)
8852 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008853 }
dea31012005-04-17 16:05:31 -05008854
8855 return error;
8856}
8857
James Smarte59058c2008-08-24 21:49:00 -04008858/**
James Smart3621a712009-04-06 18:47:14 -04008859 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008860 *
8861 * This routine is invoked when the lpfc module is removed from the kernel.
8862 * The special kernel macro module_exit() is used to indicate the role of
8863 * this routine to the kernel as lpfc module exit point.
8864 */
dea31012005-04-17 16:05:31 -05008865static void __exit
8866lpfc_exit(void)
8867{
8868 pci_unregister_driver(&lpfc_driver);
8869 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008870 if (lpfc_enable_npiv)
8871 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008872 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008873 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8874 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008875 (1L << _dump_buf_data_order), _dump_buf_data);
8876 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8877 }
8878
8879 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008880 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8881 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008882 (1L << _dump_buf_dif_order), _dump_buf_dif);
8883 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8884 }
dea31012005-04-17 16:05:31 -05008885}
8886
8887module_init(lpfc_init);
8888module_exit(lpfc_exit);
8889MODULE_LICENSE("GPL");
8890MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8891MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8892MODULE_VERSION("0:" LPFC_DRIVER_VERSION);