blob: 56421c714bf84cabd38af838fe7d383ed145b839 [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 Smarte2aed292010-02-26 14:15:00 -05002603 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
2604 phba->menlo_flag |= HBA_MENLO_SUPPORT;
2605 /* check for menlo minimum sg count */
2606 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT) {
2607 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
2608 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2609 }
2610 }
James Smart47a86172007-04-25 09:53:22 -04002611
James Bottomleyd139b9b2009-11-05 13:33:12 -06002612 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002613 if (error)
2614 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002615
James Smart549e55c2007-08-02 11:09:51 -04002616 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002617 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002618 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002619 return vport;
James Smart47a86172007-04-25 09:53:22 -04002620
James Smart2e0fef82007-06-17 19:56:36 -05002621out_put_shost:
2622 scsi_host_put(shost);
2623out:
2624 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002625}
2626
James Smarte59058c2008-08-24 21:49:00 -04002627/**
James Smart3621a712009-04-06 18:47:14 -04002628 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002629 * @vport: pointer to an lpfc virtual N_Port data structure.
2630 *
2631 * This routine destroys a FC port from the upper layer protocol. All the
2632 * resources associated with the port are released.
2633 **/
James Smart2e0fef82007-06-17 19:56:36 -05002634void
2635destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002636{
James Smart92d7f7b2007-06-17 19:56:38 -05002637 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2638 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002639
James Smart858c9f62007-06-17 19:56:39 -05002640 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002641 fc_remove_host(shost);
2642 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002643
James Smart92d7f7b2007-06-17 19:56:38 -05002644 spin_lock_irq(&phba->hbalock);
2645 list_del_init(&vport->listentry);
2646 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002647
James Smart92d7f7b2007-06-17 19:56:38 -05002648 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002649 return;
James Smart47a86172007-04-25 09:53:22 -04002650}
2651
James Smarte59058c2008-08-24 21:49:00 -04002652/**
James Smart3621a712009-04-06 18:47:14 -04002653 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002654 *
2655 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2656 * uses the kernel idr facility to perform the task.
2657 *
2658 * Return codes:
2659 * instance - a unique integer ID allocated as the new instance.
2660 * -1 - lpfc get instance failed.
2661 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002662int
2663lpfc_get_instance(void)
2664{
2665 int instance = 0;
2666
2667 /* Assign an unused number */
2668 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2669 return -1;
2670 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2671 return -1;
2672 return instance;
2673}
2674
James Smarte59058c2008-08-24 21:49:00 -04002675/**
James Smart3621a712009-04-06 18:47:14 -04002676 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002677 * @shost: pointer to SCSI host data structure.
2678 * @time: elapsed time of the scan in jiffies.
2679 *
2680 * This routine is called by the SCSI layer with a SCSI host to determine
2681 * whether the scan host is finished.
2682 *
2683 * Note: there is no scan_start function as adapter initialization will have
2684 * asynchronously kicked off the link initialization.
2685 *
2686 * Return codes
2687 * 0 - SCSI host scan is not over yet.
2688 * 1 - SCSI host scan is over.
2689 **/
James Smart47a86172007-04-25 09:53:22 -04002690int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2691{
James Smart2e0fef82007-06-17 19:56:36 -05002692 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2693 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002694 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002695
James Smart858c9f62007-06-17 19:56:39 -05002696 spin_lock_irq(shost->host_lock);
2697
James Smart51ef4c22007-08-02 11:10:31 -04002698 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002699 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002700 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002701 }
James Smart2e0fef82007-06-17 19:56:36 -05002702 if (time >= 30 * HZ) {
2703 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002704 "0461 Scanning longer than 30 "
2705 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002706 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002707 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002708 }
2709 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2710 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002711 "0465 Link down longer than 15 "
2712 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002713 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002714 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002715 }
2716
James Smart2e0fef82007-06-17 19:56:36 -05002717 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002718 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002719 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002720 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002721 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002722 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002723 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002724 goto finished;
2725
2726 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002727
2728finished:
James Smart858c9f62007-06-17 19:56:39 -05002729 spin_unlock_irq(shost->host_lock);
2730 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002731}
2732
James Smarte59058c2008-08-24 21:49:00 -04002733/**
James Smart3621a712009-04-06 18:47:14 -04002734 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002735 * @shost: pointer to SCSI host data structure.
2736 *
2737 * This routine initializes a given SCSI host attributes on a FC port. The
2738 * SCSI host can be either on top of a physical port or a virtual port.
2739 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002740void lpfc_host_attrib_init(struct Scsi_Host *shost)
2741{
2742 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2743 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002744 /*
James Smart2e0fef82007-06-17 19:56:36 -05002745 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002746 */
2747
James Smart2e0fef82007-06-17 19:56:36 -05002748 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2749 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002750 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2751
2752 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002753 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002754 fc_host_supported_fc4s(shost)[2] = 1;
2755 fc_host_supported_fc4s(shost)[7] = 1;
2756
James Smart92d7f7b2007-06-17 19:56:38 -05002757 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2758 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002759
2760 fc_host_supported_speeds(shost) = 0;
2761 if (phba->lmt & LMT_10Gb)
2762 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002763 if (phba->lmt & LMT_8Gb)
2764 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002765 if (phba->lmt & LMT_4Gb)
2766 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2767 if (phba->lmt & LMT_2Gb)
2768 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2769 if (phba->lmt & LMT_1Gb)
2770 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2771
2772 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002773 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2774 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002775
2776 /* This value is also unchanging */
2777 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002778 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002779 fc_host_active_fc4s(shost)[2] = 1;
2780 fc_host_active_fc4s(shost)[7] = 1;
2781
James Smart92d7f7b2007-06-17 19:56:38 -05002782 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002783 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002784 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002785 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002786}
dea31012005-04-17 16:05:31 -05002787
James Smarte59058c2008-08-24 21:49:00 -04002788/**
James Smartda0436e2009-05-22 14:51:39 -04002789 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002790 * @phba: pointer to lpfc hba data structure.
2791 *
James Smartda0436e2009-05-22 14:51:39 -04002792 * This routine is invoked to stop an SLI3 device port, it stops the device
2793 * from generating interrupts and stops the device driver's timers for the
2794 * device.
James Smarte59058c2008-08-24 21:49:00 -04002795 **/
James Smartdb2378e2008-02-08 18:49:51 -05002796static void
James Smartda0436e2009-05-22 14:51:39 -04002797lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002798{
James Smartda0436e2009-05-22 14:51:39 -04002799 /* Clear all interrupt enable conditions */
2800 writel(0, phba->HCregaddr);
2801 readl(phba->HCregaddr); /* flush */
2802 /* Clear all pending interrupts */
2803 writel(0xffffffff, phba->HAregaddr);
2804 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002805
James Smartda0436e2009-05-22 14:51:39 -04002806 /* Reset some HBA SLI setup states */
2807 lpfc_stop_hba_timers(phba);
2808 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002809}
2810
James Smarte59058c2008-08-24 21:49:00 -04002811/**
James Smartda0436e2009-05-22 14:51:39 -04002812 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002813 * @phba: pointer to lpfc hba data structure.
2814 *
James Smartda0436e2009-05-22 14:51:39 -04002815 * This routine is invoked to stop an SLI4 device port, it stops the device
2816 * from generating interrupts and stops the device driver's timers for the
2817 * device.
2818 **/
2819static void
2820lpfc_stop_port_s4(struct lpfc_hba *phba)
2821{
2822 /* Reset some HBA SLI4 setup states */
2823 lpfc_stop_hba_timers(phba);
2824 phba->pport->work_port_events = 0;
2825 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04002826}
2827
2828/**
2829 * lpfc_stop_port - Wrapper function for stopping hba port
2830 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002831 *
James Smartda0436e2009-05-22 14:51:39 -04002832 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2833 * the API jump table function pointer from the lpfc_hba struct.
2834 **/
2835void
2836lpfc_stop_port(struct lpfc_hba *phba)
2837{
2838 phba->lpfc_stop_port(phba);
2839}
2840
2841/**
2842 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2843 * @phba: pointer to lpfc hba data structure.
2844 *
2845 * This routine is invoked to remove the driver default fcf record from
2846 * the port. This routine currently acts on FCF Index 0.
2847 *
2848 **/
2849void
2850lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2851{
2852 int rc = 0;
2853 LPFC_MBOXQ_t *mboxq;
2854 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2855 uint32_t mbox_tmo, req_len;
2856 uint32_t shdr_status, shdr_add_status;
2857
2858 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2859 if (!mboxq) {
2860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2861 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2862 return;
2863 }
2864
2865 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2866 sizeof(struct lpfc_sli4_cfg_mhdr);
2867 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2868 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2869 req_len, LPFC_SLI4_MBX_EMBED);
2870 /*
2871 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2872 * supports multiple FCF indices.
2873 */
2874 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2875 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2876 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
James Smartecfd03c2010-02-12 14:41:27 -05002877 phba->fcf.current_rec.fcf_indx);
James Smartda0436e2009-05-22 14:51:39 -04002878
2879 if (!phba->sli4_hba.intr_enable)
2880 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2881 else {
2882 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2883 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2884 }
2885 /* The IOCTL status is embedded in the mailbox subheader. */
2886 shdr_status = bf_get(lpfc_mbox_hdr_status,
2887 &del_fcf_record->header.cfg_shdr.response);
2888 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2889 &del_fcf_record->header.cfg_shdr.response);
2890 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2892 "2516 DEL FCF of default FCF Index failed "
2893 "mbx status x%x, status x%x add_status x%x\n",
2894 rc, shdr_status, shdr_add_status);
2895 }
2896 if (rc != MBX_TIMEOUT)
2897 mempool_free(mboxq, phba->mbox_mem_pool);
2898}
2899
2900/**
James Smartecfd03c2010-02-12 14:41:27 -05002901 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2902 * @phba: Pointer to hba for which this call is being executed.
2903 *
2904 * This routine starts the timer waiting for the FCF rediscovery to complete.
2905 **/
2906void
2907lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2908{
2909 unsigned long fcf_redisc_wait_tmo =
2910 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2911 /* Start fcf rediscovery wait period timer */
2912 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2913 spin_lock_irq(&phba->hbalock);
2914 /* Allow action to new fcf asynchronous event */
2915 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2916 /* Mark the FCF rediscovery pending state */
2917 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2918 spin_unlock_irq(&phba->hbalock);
2919}
2920
2921/**
2922 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2923 * @ptr: Map to lpfc_hba data structure pointer.
2924 *
2925 * This routine is invoked when waiting for FCF table rediscover has been
2926 * timed out. If new FCF record(s) has (have) been discovered during the
2927 * wait period, a new FCF event shall be added to the FCOE async event
2928 * list, and then worker thread shall be waked up for processing from the
2929 * worker thread context.
2930 **/
2931void
2932lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2933{
2934 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2935
2936 /* Don't send FCF rediscovery event if timer cancelled */
2937 spin_lock_irq(&phba->hbalock);
2938 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2939 spin_unlock_irq(&phba->hbalock);
2940 return;
2941 }
2942 /* Clear FCF rediscovery timer pending flag */
2943 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2944 /* FCF rediscovery event to worker thread */
2945 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2946 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002947 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2948 "2776 FCF rediscover wait timer expired, post "
2949 "a worker thread event for FCF table scan\n");
James Smartecfd03c2010-02-12 14:41:27 -05002950 /* wake up worker thread */
2951 lpfc_worker_wake_up(phba);
2952}
2953
2954/**
James Smart6669f9b2009-10-02 15:16:45 -04002955 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2956 * @phba: pointer to lpfc hba data structure.
2957 *
2958 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2959 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2960 * was successful and the firmware supports FCoE. Any other return indicates
2961 * a error. It is assumed that this function will be called before interrupts
2962 * are enabled.
2963 **/
2964static int
2965lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2966{
2967 int rc = 0;
2968 LPFC_MBOXQ_t *mboxq;
2969 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2970 uint32_t length;
2971 uint32_t shdr_status, shdr_add_status;
2972
2973 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2974 if (!mboxq) {
2975 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2976 "2621 Failed to allocate mbox for "
2977 "query firmware config cmd\n");
2978 return -ENOMEM;
2979 }
2980 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2981 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2982 sizeof(struct lpfc_sli4_cfg_mhdr));
2983 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2984 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2985 length, LPFC_SLI4_MBX_EMBED);
2986 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2987 /* The IOCTL status is embedded in the mailbox subheader. */
2988 shdr_status = bf_get(lpfc_mbox_hdr_status,
2989 &query_fw_cfg->header.cfg_shdr.response);
2990 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2991 &query_fw_cfg->header.cfg_shdr.response);
2992 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2994 "2622 Query Firmware Config failed "
2995 "mbx status x%x, status x%x add_status x%x\n",
2996 rc, shdr_status, shdr_add_status);
2997 return -EINVAL;
2998 }
2999 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
3000 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3001 "2623 FCoE Function not supported by firmware. "
3002 "Function mode = %08x\n",
3003 query_fw_cfg->function_mode);
3004 return -EINVAL;
3005 }
3006 if (rc != MBX_TIMEOUT)
3007 mempool_free(mboxq, phba->mbox_mem_pool);
3008 return 0;
3009}
3010
3011/**
James Smartda0436e2009-05-22 14:51:39 -04003012 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3013 * @phba: pointer to lpfc hba data structure.
3014 * @acqe_link: pointer to the async link completion queue entry.
3015 *
3016 * This routine is to parse the SLI4 link-attention link fault code and
3017 * translate it into the base driver's read link attention mailbox command
3018 * status.
3019 *
3020 * Return: Link-attention status in terms of base driver's coding.
3021 **/
3022static uint16_t
3023lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3024 struct lpfc_acqe_link *acqe_link)
3025{
3026 uint16_t latt_fault;
3027
3028 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3029 case LPFC_ASYNC_LINK_FAULT_NONE:
3030 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3031 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3032 latt_fault = 0;
3033 break;
3034 default:
3035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3036 "0398 Invalid link fault code: x%x\n",
3037 bf_get(lpfc_acqe_link_fault, acqe_link));
3038 latt_fault = MBXERR_ERROR;
3039 break;
3040 }
3041 return latt_fault;
3042}
3043
3044/**
3045 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3046 * @phba: pointer to lpfc hba data structure.
3047 * @acqe_link: pointer to the async link completion queue entry.
3048 *
3049 * This routine is to parse the SLI4 link attention type and translate it
3050 * into the base driver's link attention type coding.
3051 *
3052 * Return: Link attention type in terms of base driver's coding.
3053 **/
3054static uint8_t
3055lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3056 struct lpfc_acqe_link *acqe_link)
3057{
3058 uint8_t att_type;
3059
3060 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3061 case LPFC_ASYNC_LINK_STATUS_DOWN:
3062 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3063 att_type = AT_LINK_DOWN;
3064 break;
3065 case LPFC_ASYNC_LINK_STATUS_UP:
3066 /* Ignore physical link up events - wait for logical link up */
3067 att_type = AT_RESERVED;
3068 break;
3069 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3070 att_type = AT_LINK_UP;
3071 break;
3072 default:
3073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3074 "0399 Invalid link attention type: x%x\n",
3075 bf_get(lpfc_acqe_link_status, acqe_link));
3076 att_type = AT_RESERVED;
3077 break;
3078 }
3079 return att_type;
3080}
3081
3082/**
3083 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3084 * @phba: pointer to lpfc hba data structure.
3085 * @acqe_link: pointer to the async link completion queue entry.
3086 *
3087 * This routine is to parse the SLI4 link-attention link speed and translate
3088 * it into the base driver's link-attention link speed coding.
3089 *
3090 * Return: Link-attention link speed in terms of base driver's coding.
3091 **/
3092static uint8_t
3093lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3094 struct lpfc_acqe_link *acqe_link)
3095{
3096 uint8_t link_speed;
3097
3098 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3099 case LPFC_ASYNC_LINK_SPEED_ZERO:
3100 link_speed = LA_UNKNW_LINK;
3101 break;
3102 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3103 link_speed = LA_UNKNW_LINK;
3104 break;
3105 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3106 link_speed = LA_UNKNW_LINK;
3107 break;
3108 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3109 link_speed = LA_1GHZ_LINK;
3110 break;
3111 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3112 link_speed = LA_10GHZ_LINK;
3113 break;
3114 default:
3115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3116 "0483 Invalid link-attention link speed: x%x\n",
3117 bf_get(lpfc_acqe_link_speed, acqe_link));
3118 link_speed = LA_UNKNW_LINK;
3119 break;
3120 }
3121 return link_speed;
3122}
3123
3124/**
3125 * lpfc_sli4_async_link_evt - Process the asynchronous link event
3126 * @phba: pointer to lpfc hba data structure.
3127 * @acqe_link: pointer to the async link completion queue entry.
3128 *
3129 * This routine is to handle the SLI4 asynchronous link event.
3130 **/
3131static void
3132lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3133 struct lpfc_acqe_link *acqe_link)
3134{
3135 struct lpfc_dmabuf *mp;
3136 LPFC_MBOXQ_t *pmb;
3137 MAILBOX_t *mb;
3138 READ_LA_VAR *la;
3139 uint8_t att_type;
3140
3141 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3142 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3143 return;
James Smart32b97932009-07-19 10:01:21 -04003144 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003145 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3146 if (!pmb) {
3147 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3148 "0395 The mboxq allocation failed\n");
3149 return;
3150 }
3151 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3152 if (!mp) {
3153 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3154 "0396 The lpfc_dmabuf allocation failed\n");
3155 goto out_free_pmb;
3156 }
3157 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3158 if (!mp->virt) {
3159 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3160 "0397 The mbuf allocation failed\n");
3161 goto out_free_dmabuf;
3162 }
3163
3164 /* Cleanup any outstanding ELS commands */
3165 lpfc_els_flush_all_cmd(phba);
3166
3167 /* Block ELS IOCBs until we have done process link event */
3168 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3169
3170 /* Update link event statistics */
3171 phba->sli.slistat.link_event++;
3172
3173 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3174 lpfc_read_la(phba, pmb, mp);
3175 pmb->vport = phba->pport;
3176
3177 /* Parse and translate status field */
3178 mb = &pmb->u.mb;
3179 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3180
3181 /* Parse and translate link attention fields */
3182 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3183 la->eventTag = acqe_link->event_tag;
3184 la->attType = att_type;
3185 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3186
3187 /* Fake the the following irrelvant fields */
3188 la->topology = TOPOLOGY_PT_PT;
3189 la->granted_AL_PA = 0;
3190 la->il = 0;
3191 la->pb = 0;
3192 la->fa = 0;
3193 la->mm = 0;
3194
3195 /* Keep the link status for extra SLI4 state machine reference */
3196 phba->sli4_hba.link_state.speed =
3197 bf_get(lpfc_acqe_link_speed, acqe_link);
3198 phba->sli4_hba.link_state.duplex =
3199 bf_get(lpfc_acqe_link_duplex, acqe_link);
3200 phba->sli4_hba.link_state.status =
3201 bf_get(lpfc_acqe_link_status, acqe_link);
3202 phba->sli4_hba.link_state.physical =
3203 bf_get(lpfc_acqe_link_physical, acqe_link);
3204 phba->sli4_hba.link_state.fault =
3205 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003206 phba->sli4_hba.link_state.logical_speed =
3207 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003208
3209 /* Invoke the lpfc_handle_latt mailbox command callback function */
3210 lpfc_mbx_cmpl_read_la(phba, pmb);
3211
3212 return;
3213
3214out_free_dmabuf:
3215 kfree(mp);
3216out_free_pmb:
3217 mempool_free(pmb, phba->mbox_mem_pool);
3218}
3219
3220/**
James Smartfc2b9892010-02-26 14:15:29 -05003221 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3222 * @vport: pointer to vport data structure.
3223 *
3224 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3225 * response to a CVL event.
3226 *
3227 * Return the pointer to the ndlp with the vport if successful, otherwise
3228 * return NULL.
3229 **/
3230static struct lpfc_nodelist *
3231lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3232{
3233 struct lpfc_nodelist *ndlp;
3234 struct Scsi_Host *shost;
3235 struct lpfc_hba *phba;
3236
3237 if (!vport)
3238 return NULL;
3239 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3240 if (!ndlp)
3241 return NULL;
3242 phba = vport->phba;
3243 if (!phba)
3244 return NULL;
3245 if (phba->pport->port_state <= LPFC_FLOGI)
3246 return NULL;
3247 /* If virtual link is not yet instantiated ignore CVL */
3248 if (vport->port_state <= LPFC_FDISC)
3249 return NULL;
3250 shost = lpfc_shost_from_vport(vport);
3251 if (!shost)
3252 return NULL;
3253 lpfc_linkdown_port(vport);
3254 lpfc_cleanup_pending_mbox(vport);
3255 spin_lock_irq(shost->host_lock);
3256 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3257 spin_unlock_irq(shost->host_lock);
3258
3259 return ndlp;
3260}
3261
3262/**
3263 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3264 * @vport: pointer to lpfc hba data structure.
3265 *
3266 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3267 * response to a FCF dead event.
3268 **/
3269static void
3270lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3271{
3272 struct lpfc_vport **vports;
3273 int i;
3274
3275 vports = lpfc_create_vport_work_array(phba);
3276 if (vports)
3277 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3278 lpfc_sli4_perform_vport_cvl(vports[i]);
3279 lpfc_destroy_vport_work_array(phba, vports);
3280}
3281
3282/**
James Smartda0436e2009-05-22 14:51:39 -04003283 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3284 * @phba: pointer to lpfc hba data structure.
3285 * @acqe_link: pointer to the async fcoe completion queue entry.
3286 *
3287 * This routine is to handle the SLI4 asynchronous fcoe event.
3288 **/
3289static void
3290lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3291 struct lpfc_acqe_fcoe *acqe_fcoe)
3292{
3293 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3294 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003295 struct lpfc_vport *vport;
3296 struct lpfc_nodelist *ndlp;
3297 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003298 int active_vlink_present;
3299 struct lpfc_vport **vports;
3300 int i;
James Smartda0436e2009-05-22 14:51:39 -04003301
James Smart4d9ab992009-10-02 15:16:39 -04003302 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003303 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003304 switch (event_type) {
3305 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
James Smartecfd03c2010-02-12 14:41:27 -05003306 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
James Smart999d8132010-03-15 11:24:56 -04003307 if (event_type == LPFC_FCOE_EVENT_TYPE_NEW_FCF)
3308 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3309 LOG_DISCOVERY,
3310 "2546 New FCF found event: "
3311 "evt_tag:x%x, fcf_index:x%x\n",
3312 acqe_fcoe->event_tag,
3313 acqe_fcoe->index);
3314 else
3315 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3316 LOG_DISCOVERY,
3317 "2788 FCF parameter modified event: "
3318 "evt_tag:x%x, fcf_index:x%x\n",
3319 acqe_fcoe->event_tag,
3320 acqe_fcoe->index);
James Smartda0436e2009-05-22 14:51:39 -04003321 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003322 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3323 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3324 /*
3325 * If the current FCF is in discovered state or
3326 * FCF discovery is in progress, do nothing.
3327 */
3328 spin_unlock_irq(&phba->hbalock);
3329 break;
3330 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003331
James Smartecfd03c2010-02-12 14:41:27 -05003332 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3333 /*
3334 * If fast FCF failover rescan event is pending,
3335 * do nothing.
3336 */
James Smartda0436e2009-05-22 14:51:39 -04003337 spin_unlock_irq(&phba->hbalock);
3338 break;
3339 }
3340 spin_unlock_irq(&phba->hbalock);
3341
James Smart0c9ab6f2010-02-26 14:15:57 -05003342 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
3343 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
3344 /*
3345 * During period of FCF discovery, read the FCF
3346 * table record indexed by the event to update
3347 * FCF round robin failover eligible FCF bmask.
3348 */
3349 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3350 LOG_DISCOVERY,
3351 "2779 Read new FCF record with "
3352 "fcf_index:x%x for updating FCF "
3353 "round robin failover bmask\n",
3354 acqe_fcoe->index);
3355 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fcoe->index);
3356 }
3357
3358 /* Otherwise, scan the entire FCF table and re-discover SAN */
3359 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3360 "2770 Start FCF table scan due to new FCF "
3361 "event: evt_tag:x%x, fcf_index:x%x\n",
3362 acqe_fcoe->event_tag, acqe_fcoe->index);
3363 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3364 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003365 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003366 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3367 "2547 Issue FCF scan read FCF mailbox "
3368 "command failed 0x%x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003369 break;
3370
3371 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3372 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003373 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003374 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3375 acqe_fcoe->event_tag);
3376 break;
3377
3378 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003379 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003380 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003381 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003382 acqe_fcoe->event_tag);
3383 /* If the event is not for currently used fcf do nothing */
James Smartecfd03c2010-02-12 14:41:27 -05003384 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003385 break;
James Smartfc2b9892010-02-26 14:15:29 -05003386 /* We request port to rediscover the entire FCF table for
3387 * a fast recovery from case that the current FCF record
James Smart0c9ab6f2010-02-26 14:15:57 -05003388 * is no longer valid if we are not in the middle of FCF
3389 * failover process already.
James Smartda0436e2009-05-22 14:51:39 -04003390 */
James Smartfc2b9892010-02-26 14:15:29 -05003391 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003392 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003393 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003394 /* Update FLOGI FCF failover eligible FCF bmask */
3395 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fcoe->index);
James Smartfc2b9892010-02-26 14:15:29 -05003396 break;
3397 }
3398 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003399 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003400 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003401 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3402 "2771 Start FCF fast failover process due to "
3403 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3404 "\n", acqe_fcoe->event_tag, acqe_fcoe->index);
James Smartfc2b9892010-02-26 14:15:29 -05003405 rc = lpfc_sli4_redisc_fcf_table(phba);
3406 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003407 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3408 LOG_DISCOVERY,
3409 "2772 Issue FCF rediscover mabilbox "
3410 "command failed, fail through to FCF "
3411 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003412 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003413 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003414 spin_unlock_irq(&phba->hbalock);
3415 /*
3416 * Last resort will fail over by treating this
3417 * as a link down to FCF registration.
3418 */
3419 lpfc_sli4_fcf_dead_failthrough(phba);
3420 } else
3421 /* Handling fast FCF failover to a DEAD FCF event
3422 * is considered equalivant to receiving CVL to all
3423 * vports.
3424 */
3425 lpfc_sli4_perform_all_vport_cvl(phba);
James Smartda0436e2009-05-22 14:51:39 -04003426 break;
James Smart6669f9b2009-10-02 15:16:45 -04003427 case LPFC_FCOE_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003428 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003429 "2718 Clear Virtual Link Received for VPI 0x%x"
3430 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3431 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003432 acqe_fcoe->index - phba->vpi_base);
James Smartfc2b9892010-02-26 14:15:29 -05003433 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003434 if (!ndlp)
3435 break;
James Smart695a8142010-01-26 23:08:03 -05003436 active_vlink_present = 0;
3437
3438 vports = lpfc_create_vport_work_array(phba);
3439 if (vports) {
3440 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3441 i++) {
3442 if ((!(vports[i]->fc_flag &
3443 FC_VPORT_CVL_RCVD)) &&
3444 (vports[i]->port_state > LPFC_FDISC)) {
3445 active_vlink_present = 1;
3446 break;
3447 }
3448 }
3449 lpfc_destroy_vport_work_array(phba, vports);
3450 }
3451
3452 if (active_vlink_present) {
3453 /*
3454 * If there are other active VLinks present,
3455 * re-instantiate the Vlink using FDISC.
3456 */
James Smart6669f9b2009-10-02 15:16:45 -04003457 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003458 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003459 spin_lock_irq(shost->host_lock);
3460 ndlp->nlp_flag |= NLP_DELAY_TMO;
3461 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003462 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3463 vport->port_state = LPFC_FDISC;
3464 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003465 /*
3466 * Otherwise, we request port to rediscover
3467 * the entire FCF table for a fast recovery
3468 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003469 * is no longer valid if we are not already
3470 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003471 */
James Smartfc2b9892010-02-26 14:15:29 -05003472 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003473 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003474 spin_unlock_irq(&phba->hbalock);
3475 break;
3476 }
3477 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003478 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003479 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003480 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3481 LOG_DISCOVERY,
3482 "2773 Start FCF fast failover due "
3483 "to CVL event: evt_tag:x%x\n",
3484 acqe_fcoe->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003485 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003486 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003487 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3488 LOG_DISCOVERY,
3489 "2774 Issue FCF rediscover "
3490 "mabilbox command failed, "
3491 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003492 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003493 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003494 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003495 /*
3496 * Last resort will be re-try on the
3497 * the current registered FCF entry.
3498 */
3499 lpfc_retry_pport_discovery(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003500 }
James Smart6669f9b2009-10-02 15:16:45 -04003501 }
3502 break;
James Smartda0436e2009-05-22 14:51:39 -04003503 default:
3504 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3505 "0288 Unknown FCoE event type 0x%x event tag "
3506 "0x%x\n", event_type, acqe_fcoe->event_tag);
3507 break;
3508 }
3509}
3510
3511/**
3512 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3513 * @phba: pointer to lpfc hba data structure.
3514 * @acqe_link: pointer to the async dcbx completion queue entry.
3515 *
3516 * This routine is to handle the SLI4 asynchronous dcbx event.
3517 **/
3518static void
3519lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3520 struct lpfc_acqe_dcbx *acqe_dcbx)
3521{
James Smart4d9ab992009-10-02 15:16:39 -04003522 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003523 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3524 "0290 The SLI4 DCBX asynchronous event is not "
3525 "handled yet\n");
3526}
3527
3528/**
3529 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3530 * @phba: pointer to lpfc hba data structure.
3531 *
3532 * This routine is invoked by the worker thread to process all the pending
3533 * SLI4 asynchronous events.
3534 **/
3535void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3536{
3537 struct lpfc_cq_event *cq_event;
3538
3539 /* First, declare the async event has been handled */
3540 spin_lock_irq(&phba->hbalock);
3541 phba->hba_flag &= ~ASYNC_EVENT;
3542 spin_unlock_irq(&phba->hbalock);
3543 /* Now, handle all the async events */
3544 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3545 /* Get the first event from the head of the event queue */
3546 spin_lock_irq(&phba->hbalock);
3547 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3548 cq_event, struct lpfc_cq_event, list);
3549 spin_unlock_irq(&phba->hbalock);
3550 /* Process the asynchronous event */
3551 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3552 case LPFC_TRAILER_CODE_LINK:
3553 lpfc_sli4_async_link_evt(phba,
3554 &cq_event->cqe.acqe_link);
3555 break;
3556 case LPFC_TRAILER_CODE_FCOE:
3557 lpfc_sli4_async_fcoe_evt(phba,
3558 &cq_event->cqe.acqe_fcoe);
3559 break;
3560 case LPFC_TRAILER_CODE_DCBX:
3561 lpfc_sli4_async_dcbx_evt(phba,
3562 &cq_event->cqe.acqe_dcbx);
3563 break;
3564 default:
3565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3566 "1804 Invalid asynchrous event code: "
3567 "x%x\n", bf_get(lpfc_trailer_code,
3568 &cq_event->cqe.mcqe_cmpl));
3569 break;
3570 }
3571 /* Free the completion event processed to the free pool */
3572 lpfc_sli4_cq_event_release(phba, cq_event);
3573 }
3574}
3575
3576/**
James Smartecfd03c2010-02-12 14:41:27 -05003577 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3578 * @phba: pointer to lpfc hba data structure.
3579 *
3580 * This routine is invoked by the worker thread to process FCF table
3581 * rediscovery pending completion event.
3582 **/
3583void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3584{
3585 int rc;
3586
3587 spin_lock_irq(&phba->hbalock);
3588 /* Clear FCF rediscovery timeout event */
3589 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3590 /* Clear driver fast failover FCF record flag */
3591 phba->fcf.failover_rec.flag = 0;
3592 /* Set state for FCF fast failover */
3593 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3594 spin_unlock_irq(&phba->hbalock);
3595
3596 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05003597 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3598 "2777 Start FCF table scan after FCF "
3599 "rediscovery quiescent period over\n");
3600 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05003601 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003602 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3603 "2747 Issue FCF scan read FCF mailbox "
3604 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05003605}
3606
3607/**
James Smartda0436e2009-05-22 14:51:39 -04003608 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3609 * @phba: pointer to lpfc hba data structure.
3610 * @dev_grp: The HBA PCI-Device group number.
3611 *
3612 * This routine is invoked to set up the per HBA PCI-Device group function
3613 * API jump table entries.
3614 *
3615 * Return: 0 if success, otherwise -ENODEV
3616 **/
3617int
3618lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003619{
3620 int rc;
3621
James Smartda0436e2009-05-22 14:51:39 -04003622 /* Set up lpfc PCI-device group */
3623 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003624
James Smartda0436e2009-05-22 14:51:39 -04003625 /* The LPFC_PCI_DEV_OC uses SLI4 */
3626 if (dev_grp == LPFC_PCI_DEV_OC)
3627 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003628
James Smartda0436e2009-05-22 14:51:39 -04003629 /* Set up device INIT API function jump table */
3630 rc = lpfc_init_api_table_setup(phba, dev_grp);
3631 if (rc)
3632 return -ENODEV;
3633 /* Set up SCSI API function jump table */
3634 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3635 if (rc)
3636 return -ENODEV;
3637 /* Set up SLI API function jump table */
3638 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3639 if (rc)
3640 return -ENODEV;
3641 /* Set up MBOX API function jump table */
3642 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3643 if (rc)
3644 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003645
James Smartda0436e2009-05-22 14:51:39 -04003646 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003647}
3648
3649/**
James Smart3621a712009-04-06 18:47:14 -04003650 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003651 * @phba: pointer to lpfc hba data structure.
3652 * @intr_mode: active interrupt mode adopted.
3653 *
3654 * This routine it invoked to log the currently used active interrupt mode
3655 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003656 **/
3657static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003658{
3659 switch (intr_mode) {
3660 case 0:
3661 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3662 "0470 Enable INTx interrupt mode.\n");
3663 break;
3664 case 1:
3665 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3666 "0481 Enabled MSI interrupt mode.\n");
3667 break;
3668 case 2:
3669 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3670 "0480 Enabled MSI-X interrupt mode.\n");
3671 break;
3672 default:
3673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3674 "0482 Illegal interrupt mode.\n");
3675 break;
3676 }
3677 return;
3678}
3679
James Smart5b75da22008-12-04 22:39:35 -05003680/**
James Smart3772a992009-05-22 14:50:54 -04003681 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003682 * @phba: pointer to lpfc hba data structure.
3683 *
James Smart3772a992009-05-22 14:50:54 -04003684 * This routine is invoked to enable the PCI device that is common to all
3685 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003686 *
3687 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003688 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003689 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003690 **/
James Smart3772a992009-05-22 14:50:54 -04003691static int
3692lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003693{
James Smart3772a992009-05-22 14:50:54 -04003694 struct pci_dev *pdev;
3695 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003696
James Smart3772a992009-05-22 14:50:54 -04003697 /* Obtain PCI device reference */
3698 if (!phba->pcidev)
3699 goto out_error;
3700 else
3701 pdev = phba->pcidev;
3702 /* Select PCI BARs */
3703 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3704 /* Enable PCI device */
3705 if (pci_enable_device_mem(pdev))
3706 goto out_error;
3707 /* Request PCI resource for the device */
3708 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3709 goto out_disable_device;
3710 /* Set up device as PCI master and save state for EEH */
3711 pci_set_master(pdev);
3712 pci_try_set_mwi(pdev);
3713 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003714
James Smart3772a992009-05-22 14:50:54 -04003715 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003716
James Smart3772a992009-05-22 14:50:54 -04003717out_disable_device:
3718 pci_disable_device(pdev);
3719out_error:
3720 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003721}
3722
3723/**
James Smart3772a992009-05-22 14:50:54 -04003724 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003725 * @phba: pointer to lpfc hba data structure.
3726 *
James Smart3772a992009-05-22 14:50:54 -04003727 * This routine is invoked to disable the PCI device that is common to all
3728 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003729 **/
3730static void
James Smart3772a992009-05-22 14:50:54 -04003731lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003732{
James Smart3772a992009-05-22 14:50:54 -04003733 struct pci_dev *pdev;
3734 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003735
James Smart3772a992009-05-22 14:50:54 -04003736 /* Obtain PCI device reference */
3737 if (!phba->pcidev)
3738 return;
3739 else
3740 pdev = phba->pcidev;
3741 /* Select PCI BARs */
3742 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3743 /* Release PCI resource and disable PCI device */
3744 pci_release_selected_regions(pdev, bars);
3745 pci_disable_device(pdev);
3746 /* Null out PCI private reference to driver */
3747 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003748
3749 return;
3750}
3751
3752/**
James Smart3772a992009-05-22 14:50:54 -04003753 * lpfc_reset_hba - Reset a hba
3754 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003755 *
James Smart3772a992009-05-22 14:50:54 -04003756 * This routine is invoked to reset a hba device. It brings the HBA
3757 * offline, performs a board restart, and then brings the board back
3758 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3759 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003760 **/
James Smart3772a992009-05-22 14:50:54 -04003761void
3762lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003763{
James Smart3772a992009-05-22 14:50:54 -04003764 /* If resets are disabled then set error state and return. */
3765 if (!phba->cfg_enable_hba_reset) {
3766 phba->link_state = LPFC_HBA_ERROR;
3767 return;
3768 }
3769 lpfc_offline_prep(phba);
3770 lpfc_offline(phba);
3771 lpfc_sli_brdrestart(phba);
3772 lpfc_online(phba);
3773 lpfc_unblock_mgmt_io(phba);
3774}
dea31012005-04-17 16:05:31 -05003775
James Smart3772a992009-05-22 14:50:54 -04003776/**
3777 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3778 * @phba: pointer to lpfc hba data structure.
3779 *
3780 * This routine is invoked to set up the driver internal resources specific to
3781 * support the SLI-3 HBA device it attached to.
3782 *
3783 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003784 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003785 * other values - error
3786 **/
3787static int
3788lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3789{
3790 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003791
James Smart3772a992009-05-22 14:50:54 -04003792 /*
3793 * Initialize timers used by driver
3794 */
dea31012005-04-17 16:05:31 -05003795
James Smart3772a992009-05-22 14:50:54 -04003796 /* Heartbeat timer */
3797 init_timer(&phba->hb_tmofunc);
3798 phba->hb_tmofunc.function = lpfc_hb_timeout;
3799 phba->hb_tmofunc.data = (unsigned long)phba;
3800
3801 psli = &phba->sli;
3802 /* MBOX heartbeat timer */
3803 init_timer(&psli->mbox_tmo);
3804 psli->mbox_tmo.function = lpfc_mbox_timeout;
3805 psli->mbox_tmo.data = (unsigned long) phba;
3806 /* FCP polling mode timer */
3807 init_timer(&phba->fcp_poll_timer);
3808 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3809 phba->fcp_poll_timer.data = (unsigned long) phba;
3810 /* Fabric block timer */
3811 init_timer(&phba->fabric_block_timer);
3812 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3813 phba->fabric_block_timer.data = (unsigned long) phba;
3814 /* EA polling mode timer */
3815 init_timer(&phba->eratt_poll);
3816 phba->eratt_poll.function = lpfc_poll_eratt;
3817 phba->eratt_poll.data = (unsigned long) phba;
3818
3819 /* Host attention work mask setup */
3820 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3821 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3822
3823 /* Get all the module params for configuring this host */
3824 lpfc_get_cfgparam(phba);
3825 /*
3826 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3827 * used to create the sg_dma_buf_pool must be dynamically calculated.
3828 * 2 segments are added since the IOCB needs a command and response bde.
3829 */
3830 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3831 sizeof(struct fcp_rsp) +
3832 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3833
3834 if (phba->cfg_enable_bg) {
3835 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3836 phba->cfg_sg_dma_buf_size +=
3837 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3838 }
3839
3840 /* Also reinitialize the host templates with new values. */
3841 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3842 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3843
3844 phba->max_vpi = LPFC_MAX_VPI;
3845 /* This will be set to correct value after config_port mbox */
3846 phba->max_vports = 0;
3847
3848 /*
3849 * Initialize the SLI Layer to run with lpfc HBAs.
3850 */
3851 lpfc_sli_setup(phba);
3852 lpfc_sli_queue_setup(phba);
3853
3854 /* Allocate device driver memory */
3855 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3856 return -ENOMEM;
3857
3858 return 0;
3859}
3860
3861/**
3862 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3863 * @phba: pointer to lpfc hba data structure.
3864 *
3865 * This routine is invoked to unset the driver internal resources set up
3866 * specific for supporting the SLI-3 HBA device it attached to.
3867 **/
3868static void
3869lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3870{
3871 /* Free device driver memory allocated */
3872 lpfc_mem_free_all(phba);
3873
3874 return;
3875}
3876
3877/**
James Smartda0436e2009-05-22 14:51:39 -04003878 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3879 * @phba: pointer to lpfc hba data structure.
3880 *
3881 * This routine is invoked to set up the driver internal resources specific to
3882 * support the SLI-4 HBA device it attached to.
3883 *
3884 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003885 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04003886 * other values - error
3887 **/
3888static int
3889lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3890{
3891 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05003892 LPFC_MBOXQ_t *mboxq;
3893 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3894 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3895 struct lpfc_mqe *mqe;
James Smart0c9ab6f2010-02-26 14:15:57 -05003896 int longs;
James Smartda0436e2009-05-22 14:51:39 -04003897
3898 /* Before proceed, wait for POST done and device ready */
3899 rc = lpfc_sli4_post_status_check(phba);
3900 if (rc)
3901 return -ENODEV;
3902
3903 /*
3904 * Initialize timers used by driver
3905 */
3906
3907 /* Heartbeat timer */
3908 init_timer(&phba->hb_tmofunc);
3909 phba->hb_tmofunc.function = lpfc_hb_timeout;
3910 phba->hb_tmofunc.data = (unsigned long)phba;
3911
3912 psli = &phba->sli;
3913 /* MBOX heartbeat timer */
3914 init_timer(&psli->mbox_tmo);
3915 psli->mbox_tmo.function = lpfc_mbox_timeout;
3916 psli->mbox_tmo.data = (unsigned long) phba;
3917 /* Fabric block timer */
3918 init_timer(&phba->fabric_block_timer);
3919 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3920 phba->fabric_block_timer.data = (unsigned long) phba;
3921 /* EA polling mode timer */
3922 init_timer(&phba->eratt_poll);
3923 phba->eratt_poll.function = lpfc_poll_eratt;
3924 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05003925 /* FCF rediscover timer */
3926 init_timer(&phba->fcf.redisc_wait);
3927 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3928 phba->fcf.redisc_wait.data = (unsigned long)phba;
3929
James Smartda0436e2009-05-22 14:51:39 -04003930 /*
3931 * We need to do a READ_CONFIG mailbox command here before
3932 * calling lpfc_get_cfgparam. For VFs this will report the
3933 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3934 * All of the resources allocated
3935 * for this Port are tied to these values.
3936 */
3937 /* Get all the module params for configuring this host */
3938 lpfc_get_cfgparam(phba);
3939 phba->max_vpi = LPFC_MAX_VPI;
3940 /* This will be set to correct value after the read_config mbox */
3941 phba->max_vports = 0;
3942
3943 /* Program the default value of vlan_id and fc_map */
3944 phba->valid_vlan = 0;
3945 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3946 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3947 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3948
3949 /*
3950 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3951 * used to create the sg_dma_buf_pool must be dynamically calculated.
3952 * 2 segments are added since the IOCB needs a command and response bde.
3953 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05003954 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04003955 */
James Smart28baac72010-02-12 14:42:03 -05003956 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
3957 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
3958 /* Feature Level 1 hardware is limited to 2 pages */
3959 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
3960 LPFC_SLI_INTF_FEATURELEVEL1_1))
3961 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04003962 else
James Smart28baac72010-02-12 14:42:03 -05003963 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
3964 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
3965 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
3966 dma_buf_size = dma_buf_size << 1)
3967 ;
3968 if (dma_buf_size == max_buf_size)
3969 phba->cfg_sg_seg_cnt = (dma_buf_size -
3970 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
3971 (2 * sizeof(struct sli4_sge))) /
3972 sizeof(struct sli4_sge);
3973 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04003974
3975 /* Initialize buffer queue management fields */
3976 hbq_count = lpfc_sli_hbq_count();
3977 for (i = 0; i < hbq_count; ++i)
3978 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3979 INIT_LIST_HEAD(&phba->rb_pend_list);
3980 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3981 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3982
3983 /*
3984 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3985 */
3986 /* Initialize the Abort scsi buffer list used by driver */
3987 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3988 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3989 /* This abort list used by worker thread */
3990 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3991
3992 /*
3993 * Initialize dirver internal slow-path work queues
3994 */
3995
3996 /* Driver internel slow-path CQ Event pool */
3997 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3998 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04003999 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004000 /* Asynchronous event CQ Event work queue list */
4001 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4002 /* Fast-path XRI aborted CQ Event work queue list */
4003 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4004 /* Slow-path XRI aborted CQ Event work queue list */
4005 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4006 /* Receive queue CQ Event work queue list */
4007 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4008
4009 /* Initialize the driver internal SLI layer lists. */
4010 lpfc_sli_setup(phba);
4011 lpfc_sli_queue_setup(phba);
4012
4013 /* Allocate device driver memory */
4014 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4015 if (rc)
4016 return -ENOMEM;
4017
4018 /* Create the bootstrap mailbox command */
4019 rc = lpfc_create_bootstrap_mbox(phba);
4020 if (unlikely(rc))
4021 goto out_free_mem;
4022
4023 /* Set up the host's endian order with the device. */
4024 rc = lpfc_setup_endian_order(phba);
4025 if (unlikely(rc))
4026 goto out_free_bsmbx;
4027
James Smart6669f9b2009-10-02 15:16:45 -04004028 rc = lpfc_sli4_fw_cfg_check(phba);
4029 if (unlikely(rc))
4030 goto out_free_bsmbx;
4031
James Smartda0436e2009-05-22 14:51:39 -04004032 /* Set up the hba's configuration parameters. */
4033 rc = lpfc_sli4_read_config(phba);
4034 if (unlikely(rc))
4035 goto out_free_bsmbx;
4036
4037 /* Perform a function reset */
4038 rc = lpfc_pci_function_reset(phba);
4039 if (unlikely(rc))
4040 goto out_free_bsmbx;
4041
James Smartcb5172e2010-03-15 11:25:07 -04004042 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4043 GFP_KERNEL);
4044 if (!mboxq) {
4045 rc = -ENOMEM;
4046 goto out_free_bsmbx;
4047 }
4048
4049 /* Get the Supported Pages. It is always available. */
4050 lpfc_supported_pages(mboxq);
4051 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4052 if (unlikely(rc)) {
4053 rc = -EIO;
4054 mempool_free(mboxq, phba->mbox_mem_pool);
4055 goto out_free_bsmbx;
4056 }
4057
4058 mqe = &mboxq->u.mqe;
4059 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4060 LPFC_MAX_SUPPORTED_PAGES);
4061 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4062 switch (pn_page[i]) {
4063 case LPFC_SLI4_PARAMETERS:
4064 phba->sli4_hba.pc_sli4_params.supported = 1;
4065 break;
4066 default:
4067 break;
4068 }
4069 }
4070
4071 /* Read the port's SLI4 Parameters capabilities if supported. */
4072 if (phba->sli4_hba.pc_sli4_params.supported)
4073 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4074 mempool_free(mboxq, phba->mbox_mem_pool);
4075 if (rc) {
4076 rc = -EIO;
4077 goto out_free_bsmbx;
4078 }
James Smartda0436e2009-05-22 14:51:39 -04004079 /* Create all the SLI4 queues */
4080 rc = lpfc_sli4_queue_create(phba);
4081 if (rc)
4082 goto out_free_bsmbx;
4083
4084 /* Create driver internal CQE event pool */
4085 rc = lpfc_sli4_cq_event_pool_create(phba);
4086 if (rc)
4087 goto out_destroy_queue;
4088
4089 /* Initialize and populate the iocb list per host */
4090 rc = lpfc_init_sgl_list(phba);
4091 if (rc) {
4092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4093 "1400 Failed to initialize sgl list.\n");
4094 goto out_destroy_cq_event_pool;
4095 }
4096 rc = lpfc_init_active_sgl_array(phba);
4097 if (rc) {
4098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4099 "1430 Failed to initialize sgl list.\n");
4100 goto out_free_sgl_list;
4101 }
4102
4103 rc = lpfc_sli4_init_rpi_hdrs(phba);
4104 if (rc) {
4105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4106 "1432 Failed to initialize rpi headers.\n");
4107 goto out_free_active_sgl;
4108 }
4109
James Smart0c9ab6f2010-02-26 14:15:57 -05004110 /* Allocate eligible FCF bmask memory for FCF round robin failover */
4111 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4112 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4113 GFP_KERNEL);
4114 if (!phba->fcf.fcf_rr_bmask) {
4115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4116 "2759 Failed allocate memory for FCF round "
4117 "robin failover bmask\n");
4118 goto out_remove_rpi_hdrs;
4119 }
4120
James Smartda0436e2009-05-22 14:51:39 -04004121 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4122 phba->cfg_fcp_eq_count), GFP_KERNEL);
4123 if (!phba->sli4_hba.fcp_eq_hdl) {
4124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4125 "2572 Failed allocate memory for fast-path "
4126 "per-EQ handle array\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004127 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04004128 }
4129
4130 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4131 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4132 if (!phba->sli4_hba.msix_entries) {
4133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4134 "2573 Failed allocate memory for msi-x "
4135 "interrupt vector entries\n");
4136 goto out_free_fcp_eq_hdl;
4137 }
4138
4139 return rc;
4140
4141out_free_fcp_eq_hdl:
4142 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004143out_free_fcf_rr_bmask:
4144 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004145out_remove_rpi_hdrs:
4146 lpfc_sli4_remove_rpi_hdrs(phba);
4147out_free_active_sgl:
4148 lpfc_free_active_sgl(phba);
4149out_free_sgl_list:
4150 lpfc_free_sgl_list(phba);
4151out_destroy_cq_event_pool:
4152 lpfc_sli4_cq_event_pool_destroy(phba);
4153out_destroy_queue:
4154 lpfc_sli4_queue_destroy(phba);
4155out_free_bsmbx:
4156 lpfc_destroy_bootstrap_mbox(phba);
4157out_free_mem:
4158 lpfc_mem_free(phba);
4159 return rc;
4160}
4161
4162/**
4163 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4164 * @phba: pointer to lpfc hba data structure.
4165 *
4166 * This routine is invoked to unset the driver internal resources set up
4167 * specific for supporting the SLI-4 HBA device it attached to.
4168 **/
4169static void
4170lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4171{
4172 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4173
4174 /* unregister default FCFI from the HBA */
4175 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4176
4177 /* Free the default FCR table */
4178 lpfc_sli_remove_dflt_fcf(phba);
4179
4180 /* Free memory allocated for msi-x interrupt vector entries */
4181 kfree(phba->sli4_hba.msix_entries);
4182
4183 /* Free memory allocated for fast-path work queue handles */
4184 kfree(phba->sli4_hba.fcp_eq_hdl);
4185
4186 /* Free the allocated rpi headers. */
4187 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004188 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004189
James Smart0c9ab6f2010-02-26 14:15:57 -05004190 /* Free eligible FCF index bmask */
4191 kfree(phba->fcf.fcf_rr_bmask);
4192
James Smartda0436e2009-05-22 14:51:39 -04004193 /* Free the ELS sgl list */
4194 lpfc_free_active_sgl(phba);
4195 lpfc_free_sgl_list(phba);
4196
4197 /* Free the SCSI sgl management array */
4198 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4199
4200 /* Free the SLI4 queues */
4201 lpfc_sli4_queue_destroy(phba);
4202
4203 /* Free the completion queue EQ event pool */
4204 lpfc_sli4_cq_event_release_all(phba);
4205 lpfc_sli4_cq_event_pool_destroy(phba);
4206
4207 /* Reset SLI4 HBA FCoE function */
4208 lpfc_pci_function_reset(phba);
4209
4210 /* Free the bsmbx region. */
4211 lpfc_destroy_bootstrap_mbox(phba);
4212
4213 /* Free the SLI Layer memory with SLI4 HBAs */
4214 lpfc_mem_free_all(phba);
4215
4216 /* Free the current connect table */
4217 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004218 &phba->fcf_conn_rec_list, list) {
4219 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004220 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004221 }
James Smartda0436e2009-05-22 14:51:39 -04004222
4223 return;
4224}
4225
4226/**
James Smart3772a992009-05-22 14:50:54 -04004227 * lpfc_init_api_table_setup - Set up init api fucntion jump table
4228 * @phba: The hba struct for which this call is being executed.
4229 * @dev_grp: The HBA PCI-Device group number.
4230 *
4231 * This routine sets up the device INIT interface API function jump table
4232 * in @phba struct.
4233 *
4234 * Returns: 0 - success, -ENODEV - failure.
4235 **/
4236int
4237lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4238{
James Smart84d1b002010-02-12 14:42:33 -05004239 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4240 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart3772a992009-05-22 14:50:54 -04004241 switch (dev_grp) {
4242 case LPFC_PCI_DEV_LP:
4243 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4244 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4245 phba->lpfc_stop_port = lpfc_stop_port_s3;
4246 break;
James Smartda0436e2009-05-22 14:51:39 -04004247 case LPFC_PCI_DEV_OC:
4248 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4249 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4250 phba->lpfc_stop_port = lpfc_stop_port_s4;
4251 break;
James Smart3772a992009-05-22 14:50:54 -04004252 default:
4253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4254 "1431 Invalid HBA PCI-device group: 0x%x\n",
4255 dev_grp);
4256 return -ENODEV;
4257 break;
4258 }
4259 return 0;
4260}
4261
4262/**
4263 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4264 * @phba: pointer to lpfc hba data structure.
4265 *
4266 * This routine is invoked to set up the driver internal resources before the
4267 * device specific resource setup to support the HBA device it attached to.
4268 *
4269 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004270 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004271 * other values - error
4272 **/
4273static int
4274lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4275{
4276 /*
4277 * Driver resources common to all SLI revisions
4278 */
James Smartea2151b2008-09-07 11:52:10 -04004279 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004280 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004281
James Smarte47c9092008-02-08 18:49:26 -05004282 /* Initialize ndlp management spinlock */
4283 spin_lock_init(&phba->ndlp_lock);
4284
James Smart2e0fef82007-06-17 19:56:36 -05004285 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004286 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004287 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004288
James Smart3772a992009-05-22 14:50:54 -04004289 /* Initialize the wait queue head for the kernel thread */
4290 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004291
James Smart3772a992009-05-22 14:50:54 -04004292 /* Initialize the scsi buffer list used by driver for scsi IO */
4293 spin_lock_init(&phba->scsi_buf_list_lock);
4294 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004295
James Smart3772a992009-05-22 14:50:54 -04004296 /* Initialize the fabric iocb list */
4297 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004298
James Smart3772a992009-05-22 14:50:54 -04004299 /* Initialize list to save ELS buffers */
4300 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004301
James Smart3772a992009-05-22 14:50:54 -04004302 /* Initialize FCF connection rec list */
4303 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004304
James Smart3772a992009-05-22 14:50:54 -04004305 return 0;
4306}
dea31012005-04-17 16:05:31 -05004307
James Smart3772a992009-05-22 14:50:54 -04004308/**
4309 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4310 * @phba: pointer to lpfc hba data structure.
4311 *
4312 * This routine is invoked to set up the driver internal resources after the
4313 * device specific resource setup to support the HBA device it attached to.
4314 *
4315 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004316 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004317 * other values - error
4318 **/
4319static int
4320lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4321{
4322 int error;
4323
4324 /* Startup the kernel thread for this host adapter. */
4325 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4326 "lpfc_worker_%d", phba->brd_no);
4327 if (IS_ERR(phba->worker_thread)) {
4328 error = PTR_ERR(phba->worker_thread);
4329 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004330 }
4331
James Smart3772a992009-05-22 14:50:54 -04004332 return 0;
4333}
4334
4335/**
4336 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4337 * @phba: pointer to lpfc hba data structure.
4338 *
4339 * This routine is invoked to unset the driver internal resources set up after
4340 * the device specific resource setup for supporting the HBA device it
4341 * attached to.
4342 **/
4343static void
4344lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4345{
4346 /* Stop kernel worker thread */
4347 kthread_stop(phba->worker_thread);
4348}
4349
4350/**
4351 * lpfc_free_iocb_list - Free iocb list.
4352 * @phba: pointer to lpfc hba data structure.
4353 *
4354 * This routine is invoked to free the driver's IOCB list and memory.
4355 **/
4356static void
4357lpfc_free_iocb_list(struct lpfc_hba *phba)
4358{
4359 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4360
4361 spin_lock_irq(&phba->hbalock);
4362 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4363 &phba->lpfc_iocb_list, list) {
4364 list_del(&iocbq_entry->list);
4365 kfree(iocbq_entry);
4366 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004367 }
James Smart3772a992009-05-22 14:50:54 -04004368 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004369
James Smart3772a992009-05-22 14:50:54 -04004370 return;
4371}
dea31012005-04-17 16:05:31 -05004372
James Smart3772a992009-05-22 14:50:54 -04004373/**
4374 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4375 * @phba: pointer to lpfc hba data structure.
4376 *
4377 * This routine is invoked to allocate and initizlize the driver's IOCB
4378 * list and set up the IOCB tag array accordingly.
4379 *
4380 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004381 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004382 * other values - error
4383 **/
4384static int
4385lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4386{
4387 struct lpfc_iocbq *iocbq_entry = NULL;
4388 uint16_t iotag;
4389 int i;
dea31012005-04-17 16:05:31 -05004390
4391 /* Initialize and populate the iocb list per host. */
4392 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004393 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004394 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004395 if (iocbq_entry == NULL) {
4396 printk(KERN_ERR "%s: only allocated %d iocbs of "
4397 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004398 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004399 goto out_free_iocbq;
4400 }
4401
James Bottomley604a3e32005-10-29 10:28:33 -05004402 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4403 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004404 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004405 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004406 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004407 goto out_free_iocbq;
4408 }
James Smart3772a992009-05-22 14:50:54 -04004409 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004410
4411 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004412 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4413 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004414 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004415 }
4416
James Smart3772a992009-05-22 14:50:54 -04004417 return 0;
4418
4419out_free_iocbq:
4420 lpfc_free_iocb_list(phba);
4421
4422 return -ENOMEM;
4423}
4424
4425/**
James Smartda0436e2009-05-22 14:51:39 -04004426 * lpfc_free_sgl_list - Free sgl list.
4427 * @phba: pointer to lpfc hba data structure.
4428 *
4429 * This routine is invoked to free the driver's sgl list and memory.
4430 **/
4431static void
4432lpfc_free_sgl_list(struct lpfc_hba *phba)
4433{
4434 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4435 LIST_HEAD(sglq_list);
4436 int rc = 0;
4437
4438 spin_lock_irq(&phba->hbalock);
4439 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4440 spin_unlock_irq(&phba->hbalock);
4441
4442 list_for_each_entry_safe(sglq_entry, sglq_next,
4443 &sglq_list, list) {
4444 list_del(&sglq_entry->list);
4445 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4446 kfree(sglq_entry);
4447 phba->sli4_hba.total_sglq_bufs--;
4448 }
4449 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4450 if (rc) {
4451 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004452 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004453 }
4454 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4455}
4456
4457/**
4458 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4459 * @phba: pointer to lpfc hba data structure.
4460 *
4461 * This routine is invoked to allocate the driver's active sgl memory.
4462 * This array will hold the sglq_entry's for active IOs.
4463 **/
4464static int
4465lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4466{
4467 int size;
4468 size = sizeof(struct lpfc_sglq *);
4469 size *= phba->sli4_hba.max_cfg_param.max_xri;
4470
4471 phba->sli4_hba.lpfc_sglq_active_list =
4472 kzalloc(size, GFP_KERNEL);
4473 if (!phba->sli4_hba.lpfc_sglq_active_list)
4474 return -ENOMEM;
4475 return 0;
4476}
4477
4478/**
4479 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4480 * @phba: pointer to lpfc hba data structure.
4481 *
4482 * This routine is invoked to walk through the array of active sglq entries
4483 * and free all of the resources.
4484 * This is just a place holder for now.
4485 **/
4486static void
4487lpfc_free_active_sgl(struct lpfc_hba *phba)
4488{
4489 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4490}
4491
4492/**
4493 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4494 * @phba: pointer to lpfc hba data structure.
4495 *
4496 * This routine is invoked to allocate and initizlize the driver's sgl
4497 * list and set up the sgl xritag tag array accordingly.
4498 *
4499 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004500 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004501 * other values - error
4502 **/
4503static int
4504lpfc_init_sgl_list(struct lpfc_hba *phba)
4505{
4506 struct lpfc_sglq *sglq_entry = NULL;
4507 int i;
4508 int els_xri_cnt;
4509
4510 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4511 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4512 "2400 lpfc_init_sgl_list els %d.\n",
4513 els_xri_cnt);
4514 /* Initialize and populate the sglq list per host/VF. */
4515 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4516 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4517
4518 /* Sanity check on XRI management */
4519 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4520 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4521 "2562 No room left for SCSI XRI allocation: "
4522 "max_xri=%d, els_xri=%d\n",
4523 phba->sli4_hba.max_cfg_param.max_xri,
4524 els_xri_cnt);
4525 return -ENOMEM;
4526 }
4527
4528 /* Allocate memory for the ELS XRI management array */
4529 phba->sli4_hba.lpfc_els_sgl_array =
4530 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4531 GFP_KERNEL);
4532
4533 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4535 "2401 Failed to allocate memory for ELS "
4536 "XRI management array of size %d.\n",
4537 els_xri_cnt);
4538 return -ENOMEM;
4539 }
4540
4541 /* Keep the SCSI XRI into the XRI management array */
4542 phba->sli4_hba.scsi_xri_max =
4543 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4544 phba->sli4_hba.scsi_xri_cnt = 0;
4545
4546 phba->sli4_hba.lpfc_scsi_psb_array =
4547 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4548 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4549
4550 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4551 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4552 "2563 Failed to allocate memory for SCSI "
4553 "XRI management array of size %d.\n",
4554 phba->sli4_hba.scsi_xri_max);
4555 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4556 return -ENOMEM;
4557 }
4558
4559 for (i = 0; i < els_xri_cnt; i++) {
4560 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4561 if (sglq_entry == NULL) {
4562 printk(KERN_ERR "%s: only allocated %d sgls of "
4563 "expected %d count. Unloading driver.\n",
4564 __func__, i, els_xri_cnt);
4565 goto out_free_mem;
4566 }
4567
4568 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4569 if (sglq_entry->sli4_xritag == NO_XRI) {
4570 kfree(sglq_entry);
4571 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4572 "Unloading driver.\n", __func__);
4573 goto out_free_mem;
4574 }
4575 sglq_entry->buff_type = GEN_BUFF_TYPE;
4576 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4577 if (sglq_entry->virt == NULL) {
4578 kfree(sglq_entry);
4579 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4580 "Unloading driver.\n", __func__);
4581 goto out_free_mem;
4582 }
4583 sglq_entry->sgl = sglq_entry->virt;
4584 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4585
4586 /* The list order is used by later block SGL registraton */
4587 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05004588 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04004589 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4590 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4591 phba->sli4_hba.total_sglq_bufs++;
4592 spin_unlock_irq(&phba->hbalock);
4593 }
4594 return 0;
4595
4596out_free_mem:
4597 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4598 lpfc_free_sgl_list(phba);
4599 return -ENOMEM;
4600}
4601
4602/**
4603 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4604 * @phba: pointer to lpfc hba data structure.
4605 *
4606 * This routine is invoked to post rpi header templates to the
4607 * HBA consistent with the SLI-4 interface spec. This routine
4608 * posts a PAGE_SIZE memory region to the port to hold up to
4609 * PAGE_SIZE modulo 64 rpi context headers.
4610 * No locks are held here because this is an initialization routine
4611 * called only from probe or lpfc_online when interrupts are not
4612 * enabled and the driver is reinitializing the device.
4613 *
4614 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004615 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004616 * ENOMEM - No availble memory
4617 * EIO - The mailbox failed to complete successfully.
4618 **/
4619int
4620lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4621{
4622 int rc = 0;
4623 int longs;
4624 uint16_t rpi_count;
4625 struct lpfc_rpi_hdr *rpi_hdr;
4626
4627 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4628
4629 /*
4630 * Provision an rpi bitmask range for discovery. The total count
4631 * is the difference between max and base + 1.
4632 */
4633 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4634 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4635
4636 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4637 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4638 GFP_KERNEL);
4639 if (!phba->sli4_hba.rpi_bmask)
4640 return -ENOMEM;
4641
4642 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4643 if (!rpi_hdr) {
4644 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4645 "0391 Error during rpi post operation\n");
4646 lpfc_sli4_remove_rpis(phba);
4647 rc = -ENODEV;
4648 }
4649
4650 return rc;
4651}
4652
4653/**
4654 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4655 * @phba: pointer to lpfc hba data structure.
4656 *
4657 * This routine is invoked to allocate a single 4KB memory region to
4658 * support rpis and stores them in the phba. This single region
4659 * provides support for up to 64 rpis. The region is used globally
4660 * by the device.
4661 *
4662 * Returns:
4663 * A valid rpi hdr on success.
4664 * A NULL pointer on any failure.
4665 **/
4666struct lpfc_rpi_hdr *
4667lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4668{
4669 uint16_t rpi_limit, curr_rpi_range;
4670 struct lpfc_dmabuf *dmabuf;
4671 struct lpfc_rpi_hdr *rpi_hdr;
4672
4673 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4674 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4675
4676 spin_lock_irq(&phba->hbalock);
4677 curr_rpi_range = phba->sli4_hba.next_rpi;
4678 spin_unlock_irq(&phba->hbalock);
4679
4680 /*
4681 * The port has a limited number of rpis. The increment here
4682 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4683 * and to allow the full max_rpi range per port.
4684 */
4685 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4686 return NULL;
4687
4688 /*
4689 * First allocate the protocol header region for the port. The
4690 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4691 */
4692 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4693 if (!dmabuf)
4694 return NULL;
4695
4696 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4697 LPFC_HDR_TEMPLATE_SIZE,
4698 &dmabuf->phys,
4699 GFP_KERNEL);
4700 if (!dmabuf->virt) {
4701 rpi_hdr = NULL;
4702 goto err_free_dmabuf;
4703 }
4704
4705 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4706 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4707 rpi_hdr = NULL;
4708 goto err_free_coherent;
4709 }
4710
4711 /* Save the rpi header data for cleanup later. */
4712 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4713 if (!rpi_hdr)
4714 goto err_free_coherent;
4715
4716 rpi_hdr->dmabuf = dmabuf;
4717 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4718 rpi_hdr->page_count = 1;
4719 spin_lock_irq(&phba->hbalock);
4720 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4721 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4722
4723 /*
4724 * The next_rpi stores the next module-64 rpi value to post
4725 * in any subsequent rpi memory region postings.
4726 */
4727 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4728 spin_unlock_irq(&phba->hbalock);
4729 return rpi_hdr;
4730
4731 err_free_coherent:
4732 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4733 dmabuf->virt, dmabuf->phys);
4734 err_free_dmabuf:
4735 kfree(dmabuf);
4736 return NULL;
4737}
4738
4739/**
4740 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4741 * @phba: pointer to lpfc hba data structure.
4742 *
4743 * This routine is invoked to remove all memory resources allocated
4744 * to support rpis. This routine presumes the caller has released all
4745 * rpis consumed by fabric or port logins and is prepared to have
4746 * the header pages removed.
4747 **/
4748void
4749lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4750{
4751 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4752
4753 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4754 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4755 list_del(&rpi_hdr->list);
4756 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4757 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4758 kfree(rpi_hdr->dmabuf);
4759 kfree(rpi_hdr);
4760 }
4761
4762 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4763 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4764}
4765
4766/**
James Smart3772a992009-05-22 14:50:54 -04004767 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4768 * @pdev: pointer to pci device data structure.
4769 *
4770 * This routine is invoked to allocate the driver hba data structure for an
4771 * HBA device. If the allocation is successful, the phba reference to the
4772 * PCI device data structure is set.
4773 *
4774 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004775 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004776 * NULL - error
4777 **/
4778static struct lpfc_hba *
4779lpfc_hba_alloc(struct pci_dev *pdev)
4780{
4781 struct lpfc_hba *phba;
4782
4783 /* Allocate memory for HBA structure */
4784 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4785 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004786 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004787 return NULL;
4788 }
4789
4790 /* Set reference to PCI device in HBA structure */
4791 phba->pcidev = pdev;
4792
4793 /* Assign an unused board number */
4794 phba->brd_no = lpfc_get_instance();
4795 if (phba->brd_no < 0) {
4796 kfree(phba);
4797 return NULL;
4798 }
4799
James Smart4fede782010-01-26 23:08:55 -05004800 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04004801 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4802
James Smart3772a992009-05-22 14:50:54 -04004803 return phba;
4804}
4805
4806/**
4807 * lpfc_hba_free - Free driver hba data structure with a device.
4808 * @phba: pointer to lpfc hba data structure.
4809 *
4810 * This routine is invoked to free the driver hba data structure with an
4811 * HBA device.
4812 **/
4813static void
4814lpfc_hba_free(struct lpfc_hba *phba)
4815{
4816 /* Release the driver assigned board number */
4817 idr_remove(&lpfc_hba_index, phba->brd_no);
4818
4819 kfree(phba);
4820 return;
4821}
4822
4823/**
4824 * lpfc_create_shost - Create hba physical port with associated scsi host.
4825 * @phba: pointer to lpfc hba data structure.
4826 *
4827 * This routine is invoked to create HBA physical port and associate a SCSI
4828 * host with it.
4829 *
4830 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004831 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004832 * other values - error
4833 **/
4834static int
4835lpfc_create_shost(struct lpfc_hba *phba)
4836{
4837 struct lpfc_vport *vport;
4838 struct Scsi_Host *shost;
4839
4840 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004841 phba->fc_edtov = FF_DEF_EDTOV;
4842 phba->fc_ratov = FF_DEF_RATOV;
4843 phba->fc_altov = FF_DEF_ALTOV;
4844 phba->fc_arbtov = FF_DEF_ARBTOV;
4845
James Smart3de2a652007-08-02 11:09:59 -04004846 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004847 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004848 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004849
4850 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004851 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004852 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004853 /* Put reference to SCSI host to driver's device private data */
4854 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004855
James Smart3772a992009-05-22 14:50:54 -04004856 return 0;
4857}
dea31012005-04-17 16:05:31 -05004858
James Smart3772a992009-05-22 14:50:54 -04004859/**
4860 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4861 * @phba: pointer to lpfc hba data structure.
4862 *
4863 * This routine is invoked to destroy HBA physical port and the associated
4864 * SCSI host.
4865 **/
4866static void
4867lpfc_destroy_shost(struct lpfc_hba *phba)
4868{
4869 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004870
James Smart3772a992009-05-22 14:50:54 -04004871 /* Destroy physical port that associated with the SCSI host */
4872 destroy_port(vport);
4873
4874 return;
4875}
4876
4877/**
4878 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4879 * @phba: pointer to lpfc hba data structure.
4880 * @shost: the shost to be used to detect Block guard settings.
4881 *
4882 * This routine sets up the local Block guard protocol settings for @shost.
4883 * This routine also allocates memory for debugging bg buffers.
4884 **/
4885static void
4886lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4887{
4888 int pagecnt = 10;
4889 if (lpfc_prot_mask && lpfc_prot_guard) {
4890 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4891 "1478 Registering BlockGuard with the "
4892 "SCSI layer\n");
4893 scsi_host_set_prot(shost, lpfc_prot_mask);
4894 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004895 }
James Smart81301a92008-12-04 22:39:46 -05004896 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004897 while (pagecnt) {
4898 spin_lock_init(&_dump_buf_lock);
4899 _dump_buf_data =
4900 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4901 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004902 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4903 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004904 "_dump_buf_data at 0x%p\n",
4905 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004906 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004907 memset(_dump_buf_data, 0,
4908 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004909 break;
James Smart3772a992009-05-22 14:50:54 -04004910 } else
James Smart81301a92008-12-04 22:39:46 -05004911 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004912 }
James Smart81301a92008-12-04 22:39:46 -05004913 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004914 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4915 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004916 "memory for hexdump\n");
4917 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004918 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4919 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004920 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004921 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004922 while (pagecnt) {
4923 _dump_buf_dif =
4924 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4925 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004926 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4927 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004928 "_dump_buf_dif at 0x%p\n",
4929 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004930 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004931 memset(_dump_buf_dif, 0,
4932 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004933 break;
James Smart3772a992009-05-22 14:50:54 -04004934 } else
James Smart81301a92008-12-04 22:39:46 -05004935 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004936 }
James Smart81301a92008-12-04 22:39:46 -05004937 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004938 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4939 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004940 "memory for hexdump\n");
4941 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004942 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4943 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004944 _dump_buf_dif);
4945}
4946
4947/**
4948 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4949 * @phba: pointer to lpfc hba data structure.
4950 *
4951 * This routine is invoked to perform all the necessary post initialization
4952 * setup for the device.
4953 **/
4954static void
4955lpfc_post_init_setup(struct lpfc_hba *phba)
4956{
4957 struct Scsi_Host *shost;
4958 struct lpfc_adapter_event_header adapter_event;
4959
4960 /* Get the default values for Model Name and Description */
4961 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4962
4963 /*
4964 * hba setup may have changed the hba_queue_depth so we need to
4965 * adjust the value of can_queue.
4966 */
4967 shost = pci_get_drvdata(phba->pcidev);
4968 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4969 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4970 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004971
4972 lpfc_host_attrib_init(shost);
4973
James Smart2e0fef82007-06-17 19:56:36 -05004974 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4975 spin_lock_irq(shost->host_lock);
4976 lpfc_poll_start_timer(phba);
4977 spin_unlock_irq(shost->host_lock);
4978 }
James Smart8f6d98d2006-08-01 07:34:00 -04004979
James Smart93996272008-08-24 21:50:30 -04004980 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4981 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004982 /* Send board arrival event to upper layer */
4983 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4984 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4985 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004986 sizeof(adapter_event),
4987 (char *) &adapter_event,
4988 LPFC_NL_VENDOR_ID);
4989 return;
4990}
4991
4992/**
4993 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4994 * @phba: pointer to lpfc hba data structure.
4995 *
4996 * This routine is invoked to set up the PCI device memory space for device
4997 * with SLI-3 interface spec.
4998 *
4999 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005000 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005001 * other values - error
5002 **/
5003static int
5004lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5005{
5006 struct pci_dev *pdev;
5007 unsigned long bar0map_len, bar2map_len;
5008 int i, hbq_count;
5009 void *ptr;
5010 int error = -ENODEV;
5011
5012 /* Obtain PCI device reference */
5013 if (!phba->pcidev)
5014 return error;
5015 else
5016 pdev = phba->pcidev;
5017
5018 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005019 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5020 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5021 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5022 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005023 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005024 }
5025 }
James Smart3772a992009-05-22 14:50:54 -04005026
5027 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5028 * required by each mapping.
5029 */
5030 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5031 bar0map_len = pci_resource_len(pdev, 0);
5032
5033 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5034 bar2map_len = pci_resource_len(pdev, 2);
5035
5036 /* Map HBA SLIM to a kernel virtual address. */
5037 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5038 if (!phba->slim_memmap_p) {
5039 dev_printk(KERN_ERR, &pdev->dev,
5040 "ioremap failed for SLIM memory.\n");
5041 goto out;
5042 }
5043
5044 /* Map HBA Control Registers to a kernel virtual address. */
5045 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5046 if (!phba->ctrl_regs_memmap_p) {
5047 dev_printk(KERN_ERR, &pdev->dev,
5048 "ioremap failed for HBA control registers.\n");
5049 goto out_iounmap_slim;
5050 }
5051
5052 /* Allocate memory for SLI-2 structures */
5053 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5054 SLI2_SLIM_SIZE,
5055 &phba->slim2p.phys,
5056 GFP_KERNEL);
5057 if (!phba->slim2p.virt)
5058 goto out_iounmap;
5059
5060 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5061 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005062 phba->mbox_ext = (phba->slim2p.virt +
5063 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005064 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5065 phba->IOCBs = (phba->slim2p.virt +
5066 offsetof(struct lpfc_sli2_slim, IOCBs));
5067
5068 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5069 lpfc_sli_hbq_size(),
5070 &phba->hbqslimp.phys,
5071 GFP_KERNEL);
5072 if (!phba->hbqslimp.virt)
5073 goto out_free_slim;
5074
5075 hbq_count = lpfc_sli_hbq_count();
5076 ptr = phba->hbqslimp.virt;
5077 for (i = 0; i < hbq_count; ++i) {
5078 phba->hbqs[i].hbq_virt = ptr;
5079 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5080 ptr += (lpfc_hbq_defs[i]->entry_count *
5081 sizeof(struct lpfc_hbq_entry));
5082 }
5083 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5084 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5085
5086 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5087
5088 INIT_LIST_HEAD(&phba->rb_pend_list);
5089
5090 phba->MBslimaddr = phba->slim_memmap_p;
5091 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5092 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5093 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5094 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005095
dea31012005-04-17 16:05:31 -05005096 return 0;
5097
dea31012005-04-17 16:05:31 -05005098out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005099 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5100 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005101out_iounmap:
5102 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005103out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005104 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005105out:
5106 return error;
5107}
5108
James Smarte59058c2008-08-24 21:49:00 -04005109/**
James Smart3772a992009-05-22 14:50:54 -04005110 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5111 * @phba: pointer to lpfc hba data structure.
5112 *
5113 * This routine is invoked to unset the PCI device memory space for device
5114 * with SLI-3 interface spec.
5115 **/
5116static void
5117lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5118{
5119 struct pci_dev *pdev;
5120
5121 /* Obtain PCI device reference */
5122 if (!phba->pcidev)
5123 return;
5124 else
5125 pdev = phba->pcidev;
5126
5127 /* Free coherent DMA memory allocated */
5128 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5129 phba->hbqslimp.virt, phba->hbqslimp.phys);
5130 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5131 phba->slim2p.virt, phba->slim2p.phys);
5132
5133 /* I/O memory unmap */
5134 iounmap(phba->ctrl_regs_memmap_p);
5135 iounmap(phba->slim_memmap_p);
5136
5137 return;
5138}
5139
5140/**
James Smartda0436e2009-05-22 14:51:39 -04005141 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5142 * @phba: pointer to lpfc hba data structure.
5143 *
5144 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5145 * done and check status.
5146 *
5147 * Return 0 if successful, otherwise -ENODEV.
5148 **/
5149int
5150lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5151{
James Smart28baac72010-02-12 14:42:03 -05005152 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
James Smartda0436e2009-05-22 14:51:39 -04005153 int i, port_error = -ENODEV;
5154
5155 if (!phba->sli4_hba.STAregaddr)
5156 return -ENODEV;
5157
James Smartda0436e2009-05-22 14:51:39 -04005158 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5159 for (i = 0; i < 3000; i++) {
5160 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
5161 /* Encounter fatal POST error, break out */
5162 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
5163 port_error = -ENODEV;
5164 break;
5165 }
5166 if (LPFC_POST_STAGE_ARMFW_READY ==
5167 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
5168 port_error = 0;
5169 break;
5170 }
5171 msleep(10);
5172 }
5173
5174 if (port_error)
5175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5176 "1408 Failure HBA POST Status: sta_reg=0x%x, "
5177 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5178 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5179 bf_get(lpfc_hst_state_perr, &sta_reg),
5180 bf_get(lpfc_hst_state_sfi, &sta_reg),
5181 bf_get(lpfc_hst_state_nip, &sta_reg),
5182 bf_get(lpfc_hst_state_ipc, &sta_reg),
5183 bf_get(lpfc_hst_state_xrom, &sta_reg),
5184 bf_get(lpfc_hst_state_dl, &sta_reg),
5185 bf_get(lpfc_hst_state_port_status, &sta_reg));
5186
5187 /* Log device information */
James Smart28baac72010-02-12 14:42:03 -05005188 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5189 if (bf_get(lpfc_sli_intf_valid,
5190 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5191 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5192 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5193 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5194 bf_get(lpfc_sli_intf_sli_family,
5195 &phba->sli4_hba.sli_intf),
5196 bf_get(lpfc_sli_intf_slirev,
5197 &phba->sli4_hba.sli_intf),
5198 bf_get(lpfc_sli_intf_featurelevel1,
5199 &phba->sli4_hba.sli_intf),
5200 bf_get(lpfc_sli_intf_featurelevel2,
5201 &phba->sli4_hba.sli_intf));
5202 }
James Smarta747c9c2009-11-18 15:41:10 -05005203 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5204 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04005205 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05005206 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5207 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5208 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5209 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5211 "1422 HBA Unrecoverable error: "
5212 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5213 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5214 uerrlo_reg.word0, uerrhi_reg.word0,
5215 phba->sli4_hba.ue_mask_lo,
5216 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04005217 return -ENODEV;
5218 }
5219
James Smartda0436e2009-05-22 14:51:39 -04005220 return port_error;
5221}
5222
5223/**
5224 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5225 * @phba: pointer to lpfc hba data structure.
5226 *
5227 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5228 * memory map.
5229 **/
5230static void
5231lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5232{
5233 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5234 LPFC_UERR_STATUS_LO;
5235 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5236 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05005237 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5238 LPFC_UE_MASK_LO;
5239 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5240 LPFC_UE_MASK_HI;
James Smart28baac72010-02-12 14:42:03 -05005241 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5242 LPFC_SLI_INTF;
James Smartda0436e2009-05-22 14:51:39 -04005243}
5244
5245/**
5246 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5247 * @phba: pointer to lpfc hba data structure.
5248 *
5249 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5250 * memory map.
5251 **/
5252static void
5253lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5254{
5255
5256 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5257 LPFC_HST_STATE;
5258 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5259 LPFC_HST_ISR0;
5260 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5261 LPFC_HST_IMR0;
5262 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5263 LPFC_HST_ISCR0;
5264 return;
5265}
5266
5267/**
5268 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5269 * @phba: pointer to lpfc hba data structure.
5270 * @vf: virtual function number
5271 *
5272 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5273 * based on the given viftual function number, @vf.
5274 *
5275 * Return 0 if successful, otherwise -ENODEV.
5276 **/
5277static int
5278lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5279{
5280 if (vf > LPFC_VIR_FUNC_MAX)
5281 return -ENODEV;
5282
5283 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5284 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5285 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5286 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5287 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5288 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5289 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5290 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5291 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5292 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5293 return 0;
5294}
5295
5296/**
5297 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5298 * @phba: pointer to lpfc hba data structure.
5299 *
5300 * This routine is invoked to create the bootstrap mailbox
5301 * region consistent with the SLI-4 interface spec. This
5302 * routine allocates all memory necessary to communicate
5303 * mailbox commands to the port and sets up all alignment
5304 * needs. No locks are expected to be held when calling
5305 * this routine.
5306 *
5307 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005308 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005309 * ENOMEM - could not allocated memory.
5310 **/
5311static int
5312lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5313{
5314 uint32_t bmbx_size;
5315 struct lpfc_dmabuf *dmabuf;
5316 struct dma_address *dma_address;
5317 uint32_t pa_addr;
5318 uint64_t phys_addr;
5319
5320 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5321 if (!dmabuf)
5322 return -ENOMEM;
5323
5324 /*
5325 * The bootstrap mailbox region is comprised of 2 parts
5326 * plus an alignment restriction of 16 bytes.
5327 */
5328 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5329 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5330 bmbx_size,
5331 &dmabuf->phys,
5332 GFP_KERNEL);
5333 if (!dmabuf->virt) {
5334 kfree(dmabuf);
5335 return -ENOMEM;
5336 }
5337 memset(dmabuf->virt, 0, bmbx_size);
5338
5339 /*
5340 * Initialize the bootstrap mailbox pointers now so that the register
5341 * operations are simple later. The mailbox dma address is required
5342 * to be 16-byte aligned. Also align the virtual memory as each
5343 * maibox is copied into the bmbx mailbox region before issuing the
5344 * command to the port.
5345 */
5346 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5347 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5348
5349 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5350 LPFC_ALIGN_16_BYTE);
5351 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5352 LPFC_ALIGN_16_BYTE);
5353
5354 /*
5355 * Set the high and low physical addresses now. The SLI4 alignment
5356 * requirement is 16 bytes and the mailbox is posted to the port
5357 * as two 30-bit addresses. The other data is a bit marking whether
5358 * the 30-bit address is the high or low address.
5359 * Upcast bmbx aphys to 64bits so shift instruction compiles
5360 * clean on 32 bit machines.
5361 */
5362 dma_address = &phba->sli4_hba.bmbx.dma_address;
5363 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5364 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5365 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5366 LPFC_BMBX_BIT1_ADDR_HI);
5367
5368 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5369 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5370 LPFC_BMBX_BIT1_ADDR_LO);
5371 return 0;
5372}
5373
5374/**
5375 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5376 * @phba: pointer to lpfc hba data structure.
5377 *
5378 * This routine is invoked to teardown the bootstrap mailbox
5379 * region and release all host resources. This routine requires
5380 * the caller to ensure all mailbox commands recovered, no
5381 * additional mailbox comands are sent, and interrupts are disabled
5382 * before calling this routine.
5383 *
5384 **/
5385static void
5386lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5387{
5388 dma_free_coherent(&phba->pcidev->dev,
5389 phba->sli4_hba.bmbx.bmbx_size,
5390 phba->sli4_hba.bmbx.dmabuf->virt,
5391 phba->sli4_hba.bmbx.dmabuf->phys);
5392
5393 kfree(phba->sli4_hba.bmbx.dmabuf);
5394 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5395}
5396
5397/**
5398 * lpfc_sli4_read_config - Get the config parameters.
5399 * @phba: pointer to lpfc hba data structure.
5400 *
5401 * This routine is invoked to read the configuration parameters from the HBA.
5402 * The configuration parameters are used to set the base and maximum values
5403 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5404 * allocation for the port.
5405 *
5406 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005407 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005408 * ENOMEM - No availble memory
5409 * EIO - The mailbox failed to complete successfully.
5410 **/
5411static int
5412lpfc_sli4_read_config(struct lpfc_hba *phba)
5413{
5414 LPFC_MBOXQ_t *pmb;
5415 struct lpfc_mbx_read_config *rd_config;
5416 uint32_t rc = 0;
5417
5418 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5419 if (!pmb) {
5420 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5421 "2011 Unable to allocate memory for issuing "
5422 "SLI_CONFIG_SPECIAL mailbox command\n");
5423 return -ENOMEM;
5424 }
5425
5426 lpfc_read_config(phba, pmb);
5427
5428 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5429 if (rc != MBX_SUCCESS) {
5430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5431 "2012 Mailbox failed , mbxCmd x%x "
5432 "READ_CONFIG, mbxStatus x%x\n",
5433 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5434 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5435 rc = -EIO;
5436 } else {
5437 rd_config = &pmb->u.mqe.un.rd_config;
5438 phba->sli4_hba.max_cfg_param.max_xri =
5439 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5440 phba->sli4_hba.max_cfg_param.xri_base =
5441 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5442 phba->sli4_hba.max_cfg_param.max_vpi =
5443 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5444 phba->sli4_hba.max_cfg_param.vpi_base =
5445 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5446 phba->sli4_hba.max_cfg_param.max_rpi =
5447 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5448 phba->sli4_hba.max_cfg_param.rpi_base =
5449 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5450 phba->sli4_hba.max_cfg_param.max_vfi =
5451 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5452 phba->sli4_hba.max_cfg_param.vfi_base =
5453 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5454 phba->sli4_hba.max_cfg_param.max_fcfi =
5455 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5456 phba->sli4_hba.max_cfg_param.fcfi_base =
5457 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5458 phba->sli4_hba.max_cfg_param.max_eq =
5459 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5460 phba->sli4_hba.max_cfg_param.max_rq =
5461 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5462 phba->sli4_hba.max_cfg_param.max_wq =
5463 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5464 phba->sli4_hba.max_cfg_param.max_cq =
5465 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5466 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5467 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5468 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5469 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5470 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005471 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5472 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005473 phba->max_vports = phba->max_vpi;
5474 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5475 "2003 cfg params XRI(B:%d M:%d), "
5476 "VPI(B:%d M:%d) "
5477 "VFI(B:%d M:%d) "
5478 "RPI(B:%d M:%d) "
5479 "FCFI(B:%d M:%d)\n",
5480 phba->sli4_hba.max_cfg_param.xri_base,
5481 phba->sli4_hba.max_cfg_param.max_xri,
5482 phba->sli4_hba.max_cfg_param.vpi_base,
5483 phba->sli4_hba.max_cfg_param.max_vpi,
5484 phba->sli4_hba.max_cfg_param.vfi_base,
5485 phba->sli4_hba.max_cfg_param.max_vfi,
5486 phba->sli4_hba.max_cfg_param.rpi_base,
5487 phba->sli4_hba.max_cfg_param.max_rpi,
5488 phba->sli4_hba.max_cfg_param.fcfi_base,
5489 phba->sli4_hba.max_cfg_param.max_fcfi);
5490 }
5491 mempool_free(pmb, phba->mbox_mem_pool);
5492
5493 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5494 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5495 phba->cfg_hba_queue_depth =
5496 phba->sli4_hba.max_cfg_param.max_xri;
5497 return rc;
5498}
5499
5500/**
5501 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5502 * @phba: pointer to lpfc hba data structure.
5503 *
5504 * This routine is invoked to setup the host-side endian order to the
5505 * HBA consistent with the SLI-4 interface spec.
5506 *
5507 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005508 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005509 * ENOMEM - No availble memory
5510 * EIO - The mailbox failed to complete successfully.
5511 **/
5512static int
5513lpfc_setup_endian_order(struct lpfc_hba *phba)
5514{
5515 LPFC_MBOXQ_t *mboxq;
5516 uint32_t rc = 0;
5517 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5518 HOST_ENDIAN_HIGH_WORD1};
5519
5520 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5521 if (!mboxq) {
5522 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5523 "0492 Unable to allocate memory for issuing "
5524 "SLI_CONFIG_SPECIAL mailbox command\n");
5525 return -ENOMEM;
5526 }
5527
5528 /*
5529 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5530 * words to contain special data values and no other data.
5531 */
5532 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5533 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5534 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5535 if (rc != MBX_SUCCESS) {
5536 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5537 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5538 "status x%x\n",
5539 rc);
5540 rc = -EIO;
5541 }
5542
5543 mempool_free(mboxq, phba->mbox_mem_pool);
5544 return rc;
5545}
5546
5547/**
5548 * lpfc_sli4_queue_create - Create all the SLI4 queues
5549 * @phba: pointer to lpfc hba data structure.
5550 *
5551 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5552 * operation. For each SLI4 queue type, the parameters such as queue entry
5553 * count (queue depth) shall be taken from the module parameter. For now,
5554 * we just use some constant number as place holder.
5555 *
5556 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005557 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005558 * ENOMEM - No availble memory
5559 * EIO - The mailbox failed to complete successfully.
5560 **/
5561static int
5562lpfc_sli4_queue_create(struct lpfc_hba *phba)
5563{
5564 struct lpfc_queue *qdesc;
5565 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5566 int cfg_fcp_wq_count;
5567 int cfg_fcp_eq_count;
5568
5569 /*
5570 * Sanity check for confiugred queue parameters against the run-time
5571 * device parameters
5572 */
5573
5574 /* Sanity check on FCP fast-path WQ parameters */
5575 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5576 if (cfg_fcp_wq_count >
5577 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5578 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5579 LPFC_SP_WQN_DEF;
5580 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5582 "2581 Not enough WQs (%d) from "
5583 "the pci function for supporting "
5584 "FCP WQs (%d)\n",
5585 phba->sli4_hba.max_cfg_param.max_wq,
5586 phba->cfg_fcp_wq_count);
5587 goto out_error;
5588 }
5589 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5590 "2582 Not enough WQs (%d) from the pci "
5591 "function for supporting the requested "
5592 "FCP WQs (%d), the actual FCP WQs can "
5593 "be supported: %d\n",
5594 phba->sli4_hba.max_cfg_param.max_wq,
5595 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5596 }
5597 /* The actual number of FCP work queues adopted */
5598 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5599
5600 /* Sanity check on FCP fast-path EQ parameters */
5601 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5602 if (cfg_fcp_eq_count >
5603 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5604 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5605 LPFC_SP_EQN_DEF;
5606 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5608 "2574 Not enough EQs (%d) from the "
5609 "pci function for supporting FCP "
5610 "EQs (%d)\n",
5611 phba->sli4_hba.max_cfg_param.max_eq,
5612 phba->cfg_fcp_eq_count);
5613 goto out_error;
5614 }
5615 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5616 "2575 Not enough EQs (%d) from the pci "
5617 "function for supporting the requested "
5618 "FCP EQs (%d), the actual FCP EQs can "
5619 "be supported: %d\n",
5620 phba->sli4_hba.max_cfg_param.max_eq,
5621 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5622 }
5623 /* It does not make sense to have more EQs than WQs */
5624 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5625 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005626 "2593 The FCP EQ count(%d) cannot be greater "
5627 "than the FCP WQ count(%d), limiting the "
5628 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005629 phba->cfg_fcp_wq_count,
5630 phba->cfg_fcp_wq_count);
5631 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5632 }
5633 /* The actual number of FCP event queues adopted */
5634 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5635 /* The overall number of event queues used */
5636 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5637
5638 /*
5639 * Create Event Queues (EQs)
5640 */
5641
5642 /* Get EQ depth from module parameter, fake the default for now */
5643 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5644 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5645
5646 /* Create slow path event queue */
5647 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5648 phba->sli4_hba.eq_ecount);
5649 if (!qdesc) {
5650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5651 "0496 Failed allocate slow-path EQ\n");
5652 goto out_error;
5653 }
5654 phba->sli4_hba.sp_eq = qdesc;
5655
5656 /* Create fast-path FCP Event Queue(s) */
5657 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5658 phba->cfg_fcp_eq_count), GFP_KERNEL);
5659 if (!phba->sli4_hba.fp_eq) {
5660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5661 "2576 Failed allocate memory for fast-path "
5662 "EQ record array\n");
5663 goto out_free_sp_eq;
5664 }
5665 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5666 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5667 phba->sli4_hba.eq_ecount);
5668 if (!qdesc) {
5669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5670 "0497 Failed allocate fast-path EQ\n");
5671 goto out_free_fp_eq;
5672 }
5673 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5674 }
5675
5676 /*
5677 * Create Complete Queues (CQs)
5678 */
5679
5680 /* Get CQ depth from module parameter, fake the default for now */
5681 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5682 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5683
5684 /* Create slow-path Mailbox Command Complete Queue */
5685 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5686 phba->sli4_hba.cq_ecount);
5687 if (!qdesc) {
5688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5689 "0500 Failed allocate slow-path mailbox CQ\n");
5690 goto out_free_fp_eq;
5691 }
5692 phba->sli4_hba.mbx_cq = qdesc;
5693
5694 /* Create slow-path ELS Complete Queue */
5695 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5696 phba->sli4_hba.cq_ecount);
5697 if (!qdesc) {
5698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5699 "0501 Failed allocate slow-path ELS CQ\n");
5700 goto out_free_mbx_cq;
5701 }
5702 phba->sli4_hba.els_cq = qdesc;
5703
James Smartda0436e2009-05-22 14:51:39 -04005704
5705 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5706 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5707 phba->cfg_fcp_eq_count), GFP_KERNEL);
5708 if (!phba->sli4_hba.fcp_cq) {
5709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5710 "2577 Failed allocate memory for fast-path "
5711 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005712 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005713 }
5714 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5715 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5716 phba->sli4_hba.cq_ecount);
5717 if (!qdesc) {
5718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5719 "0499 Failed allocate fast-path FCP "
5720 "CQ (%d)\n", fcp_cqidx);
5721 goto out_free_fcp_cq;
5722 }
5723 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5724 }
5725
5726 /* Create Mailbox Command Queue */
5727 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5728 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5729
5730 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5731 phba->sli4_hba.mq_ecount);
5732 if (!qdesc) {
5733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5734 "0505 Failed allocate slow-path MQ\n");
5735 goto out_free_fcp_cq;
5736 }
5737 phba->sli4_hba.mbx_wq = qdesc;
5738
5739 /*
5740 * Create all the Work Queues (WQs)
5741 */
5742 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5743 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5744
5745 /* Create slow-path ELS Work Queue */
5746 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5747 phba->sli4_hba.wq_ecount);
5748 if (!qdesc) {
5749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5750 "0504 Failed allocate slow-path ELS WQ\n");
5751 goto out_free_mbx_wq;
5752 }
5753 phba->sli4_hba.els_wq = qdesc;
5754
5755 /* Create fast-path FCP Work Queue(s) */
5756 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5757 phba->cfg_fcp_wq_count), GFP_KERNEL);
5758 if (!phba->sli4_hba.fcp_wq) {
5759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5760 "2578 Failed allocate memory for fast-path "
5761 "WQ record array\n");
5762 goto out_free_els_wq;
5763 }
5764 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5765 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5766 phba->sli4_hba.wq_ecount);
5767 if (!qdesc) {
5768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5769 "0503 Failed allocate fast-path FCP "
5770 "WQ (%d)\n", fcp_wqidx);
5771 goto out_free_fcp_wq;
5772 }
5773 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5774 }
5775
5776 /*
5777 * Create Receive Queue (RQ)
5778 */
5779 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5780 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5781
5782 /* Create Receive Queue for header */
5783 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5784 phba->sli4_hba.rq_ecount);
5785 if (!qdesc) {
5786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5787 "0506 Failed allocate receive HRQ\n");
5788 goto out_free_fcp_wq;
5789 }
5790 phba->sli4_hba.hdr_rq = qdesc;
5791
5792 /* Create Receive Queue for data */
5793 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5794 phba->sli4_hba.rq_ecount);
5795 if (!qdesc) {
5796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5797 "0507 Failed allocate receive DRQ\n");
5798 goto out_free_hdr_rq;
5799 }
5800 phba->sli4_hba.dat_rq = qdesc;
5801
5802 return 0;
5803
5804out_free_hdr_rq:
5805 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5806 phba->sli4_hba.hdr_rq = NULL;
5807out_free_fcp_wq:
5808 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5809 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5810 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5811 }
5812 kfree(phba->sli4_hba.fcp_wq);
5813out_free_els_wq:
5814 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5815 phba->sli4_hba.els_wq = NULL;
5816out_free_mbx_wq:
5817 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5818 phba->sli4_hba.mbx_wq = NULL;
5819out_free_fcp_cq:
5820 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5821 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5822 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5823 }
5824 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005825out_free_els_cq:
5826 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5827 phba->sli4_hba.els_cq = NULL;
5828out_free_mbx_cq:
5829 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5830 phba->sli4_hba.mbx_cq = NULL;
5831out_free_fp_eq:
5832 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5833 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5834 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5835 }
5836 kfree(phba->sli4_hba.fp_eq);
5837out_free_sp_eq:
5838 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5839 phba->sli4_hba.sp_eq = NULL;
5840out_error:
5841 return -ENOMEM;
5842}
5843
5844/**
5845 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5846 * @phba: pointer to lpfc hba data structure.
5847 *
5848 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5849 * operation.
5850 *
5851 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005852 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005853 * ENOMEM - No availble memory
5854 * EIO - The mailbox failed to complete successfully.
5855 **/
5856static void
5857lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5858{
5859 int fcp_qidx;
5860
5861 /* Release mailbox command work queue */
5862 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5863 phba->sli4_hba.mbx_wq = NULL;
5864
5865 /* Release ELS work queue */
5866 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5867 phba->sli4_hba.els_wq = NULL;
5868
5869 /* Release FCP work queue */
5870 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5871 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5872 kfree(phba->sli4_hba.fcp_wq);
5873 phba->sli4_hba.fcp_wq = NULL;
5874
5875 /* Release unsolicited receive queue */
5876 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5877 phba->sli4_hba.hdr_rq = NULL;
5878 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5879 phba->sli4_hba.dat_rq = NULL;
5880
James Smartda0436e2009-05-22 14:51:39 -04005881 /* Release ELS complete queue */
5882 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5883 phba->sli4_hba.els_cq = NULL;
5884
5885 /* Release mailbox command complete queue */
5886 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5887 phba->sli4_hba.mbx_cq = NULL;
5888
5889 /* Release FCP response complete queue */
5890 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5891 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5892 kfree(phba->sli4_hba.fcp_cq);
5893 phba->sli4_hba.fcp_cq = NULL;
5894
5895 /* Release fast-path event queue */
5896 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5897 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5898 kfree(phba->sli4_hba.fp_eq);
5899 phba->sli4_hba.fp_eq = NULL;
5900
5901 /* Release slow-path event queue */
5902 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5903 phba->sli4_hba.sp_eq = NULL;
5904
5905 return;
5906}
5907
5908/**
5909 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5910 * @phba: pointer to lpfc hba data structure.
5911 *
5912 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5913 * operation.
5914 *
5915 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005916 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005917 * ENOMEM - No availble memory
5918 * EIO - The mailbox failed to complete successfully.
5919 **/
5920int
5921lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5922{
5923 int rc = -ENOMEM;
5924 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5925 int fcp_cq_index = 0;
5926
5927 /*
5928 * Set up Event Queues (EQs)
5929 */
5930
5931 /* Set up slow-path event queue */
5932 if (!phba->sli4_hba.sp_eq) {
5933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5934 "0520 Slow-path EQ not allocated\n");
5935 goto out_error;
5936 }
5937 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5938 LPFC_SP_DEF_IMAX);
5939 if (rc) {
5940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5941 "0521 Failed setup of slow-path EQ: "
5942 "rc = 0x%x\n", rc);
5943 goto out_error;
5944 }
5945 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5946 "2583 Slow-path EQ setup: queue-id=%d\n",
5947 phba->sli4_hba.sp_eq->queue_id);
5948
5949 /* Set up fast-path event queue */
5950 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5951 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5953 "0522 Fast-path EQ (%d) not "
5954 "allocated\n", fcp_eqidx);
5955 goto out_destroy_fp_eq;
5956 }
5957 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5958 phba->cfg_fcp_imax);
5959 if (rc) {
5960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5961 "0523 Failed setup of fast-path EQ "
5962 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5963 goto out_destroy_fp_eq;
5964 }
5965 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5966 "2584 Fast-path EQ setup: "
5967 "queue[%d]-id=%d\n", fcp_eqidx,
5968 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5969 }
5970
5971 /*
5972 * Set up Complete Queues (CQs)
5973 */
5974
5975 /* Set up slow-path MBOX Complete Queue as the first CQ */
5976 if (!phba->sli4_hba.mbx_cq) {
5977 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5978 "0528 Mailbox CQ not allocated\n");
5979 goto out_destroy_fp_eq;
5980 }
5981 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5982 LPFC_MCQ, LPFC_MBOX);
5983 if (rc) {
5984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5985 "0529 Failed setup of slow-path mailbox CQ: "
5986 "rc = 0x%x\n", rc);
5987 goto out_destroy_fp_eq;
5988 }
5989 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5990 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5991 phba->sli4_hba.mbx_cq->queue_id,
5992 phba->sli4_hba.sp_eq->queue_id);
5993
5994 /* Set up slow-path ELS Complete Queue */
5995 if (!phba->sli4_hba.els_cq) {
5996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5997 "0530 ELS CQ not allocated\n");
5998 goto out_destroy_mbx_cq;
5999 }
6000 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6001 LPFC_WCQ, LPFC_ELS);
6002 if (rc) {
6003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6004 "0531 Failed setup of slow-path ELS CQ: "
6005 "rc = 0x%x\n", rc);
6006 goto out_destroy_mbx_cq;
6007 }
6008 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6009 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6010 phba->sli4_hba.els_cq->queue_id,
6011 phba->sli4_hba.sp_eq->queue_id);
6012
James Smartda0436e2009-05-22 14:51:39 -04006013 /* Set up fast-path FCP Response Complete Queue */
6014 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6015 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6016 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6017 "0526 Fast-path FCP CQ (%d) not "
6018 "allocated\n", fcp_cqidx);
6019 goto out_destroy_fcp_cq;
6020 }
6021 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
6022 phba->sli4_hba.fp_eq[fcp_cqidx],
6023 LPFC_WCQ, LPFC_FCP);
6024 if (rc) {
6025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6026 "0527 Failed setup of fast-path FCP "
6027 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6028 goto out_destroy_fcp_cq;
6029 }
6030 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6031 "2588 FCP CQ setup: cq[%d]-id=%d, "
6032 "parent eq[%d]-id=%d\n",
6033 fcp_cqidx,
6034 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6035 fcp_cqidx,
6036 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
6037 }
6038
6039 /*
6040 * Set up all the Work Queues (WQs)
6041 */
6042
6043 /* Set up Mailbox Command Queue */
6044 if (!phba->sli4_hba.mbx_wq) {
6045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6046 "0538 Slow-path MQ not allocated\n");
6047 goto out_destroy_fcp_cq;
6048 }
6049 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6050 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6051 if (rc) {
6052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6053 "0539 Failed setup of slow-path MQ: "
6054 "rc = 0x%x\n", rc);
6055 goto out_destroy_fcp_cq;
6056 }
6057 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6058 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6059 phba->sli4_hba.mbx_wq->queue_id,
6060 phba->sli4_hba.mbx_cq->queue_id);
6061
6062 /* Set up slow-path ELS Work Queue */
6063 if (!phba->sli4_hba.els_wq) {
6064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6065 "0536 Slow-path ELS WQ not allocated\n");
6066 goto out_destroy_mbx_wq;
6067 }
6068 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6069 phba->sli4_hba.els_cq, LPFC_ELS);
6070 if (rc) {
6071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6072 "0537 Failed setup of slow-path ELS WQ: "
6073 "rc = 0x%x\n", rc);
6074 goto out_destroy_mbx_wq;
6075 }
6076 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6077 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6078 phba->sli4_hba.els_wq->queue_id,
6079 phba->sli4_hba.els_cq->queue_id);
6080
6081 /* Set up fast-path FCP Work Queue */
6082 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6083 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6084 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6085 "0534 Fast-path FCP WQ (%d) not "
6086 "allocated\n", fcp_wqidx);
6087 goto out_destroy_fcp_wq;
6088 }
6089 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6090 phba->sli4_hba.fcp_cq[fcp_cq_index],
6091 LPFC_FCP);
6092 if (rc) {
6093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6094 "0535 Failed setup of fast-path FCP "
6095 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6096 goto out_destroy_fcp_wq;
6097 }
6098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6099 "2591 FCP WQ setup: wq[%d]-id=%d, "
6100 "parent cq[%d]-id=%d\n",
6101 fcp_wqidx,
6102 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6103 fcp_cq_index,
6104 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6105 /* Round robin FCP Work Queue's Completion Queue assignment */
6106 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
6107 }
6108
6109 /*
6110 * Create Receive Queue (RQ)
6111 */
6112 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6114 "0540 Receive Queue not allocated\n");
6115 goto out_destroy_fcp_wq;
6116 }
6117 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006118 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006119 if (rc) {
6120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6121 "0541 Failed setup of Receive Queue: "
6122 "rc = 0x%x\n", rc);
6123 goto out_destroy_fcp_wq;
6124 }
6125 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6126 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6127 "parent cq-id=%d\n",
6128 phba->sli4_hba.hdr_rq->queue_id,
6129 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006130 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006131 return 0;
6132
6133out_destroy_fcp_wq:
6134 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6135 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6136 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6137out_destroy_mbx_wq:
6138 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6139out_destroy_fcp_cq:
6140 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6141 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04006142 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6143out_destroy_mbx_cq:
6144 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6145out_destroy_fp_eq:
6146 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6147 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6148 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6149out_error:
6150 return rc;
6151}
6152
6153/**
6154 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6155 * @phba: pointer to lpfc hba data structure.
6156 *
6157 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6158 * operation.
6159 *
6160 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006161 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006162 * ENOMEM - No availble memory
6163 * EIO - The mailbox failed to complete successfully.
6164 **/
6165void
6166lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6167{
6168 int fcp_qidx;
6169
6170 /* Unset mailbox command work queue */
6171 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6172 /* Unset ELS work queue */
6173 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6174 /* Unset unsolicited receive queue */
6175 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6176 /* Unset FCP work queue */
6177 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6178 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6179 /* Unset mailbox command complete queue */
6180 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6181 /* Unset ELS complete queue */
6182 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006183 /* Unset FCP response complete queue */
6184 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6185 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6186 /* Unset fast-path event queue */
6187 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6188 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6189 /* Unset slow-path event queue */
6190 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6191}
6192
6193/**
6194 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6195 * @phba: pointer to lpfc hba data structure.
6196 *
6197 * This routine is invoked to allocate and set up a pool of completion queue
6198 * events. The body of the completion queue event is a completion queue entry
6199 * CQE. For now, this pool is used for the interrupt service routine to queue
6200 * the following HBA completion queue events for the worker thread to process:
6201 * - Mailbox asynchronous events
6202 * - Receive queue completion unsolicited events
6203 * Later, this can be used for all the slow-path events.
6204 *
6205 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006206 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006207 * -ENOMEM - No availble memory
6208 **/
6209static int
6210lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6211{
6212 struct lpfc_cq_event *cq_event;
6213 int i;
6214
6215 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6216 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6217 if (!cq_event)
6218 goto out_pool_create_fail;
6219 list_add_tail(&cq_event->list,
6220 &phba->sli4_hba.sp_cqe_event_pool);
6221 }
6222 return 0;
6223
6224out_pool_create_fail:
6225 lpfc_sli4_cq_event_pool_destroy(phba);
6226 return -ENOMEM;
6227}
6228
6229/**
6230 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6231 * @phba: pointer to lpfc hba data structure.
6232 *
6233 * This routine is invoked to free the pool of completion queue events at
6234 * driver unload time. Note that, it is the responsibility of the driver
6235 * cleanup routine to free all the outstanding completion-queue events
6236 * allocated from this pool back into the pool before invoking this routine
6237 * to destroy the pool.
6238 **/
6239static void
6240lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6241{
6242 struct lpfc_cq_event *cq_event, *next_cq_event;
6243
6244 list_for_each_entry_safe(cq_event, next_cq_event,
6245 &phba->sli4_hba.sp_cqe_event_pool, list) {
6246 list_del(&cq_event->list);
6247 kfree(cq_event);
6248 }
6249}
6250
6251/**
6252 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6253 * @phba: pointer to lpfc hba data structure.
6254 *
6255 * This routine is the lock free version of the API invoked to allocate a
6256 * completion-queue event from the free pool.
6257 *
6258 * Return: Pointer to the newly allocated completion-queue event if successful
6259 * NULL otherwise.
6260 **/
6261struct lpfc_cq_event *
6262__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6263{
6264 struct lpfc_cq_event *cq_event = NULL;
6265
6266 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6267 struct lpfc_cq_event, list);
6268 return cq_event;
6269}
6270
6271/**
6272 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6273 * @phba: pointer to lpfc hba data structure.
6274 *
6275 * This routine is the lock version of the API invoked to allocate a
6276 * completion-queue event from the free pool.
6277 *
6278 * Return: Pointer to the newly allocated completion-queue event if successful
6279 * NULL otherwise.
6280 **/
6281struct lpfc_cq_event *
6282lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6283{
6284 struct lpfc_cq_event *cq_event;
6285 unsigned long iflags;
6286
6287 spin_lock_irqsave(&phba->hbalock, iflags);
6288 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6289 spin_unlock_irqrestore(&phba->hbalock, iflags);
6290 return cq_event;
6291}
6292
6293/**
6294 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6295 * @phba: pointer to lpfc hba data structure.
6296 * @cq_event: pointer to the completion queue event to be freed.
6297 *
6298 * This routine is the lock free version of the API invoked to release a
6299 * completion-queue event back into the free pool.
6300 **/
6301void
6302__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6303 struct lpfc_cq_event *cq_event)
6304{
6305 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6306}
6307
6308/**
6309 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6310 * @phba: pointer to lpfc hba data structure.
6311 * @cq_event: pointer to the completion queue event to be freed.
6312 *
6313 * This routine is the lock version of the API invoked to release a
6314 * completion-queue event back into the free pool.
6315 **/
6316void
6317lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6318 struct lpfc_cq_event *cq_event)
6319{
6320 unsigned long iflags;
6321 spin_lock_irqsave(&phba->hbalock, iflags);
6322 __lpfc_sli4_cq_event_release(phba, cq_event);
6323 spin_unlock_irqrestore(&phba->hbalock, iflags);
6324}
6325
6326/**
6327 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6328 * @phba: pointer to lpfc hba data structure.
6329 *
6330 * This routine is to free all the pending completion-queue events to the
6331 * back into the free pool for device reset.
6332 **/
6333static void
6334lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6335{
6336 LIST_HEAD(cqelist);
6337 struct lpfc_cq_event *cqe;
6338 unsigned long iflags;
6339
6340 /* Retrieve all the pending WCQEs from pending WCQE lists */
6341 spin_lock_irqsave(&phba->hbalock, iflags);
6342 /* Pending FCP XRI abort events */
6343 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6344 &cqelist);
6345 /* Pending ELS XRI abort events */
6346 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6347 &cqelist);
6348 /* Pending asynnc events */
6349 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6350 &cqelist);
6351 spin_unlock_irqrestore(&phba->hbalock, iflags);
6352
6353 while (!list_empty(&cqelist)) {
6354 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6355 lpfc_sli4_cq_event_release(phba, cqe);
6356 }
6357}
6358
6359/**
6360 * lpfc_pci_function_reset - Reset pci function.
6361 * @phba: pointer to lpfc hba data structure.
6362 *
6363 * This routine is invoked to request a PCI function reset. It will destroys
6364 * all resources assigned to the PCI function which originates this request.
6365 *
6366 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006367 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006368 * ENOMEM - No availble memory
6369 * EIO - The mailbox failed to complete successfully.
6370 **/
6371int
6372lpfc_pci_function_reset(struct lpfc_hba *phba)
6373{
6374 LPFC_MBOXQ_t *mboxq;
6375 uint32_t rc = 0;
6376 uint32_t shdr_status, shdr_add_status;
6377 union lpfc_sli4_cfg_shdr *shdr;
6378
6379 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6380 if (!mboxq) {
6381 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6382 "0494 Unable to allocate memory for issuing "
6383 "SLI_FUNCTION_RESET mailbox command\n");
6384 return -ENOMEM;
6385 }
6386
6387 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6388 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6389 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6390 LPFC_SLI4_MBX_EMBED);
6391 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6392 shdr = (union lpfc_sli4_cfg_shdr *)
6393 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6394 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6395 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6396 if (rc != MBX_TIMEOUT)
6397 mempool_free(mboxq, phba->mbox_mem_pool);
6398 if (shdr_status || shdr_add_status || rc) {
6399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6400 "0495 SLI_FUNCTION_RESET mailbox failed with "
6401 "status x%x add_status x%x, mbx status x%x\n",
6402 shdr_status, shdr_add_status, rc);
6403 rc = -ENXIO;
6404 }
6405 return rc;
6406}
6407
6408/**
6409 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6410 * @phba: pointer to lpfc hba data structure.
6411 * @cnt: number of nop mailbox commands to send.
6412 *
6413 * This routine is invoked to send a number @cnt of NOP mailbox command and
6414 * wait for each command to complete.
6415 *
6416 * Return: the number of NOP mailbox command completed.
6417 **/
6418static int
6419lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6420{
6421 LPFC_MBOXQ_t *mboxq;
6422 int length, cmdsent;
6423 uint32_t mbox_tmo;
6424 uint32_t rc = 0;
6425 uint32_t shdr_status, shdr_add_status;
6426 union lpfc_sli4_cfg_shdr *shdr;
6427
6428 if (cnt == 0) {
6429 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6430 "2518 Requested to send 0 NOP mailbox cmd\n");
6431 return cnt;
6432 }
6433
6434 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6435 if (!mboxq) {
6436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6437 "2519 Unable to allocate memory for issuing "
6438 "NOP mailbox command\n");
6439 return 0;
6440 }
6441
6442 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6443 length = (sizeof(struct lpfc_mbx_nop) -
6444 sizeof(struct lpfc_sli4_cfg_mhdr));
6445 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6446 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6447
6448 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6449 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6450 if (!phba->sli4_hba.intr_enable)
6451 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6452 else
6453 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6454 if (rc == MBX_TIMEOUT)
6455 break;
6456 /* Check return status */
6457 shdr = (union lpfc_sli4_cfg_shdr *)
6458 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6459 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6460 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6461 &shdr->response);
6462 if (shdr_status || shdr_add_status || rc) {
6463 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6464 "2520 NOP mailbox command failed "
6465 "status x%x add_status x%x mbx "
6466 "status x%x\n", shdr_status,
6467 shdr_add_status, rc);
6468 break;
6469 }
6470 }
6471
6472 if (rc != MBX_TIMEOUT)
6473 mempool_free(mboxq, phba->mbox_mem_pool);
6474
6475 return cmdsent;
6476}
6477
6478/**
6479 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6480 * @phba: pointer to lpfc hba data structure.
6481 * @fcfi: fcf index.
6482 *
6483 * This routine is invoked to unregister a FCFI from device.
6484 **/
6485void
6486lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6487{
6488 LPFC_MBOXQ_t *mbox;
6489 uint32_t mbox_tmo;
6490 int rc;
6491 unsigned long flags;
6492
6493 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6494
6495 if (!mbox)
6496 return;
6497
6498 lpfc_unreg_fcfi(mbox, fcfi);
6499
6500 if (!phba->sli4_hba.intr_enable)
6501 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6502 else {
6503 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6504 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6505 }
6506 if (rc != MBX_TIMEOUT)
6507 mempool_free(mbox, phba->mbox_mem_pool);
6508 if (rc != MBX_SUCCESS)
6509 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6510 "2517 Unregister FCFI command failed "
6511 "status %d, mbxStatus x%x\n", rc,
6512 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6513 else {
6514 spin_lock_irqsave(&phba->hbalock, flags);
6515 /* Mark the FCFI is no longer registered */
6516 phba->fcf.fcf_flag &=
James Smartecfd03c2010-02-12 14:41:27 -05006517 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
James Smartda0436e2009-05-22 14:51:39 -04006518 spin_unlock_irqrestore(&phba->hbalock, flags);
6519 }
6520}
6521
6522/**
6523 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6524 * @phba: pointer to lpfc hba data structure.
6525 *
6526 * This routine is invoked to set up the PCI device memory space for device
6527 * with SLI-4 interface spec.
6528 *
6529 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006530 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006531 * other values - error
6532 **/
6533static int
6534lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6535{
6536 struct pci_dev *pdev;
6537 unsigned long bar0map_len, bar1map_len, bar2map_len;
6538 int error = -ENODEV;
6539
6540 /* Obtain PCI device reference */
6541 if (!phba->pcidev)
6542 return error;
6543 else
6544 pdev = phba->pcidev;
6545
6546 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006547 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6548 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6549 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6550 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006551 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006552 }
6553 }
James Smartda0436e2009-05-22 14:51:39 -04006554
6555 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6556 * number of bytes required by each mapping. They are actually
James Smart1dfb5a42010-02-12 14:40:50 -05006557 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006558 */
James Smart1dfb5a42010-02-12 14:40:50 -05006559 if (pci_resource_start(pdev, 0)) {
6560 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6561 bar0map_len = pci_resource_len(pdev, 0);
6562 } else {
6563 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6564 bar0map_len = pci_resource_len(pdev, 1);
6565 }
6566 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6567 bar1map_len = pci_resource_len(pdev, 2);
James Smartda0436e2009-05-22 14:51:39 -04006568
James Smart1dfb5a42010-02-12 14:40:50 -05006569 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6570 bar2map_len = pci_resource_len(pdev, 4);
James Smartda0436e2009-05-22 14:51:39 -04006571
6572 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6573 phba->sli4_hba.conf_regs_memmap_p =
6574 ioremap(phba->pci_bar0_map, bar0map_len);
6575 if (!phba->sli4_hba.conf_regs_memmap_p) {
6576 dev_printk(KERN_ERR, &pdev->dev,
6577 "ioremap failed for SLI4 PCI config registers.\n");
6578 goto out;
6579 }
6580
6581 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6582 phba->sli4_hba.ctrl_regs_memmap_p =
6583 ioremap(phba->pci_bar1_map, bar1map_len);
6584 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6585 dev_printk(KERN_ERR, &pdev->dev,
6586 "ioremap failed for SLI4 HBA control registers.\n");
6587 goto out_iounmap_conf;
6588 }
6589
6590 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6591 phba->sli4_hba.drbl_regs_memmap_p =
6592 ioremap(phba->pci_bar2_map, bar2map_len);
6593 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6594 dev_printk(KERN_ERR, &pdev->dev,
6595 "ioremap failed for SLI4 HBA doorbell registers.\n");
6596 goto out_iounmap_ctrl;
6597 }
6598
6599 /* Set up BAR0 PCI config space register memory map */
6600 lpfc_sli4_bar0_register_memmap(phba);
6601
6602 /* Set up BAR1 register memory map */
6603 lpfc_sli4_bar1_register_memmap(phba);
6604
6605 /* Set up BAR2 register memory map */
6606 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6607 if (error)
6608 goto out_iounmap_all;
6609
6610 return 0;
6611
6612out_iounmap_all:
6613 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6614out_iounmap_ctrl:
6615 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6616out_iounmap_conf:
6617 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6618out:
6619 return error;
6620}
6621
6622/**
6623 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6624 * @phba: pointer to lpfc hba data structure.
6625 *
6626 * This routine is invoked to unset the PCI device memory space for device
6627 * with SLI-4 interface spec.
6628 **/
6629static void
6630lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6631{
6632 struct pci_dev *pdev;
6633
6634 /* Obtain PCI device reference */
6635 if (!phba->pcidev)
6636 return;
6637 else
6638 pdev = phba->pcidev;
6639
6640 /* Free coherent DMA memory allocated */
6641
6642 /* Unmap I/O memory space */
6643 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6644 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6645 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6646
6647 return;
6648}
6649
6650/**
James Smart3772a992009-05-22 14:50:54 -04006651 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6652 * @phba: pointer to lpfc hba data structure.
6653 *
6654 * This routine is invoked to enable the MSI-X interrupt vectors to device
6655 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6656 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6657 * invoked, enables either all or nothing, depending on the current
6658 * availability of PCI vector resources. The device driver is responsible
6659 * for calling the individual request_irq() to register each MSI-X vector
6660 * with a interrupt handler, which is done in this function. Note that
6661 * later when device is unloading, the driver should always call free_irq()
6662 * on all MSI-X vectors it has done request_irq() on before calling
6663 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6664 * will be left with MSI-X enabled and leaks its vectors.
6665 *
6666 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006667 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006668 * other values - error
6669 **/
6670static int
6671lpfc_sli_enable_msix(struct lpfc_hba *phba)
6672{
6673 int rc, i;
6674 LPFC_MBOXQ_t *pmb;
6675
6676 /* Set up MSI-X multi-message vectors */
6677 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6678 phba->msix_entries[i].entry = i;
6679
6680 /* Configure MSI-X capability structure */
6681 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6682 ARRAY_SIZE(phba->msix_entries));
6683 if (rc) {
6684 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6685 "0420 PCI enable MSI-X failed (%d)\n", rc);
6686 goto msi_fail_out;
6687 }
6688 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6689 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6690 "0477 MSI-X entry[%d]: vector=x%x "
6691 "message=%d\n", i,
6692 phba->msix_entries[i].vector,
6693 phba->msix_entries[i].entry);
6694 /*
6695 * Assign MSI-X vectors to interrupt handlers
6696 */
6697
6698 /* vector-0 is associated to slow-path handler */
6699 rc = request_irq(phba->msix_entries[0].vector,
6700 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6701 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6702 if (rc) {
6703 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6704 "0421 MSI-X slow-path request_irq failed "
6705 "(%d)\n", rc);
6706 goto msi_fail_out;
6707 }
6708
6709 /* vector-1 is associated to fast-path handler */
6710 rc = request_irq(phba->msix_entries[1].vector,
6711 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6712 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6713
6714 if (rc) {
6715 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6716 "0429 MSI-X fast-path request_irq failed "
6717 "(%d)\n", rc);
6718 goto irq_fail_out;
6719 }
6720
6721 /*
6722 * Configure HBA MSI-X attention conditions to messages
6723 */
6724 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6725
6726 if (!pmb) {
6727 rc = -ENOMEM;
6728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6729 "0474 Unable to allocate memory for issuing "
6730 "MBOX_CONFIG_MSI command\n");
6731 goto mem_fail_out;
6732 }
6733 rc = lpfc_config_msi(phba, pmb);
6734 if (rc)
6735 goto mbx_fail_out;
6736 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6737 if (rc != MBX_SUCCESS) {
6738 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6739 "0351 Config MSI mailbox command failed, "
6740 "mbxCmd x%x, mbxStatus x%x\n",
6741 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6742 goto mbx_fail_out;
6743 }
6744
6745 /* Free memory allocated for mailbox command */
6746 mempool_free(pmb, phba->mbox_mem_pool);
6747 return rc;
6748
6749mbx_fail_out:
6750 /* Free memory allocated for mailbox command */
6751 mempool_free(pmb, phba->mbox_mem_pool);
6752
6753mem_fail_out:
6754 /* free the irq already requested */
6755 free_irq(phba->msix_entries[1].vector, phba);
6756
6757irq_fail_out:
6758 /* free the irq already requested */
6759 free_irq(phba->msix_entries[0].vector, phba);
6760
6761msi_fail_out:
6762 /* Unconfigure MSI-X capability structure */
6763 pci_disable_msix(phba->pcidev);
6764 return rc;
6765}
6766
6767/**
6768 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6769 * @phba: pointer to lpfc hba data structure.
6770 *
6771 * This routine is invoked to release the MSI-X vectors and then disable the
6772 * MSI-X interrupt mode to device with SLI-3 interface spec.
6773 **/
6774static void
6775lpfc_sli_disable_msix(struct lpfc_hba *phba)
6776{
6777 int i;
6778
6779 /* Free up MSI-X multi-message vectors */
6780 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6781 free_irq(phba->msix_entries[i].vector, phba);
6782 /* Disable MSI-X */
6783 pci_disable_msix(phba->pcidev);
6784
6785 return;
6786}
6787
6788/**
6789 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6790 * @phba: pointer to lpfc hba data structure.
6791 *
6792 * This routine is invoked to enable the MSI interrupt mode to device with
6793 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6794 * enable the MSI vector. The device driver is responsible for calling the
6795 * request_irq() to register MSI vector with a interrupt the handler, which
6796 * is done in this function.
6797 *
6798 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006799 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006800 * other values - error
6801 */
6802static int
6803lpfc_sli_enable_msi(struct lpfc_hba *phba)
6804{
6805 int rc;
6806
6807 rc = pci_enable_msi(phba->pcidev);
6808 if (!rc)
6809 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6810 "0462 PCI enable MSI mode success.\n");
6811 else {
6812 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6813 "0471 PCI enable MSI mode failed (%d)\n", rc);
6814 return rc;
6815 }
6816
6817 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6818 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6819 if (rc) {
6820 pci_disable_msi(phba->pcidev);
6821 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6822 "0478 MSI request_irq failed (%d)\n", rc);
6823 }
6824 return rc;
6825}
6826
6827/**
6828 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6829 * @phba: pointer to lpfc hba data structure.
6830 *
6831 * This routine is invoked to disable the MSI interrupt mode to device with
6832 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6833 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6834 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6835 * its vector.
6836 */
6837static void
6838lpfc_sli_disable_msi(struct lpfc_hba *phba)
6839{
6840 free_irq(phba->pcidev->irq, phba);
6841 pci_disable_msi(phba->pcidev);
6842 return;
6843}
6844
6845/**
6846 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6847 * @phba: pointer to lpfc hba data structure.
6848 *
6849 * This routine is invoked to enable device interrupt and associate driver's
6850 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6851 * spec. Depends on the interrupt mode configured to the driver, the driver
6852 * will try to fallback from the configured interrupt mode to an interrupt
6853 * mode which is supported by the platform, kernel, and device in the order
6854 * of:
6855 * MSI-X -> MSI -> IRQ.
6856 *
6857 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006858 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006859 * other values - error
6860 **/
6861static uint32_t
6862lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6863{
6864 uint32_t intr_mode = LPFC_INTR_ERROR;
6865 int retval;
6866
6867 if (cfg_mode == 2) {
6868 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6869 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6870 if (!retval) {
6871 /* Now, try to enable MSI-X interrupt mode */
6872 retval = lpfc_sli_enable_msix(phba);
6873 if (!retval) {
6874 /* Indicate initialization to MSI-X mode */
6875 phba->intr_type = MSIX;
6876 intr_mode = 2;
6877 }
6878 }
6879 }
6880
6881 /* Fallback to MSI if MSI-X initialization failed */
6882 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6883 retval = lpfc_sli_enable_msi(phba);
6884 if (!retval) {
6885 /* Indicate initialization to MSI mode */
6886 phba->intr_type = MSI;
6887 intr_mode = 1;
6888 }
6889 }
6890
6891 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6892 if (phba->intr_type == NONE) {
6893 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6894 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6895 if (!retval) {
6896 /* Indicate initialization to INTx mode */
6897 phba->intr_type = INTx;
6898 intr_mode = 0;
6899 }
6900 }
6901 return intr_mode;
6902}
6903
6904/**
6905 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6906 * @phba: pointer to lpfc hba data structure.
6907 *
6908 * This routine is invoked to disable device interrupt and disassociate the
6909 * driver's interrupt handler(s) from interrupt vector(s) to device with
6910 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6911 * release the interrupt vector(s) for the message signaled interrupt.
6912 **/
6913static void
6914lpfc_sli_disable_intr(struct lpfc_hba *phba)
6915{
6916 /* Disable the currently initialized interrupt mode */
6917 if (phba->intr_type == MSIX)
6918 lpfc_sli_disable_msix(phba);
6919 else if (phba->intr_type == MSI)
6920 lpfc_sli_disable_msi(phba);
6921 else if (phba->intr_type == INTx)
6922 free_irq(phba->pcidev->irq, phba);
6923
6924 /* Reset interrupt management states */
6925 phba->intr_type = NONE;
6926 phba->sli.slistat.sli_intr = 0;
6927
6928 return;
6929}
6930
6931/**
James Smartda0436e2009-05-22 14:51:39 -04006932 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6933 * @phba: pointer to lpfc hba data structure.
6934 *
6935 * This routine is invoked to enable the MSI-X interrupt vectors to device
6936 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6937 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6938 * enables either all or nothing, depending on the current availability of
6939 * PCI vector resources. The device driver is responsible for calling the
6940 * individual request_irq() to register each MSI-X vector with a interrupt
6941 * handler, which is done in this function. Note that later when device is
6942 * unloading, the driver should always call free_irq() on all MSI-X vectors
6943 * it has done request_irq() on before calling pci_disable_msix(). Failure
6944 * to do so results in a BUG_ON() and a device will be left with MSI-X
6945 * enabled and leaks its vectors.
6946 *
6947 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006948 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006949 * other values - error
6950 **/
6951static int
6952lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6953{
6954 int rc, index;
6955
6956 /* Set up MSI-X multi-message vectors */
6957 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6958 phba->sli4_hba.msix_entries[index].entry = index;
6959
6960 /* Configure MSI-X capability structure */
6961 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6962 phba->sli4_hba.cfg_eqn);
6963 if (rc) {
6964 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6965 "0484 PCI enable MSI-X failed (%d)\n", rc);
6966 goto msi_fail_out;
6967 }
6968 /* Log MSI-X vector assignment */
6969 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6970 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6971 "0489 MSI-X entry[%d]: vector=x%x "
6972 "message=%d\n", index,
6973 phba->sli4_hba.msix_entries[index].vector,
6974 phba->sli4_hba.msix_entries[index].entry);
6975 /*
6976 * Assign MSI-X vectors to interrupt handlers
6977 */
6978
6979 /* The first vector must associated to slow-path handler for MQ */
6980 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6981 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6982 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6983 if (rc) {
6984 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6985 "0485 MSI-X slow-path request_irq failed "
6986 "(%d)\n", rc);
6987 goto msi_fail_out;
6988 }
6989
6990 /* The rest of the vector(s) are associated to fast-path handler(s) */
6991 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6992 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6993 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6994 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6995 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6996 LPFC_FP_DRIVER_HANDLER_NAME,
6997 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6998 if (rc) {
6999 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7000 "0486 MSI-X fast-path (%d) "
7001 "request_irq failed (%d)\n", index, rc);
7002 goto cfg_fail_out;
7003 }
7004 }
7005
7006 return rc;
7007
7008cfg_fail_out:
7009 /* free the irq already requested */
7010 for (--index; index >= 1; index--)
7011 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7012 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7013
7014 /* free the irq already requested */
7015 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7016
7017msi_fail_out:
7018 /* Unconfigure MSI-X capability structure */
7019 pci_disable_msix(phba->pcidev);
7020 return rc;
7021}
7022
7023/**
7024 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7025 * @phba: pointer to lpfc hba data structure.
7026 *
7027 * This routine is invoked to release the MSI-X vectors and then disable the
7028 * MSI-X interrupt mode to device with SLI-4 interface spec.
7029 **/
7030static void
7031lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7032{
7033 int index;
7034
7035 /* Free up MSI-X multi-message vectors */
7036 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7037
7038 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
7039 free_irq(phba->sli4_hba.msix_entries[index].vector,
7040 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7041 /* Disable MSI-X */
7042 pci_disable_msix(phba->pcidev);
7043
7044 return;
7045}
7046
7047/**
7048 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7049 * @phba: pointer to lpfc hba data structure.
7050 *
7051 * This routine is invoked to enable the MSI interrupt mode to device with
7052 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7053 * to enable the MSI vector. The device driver is responsible for calling
7054 * the request_irq() to register MSI vector with a interrupt the handler,
7055 * which is done in this function.
7056 *
7057 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007058 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007059 * other values - error
7060 **/
7061static int
7062lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7063{
7064 int rc, index;
7065
7066 rc = pci_enable_msi(phba->pcidev);
7067 if (!rc)
7068 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7069 "0487 PCI enable MSI mode success.\n");
7070 else {
7071 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7072 "0488 PCI enable MSI mode failed (%d)\n", rc);
7073 return rc;
7074 }
7075
7076 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7077 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7078 if (rc) {
7079 pci_disable_msi(phba->pcidev);
7080 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7081 "0490 MSI request_irq failed (%d)\n", rc);
7082 }
7083
7084 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7085 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7086 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7087 }
7088
7089 return rc;
7090}
7091
7092/**
7093 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7094 * @phba: pointer to lpfc hba data structure.
7095 *
7096 * This routine is invoked to disable the MSI interrupt mode to device with
7097 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7098 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7099 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7100 * its vector.
7101 **/
7102static void
7103lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7104{
7105 free_irq(phba->pcidev->irq, phba);
7106 pci_disable_msi(phba->pcidev);
7107 return;
7108}
7109
7110/**
7111 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7112 * @phba: pointer to lpfc hba data structure.
7113 *
7114 * This routine is invoked to enable device interrupt and associate driver's
7115 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7116 * interface spec. Depends on the interrupt mode configured to the driver,
7117 * the driver will try to fallback from the configured interrupt mode to an
7118 * interrupt mode which is supported by the platform, kernel, and device in
7119 * the order of:
7120 * MSI-X -> MSI -> IRQ.
7121 *
7122 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007123 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007124 * other values - error
7125 **/
7126static uint32_t
7127lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7128{
7129 uint32_t intr_mode = LPFC_INTR_ERROR;
7130 int retval, index;
7131
7132 if (cfg_mode == 2) {
7133 /* Preparation before conf_msi mbox cmd */
7134 retval = 0;
7135 if (!retval) {
7136 /* Now, try to enable MSI-X interrupt mode */
7137 retval = lpfc_sli4_enable_msix(phba);
7138 if (!retval) {
7139 /* Indicate initialization to MSI-X mode */
7140 phba->intr_type = MSIX;
7141 intr_mode = 2;
7142 }
7143 }
7144 }
7145
7146 /* Fallback to MSI if MSI-X initialization failed */
7147 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7148 retval = lpfc_sli4_enable_msi(phba);
7149 if (!retval) {
7150 /* Indicate initialization to MSI mode */
7151 phba->intr_type = MSI;
7152 intr_mode = 1;
7153 }
7154 }
7155
7156 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7157 if (phba->intr_type == NONE) {
7158 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7159 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7160 if (!retval) {
7161 /* Indicate initialization to INTx mode */
7162 phba->intr_type = INTx;
7163 intr_mode = 0;
7164 for (index = 0; index < phba->cfg_fcp_eq_count;
7165 index++) {
7166 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7167 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7168 }
7169 }
7170 }
7171 return intr_mode;
7172}
7173
7174/**
7175 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7176 * @phba: pointer to lpfc hba data structure.
7177 *
7178 * This routine is invoked to disable device interrupt and disassociate
7179 * the driver's interrupt handler(s) from interrupt vector(s) to device
7180 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7181 * will release the interrupt vector(s) for the message signaled interrupt.
7182 **/
7183static void
7184lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7185{
7186 /* Disable the currently initialized interrupt mode */
7187 if (phba->intr_type == MSIX)
7188 lpfc_sli4_disable_msix(phba);
7189 else if (phba->intr_type == MSI)
7190 lpfc_sli4_disable_msi(phba);
7191 else if (phba->intr_type == INTx)
7192 free_irq(phba->pcidev->irq, phba);
7193
7194 /* Reset interrupt management states */
7195 phba->intr_type = NONE;
7196 phba->sli.slistat.sli_intr = 0;
7197
7198 return;
7199}
7200
7201/**
James Smart3772a992009-05-22 14:50:54 -04007202 * lpfc_unset_hba - Unset SLI3 hba device initialization
7203 * @phba: pointer to lpfc hba data structure.
7204 *
7205 * This routine is invoked to unset the HBA device initialization steps to
7206 * a device with SLI-3 interface spec.
7207 **/
7208static void
7209lpfc_unset_hba(struct lpfc_hba *phba)
7210{
7211 struct lpfc_vport *vport = phba->pport;
7212 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7213
7214 spin_lock_irq(shost->host_lock);
7215 vport->load_flag |= FC_UNLOADING;
7216 spin_unlock_irq(shost->host_lock);
7217
7218 lpfc_stop_hba_timers(phba);
7219
7220 phba->pport->work_port_events = 0;
7221
7222 lpfc_sli_hba_down(phba);
7223
7224 lpfc_sli_brdrestart(phba);
7225
7226 lpfc_sli_disable_intr(phba);
7227
7228 return;
7229}
7230
7231/**
James Smartda0436e2009-05-22 14:51:39 -04007232 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7233 * @phba: pointer to lpfc hba data structure.
7234 *
7235 * This routine is invoked to unset the HBA device initialization steps to
7236 * a device with SLI-4 interface spec.
7237 **/
7238static void
7239lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7240{
7241 struct lpfc_vport *vport = phba->pport;
7242 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7243
7244 spin_lock_irq(shost->host_lock);
7245 vport->load_flag |= FC_UNLOADING;
7246 spin_unlock_irq(shost->host_lock);
7247
7248 phba->pport->work_port_events = 0;
7249
7250 lpfc_sli4_hba_down(phba);
7251
7252 lpfc_sli4_disable_intr(phba);
7253
7254 return;
7255}
7256
7257/**
7258 * lpfc_sli4_hba_unset - Unset the fcoe hba
7259 * @phba: Pointer to HBA context object.
7260 *
7261 * This function is called in the SLI4 code path to reset the HBA's FCoE
7262 * function. The caller is not required to hold any lock. This routine
7263 * issues PCI function reset mailbox command to reset the FCoE function.
7264 * At the end of the function, it calls lpfc_hba_down_post function to
7265 * free any pending commands.
7266 **/
7267static void
7268lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7269{
7270 int wait_cnt = 0;
7271 LPFC_MBOXQ_t *mboxq;
7272
7273 lpfc_stop_hba_timers(phba);
7274 phba->sli4_hba.intr_enable = 0;
7275
7276 /*
7277 * Gracefully wait out the potential current outstanding asynchronous
7278 * mailbox command.
7279 */
7280
7281 /* First, block any pending async mailbox command from posted */
7282 spin_lock_irq(&phba->hbalock);
7283 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7284 spin_unlock_irq(&phba->hbalock);
7285 /* Now, trying to wait it out if we can */
7286 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7287 msleep(10);
7288 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7289 break;
7290 }
7291 /* Forcefully release the outstanding mailbox command if timed out */
7292 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7293 spin_lock_irq(&phba->hbalock);
7294 mboxq = phba->sli.mbox_active;
7295 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7296 __lpfc_mbox_cmpl_put(phba, mboxq);
7297 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7298 phba->sli.mbox_active = NULL;
7299 spin_unlock_irq(&phba->hbalock);
7300 }
7301
7302 /* Tear down the queues in the HBA */
7303 lpfc_sli4_queue_unset(phba);
7304
7305 /* Disable PCI subsystem interrupt */
7306 lpfc_sli4_disable_intr(phba);
7307
7308 /* Stop kthread signal shall trigger work_done one more time */
7309 kthread_stop(phba->worker_thread);
7310
7311 /* Stop the SLI4 device port */
7312 phba->pport->work_port_events = 0;
7313}
7314
James Smart28baac72010-02-12 14:42:03 -05007315 /**
7316 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7317 * @phba: Pointer to HBA context object.
7318 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7319 *
7320 * This function is called in the SLI4 code path to read the port's
7321 * sli4 capabilities.
7322 *
7323 * This function may be be called from any context that can block-wait
7324 * for the completion. The expectation is that this routine is called
7325 * typically from probe_one or from the online routine.
7326 **/
7327int
7328lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7329{
7330 int rc;
7331 struct lpfc_mqe *mqe;
7332 struct lpfc_pc_sli4_params *sli4_params;
7333 uint32_t mbox_tmo;
7334
7335 rc = 0;
7336 mqe = &mboxq->u.mqe;
7337
7338 /* Read the port's SLI4 Parameters port capabilities */
7339 lpfc_sli4_params(mboxq);
7340 if (!phba->sli4_hba.intr_enable)
7341 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7342 else {
7343 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7344 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7345 }
7346
7347 if (unlikely(rc))
7348 return 1;
7349
7350 sli4_params = &phba->sli4_hba.pc_sli4_params;
7351 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7352 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7353 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7354 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7355 &mqe->un.sli4_params);
7356 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7357 &mqe->un.sli4_params);
7358 sli4_params->proto_types = mqe->un.sli4_params.word3;
7359 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7360 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7361 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7362 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7363 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7364 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7365 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7366 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7367 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7368 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7369 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7370 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7371 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7372 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7373 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7374 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7375 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7376 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7377 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7378 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7379 return rc;
7380}
7381
James Smartda0436e2009-05-22 14:51:39 -04007382/**
James Smart3772a992009-05-22 14:50:54 -04007383 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7384 * @pdev: pointer to PCI device
7385 * @pid: pointer to PCI device identifier
7386 *
7387 * This routine is to be called to attach a device with SLI-3 interface spec
7388 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7389 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7390 * information of the device and driver to see if the driver state that it can
7391 * support this kind of device. If the match is successful, the driver core
7392 * invokes this routine. If this routine determines it can claim the HBA, it
7393 * does all the initialization that it needs to do to handle the HBA properly.
7394 *
7395 * Return code
7396 * 0 - driver can claim the device
7397 * negative value - driver can not claim the device
7398 **/
7399static int __devinit
7400lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7401{
7402 struct lpfc_hba *phba;
7403 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007404 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007405 int error;
7406 uint32_t cfg_mode, intr_mode;
7407
7408 /* Allocate memory for HBA structure */
7409 phba = lpfc_hba_alloc(pdev);
7410 if (!phba)
7411 return -ENOMEM;
7412
7413 /* Perform generic PCI device enabling operation */
7414 error = lpfc_enable_pci_dev(phba);
7415 if (error) {
7416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7417 "1401 Failed to enable pci device.\n");
7418 goto out_free_phba;
7419 }
7420
7421 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7422 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7423 if (error)
7424 goto out_disable_pci_dev;
7425
7426 /* Set up SLI-3 specific device PCI memory space */
7427 error = lpfc_sli_pci_mem_setup(phba);
7428 if (error) {
7429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7430 "1402 Failed to set up pci memory space.\n");
7431 goto out_disable_pci_dev;
7432 }
7433
7434 /* Set up phase-1 common device driver resources */
7435 error = lpfc_setup_driver_resource_phase1(phba);
7436 if (error) {
7437 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7438 "1403 Failed to set up driver resource.\n");
7439 goto out_unset_pci_mem_s3;
7440 }
7441
7442 /* Set up SLI-3 specific device driver resources */
7443 error = lpfc_sli_driver_resource_setup(phba);
7444 if (error) {
7445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7446 "1404 Failed to set up driver resource.\n");
7447 goto out_unset_pci_mem_s3;
7448 }
7449
7450 /* Initialize and populate the iocb list per host */
7451 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7452 if (error) {
7453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7454 "1405 Failed to initialize iocb list.\n");
7455 goto out_unset_driver_resource_s3;
7456 }
7457
7458 /* Set up common device driver resources */
7459 error = lpfc_setup_driver_resource_phase2(phba);
7460 if (error) {
7461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7462 "1406 Failed to set up driver resource.\n");
7463 goto out_free_iocb_list;
7464 }
7465
7466 /* Create SCSI host to the physical port */
7467 error = lpfc_create_shost(phba);
7468 if (error) {
7469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7470 "1407 Failed to create scsi host.\n");
7471 goto out_unset_driver_resource;
7472 }
7473
7474 /* Configure sysfs attributes */
7475 vport = phba->pport;
7476 error = lpfc_alloc_sysfs_attr(vport);
7477 if (error) {
7478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7479 "1476 Failed to allocate sysfs attr\n");
7480 goto out_destroy_shost;
7481 }
7482
James Smart6669f9b2009-10-02 15:16:45 -04007483 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04007484 /* Now, trying to enable interrupt and bring up the device */
7485 cfg_mode = phba->cfg_use_msi;
7486 while (true) {
7487 /* Put device to a known state before enabling interrupt */
7488 lpfc_stop_port(phba);
7489 /* Configure and enable interrupt */
7490 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7491 if (intr_mode == LPFC_INTR_ERROR) {
7492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7493 "0431 Failed to enable interrupt.\n");
7494 error = -ENODEV;
7495 goto out_free_sysfs_attr;
7496 }
7497 /* SLI-3 HBA setup */
7498 if (lpfc_sli_hba_setup(phba)) {
7499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7500 "1477 Failed to set up hba\n");
7501 error = -ENODEV;
7502 goto out_remove_device;
7503 }
7504
7505 /* Wait 50ms for the interrupts of previous mailbox commands */
7506 msleep(50);
7507 /* Check active interrupts on message signaled interrupts */
7508 if (intr_mode == 0 ||
7509 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7510 /* Log the current active interrupt mode */
7511 phba->intr_mode = intr_mode;
7512 lpfc_log_intr_mode(phba, intr_mode);
7513 break;
7514 } else {
7515 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7516 "0447 Configure interrupt mode (%d) "
7517 "failed active interrupt test.\n",
7518 intr_mode);
7519 /* Disable the current interrupt mode */
7520 lpfc_sli_disable_intr(phba);
7521 /* Try next level of interrupt mode */
7522 cfg_mode = --intr_mode;
7523 }
7524 }
7525
7526 /* Perform post initialization setup */
7527 lpfc_post_init_setup(phba);
7528
7529 /* Check if there are static vports to be created. */
7530 lpfc_create_static_vport(phba);
7531
7532 return 0;
7533
7534out_remove_device:
7535 lpfc_unset_hba(phba);
7536out_free_sysfs_attr:
7537 lpfc_free_sysfs_attr(vport);
7538out_destroy_shost:
7539 lpfc_destroy_shost(phba);
7540out_unset_driver_resource:
7541 lpfc_unset_driver_resource_phase2(phba);
7542out_free_iocb_list:
7543 lpfc_free_iocb_list(phba);
7544out_unset_driver_resource_s3:
7545 lpfc_sli_driver_resource_unset(phba);
7546out_unset_pci_mem_s3:
7547 lpfc_sli_pci_mem_unset(phba);
7548out_disable_pci_dev:
7549 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007550 if (shost)
7551 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007552out_free_phba:
7553 lpfc_hba_free(phba);
7554 return error;
7555}
7556
7557/**
7558 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007559 * @pdev: pointer to PCI device
7560 *
James Smart3772a992009-05-22 14:50:54 -04007561 * This routine is to be called to disattach a device with SLI-3 interface
7562 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7563 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7564 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007565 **/
dea31012005-04-17 16:05:31 -05007566static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007567lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007568{
James Smart2e0fef82007-06-17 19:56:36 -05007569 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7570 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007571 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007572 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007573 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007574 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7575
James Smart549e55c2007-08-02 11:09:51 -04007576 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007577 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007578 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007579
James Smart858c9f62007-06-17 19:56:39 -05007580 lpfc_free_sysfs_attr(vport);
7581
James Smarteada2722008-12-04 22:39:13 -05007582 /* Release all the vports against this physical port */
7583 vports = lpfc_create_vport_work_array(phba);
7584 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007585 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007586 fc_vport_terminate(vports[i]->fc_vport);
7587 lpfc_destroy_vport_work_array(phba, vports);
7588
7589 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007590 fc_remove_host(shost);
7591 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007592 lpfc_cleanup(vport);
7593
James Smart2e0fef82007-06-17 19:56:36 -05007594 /*
7595 * Bring down the SLI Layer. This step disable all interrupts,
7596 * clears the rings, discards all mailbox commands, and resets
7597 * the HBA.
7598 */
James Smarta257bf92009-04-06 18:48:10 -04007599
7600 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007601 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007602 /* Stop kthread signal shall trigger work_done one more time */
7603 kthread_stop(phba->worker_thread);
7604 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007605 lpfc_sli_brdrestart(phba);
7606
James Smart3772a992009-05-22 14:50:54 -04007607 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007608 spin_lock_irq(&phba->hbalock);
7609 list_del_init(&vport->listentry);
7610 spin_unlock_irq(&phba->hbalock);
7611
James Smart858c9f62007-06-17 19:56:39 -05007612 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007613
James Smart5b75da22008-12-04 22:39:35 -05007614 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007615 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007616
7617 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007618 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007619
7620 /*
7621 * Call scsi_free before mem_free since scsi bufs are released to their
7622 * corresponding pools here.
7623 */
7624 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007625 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007626
James Smart34b02dc2008-08-24 21:49:55 -04007627 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7628 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007629
James Smart2e0fef82007-06-17 19:56:36 -05007630 /* Free resources associated with SLI2 interface */
7631 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007632 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007633
7634 /* unmap adapter SLIM and Control Registers */
7635 iounmap(phba->ctrl_regs_memmap_p);
7636 iounmap(phba->slim_memmap_p);
7637
James Smart3772a992009-05-22 14:50:54 -04007638 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007639
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007640 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007641 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007642}
7643
Linas Vepstas8d63f372007-02-14 14:28:36 -06007644/**
James Smart3772a992009-05-22 14:50:54 -04007645 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007646 * @pdev: pointer to PCI device
7647 * @msg: power management message
7648 *
James Smart3772a992009-05-22 14:50:54 -04007649 * This routine is to be called from the kernel's PCI subsystem to support
7650 * system Power Management (PM) to device with SLI-3 interface spec. When
7651 * PM invokes this method, it quiesces the device by stopping the driver's
7652 * worker thread for the device, turning off device's interrupt and DMA,
7653 * and bring the device offline. Note that as the driver implements the
7654 * minimum PM requirements to a power-aware driver's PM support for the
7655 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7656 * to the suspend() method call will be treated as SUSPEND and the driver will
7657 * fully reinitialize its device during resume() method call, the driver will
7658 * set device to PCI_D3hot state in PCI config space instead of setting it
7659 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007660 *
7661 * Return code
James Smart3772a992009-05-22 14:50:54 -04007662 * 0 - driver suspended the device
7663 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007664 **/
7665static int
James Smart3772a992009-05-22 14:50:54 -04007666lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007667{
7668 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7669 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7670
7671 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7672 "0473 PCI device Power Management suspend.\n");
7673
7674 /* Bring down the device */
7675 lpfc_offline_prep(phba);
7676 lpfc_offline(phba);
7677 kthread_stop(phba->worker_thread);
7678
7679 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007680 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007681
7682 /* Save device state to PCI config space */
7683 pci_save_state(pdev);
7684 pci_set_power_state(pdev, PCI_D3hot);
7685
7686 return 0;
7687}
7688
7689/**
James Smart3772a992009-05-22 14:50:54 -04007690 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007691 * @pdev: pointer to PCI device
7692 *
James Smart3772a992009-05-22 14:50:54 -04007693 * This routine is to be called from the kernel's PCI subsystem to support
7694 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7695 * invokes this method, it restores the device's PCI config space state and
7696 * fully reinitializes the device and brings it online. Note that as the
7697 * driver implements the minimum PM requirements to a power-aware driver's
7698 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7699 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7700 * driver will fully reinitialize its device during resume() method call,
7701 * the device will be set to PCI_D0 directly in PCI config space before
7702 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007703 *
7704 * Return code
James Smart3772a992009-05-22 14:50:54 -04007705 * 0 - driver suspended the device
7706 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007707 **/
7708static int
James Smart3772a992009-05-22 14:50:54 -04007709lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007710{
7711 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7712 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007713 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007714 int error;
7715
7716 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7717 "0452 PCI device Power Management resume.\n");
7718
7719 /* Restore device state from PCI config space */
7720 pci_set_power_state(pdev, PCI_D0);
7721 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007722
James Smart1dfb5a42010-02-12 14:40:50 -05007723 /*
7724 * As the new kernel behavior of pci_restore_state() API call clears
7725 * device saved_state flag, need to save the restored state again.
7726 */
7727 pci_save_state(pdev);
7728
James Smart3a55b532008-12-04 22:38:54 -05007729 if (pdev->is_busmaster)
7730 pci_set_master(pdev);
7731
7732 /* Startup the kernel thread for this host adapter. */
7733 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7734 "lpfc_worker_%d", phba->brd_no);
7735 if (IS_ERR(phba->worker_thread)) {
7736 error = PTR_ERR(phba->worker_thread);
7737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7738 "0434 PM resume failed to start worker "
7739 "thread: error=x%x.\n", error);
7740 return error;
7741 }
7742
James Smart5b75da22008-12-04 22:39:35 -05007743 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007744 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007745 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007747 "0430 PM resume Failed to enable interrupt\n");
7748 return -EIO;
7749 } else
7750 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007751
7752 /* Restart HBA and bring it online */
7753 lpfc_sli_brdrestart(phba);
7754 lpfc_online(phba);
7755
James Smart5b75da22008-12-04 22:39:35 -05007756 /* Log the current active interrupt mode */
7757 lpfc_log_intr_mode(phba, phba->intr_mode);
7758
James Smart3a55b532008-12-04 22:38:54 -05007759 return 0;
7760}
7761
7762/**
James Smart891478a2009-11-18 15:40:23 -05007763 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7764 * @phba: pointer to lpfc hba data structure.
7765 *
7766 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04007767 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05007768 **/
7769static void
7770lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7771{
James Smarte2af0d22010-03-15 11:25:32 -04007772 struct lpfc_sli *psli = &phba->sli;
7773 struct lpfc_sli_ring *pring;
7774
James Smart891478a2009-11-18 15:40:23 -05007775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7776 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04007777
7778 /*
7779 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
7780 * and let the SCSI mid-layer to retry them to recover.
7781 */
7782 pring = &psli->ring[psli->fcp_ring];
7783 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05007784}
7785
7786/**
James Smart0d878412009-10-02 15:16:56 -04007787 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7788 * @phba: pointer to lpfc hba data structure.
7789 *
7790 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7791 * disables the device interrupt and pci device, and aborts the internal FCP
7792 * pending I/Os.
7793 **/
7794static void
7795lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7796{
James Smart0d878412009-10-02 15:16:56 -04007797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007798 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04007799
7800 /* Block all SCSI devices' I/Os on the host */
7801 lpfc_scsi_dev_block(phba);
7802
7803 /* stop all timers */
7804 lpfc_stop_hba_timers(phba);
7805
James Smart0d878412009-10-02 15:16:56 -04007806 /* Disable interrupt and pci device */
7807 lpfc_sli_disable_intr(phba);
7808 pci_disable_device(phba->pcidev);
James Smarte2af0d22010-03-15 11:25:32 -04007809 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
7810 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04007811}
7812
7813/**
7814 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7815 * @phba: pointer to lpfc hba data structure.
7816 *
7817 * This routine is called to prepare the SLI3 device for PCI slot permanently
7818 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7819 * pending I/Os.
7820 **/
7821static void
7822lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7823{
7824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007825 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04007826 /* Block all SCSI devices' I/Os on the host */
7827 lpfc_scsi_dev_block(phba);
7828
7829 /* stop all timers */
7830 lpfc_stop_hba_timers(phba);
7831
James Smart0d878412009-10-02 15:16:56 -04007832 /* Clean up all driver's outstanding SCSI I/Os */
7833 lpfc_sli_flush_fcp_rings(phba);
7834}
7835
7836/**
James Smart3772a992009-05-22 14:50:54 -04007837 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007838 * @pdev: pointer to PCI device.
7839 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007840 *
James Smart3772a992009-05-22 14:50:54 -04007841 * This routine is called from the PCI subsystem for I/O error handling to
7842 * device with SLI-3 interface spec. This function is called by the PCI
7843 * subsystem after a PCI bus error affecting this device has been detected.
7844 * When this function is invoked, it will need to stop all the I/Os and
7845 * interrupt(s) to the device. Once that is done, it will return
7846 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7847 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007848 *
7849 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007850 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007851 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7852 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007853 **/
James Smart3772a992009-05-22 14:50:54 -04007854static pci_ers_result_t
7855lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007856{
James Smart51ef4c22007-08-02 11:10:31 -04007857 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7858 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007859
James Smart0d878412009-10-02 15:16:56 -04007860 switch (state) {
7861 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007862 /* Non-fatal error, prepare for recovery */
7863 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007864 return PCI_ERS_RESULT_CAN_RECOVER;
7865 case pci_channel_io_frozen:
7866 /* Fatal error, prepare for slot reset */
7867 lpfc_sli_prep_dev_for_reset(phba);
7868 return PCI_ERS_RESULT_NEED_RESET;
7869 case pci_channel_io_perm_failure:
7870 /* Permanent failure, prepare for device down */
7871 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007872 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007873 default:
7874 /* Unknown state, prepare and request slot reset */
7875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7876 "0472 Unknown PCI error state: x%x\n", state);
7877 lpfc_sli_prep_dev_for_reset(phba);
7878 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007879 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007880}
7881
7882/**
James Smart3772a992009-05-22 14:50:54 -04007883 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007884 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007885 *
James Smart3772a992009-05-22 14:50:54 -04007886 * This routine is called from the PCI subsystem for error handling to
7887 * device with SLI-3 interface spec. This is called after PCI bus has been
7888 * reset to restart the PCI card from scratch, as if from a cold-boot.
7889 * During the PCI subsystem error recovery, after driver returns
7890 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7891 * recovery and then call this routine before calling the .resume method
7892 * to recover the device. This function will initialize the HBA device,
7893 * enable the interrupt, but it will just put the HBA to offline state
7894 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007895 *
7896 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007897 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7898 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007899 */
James Smart3772a992009-05-22 14:50:54 -04007900static pci_ers_result_t
7901lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007902{
James Smart51ef4c22007-08-02 11:10:31 -04007903 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7904 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007905 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007906 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007907
7908 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007909 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007910 printk(KERN_ERR "lpfc: Cannot re-enable "
7911 "PCI device after reset.\n");
7912 return PCI_ERS_RESULT_DISCONNECT;
7913 }
7914
James Smart97207482008-12-04 22:39:19 -05007915 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05007916
7917 /*
7918 * As the new kernel behavior of pci_restore_state() API call clears
7919 * device saved_state flag, need to save the restored state again.
7920 */
7921 pci_save_state(pdev);
7922
James Smart97207482008-12-04 22:39:19 -05007923 if (pdev->is_busmaster)
7924 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007925
James Smart92d7f7b2007-06-17 19:56:38 -05007926 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007927 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007928 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007929
James Smart5b75da22008-12-04 22:39:35 -05007930 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007931 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007932 if (intr_mode == LPFC_INTR_ERROR) {
7933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7934 "0427 Cannot re-enable interrupt after "
7935 "slot reset.\n");
7936 return PCI_ERS_RESULT_DISCONNECT;
7937 } else
7938 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007939
7940 /* Take device offline; this will perform cleanup */
7941 lpfc_offline(phba);
7942 lpfc_sli_brdrestart(phba);
7943
James Smart5b75da22008-12-04 22:39:35 -05007944 /* Log the current active interrupt mode */
7945 lpfc_log_intr_mode(phba, phba->intr_mode);
7946
Linas Vepstas8d63f372007-02-14 14:28:36 -06007947 return PCI_ERS_RESULT_RECOVERED;
7948}
7949
7950/**
James Smart3772a992009-05-22 14:50:54 -04007951 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007952 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007953 *
James Smart3772a992009-05-22 14:50:54 -04007954 * This routine is called from the PCI subsystem for error handling to device
7955 * with SLI-3 interface spec. It is called when kernel error recovery tells
7956 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7957 * error recovery. After this call, traffic can start to flow from this device
7958 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007959 */
James Smart3772a992009-05-22 14:50:54 -04007960static void
7961lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007962{
James Smart51ef4c22007-08-02 11:10:31 -04007963 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7964 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007965
James Smarte2af0d22010-03-15 11:25:32 -04007966 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04007967 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04007968
7969 /* Clean up Advanced Error Reporting (AER) if needed */
7970 if (phba->hba_flag & HBA_AER_ENABLED)
7971 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007972}
7973
James Smart3772a992009-05-22 14:50:54 -04007974/**
James Smartda0436e2009-05-22 14:51:39 -04007975 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7976 * @phba: pointer to lpfc hba data structure.
7977 *
7978 * returns the number of ELS/CT IOCBs to reserve
7979 **/
7980int
7981lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7982{
7983 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7984
James Smartf1126682009-06-10 17:22:44 -04007985 if (phba->sli_rev == LPFC_SLI_REV4) {
7986 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04007987 return 10;
James Smartf1126682009-06-10 17:22:44 -04007988 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04007989 return 25;
James Smartf1126682009-06-10 17:22:44 -04007990 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04007991 return 50;
James Smartf1126682009-06-10 17:22:44 -04007992 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04007993 return 100;
James Smartf1126682009-06-10 17:22:44 -04007994 else
James Smart6a9c52c2009-10-02 15:16:51 -04007995 return 150;
James Smartf1126682009-06-10 17:22:44 -04007996 } else
7997 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007998}
7999
8000/**
8001 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
8002 * @pdev: pointer to PCI device
8003 * @pid: pointer to PCI device identifier
8004 *
8005 * This routine is called from the kernel's PCI subsystem to device with
8006 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8007 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8008 * information of the device and driver to see if the driver state that it
8009 * can support this kind of device. If the match is successful, the driver
8010 * core invokes this routine. If this routine determines it can claim the HBA,
8011 * it does all the initialization that it needs to do to handle the HBA
8012 * properly.
8013 *
8014 * Return code
8015 * 0 - driver can claim the device
8016 * negative value - driver can not claim the device
8017 **/
8018static int __devinit
8019lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8020{
8021 struct lpfc_hba *phba;
8022 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008023 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008024 int error;
8025 uint32_t cfg_mode, intr_mode;
8026 int mcnt;
8027
8028 /* Allocate memory for HBA structure */
8029 phba = lpfc_hba_alloc(pdev);
8030 if (!phba)
8031 return -ENOMEM;
8032
8033 /* Perform generic PCI device enabling operation */
8034 error = lpfc_enable_pci_dev(phba);
8035 if (error) {
8036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8037 "1409 Failed to enable pci device.\n");
8038 goto out_free_phba;
8039 }
8040
8041 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
8042 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
8043 if (error)
8044 goto out_disable_pci_dev;
8045
8046 /* Set up SLI-4 specific device PCI memory space */
8047 error = lpfc_sli4_pci_mem_setup(phba);
8048 if (error) {
8049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8050 "1410 Failed to set up pci memory space.\n");
8051 goto out_disable_pci_dev;
8052 }
8053
8054 /* Set up phase-1 common device driver resources */
8055 error = lpfc_setup_driver_resource_phase1(phba);
8056 if (error) {
8057 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8058 "1411 Failed to set up driver resource.\n");
8059 goto out_unset_pci_mem_s4;
8060 }
8061
8062 /* Set up SLI-4 Specific device driver resources */
8063 error = lpfc_sli4_driver_resource_setup(phba);
8064 if (error) {
8065 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8066 "1412 Failed to set up driver resource.\n");
8067 goto out_unset_pci_mem_s4;
8068 }
8069
8070 /* Initialize and populate the iocb list per host */
8071 error = lpfc_init_iocb_list(phba,
8072 phba->sli4_hba.max_cfg_param.max_xri);
8073 if (error) {
8074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8075 "1413 Failed to initialize iocb list.\n");
8076 goto out_unset_driver_resource_s4;
8077 }
8078
8079 /* Set up common device driver resources */
8080 error = lpfc_setup_driver_resource_phase2(phba);
8081 if (error) {
8082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8083 "1414 Failed to set up driver resource.\n");
8084 goto out_free_iocb_list;
8085 }
8086
8087 /* Create SCSI host to the physical port */
8088 error = lpfc_create_shost(phba);
8089 if (error) {
8090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8091 "1415 Failed to create scsi host.\n");
8092 goto out_unset_driver_resource;
8093 }
8094
8095 /* Configure sysfs attributes */
8096 vport = phba->pport;
8097 error = lpfc_alloc_sysfs_attr(vport);
8098 if (error) {
8099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8100 "1416 Failed to allocate sysfs attr\n");
8101 goto out_destroy_shost;
8102 }
8103
James Smart6669f9b2009-10-02 15:16:45 -04008104 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04008105 /* Now, trying to enable interrupt and bring up the device */
8106 cfg_mode = phba->cfg_use_msi;
8107 while (true) {
8108 /* Put device to a known state before enabling interrupt */
8109 lpfc_stop_port(phba);
8110 /* Configure and enable interrupt */
8111 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
8112 if (intr_mode == LPFC_INTR_ERROR) {
8113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8114 "0426 Failed to enable interrupt.\n");
8115 error = -ENODEV;
8116 goto out_free_sysfs_attr;
8117 }
James Smartdef9c7a2009-12-21 17:02:28 -05008118 /* Default to single FCP EQ for non-MSI-X */
8119 if (phba->intr_type != MSIX)
8120 phba->cfg_fcp_eq_count = 1;
James Smartda0436e2009-05-22 14:51:39 -04008121 /* Set up SLI-4 HBA */
8122 if (lpfc_sli4_hba_setup(phba)) {
8123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8124 "1421 Failed to set up hba\n");
8125 error = -ENODEV;
8126 goto out_disable_intr;
8127 }
8128
8129 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
8130 if (intr_mode != 0)
8131 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
8132 LPFC_ACT_INTR_CNT);
8133
8134 /* Check active interrupts received only for MSI/MSI-X */
8135 if (intr_mode == 0 ||
8136 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
8137 /* Log the current active interrupt mode */
8138 phba->intr_mode = intr_mode;
8139 lpfc_log_intr_mode(phba, intr_mode);
8140 break;
8141 }
8142 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8143 "0451 Configure interrupt mode (%d) "
8144 "failed active interrupt test.\n",
8145 intr_mode);
8146 /* Unset the preivous SLI-4 HBA setup */
8147 lpfc_sli4_unset_hba(phba);
8148 /* Try next level of interrupt mode */
8149 cfg_mode = --intr_mode;
8150 }
8151
8152 /* Perform post initialization setup */
8153 lpfc_post_init_setup(phba);
8154
James Smart1c6834a2009-07-19 10:01:26 -04008155 /* Check if there are static vports to be created. */
8156 lpfc_create_static_vport(phba);
8157
James Smartda0436e2009-05-22 14:51:39 -04008158 return 0;
8159
8160out_disable_intr:
8161 lpfc_sli4_disable_intr(phba);
8162out_free_sysfs_attr:
8163 lpfc_free_sysfs_attr(vport);
8164out_destroy_shost:
8165 lpfc_destroy_shost(phba);
8166out_unset_driver_resource:
8167 lpfc_unset_driver_resource_phase2(phba);
8168out_free_iocb_list:
8169 lpfc_free_iocb_list(phba);
8170out_unset_driver_resource_s4:
8171 lpfc_sli4_driver_resource_unset(phba);
8172out_unset_pci_mem_s4:
8173 lpfc_sli4_pci_mem_unset(phba);
8174out_disable_pci_dev:
8175 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008176 if (shost)
8177 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04008178out_free_phba:
8179 lpfc_hba_free(phba);
8180 return error;
8181}
8182
8183/**
8184 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8185 * @pdev: pointer to PCI device
8186 *
8187 * This routine is called from the kernel's PCI subsystem to device with
8188 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8189 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8190 * device to be removed from the PCI subsystem properly.
8191 **/
8192static void __devexit
8193lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8194{
8195 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8196 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8197 struct lpfc_vport **vports;
8198 struct lpfc_hba *phba = vport->phba;
8199 int i;
8200
8201 /* Mark the device unloading flag */
8202 spin_lock_irq(&phba->hbalock);
8203 vport->load_flag |= FC_UNLOADING;
8204 spin_unlock_irq(&phba->hbalock);
8205
8206 /* Free the HBA sysfs attributes */
8207 lpfc_free_sysfs_attr(vport);
8208
8209 /* Release all the vports against this physical port */
8210 vports = lpfc_create_vport_work_array(phba);
8211 if (vports != NULL)
8212 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8213 fc_vport_terminate(vports[i]->fc_vport);
8214 lpfc_destroy_vport_work_array(phba, vports);
8215
8216 /* Remove FC host and then SCSI host with the physical port */
8217 fc_remove_host(shost);
8218 scsi_remove_host(shost);
8219
8220 /* Perform cleanup on the physical port */
8221 lpfc_cleanup(vport);
8222
8223 /*
8224 * Bring down the SLI Layer. This step disables all interrupts,
8225 * clears the rings, discards all mailbox commands, and resets
8226 * the HBA FCoE function.
8227 */
8228 lpfc_debugfs_terminate(vport);
8229 lpfc_sli4_hba_unset(phba);
8230
8231 spin_lock_irq(&phba->hbalock);
8232 list_del_init(&vport->listentry);
8233 spin_unlock_irq(&phba->hbalock);
8234
8235 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
8236 * buffers are released to their corresponding pools here.
8237 */
8238 lpfc_scsi_free(phba);
8239 lpfc_sli4_driver_resource_unset(phba);
8240
8241 /* Unmap adapter Control and Doorbell registers */
8242 lpfc_sli4_pci_mem_unset(phba);
8243
8244 /* Release PCI resources and disable device's PCI function */
8245 scsi_host_put(shost);
8246 lpfc_disable_pci_dev(phba);
8247
8248 /* Finally, free the driver's device data structure */
8249 lpfc_hba_free(phba);
8250
8251 return;
8252}
8253
8254/**
8255 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
8256 * @pdev: pointer to PCI device
8257 * @msg: power management message
8258 *
8259 * This routine is called from the kernel's PCI subsystem to support system
8260 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8261 * this method, it quiesces the device by stopping the driver's worker
8262 * thread for the device, turning off device's interrupt and DMA, and bring
8263 * the device offline. Note that as the driver implements the minimum PM
8264 * requirements to a power-aware driver's PM support for suspend/resume -- all
8265 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8266 * method call will be treated as SUSPEND and the driver will fully
8267 * reinitialize its device during resume() method call, the driver will set
8268 * device to PCI_D3hot state in PCI config space instead of setting it
8269 * according to the @msg provided by the PM.
8270 *
8271 * Return code
8272 * 0 - driver suspended the device
8273 * Error otherwise
8274 **/
8275static int
8276lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
8277{
8278 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8279 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8280
8281 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8282 "0298 PCI device Power Management suspend.\n");
8283
8284 /* Bring down the device */
8285 lpfc_offline_prep(phba);
8286 lpfc_offline(phba);
8287 kthread_stop(phba->worker_thread);
8288
8289 /* Disable interrupt from device */
8290 lpfc_sli4_disable_intr(phba);
8291
8292 /* Save device state to PCI config space */
8293 pci_save_state(pdev);
8294 pci_set_power_state(pdev, PCI_D3hot);
8295
8296 return 0;
8297}
8298
8299/**
8300 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8301 * @pdev: pointer to PCI device
8302 *
8303 * This routine is called from the kernel's PCI subsystem to support system
8304 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8305 * this method, it restores the device's PCI config space state and fully
8306 * reinitializes the device and brings it online. Note that as the driver
8307 * implements the minimum PM requirements to a power-aware driver's PM for
8308 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8309 * to the suspend() method call will be treated as SUSPEND and the driver
8310 * will fully reinitialize its device during resume() method call, the device
8311 * will be set to PCI_D0 directly in PCI config space before restoring the
8312 * state.
8313 *
8314 * Return code
8315 * 0 - driver suspended the device
8316 * Error otherwise
8317 **/
8318static int
8319lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8320{
8321 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8322 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8323 uint32_t intr_mode;
8324 int error;
8325
8326 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8327 "0292 PCI device Power Management resume.\n");
8328
8329 /* Restore device state from PCI config space */
8330 pci_set_power_state(pdev, PCI_D0);
8331 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008332
8333 /*
8334 * As the new kernel behavior of pci_restore_state() API call clears
8335 * device saved_state flag, need to save the restored state again.
8336 */
8337 pci_save_state(pdev);
8338
James Smartda0436e2009-05-22 14:51:39 -04008339 if (pdev->is_busmaster)
8340 pci_set_master(pdev);
8341
8342 /* Startup the kernel thread for this host adapter. */
8343 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8344 "lpfc_worker_%d", phba->brd_no);
8345 if (IS_ERR(phba->worker_thread)) {
8346 error = PTR_ERR(phba->worker_thread);
8347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8348 "0293 PM resume failed to start worker "
8349 "thread: error=x%x.\n", error);
8350 return error;
8351 }
8352
8353 /* Configure and enable interrupt */
8354 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8355 if (intr_mode == LPFC_INTR_ERROR) {
8356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8357 "0294 PM resume Failed to enable interrupt\n");
8358 return -EIO;
8359 } else
8360 phba->intr_mode = intr_mode;
8361
8362 /* Restart HBA and bring it online */
8363 lpfc_sli_brdrestart(phba);
8364 lpfc_online(phba);
8365
8366 /* Log the current active interrupt mode */
8367 lpfc_log_intr_mode(phba, phba->intr_mode);
8368
8369 return 0;
8370}
8371
8372/**
8373 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
8374 * @pdev: pointer to PCI device.
8375 * @state: the current PCI connection state.
8376 *
8377 * This routine is called from the PCI subsystem for error handling to device
8378 * with SLI-4 interface spec. This function is called by the PCI subsystem
8379 * after a PCI bus error affecting this device has been detected. When this
8380 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8381 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8382 * for the PCI subsystem to perform proper recovery as desired.
8383 *
8384 * Return codes
8385 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8386 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8387 **/
8388static pci_ers_result_t
8389lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8390{
8391 return PCI_ERS_RESULT_NEED_RESET;
8392}
8393
8394/**
8395 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
8396 * @pdev: pointer to PCI device.
8397 *
8398 * This routine is called from the PCI subsystem for error handling to device
8399 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8400 * restart the PCI card from scratch, as if from a cold-boot. During the
8401 * PCI subsystem error recovery, after the driver returns
8402 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8403 * recovery and then call this routine before calling the .resume method to
8404 * recover the device. This function will initialize the HBA device, enable
8405 * the interrupt, but it will just put the HBA to offline state without
8406 * passing any I/O traffic.
8407 *
8408 * Return codes
8409 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8410 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8411 */
8412static pci_ers_result_t
8413lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8414{
8415 return PCI_ERS_RESULT_RECOVERED;
8416}
8417
8418/**
8419 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8420 * @pdev: pointer to PCI device
8421 *
8422 * This routine is called from the PCI subsystem for error handling to device
8423 * with SLI-4 interface spec. It is called when kernel error recovery tells
8424 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8425 * error recovery. After this call, traffic can start to flow from this device
8426 * again.
8427 **/
8428static void
8429lpfc_io_resume_s4(struct pci_dev *pdev)
8430{
8431 return;
8432}
8433
8434/**
James Smart3772a992009-05-22 14:50:54 -04008435 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8436 * @pdev: pointer to PCI device
8437 * @pid: pointer to PCI device identifier
8438 *
8439 * This routine is to be registered to the kernel's PCI subsystem. When an
8440 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8441 * at PCI device-specific information of the device and driver to see if the
8442 * driver state that it can support this kind of device. If the match is
8443 * successful, the driver core invokes this routine. This routine dispatches
8444 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8445 * do all the initialization that it needs to do to handle the HBA device
8446 * properly.
8447 *
8448 * Return code
8449 * 0 - driver can claim the device
8450 * negative value - driver can not claim the device
8451 **/
8452static int __devinit
8453lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8454{
8455 int rc;
James Smart8fa38512009-07-19 10:01:03 -04008456 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04008457
James Smart28baac72010-02-12 14:42:03 -05008458 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04008459 return -ENODEV;
8460
James Smart8fa38512009-07-19 10:01:03 -04008461 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05008462 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04008463 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008464 else
James Smart3772a992009-05-22 14:50:54 -04008465 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008466
James Smart3772a992009-05-22 14:50:54 -04008467 return rc;
8468}
8469
8470/**
8471 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8472 * @pdev: pointer to PCI device
8473 *
8474 * This routine is to be registered to the kernel's PCI subsystem. When an
8475 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8476 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8477 * remove routine, which will perform all the necessary cleanup for the
8478 * device to be removed from the PCI subsystem properly.
8479 **/
8480static void __devexit
8481lpfc_pci_remove_one(struct pci_dev *pdev)
8482{
8483 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8484 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8485
8486 switch (phba->pci_dev_grp) {
8487 case LPFC_PCI_DEV_LP:
8488 lpfc_pci_remove_one_s3(pdev);
8489 break;
James Smartda0436e2009-05-22 14:51:39 -04008490 case LPFC_PCI_DEV_OC:
8491 lpfc_pci_remove_one_s4(pdev);
8492 break;
James Smart3772a992009-05-22 14:50:54 -04008493 default:
8494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8495 "1424 Invalid PCI device group: 0x%x\n",
8496 phba->pci_dev_grp);
8497 break;
8498 }
8499 return;
8500}
8501
8502/**
8503 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8504 * @pdev: pointer to PCI device
8505 * @msg: power management message
8506 *
8507 * This routine is to be registered to the kernel's PCI subsystem to support
8508 * system Power Management (PM). When PM invokes this method, it dispatches
8509 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8510 * suspend the device.
8511 *
8512 * Return code
8513 * 0 - driver suspended the device
8514 * Error otherwise
8515 **/
8516static int
8517lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8518{
8519 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8520 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8521 int rc = -ENODEV;
8522
8523 switch (phba->pci_dev_grp) {
8524 case LPFC_PCI_DEV_LP:
8525 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8526 break;
James Smartda0436e2009-05-22 14:51:39 -04008527 case LPFC_PCI_DEV_OC:
8528 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8529 break;
James Smart3772a992009-05-22 14:50:54 -04008530 default:
8531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8532 "1425 Invalid PCI device group: 0x%x\n",
8533 phba->pci_dev_grp);
8534 break;
8535 }
8536 return rc;
8537}
8538
8539/**
8540 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8541 * @pdev: pointer to PCI device
8542 *
8543 * This routine is to be registered to the kernel's PCI subsystem to support
8544 * system Power Management (PM). When PM invokes this method, it dispatches
8545 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8546 * resume the device.
8547 *
8548 * Return code
8549 * 0 - driver suspended the device
8550 * Error otherwise
8551 **/
8552static int
8553lpfc_pci_resume_one(struct pci_dev *pdev)
8554{
8555 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8556 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8557 int rc = -ENODEV;
8558
8559 switch (phba->pci_dev_grp) {
8560 case LPFC_PCI_DEV_LP:
8561 rc = lpfc_pci_resume_one_s3(pdev);
8562 break;
James Smartda0436e2009-05-22 14:51:39 -04008563 case LPFC_PCI_DEV_OC:
8564 rc = lpfc_pci_resume_one_s4(pdev);
8565 break;
James Smart3772a992009-05-22 14:50:54 -04008566 default:
8567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8568 "1426 Invalid PCI device group: 0x%x\n",
8569 phba->pci_dev_grp);
8570 break;
8571 }
8572 return rc;
8573}
8574
8575/**
8576 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8577 * @pdev: pointer to PCI device.
8578 * @state: the current PCI connection state.
8579 *
8580 * This routine is registered to the PCI subsystem for error handling. This
8581 * function is called by the PCI subsystem after a PCI bus error affecting
8582 * this device has been detected. When this routine is invoked, it dispatches
8583 * the action to the proper SLI-3 or SLI-4 device error detected handling
8584 * routine, which will perform the proper error detected operation.
8585 *
8586 * Return codes
8587 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8588 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8589 **/
8590static pci_ers_result_t
8591lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8592{
8593 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8594 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8595 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8596
8597 switch (phba->pci_dev_grp) {
8598 case LPFC_PCI_DEV_LP:
8599 rc = lpfc_io_error_detected_s3(pdev, state);
8600 break;
James Smartda0436e2009-05-22 14:51:39 -04008601 case LPFC_PCI_DEV_OC:
8602 rc = lpfc_io_error_detected_s4(pdev, state);
8603 break;
James Smart3772a992009-05-22 14:50:54 -04008604 default:
8605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8606 "1427 Invalid PCI device group: 0x%x\n",
8607 phba->pci_dev_grp);
8608 break;
8609 }
8610 return rc;
8611}
8612
8613/**
8614 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8615 * @pdev: pointer to PCI device.
8616 *
8617 * This routine is registered to the PCI subsystem for error handling. This
8618 * function is called after PCI bus has been reset to restart the PCI card
8619 * from scratch, as if from a cold-boot. When this routine is invoked, it
8620 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8621 * routine, which will perform the proper device reset.
8622 *
8623 * Return codes
8624 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8625 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8626 **/
8627static pci_ers_result_t
8628lpfc_io_slot_reset(struct pci_dev *pdev)
8629{
8630 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8631 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8632 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8633
8634 switch (phba->pci_dev_grp) {
8635 case LPFC_PCI_DEV_LP:
8636 rc = lpfc_io_slot_reset_s3(pdev);
8637 break;
James Smartda0436e2009-05-22 14:51:39 -04008638 case LPFC_PCI_DEV_OC:
8639 rc = lpfc_io_slot_reset_s4(pdev);
8640 break;
James Smart3772a992009-05-22 14:50:54 -04008641 default:
8642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8643 "1428 Invalid PCI device group: 0x%x\n",
8644 phba->pci_dev_grp);
8645 break;
8646 }
8647 return rc;
8648}
8649
8650/**
8651 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8652 * @pdev: pointer to PCI device
8653 *
8654 * This routine is registered to the PCI subsystem for error handling. It
8655 * is called when kernel error recovery tells the lpfc driver that it is
8656 * OK to resume normal PCI operation after PCI bus error recovery. When
8657 * this routine is invoked, it dispatches the action to the proper SLI-3
8658 * or SLI-4 device io_resume routine, which will resume the device operation.
8659 **/
8660static void
8661lpfc_io_resume(struct pci_dev *pdev)
8662{
8663 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8664 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8665
8666 switch (phba->pci_dev_grp) {
8667 case LPFC_PCI_DEV_LP:
8668 lpfc_io_resume_s3(pdev);
8669 break;
James Smartda0436e2009-05-22 14:51:39 -04008670 case LPFC_PCI_DEV_OC:
8671 lpfc_io_resume_s4(pdev);
8672 break;
James Smart3772a992009-05-22 14:50:54 -04008673 default:
8674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8675 "1429 Invalid PCI device group: 0x%x\n",
8676 phba->pci_dev_grp);
8677 break;
8678 }
8679 return;
8680}
8681
dea31012005-04-17 16:05:31 -05008682static struct pci_device_id lpfc_id_table[] = {
8683 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8684 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008685 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8686 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008687 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8688 PCI_ANY_ID, PCI_ANY_ID, },
8689 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8690 PCI_ANY_ID, PCI_ANY_ID, },
8691 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8692 PCI_ANY_ID, PCI_ANY_ID, },
8693 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8694 PCI_ANY_ID, PCI_ANY_ID, },
8695 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8696 PCI_ANY_ID, PCI_ANY_ID, },
8697 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8698 PCI_ANY_ID, PCI_ANY_ID, },
8699 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8700 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008701 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8702 PCI_ANY_ID, PCI_ANY_ID, },
8703 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8704 PCI_ANY_ID, PCI_ANY_ID, },
8705 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8706 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008707 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8708 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008709 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8710 PCI_ANY_ID, PCI_ANY_ID, },
8711 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8712 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008713 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8714 PCI_ANY_ID, PCI_ANY_ID, },
8715 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8716 PCI_ANY_ID, PCI_ANY_ID, },
8717 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8718 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008719 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8720 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008721 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8722 PCI_ANY_ID, PCI_ANY_ID, },
8723 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8724 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008725 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8726 PCI_ANY_ID, PCI_ANY_ID, },
8727 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8728 PCI_ANY_ID, PCI_ANY_ID, },
8729 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8730 PCI_ANY_ID, PCI_ANY_ID, },
8731 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8732 PCI_ANY_ID, PCI_ANY_ID, },
8733 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8734 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008735 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8736 PCI_ANY_ID, PCI_ANY_ID, },
8737 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8738 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008739 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8740 PCI_ANY_ID, PCI_ANY_ID, },
8741 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8742 PCI_ANY_ID, PCI_ANY_ID, },
8743 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8744 PCI_ANY_ID, PCI_ANY_ID, },
8745 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8746 PCI_ANY_ID, PCI_ANY_ID, },
8747 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8748 PCI_ANY_ID, PCI_ANY_ID, },
8749 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8750 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008751 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8752 PCI_ANY_ID, PCI_ANY_ID, },
8753 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8754 PCI_ANY_ID, PCI_ANY_ID, },
8755 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8756 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008757 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8758 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05008759 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8760 PCI_ANY_ID, PCI_ANY_ID, },
8761 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04008762 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008763 { 0 }
8764};
8765
8766MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8767
Linas Vepstas8d63f372007-02-14 14:28:36 -06008768static struct pci_error_handlers lpfc_err_handler = {
8769 .error_detected = lpfc_io_error_detected,
8770 .slot_reset = lpfc_io_slot_reset,
8771 .resume = lpfc_io_resume,
8772};
8773
dea31012005-04-17 16:05:31 -05008774static struct pci_driver lpfc_driver = {
8775 .name = LPFC_DRIVER_NAME,
8776 .id_table = lpfc_id_table,
8777 .probe = lpfc_pci_probe_one,
8778 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008779 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008780 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008781 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008782};
8783
James Smarte59058c2008-08-24 21:49:00 -04008784/**
James Smart3621a712009-04-06 18:47:14 -04008785 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008786 *
8787 * This routine is to be invoked when the lpfc module is loaded into the
8788 * kernel. The special kernel macro module_init() is used to indicate the
8789 * role of this routine to the kernel as lpfc module entry point.
8790 *
8791 * Return codes
8792 * 0 - successful
8793 * -ENOMEM - FC attach transport failed
8794 * all others - failed
8795 */
dea31012005-04-17 16:05:31 -05008796static int __init
8797lpfc_init(void)
8798{
8799 int error = 0;
8800
8801 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008802 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008803
James Smart7ee5d432007-10-27 13:37:17 -04008804 if (lpfc_enable_npiv) {
8805 lpfc_transport_functions.vport_create = lpfc_vport_create;
8806 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8807 }
dea31012005-04-17 16:05:31 -05008808 lpfc_transport_template =
8809 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008810 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008811 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008812 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008813 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008814 fc_attach_transport(&lpfc_vport_transport_functions);
8815 if (lpfc_vport_transport_template == NULL) {
8816 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008817 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008818 }
James Smart7ee5d432007-10-27 13:37:17 -04008819 }
dea31012005-04-17 16:05:31 -05008820 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008821 if (error) {
dea31012005-04-17 16:05:31 -05008822 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008823 if (lpfc_enable_npiv)
8824 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008825 }
dea31012005-04-17 16:05:31 -05008826
8827 return error;
8828}
8829
James Smarte59058c2008-08-24 21:49:00 -04008830/**
James Smart3621a712009-04-06 18:47:14 -04008831 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008832 *
8833 * This routine is invoked when the lpfc module is removed from the kernel.
8834 * The special kernel macro module_exit() is used to indicate the role of
8835 * this routine to the kernel as lpfc module exit point.
8836 */
dea31012005-04-17 16:05:31 -05008837static void __exit
8838lpfc_exit(void)
8839{
8840 pci_unregister_driver(&lpfc_driver);
8841 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008842 if (lpfc_enable_npiv)
8843 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008844 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008845 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8846 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008847 (1L << _dump_buf_data_order), _dump_buf_data);
8848 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8849 }
8850
8851 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008852 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8853 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008854 (1L << _dump_buf_dif_order), _dump_buf_dif);
8855 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8856 }
dea31012005-04-17 16:05:31 -05008857}
8858
8859module_init(lpfc_init);
8860module_exit(lpfc_exit);
8861MODULE_LICENSE("GPL");
8862MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8863MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8864MODULE_VERSION("0:" LPFC_DRIVER_VERSION);