blob: 053eaef0900577dcc1542bb7f3f85163ebdc04e8 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart4fede782010-01-26 23:08:55 -05004 * Copyright (C) 2004-2010 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040031#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050033
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040034#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050035#include <scsi/scsi_device.h>
36#include <scsi/scsi_host.h>
37#include <scsi/scsi_transport_fc.h>
38
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_hw.h"
41#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040043#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_disc.h"
45#include "lpfc_scsi.h"
46#include "lpfc.h"
47#include "lpfc_logmsg.h"
48#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050049#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050050#include "lpfc_version.h"
51
James Smart81301a92008-12-04 22:39:46 -050052char *_dump_buf_data;
53unsigned long _dump_buf_data_order;
54char *_dump_buf_dif;
55unsigned long _dump_buf_dif_order;
56spinlock_t _dump_buf_lock;
57
dea31012005-04-17 16:05:31 -050058static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
59static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040060static int lpfc_sli4_queue_create(struct lpfc_hba *);
61static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
62static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
63static int lpfc_setup_endian_order(struct lpfc_hba *);
64static int lpfc_sli4_read_config(struct lpfc_hba *);
65static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
66static void lpfc_free_sgl_list(struct lpfc_hba *);
67static int lpfc_init_sgl_list(struct lpfc_hba *);
68static int lpfc_init_active_sgl_array(struct lpfc_hba *);
69static void lpfc_free_active_sgl(struct lpfc_hba *);
70static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
71static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
72static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
73static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
74static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050075
76static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050077static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050078static DEFINE_IDR(lpfc_hba_index);
79
James Smarte59058c2008-08-24 21:49:00 -040080/**
James Smart3621a712009-04-06 18:47:14 -040081 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040082 * @phba: pointer to lpfc hba data structure.
83 *
84 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
85 * mailbox command. It retrieves the revision information from the HBA and
86 * collects the Vital Product Data (VPD) about the HBA for preparing the
87 * configuration of the HBA.
88 *
89 * Return codes:
90 * 0 - success.
91 * -ERESTART - requests the SLI layer to reset the HBA and try again.
92 * Any other value - indicates an error.
93 **/
dea31012005-04-17 16:05:31 -050094int
James Smart2e0fef82007-06-17 19:56:36 -050095lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050096{
97 lpfc_vpd_t *vp = &phba->vpd;
98 int i = 0, rc;
99 LPFC_MBOXQ_t *pmb;
100 MAILBOX_t *mb;
101 char *lpfc_vpd_data = NULL;
102 uint16_t offset = 0;
103 static char licensed[56] =
104 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400105 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500106
107 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
108 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500109 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500110 return -ENOMEM;
111 }
112
James Smart04c68492009-05-22 14:52:52 -0400113 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500114 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500115
116 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400117 if (init_key) {
118 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500119
James Smart65a29c12006-07-06 15:50:50 -0400120 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
121 *ptext = cpu_to_be32(*ptext);
122 init_key = 0;
123 }
dea31012005-04-17 16:05:31 -0500124
125 lpfc_read_nv(phba, pmb);
126 memset((char*)mb->un.varRDnvp.rsvd3, 0,
127 sizeof (mb->un.varRDnvp.rsvd3));
128 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
129 sizeof (licensed));
130
131 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
132
133 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500134 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400135 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500136 "error, mbxCmd x%x READ_NVPARM, "
137 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500138 mb->mbxCommand, mb->mbxStatus);
139 mempool_free(pmb, phba->mbox_mem_pool);
140 return -ERESTART;
141 }
142 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500143 sizeof(phba->wwnn));
144 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
145 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500146 }
147
James Smart92d7f7b2007-06-17 19:56:38 -0500148 phba->sli3_options = 0x0;
149
dea31012005-04-17 16:05:31 -0500150 /* Setup and issue mailbox READ REV command */
151 lpfc_read_rev(phba, pmb);
152 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
153 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400155 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500156 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500157 mb->mbxCommand, mb->mbxStatus);
158 mempool_free( pmb, phba->mbox_mem_pool);
159 return -ERESTART;
160 }
161
James Smart92d7f7b2007-06-17 19:56:38 -0500162
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500163 /*
164 * The value of rr must be 1 since the driver set the cv field to 1.
165 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500166 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500167 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500168 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400170 "0440 Adapter failed to init, READ_REV has "
171 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500172 mempool_free(pmb, phba->mbox_mem_pool);
173 return -ERESTART;
dea31012005-04-17 16:05:31 -0500174 }
175
James Smart495a7142008-06-14 22:52:59 -0400176 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
177 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500178 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400179 }
James Smarted957682007-06-17 19:56:37 -0500180
dea31012005-04-17 16:05:31 -0500181 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500182 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500183 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500184 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
185 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
186 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
187 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500188 vp->rev.biuRev = mb->un.varRdRev.biuRev;
189 vp->rev.smRev = mb->un.varRdRev.smRev;
190 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
191 vp->rev.endecRev = mb->un.varRdRev.endecRev;
192 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
193 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
194 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
195 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
196 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
197 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
198
James Smart92d7f7b2007-06-17 19:56:38 -0500199 /* If the sli feature level is less then 9, we must
200 * tear down all RPIs and VPIs on link down if NPIV
201 * is enabled.
202 */
203 if (vp->rev.feaLevelHigh < 9)
204 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
205
dea31012005-04-17 16:05:31 -0500206 if (lpfc_is_LC_HBA(phba->pcidev->device))
207 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
208 sizeof (phba->RandomData));
209
dea31012005-04-17 16:05:31 -0500210 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500211 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
212 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400213 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500214
215 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400216 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500217 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
218
219 if (rc != MBX_SUCCESS) {
220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400221 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500222 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500223 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500224 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500225 }
James Smart04c68492009-05-22 14:52:52 -0400226 /* dump mem may return a zero when finished or we got a
227 * mailbox error, either way we are done.
228 */
229 if (mb->un.varDmp.word_cnt == 0)
230 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500231 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
232 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400233 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
234 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500235 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500236 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500237 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
238 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500239
240 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500241out_free_mbox:
242 mempool_free(pmb, phba->mbox_mem_pool);
243 return 0;
244}
245
James Smarte59058c2008-08-24 21:49:00 -0400246/**
James Smart3621a712009-04-06 18:47:14 -0400247 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400248 * @phba: pointer to lpfc hba data structure.
249 * @pmboxq: pointer to the driver internal queue element for mailbox command.
250 *
251 * This is the completion handler for driver's configuring asynchronous event
252 * mailbox command to the device. If the mailbox command returns successfully,
253 * it will set internal async event support flag to 1; otherwise, it will
254 * set internal async event support flag to 0.
255 **/
James Smart57127f12007-10-27 13:37:05 -0400256static void
257lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
258{
James Smart04c68492009-05-22 14:52:52 -0400259 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400260 phba->temp_sensor_support = 1;
261 else
262 phba->temp_sensor_support = 0;
263 mempool_free(pmboxq, phba->mbox_mem_pool);
264 return;
265}
266
James Smarte59058c2008-08-24 21:49:00 -0400267/**
James Smart3621a712009-04-06 18:47:14 -0400268 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500269 * @phba: pointer to lpfc hba data structure.
270 * @pmboxq: pointer to the driver internal queue element for mailbox command.
271 *
272 * This is the completion handler for dump mailbox command for getting
273 * wake up parameters. When this command complete, the response contain
274 * Option rom version of the HBA. This function translate the version number
275 * into a human readable string and store it in OptionROMVersion.
276 **/
277static void
278lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
279{
280 struct prog_id *prg;
281 uint32_t prog_id_word;
282 char dist = ' ';
283 /* character array used for decoding dist type. */
284 char dist_char[] = "nabx";
285
James Smart04c68492009-05-22 14:52:52 -0400286 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500287 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500288 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500289 }
James Smart97207482008-12-04 22:39:19 -0500290
291 prg = (struct prog_id *) &prog_id_word;
292
293 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400294 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500295
296 /* Decode the Option rom version word to a readable string */
297 if (prg->dist < 4)
298 dist = dist_char[prg->dist];
299
300 if ((prg->dist == 3) && (prg->num == 0))
301 sprintf(phba->OptionROMVersion, "%d.%d%d",
302 prg->ver, prg->rev, prg->lev);
303 else
304 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
305 prg->ver, prg->rev, prg->lev,
306 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500307 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500308 return;
309}
310
311/**
James Smart3621a712009-04-06 18:47:14 -0400312 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400313 * @phba: pointer to lpfc hba data structure.
314 *
315 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
316 * command call. It performs all internal resource and state setups on the
317 * port: post IOCB buffers, enable appropriate host interrupt attentions,
318 * ELS ring timers, etc.
319 *
320 * Return codes
321 * 0 - success.
322 * Any other value - error.
323 **/
dea31012005-04-17 16:05:31 -0500324int
James Smart2e0fef82007-06-17 19:56:36 -0500325lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500326{
James Smart2e0fef82007-06-17 19:56:36 -0500327 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400328 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500329 LPFC_MBOXQ_t *pmb;
330 MAILBOX_t *mb;
331 struct lpfc_dmabuf *mp;
332 struct lpfc_sli *psli = &phba->sli;
333 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500334 int i, j;
335 int rc;
dea31012005-04-17 16:05:31 -0500336
James Smart7af67052007-10-27 13:38:11 -0400337 spin_lock_irq(&phba->hbalock);
338 /*
339 * If the Config port completed correctly the HBA is not
340 * over heated any more.
341 */
342 if (phba->over_temp_state == HBA_OVER_TEMP)
343 phba->over_temp_state = HBA_NORMAL_TEMP;
344 spin_unlock_irq(&phba->hbalock);
345
dea31012005-04-17 16:05:31 -0500346 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
347 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500348 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500349 return -ENOMEM;
350 }
James Smart04c68492009-05-22 14:52:52 -0400351 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500352
dea31012005-04-17 16:05:31 -0500353 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500354 rc = lpfc_read_sparam(phba, pmb, 0);
355 if (rc) {
356 mempool_free(pmb, phba->mbox_mem_pool);
357 return -ENOMEM;
358 }
359
James Smarted957682007-06-17 19:56:37 -0500360 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500361 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400363 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500364 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500365 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500366 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500367 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500368 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500369 lpfc_mbuf_free(phba, mp->virt, mp->phys);
370 kfree(mp);
371 return -EIO;
372 }
373
374 mp = (struct lpfc_dmabuf *) pmb->context1;
375
James Smart2e0fef82007-06-17 19:56:36 -0500376 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500377 lpfc_mbuf_free(phba, mp->virt, mp->phys);
378 kfree(mp);
379 pmb->context1 = NULL;
380
James Smarta12e07b2006-12-02 13:35:30 -0500381 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500382 u64_to_wwn(phba->cfg_soft_wwnn,
383 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400384 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500385 u64_to_wwn(phba->cfg_soft_wwpn,
386 vport->fc_sparam.portName.u.wwn);
387 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500388 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500389 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500390 sizeof (struct lpfc_name));
James Smarta257bf92009-04-06 18:48:10 -0400391
392 /* Update the fc_host data structures with new wwn. */
393 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
394 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400395 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400396
dea31012005-04-17 16:05:31 -0500397 /* If no serial number in VPD data, use low 6 bytes of WWNN */
398 /* This should be consolidated into parse_vpd ? - mr */
399 if (phba->SerialNumber[0] == 0) {
400 uint8_t *outptr;
401
James Smart2e0fef82007-06-17 19:56:36 -0500402 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500403 for (i = 0; i < 12; i++) {
404 status = *outptr++;
405 j = ((status & 0xf0) >> 4);
406 if (j <= 9)
407 phba->SerialNumber[i] =
408 (char)((uint8_t) 0x30 + (uint8_t) j);
409 else
410 phba->SerialNumber[i] =
411 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
412 i++;
413 j = (status & 0xf);
414 if (j <= 9)
415 phba->SerialNumber[i] =
416 (char)((uint8_t) 0x30 + (uint8_t) j);
417 else
418 phba->SerialNumber[i] =
419 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
420 }
421 }
422
dea31012005-04-17 16:05:31 -0500423 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500424 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500425 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400427 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500428 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500429 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500430 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500431 mempool_free( pmb, phba->mbox_mem_pool);
432 return -EIO;
433 }
434
James Smarta0c87cb2009-07-19 10:01:10 -0400435 /* Check if the port is disabled */
436 lpfc_sli_read_link_ste(phba);
437
dea31012005-04-17 16:05:31 -0500438 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
439 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
440 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400441 (mb->un.varRdConfig.max_xri + 1) -
442 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500443
444 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500445
446 /* Get the default values for Model Name and Description */
447 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
448
449 if ((phba->cfg_link_speed > LINK_SPEED_10G)
450 || ((phba->cfg_link_speed == LINK_SPEED_1G)
451 && !(phba->lmt & LMT_1Gb))
452 || ((phba->cfg_link_speed == LINK_SPEED_2G)
453 && !(phba->lmt & LMT_2Gb))
454 || ((phba->cfg_link_speed == LINK_SPEED_4G)
455 && !(phba->lmt & LMT_4Gb))
456 || ((phba->cfg_link_speed == LINK_SPEED_8G)
457 && !(phba->lmt & LMT_8Gb))
458 || ((phba->cfg_link_speed == LINK_SPEED_10G)
459 && !(phba->lmt & LMT_10Gb))) {
460 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500461 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400462 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500463 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500464 phba->cfg_link_speed);
465 phba->cfg_link_speed = LINK_SPEED_AUTO;
466 }
467
James Smart2e0fef82007-06-17 19:56:36 -0500468 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500469
James Smart0b727fe2007-10-27 13:37:25 -0400470 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500471 if (psli->ring[psli->extra_ring].cmdringaddr)
472 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500473 if (psli->ring[psli->fcp_ring].cmdringaddr)
474 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
475 if (psli->ring[psli->next_ring].cmdringaddr)
476 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
477
478 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500479 if (phba->sli_rev != 3)
480 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500481
James Smart93996272008-08-24 21:50:30 -0400482 /*
483 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
484 */
485 if (phba->intr_type == MSIX) {
486 rc = lpfc_config_msi(phba, pmb);
487 if (rc) {
488 mempool_free(pmb, phba->mbox_mem_pool);
489 return -EIO;
490 }
491 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
492 if (rc != MBX_SUCCESS) {
493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
494 "0352 Config MSI mailbox command "
495 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400496 pmb->u.mb.mbxCommand,
497 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400498 mempool_free(pmb, phba->mbox_mem_pool);
499 return -EIO;
500 }
501 }
502
James Smart04c68492009-05-22 14:52:52 -0400503 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400504 /* Initialize ERATT handling flag */
505 phba->hba_flag &= ~HBA_ERATT_HANDLED;
506
dea31012005-04-17 16:05:31 -0500507 /* Enable appropriate host interrupts */
dea31012005-04-17 16:05:31 -0500508 status = readl(phba->HCregaddr);
509 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
510 if (psli->num_rings > 0)
511 status |= HC_R0INT_ENA;
512 if (psli->num_rings > 1)
513 status |= HC_R1INT_ENA;
514 if (psli->num_rings > 2)
515 status |= HC_R2INT_ENA;
516 if (psli->num_rings > 3)
517 status |= HC_R3INT_ENA;
518
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500519 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
520 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400521 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500522
dea31012005-04-17 16:05:31 -0500523 writel(status, phba->HCregaddr);
524 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500525 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500526
James Smart93996272008-08-24 21:50:30 -0400527 /* Set up ring-0 (ELS) timer */
528 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500529 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400530 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500531 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
532 phba->hb_outstanding = 0;
533 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400534 /* Set up error attention (ERATT) polling timer */
535 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500536
James Smarta0c87cb2009-07-19 10:01:10 -0400537 if (phba->hba_flag & LINK_DISABLED) {
538 lpfc_printf_log(phba,
539 KERN_ERR, LOG_INIT,
540 "2598 Adapter Link is disabled.\n");
541 lpfc_down_link(phba, pmb);
542 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
543 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
544 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
545 lpfc_printf_log(phba,
546 KERN_ERR, LOG_INIT,
547 "2599 Adapter failed to issue DOWN_LINK"
548 " mbox command rc 0x%x\n", rc);
549
550 mempool_free(pmb, phba->mbox_mem_pool);
551 return -EIO;
552 }
James Smarte40a02c2010-02-26 14:13:54 -0500553 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smarta0c87cb2009-07-19 10:01:10 -0400554 lpfc_init_link(phba, pmb, phba->cfg_topology,
555 phba->cfg_link_speed);
556 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
557 lpfc_set_loopback_flag(phba);
558 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
559 if (rc != MBX_SUCCESS) {
560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400561 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500562 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500563 mb->mbxCommand, mb->mbxStatus);
564
James Smarta0c87cb2009-07-19 10:01:10 -0400565 /* Clear all interrupt enable conditions */
566 writel(0, phba->HCregaddr);
567 readl(phba->HCregaddr); /* flush */
568 /* Clear all pending interrupts */
569 writel(0xffffffff, phba->HAregaddr);
570 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500571
James Smarta0c87cb2009-07-19 10:01:10 -0400572 phba->link_state = LPFC_HBA_ERROR;
573 if (rc != MBX_BUSY)
574 mempool_free(pmb, phba->mbox_mem_pool);
575 return -EIO;
576 }
dea31012005-04-17 16:05:31 -0500577 }
578 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400579 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500580 if (!pmb) {
581 phba->link_state = LPFC_HBA_ERROR;
582 return -ENOMEM;
583 }
584
James Smart57127f12007-10-27 13:37:05 -0400585 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
586 pmb->mbox_cmpl = lpfc_config_async_cmpl;
587 pmb->vport = phba->pport;
588 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500589
James Smart57127f12007-10-27 13:37:05 -0400590 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
591 lpfc_printf_log(phba,
592 KERN_ERR,
593 LOG_INIT,
594 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400595 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400596 rc);
597 mempool_free(pmb, phba->mbox_mem_pool);
598 }
James Smart97207482008-12-04 22:39:19 -0500599
600 /* Get Option rom version */
601 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500602 if (!pmb) {
603 phba->link_state = LPFC_HBA_ERROR;
604 return -ENOMEM;
605 }
606
James Smart97207482008-12-04 22:39:19 -0500607 lpfc_dump_wakeup_param(phba, pmb);
608 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
609 pmb->vport = phba->pport;
610 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
611
612 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400614 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500615 mempool_free(pmb, phba->mbox_mem_pool);
616 }
617
James Smartd7c255b2008-08-24 21:50:00 -0400618 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400619}
620
James Smarte59058c2008-08-24 21:49:00 -0400621/**
James Smart84d1b002010-02-12 14:42:33 -0500622 * lpfc_hba_init_link - Initialize the FC link
623 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400624 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500625 *
626 * This routine will issue the INIT_LINK mailbox command call.
627 * It is available to other drivers through the lpfc_hba data
628 * structure for use as a delayed link up mechanism with the
629 * module parameter lpfc_suppress_link_up.
630 *
631 * Return code
632 * 0 - success
633 * Any other value - error
634 **/
635int
James Smart6e7288d2010-06-07 15:23:35 -0400636lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500637{
638 struct lpfc_vport *vport = phba->pport;
639 LPFC_MBOXQ_t *pmb;
640 MAILBOX_t *mb;
641 int rc;
642
643 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
644 if (!pmb) {
645 phba->link_state = LPFC_HBA_ERROR;
646 return -ENOMEM;
647 }
648 mb = &pmb->u.mb;
649 pmb->vport = vport;
650
651 lpfc_init_link(phba, pmb, phba->cfg_topology,
652 phba->cfg_link_speed);
653 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
654 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400655 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500656 if (rc != MBX_SUCCESS) {
657 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
658 "0498 Adapter failed to init, mbxCmd x%x "
659 "INIT_LINK, mbxStatus x%x\n",
660 mb->mbxCommand, mb->mbxStatus);
661 /* Clear all interrupt enable conditions */
662 writel(0, phba->HCregaddr);
663 readl(phba->HCregaddr); /* flush */
664 /* Clear all pending interrupts */
665 writel(0xffffffff, phba->HAregaddr);
666 readl(phba->HAregaddr); /* flush */
667 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400668 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500669 mempool_free(pmb, phba->mbox_mem_pool);
670 return -EIO;
671 }
James Smarte40a02c2010-02-26 14:13:54 -0500672 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400673 if (flag == MBX_POLL)
674 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500675
676 return 0;
677}
678
679/**
680 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400681 * @phba: pointer to lpfc hba data structure.
682 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500683 *
684 * This routine will issue the DOWN_LINK mailbox command call.
685 * It is available to other drivers through the lpfc_hba data
686 * structure for use to stop the link.
687 *
688 * Return code
689 * 0 - success
690 * Any other value - error
691 **/
692int
James Smart6e7288d2010-06-07 15:23:35 -0400693lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500694{
695 LPFC_MBOXQ_t *pmb;
696 int rc;
697
698 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
699 if (!pmb) {
700 phba->link_state = LPFC_HBA_ERROR;
701 return -ENOMEM;
702 }
703
704 lpfc_printf_log(phba,
705 KERN_ERR, LOG_INIT,
706 "0491 Adapter Link is disabled.\n");
707 lpfc_down_link(phba, pmb);
708 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400709 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500710 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
711 lpfc_printf_log(phba,
712 KERN_ERR, LOG_INIT,
713 "2522 Adapter failed to issue DOWN_LINK"
714 " mbox command rc 0x%x\n", rc);
715
716 mempool_free(pmb, phba->mbox_mem_pool);
717 return -EIO;
718 }
James Smart6e7288d2010-06-07 15:23:35 -0400719 if (flag == MBX_POLL)
720 mempool_free(pmb, phba->mbox_mem_pool);
721
James Smart84d1b002010-02-12 14:42:33 -0500722 return 0;
723}
724
725/**
James Smart3621a712009-04-06 18:47:14 -0400726 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400727 * @phba: pointer to lpfc HBA data structure.
728 *
729 * This routine will do LPFC uninitialization before the HBA is reset when
730 * bringing down the SLI Layer.
731 *
732 * Return codes
733 * 0 - success.
734 * Any other value - error.
735 **/
dea31012005-04-17 16:05:31 -0500736int
James Smart2e0fef82007-06-17 19:56:36 -0500737lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500738{
James Smart1b32f6a2008-02-08 18:49:39 -0500739 struct lpfc_vport **vports;
740 int i;
James Smart3772a992009-05-22 14:50:54 -0400741
742 if (phba->sli_rev <= LPFC_SLI_REV3) {
743 /* Disable interrupts */
744 writel(0, phba->HCregaddr);
745 readl(phba->HCregaddr); /* flush */
746 }
dea31012005-04-17 16:05:31 -0500747
James Smart1b32f6a2008-02-08 18:49:39 -0500748 if (phba->pport->load_flag & FC_UNLOADING)
749 lpfc_cleanup_discovery_resources(phba->pport);
750 else {
751 vports = lpfc_create_vport_work_array(phba);
752 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400753 for (i = 0; i <= phba->max_vports &&
754 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500755 lpfc_cleanup_discovery_resources(vports[i]);
756 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500757 }
758 return 0;
dea31012005-04-17 16:05:31 -0500759}
760
James Smarte59058c2008-08-24 21:49:00 -0400761/**
James Smart3772a992009-05-22 14:50:54 -0400762 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400763 * @phba: pointer to lpfc HBA data structure.
764 *
765 * This routine will do uninitialization after the HBA is reset when bring
766 * down the SLI Layer.
767 *
768 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200769 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400770 * Any other value - error.
771 **/
James Smart3772a992009-05-22 14:50:54 -0400772static int
773lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500774{
775 struct lpfc_sli *psli = &phba->sli;
776 struct lpfc_sli_ring *pring;
777 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500778 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500779 int i;
780
James Smart92d7f7b2007-06-17 19:56:38 -0500781 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
782 lpfc_sli_hbqbuf_free_all(phba);
783 else {
784 /* Cleanup preposted buffers on the ELS ring */
785 pring = &psli->ring[LPFC_ELS_RING];
786 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
787 list_del(&mp->list);
788 pring->postbufq_cnt--;
789 lpfc_mbuf_free(phba, mp->virt, mp->phys);
790 kfree(mp);
791 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500792 }
793
James Smart09372822008-01-11 01:52:54 -0500794 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500795 for (i = 0; i < psli->num_rings; i++) {
796 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500797
798 /* At this point in time the HBA is either reset or DOA. Either
799 * way, nothing should be on txcmplq as it will NEVER complete.
800 */
801 list_splice_init(&pring->txcmplq, &completions);
802 pring->txcmplq_cnt = 0;
803 spin_unlock_irq(&phba->hbalock);
804
James Smarta257bf92009-04-06 18:48:10 -0400805 /* Cancel all the IOCBs from the completions list */
806 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
807 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500808
Jamie Wellnitz41415862006-02-28 19:25:27 -0500809 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500810 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500811 }
James Smart09372822008-01-11 01:52:54 -0500812 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500813
814 return 0;
815}
James Smartda0436e2009-05-22 14:51:39 -0400816/**
817 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
818 * @phba: pointer to lpfc HBA data structure.
819 *
820 * This routine will do uninitialization after the HBA is reset when bring
821 * down the SLI Layer.
822 *
823 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200824 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400825 * Any other value - error.
826 **/
827static int
828lpfc_hba_down_post_s4(struct lpfc_hba *phba)
829{
830 struct lpfc_scsi_buf *psb, *psb_next;
831 LIST_HEAD(aborts);
832 int ret;
833 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500834 struct lpfc_sglq *sglq_entry = NULL;
835
James Smartda0436e2009-05-22 14:51:39 -0400836 ret = lpfc_hba_down_post_s3(phba);
837 if (ret)
838 return ret;
839 /* At this point in time the HBA is either reset or DOA. Either
840 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
841 * on the lpfc_sgl_list so that it can either be freed if the
842 * driver is unloading or reposted if the driver is restarting
843 * the port.
844 */
845 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
846 /* scsl_buf_list */
847 /* abts_sgl_list_lock required because worker thread uses this
848 * list.
849 */
850 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500851 list_for_each_entry(sglq_entry,
852 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
853 sglq_entry->state = SGL_FREED;
854
James Smartda0436e2009-05-22 14:51:39 -0400855 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
856 &phba->sli4_hba.lpfc_sgl_list);
857 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
858 /* abts_scsi_buf_list_lock required because worker thread uses this
859 * list.
860 */
861 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
862 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
863 &aborts);
864 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
865 spin_unlock_irq(&phba->hbalock);
866
867 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
868 psb->pCmd = NULL;
869 psb->status = IOSTAT_SUCCESS;
870 }
871 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
872 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
873 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
874 return 0;
875}
876
877/**
878 * lpfc_hba_down_post - Wrapper func for hba down post routine
879 * @phba: pointer to lpfc HBA data structure.
880 *
881 * This routine wraps the actual SLI3 or SLI4 routine for performing
882 * uninitialization after the HBA is reset when bring down the SLI Layer.
883 *
884 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200885 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400886 * Any other value - error.
887 **/
888int
889lpfc_hba_down_post(struct lpfc_hba *phba)
890{
891 return (*phba->lpfc_hba_down_post)(phba);
892}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500893
James Smarte59058c2008-08-24 21:49:00 -0400894/**
James Smart3621a712009-04-06 18:47:14 -0400895 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400896 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
897 *
898 * This is the HBA-timer timeout handler registered to the lpfc driver. When
899 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
900 * work-port-events bitmap and the worker thread is notified. This timeout
901 * event will be used by the worker thread to invoke the actual timeout
902 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
903 * be performed in the timeout handler and the HBA timeout event bit shall
904 * be cleared by the worker thread after it has taken the event bitmap out.
905 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100906static void
James Smart858c9f62007-06-17 19:56:39 -0500907lpfc_hb_timeout(unsigned long ptr)
908{
909 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400910 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500911 unsigned long iflag;
912
913 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400914
915 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500916 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400917 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
918 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500919 phba->pport->work_port_events |= WORKER_HB_TMO;
920 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
921
James Smart93996272008-08-24 21:50:30 -0400922 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400923 if (!tmo_posted)
924 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500925 return;
926}
927
James Smarte59058c2008-08-24 21:49:00 -0400928/**
James Smart3621a712009-04-06 18:47:14 -0400929 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400930 * @phba: pointer to lpfc hba data structure.
931 * @pmboxq: pointer to the driver internal queue element for mailbox command.
932 *
933 * This is the callback function to the lpfc heart-beat mailbox command.
934 * If configured, the lpfc driver issues the heart-beat mailbox command to
935 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
936 * heart-beat mailbox command is issued, the driver shall set up heart-beat
937 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
938 * heart-beat outstanding state. Once the mailbox command comes back and
939 * no error conditions detected, the heart-beat mailbox command timer is
940 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
941 * state is cleared for the next heart-beat. If the timer expired with the
942 * heart-beat outstanding state set, the driver will put the HBA offline.
943 **/
James Smart858c9f62007-06-17 19:56:39 -0500944static void
945lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
946{
947 unsigned long drvr_flag;
948
949 spin_lock_irqsave(&phba->hbalock, drvr_flag);
950 phba->hb_outstanding = 0;
951 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
952
James Smart93996272008-08-24 21:50:30 -0400953 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500954 mempool_free(pmboxq, phba->mbox_mem_pool);
955 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
956 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400957 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500958 mod_timer(&phba->hb_tmofunc,
959 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
960 return;
961}
962
James Smarte59058c2008-08-24 21:49:00 -0400963/**
James Smart3621a712009-04-06 18:47:14 -0400964 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400965 * @phba: pointer to lpfc hba data structure.
966 *
967 * This is the actual HBA-timer timeout handler to be invoked by the worker
968 * thread whenever the HBA timer fired and HBA-timeout event posted. This
969 * handler performs any periodic operations needed for the device. If such
970 * periodic event has already been attended to either in the interrupt handler
971 * or by processing slow-ring or fast-ring events within the HBA-timer
972 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
973 * the timer for the next timeout period. If lpfc heart-beat mailbox command
974 * is configured and there is no heart-beat mailbox command outstanding, a
975 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
976 * has been a heart-beat mailbox command outstanding, the HBA shall be put
977 * to offline.
978 **/
James Smart858c9f62007-06-17 19:56:39 -0500979void
980lpfc_hb_timeout_handler(struct lpfc_hba *phba)
981{
James Smart45ed1192009-10-02 15:17:02 -0400982 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -0500983 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500984 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -0400985 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -0500986 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500987 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500988
James Smart45ed1192009-10-02 15:17:02 -0400989 vports = lpfc_create_vport_work_array(phba);
990 if (vports != NULL)
991 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
992 lpfc_rcv_seq_check_edtov(vports[i]);
993 lpfc_destroy_vport_work_array(phba, vports);
994
James Smart858c9f62007-06-17 19:56:39 -0500995 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400996 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500997 (phba->pport->fc_flag & FC_OFFLINE_MODE))
998 return;
999
1000 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001001
1002 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1003 jiffies)) {
1004 spin_unlock_irq(&phba->pport->work_port_lock);
1005 if (!phba->hb_outstanding)
1006 mod_timer(&phba->hb_tmofunc,
1007 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1008 else
1009 mod_timer(&phba->hb_tmofunc,
1010 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1011 return;
1012 }
1013 spin_unlock_irq(&phba->pport->work_port_lock);
1014
James Smart0ff10d42008-01-11 01:52:36 -05001015 if (phba->elsbuf_cnt &&
1016 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1017 spin_lock_irq(&phba->hbalock);
1018 list_splice_init(&phba->elsbuf, &completions);
1019 phba->elsbuf_cnt = 0;
1020 phba->elsbuf_prev_cnt = 0;
1021 spin_unlock_irq(&phba->hbalock);
1022
1023 while (!list_empty(&completions)) {
1024 list_remove_head(&completions, buf_ptr,
1025 struct lpfc_dmabuf, list);
1026 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1027 kfree(buf_ptr);
1028 }
1029 }
1030 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1031
James Smart858c9f62007-06-17 19:56:39 -05001032 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001033 if (phba->cfg_enable_hba_heartbeat) {
1034 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001035 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1036 (list_empty(&psli->mboxq))) {
1037 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1038 GFP_KERNEL);
1039 if (!pmboxq) {
1040 mod_timer(&phba->hb_tmofunc,
1041 jiffies +
1042 HZ * LPFC_HB_MBOX_INTERVAL);
1043 return;
1044 }
James Smart13815c82008-01-11 01:52:48 -05001045
James Smartbc739052010-08-04 16:11:18 -04001046 lpfc_heart_beat(phba, pmboxq);
1047 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1048 pmboxq->vport = phba->pport;
1049 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1050 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001051
James Smartbc739052010-08-04 16:11:18 -04001052 if (retval != MBX_BUSY &&
1053 retval != MBX_SUCCESS) {
1054 mempool_free(pmboxq,
1055 phba->mbox_mem_pool);
1056 mod_timer(&phba->hb_tmofunc,
1057 jiffies +
1058 HZ * LPFC_HB_MBOX_INTERVAL);
1059 return;
1060 }
1061 phba->skipped_hb = 0;
1062 phba->hb_outstanding = 1;
1063 } else if (time_before_eq(phba->last_completion_time,
1064 phba->skipped_hb)) {
1065 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1066 "2857 Last completion time not "
1067 " updated in %d ms\n",
1068 jiffies_to_msecs(jiffies
1069 - phba->last_completion_time));
1070 } else
1071 phba->skipped_hb = jiffies;
1072
James Smart858c9f62007-06-17 19:56:39 -05001073 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001074 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001075 return;
James Smart13815c82008-01-11 01:52:48 -05001076 } else {
1077 /*
1078 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001079 * we need to give the hb mailbox cmd a chance to
1080 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001081 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001082 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1083 "0459 Adapter heartbeat still out"
1084 "standing:last compl time was %d ms.\n",
1085 jiffies_to_msecs(jiffies
1086 - phba->last_completion_time));
1087 mod_timer(&phba->hb_tmofunc,
1088 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001089 }
James Smart858c9f62007-06-17 19:56:39 -05001090 }
1091}
1092
James Smarte59058c2008-08-24 21:49:00 -04001093/**
James Smart3621a712009-04-06 18:47:14 -04001094 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001095 * @phba: pointer to lpfc hba data structure.
1096 *
1097 * This routine is called to bring the HBA offline when HBA hardware error
1098 * other than Port Error 6 has been detected.
1099 **/
James Smart09372822008-01-11 01:52:54 -05001100static void
1101lpfc_offline_eratt(struct lpfc_hba *phba)
1102{
1103 struct lpfc_sli *psli = &phba->sli;
1104
1105 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001106 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001107 spin_unlock_irq(&phba->hbalock);
1108 lpfc_offline_prep(phba);
1109
1110 lpfc_offline(phba);
1111 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001112 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001113 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001114 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001115 lpfc_hba_down_post(phba);
1116 lpfc_sli_brdready(phba, HS_MBRDY);
1117 lpfc_unblock_mgmt_io(phba);
1118 phba->link_state = LPFC_HBA_ERROR;
1119 return;
1120}
1121
James Smarte59058c2008-08-24 21:49:00 -04001122/**
James Smartda0436e2009-05-22 14:51:39 -04001123 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1124 * @phba: pointer to lpfc hba data structure.
1125 *
1126 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1127 * other than Port Error 6 has been detected.
1128 **/
1129static void
1130lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1131{
1132 lpfc_offline_prep(phba);
1133 lpfc_offline(phba);
1134 lpfc_sli4_brdreset(phba);
1135 lpfc_hba_down_post(phba);
1136 lpfc_sli4_post_status_check(phba);
1137 lpfc_unblock_mgmt_io(phba);
1138 phba->link_state = LPFC_HBA_ERROR;
1139}
1140
1141/**
James Smarta257bf92009-04-06 18:48:10 -04001142 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1143 * @phba: pointer to lpfc hba data structure.
1144 *
1145 * This routine is invoked to handle the deferred HBA hardware error
1146 * conditions. This type of error is indicated by HBA by setting ER1
1147 * and another ER bit in the host status register. The driver will
1148 * wait until the ER1 bit clears before handling the error condition.
1149 **/
1150static void
1151lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1152{
1153 uint32_t old_host_status = phba->work_hs;
1154 struct lpfc_sli_ring *pring;
1155 struct lpfc_sli *psli = &phba->sli;
1156
James Smartf4b4c682009-05-22 14:53:12 -04001157 /* If the pci channel is offline, ignore possible errors,
1158 * since we cannot communicate with the pci card anyway.
1159 */
1160 if (pci_channel_offline(phba->pcidev)) {
1161 spin_lock_irq(&phba->hbalock);
1162 phba->hba_flag &= ~DEFER_ERATT;
1163 spin_unlock_irq(&phba->hbalock);
1164 return;
1165 }
1166
James Smarta257bf92009-04-06 18:48:10 -04001167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1168 "0479 Deferred Adapter Hardware Error "
1169 "Data: x%x x%x x%x\n",
1170 phba->work_hs,
1171 phba->work_status[0], phba->work_status[1]);
1172
1173 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001174 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001175 spin_unlock_irq(&phba->hbalock);
1176
1177
1178 /*
1179 * Firmware stops when it triggred erratt. That could cause the I/Os
1180 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1181 * SCSI layer retry it after re-establishing link.
1182 */
1183 pring = &psli->ring[psli->fcp_ring];
1184 lpfc_sli_abort_iocb_ring(phba, pring);
1185
1186 /*
1187 * There was a firmware error. Take the hba offline and then
1188 * attempt to restart it.
1189 */
1190 lpfc_offline_prep(phba);
1191 lpfc_offline(phba);
1192
1193 /* Wait for the ER1 bit to clear.*/
1194 while (phba->work_hs & HS_FFER1) {
1195 msleep(100);
1196 phba->work_hs = readl(phba->HSregaddr);
1197 /* If driver is unloading let the worker thread continue */
1198 if (phba->pport->load_flag & FC_UNLOADING) {
1199 phba->work_hs = 0;
1200 break;
1201 }
1202 }
1203
1204 /*
1205 * This is to ptrotect against a race condition in which
1206 * first write to the host attention register clear the
1207 * host status register.
1208 */
1209 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1210 phba->work_hs = old_host_status & ~HS_FFER1;
1211
James Smart3772a992009-05-22 14:50:54 -04001212 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001213 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001214 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001215 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1216 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1217}
1218
James Smart3772a992009-05-22 14:50:54 -04001219static void
1220lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1221{
1222 struct lpfc_board_event_header board_event;
1223 struct Scsi_Host *shost;
1224
1225 board_event.event_type = FC_REG_BOARD_EVENT;
1226 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1227 shost = lpfc_shost_from_vport(phba->pport);
1228 fc_host_post_vendor_event(shost, fc_get_event_number(),
1229 sizeof(board_event),
1230 (char *) &board_event,
1231 LPFC_NL_VENDOR_ID);
1232}
1233
James Smarta257bf92009-04-06 18:48:10 -04001234/**
James Smart3772a992009-05-22 14:50:54 -04001235 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001236 * @phba: pointer to lpfc hba data structure.
1237 *
1238 * This routine is invoked to handle the following HBA hardware error
1239 * conditions:
1240 * 1 - HBA error attention interrupt
1241 * 2 - DMA ring index out of range
1242 * 3 - Mailbox command came back as unknown
1243 **/
James Smart3772a992009-05-22 14:50:54 -04001244static void
1245lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001246{
James Smart2e0fef82007-06-17 19:56:36 -05001247 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001248 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001249 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001250 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001251 unsigned long temperature;
1252 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001253 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001254
Linas Vepstas8d63f372007-02-14 14:28:36 -06001255 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001256 * since we cannot communicate with the pci card anyway.
1257 */
1258 if (pci_channel_offline(phba->pcidev)) {
1259 spin_lock_irq(&phba->hbalock);
1260 phba->hba_flag &= ~DEFER_ERATT;
1261 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001262 return;
James Smart3772a992009-05-22 14:50:54 -04001263 }
1264
James Smart13815c82008-01-11 01:52:48 -05001265 /* If resets are disabled then leave the HBA alone and return */
1266 if (!phba->cfg_enable_hba_reset)
1267 return;
dea31012005-04-17 16:05:31 -05001268
James Smartea2151b2008-09-07 11:52:10 -04001269 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001270 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001271
James Smarta257bf92009-04-06 18:48:10 -04001272 if (phba->hba_flag & DEFER_ERATT)
1273 lpfc_handle_deferred_eratt(phba);
1274
James Smartdcf2a4e2010-09-29 11:18:53 -04001275 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1276 if (phba->work_hs & HS_FFER6)
1277 /* Re-establishing Link */
1278 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1279 "1301 Re-establishing Link "
1280 "Data: x%x x%x x%x\n",
1281 phba->work_hs, phba->work_status[0],
1282 phba->work_status[1]);
1283 if (phba->work_hs & HS_FFER8)
1284 /* Device Zeroization */
1285 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1286 "2861 Host Authentication device "
1287 "zeroization Data:x%x x%x x%x\n",
1288 phba->work_hs, phba->work_status[0],
1289 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001290
James Smart92d7f7b2007-06-17 19:56:38 -05001291 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001292 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001293 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001294
1295 /*
1296 * Firmware stops when it triggled erratt with HS_FFER6.
1297 * That could cause the I/Os dropped by the firmware.
1298 * Error iocb (I/O) on txcmplq and let the SCSI layer
1299 * retry it after re-establishing link.
1300 */
1301 pring = &psli->ring[psli->fcp_ring];
1302 lpfc_sli_abort_iocb_ring(phba, pring);
1303
dea31012005-04-17 16:05:31 -05001304 /*
1305 * There was a firmware error. Take the hba offline and then
1306 * attempt to restart it.
1307 */
James Smart46fa3112007-04-25 09:51:45 -04001308 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001309 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001310 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001311 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001312 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001313 return;
1314 }
James Smart46fa3112007-04-25 09:51:45 -04001315 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001316 } else if (phba->work_hs & HS_CRIT_TEMP) {
1317 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1318 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1319 temp_event_data.event_code = LPFC_CRIT_TEMP;
1320 temp_event_data.data = (uint32_t)temperature;
1321
1322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001323 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001324 "(%ld), taking this port offline "
1325 "Data: x%x x%x x%x\n",
1326 temperature, phba->work_hs,
1327 phba->work_status[0], phba->work_status[1]);
1328
1329 shost = lpfc_shost_from_vport(phba->pport);
1330 fc_host_post_vendor_event(shost, fc_get_event_number(),
1331 sizeof(temp_event_data),
1332 (char *) &temp_event_data,
1333 SCSI_NL_VID_TYPE_PCI
1334 | PCI_VENDOR_ID_EMULEX);
1335
James Smart7af67052007-10-27 13:38:11 -04001336 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001337 phba->over_temp_state = HBA_OVER_TEMP;
1338 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001339 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001340
dea31012005-04-17 16:05:31 -05001341 } else {
1342 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001343 * failure is a value other than FFER6. Do not call the offline
1344 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001345 */
1346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001347 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001348 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001349 phba->work_hs,
dea31012005-04-17 16:05:31 -05001350 phba->work_status[0], phba->work_status[1]);
1351
James Smartd2873e42006-08-18 17:46:43 -04001352 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001353 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001354 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001355 sizeof(event_data), (char *) &event_data,
1356 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1357
James Smart09372822008-01-11 01:52:54 -05001358 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001359 }
James Smart93996272008-08-24 21:50:30 -04001360 return;
dea31012005-04-17 16:05:31 -05001361}
1362
James Smarte59058c2008-08-24 21:49:00 -04001363/**
James Smartda0436e2009-05-22 14:51:39 -04001364 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1365 * @phba: pointer to lpfc hba data structure.
1366 *
1367 * This routine is invoked to handle the SLI4 HBA hardware error attention
1368 * conditions.
1369 **/
1370static void
1371lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1372{
1373 struct lpfc_vport *vport = phba->pport;
1374 uint32_t event_data;
1375 struct Scsi_Host *shost;
1376
1377 /* If the pci channel is offline, ignore possible errors, since
1378 * we cannot communicate with the pci card anyway.
1379 */
1380 if (pci_channel_offline(phba->pcidev))
1381 return;
1382 /* If resets are disabled then leave the HBA alone and return */
1383 if (!phba->cfg_enable_hba_reset)
1384 return;
1385
1386 /* Send an internal error event to mgmt application */
1387 lpfc_board_errevt_to_mgmt(phba);
1388
1389 /* For now, the actual action for SLI4 device handling is not
1390 * specified yet, just treated it as adaptor hardware failure
1391 */
1392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1393 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1394 phba->work_status[0], phba->work_status[1]);
1395
1396 event_data = FC_REG_DUMP_EVENT;
1397 shost = lpfc_shost_from_vport(vport);
1398 fc_host_post_vendor_event(shost, fc_get_event_number(),
1399 sizeof(event_data), (char *) &event_data,
1400 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1401
1402 lpfc_sli4_offline_eratt(phba);
1403}
1404
1405/**
1406 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1407 * @phba: pointer to lpfc HBA data structure.
1408 *
1409 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1410 * routine from the API jump table function pointer from the lpfc_hba struct.
1411 *
1412 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001413 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001414 * Any other value - error.
1415 **/
1416void
1417lpfc_handle_eratt(struct lpfc_hba *phba)
1418{
1419 (*phba->lpfc_handle_eratt)(phba);
1420}
1421
1422/**
James Smart3621a712009-04-06 18:47:14 -04001423 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001424 * @phba: pointer to lpfc hba data structure.
1425 *
1426 * This routine is invoked from the worker thread to handle a HBA host
1427 * attention link event.
1428 **/
dea31012005-04-17 16:05:31 -05001429void
James Smart2e0fef82007-06-17 19:56:36 -05001430lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001431{
James Smart2e0fef82007-06-17 19:56:36 -05001432 struct lpfc_vport *vport = phba->pport;
1433 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001434 LPFC_MBOXQ_t *pmb;
1435 volatile uint32_t control;
1436 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001437 int rc = 0;
dea31012005-04-17 16:05:31 -05001438
1439 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001440 if (!pmb) {
1441 rc = 1;
dea31012005-04-17 16:05:31 -05001442 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001443 }
dea31012005-04-17 16:05:31 -05001444
1445 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001446 if (!mp) {
1447 rc = 2;
dea31012005-04-17 16:05:31 -05001448 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001449 }
dea31012005-04-17 16:05:31 -05001450
1451 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001452 if (!mp->virt) {
1453 rc = 3;
dea31012005-04-17 16:05:31 -05001454 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001455 }
dea31012005-04-17 16:05:31 -05001456
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001457 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001458 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001459
1460 psli->slistat.link_event++;
1461 lpfc_read_la(phba, pmb, mp);
1462 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001463 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001464 /* Block ELS IOCBs until we have processed this mbox command */
1465 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001466 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001467 if (rc == MBX_NOT_FINISHED) {
1468 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001469 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001470 }
dea31012005-04-17 16:05:31 -05001471
1472 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001473 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001474 writel(HA_LATT, phba->HAregaddr);
1475 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001476 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001477
1478 return;
1479
James Smart14691152006-12-02 13:34:28 -05001480lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001481 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001482 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001483lpfc_handle_latt_free_mp:
1484 kfree(mp);
1485lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001486 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001487lpfc_handle_latt_err_exit:
1488 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001489 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001490 psli->sli_flag |= LPFC_PROCESS_LA;
1491 control = readl(phba->HCregaddr);
1492 control |= HC_LAINT_ENA;
1493 writel(control, phba->HCregaddr);
1494 readl(phba->HCregaddr); /* flush */
1495
1496 /* Clear Link Attention in HA REG */
1497 writel(HA_LATT, phba->HAregaddr);
1498 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001499 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001500 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001501 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001502
James Smart09372822008-01-11 01:52:54 -05001503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1504 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001505
1506 return;
1507}
1508
James Smarte59058c2008-08-24 21:49:00 -04001509/**
James Smart3621a712009-04-06 18:47:14 -04001510 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001511 * @phba: pointer to lpfc hba data structure.
1512 * @vpd: pointer to the vital product data.
1513 * @len: length of the vital product data in bytes.
1514 *
1515 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1516 * an array of characters. In this routine, the ModelName, ProgramType, and
1517 * ModelDesc, etc. fields of the phba data structure will be populated.
1518 *
1519 * Return codes
1520 * 0 - pointer to the VPD passed in is NULL
1521 * 1 - success
1522 **/
James Smart3772a992009-05-22 14:50:54 -04001523int
James Smart2e0fef82007-06-17 19:56:36 -05001524lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001525{
1526 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001527 int Length;
dea31012005-04-17 16:05:31 -05001528 int i, j;
1529 int finished = 0;
1530 int index = 0;
1531
1532 if (!vpd)
1533 return 0;
1534
1535 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001536 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001537 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001538 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1539 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001540 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001541 switch (vpd[index]) {
1542 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001543 case 0x91:
dea31012005-04-17 16:05:31 -05001544 index += 1;
1545 lenlo = vpd[index];
1546 index += 1;
1547 lenhi = vpd[index];
1548 index += 1;
1549 i = ((((unsigned short)lenhi) << 8) + lenlo);
1550 index += i;
1551 break;
1552 case 0x90:
1553 index += 1;
1554 lenlo = vpd[index];
1555 index += 1;
1556 lenhi = vpd[index];
1557 index += 1;
1558 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001559 if (Length > len - index)
1560 Length = len - index;
dea31012005-04-17 16:05:31 -05001561 while (Length > 0) {
1562 /* Look for Serial Number */
1563 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1564 index += 2;
1565 i = vpd[index];
1566 index += 1;
1567 j = 0;
1568 Length -= (3+i);
1569 while(i--) {
1570 phba->SerialNumber[j++] = vpd[index++];
1571 if (j == 31)
1572 break;
1573 }
1574 phba->SerialNumber[j] = 0;
1575 continue;
1576 }
1577 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1578 phba->vpd_flag |= VPD_MODEL_DESC;
1579 index += 2;
1580 i = vpd[index];
1581 index += 1;
1582 j = 0;
1583 Length -= (3+i);
1584 while(i--) {
1585 phba->ModelDesc[j++] = vpd[index++];
1586 if (j == 255)
1587 break;
1588 }
1589 phba->ModelDesc[j] = 0;
1590 continue;
1591 }
1592 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1593 phba->vpd_flag |= VPD_MODEL_NAME;
1594 index += 2;
1595 i = vpd[index];
1596 index += 1;
1597 j = 0;
1598 Length -= (3+i);
1599 while(i--) {
1600 phba->ModelName[j++] = vpd[index++];
1601 if (j == 79)
1602 break;
1603 }
1604 phba->ModelName[j] = 0;
1605 continue;
1606 }
1607 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1608 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1609 index += 2;
1610 i = vpd[index];
1611 index += 1;
1612 j = 0;
1613 Length -= (3+i);
1614 while(i--) {
1615 phba->ProgramType[j++] = vpd[index++];
1616 if (j == 255)
1617 break;
1618 }
1619 phba->ProgramType[j] = 0;
1620 continue;
1621 }
1622 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1623 phba->vpd_flag |= VPD_PORT;
1624 index += 2;
1625 i = vpd[index];
1626 index += 1;
1627 j = 0;
1628 Length -= (3+i);
1629 while(i--) {
1630 phba->Port[j++] = vpd[index++];
1631 if (j == 19)
1632 break;
1633 }
1634 phba->Port[j] = 0;
1635 continue;
1636 }
1637 else {
1638 index += 2;
1639 i = vpd[index];
1640 index += 1;
1641 index += i;
1642 Length -= (3 + i);
1643 }
1644 }
1645 finished = 0;
1646 break;
1647 case 0x78:
1648 finished = 1;
1649 break;
1650 default:
1651 index ++;
1652 break;
1653 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001654 }
dea31012005-04-17 16:05:31 -05001655
1656 return(1);
1657}
1658
James Smarte59058c2008-08-24 21:49:00 -04001659/**
James Smart3621a712009-04-06 18:47:14 -04001660 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001661 * @phba: pointer to lpfc hba data structure.
1662 * @mdp: pointer to the data structure to hold the derived model name.
1663 * @descp: pointer to the data structure to hold the derived description.
1664 *
1665 * This routine retrieves HBA's description based on its registered PCI device
1666 * ID. The @descp passed into this function points to an array of 256 chars. It
1667 * shall be returned with the model name, maximum speed, and the host bus type.
1668 * The @mdp passed into this function points to an array of 80 chars. When the
1669 * function returns, the @mdp will be filled with the model name.
1670 **/
dea31012005-04-17 16:05:31 -05001671static void
James Smart2e0fef82007-06-17 19:56:36 -05001672lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001673{
1674 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001675 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001676 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001677 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001678 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001679 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001680 char *name;
1681 char *bus;
1682 char *function;
1683 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001684
1685 if (mdp && mdp[0] != '\0'
1686 && descp && descp[0] != '\0')
1687 return;
1688
1689 if (phba->lmt & LMT_10Gb)
1690 max_speed = 10;
1691 else if (phba->lmt & LMT_8Gb)
1692 max_speed = 8;
1693 else if (phba->lmt & LMT_4Gb)
1694 max_speed = 4;
1695 else if (phba->lmt & LMT_2Gb)
1696 max_speed = 2;
1697 else
1698 max_speed = 1;
dea31012005-04-17 16:05:31 -05001699
1700 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001701
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001702 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001703 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001704 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001705 break;
dea31012005-04-17 16:05:31 -05001706 case PCI_DEVICE_ID_SUPERFLY:
1707 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001708 m = (typeof(m)){"LP7000", "PCI",
1709 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001710 else
James Smarta747c9c2009-11-18 15:41:10 -05001711 m = (typeof(m)){"LP7000E", "PCI",
1712 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001713 break;
1714 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001715 m = (typeof(m)){"LP8000", "PCI",
1716 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001717 break;
1718 case PCI_DEVICE_ID_CENTAUR:
1719 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001720 m = (typeof(m)){"LP9002", "PCI",
1721 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001722 else
James Smarta747c9c2009-11-18 15:41:10 -05001723 m = (typeof(m)){"LP9000", "PCI",
1724 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001725 break;
1726 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001727 m = (typeof(m)){"LP952", "PCI",
1728 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001729 break;
1730 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001731 m = (typeof(m)){"LP9802", "PCI-X",
1732 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001733 break;
1734 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001735 m = (typeof(m)){"LP10000", "PCI-X",
1736 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001737 break;
1738 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001739 m = (typeof(m)){"LPX1000", "PCI-X",
1740 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001741 break;
1742 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001743 m = (typeof(m)){"LP982", "PCI-X",
1744 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001745 break;
1746 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001747 m = (typeof(m)){"LP1050", "PCI-X",
1748 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001749 break;
1750 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001751 m = (typeof(m)){"LP11000", "PCI-X2",
1752 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001753 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001754 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001755 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1756 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001757 break;
1758 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001759 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1760 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001761 break;
1762 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001763 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001764 break;
1765 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001766 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001767 break;
1768 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001769 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001770 break;
dea31012005-04-17 16:05:31 -05001771 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001772 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001773 break;
1774 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001775 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001776 break;
1777 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001778 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001779 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001780 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001781 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001782 break;
1783 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001784 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001785 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001786 break;
dea31012005-04-17 16:05:31 -05001787 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001788 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001789 break;
1790 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001791 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001792 break;
1793 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001794 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001795 break;
1796 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001797 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001798 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001799 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001800 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001801 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001802 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001803 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001804 break;
James Smartb87eab32007-04-25 09:53:28 -04001805 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001806 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001807 break;
1808 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001809 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001810 break;
1811 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001812 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001813 break;
1814 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001815 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001816 break;
1817 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001818 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001819 break;
1820 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001821 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001822 break;
James Smart84774a42008-08-24 21:50:06 -04001823 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001824 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001825 GE = 1;
1826 break;
1827 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001828 m = (typeof(m)){"LPev12000", "PCIe IOV",
1829 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001830 break;
1831 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001832 m = (typeof(m)){"LPev12000", "PCIe IOV",
1833 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001834 break;
1835 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001836 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1837 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001838 break;
James Smartda0436e2009-05-22 14:51:39 -04001839 case PCI_DEVICE_ID_TIGERSHARK:
1840 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001841 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04001842 break;
James Smarta747c9c2009-11-18 15:41:10 -05001843 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04001844 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001845 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1846 break;
1847 case PCI_DEVICE_ID_FALCON:
1848 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1849 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04001850 break;
James Smart98fc5dd2010-06-07 15:24:29 -04001851 case PCI_DEVICE_ID_BALIUS:
1852 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
1853 "Fibre Channel Adapter"};
1854 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001855 default:
James Smarta747c9c2009-11-18 15:41:10 -05001856 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001857 break;
dea31012005-04-17 16:05:31 -05001858 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001859
1860 if (mdp && mdp[0] == '\0')
1861 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001862 /* oneConnect hba requires special processing, they are all initiators
1863 * and we put the port number on the end
1864 */
1865 if (descp && descp[0] == '\0') {
1866 if (oneConnect)
1867 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05001868 "Emulex OneConnect %s, %s Initiator, Port %s",
1869 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04001870 phba->Port);
1871 else
1872 snprintf(descp, 255,
1873 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05001874 m.name, max_speed, (GE) ? "GE" : "Gb",
1875 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04001876 }
dea31012005-04-17 16:05:31 -05001877}
1878
James Smarte59058c2008-08-24 21:49:00 -04001879/**
James Smart3621a712009-04-06 18:47:14 -04001880 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001881 * @phba: pointer to lpfc hba data structure.
1882 * @pring: pointer to a IOCB ring.
1883 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1884 *
1885 * This routine posts a given number of IOCBs with the associated DMA buffer
1886 * descriptors specified by the cnt argument to the given IOCB ring.
1887 *
1888 * Return codes
1889 * The number of IOCBs NOT able to be posted to the IOCB ring.
1890 **/
dea31012005-04-17 16:05:31 -05001891int
James Smart495a7142008-06-14 22:52:59 -04001892lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001893{
1894 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001895 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001896 struct lpfc_dmabuf *mp1, *mp2;
1897
1898 cnt += pring->missbufcnt;
1899
1900 /* While there are buffers to post */
1901 while (cnt > 0) {
1902 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001903 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001904 if (iocb == NULL) {
1905 pring->missbufcnt = cnt;
1906 return cnt;
1907 }
dea31012005-04-17 16:05:31 -05001908 icmd = &iocb->iocb;
1909
1910 /* 2 buffers can be posted per command */
1911 /* Allocate buffer to post */
1912 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1913 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001914 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1915 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001916 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001917 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001918 pring->missbufcnt = cnt;
1919 return cnt;
1920 }
1921
1922 INIT_LIST_HEAD(&mp1->list);
1923 /* Allocate buffer to post */
1924 if (cnt > 1) {
1925 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1926 if (mp2)
1927 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1928 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001929 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001930 kfree(mp2);
dea31012005-04-17 16:05:31 -05001931 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1932 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001933 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001934 pring->missbufcnt = cnt;
1935 return cnt;
1936 }
1937
1938 INIT_LIST_HEAD(&mp2->list);
1939 } else {
1940 mp2 = NULL;
1941 }
1942
1943 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1944 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1945 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1946 icmd->ulpBdeCount = 1;
1947 cnt--;
1948 if (mp2) {
1949 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1950 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1951 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1952 cnt--;
1953 icmd->ulpBdeCount = 2;
1954 }
1955
1956 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1957 icmd->ulpLe = 1;
1958
James Smart3772a992009-05-22 14:50:54 -04001959 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1960 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001961 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1962 kfree(mp1);
1963 cnt++;
1964 if (mp2) {
1965 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1966 kfree(mp2);
1967 cnt++;
1968 }
James Bottomley604a3e32005-10-29 10:28:33 -05001969 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001970 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001971 return cnt;
1972 }
dea31012005-04-17 16:05:31 -05001973 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001974 if (mp2)
dea31012005-04-17 16:05:31 -05001975 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001976 }
1977 pring->missbufcnt = 0;
1978 return 0;
1979}
1980
James Smarte59058c2008-08-24 21:49:00 -04001981/**
James Smart3621a712009-04-06 18:47:14 -04001982 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001983 * @phba: pointer to lpfc hba data structure.
1984 *
1985 * This routine posts initial receive IOCB buffers to the ELS ring. The
1986 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1987 * set to 64 IOCBs.
1988 *
1989 * Return codes
1990 * 0 - success (currently always success)
1991 **/
dea31012005-04-17 16:05:31 -05001992static int
James Smart2e0fef82007-06-17 19:56:36 -05001993lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001994{
1995 struct lpfc_sli *psli = &phba->sli;
1996
1997 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001998 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001999 /* Ring 2 - FCP no buffers needed */
2000
2001 return 0;
2002}
2003
2004#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2005
James Smarte59058c2008-08-24 21:49:00 -04002006/**
James Smart3621a712009-04-06 18:47:14 -04002007 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002008 * @HashResultPointer: pointer to an array as hash table.
2009 *
2010 * This routine sets up the initial values to the array of hash table entries
2011 * for the LC HBAs.
2012 **/
dea31012005-04-17 16:05:31 -05002013static void
2014lpfc_sha_init(uint32_t * HashResultPointer)
2015{
2016 HashResultPointer[0] = 0x67452301;
2017 HashResultPointer[1] = 0xEFCDAB89;
2018 HashResultPointer[2] = 0x98BADCFE;
2019 HashResultPointer[3] = 0x10325476;
2020 HashResultPointer[4] = 0xC3D2E1F0;
2021}
2022
James Smarte59058c2008-08-24 21:49:00 -04002023/**
James Smart3621a712009-04-06 18:47:14 -04002024 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002025 * @HashResultPointer: pointer to an initial/result hash table.
2026 * @HashWorkingPointer: pointer to an working hash table.
2027 *
2028 * This routine iterates an initial hash table pointed by @HashResultPointer
2029 * with the values from the working hash table pointeed by @HashWorkingPointer.
2030 * The results are putting back to the initial hash table, returned through
2031 * the @HashResultPointer as the result hash table.
2032 **/
dea31012005-04-17 16:05:31 -05002033static void
2034lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2035{
2036 int t;
2037 uint32_t TEMP;
2038 uint32_t A, B, C, D, E;
2039 t = 16;
2040 do {
2041 HashWorkingPointer[t] =
2042 S(1,
2043 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2044 8] ^
2045 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2046 } while (++t <= 79);
2047 t = 0;
2048 A = HashResultPointer[0];
2049 B = HashResultPointer[1];
2050 C = HashResultPointer[2];
2051 D = HashResultPointer[3];
2052 E = HashResultPointer[4];
2053
2054 do {
2055 if (t < 20) {
2056 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2057 } else if (t < 40) {
2058 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2059 } else if (t < 60) {
2060 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2061 } else {
2062 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2063 }
2064 TEMP += S(5, A) + E + HashWorkingPointer[t];
2065 E = D;
2066 D = C;
2067 C = S(30, B);
2068 B = A;
2069 A = TEMP;
2070 } while (++t <= 79);
2071
2072 HashResultPointer[0] += A;
2073 HashResultPointer[1] += B;
2074 HashResultPointer[2] += C;
2075 HashResultPointer[3] += D;
2076 HashResultPointer[4] += E;
2077
2078}
2079
James Smarte59058c2008-08-24 21:49:00 -04002080/**
James Smart3621a712009-04-06 18:47:14 -04002081 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002082 * @RandomChallenge: pointer to the entry of host challenge random number array.
2083 * @HashWorking: pointer to the entry of the working hash array.
2084 *
2085 * This routine calculates the working hash array referred by @HashWorking
2086 * from the challenge random numbers associated with the host, referred by
2087 * @RandomChallenge. The result is put into the entry of the working hash
2088 * array and returned by reference through @HashWorking.
2089 **/
dea31012005-04-17 16:05:31 -05002090static void
2091lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2092{
2093 *HashWorking = (*RandomChallenge ^ *HashWorking);
2094}
2095
James Smarte59058c2008-08-24 21:49:00 -04002096/**
James Smart3621a712009-04-06 18:47:14 -04002097 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002098 * @phba: pointer to lpfc hba data structure.
2099 * @hbainit: pointer to an array of unsigned 32-bit integers.
2100 *
2101 * This routine performs the special handling for LC HBA initialization.
2102 **/
dea31012005-04-17 16:05:31 -05002103void
2104lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2105{
2106 int t;
2107 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002108 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002109
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002110 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002111 if (!HashWorking)
2112 return;
2113
dea31012005-04-17 16:05:31 -05002114 HashWorking[0] = HashWorking[78] = *pwwnn++;
2115 HashWorking[1] = HashWorking[79] = *pwwnn;
2116
2117 for (t = 0; t < 7; t++)
2118 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2119
2120 lpfc_sha_init(hbainit);
2121 lpfc_sha_iterate(hbainit, HashWorking);
2122 kfree(HashWorking);
2123}
2124
James Smarte59058c2008-08-24 21:49:00 -04002125/**
James Smart3621a712009-04-06 18:47:14 -04002126 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002127 * @vport: pointer to a virtual N_Port data structure.
2128 *
2129 * This routine performs the necessary cleanups before deleting the @vport.
2130 * It invokes the discovery state machine to perform necessary state
2131 * transitions and to release the ndlps associated with the @vport. Note,
2132 * the physical port is treated as @vport 0.
2133 **/
James Smart87af33f2007-10-27 13:37:43 -04002134void
James Smart2e0fef82007-06-17 19:56:36 -05002135lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002136{
James Smart87af33f2007-10-27 13:37:43 -04002137 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002138 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002139 int i = 0;
dea31012005-04-17 16:05:31 -05002140
James Smart87af33f2007-10-27 13:37:43 -04002141 if (phba->link_state > LPFC_LINK_DOWN)
2142 lpfc_port_link_failure(vport);
2143
2144 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002145 if (!NLP_CHK_NODE_ACT(ndlp)) {
2146 ndlp = lpfc_enable_node(vport, ndlp,
2147 NLP_STE_UNUSED_NODE);
2148 if (!ndlp)
2149 continue;
2150 spin_lock_irq(&phba->ndlp_lock);
2151 NLP_SET_FREE_REQ(ndlp);
2152 spin_unlock_irq(&phba->ndlp_lock);
2153 /* Trigger the release of the ndlp memory */
2154 lpfc_nlp_put(ndlp);
2155 continue;
2156 }
2157 spin_lock_irq(&phba->ndlp_lock);
2158 if (NLP_CHK_FREE_REQ(ndlp)) {
2159 /* The ndlp should not be in memory free mode already */
2160 spin_unlock_irq(&phba->ndlp_lock);
2161 continue;
2162 } else
2163 /* Indicate request for freeing ndlp memory */
2164 NLP_SET_FREE_REQ(ndlp);
2165 spin_unlock_irq(&phba->ndlp_lock);
2166
James Smart58da1ff2008-04-07 10:15:56 -04002167 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2168 ndlp->nlp_DID == Fabric_DID) {
2169 /* Just free up ndlp with Fabric_DID for vports */
2170 lpfc_nlp_put(ndlp);
2171 continue;
2172 }
2173
James Smart87af33f2007-10-27 13:37:43 -04002174 if (ndlp->nlp_type & NLP_FABRIC)
2175 lpfc_disc_state_machine(vport, ndlp, NULL,
2176 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002177
James Smart87af33f2007-10-27 13:37:43 -04002178 lpfc_disc_state_machine(vport, ndlp, NULL,
2179 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002180
James Smart87af33f2007-10-27 13:37:43 -04002181 }
2182
James Smarta8adb832007-10-27 13:37:53 -04002183 /* At this point, ALL ndlp's should be gone
2184 * because of the previous NLP_EVT_DEVICE_RM.
2185 * Lets wait for this to happen, if needed.
2186 */
James Smart87af33f2007-10-27 13:37:43 -04002187 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002188 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002189 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002190 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002191 list_for_each_entry_safe(ndlp, next_ndlp,
2192 &vport->fc_nodes, nlp_listp) {
2193 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2194 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002195 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002196 "usgmap:x%x refcnt:%d\n",
2197 ndlp->nlp_DID, (void *)ndlp,
2198 ndlp->nlp_usg_map,
2199 atomic_read(
2200 &ndlp->kref.refcount));
2201 }
James Smarta8adb832007-10-27 13:37:53 -04002202 break;
James Smart87af33f2007-10-27 13:37:43 -04002203 }
James Smarta8adb832007-10-27 13:37:53 -04002204
2205 /* Wait for any activity on ndlps to settle */
2206 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002207 }
dea31012005-04-17 16:05:31 -05002208}
2209
James Smarte59058c2008-08-24 21:49:00 -04002210/**
James Smart3621a712009-04-06 18:47:14 -04002211 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002212 * @vport: pointer to a virtual N_Port data structure.
2213 *
2214 * This routine stops all the timers associated with a @vport. This function
2215 * is invoked before disabling or deleting a @vport. Note that the physical
2216 * port is treated as @vport 0.
2217 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002218void
2219lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002220{
James Smart92d7f7b2007-06-17 19:56:38 -05002221 del_timer_sync(&vport->els_tmofunc);
2222 del_timer_sync(&vport->fc_fdmitmo);
2223 lpfc_can_disctmo(vport);
2224 return;
dea31012005-04-17 16:05:31 -05002225}
2226
James Smarte59058c2008-08-24 21:49:00 -04002227/**
James Smartecfd03c2010-02-12 14:41:27 -05002228 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2229 * @phba: pointer to lpfc hba data structure.
2230 *
2231 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2232 * caller of this routine should already hold the host lock.
2233 **/
2234void
2235__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2236{
James Smartfc2b9892010-02-26 14:15:29 -05002237 /* Clear pending FCF rediscovery wait and failover in progress flags */
2238 phba->fcf.fcf_flag &= ~(FCF_REDISC_PEND |
James Smart0c9ab6f2010-02-26 14:15:57 -05002239 FCF_DEAD_DISC |
2240 FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002241 /* Now, try to stop the timer */
2242 del_timer(&phba->fcf.redisc_wait);
2243}
2244
2245/**
2246 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2247 * @phba: pointer to lpfc hba data structure.
2248 *
2249 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2250 * checks whether the FCF rediscovery wait timer is pending with the host
2251 * lock held before proceeding with disabling the timer and clearing the
2252 * wait timer pendig flag.
2253 **/
2254void
2255lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2256{
2257 spin_lock_irq(&phba->hbalock);
2258 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2259 /* FCF rediscovery timer already fired or stopped */
2260 spin_unlock_irq(&phba->hbalock);
2261 return;
2262 }
2263 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2264 spin_unlock_irq(&phba->hbalock);
2265}
2266
2267/**
James Smart3772a992009-05-22 14:50:54 -04002268 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002269 * @phba: pointer to lpfc hba data structure.
2270 *
2271 * This routine stops all the timers associated with a HBA. This function is
2272 * invoked before either putting a HBA offline or unloading the driver.
2273 **/
James Smart3772a992009-05-22 14:50:54 -04002274void
2275lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002276{
James Smart51ef4c22007-08-02 11:10:31 -04002277 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002278 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002279 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002280 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002281 del_timer_sync(&phba->hb_tmofunc);
2282 phba->hb_outstanding = 0;
2283
2284 switch (phba->pci_dev_grp) {
2285 case LPFC_PCI_DEV_LP:
2286 /* Stop any LightPulse device specific driver timers */
2287 del_timer_sync(&phba->fcp_poll_timer);
2288 break;
2289 case LPFC_PCI_DEV_OC:
2290 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002291 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002292 break;
2293 default:
2294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2295 "0297 Invalid device group (x%x)\n",
2296 phba->pci_dev_grp);
2297 break;
2298 }
James Smart2e0fef82007-06-17 19:56:36 -05002299 return;
dea31012005-04-17 16:05:31 -05002300}
2301
James Smarte59058c2008-08-24 21:49:00 -04002302/**
James Smart3621a712009-04-06 18:47:14 -04002303 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002304 * @phba: pointer to lpfc hba data structure.
2305 *
2306 * This routine marks a HBA's management interface as blocked. Once the HBA's
2307 * management interface is marked as blocked, all the user space access to
2308 * the HBA, whether they are from sysfs interface or libdfc interface will
2309 * all be blocked. The HBA is set to block the management interface when the
2310 * driver prepares the HBA interface for online or offline.
2311 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002312static void
2313lpfc_block_mgmt_io(struct lpfc_hba * phba)
2314{
2315 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002316 uint8_t actcmd = MBX_HEARTBEAT;
2317 unsigned long timeout;
2318
Adrian Bunka6ababd2007-11-05 18:07:33 +01002319
2320 spin_lock_irqsave(&phba->hbalock, iflag);
2321 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart6e7288d2010-06-07 15:23:35 -04002322 if (phba->sli.mbox_active)
2323 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
Adrian Bunka6ababd2007-11-05 18:07:33 +01002324 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6e7288d2010-06-07 15:23:35 -04002325 /* Determine how long we might wait for the active mailbox
2326 * command to be gracefully completed by firmware.
2327 */
2328 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
2329 jiffies;
2330 /* Wait for the outstnading mailbox command to complete */
2331 while (phba->sli.mbox_active) {
2332 /* Check active mailbox complete status every 2ms */
2333 msleep(2);
2334 if (time_after(jiffies, timeout)) {
2335 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2336 "2813 Mgmt IO is Blocked %x "
2337 "- mbox cmd %x still active\n",
2338 phba->sli.sli_flag, actcmd);
2339 break;
2340 }
2341 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002342}
2343
James Smarte59058c2008-08-24 21:49:00 -04002344/**
James Smart3621a712009-04-06 18:47:14 -04002345 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002346 * @phba: pointer to lpfc hba data structure.
2347 *
2348 * This routine initializes the HBA and brings a HBA online. During this
2349 * process, the management interface is blocked to prevent user space access
2350 * to the HBA interfering with the driver initialization.
2351 *
2352 * Return codes
2353 * 0 - successful
2354 * 1 - failed
2355 **/
dea31012005-04-17 16:05:31 -05002356int
James Smart2e0fef82007-06-17 19:56:36 -05002357lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002358{
Julia Lawall372bd282008-12-16 16:15:08 +01002359 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002360 struct lpfc_vport **vports;
2361 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002362
dea31012005-04-17 16:05:31 -05002363 if (!phba)
2364 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002365 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002366
James Smart2e0fef82007-06-17 19:56:36 -05002367 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002368 return 0;
2369
James Smarted957682007-06-17 19:56:37 -05002370 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002371 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002372
James Smart46fa3112007-04-25 09:51:45 -04002373 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002374
James Smart46fa3112007-04-25 09:51:45 -04002375 if (!lpfc_sli_queue_setup(phba)) {
2376 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002377 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002378 }
2379
James Smartda0436e2009-05-22 14:51:39 -04002380 if (phba->sli_rev == LPFC_SLI_REV4) {
2381 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2382 lpfc_unblock_mgmt_io(phba);
2383 return 1;
2384 }
2385 } else {
2386 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2387 lpfc_unblock_mgmt_io(phba);
2388 return 1;
2389 }
James Smart46fa3112007-04-25 09:51:45 -04002390 }
dea31012005-04-17 16:05:31 -05002391
James Smart549e55c2007-08-02 11:09:51 -04002392 vports = lpfc_create_vport_work_array(phba);
2393 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002394 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002395 struct Scsi_Host *shost;
2396 shost = lpfc_shost_from_vport(vports[i]);
2397 spin_lock_irq(shost->host_lock);
2398 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2399 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2400 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002401 if (phba->sli_rev == LPFC_SLI_REV4)
2402 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002403 spin_unlock_irq(shost->host_lock);
2404 }
James Smart09372822008-01-11 01:52:54 -05002405 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002406
James Smart46fa3112007-04-25 09:51:45 -04002407 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002408 return 0;
2409}
2410
James Smarte59058c2008-08-24 21:49:00 -04002411/**
James Smart3621a712009-04-06 18:47:14 -04002412 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002413 * @phba: pointer to lpfc hba data structure.
2414 *
2415 * This routine marks a HBA's management interface as not blocked. Once the
2416 * HBA's management interface is marked as not blocked, all the user space
2417 * access to the HBA, whether they are from sysfs interface or libdfc
2418 * interface will be allowed. The HBA is set to block the management interface
2419 * when the driver prepares the HBA interface for online or offline and then
2420 * set to unblock the management interface afterwards.
2421 **/
James Smart46fa3112007-04-25 09:51:45 -04002422void
James Smart46fa3112007-04-25 09:51:45 -04002423lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2424{
2425 unsigned long iflag;
2426
James Smart2e0fef82007-06-17 19:56:36 -05002427 spin_lock_irqsave(&phba->hbalock, iflag);
2428 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2429 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002430}
2431
James Smarte59058c2008-08-24 21:49:00 -04002432/**
James Smart3621a712009-04-06 18:47:14 -04002433 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002434 * @phba: pointer to lpfc hba data structure.
2435 *
2436 * This routine is invoked to prepare a HBA to be brought offline. It performs
2437 * unregistration login to all the nodes on all vports and flushes the mailbox
2438 * queue to make it ready to be brought offline.
2439 **/
James Smart46fa3112007-04-25 09:51:45 -04002440void
2441lpfc_offline_prep(struct lpfc_hba * phba)
2442{
James Smart2e0fef82007-06-17 19:56:36 -05002443 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002444 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002445 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002446 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002447 int i;
dea31012005-04-17 16:05:31 -05002448
James Smart2e0fef82007-06-17 19:56:36 -05002449 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002450 return;
dea31012005-04-17 16:05:31 -05002451
James Smart46fa3112007-04-25 09:51:45 -04002452 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002453
2454 lpfc_linkdown(phba);
2455
James Smart87af33f2007-10-27 13:37:43 -04002456 /* Issue an unreg_login to all nodes on all vports */
2457 vports = lpfc_create_vport_work_array(phba);
2458 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002459 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002460 if (vports[i]->load_flag & FC_UNLOADING)
2461 continue;
James Smart72100cc2010-02-12 14:43:01 -05002462 shost = lpfc_shost_from_vport(vports[i]);
2463 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002464 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002465 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2466 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002467 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002468
James Smart87af33f2007-10-27 13:37:43 -04002469 shost = lpfc_shost_from_vport(vports[i]);
2470 list_for_each_entry_safe(ndlp, next_ndlp,
2471 &vports[i]->fc_nodes,
2472 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002473 if (!NLP_CHK_NODE_ACT(ndlp))
2474 continue;
James Smart87af33f2007-10-27 13:37:43 -04002475 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2476 continue;
2477 if (ndlp->nlp_type & NLP_FABRIC) {
2478 lpfc_disc_state_machine(vports[i], ndlp,
2479 NULL, NLP_EVT_DEVICE_RECOVERY);
2480 lpfc_disc_state_machine(vports[i], ndlp,
2481 NULL, NLP_EVT_DEVICE_RM);
2482 }
2483 spin_lock_irq(shost->host_lock);
2484 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2485 spin_unlock_irq(shost->host_lock);
2486 lpfc_unreg_rpi(vports[i], ndlp);
2487 }
2488 }
2489 }
James Smart09372822008-01-11 01:52:54 -05002490 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002491
James Smart04c68492009-05-22 14:52:52 -04002492 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002493}
2494
James Smarte59058c2008-08-24 21:49:00 -04002495/**
James Smart3621a712009-04-06 18:47:14 -04002496 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002497 * @phba: pointer to lpfc hba data structure.
2498 *
2499 * This routine actually brings a HBA offline. It stops all the timers
2500 * associated with the HBA, brings down the SLI layer, and eventually
2501 * marks the HBA as in offline state for the upper layer protocol.
2502 **/
James Smart46fa3112007-04-25 09:51:45 -04002503void
James Smart2e0fef82007-06-17 19:56:36 -05002504lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002505{
James Smart549e55c2007-08-02 11:09:51 -04002506 struct Scsi_Host *shost;
2507 struct lpfc_vport **vports;
2508 int i;
James Smart46fa3112007-04-25 09:51:45 -04002509
James Smart549e55c2007-08-02 11:09:51 -04002510 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002511 return;
James Smart688a8862006-07-06 15:49:56 -04002512
James Smartda0436e2009-05-22 14:51:39 -04002513 /* stop port and all timers associated with this hba */
2514 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002515 vports = lpfc_create_vport_work_array(phba);
2516 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002517 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002518 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002519 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002520 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002521 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002522 /* Bring down the SLI Layer and cleanup. The HBA is offline
2523 now. */
2524 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002525 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002526 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002527 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002528 vports = lpfc_create_vport_work_array(phba);
2529 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002530 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002531 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002532 spin_lock_irq(shost->host_lock);
2533 vports[i]->work_port_events = 0;
2534 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2535 spin_unlock_irq(shost->host_lock);
2536 }
James Smart09372822008-01-11 01:52:54 -05002537 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002538}
2539
James Smarte59058c2008-08-24 21:49:00 -04002540/**
James Smart3621a712009-04-06 18:47:14 -04002541 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002542 * @phba: pointer to lpfc hba data structure.
2543 *
2544 * This routine is to free all the SCSI buffers and IOCBs from the driver
2545 * list back to kernel. It is called from lpfc_pci_remove_one to free
2546 * the internal resources before the device is removed from the system.
2547 *
2548 * Return codes
2549 * 0 - successful (for now, it always returns 0)
2550 **/
dea31012005-04-17 16:05:31 -05002551static int
James Smart2e0fef82007-06-17 19:56:36 -05002552lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002553{
2554 struct lpfc_scsi_buf *sb, *sb_next;
2555 struct lpfc_iocbq *io, *io_next;
2556
James Smart2e0fef82007-06-17 19:56:36 -05002557 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002558 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002559 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002560 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2561 list_del(&sb->list);
2562 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002563 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002564 kfree(sb);
2565 phba->total_scsi_bufs--;
2566 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002567 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002568
2569 /* Release all the lpfc_iocbq entries maintained by this host. */
2570 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2571 list_del(&io->list);
2572 kfree(io);
2573 phba->total_iocbq_bufs--;
2574 }
James Smart2e0fef82007-06-17 19:56:36 -05002575 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002576 return 0;
2577}
2578
James Smarte59058c2008-08-24 21:49:00 -04002579/**
James Smart3621a712009-04-06 18:47:14 -04002580 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002581 * @phba: pointer to lpfc hba data structure.
2582 * @instance: a unique integer ID to this FC port.
2583 * @dev: pointer to the device data structure.
2584 *
2585 * This routine creates a FC port for the upper layer protocol. The FC port
2586 * can be created on top of either a physical port or a virtual port provided
2587 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2588 * and associates the FC port created before adding the shost into the SCSI
2589 * layer.
2590 *
2591 * Return codes
2592 * @vport - pointer to the virtual N_Port data structure.
2593 * NULL - port create failed.
2594 **/
James Smart2e0fef82007-06-17 19:56:36 -05002595struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002596lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002597{
James Smart2e0fef82007-06-17 19:56:36 -05002598 struct lpfc_vport *vport;
2599 struct Scsi_Host *shost;
2600 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002601
James Smart3de2a652007-08-02 11:09:59 -04002602 if (dev != &phba->pcidev->dev)
2603 shost = scsi_host_alloc(&lpfc_vport_template,
2604 sizeof(struct lpfc_vport));
2605 else
2606 shost = scsi_host_alloc(&lpfc_template,
2607 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002608 if (!shost)
2609 goto out;
James Smart47a86172007-04-25 09:53:22 -04002610
James Smart2e0fef82007-06-17 19:56:36 -05002611 vport = (struct lpfc_vport *) shost->hostdata;
2612 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002613 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002614 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002615 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002616
James Smart3de2a652007-08-02 11:09:59 -04002617 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002618 shost->unique_id = instance;
2619 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002620 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002621 shost->this_id = -1;
2622 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002623 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002624 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04002625 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04002626 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2627 }
James Smart81301a92008-12-04 22:39:46 -05002628
James Smart47a86172007-04-25 09:53:22 -04002629 /*
James Smart2e0fef82007-06-17 19:56:36 -05002630 * Set initial can_queue value since 0 is no longer supported and
2631 * scsi_add_host will fail. This will be adjusted later based on the
2632 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002633 */
James Smart2e0fef82007-06-17 19:56:36 -05002634 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002635 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002636 shost->transportt = lpfc_vport_transport_template;
2637 vport->port_type = LPFC_NPIV_PORT;
2638 } else {
2639 shost->transportt = lpfc_transport_template;
2640 vport->port_type = LPFC_PHYSICAL_PORT;
2641 }
James Smart47a86172007-04-25 09:53:22 -04002642
James Smart2e0fef82007-06-17 19:56:36 -05002643 /* Initialize all internally managed lists. */
2644 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002645 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002646 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002647
James Smart2e0fef82007-06-17 19:56:36 -05002648 init_timer(&vport->fc_disctmo);
2649 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002650 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002651
James Smart2e0fef82007-06-17 19:56:36 -05002652 init_timer(&vport->fc_fdmitmo);
2653 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002654 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002655
James Smart2e0fef82007-06-17 19:56:36 -05002656 init_timer(&vport->els_tmofunc);
2657 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002658 vport->els_tmofunc.data = (unsigned long)vport;
James Bottomleyd139b9b2009-11-05 13:33:12 -06002659 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002660 if (error)
2661 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002662
James Smart549e55c2007-08-02 11:09:51 -04002663 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002664 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002665 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002666 return vport;
James Smart47a86172007-04-25 09:53:22 -04002667
James Smart2e0fef82007-06-17 19:56:36 -05002668out_put_shost:
2669 scsi_host_put(shost);
2670out:
2671 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002672}
2673
James Smarte59058c2008-08-24 21:49:00 -04002674/**
James Smart3621a712009-04-06 18:47:14 -04002675 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002676 * @vport: pointer to an lpfc virtual N_Port data structure.
2677 *
2678 * This routine destroys a FC port from the upper layer protocol. All the
2679 * resources associated with the port are released.
2680 **/
James Smart2e0fef82007-06-17 19:56:36 -05002681void
2682destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002683{
James Smart92d7f7b2007-06-17 19:56:38 -05002684 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2685 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002686
James Smart858c9f62007-06-17 19:56:39 -05002687 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002688 fc_remove_host(shost);
2689 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002690
James Smart92d7f7b2007-06-17 19:56:38 -05002691 spin_lock_irq(&phba->hbalock);
2692 list_del_init(&vport->listentry);
2693 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002694
James Smart92d7f7b2007-06-17 19:56:38 -05002695 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002696 return;
James Smart47a86172007-04-25 09:53:22 -04002697}
2698
James Smarte59058c2008-08-24 21:49:00 -04002699/**
James Smart3621a712009-04-06 18:47:14 -04002700 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002701 *
2702 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2703 * uses the kernel idr facility to perform the task.
2704 *
2705 * Return codes:
2706 * instance - a unique integer ID allocated as the new instance.
2707 * -1 - lpfc get instance failed.
2708 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002709int
2710lpfc_get_instance(void)
2711{
2712 int instance = 0;
2713
2714 /* Assign an unused number */
2715 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2716 return -1;
2717 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2718 return -1;
2719 return instance;
2720}
2721
James Smarte59058c2008-08-24 21:49:00 -04002722/**
James Smart3621a712009-04-06 18:47:14 -04002723 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002724 * @shost: pointer to SCSI host data structure.
2725 * @time: elapsed time of the scan in jiffies.
2726 *
2727 * This routine is called by the SCSI layer with a SCSI host to determine
2728 * whether the scan host is finished.
2729 *
2730 * Note: there is no scan_start function as adapter initialization will have
2731 * asynchronously kicked off the link initialization.
2732 *
2733 * Return codes
2734 * 0 - SCSI host scan is not over yet.
2735 * 1 - SCSI host scan is over.
2736 **/
James Smart47a86172007-04-25 09:53:22 -04002737int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2738{
James Smart2e0fef82007-06-17 19:56:36 -05002739 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2740 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002741 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002742
James Smart858c9f62007-06-17 19:56:39 -05002743 spin_lock_irq(shost->host_lock);
2744
James Smart51ef4c22007-08-02 11:10:31 -04002745 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002746 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002747 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002748 }
James Smart2e0fef82007-06-17 19:56:36 -05002749 if (time >= 30 * HZ) {
2750 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002751 "0461 Scanning longer than 30 "
2752 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002753 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002754 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002755 }
2756 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2757 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002758 "0465 Link down longer than 15 "
2759 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002760 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002761 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002762 }
2763
James Smart2e0fef82007-06-17 19:56:36 -05002764 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002765 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002766 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002767 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002768 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002769 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002770 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002771 goto finished;
2772
2773 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002774
2775finished:
James Smart858c9f62007-06-17 19:56:39 -05002776 spin_unlock_irq(shost->host_lock);
2777 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002778}
2779
James Smarte59058c2008-08-24 21:49:00 -04002780/**
James Smart3621a712009-04-06 18:47:14 -04002781 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002782 * @shost: pointer to SCSI host data structure.
2783 *
2784 * This routine initializes a given SCSI host attributes on a FC port. The
2785 * SCSI host can be either on top of a physical port or a virtual port.
2786 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002787void lpfc_host_attrib_init(struct Scsi_Host *shost)
2788{
2789 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2790 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002791 /*
James Smart2e0fef82007-06-17 19:56:36 -05002792 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002793 */
2794
James Smart2e0fef82007-06-17 19:56:36 -05002795 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2796 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002797 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2798
2799 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002800 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002801 fc_host_supported_fc4s(shost)[2] = 1;
2802 fc_host_supported_fc4s(shost)[7] = 1;
2803
James Smart92d7f7b2007-06-17 19:56:38 -05002804 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2805 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002806
2807 fc_host_supported_speeds(shost) = 0;
2808 if (phba->lmt & LMT_10Gb)
2809 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002810 if (phba->lmt & LMT_8Gb)
2811 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002812 if (phba->lmt & LMT_4Gb)
2813 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2814 if (phba->lmt & LMT_2Gb)
2815 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2816 if (phba->lmt & LMT_1Gb)
2817 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2818
2819 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002820 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2821 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002822
Mike Christie0af5d702010-09-15 16:52:31 -05002823 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
2824
James Smart47a86172007-04-25 09:53:22 -04002825 /* This value is also unchanging */
2826 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002827 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002828 fc_host_active_fc4s(shost)[2] = 1;
2829 fc_host_active_fc4s(shost)[7] = 1;
2830
James Smart92d7f7b2007-06-17 19:56:38 -05002831 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002832 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002833 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002834 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002835}
dea31012005-04-17 16:05:31 -05002836
James Smarte59058c2008-08-24 21:49:00 -04002837/**
James Smartda0436e2009-05-22 14:51:39 -04002838 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002839 * @phba: pointer to lpfc hba data structure.
2840 *
James Smartda0436e2009-05-22 14:51:39 -04002841 * This routine is invoked to stop an SLI3 device port, it stops the device
2842 * from generating interrupts and stops the device driver's timers for the
2843 * device.
James Smarte59058c2008-08-24 21:49:00 -04002844 **/
James Smartdb2378e2008-02-08 18:49:51 -05002845static void
James Smartda0436e2009-05-22 14:51:39 -04002846lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002847{
James Smartda0436e2009-05-22 14:51:39 -04002848 /* Clear all interrupt enable conditions */
2849 writel(0, phba->HCregaddr);
2850 readl(phba->HCregaddr); /* flush */
2851 /* Clear all pending interrupts */
2852 writel(0xffffffff, phba->HAregaddr);
2853 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002854
James Smartda0436e2009-05-22 14:51:39 -04002855 /* Reset some HBA SLI setup states */
2856 lpfc_stop_hba_timers(phba);
2857 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002858}
2859
James Smarte59058c2008-08-24 21:49:00 -04002860/**
James Smartda0436e2009-05-22 14:51:39 -04002861 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002862 * @phba: pointer to lpfc hba data structure.
2863 *
James Smartda0436e2009-05-22 14:51:39 -04002864 * This routine is invoked to stop an SLI4 device port, it stops the device
2865 * from generating interrupts and stops the device driver's timers for the
2866 * device.
2867 **/
2868static void
2869lpfc_stop_port_s4(struct lpfc_hba *phba)
2870{
2871 /* Reset some HBA SLI4 setup states */
2872 lpfc_stop_hba_timers(phba);
2873 phba->pport->work_port_events = 0;
2874 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04002875}
2876
2877/**
2878 * lpfc_stop_port - Wrapper function for stopping hba port
2879 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002880 *
James Smartda0436e2009-05-22 14:51:39 -04002881 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2882 * the API jump table function pointer from the lpfc_hba struct.
2883 **/
2884void
2885lpfc_stop_port(struct lpfc_hba *phba)
2886{
2887 phba->lpfc_stop_port(phba);
2888}
2889
2890/**
2891 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2892 * @phba: pointer to lpfc hba data structure.
2893 *
2894 * This routine is invoked to remove the driver default fcf record from
2895 * the port. This routine currently acts on FCF Index 0.
2896 *
2897 **/
2898void
2899lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2900{
2901 int rc = 0;
2902 LPFC_MBOXQ_t *mboxq;
2903 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2904 uint32_t mbox_tmo, req_len;
2905 uint32_t shdr_status, shdr_add_status;
2906
2907 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2908 if (!mboxq) {
2909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2910 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2911 return;
2912 }
2913
2914 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2915 sizeof(struct lpfc_sli4_cfg_mhdr);
2916 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2917 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2918 req_len, LPFC_SLI4_MBX_EMBED);
2919 /*
2920 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2921 * supports multiple FCF indices.
2922 */
2923 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2924 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2925 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
James Smartecfd03c2010-02-12 14:41:27 -05002926 phba->fcf.current_rec.fcf_indx);
James Smartda0436e2009-05-22 14:51:39 -04002927
2928 if (!phba->sli4_hba.intr_enable)
2929 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2930 else {
2931 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2932 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2933 }
2934 /* The IOCTL status is embedded in the mailbox subheader. */
2935 shdr_status = bf_get(lpfc_mbox_hdr_status,
2936 &del_fcf_record->header.cfg_shdr.response);
2937 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2938 &del_fcf_record->header.cfg_shdr.response);
2939 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2940 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2941 "2516 DEL FCF of default FCF Index failed "
2942 "mbx status x%x, status x%x add_status x%x\n",
2943 rc, shdr_status, shdr_add_status);
2944 }
2945 if (rc != MBX_TIMEOUT)
2946 mempool_free(mboxq, phba->mbox_mem_pool);
2947}
2948
2949/**
James Smartecfd03c2010-02-12 14:41:27 -05002950 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2951 * @phba: Pointer to hba for which this call is being executed.
2952 *
2953 * This routine starts the timer waiting for the FCF rediscovery to complete.
2954 **/
2955void
2956lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2957{
2958 unsigned long fcf_redisc_wait_tmo =
2959 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2960 /* Start fcf rediscovery wait period timer */
2961 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2962 spin_lock_irq(&phba->hbalock);
2963 /* Allow action to new fcf asynchronous event */
2964 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2965 /* Mark the FCF rediscovery pending state */
2966 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2967 spin_unlock_irq(&phba->hbalock);
2968}
2969
2970/**
2971 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2972 * @ptr: Map to lpfc_hba data structure pointer.
2973 *
2974 * This routine is invoked when waiting for FCF table rediscover has been
2975 * timed out. If new FCF record(s) has (have) been discovered during the
2976 * wait period, a new FCF event shall be added to the FCOE async event
2977 * list, and then worker thread shall be waked up for processing from the
2978 * worker thread context.
2979 **/
2980void
2981lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2982{
2983 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2984
2985 /* Don't send FCF rediscovery event if timer cancelled */
2986 spin_lock_irq(&phba->hbalock);
2987 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2988 spin_unlock_irq(&phba->hbalock);
2989 return;
2990 }
2991 /* Clear FCF rediscovery timer pending flag */
2992 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2993 /* FCF rediscovery event to worker thread */
2994 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2995 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002996 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2997 "2776 FCF rediscover wait timer expired, post "
2998 "a worker thread event for FCF table scan\n");
James Smartecfd03c2010-02-12 14:41:27 -05002999 /* wake up worker thread */
3000 lpfc_worker_wake_up(phba);
3001}
3002
3003/**
James Smart6669f9b2009-10-02 15:16:45 -04003004 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
3005 * @phba: pointer to lpfc hba data structure.
3006 *
3007 * This function uses the QUERY_FW_CFG mailbox command to determine if the
3008 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
3009 * was successful and the firmware supports FCoE. Any other return indicates
3010 * a error. It is assumed that this function will be called before interrupts
3011 * are enabled.
3012 **/
3013static int
3014lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
3015{
3016 int rc = 0;
3017 LPFC_MBOXQ_t *mboxq;
3018 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
3019 uint32_t length;
3020 uint32_t shdr_status, shdr_add_status;
3021
3022 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3023 if (!mboxq) {
3024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3025 "2621 Failed to allocate mbox for "
3026 "query firmware config cmd\n");
3027 return -ENOMEM;
3028 }
3029 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
3030 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
3031 sizeof(struct lpfc_sli4_cfg_mhdr));
3032 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
3033 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
3034 length, LPFC_SLI4_MBX_EMBED);
3035 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3036 /* The IOCTL status is embedded in the mailbox subheader. */
3037 shdr_status = bf_get(lpfc_mbox_hdr_status,
3038 &query_fw_cfg->header.cfg_shdr.response);
3039 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
3040 &query_fw_cfg->header.cfg_shdr.response);
3041 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
3042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3043 "2622 Query Firmware Config failed "
3044 "mbx status x%x, status x%x add_status x%x\n",
3045 rc, shdr_status, shdr_add_status);
3046 return -EINVAL;
3047 }
3048 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
3049 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3050 "2623 FCoE Function not supported by firmware. "
3051 "Function mode = %08x\n",
3052 query_fw_cfg->function_mode);
3053 return -EINVAL;
3054 }
3055 if (rc != MBX_TIMEOUT)
3056 mempool_free(mboxq, phba->mbox_mem_pool);
3057 return 0;
3058}
3059
3060/**
James Smartda0436e2009-05-22 14:51:39 -04003061 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3062 * @phba: pointer to lpfc hba data structure.
3063 * @acqe_link: pointer to the async link completion queue entry.
3064 *
3065 * This routine is to parse the SLI4 link-attention link fault code and
3066 * translate it into the base driver's read link attention mailbox command
3067 * status.
3068 *
3069 * Return: Link-attention status in terms of base driver's coding.
3070 **/
3071static uint16_t
3072lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3073 struct lpfc_acqe_link *acqe_link)
3074{
3075 uint16_t latt_fault;
3076
3077 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3078 case LPFC_ASYNC_LINK_FAULT_NONE:
3079 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3080 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3081 latt_fault = 0;
3082 break;
3083 default:
3084 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3085 "0398 Invalid link fault code: x%x\n",
3086 bf_get(lpfc_acqe_link_fault, acqe_link));
3087 latt_fault = MBXERR_ERROR;
3088 break;
3089 }
3090 return latt_fault;
3091}
3092
3093/**
3094 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3095 * @phba: pointer to lpfc hba data structure.
3096 * @acqe_link: pointer to the async link completion queue entry.
3097 *
3098 * This routine is to parse the SLI4 link attention type and translate it
3099 * into the base driver's link attention type coding.
3100 *
3101 * Return: Link attention type in terms of base driver's coding.
3102 **/
3103static uint8_t
3104lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3105 struct lpfc_acqe_link *acqe_link)
3106{
3107 uint8_t att_type;
3108
3109 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3110 case LPFC_ASYNC_LINK_STATUS_DOWN:
3111 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3112 att_type = AT_LINK_DOWN;
3113 break;
3114 case LPFC_ASYNC_LINK_STATUS_UP:
3115 /* Ignore physical link up events - wait for logical link up */
3116 att_type = AT_RESERVED;
3117 break;
3118 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3119 att_type = AT_LINK_UP;
3120 break;
3121 default:
3122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3123 "0399 Invalid link attention type: x%x\n",
3124 bf_get(lpfc_acqe_link_status, acqe_link));
3125 att_type = AT_RESERVED;
3126 break;
3127 }
3128 return att_type;
3129}
3130
3131/**
3132 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3133 * @phba: pointer to lpfc hba data structure.
3134 * @acqe_link: pointer to the async link completion queue entry.
3135 *
3136 * This routine is to parse the SLI4 link-attention link speed and translate
3137 * it into the base driver's link-attention link speed coding.
3138 *
3139 * Return: Link-attention link speed in terms of base driver's coding.
3140 **/
3141static uint8_t
3142lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3143 struct lpfc_acqe_link *acqe_link)
3144{
3145 uint8_t link_speed;
3146
3147 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3148 case LPFC_ASYNC_LINK_SPEED_ZERO:
3149 link_speed = LA_UNKNW_LINK;
3150 break;
3151 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3152 link_speed = LA_UNKNW_LINK;
3153 break;
3154 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3155 link_speed = LA_UNKNW_LINK;
3156 break;
3157 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3158 link_speed = LA_1GHZ_LINK;
3159 break;
3160 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3161 link_speed = LA_10GHZ_LINK;
3162 break;
3163 default:
3164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3165 "0483 Invalid link-attention link speed: x%x\n",
3166 bf_get(lpfc_acqe_link_speed, acqe_link));
3167 link_speed = LA_UNKNW_LINK;
3168 break;
3169 }
3170 return link_speed;
3171}
3172
3173/**
3174 * lpfc_sli4_async_link_evt - Process the asynchronous link event
3175 * @phba: pointer to lpfc hba data structure.
3176 * @acqe_link: pointer to the async link completion queue entry.
3177 *
3178 * This routine is to handle the SLI4 asynchronous link event.
3179 **/
3180static void
3181lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3182 struct lpfc_acqe_link *acqe_link)
3183{
3184 struct lpfc_dmabuf *mp;
3185 LPFC_MBOXQ_t *pmb;
3186 MAILBOX_t *mb;
3187 READ_LA_VAR *la;
3188 uint8_t att_type;
3189
3190 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3191 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3192 return;
James Smart32b97932009-07-19 10:01:21 -04003193 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003194 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3195 if (!pmb) {
3196 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3197 "0395 The mboxq allocation failed\n");
3198 return;
3199 }
3200 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3201 if (!mp) {
3202 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3203 "0396 The lpfc_dmabuf allocation failed\n");
3204 goto out_free_pmb;
3205 }
3206 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3207 if (!mp->virt) {
3208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3209 "0397 The mbuf allocation failed\n");
3210 goto out_free_dmabuf;
3211 }
3212
3213 /* Cleanup any outstanding ELS commands */
3214 lpfc_els_flush_all_cmd(phba);
3215
3216 /* Block ELS IOCBs until we have done process link event */
3217 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3218
3219 /* Update link event statistics */
3220 phba->sli.slistat.link_event++;
3221
3222 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3223 lpfc_read_la(phba, pmb, mp);
3224 pmb->vport = phba->pport;
3225
3226 /* Parse and translate status field */
3227 mb = &pmb->u.mb;
3228 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3229
3230 /* Parse and translate link attention fields */
3231 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3232 la->eventTag = acqe_link->event_tag;
3233 la->attType = att_type;
3234 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3235
3236 /* Fake the the following irrelvant fields */
3237 la->topology = TOPOLOGY_PT_PT;
3238 la->granted_AL_PA = 0;
3239 la->il = 0;
3240 la->pb = 0;
3241 la->fa = 0;
3242 la->mm = 0;
3243
3244 /* Keep the link status for extra SLI4 state machine reference */
3245 phba->sli4_hba.link_state.speed =
3246 bf_get(lpfc_acqe_link_speed, acqe_link);
3247 phba->sli4_hba.link_state.duplex =
3248 bf_get(lpfc_acqe_link_duplex, acqe_link);
3249 phba->sli4_hba.link_state.status =
3250 bf_get(lpfc_acqe_link_status, acqe_link);
3251 phba->sli4_hba.link_state.physical =
3252 bf_get(lpfc_acqe_link_physical, acqe_link);
3253 phba->sli4_hba.link_state.fault =
3254 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003255 phba->sli4_hba.link_state.logical_speed =
3256 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003257
3258 /* Invoke the lpfc_handle_latt mailbox command callback function */
3259 lpfc_mbx_cmpl_read_la(phba, pmb);
3260
3261 return;
3262
3263out_free_dmabuf:
3264 kfree(mp);
3265out_free_pmb:
3266 mempool_free(pmb, phba->mbox_mem_pool);
3267}
3268
3269/**
James Smartfc2b9892010-02-26 14:15:29 -05003270 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3271 * @vport: pointer to vport data structure.
3272 *
3273 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3274 * response to a CVL event.
3275 *
3276 * Return the pointer to the ndlp with the vport if successful, otherwise
3277 * return NULL.
3278 **/
3279static struct lpfc_nodelist *
3280lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3281{
3282 struct lpfc_nodelist *ndlp;
3283 struct Scsi_Host *shost;
3284 struct lpfc_hba *phba;
3285
3286 if (!vport)
3287 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003288 phba = vport->phba;
3289 if (!phba)
3290 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003291 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3292 if (!ndlp) {
3293 /* Cannot find existing Fabric ndlp, so allocate a new one */
3294 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3295 if (!ndlp)
3296 return 0;
3297 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3298 /* Set the node type */
3299 ndlp->nlp_type |= NLP_FABRIC;
3300 /* Put ndlp onto node list */
3301 lpfc_enqueue_node(vport, ndlp);
3302 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3303 /* re-setup ndlp without removing from node list */
3304 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3305 if (!ndlp)
3306 return 0;
3307 }
James Smart38b92ef2010-08-04 16:11:39 -04003308 if (phba->pport->port_state < LPFC_FLOGI)
James Smartfc2b9892010-02-26 14:15:29 -05003309 return NULL;
3310 /* If virtual link is not yet instantiated ignore CVL */
James Smart38b92ef2010-08-04 16:11:39 -04003311 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC))
James Smartfc2b9892010-02-26 14:15:29 -05003312 return NULL;
3313 shost = lpfc_shost_from_vport(vport);
3314 if (!shost)
3315 return NULL;
3316 lpfc_linkdown_port(vport);
3317 lpfc_cleanup_pending_mbox(vport);
3318 spin_lock_irq(shost->host_lock);
3319 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3320 spin_unlock_irq(shost->host_lock);
3321
3322 return ndlp;
3323}
3324
3325/**
3326 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3327 * @vport: pointer to lpfc hba data structure.
3328 *
3329 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3330 * response to a FCF dead event.
3331 **/
3332static void
3333lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3334{
3335 struct lpfc_vport **vports;
3336 int i;
3337
3338 vports = lpfc_create_vport_work_array(phba);
3339 if (vports)
3340 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3341 lpfc_sli4_perform_vport_cvl(vports[i]);
3342 lpfc_destroy_vport_work_array(phba, vports);
3343}
3344
3345/**
James Smartda0436e2009-05-22 14:51:39 -04003346 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3347 * @phba: pointer to lpfc hba data structure.
3348 * @acqe_link: pointer to the async fcoe completion queue entry.
3349 *
3350 * This routine is to handle the SLI4 asynchronous fcoe event.
3351 **/
3352static void
3353lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3354 struct lpfc_acqe_fcoe *acqe_fcoe)
3355{
3356 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3357 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003358 struct lpfc_vport *vport;
3359 struct lpfc_nodelist *ndlp;
3360 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003361 int active_vlink_present;
3362 struct lpfc_vport **vports;
3363 int i;
James Smartda0436e2009-05-22 14:51:39 -04003364
James Smart4d9ab992009-10-02 15:16:39 -04003365 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003366 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003367 switch (event_type) {
3368 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
James Smartecfd03c2010-02-12 14:41:27 -05003369 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
James Smart999d8132010-03-15 11:24:56 -04003370 if (event_type == LPFC_FCOE_EVENT_TYPE_NEW_FCF)
3371 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3372 LOG_DISCOVERY,
3373 "2546 New FCF found event: "
3374 "evt_tag:x%x, fcf_index:x%x\n",
3375 acqe_fcoe->event_tag,
3376 acqe_fcoe->index);
3377 else
3378 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3379 LOG_DISCOVERY,
3380 "2788 FCF parameter modified event: "
3381 "evt_tag:x%x, fcf_index:x%x\n",
3382 acqe_fcoe->event_tag,
3383 acqe_fcoe->index);
James Smart38b92ef2010-08-04 16:11:39 -04003384 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003385 /*
3386 * During period of FCF discovery, read the FCF
3387 * table record indexed by the event to update
3388 * FCF round robin failover eligible FCF bmask.
3389 */
3390 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3391 LOG_DISCOVERY,
3392 "2779 Read new FCF record with "
3393 "fcf_index:x%x for updating FCF "
3394 "round robin failover bmask\n",
3395 acqe_fcoe->index);
3396 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fcoe->index);
3397 }
James Smart38b92ef2010-08-04 16:11:39 -04003398
3399 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003400 spin_lock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003401 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
3402 spin_unlock_irq(&phba->hbalock);
3403 break;
3404 }
3405 /* If fast FCF failover rescan event is pending, do nothing */
3406 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3407 spin_unlock_irq(&phba->hbalock);
3408 break;
3409 }
3410
3411 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003412 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3413 spin_unlock_irq(&phba->hbalock);
3414 break;
3415 }
3416 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003417
James Smart0c9ab6f2010-02-26 14:15:57 -05003418 /* Otherwise, scan the entire FCF table and re-discover SAN */
3419 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3420 "2770 Start FCF table scan due to new FCF "
3421 "event: evt_tag:x%x, fcf_index:x%x\n",
3422 acqe_fcoe->event_tag, acqe_fcoe->index);
3423 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3424 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003425 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003426 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3427 "2547 Issue FCF scan read FCF mailbox "
3428 "command failed 0x%x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003429 break;
3430
3431 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003433 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003434 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3435 acqe_fcoe->event_tag);
3436 break;
3437
3438 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003439 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003440 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003441 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003442 acqe_fcoe->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04003443 /*
3444 * If we are in the middle of FCF failover process, clear
3445 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04003446 */
James Smartfc2b9892010-02-26 14:15:29 -05003447 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003448 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003449 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003450 /* Update FLOGI FCF failover eligible FCF bmask */
3451 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fcoe->index);
James Smartfc2b9892010-02-26 14:15:29 -05003452 break;
3453 }
James Smart38b92ef2010-08-04 16:11:39 -04003454 spin_unlock_irq(&phba->hbalock);
3455
3456 /* If the event is not for currently used fcf do nothing */
3457 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
3458 break;
3459
3460 /*
3461 * Otherwise, request the port to rediscover the entire FCF
3462 * table for a fast recovery from case that the current FCF
3463 * is no longer valid as we are not in the middle of FCF
3464 * failover process already.
3465 */
3466 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05003467 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003468 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003469 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003470
James Smart0c9ab6f2010-02-26 14:15:57 -05003471 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3472 "2771 Start FCF fast failover process due to "
3473 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3474 "\n", acqe_fcoe->event_tag, acqe_fcoe->index);
James Smartfc2b9892010-02-26 14:15:29 -05003475 rc = lpfc_sli4_redisc_fcf_table(phba);
3476 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003477 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3478 LOG_DISCOVERY,
3479 "2772 Issue FCF rediscover mabilbox "
3480 "command failed, fail through to FCF "
3481 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003482 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003483 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003484 spin_unlock_irq(&phba->hbalock);
3485 /*
3486 * Last resort will fail over by treating this
3487 * as a link down to FCF registration.
3488 */
3489 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003490 } else {
3491 /* Reset FCF roundrobin bmask for new discovery */
3492 memset(phba->fcf.fcf_rr_bmask, 0,
3493 sizeof(*phba->fcf.fcf_rr_bmask));
3494 /*
3495 * Handling fast FCF failover to a DEAD FCF event is
3496 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05003497 */
3498 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003499 }
James Smartda0436e2009-05-22 14:51:39 -04003500 break;
James Smart6669f9b2009-10-02 15:16:45 -04003501 case LPFC_FCOE_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003502 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003503 "2718 Clear Virtual Link Received for VPI 0x%x"
3504 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3505 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003506 acqe_fcoe->index - phba->vpi_base);
James Smartfc2b9892010-02-26 14:15:29 -05003507 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003508 if (!ndlp)
3509 break;
James Smart695a8142010-01-26 23:08:03 -05003510 active_vlink_present = 0;
3511
3512 vports = lpfc_create_vport_work_array(phba);
3513 if (vports) {
3514 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3515 i++) {
3516 if ((!(vports[i]->fc_flag &
3517 FC_VPORT_CVL_RCVD)) &&
3518 (vports[i]->port_state > LPFC_FDISC)) {
3519 active_vlink_present = 1;
3520 break;
3521 }
3522 }
3523 lpfc_destroy_vport_work_array(phba, vports);
3524 }
3525
3526 if (active_vlink_present) {
3527 /*
3528 * If there are other active VLinks present,
3529 * re-instantiate the Vlink using FDISC.
3530 */
James Smart6669f9b2009-10-02 15:16:45 -04003531 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003532 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003533 spin_lock_irq(shost->host_lock);
3534 ndlp->nlp_flag |= NLP_DELAY_TMO;
3535 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003536 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3537 vport->port_state = LPFC_FDISC;
3538 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003539 /*
3540 * Otherwise, we request port to rediscover
3541 * the entire FCF table for a fast recovery
3542 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003543 * is no longer valid if we are not already
3544 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003545 */
James Smartfc2b9892010-02-26 14:15:29 -05003546 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003547 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003548 spin_unlock_irq(&phba->hbalock);
3549 break;
3550 }
3551 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003552 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003553 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003554 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3555 LOG_DISCOVERY,
3556 "2773 Start FCF fast failover due "
3557 "to CVL event: evt_tag:x%x\n",
3558 acqe_fcoe->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003559 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003560 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003561 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3562 LOG_DISCOVERY,
3563 "2774 Issue FCF rediscover "
3564 "mabilbox command failed, "
3565 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003566 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003567 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003568 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003569 /*
3570 * Last resort will be re-try on the
3571 * the current registered FCF entry.
3572 */
3573 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003574 } else
3575 /*
3576 * Reset FCF roundrobin bmask for new
3577 * discovery.
3578 */
3579 memset(phba->fcf.fcf_rr_bmask, 0,
3580 sizeof(*phba->fcf.fcf_rr_bmask));
James Smart6669f9b2009-10-02 15:16:45 -04003581 }
3582 break;
James Smartda0436e2009-05-22 14:51:39 -04003583 default:
3584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3585 "0288 Unknown FCoE event type 0x%x event tag "
3586 "0x%x\n", event_type, acqe_fcoe->event_tag);
3587 break;
3588 }
3589}
3590
3591/**
3592 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3593 * @phba: pointer to lpfc hba data structure.
3594 * @acqe_link: pointer to the async dcbx completion queue entry.
3595 *
3596 * This routine is to handle the SLI4 asynchronous dcbx event.
3597 **/
3598static void
3599lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3600 struct lpfc_acqe_dcbx *acqe_dcbx)
3601{
James Smart4d9ab992009-10-02 15:16:39 -04003602 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3604 "0290 The SLI4 DCBX asynchronous event is not "
3605 "handled yet\n");
3606}
3607
3608/**
James Smartb19a0612010-04-06 14:48:51 -04003609 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3610 * @phba: pointer to lpfc hba data structure.
3611 * @acqe_link: pointer to the async grp5 completion queue entry.
3612 *
3613 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3614 * is an asynchronous notified of a logical link speed change. The Port
3615 * reports the logical link speed in units of 10Mbps.
3616 **/
3617static void
3618lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3619 struct lpfc_acqe_grp5 *acqe_grp5)
3620{
3621 uint16_t prev_ll_spd;
3622
3623 phba->fc_eventTag = acqe_grp5->event_tag;
3624 phba->fcoe_eventtag = acqe_grp5->event_tag;
3625 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3626 phba->sli4_hba.link_state.logical_speed =
3627 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3628 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3629 "2789 GRP5 Async Event: Updating logical link speed "
3630 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3631 (phba->sli4_hba.link_state.logical_speed*10));
3632}
3633
3634/**
James Smartda0436e2009-05-22 14:51:39 -04003635 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3636 * @phba: pointer to lpfc hba data structure.
3637 *
3638 * This routine is invoked by the worker thread to process all the pending
3639 * SLI4 asynchronous events.
3640 **/
3641void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3642{
3643 struct lpfc_cq_event *cq_event;
3644
3645 /* First, declare the async event has been handled */
3646 spin_lock_irq(&phba->hbalock);
3647 phba->hba_flag &= ~ASYNC_EVENT;
3648 spin_unlock_irq(&phba->hbalock);
3649 /* Now, handle all the async events */
3650 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3651 /* Get the first event from the head of the event queue */
3652 spin_lock_irq(&phba->hbalock);
3653 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3654 cq_event, struct lpfc_cq_event, list);
3655 spin_unlock_irq(&phba->hbalock);
3656 /* Process the asynchronous event */
3657 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3658 case LPFC_TRAILER_CODE_LINK:
3659 lpfc_sli4_async_link_evt(phba,
3660 &cq_event->cqe.acqe_link);
3661 break;
3662 case LPFC_TRAILER_CODE_FCOE:
3663 lpfc_sli4_async_fcoe_evt(phba,
3664 &cq_event->cqe.acqe_fcoe);
3665 break;
3666 case LPFC_TRAILER_CODE_DCBX:
3667 lpfc_sli4_async_dcbx_evt(phba,
3668 &cq_event->cqe.acqe_dcbx);
3669 break;
James Smartb19a0612010-04-06 14:48:51 -04003670 case LPFC_TRAILER_CODE_GRP5:
3671 lpfc_sli4_async_grp5_evt(phba,
3672 &cq_event->cqe.acqe_grp5);
3673 break;
James Smartda0436e2009-05-22 14:51:39 -04003674 default:
3675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3676 "1804 Invalid asynchrous event code: "
3677 "x%x\n", bf_get(lpfc_trailer_code,
3678 &cq_event->cqe.mcqe_cmpl));
3679 break;
3680 }
3681 /* Free the completion event processed to the free pool */
3682 lpfc_sli4_cq_event_release(phba, cq_event);
3683 }
3684}
3685
3686/**
James Smartecfd03c2010-02-12 14:41:27 -05003687 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3688 * @phba: pointer to lpfc hba data structure.
3689 *
3690 * This routine is invoked by the worker thread to process FCF table
3691 * rediscovery pending completion event.
3692 **/
3693void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3694{
3695 int rc;
3696
3697 spin_lock_irq(&phba->hbalock);
3698 /* Clear FCF rediscovery timeout event */
3699 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3700 /* Clear driver fast failover FCF record flag */
3701 phba->fcf.failover_rec.flag = 0;
3702 /* Set state for FCF fast failover */
3703 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3704 spin_unlock_irq(&phba->hbalock);
3705
3706 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05003707 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3708 "2777 Start FCF table scan after FCF "
3709 "rediscovery quiescent period over\n");
3710 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05003711 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003712 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3713 "2747 Issue FCF scan read FCF mailbox "
3714 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05003715}
3716
3717/**
James Smartda0436e2009-05-22 14:51:39 -04003718 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3719 * @phba: pointer to lpfc hba data structure.
3720 * @dev_grp: The HBA PCI-Device group number.
3721 *
3722 * This routine is invoked to set up the per HBA PCI-Device group function
3723 * API jump table entries.
3724 *
3725 * Return: 0 if success, otherwise -ENODEV
3726 **/
3727int
3728lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003729{
3730 int rc;
3731
James Smartda0436e2009-05-22 14:51:39 -04003732 /* Set up lpfc PCI-device group */
3733 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003734
James Smartda0436e2009-05-22 14:51:39 -04003735 /* The LPFC_PCI_DEV_OC uses SLI4 */
3736 if (dev_grp == LPFC_PCI_DEV_OC)
3737 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003738
James Smartda0436e2009-05-22 14:51:39 -04003739 /* Set up device INIT API function jump table */
3740 rc = lpfc_init_api_table_setup(phba, dev_grp);
3741 if (rc)
3742 return -ENODEV;
3743 /* Set up SCSI API function jump table */
3744 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3745 if (rc)
3746 return -ENODEV;
3747 /* Set up SLI API function jump table */
3748 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3749 if (rc)
3750 return -ENODEV;
3751 /* Set up MBOX API function jump table */
3752 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3753 if (rc)
3754 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003755
James Smartda0436e2009-05-22 14:51:39 -04003756 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003757}
3758
3759/**
James Smart3621a712009-04-06 18:47:14 -04003760 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003761 * @phba: pointer to lpfc hba data structure.
3762 * @intr_mode: active interrupt mode adopted.
3763 *
3764 * This routine it invoked to log the currently used active interrupt mode
3765 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003766 **/
3767static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003768{
3769 switch (intr_mode) {
3770 case 0:
3771 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3772 "0470 Enable INTx interrupt mode.\n");
3773 break;
3774 case 1:
3775 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3776 "0481 Enabled MSI interrupt mode.\n");
3777 break;
3778 case 2:
3779 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3780 "0480 Enabled MSI-X interrupt mode.\n");
3781 break;
3782 default:
3783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3784 "0482 Illegal interrupt mode.\n");
3785 break;
3786 }
3787 return;
3788}
3789
James Smart5b75da22008-12-04 22:39:35 -05003790/**
James Smart3772a992009-05-22 14:50:54 -04003791 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003792 * @phba: pointer to lpfc hba data structure.
3793 *
James Smart3772a992009-05-22 14:50:54 -04003794 * This routine is invoked to enable the PCI device that is common to all
3795 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003796 *
3797 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003798 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003799 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003800 **/
James Smart3772a992009-05-22 14:50:54 -04003801static int
3802lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003803{
James Smart3772a992009-05-22 14:50:54 -04003804 struct pci_dev *pdev;
3805 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003806
James Smart3772a992009-05-22 14:50:54 -04003807 /* Obtain PCI device reference */
3808 if (!phba->pcidev)
3809 goto out_error;
3810 else
3811 pdev = phba->pcidev;
3812 /* Select PCI BARs */
3813 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3814 /* Enable PCI device */
3815 if (pci_enable_device_mem(pdev))
3816 goto out_error;
3817 /* Request PCI resource for the device */
3818 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3819 goto out_disable_device;
3820 /* Set up device as PCI master and save state for EEH */
3821 pci_set_master(pdev);
3822 pci_try_set_mwi(pdev);
3823 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003824
James Smart3772a992009-05-22 14:50:54 -04003825 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003826
James Smart3772a992009-05-22 14:50:54 -04003827out_disable_device:
3828 pci_disable_device(pdev);
3829out_error:
3830 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003831}
3832
3833/**
James Smart3772a992009-05-22 14:50:54 -04003834 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003835 * @phba: pointer to lpfc hba data structure.
3836 *
James Smart3772a992009-05-22 14:50:54 -04003837 * This routine is invoked to disable the PCI device that is common to all
3838 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003839 **/
3840static void
James Smart3772a992009-05-22 14:50:54 -04003841lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003842{
James Smart3772a992009-05-22 14:50:54 -04003843 struct pci_dev *pdev;
3844 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003845
James Smart3772a992009-05-22 14:50:54 -04003846 /* Obtain PCI device reference */
3847 if (!phba->pcidev)
3848 return;
3849 else
3850 pdev = phba->pcidev;
3851 /* Select PCI BARs */
3852 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3853 /* Release PCI resource and disable PCI device */
3854 pci_release_selected_regions(pdev, bars);
3855 pci_disable_device(pdev);
3856 /* Null out PCI private reference to driver */
3857 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003858
3859 return;
3860}
3861
3862/**
James Smart3772a992009-05-22 14:50:54 -04003863 * lpfc_reset_hba - Reset a hba
3864 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003865 *
James Smart3772a992009-05-22 14:50:54 -04003866 * This routine is invoked to reset a hba device. It brings the HBA
3867 * offline, performs a board restart, and then brings the board back
3868 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3869 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003870 **/
James Smart3772a992009-05-22 14:50:54 -04003871void
3872lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003873{
James Smart3772a992009-05-22 14:50:54 -04003874 /* If resets are disabled then set error state and return. */
3875 if (!phba->cfg_enable_hba_reset) {
3876 phba->link_state = LPFC_HBA_ERROR;
3877 return;
3878 }
3879 lpfc_offline_prep(phba);
3880 lpfc_offline(phba);
3881 lpfc_sli_brdrestart(phba);
3882 lpfc_online(phba);
3883 lpfc_unblock_mgmt_io(phba);
3884}
dea31012005-04-17 16:05:31 -05003885
James Smart3772a992009-05-22 14:50:54 -04003886/**
3887 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3888 * @phba: pointer to lpfc hba data structure.
3889 *
3890 * This routine is invoked to set up the driver internal resources specific to
3891 * support the SLI-3 HBA device it attached to.
3892 *
3893 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003894 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003895 * other values - error
3896 **/
3897static int
3898lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3899{
3900 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003901
James Smart3772a992009-05-22 14:50:54 -04003902 /*
3903 * Initialize timers used by driver
3904 */
dea31012005-04-17 16:05:31 -05003905
James Smart3772a992009-05-22 14:50:54 -04003906 /* Heartbeat timer */
3907 init_timer(&phba->hb_tmofunc);
3908 phba->hb_tmofunc.function = lpfc_hb_timeout;
3909 phba->hb_tmofunc.data = (unsigned long)phba;
3910
3911 psli = &phba->sli;
3912 /* MBOX heartbeat timer */
3913 init_timer(&psli->mbox_tmo);
3914 psli->mbox_tmo.function = lpfc_mbox_timeout;
3915 psli->mbox_tmo.data = (unsigned long) phba;
3916 /* FCP polling mode timer */
3917 init_timer(&phba->fcp_poll_timer);
3918 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3919 phba->fcp_poll_timer.data = (unsigned long) phba;
3920 /* Fabric block timer */
3921 init_timer(&phba->fabric_block_timer);
3922 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3923 phba->fabric_block_timer.data = (unsigned long) phba;
3924 /* EA polling mode timer */
3925 init_timer(&phba->eratt_poll);
3926 phba->eratt_poll.function = lpfc_poll_eratt;
3927 phba->eratt_poll.data = (unsigned long) phba;
3928
3929 /* Host attention work mask setup */
3930 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3931 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3932
3933 /* Get all the module params for configuring this host */
3934 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04003935 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
3936 phba->menlo_flag |= HBA_MENLO_SUPPORT;
3937 /* check for menlo minimum sg count */
3938 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
3939 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
3940 }
3941
James Smart3772a992009-05-22 14:50:54 -04003942 /*
3943 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3944 * used to create the sg_dma_buf_pool must be dynamically calculated.
3945 * 2 segments are added since the IOCB needs a command and response bde.
3946 */
3947 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3948 sizeof(struct fcp_rsp) +
3949 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3950
3951 if (phba->cfg_enable_bg) {
3952 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3953 phba->cfg_sg_dma_buf_size +=
3954 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3955 }
3956
3957 /* Also reinitialize the host templates with new values. */
3958 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3959 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3960
3961 phba->max_vpi = LPFC_MAX_VPI;
3962 /* This will be set to correct value after config_port mbox */
3963 phba->max_vports = 0;
3964
3965 /*
3966 * Initialize the SLI Layer to run with lpfc HBAs.
3967 */
3968 lpfc_sli_setup(phba);
3969 lpfc_sli_queue_setup(phba);
3970
3971 /* Allocate device driver memory */
3972 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3973 return -ENOMEM;
3974
3975 return 0;
3976}
3977
3978/**
3979 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3980 * @phba: pointer to lpfc hba data structure.
3981 *
3982 * This routine is invoked to unset the driver internal resources set up
3983 * specific for supporting the SLI-3 HBA device it attached to.
3984 **/
3985static void
3986lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3987{
3988 /* Free device driver memory allocated */
3989 lpfc_mem_free_all(phba);
3990
3991 return;
3992}
3993
3994/**
James Smartda0436e2009-05-22 14:51:39 -04003995 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3996 * @phba: pointer to lpfc hba data structure.
3997 *
3998 * This routine is invoked to set up the driver internal resources specific to
3999 * support the SLI-4 HBA device it attached to.
4000 *
4001 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004002 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004003 * other values - error
4004 **/
4005static int
4006lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4007{
4008 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004009 LPFC_MBOXQ_t *mboxq;
4010 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4011 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4012 struct lpfc_mqe *mqe;
James Smart0c9ab6f2010-02-26 14:15:57 -05004013 int longs;
James Smartda0436e2009-05-22 14:51:39 -04004014
4015 /* Before proceed, wait for POST done and device ready */
4016 rc = lpfc_sli4_post_status_check(phba);
4017 if (rc)
4018 return -ENODEV;
4019
4020 /*
4021 * Initialize timers used by driver
4022 */
4023
4024 /* Heartbeat timer */
4025 init_timer(&phba->hb_tmofunc);
4026 phba->hb_tmofunc.function = lpfc_hb_timeout;
4027 phba->hb_tmofunc.data = (unsigned long)phba;
4028
4029 psli = &phba->sli;
4030 /* MBOX heartbeat timer */
4031 init_timer(&psli->mbox_tmo);
4032 psli->mbox_tmo.function = lpfc_mbox_timeout;
4033 psli->mbox_tmo.data = (unsigned long) phba;
4034 /* Fabric block timer */
4035 init_timer(&phba->fabric_block_timer);
4036 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4037 phba->fabric_block_timer.data = (unsigned long) phba;
4038 /* EA polling mode timer */
4039 init_timer(&phba->eratt_poll);
4040 phba->eratt_poll.function = lpfc_poll_eratt;
4041 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004042 /* FCF rediscover timer */
4043 init_timer(&phba->fcf.redisc_wait);
4044 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4045 phba->fcf.redisc_wait.data = (unsigned long)phba;
4046
James Smartda0436e2009-05-22 14:51:39 -04004047 /*
4048 * We need to do a READ_CONFIG mailbox command here before
4049 * calling lpfc_get_cfgparam. For VFs this will report the
4050 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4051 * All of the resources allocated
4052 * for this Port are tied to these values.
4053 */
4054 /* Get all the module params for configuring this host */
4055 lpfc_get_cfgparam(phba);
4056 phba->max_vpi = LPFC_MAX_VPI;
4057 /* This will be set to correct value after the read_config mbox */
4058 phba->max_vports = 0;
4059
4060 /* Program the default value of vlan_id and fc_map */
4061 phba->valid_vlan = 0;
4062 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4063 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4064 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4065
4066 /*
4067 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4068 * used to create the sg_dma_buf_pool must be dynamically calculated.
4069 * 2 segments are added since the IOCB needs a command and response bde.
4070 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004071 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004072 */
James Smart28baac72010-02-12 14:42:03 -05004073 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4074 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
4075 /* Feature Level 1 hardware is limited to 2 pages */
4076 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
4077 LPFC_SLI_INTF_FEATURELEVEL1_1))
4078 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004079 else
James Smart28baac72010-02-12 14:42:03 -05004080 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4081 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4082 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4083 dma_buf_size = dma_buf_size << 1)
4084 ;
4085 if (dma_buf_size == max_buf_size)
4086 phba->cfg_sg_seg_cnt = (dma_buf_size -
4087 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4088 (2 * sizeof(struct sli4_sge))) /
4089 sizeof(struct sli4_sge);
4090 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004091
4092 /* Initialize buffer queue management fields */
4093 hbq_count = lpfc_sli_hbq_count();
4094 for (i = 0; i < hbq_count; ++i)
4095 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4096 INIT_LIST_HEAD(&phba->rb_pend_list);
4097 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4098 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4099
4100 /*
4101 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4102 */
4103 /* Initialize the Abort scsi buffer list used by driver */
4104 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4105 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4106 /* This abort list used by worker thread */
4107 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4108
4109 /*
4110 * Initialize dirver internal slow-path work queues
4111 */
4112
4113 /* Driver internel slow-path CQ Event pool */
4114 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4115 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004116 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004117 /* Asynchronous event CQ Event work queue list */
4118 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4119 /* Fast-path XRI aborted CQ Event work queue list */
4120 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4121 /* Slow-path XRI aborted CQ Event work queue list */
4122 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4123 /* Receive queue CQ Event work queue list */
4124 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4125
4126 /* Initialize the driver internal SLI layer lists. */
4127 lpfc_sli_setup(phba);
4128 lpfc_sli_queue_setup(phba);
4129
4130 /* Allocate device driver memory */
4131 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4132 if (rc)
4133 return -ENOMEM;
4134
4135 /* Create the bootstrap mailbox command */
4136 rc = lpfc_create_bootstrap_mbox(phba);
4137 if (unlikely(rc))
4138 goto out_free_mem;
4139
4140 /* Set up the host's endian order with the device. */
4141 rc = lpfc_setup_endian_order(phba);
4142 if (unlikely(rc))
4143 goto out_free_bsmbx;
4144
James Smart6669f9b2009-10-02 15:16:45 -04004145 rc = lpfc_sli4_fw_cfg_check(phba);
4146 if (unlikely(rc))
4147 goto out_free_bsmbx;
4148
James Smartda0436e2009-05-22 14:51:39 -04004149 /* Set up the hba's configuration parameters. */
4150 rc = lpfc_sli4_read_config(phba);
4151 if (unlikely(rc))
4152 goto out_free_bsmbx;
4153
4154 /* Perform a function reset */
4155 rc = lpfc_pci_function_reset(phba);
4156 if (unlikely(rc))
4157 goto out_free_bsmbx;
4158
James Smartcb5172e2010-03-15 11:25:07 -04004159 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4160 GFP_KERNEL);
4161 if (!mboxq) {
4162 rc = -ENOMEM;
4163 goto out_free_bsmbx;
4164 }
4165
4166 /* Get the Supported Pages. It is always available. */
4167 lpfc_supported_pages(mboxq);
4168 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4169 if (unlikely(rc)) {
4170 rc = -EIO;
4171 mempool_free(mboxq, phba->mbox_mem_pool);
4172 goto out_free_bsmbx;
4173 }
4174
4175 mqe = &mboxq->u.mqe;
4176 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4177 LPFC_MAX_SUPPORTED_PAGES);
4178 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4179 switch (pn_page[i]) {
4180 case LPFC_SLI4_PARAMETERS:
4181 phba->sli4_hba.pc_sli4_params.supported = 1;
4182 break;
4183 default:
4184 break;
4185 }
4186 }
4187
4188 /* Read the port's SLI4 Parameters capabilities if supported. */
4189 if (phba->sli4_hba.pc_sli4_params.supported)
4190 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4191 mempool_free(mboxq, phba->mbox_mem_pool);
4192 if (rc) {
4193 rc = -EIO;
4194 goto out_free_bsmbx;
4195 }
James Smartda0436e2009-05-22 14:51:39 -04004196 /* Create all the SLI4 queues */
4197 rc = lpfc_sli4_queue_create(phba);
4198 if (rc)
4199 goto out_free_bsmbx;
4200
4201 /* Create driver internal CQE event pool */
4202 rc = lpfc_sli4_cq_event_pool_create(phba);
4203 if (rc)
4204 goto out_destroy_queue;
4205
4206 /* Initialize and populate the iocb list per host */
4207 rc = lpfc_init_sgl_list(phba);
4208 if (rc) {
4209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4210 "1400 Failed to initialize sgl list.\n");
4211 goto out_destroy_cq_event_pool;
4212 }
4213 rc = lpfc_init_active_sgl_array(phba);
4214 if (rc) {
4215 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4216 "1430 Failed to initialize sgl list.\n");
4217 goto out_free_sgl_list;
4218 }
4219
4220 rc = lpfc_sli4_init_rpi_hdrs(phba);
4221 if (rc) {
4222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4223 "1432 Failed to initialize rpi headers.\n");
4224 goto out_free_active_sgl;
4225 }
4226
James Smart0c9ab6f2010-02-26 14:15:57 -05004227 /* Allocate eligible FCF bmask memory for FCF round robin failover */
4228 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4229 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4230 GFP_KERNEL);
4231 if (!phba->fcf.fcf_rr_bmask) {
4232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4233 "2759 Failed allocate memory for FCF round "
4234 "robin failover bmask\n");
4235 goto out_remove_rpi_hdrs;
4236 }
4237
James Smartda0436e2009-05-22 14:51:39 -04004238 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4239 phba->cfg_fcp_eq_count), GFP_KERNEL);
4240 if (!phba->sli4_hba.fcp_eq_hdl) {
4241 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4242 "2572 Failed allocate memory for fast-path "
4243 "per-EQ handle array\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004244 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04004245 }
4246
4247 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4248 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4249 if (!phba->sli4_hba.msix_entries) {
4250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4251 "2573 Failed allocate memory for msi-x "
4252 "interrupt vector entries\n");
4253 goto out_free_fcp_eq_hdl;
4254 }
4255
4256 return rc;
4257
4258out_free_fcp_eq_hdl:
4259 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004260out_free_fcf_rr_bmask:
4261 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004262out_remove_rpi_hdrs:
4263 lpfc_sli4_remove_rpi_hdrs(phba);
4264out_free_active_sgl:
4265 lpfc_free_active_sgl(phba);
4266out_free_sgl_list:
4267 lpfc_free_sgl_list(phba);
4268out_destroy_cq_event_pool:
4269 lpfc_sli4_cq_event_pool_destroy(phba);
4270out_destroy_queue:
4271 lpfc_sli4_queue_destroy(phba);
4272out_free_bsmbx:
4273 lpfc_destroy_bootstrap_mbox(phba);
4274out_free_mem:
4275 lpfc_mem_free(phba);
4276 return rc;
4277}
4278
4279/**
4280 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4281 * @phba: pointer to lpfc hba data structure.
4282 *
4283 * This routine is invoked to unset the driver internal resources set up
4284 * specific for supporting the SLI-4 HBA device it attached to.
4285 **/
4286static void
4287lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4288{
4289 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4290
4291 /* unregister default FCFI from the HBA */
4292 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4293
4294 /* Free the default FCR table */
4295 lpfc_sli_remove_dflt_fcf(phba);
4296
4297 /* Free memory allocated for msi-x interrupt vector entries */
4298 kfree(phba->sli4_hba.msix_entries);
4299
4300 /* Free memory allocated for fast-path work queue handles */
4301 kfree(phba->sli4_hba.fcp_eq_hdl);
4302
4303 /* Free the allocated rpi headers. */
4304 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004305 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004306
James Smart0c9ab6f2010-02-26 14:15:57 -05004307 /* Free eligible FCF index bmask */
4308 kfree(phba->fcf.fcf_rr_bmask);
4309
James Smartda0436e2009-05-22 14:51:39 -04004310 /* Free the ELS sgl list */
4311 lpfc_free_active_sgl(phba);
4312 lpfc_free_sgl_list(phba);
4313
4314 /* Free the SCSI sgl management array */
4315 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4316
4317 /* Free the SLI4 queues */
4318 lpfc_sli4_queue_destroy(phba);
4319
4320 /* Free the completion queue EQ event pool */
4321 lpfc_sli4_cq_event_release_all(phba);
4322 lpfc_sli4_cq_event_pool_destroy(phba);
4323
4324 /* Reset SLI4 HBA FCoE function */
4325 lpfc_pci_function_reset(phba);
4326
4327 /* Free the bsmbx region. */
4328 lpfc_destroy_bootstrap_mbox(phba);
4329
4330 /* Free the SLI Layer memory with SLI4 HBAs */
4331 lpfc_mem_free_all(phba);
4332
4333 /* Free the current connect table */
4334 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004335 &phba->fcf_conn_rec_list, list) {
4336 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004337 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004338 }
James Smartda0436e2009-05-22 14:51:39 -04004339
4340 return;
4341}
4342
4343/**
James Smart3772a992009-05-22 14:50:54 -04004344 * lpfc_init_api_table_setup - Set up init api fucntion jump table
4345 * @phba: The hba struct for which this call is being executed.
4346 * @dev_grp: The HBA PCI-Device group number.
4347 *
4348 * This routine sets up the device INIT interface API function jump table
4349 * in @phba struct.
4350 *
4351 * Returns: 0 - success, -ENODEV - failure.
4352 **/
4353int
4354lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4355{
James Smart84d1b002010-02-12 14:42:33 -05004356 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4357 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart3772a992009-05-22 14:50:54 -04004358 switch (dev_grp) {
4359 case LPFC_PCI_DEV_LP:
4360 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4361 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4362 phba->lpfc_stop_port = lpfc_stop_port_s3;
4363 break;
James Smartda0436e2009-05-22 14:51:39 -04004364 case LPFC_PCI_DEV_OC:
4365 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4366 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4367 phba->lpfc_stop_port = lpfc_stop_port_s4;
4368 break;
James Smart3772a992009-05-22 14:50:54 -04004369 default:
4370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4371 "1431 Invalid HBA PCI-device group: 0x%x\n",
4372 dev_grp);
4373 return -ENODEV;
4374 break;
4375 }
4376 return 0;
4377}
4378
4379/**
4380 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4381 * @phba: pointer to lpfc hba data structure.
4382 *
4383 * This routine is invoked to set up the driver internal resources before the
4384 * device specific resource setup to support the HBA device it attached to.
4385 *
4386 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004387 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004388 * other values - error
4389 **/
4390static int
4391lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4392{
4393 /*
4394 * Driver resources common to all SLI revisions
4395 */
James Smartea2151b2008-09-07 11:52:10 -04004396 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004397 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004398
James Smarte47c9092008-02-08 18:49:26 -05004399 /* Initialize ndlp management spinlock */
4400 spin_lock_init(&phba->ndlp_lock);
4401
James Smart2e0fef82007-06-17 19:56:36 -05004402 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004403 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004404 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004405
James Smart3772a992009-05-22 14:50:54 -04004406 /* Initialize the wait queue head for the kernel thread */
4407 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004408
James Smart3772a992009-05-22 14:50:54 -04004409 /* Initialize the scsi buffer list used by driver for scsi IO */
4410 spin_lock_init(&phba->scsi_buf_list_lock);
4411 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004412
James Smart3772a992009-05-22 14:50:54 -04004413 /* Initialize the fabric iocb list */
4414 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004415
James Smart3772a992009-05-22 14:50:54 -04004416 /* Initialize list to save ELS buffers */
4417 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004418
James Smart3772a992009-05-22 14:50:54 -04004419 /* Initialize FCF connection rec list */
4420 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004421
James Smart3772a992009-05-22 14:50:54 -04004422 return 0;
4423}
dea31012005-04-17 16:05:31 -05004424
James Smart3772a992009-05-22 14:50:54 -04004425/**
4426 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4427 * @phba: pointer to lpfc hba data structure.
4428 *
4429 * This routine is invoked to set up the driver internal resources after the
4430 * device specific resource setup to support the HBA device it attached to.
4431 *
4432 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004433 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004434 * other values - error
4435 **/
4436static int
4437lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4438{
4439 int error;
4440
4441 /* Startup the kernel thread for this host adapter. */
4442 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4443 "lpfc_worker_%d", phba->brd_no);
4444 if (IS_ERR(phba->worker_thread)) {
4445 error = PTR_ERR(phba->worker_thread);
4446 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004447 }
4448
James Smart3772a992009-05-22 14:50:54 -04004449 return 0;
4450}
4451
4452/**
4453 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4454 * @phba: pointer to lpfc hba data structure.
4455 *
4456 * This routine is invoked to unset the driver internal resources set up after
4457 * the device specific resource setup for supporting the HBA device it
4458 * attached to.
4459 **/
4460static void
4461lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4462{
4463 /* Stop kernel worker thread */
4464 kthread_stop(phba->worker_thread);
4465}
4466
4467/**
4468 * lpfc_free_iocb_list - Free iocb list.
4469 * @phba: pointer to lpfc hba data structure.
4470 *
4471 * This routine is invoked to free the driver's IOCB list and memory.
4472 **/
4473static void
4474lpfc_free_iocb_list(struct lpfc_hba *phba)
4475{
4476 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4477
4478 spin_lock_irq(&phba->hbalock);
4479 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4480 &phba->lpfc_iocb_list, list) {
4481 list_del(&iocbq_entry->list);
4482 kfree(iocbq_entry);
4483 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004484 }
James Smart3772a992009-05-22 14:50:54 -04004485 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004486
James Smart3772a992009-05-22 14:50:54 -04004487 return;
4488}
dea31012005-04-17 16:05:31 -05004489
James Smart3772a992009-05-22 14:50:54 -04004490/**
4491 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4492 * @phba: pointer to lpfc hba data structure.
4493 *
4494 * This routine is invoked to allocate and initizlize the driver's IOCB
4495 * list and set up the IOCB tag array accordingly.
4496 *
4497 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004498 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004499 * other values - error
4500 **/
4501static int
4502lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4503{
4504 struct lpfc_iocbq *iocbq_entry = NULL;
4505 uint16_t iotag;
4506 int i;
dea31012005-04-17 16:05:31 -05004507
4508 /* Initialize and populate the iocb list per host. */
4509 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004510 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004511 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004512 if (iocbq_entry == NULL) {
4513 printk(KERN_ERR "%s: only allocated %d iocbs of "
4514 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004515 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004516 goto out_free_iocbq;
4517 }
4518
James Bottomley604a3e32005-10-29 10:28:33 -05004519 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4520 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004521 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004522 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004523 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004524 goto out_free_iocbq;
4525 }
James Smart3772a992009-05-22 14:50:54 -04004526 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004527
4528 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004529 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4530 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004531 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004532 }
4533
James Smart3772a992009-05-22 14:50:54 -04004534 return 0;
4535
4536out_free_iocbq:
4537 lpfc_free_iocb_list(phba);
4538
4539 return -ENOMEM;
4540}
4541
4542/**
James Smartda0436e2009-05-22 14:51:39 -04004543 * lpfc_free_sgl_list - Free sgl list.
4544 * @phba: pointer to lpfc hba data structure.
4545 *
4546 * This routine is invoked to free the driver's sgl list and memory.
4547 **/
4548static void
4549lpfc_free_sgl_list(struct lpfc_hba *phba)
4550{
4551 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4552 LIST_HEAD(sglq_list);
4553 int rc = 0;
4554
4555 spin_lock_irq(&phba->hbalock);
4556 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4557 spin_unlock_irq(&phba->hbalock);
4558
4559 list_for_each_entry_safe(sglq_entry, sglq_next,
4560 &sglq_list, list) {
4561 list_del(&sglq_entry->list);
4562 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4563 kfree(sglq_entry);
4564 phba->sli4_hba.total_sglq_bufs--;
4565 }
4566 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4567 if (rc) {
4568 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004569 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004570 }
4571 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4572}
4573
4574/**
4575 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4576 * @phba: pointer to lpfc hba data structure.
4577 *
4578 * This routine is invoked to allocate the driver's active sgl memory.
4579 * This array will hold the sglq_entry's for active IOs.
4580 **/
4581static int
4582lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4583{
4584 int size;
4585 size = sizeof(struct lpfc_sglq *);
4586 size *= phba->sli4_hba.max_cfg_param.max_xri;
4587
4588 phba->sli4_hba.lpfc_sglq_active_list =
4589 kzalloc(size, GFP_KERNEL);
4590 if (!phba->sli4_hba.lpfc_sglq_active_list)
4591 return -ENOMEM;
4592 return 0;
4593}
4594
4595/**
4596 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4597 * @phba: pointer to lpfc hba data structure.
4598 *
4599 * This routine is invoked to walk through the array of active sglq entries
4600 * and free all of the resources.
4601 * This is just a place holder for now.
4602 **/
4603static void
4604lpfc_free_active_sgl(struct lpfc_hba *phba)
4605{
4606 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4607}
4608
4609/**
4610 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4611 * @phba: pointer to lpfc hba data structure.
4612 *
4613 * This routine is invoked to allocate and initizlize the driver's sgl
4614 * list and set up the sgl xritag tag array accordingly.
4615 *
4616 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004617 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004618 * other values - error
4619 **/
4620static int
4621lpfc_init_sgl_list(struct lpfc_hba *phba)
4622{
4623 struct lpfc_sglq *sglq_entry = NULL;
4624 int i;
4625 int els_xri_cnt;
4626
4627 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4628 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4629 "2400 lpfc_init_sgl_list els %d.\n",
4630 els_xri_cnt);
4631 /* Initialize and populate the sglq list per host/VF. */
4632 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4633 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4634
4635 /* Sanity check on XRI management */
4636 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4637 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4638 "2562 No room left for SCSI XRI allocation: "
4639 "max_xri=%d, els_xri=%d\n",
4640 phba->sli4_hba.max_cfg_param.max_xri,
4641 els_xri_cnt);
4642 return -ENOMEM;
4643 }
4644
4645 /* Allocate memory for the ELS XRI management array */
4646 phba->sli4_hba.lpfc_els_sgl_array =
4647 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4648 GFP_KERNEL);
4649
4650 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4652 "2401 Failed to allocate memory for ELS "
4653 "XRI management array of size %d.\n",
4654 els_xri_cnt);
4655 return -ENOMEM;
4656 }
4657
4658 /* Keep the SCSI XRI into the XRI management array */
4659 phba->sli4_hba.scsi_xri_max =
4660 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4661 phba->sli4_hba.scsi_xri_cnt = 0;
4662
4663 phba->sli4_hba.lpfc_scsi_psb_array =
4664 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4665 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4666
4667 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4668 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4669 "2563 Failed to allocate memory for SCSI "
4670 "XRI management array of size %d.\n",
4671 phba->sli4_hba.scsi_xri_max);
4672 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4673 return -ENOMEM;
4674 }
4675
4676 for (i = 0; i < els_xri_cnt; i++) {
4677 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4678 if (sglq_entry == NULL) {
4679 printk(KERN_ERR "%s: only allocated %d sgls of "
4680 "expected %d count. Unloading driver.\n",
4681 __func__, i, els_xri_cnt);
4682 goto out_free_mem;
4683 }
4684
4685 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4686 if (sglq_entry->sli4_xritag == NO_XRI) {
4687 kfree(sglq_entry);
4688 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4689 "Unloading driver.\n", __func__);
4690 goto out_free_mem;
4691 }
4692 sglq_entry->buff_type = GEN_BUFF_TYPE;
4693 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4694 if (sglq_entry->virt == NULL) {
4695 kfree(sglq_entry);
4696 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4697 "Unloading driver.\n", __func__);
4698 goto out_free_mem;
4699 }
4700 sglq_entry->sgl = sglq_entry->virt;
4701 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4702
4703 /* The list order is used by later block SGL registraton */
4704 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05004705 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04004706 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4707 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4708 phba->sli4_hba.total_sglq_bufs++;
4709 spin_unlock_irq(&phba->hbalock);
4710 }
4711 return 0;
4712
4713out_free_mem:
4714 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4715 lpfc_free_sgl_list(phba);
4716 return -ENOMEM;
4717}
4718
4719/**
4720 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4721 * @phba: pointer to lpfc hba data structure.
4722 *
4723 * This routine is invoked to post rpi header templates to the
4724 * HBA consistent with the SLI-4 interface spec. This routine
4725 * posts a PAGE_SIZE memory region to the port to hold up to
4726 * PAGE_SIZE modulo 64 rpi context headers.
4727 * No locks are held here because this is an initialization routine
4728 * called only from probe or lpfc_online when interrupts are not
4729 * enabled and the driver is reinitializing the device.
4730 *
4731 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004732 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004733 * -ENOMEM - No availble memory
4734 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04004735 **/
4736int
4737lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4738{
4739 int rc = 0;
4740 int longs;
4741 uint16_t rpi_count;
4742 struct lpfc_rpi_hdr *rpi_hdr;
4743
4744 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4745
4746 /*
4747 * Provision an rpi bitmask range for discovery. The total count
4748 * is the difference between max and base + 1.
4749 */
4750 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4751 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4752
4753 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4754 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4755 GFP_KERNEL);
4756 if (!phba->sli4_hba.rpi_bmask)
4757 return -ENOMEM;
4758
4759 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4760 if (!rpi_hdr) {
4761 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4762 "0391 Error during rpi post operation\n");
4763 lpfc_sli4_remove_rpis(phba);
4764 rc = -ENODEV;
4765 }
4766
4767 return rc;
4768}
4769
4770/**
4771 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4772 * @phba: pointer to lpfc hba data structure.
4773 *
4774 * This routine is invoked to allocate a single 4KB memory region to
4775 * support rpis and stores them in the phba. This single region
4776 * provides support for up to 64 rpis. The region is used globally
4777 * by the device.
4778 *
4779 * Returns:
4780 * A valid rpi hdr on success.
4781 * A NULL pointer on any failure.
4782 **/
4783struct lpfc_rpi_hdr *
4784lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4785{
4786 uint16_t rpi_limit, curr_rpi_range;
4787 struct lpfc_dmabuf *dmabuf;
4788 struct lpfc_rpi_hdr *rpi_hdr;
4789
4790 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4791 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4792
4793 spin_lock_irq(&phba->hbalock);
4794 curr_rpi_range = phba->sli4_hba.next_rpi;
4795 spin_unlock_irq(&phba->hbalock);
4796
4797 /*
4798 * The port has a limited number of rpis. The increment here
4799 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4800 * and to allow the full max_rpi range per port.
4801 */
4802 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4803 return NULL;
4804
4805 /*
4806 * First allocate the protocol header region for the port. The
4807 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4808 */
4809 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4810 if (!dmabuf)
4811 return NULL;
4812
4813 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4814 LPFC_HDR_TEMPLATE_SIZE,
4815 &dmabuf->phys,
4816 GFP_KERNEL);
4817 if (!dmabuf->virt) {
4818 rpi_hdr = NULL;
4819 goto err_free_dmabuf;
4820 }
4821
4822 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4823 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4824 rpi_hdr = NULL;
4825 goto err_free_coherent;
4826 }
4827
4828 /* Save the rpi header data for cleanup later. */
4829 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4830 if (!rpi_hdr)
4831 goto err_free_coherent;
4832
4833 rpi_hdr->dmabuf = dmabuf;
4834 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4835 rpi_hdr->page_count = 1;
4836 spin_lock_irq(&phba->hbalock);
4837 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4838 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4839
4840 /*
4841 * The next_rpi stores the next module-64 rpi value to post
4842 * in any subsequent rpi memory region postings.
4843 */
4844 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4845 spin_unlock_irq(&phba->hbalock);
4846 return rpi_hdr;
4847
4848 err_free_coherent:
4849 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4850 dmabuf->virt, dmabuf->phys);
4851 err_free_dmabuf:
4852 kfree(dmabuf);
4853 return NULL;
4854}
4855
4856/**
4857 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4858 * @phba: pointer to lpfc hba data structure.
4859 *
4860 * This routine is invoked to remove all memory resources allocated
4861 * to support rpis. This routine presumes the caller has released all
4862 * rpis consumed by fabric or port logins and is prepared to have
4863 * the header pages removed.
4864 **/
4865void
4866lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4867{
4868 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4869
4870 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4871 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4872 list_del(&rpi_hdr->list);
4873 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4874 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4875 kfree(rpi_hdr->dmabuf);
4876 kfree(rpi_hdr);
4877 }
4878
4879 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4880 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4881}
4882
4883/**
James Smart3772a992009-05-22 14:50:54 -04004884 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4885 * @pdev: pointer to pci device data structure.
4886 *
4887 * This routine is invoked to allocate the driver hba data structure for an
4888 * HBA device. If the allocation is successful, the phba reference to the
4889 * PCI device data structure is set.
4890 *
4891 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004892 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004893 * NULL - error
4894 **/
4895static struct lpfc_hba *
4896lpfc_hba_alloc(struct pci_dev *pdev)
4897{
4898 struct lpfc_hba *phba;
4899
4900 /* Allocate memory for HBA structure */
4901 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4902 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004903 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004904 return NULL;
4905 }
4906
4907 /* Set reference to PCI device in HBA structure */
4908 phba->pcidev = pdev;
4909
4910 /* Assign an unused board number */
4911 phba->brd_no = lpfc_get_instance();
4912 if (phba->brd_no < 0) {
4913 kfree(phba);
4914 return NULL;
4915 }
4916
James Smart4fede782010-01-26 23:08:55 -05004917 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04004918 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4919
James Smart3772a992009-05-22 14:50:54 -04004920 return phba;
4921}
4922
4923/**
4924 * lpfc_hba_free - Free driver hba data structure with a device.
4925 * @phba: pointer to lpfc hba data structure.
4926 *
4927 * This routine is invoked to free the driver hba data structure with an
4928 * HBA device.
4929 **/
4930static void
4931lpfc_hba_free(struct lpfc_hba *phba)
4932{
4933 /* Release the driver assigned board number */
4934 idr_remove(&lpfc_hba_index, phba->brd_no);
4935
4936 kfree(phba);
4937 return;
4938}
4939
4940/**
4941 * lpfc_create_shost - Create hba physical port with associated scsi host.
4942 * @phba: pointer to lpfc hba data structure.
4943 *
4944 * This routine is invoked to create HBA physical port and associate a SCSI
4945 * host with it.
4946 *
4947 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004948 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004949 * other values - error
4950 **/
4951static int
4952lpfc_create_shost(struct lpfc_hba *phba)
4953{
4954 struct lpfc_vport *vport;
4955 struct Scsi_Host *shost;
4956
4957 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004958 phba->fc_edtov = FF_DEF_EDTOV;
4959 phba->fc_ratov = FF_DEF_RATOV;
4960 phba->fc_altov = FF_DEF_ALTOV;
4961 phba->fc_arbtov = FF_DEF_ARBTOV;
4962
James Smartd7c47992010-06-08 18:31:54 -04004963 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04004964 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004965 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004966 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004967
4968 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004969 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004970 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004971 /* Put reference to SCSI host to driver's device private data */
4972 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004973
James Smart3772a992009-05-22 14:50:54 -04004974 return 0;
4975}
dea31012005-04-17 16:05:31 -05004976
James Smart3772a992009-05-22 14:50:54 -04004977/**
4978 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4979 * @phba: pointer to lpfc hba data structure.
4980 *
4981 * This routine is invoked to destroy HBA physical port and the associated
4982 * SCSI host.
4983 **/
4984static void
4985lpfc_destroy_shost(struct lpfc_hba *phba)
4986{
4987 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004988
James Smart3772a992009-05-22 14:50:54 -04004989 /* Destroy physical port that associated with the SCSI host */
4990 destroy_port(vport);
4991
4992 return;
4993}
4994
4995/**
4996 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4997 * @phba: pointer to lpfc hba data structure.
4998 * @shost: the shost to be used to detect Block guard settings.
4999 *
5000 * This routine sets up the local Block guard protocol settings for @shost.
5001 * This routine also allocates memory for debugging bg buffers.
5002 **/
5003static void
5004lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5005{
5006 int pagecnt = 10;
5007 if (lpfc_prot_mask && lpfc_prot_guard) {
5008 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5009 "1478 Registering BlockGuard with the "
5010 "SCSI layer\n");
5011 scsi_host_set_prot(shost, lpfc_prot_mask);
5012 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005013 }
James Smart81301a92008-12-04 22:39:46 -05005014 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005015 while (pagecnt) {
5016 spin_lock_init(&_dump_buf_lock);
5017 _dump_buf_data =
5018 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5019 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005020 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5021 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005022 "_dump_buf_data at 0x%p\n",
5023 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005024 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005025 memset(_dump_buf_data, 0,
5026 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005027 break;
James Smart3772a992009-05-22 14:50:54 -04005028 } else
James Smart81301a92008-12-04 22:39:46 -05005029 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005030 }
James Smart81301a92008-12-04 22:39:46 -05005031 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005032 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5033 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005034 "memory for hexdump\n");
5035 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005036 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5037 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005038 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005039 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005040 while (pagecnt) {
5041 _dump_buf_dif =
5042 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5043 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005044 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5045 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005046 "_dump_buf_dif at 0x%p\n",
5047 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005048 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005049 memset(_dump_buf_dif, 0,
5050 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005051 break;
James Smart3772a992009-05-22 14:50:54 -04005052 } else
James Smart81301a92008-12-04 22:39:46 -05005053 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005054 }
James Smart81301a92008-12-04 22:39:46 -05005055 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005056 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5057 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005058 "memory for hexdump\n");
5059 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005060 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5061 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005062 _dump_buf_dif);
5063}
5064
5065/**
5066 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5067 * @phba: pointer to lpfc hba data structure.
5068 *
5069 * This routine is invoked to perform all the necessary post initialization
5070 * setup for the device.
5071 **/
5072static void
5073lpfc_post_init_setup(struct lpfc_hba *phba)
5074{
5075 struct Scsi_Host *shost;
5076 struct lpfc_adapter_event_header adapter_event;
5077
5078 /* Get the default values for Model Name and Description */
5079 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5080
5081 /*
5082 * hba setup may have changed the hba_queue_depth so we need to
5083 * adjust the value of can_queue.
5084 */
5085 shost = pci_get_drvdata(phba->pcidev);
5086 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5087 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5088 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005089
5090 lpfc_host_attrib_init(shost);
5091
James Smart2e0fef82007-06-17 19:56:36 -05005092 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5093 spin_lock_irq(shost->host_lock);
5094 lpfc_poll_start_timer(phba);
5095 spin_unlock_irq(shost->host_lock);
5096 }
James Smart8f6d98d2006-08-01 07:34:00 -04005097
James Smart93996272008-08-24 21:50:30 -04005098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5099 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005100 /* Send board arrival event to upper layer */
5101 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5102 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5103 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005104 sizeof(adapter_event),
5105 (char *) &adapter_event,
5106 LPFC_NL_VENDOR_ID);
5107 return;
5108}
5109
5110/**
5111 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5112 * @phba: pointer to lpfc hba data structure.
5113 *
5114 * This routine is invoked to set up the PCI device memory space for device
5115 * with SLI-3 interface spec.
5116 *
5117 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005118 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005119 * other values - error
5120 **/
5121static int
5122lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5123{
5124 struct pci_dev *pdev;
5125 unsigned long bar0map_len, bar2map_len;
5126 int i, hbq_count;
5127 void *ptr;
5128 int error = -ENODEV;
5129
5130 /* Obtain PCI device reference */
5131 if (!phba->pcidev)
5132 return error;
5133 else
5134 pdev = phba->pcidev;
5135
5136 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005137 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5138 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5139 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5140 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005141 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005142 }
5143 }
James Smart3772a992009-05-22 14:50:54 -04005144
5145 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5146 * required by each mapping.
5147 */
5148 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5149 bar0map_len = pci_resource_len(pdev, 0);
5150
5151 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5152 bar2map_len = pci_resource_len(pdev, 2);
5153
5154 /* Map HBA SLIM to a kernel virtual address. */
5155 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5156 if (!phba->slim_memmap_p) {
5157 dev_printk(KERN_ERR, &pdev->dev,
5158 "ioremap failed for SLIM memory.\n");
5159 goto out;
5160 }
5161
5162 /* Map HBA Control Registers to a kernel virtual address. */
5163 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5164 if (!phba->ctrl_regs_memmap_p) {
5165 dev_printk(KERN_ERR, &pdev->dev,
5166 "ioremap failed for HBA control registers.\n");
5167 goto out_iounmap_slim;
5168 }
5169
5170 /* Allocate memory for SLI-2 structures */
5171 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5172 SLI2_SLIM_SIZE,
5173 &phba->slim2p.phys,
5174 GFP_KERNEL);
5175 if (!phba->slim2p.virt)
5176 goto out_iounmap;
5177
5178 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5179 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005180 phba->mbox_ext = (phba->slim2p.virt +
5181 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005182 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5183 phba->IOCBs = (phba->slim2p.virt +
5184 offsetof(struct lpfc_sli2_slim, IOCBs));
5185
5186 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5187 lpfc_sli_hbq_size(),
5188 &phba->hbqslimp.phys,
5189 GFP_KERNEL);
5190 if (!phba->hbqslimp.virt)
5191 goto out_free_slim;
5192
5193 hbq_count = lpfc_sli_hbq_count();
5194 ptr = phba->hbqslimp.virt;
5195 for (i = 0; i < hbq_count; ++i) {
5196 phba->hbqs[i].hbq_virt = ptr;
5197 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5198 ptr += (lpfc_hbq_defs[i]->entry_count *
5199 sizeof(struct lpfc_hbq_entry));
5200 }
5201 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5202 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5203
5204 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5205
5206 INIT_LIST_HEAD(&phba->rb_pend_list);
5207
5208 phba->MBslimaddr = phba->slim_memmap_p;
5209 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5210 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5211 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5212 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005213
dea31012005-04-17 16:05:31 -05005214 return 0;
5215
dea31012005-04-17 16:05:31 -05005216out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005217 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5218 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005219out_iounmap:
5220 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005221out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005222 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005223out:
5224 return error;
5225}
5226
James Smarte59058c2008-08-24 21:49:00 -04005227/**
James Smart3772a992009-05-22 14:50:54 -04005228 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5229 * @phba: pointer to lpfc hba data structure.
5230 *
5231 * This routine is invoked to unset the PCI device memory space for device
5232 * with SLI-3 interface spec.
5233 **/
5234static void
5235lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5236{
5237 struct pci_dev *pdev;
5238
5239 /* Obtain PCI device reference */
5240 if (!phba->pcidev)
5241 return;
5242 else
5243 pdev = phba->pcidev;
5244
5245 /* Free coherent DMA memory allocated */
5246 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5247 phba->hbqslimp.virt, phba->hbqslimp.phys);
5248 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5249 phba->slim2p.virt, phba->slim2p.phys);
5250
5251 /* I/O memory unmap */
5252 iounmap(phba->ctrl_regs_memmap_p);
5253 iounmap(phba->slim_memmap_p);
5254
5255 return;
5256}
5257
5258/**
James Smartda0436e2009-05-22 14:51:39 -04005259 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5260 * @phba: pointer to lpfc hba data structure.
5261 *
5262 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5263 * done and check status.
5264 *
5265 * Return 0 if successful, otherwise -ENODEV.
5266 **/
5267int
5268lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5269{
James Smart28baac72010-02-12 14:42:03 -05005270 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
James Smartda0436e2009-05-22 14:51:39 -04005271 int i, port_error = -ENODEV;
5272
5273 if (!phba->sli4_hba.STAregaddr)
5274 return -ENODEV;
5275
James Smartda0436e2009-05-22 14:51:39 -04005276 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5277 for (i = 0; i < 3000; i++) {
5278 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
5279 /* Encounter fatal POST error, break out */
5280 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
5281 port_error = -ENODEV;
5282 break;
5283 }
5284 if (LPFC_POST_STAGE_ARMFW_READY ==
5285 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
5286 port_error = 0;
5287 break;
5288 }
5289 msleep(10);
5290 }
5291
5292 if (port_error)
5293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5294 "1408 Failure HBA POST Status: sta_reg=0x%x, "
5295 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5296 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5297 bf_get(lpfc_hst_state_perr, &sta_reg),
5298 bf_get(lpfc_hst_state_sfi, &sta_reg),
5299 bf_get(lpfc_hst_state_nip, &sta_reg),
5300 bf_get(lpfc_hst_state_ipc, &sta_reg),
5301 bf_get(lpfc_hst_state_xrom, &sta_reg),
5302 bf_get(lpfc_hst_state_dl, &sta_reg),
5303 bf_get(lpfc_hst_state_port_status, &sta_reg));
5304
5305 /* Log device information */
James Smart28baac72010-02-12 14:42:03 -05005306 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5307 if (bf_get(lpfc_sli_intf_valid,
5308 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5309 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5310 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5311 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5312 bf_get(lpfc_sli_intf_sli_family,
5313 &phba->sli4_hba.sli_intf),
5314 bf_get(lpfc_sli_intf_slirev,
5315 &phba->sli4_hba.sli_intf),
5316 bf_get(lpfc_sli_intf_featurelevel1,
5317 &phba->sli4_hba.sli_intf),
5318 bf_get(lpfc_sli_intf_featurelevel2,
5319 &phba->sli4_hba.sli_intf));
5320 }
James Smarta747c9c2009-11-18 15:41:10 -05005321 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5322 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04005323 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05005324 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5325 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5326 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5327 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5329 "1422 HBA Unrecoverable error: "
5330 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5331 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5332 uerrlo_reg.word0, uerrhi_reg.word0,
5333 phba->sli4_hba.ue_mask_lo,
5334 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04005335 return -ENODEV;
5336 }
5337
James Smartda0436e2009-05-22 14:51:39 -04005338 return port_error;
5339}
5340
5341/**
5342 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5343 * @phba: pointer to lpfc hba data structure.
5344 *
5345 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5346 * memory map.
5347 **/
5348static void
5349lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5350{
5351 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5352 LPFC_UERR_STATUS_LO;
5353 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5354 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05005355 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5356 LPFC_UE_MASK_LO;
5357 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5358 LPFC_UE_MASK_HI;
James Smart28baac72010-02-12 14:42:03 -05005359 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5360 LPFC_SLI_INTF;
James Smartda0436e2009-05-22 14:51:39 -04005361}
5362
5363/**
5364 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5365 * @phba: pointer to lpfc hba data structure.
5366 *
5367 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5368 * memory map.
5369 **/
5370static void
5371lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5372{
5373
5374 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5375 LPFC_HST_STATE;
5376 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5377 LPFC_HST_ISR0;
5378 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5379 LPFC_HST_IMR0;
5380 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5381 LPFC_HST_ISCR0;
5382 return;
5383}
5384
5385/**
5386 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5387 * @phba: pointer to lpfc hba data structure.
5388 * @vf: virtual function number
5389 *
5390 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5391 * based on the given viftual function number, @vf.
5392 *
5393 * Return 0 if successful, otherwise -ENODEV.
5394 **/
5395static int
5396lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5397{
5398 if (vf > LPFC_VIR_FUNC_MAX)
5399 return -ENODEV;
5400
5401 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5402 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5403 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5404 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5405 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5406 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5407 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5408 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5409 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5410 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5411 return 0;
5412}
5413
5414/**
5415 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5416 * @phba: pointer to lpfc hba data structure.
5417 *
5418 * This routine is invoked to create the bootstrap mailbox
5419 * region consistent with the SLI-4 interface spec. This
5420 * routine allocates all memory necessary to communicate
5421 * mailbox commands to the port and sets up all alignment
5422 * needs. No locks are expected to be held when calling
5423 * this routine.
5424 *
5425 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005426 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005427 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005428 **/
5429static int
5430lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5431{
5432 uint32_t bmbx_size;
5433 struct lpfc_dmabuf *dmabuf;
5434 struct dma_address *dma_address;
5435 uint32_t pa_addr;
5436 uint64_t phys_addr;
5437
5438 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5439 if (!dmabuf)
5440 return -ENOMEM;
5441
5442 /*
5443 * The bootstrap mailbox region is comprised of 2 parts
5444 * plus an alignment restriction of 16 bytes.
5445 */
5446 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5447 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5448 bmbx_size,
5449 &dmabuf->phys,
5450 GFP_KERNEL);
5451 if (!dmabuf->virt) {
5452 kfree(dmabuf);
5453 return -ENOMEM;
5454 }
5455 memset(dmabuf->virt, 0, bmbx_size);
5456
5457 /*
5458 * Initialize the bootstrap mailbox pointers now so that the register
5459 * operations are simple later. The mailbox dma address is required
5460 * to be 16-byte aligned. Also align the virtual memory as each
5461 * maibox is copied into the bmbx mailbox region before issuing the
5462 * command to the port.
5463 */
5464 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5465 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5466
5467 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5468 LPFC_ALIGN_16_BYTE);
5469 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5470 LPFC_ALIGN_16_BYTE);
5471
5472 /*
5473 * Set the high and low physical addresses now. The SLI4 alignment
5474 * requirement is 16 bytes and the mailbox is posted to the port
5475 * as two 30-bit addresses. The other data is a bit marking whether
5476 * the 30-bit address is the high or low address.
5477 * Upcast bmbx aphys to 64bits so shift instruction compiles
5478 * clean on 32 bit machines.
5479 */
5480 dma_address = &phba->sli4_hba.bmbx.dma_address;
5481 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5482 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5483 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5484 LPFC_BMBX_BIT1_ADDR_HI);
5485
5486 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5487 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5488 LPFC_BMBX_BIT1_ADDR_LO);
5489 return 0;
5490}
5491
5492/**
5493 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5494 * @phba: pointer to lpfc hba data structure.
5495 *
5496 * This routine is invoked to teardown the bootstrap mailbox
5497 * region and release all host resources. This routine requires
5498 * the caller to ensure all mailbox commands recovered, no
5499 * additional mailbox comands are sent, and interrupts are disabled
5500 * before calling this routine.
5501 *
5502 **/
5503static void
5504lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5505{
5506 dma_free_coherent(&phba->pcidev->dev,
5507 phba->sli4_hba.bmbx.bmbx_size,
5508 phba->sli4_hba.bmbx.dmabuf->virt,
5509 phba->sli4_hba.bmbx.dmabuf->phys);
5510
5511 kfree(phba->sli4_hba.bmbx.dmabuf);
5512 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5513}
5514
5515/**
5516 * lpfc_sli4_read_config - Get the config parameters.
5517 * @phba: pointer to lpfc hba data structure.
5518 *
5519 * This routine is invoked to read the configuration parameters from the HBA.
5520 * The configuration parameters are used to set the base and maximum values
5521 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5522 * allocation for the port.
5523 *
5524 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005525 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005526 * -ENOMEM - No availble memory
5527 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005528 **/
5529static int
5530lpfc_sli4_read_config(struct lpfc_hba *phba)
5531{
5532 LPFC_MBOXQ_t *pmb;
5533 struct lpfc_mbx_read_config *rd_config;
5534 uint32_t rc = 0;
5535
5536 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5537 if (!pmb) {
5538 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5539 "2011 Unable to allocate memory for issuing "
5540 "SLI_CONFIG_SPECIAL mailbox command\n");
5541 return -ENOMEM;
5542 }
5543
5544 lpfc_read_config(phba, pmb);
5545
5546 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5547 if (rc != MBX_SUCCESS) {
5548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5549 "2012 Mailbox failed , mbxCmd x%x "
5550 "READ_CONFIG, mbxStatus x%x\n",
5551 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5552 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5553 rc = -EIO;
5554 } else {
5555 rd_config = &pmb->u.mqe.un.rd_config;
5556 phba->sli4_hba.max_cfg_param.max_xri =
5557 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5558 phba->sli4_hba.max_cfg_param.xri_base =
5559 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5560 phba->sli4_hba.max_cfg_param.max_vpi =
5561 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5562 phba->sli4_hba.max_cfg_param.vpi_base =
5563 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5564 phba->sli4_hba.max_cfg_param.max_rpi =
5565 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5566 phba->sli4_hba.max_cfg_param.rpi_base =
5567 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5568 phba->sli4_hba.max_cfg_param.max_vfi =
5569 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5570 phba->sli4_hba.max_cfg_param.vfi_base =
5571 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5572 phba->sli4_hba.max_cfg_param.max_fcfi =
5573 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5574 phba->sli4_hba.max_cfg_param.fcfi_base =
5575 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5576 phba->sli4_hba.max_cfg_param.max_eq =
5577 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5578 phba->sli4_hba.max_cfg_param.max_rq =
5579 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5580 phba->sli4_hba.max_cfg_param.max_wq =
5581 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5582 phba->sli4_hba.max_cfg_param.max_cq =
5583 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5584 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5585 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5586 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5587 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5588 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005589 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5590 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005591 phba->max_vports = phba->max_vpi;
5592 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5593 "2003 cfg params XRI(B:%d M:%d), "
5594 "VPI(B:%d M:%d) "
5595 "VFI(B:%d M:%d) "
5596 "RPI(B:%d M:%d) "
5597 "FCFI(B:%d M:%d)\n",
5598 phba->sli4_hba.max_cfg_param.xri_base,
5599 phba->sli4_hba.max_cfg_param.max_xri,
5600 phba->sli4_hba.max_cfg_param.vpi_base,
5601 phba->sli4_hba.max_cfg_param.max_vpi,
5602 phba->sli4_hba.max_cfg_param.vfi_base,
5603 phba->sli4_hba.max_cfg_param.max_vfi,
5604 phba->sli4_hba.max_cfg_param.rpi_base,
5605 phba->sli4_hba.max_cfg_param.max_rpi,
5606 phba->sli4_hba.max_cfg_param.fcfi_base,
5607 phba->sli4_hba.max_cfg_param.max_fcfi);
5608 }
5609 mempool_free(pmb, phba->mbox_mem_pool);
5610
5611 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04005612 if (phba->cfg_hba_queue_depth >
5613 (phba->sli4_hba.max_cfg_param.max_xri -
5614 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04005615 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04005616 phba->sli4_hba.max_cfg_param.max_xri -
5617 lpfc_sli4_get_els_iocb_cnt(phba);
James Smartda0436e2009-05-22 14:51:39 -04005618 return rc;
5619}
5620
5621/**
5622 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5623 * @phba: pointer to lpfc hba data structure.
5624 *
5625 * This routine is invoked to setup the host-side endian order to the
5626 * HBA consistent with the SLI-4 interface spec.
5627 *
5628 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005629 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005630 * -ENOMEM - No availble memory
5631 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005632 **/
5633static int
5634lpfc_setup_endian_order(struct lpfc_hba *phba)
5635{
5636 LPFC_MBOXQ_t *mboxq;
5637 uint32_t rc = 0;
5638 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5639 HOST_ENDIAN_HIGH_WORD1};
5640
5641 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5642 if (!mboxq) {
5643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5644 "0492 Unable to allocate memory for issuing "
5645 "SLI_CONFIG_SPECIAL mailbox command\n");
5646 return -ENOMEM;
5647 }
5648
5649 /*
5650 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5651 * words to contain special data values and no other data.
5652 */
5653 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5654 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5655 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5656 if (rc != MBX_SUCCESS) {
5657 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5658 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5659 "status x%x\n",
5660 rc);
5661 rc = -EIO;
5662 }
5663
5664 mempool_free(mboxq, phba->mbox_mem_pool);
5665 return rc;
5666}
5667
5668/**
5669 * lpfc_sli4_queue_create - Create all the SLI4 queues
5670 * @phba: pointer to lpfc hba data structure.
5671 *
5672 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5673 * operation. For each SLI4 queue type, the parameters such as queue entry
5674 * count (queue depth) shall be taken from the module parameter. For now,
5675 * we just use some constant number as place holder.
5676 *
5677 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005678 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005679 * -ENOMEM - No availble memory
5680 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005681 **/
5682static int
5683lpfc_sli4_queue_create(struct lpfc_hba *phba)
5684{
5685 struct lpfc_queue *qdesc;
5686 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5687 int cfg_fcp_wq_count;
5688 int cfg_fcp_eq_count;
5689
5690 /*
5691 * Sanity check for confiugred queue parameters against the run-time
5692 * device parameters
5693 */
5694
5695 /* Sanity check on FCP fast-path WQ parameters */
5696 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5697 if (cfg_fcp_wq_count >
5698 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5699 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5700 LPFC_SP_WQN_DEF;
5701 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5703 "2581 Not enough WQs (%d) from "
5704 "the pci function for supporting "
5705 "FCP WQs (%d)\n",
5706 phba->sli4_hba.max_cfg_param.max_wq,
5707 phba->cfg_fcp_wq_count);
5708 goto out_error;
5709 }
5710 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5711 "2582 Not enough WQs (%d) from the pci "
5712 "function for supporting the requested "
5713 "FCP WQs (%d), the actual FCP WQs can "
5714 "be supported: %d\n",
5715 phba->sli4_hba.max_cfg_param.max_wq,
5716 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5717 }
5718 /* The actual number of FCP work queues adopted */
5719 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5720
5721 /* Sanity check on FCP fast-path EQ parameters */
5722 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5723 if (cfg_fcp_eq_count >
5724 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5725 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5726 LPFC_SP_EQN_DEF;
5727 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5729 "2574 Not enough EQs (%d) from the "
5730 "pci function for supporting FCP "
5731 "EQs (%d)\n",
5732 phba->sli4_hba.max_cfg_param.max_eq,
5733 phba->cfg_fcp_eq_count);
5734 goto out_error;
5735 }
5736 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5737 "2575 Not enough EQs (%d) from the pci "
5738 "function for supporting the requested "
5739 "FCP EQs (%d), the actual FCP EQs can "
5740 "be supported: %d\n",
5741 phba->sli4_hba.max_cfg_param.max_eq,
5742 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5743 }
5744 /* It does not make sense to have more EQs than WQs */
5745 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5746 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005747 "2593 The FCP EQ count(%d) cannot be greater "
5748 "than the FCP WQ count(%d), limiting the "
5749 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005750 phba->cfg_fcp_wq_count,
5751 phba->cfg_fcp_wq_count);
5752 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5753 }
5754 /* The actual number of FCP event queues adopted */
5755 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5756 /* The overall number of event queues used */
5757 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5758
5759 /*
5760 * Create Event Queues (EQs)
5761 */
5762
5763 /* Get EQ depth from module parameter, fake the default for now */
5764 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5765 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5766
5767 /* Create slow path event queue */
5768 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5769 phba->sli4_hba.eq_ecount);
5770 if (!qdesc) {
5771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5772 "0496 Failed allocate slow-path EQ\n");
5773 goto out_error;
5774 }
5775 phba->sli4_hba.sp_eq = qdesc;
5776
5777 /* Create fast-path FCP Event Queue(s) */
5778 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5779 phba->cfg_fcp_eq_count), GFP_KERNEL);
5780 if (!phba->sli4_hba.fp_eq) {
5781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5782 "2576 Failed allocate memory for fast-path "
5783 "EQ record array\n");
5784 goto out_free_sp_eq;
5785 }
5786 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5787 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5788 phba->sli4_hba.eq_ecount);
5789 if (!qdesc) {
5790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5791 "0497 Failed allocate fast-path EQ\n");
5792 goto out_free_fp_eq;
5793 }
5794 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5795 }
5796
5797 /*
5798 * Create Complete Queues (CQs)
5799 */
5800
5801 /* Get CQ depth from module parameter, fake the default for now */
5802 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5803 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5804
5805 /* Create slow-path Mailbox Command Complete Queue */
5806 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5807 phba->sli4_hba.cq_ecount);
5808 if (!qdesc) {
5809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5810 "0500 Failed allocate slow-path mailbox CQ\n");
5811 goto out_free_fp_eq;
5812 }
5813 phba->sli4_hba.mbx_cq = qdesc;
5814
5815 /* Create slow-path ELS Complete Queue */
5816 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5817 phba->sli4_hba.cq_ecount);
5818 if (!qdesc) {
5819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5820 "0501 Failed allocate slow-path ELS CQ\n");
5821 goto out_free_mbx_cq;
5822 }
5823 phba->sli4_hba.els_cq = qdesc;
5824
James Smartda0436e2009-05-22 14:51:39 -04005825
5826 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5827 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5828 phba->cfg_fcp_eq_count), GFP_KERNEL);
5829 if (!phba->sli4_hba.fcp_cq) {
5830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5831 "2577 Failed allocate memory for fast-path "
5832 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005833 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005834 }
5835 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5836 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5837 phba->sli4_hba.cq_ecount);
5838 if (!qdesc) {
5839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5840 "0499 Failed allocate fast-path FCP "
5841 "CQ (%d)\n", fcp_cqidx);
5842 goto out_free_fcp_cq;
5843 }
5844 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5845 }
5846
5847 /* Create Mailbox Command Queue */
5848 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5849 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5850
5851 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5852 phba->sli4_hba.mq_ecount);
5853 if (!qdesc) {
5854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5855 "0505 Failed allocate slow-path MQ\n");
5856 goto out_free_fcp_cq;
5857 }
5858 phba->sli4_hba.mbx_wq = qdesc;
5859
5860 /*
5861 * Create all the Work Queues (WQs)
5862 */
5863 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5864 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5865
5866 /* Create slow-path ELS Work Queue */
5867 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5868 phba->sli4_hba.wq_ecount);
5869 if (!qdesc) {
5870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5871 "0504 Failed allocate slow-path ELS WQ\n");
5872 goto out_free_mbx_wq;
5873 }
5874 phba->sli4_hba.els_wq = qdesc;
5875
5876 /* Create fast-path FCP Work Queue(s) */
5877 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5878 phba->cfg_fcp_wq_count), GFP_KERNEL);
5879 if (!phba->sli4_hba.fcp_wq) {
5880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5881 "2578 Failed allocate memory for fast-path "
5882 "WQ record array\n");
5883 goto out_free_els_wq;
5884 }
5885 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5886 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5887 phba->sli4_hba.wq_ecount);
5888 if (!qdesc) {
5889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5890 "0503 Failed allocate fast-path FCP "
5891 "WQ (%d)\n", fcp_wqidx);
5892 goto out_free_fcp_wq;
5893 }
5894 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5895 }
5896
5897 /*
5898 * Create Receive Queue (RQ)
5899 */
5900 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5901 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5902
5903 /* Create Receive Queue for header */
5904 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5905 phba->sli4_hba.rq_ecount);
5906 if (!qdesc) {
5907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5908 "0506 Failed allocate receive HRQ\n");
5909 goto out_free_fcp_wq;
5910 }
5911 phba->sli4_hba.hdr_rq = qdesc;
5912
5913 /* Create Receive Queue for data */
5914 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5915 phba->sli4_hba.rq_ecount);
5916 if (!qdesc) {
5917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5918 "0507 Failed allocate receive DRQ\n");
5919 goto out_free_hdr_rq;
5920 }
5921 phba->sli4_hba.dat_rq = qdesc;
5922
5923 return 0;
5924
5925out_free_hdr_rq:
5926 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5927 phba->sli4_hba.hdr_rq = NULL;
5928out_free_fcp_wq:
5929 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5930 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5931 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5932 }
5933 kfree(phba->sli4_hba.fcp_wq);
5934out_free_els_wq:
5935 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5936 phba->sli4_hba.els_wq = NULL;
5937out_free_mbx_wq:
5938 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5939 phba->sli4_hba.mbx_wq = NULL;
5940out_free_fcp_cq:
5941 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5942 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5943 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5944 }
5945 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005946out_free_els_cq:
5947 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5948 phba->sli4_hba.els_cq = NULL;
5949out_free_mbx_cq:
5950 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5951 phba->sli4_hba.mbx_cq = NULL;
5952out_free_fp_eq:
5953 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5954 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5955 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5956 }
5957 kfree(phba->sli4_hba.fp_eq);
5958out_free_sp_eq:
5959 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5960 phba->sli4_hba.sp_eq = NULL;
5961out_error:
5962 return -ENOMEM;
5963}
5964
5965/**
5966 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5967 * @phba: pointer to lpfc hba data structure.
5968 *
5969 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5970 * operation.
5971 *
5972 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005973 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005974 * -ENOMEM - No availble memory
5975 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005976 **/
5977static void
5978lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5979{
5980 int fcp_qidx;
5981
5982 /* Release mailbox command work queue */
5983 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5984 phba->sli4_hba.mbx_wq = NULL;
5985
5986 /* Release ELS work queue */
5987 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5988 phba->sli4_hba.els_wq = NULL;
5989
5990 /* Release FCP work queue */
5991 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5992 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5993 kfree(phba->sli4_hba.fcp_wq);
5994 phba->sli4_hba.fcp_wq = NULL;
5995
5996 /* Release unsolicited receive queue */
5997 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5998 phba->sli4_hba.hdr_rq = NULL;
5999 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6000 phba->sli4_hba.dat_rq = NULL;
6001
James Smartda0436e2009-05-22 14:51:39 -04006002 /* Release ELS complete queue */
6003 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6004 phba->sli4_hba.els_cq = NULL;
6005
6006 /* Release mailbox command complete queue */
6007 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6008 phba->sli4_hba.mbx_cq = NULL;
6009
6010 /* Release FCP response complete queue */
6011 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6012 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
6013 kfree(phba->sli4_hba.fcp_cq);
6014 phba->sli4_hba.fcp_cq = NULL;
6015
6016 /* Release fast-path event queue */
6017 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6018 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
6019 kfree(phba->sli4_hba.fp_eq);
6020 phba->sli4_hba.fp_eq = NULL;
6021
6022 /* Release slow-path event queue */
6023 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6024 phba->sli4_hba.sp_eq = NULL;
6025
6026 return;
6027}
6028
6029/**
6030 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6031 * @phba: pointer to lpfc hba data structure.
6032 *
6033 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6034 * operation.
6035 *
6036 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006037 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006038 * -ENOMEM - No availble memory
6039 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006040 **/
6041int
6042lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6043{
6044 int rc = -ENOMEM;
6045 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6046 int fcp_cq_index = 0;
6047
6048 /*
6049 * Set up Event Queues (EQs)
6050 */
6051
6052 /* Set up slow-path event queue */
6053 if (!phba->sli4_hba.sp_eq) {
6054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6055 "0520 Slow-path EQ not allocated\n");
6056 goto out_error;
6057 }
6058 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6059 LPFC_SP_DEF_IMAX);
6060 if (rc) {
6061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6062 "0521 Failed setup of slow-path EQ: "
6063 "rc = 0x%x\n", rc);
6064 goto out_error;
6065 }
6066 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6067 "2583 Slow-path EQ setup: queue-id=%d\n",
6068 phba->sli4_hba.sp_eq->queue_id);
6069
6070 /* Set up fast-path event queue */
6071 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6072 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6074 "0522 Fast-path EQ (%d) not "
6075 "allocated\n", fcp_eqidx);
6076 goto out_destroy_fp_eq;
6077 }
6078 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6079 phba->cfg_fcp_imax);
6080 if (rc) {
6081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6082 "0523 Failed setup of fast-path EQ "
6083 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6084 goto out_destroy_fp_eq;
6085 }
6086 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6087 "2584 Fast-path EQ setup: "
6088 "queue[%d]-id=%d\n", fcp_eqidx,
6089 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6090 }
6091
6092 /*
6093 * Set up Complete Queues (CQs)
6094 */
6095
6096 /* Set up slow-path MBOX Complete Queue as the first CQ */
6097 if (!phba->sli4_hba.mbx_cq) {
6098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6099 "0528 Mailbox CQ not allocated\n");
6100 goto out_destroy_fp_eq;
6101 }
6102 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6103 LPFC_MCQ, LPFC_MBOX);
6104 if (rc) {
6105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6106 "0529 Failed setup of slow-path mailbox CQ: "
6107 "rc = 0x%x\n", rc);
6108 goto out_destroy_fp_eq;
6109 }
6110 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6111 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6112 phba->sli4_hba.mbx_cq->queue_id,
6113 phba->sli4_hba.sp_eq->queue_id);
6114
6115 /* Set up slow-path ELS Complete Queue */
6116 if (!phba->sli4_hba.els_cq) {
6117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6118 "0530 ELS CQ not allocated\n");
6119 goto out_destroy_mbx_cq;
6120 }
6121 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6122 LPFC_WCQ, LPFC_ELS);
6123 if (rc) {
6124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6125 "0531 Failed setup of slow-path ELS CQ: "
6126 "rc = 0x%x\n", rc);
6127 goto out_destroy_mbx_cq;
6128 }
6129 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6130 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6131 phba->sli4_hba.els_cq->queue_id,
6132 phba->sli4_hba.sp_eq->queue_id);
6133
James Smartda0436e2009-05-22 14:51:39 -04006134 /* Set up fast-path FCP Response Complete Queue */
6135 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6136 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6138 "0526 Fast-path FCP CQ (%d) not "
6139 "allocated\n", fcp_cqidx);
6140 goto out_destroy_fcp_cq;
6141 }
6142 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
6143 phba->sli4_hba.fp_eq[fcp_cqidx],
6144 LPFC_WCQ, LPFC_FCP);
6145 if (rc) {
6146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6147 "0527 Failed setup of fast-path FCP "
6148 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6149 goto out_destroy_fcp_cq;
6150 }
6151 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6152 "2588 FCP CQ setup: cq[%d]-id=%d, "
6153 "parent eq[%d]-id=%d\n",
6154 fcp_cqidx,
6155 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6156 fcp_cqidx,
6157 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
6158 }
6159
6160 /*
6161 * Set up all the Work Queues (WQs)
6162 */
6163
6164 /* Set up Mailbox Command Queue */
6165 if (!phba->sli4_hba.mbx_wq) {
6166 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6167 "0538 Slow-path MQ not allocated\n");
6168 goto out_destroy_fcp_cq;
6169 }
6170 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6171 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6172 if (rc) {
6173 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6174 "0539 Failed setup of slow-path MQ: "
6175 "rc = 0x%x\n", rc);
6176 goto out_destroy_fcp_cq;
6177 }
6178 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6179 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6180 phba->sli4_hba.mbx_wq->queue_id,
6181 phba->sli4_hba.mbx_cq->queue_id);
6182
6183 /* Set up slow-path ELS Work Queue */
6184 if (!phba->sli4_hba.els_wq) {
6185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6186 "0536 Slow-path ELS WQ not allocated\n");
6187 goto out_destroy_mbx_wq;
6188 }
6189 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6190 phba->sli4_hba.els_cq, LPFC_ELS);
6191 if (rc) {
6192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6193 "0537 Failed setup of slow-path ELS WQ: "
6194 "rc = 0x%x\n", rc);
6195 goto out_destroy_mbx_wq;
6196 }
6197 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6198 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6199 phba->sli4_hba.els_wq->queue_id,
6200 phba->sli4_hba.els_cq->queue_id);
6201
6202 /* Set up fast-path FCP Work Queue */
6203 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6204 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6206 "0534 Fast-path FCP WQ (%d) not "
6207 "allocated\n", fcp_wqidx);
6208 goto out_destroy_fcp_wq;
6209 }
6210 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6211 phba->sli4_hba.fcp_cq[fcp_cq_index],
6212 LPFC_FCP);
6213 if (rc) {
6214 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6215 "0535 Failed setup of fast-path FCP "
6216 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6217 goto out_destroy_fcp_wq;
6218 }
6219 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6220 "2591 FCP WQ setup: wq[%d]-id=%d, "
6221 "parent cq[%d]-id=%d\n",
6222 fcp_wqidx,
6223 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6224 fcp_cq_index,
6225 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6226 /* Round robin FCP Work Queue's Completion Queue assignment */
6227 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
6228 }
6229
6230 /*
6231 * Create Receive Queue (RQ)
6232 */
6233 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6235 "0540 Receive Queue not allocated\n");
6236 goto out_destroy_fcp_wq;
6237 }
6238 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006239 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006240 if (rc) {
6241 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6242 "0541 Failed setup of Receive Queue: "
6243 "rc = 0x%x\n", rc);
6244 goto out_destroy_fcp_wq;
6245 }
6246 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6247 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6248 "parent cq-id=%d\n",
6249 phba->sli4_hba.hdr_rq->queue_id,
6250 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006251 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006252 return 0;
6253
6254out_destroy_fcp_wq:
6255 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6256 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6257 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6258out_destroy_mbx_wq:
6259 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6260out_destroy_fcp_cq:
6261 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6262 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04006263 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6264out_destroy_mbx_cq:
6265 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6266out_destroy_fp_eq:
6267 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6268 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6269 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6270out_error:
6271 return rc;
6272}
6273
6274/**
6275 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6276 * @phba: pointer to lpfc hba data structure.
6277 *
6278 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6279 * operation.
6280 *
6281 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006282 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006283 * -ENOMEM - No availble memory
6284 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006285 **/
6286void
6287lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6288{
6289 int fcp_qidx;
6290
6291 /* Unset mailbox command work queue */
6292 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6293 /* Unset ELS work queue */
6294 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6295 /* Unset unsolicited receive queue */
6296 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6297 /* Unset FCP work queue */
6298 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6299 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6300 /* Unset mailbox command complete queue */
6301 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6302 /* Unset ELS complete queue */
6303 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006304 /* Unset FCP response complete queue */
6305 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6306 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6307 /* Unset fast-path event queue */
6308 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6309 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6310 /* Unset slow-path event queue */
6311 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6312}
6313
6314/**
6315 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6316 * @phba: pointer to lpfc hba data structure.
6317 *
6318 * This routine is invoked to allocate and set up a pool of completion queue
6319 * events. The body of the completion queue event is a completion queue entry
6320 * CQE. For now, this pool is used for the interrupt service routine to queue
6321 * the following HBA completion queue events for the worker thread to process:
6322 * - Mailbox asynchronous events
6323 * - Receive queue completion unsolicited events
6324 * Later, this can be used for all the slow-path events.
6325 *
6326 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006327 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006328 * -ENOMEM - No availble memory
6329 **/
6330static int
6331lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6332{
6333 struct lpfc_cq_event *cq_event;
6334 int i;
6335
6336 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6337 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6338 if (!cq_event)
6339 goto out_pool_create_fail;
6340 list_add_tail(&cq_event->list,
6341 &phba->sli4_hba.sp_cqe_event_pool);
6342 }
6343 return 0;
6344
6345out_pool_create_fail:
6346 lpfc_sli4_cq_event_pool_destroy(phba);
6347 return -ENOMEM;
6348}
6349
6350/**
6351 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6352 * @phba: pointer to lpfc hba data structure.
6353 *
6354 * This routine is invoked to free the pool of completion queue events at
6355 * driver unload time. Note that, it is the responsibility of the driver
6356 * cleanup routine to free all the outstanding completion-queue events
6357 * allocated from this pool back into the pool before invoking this routine
6358 * to destroy the pool.
6359 **/
6360static void
6361lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6362{
6363 struct lpfc_cq_event *cq_event, *next_cq_event;
6364
6365 list_for_each_entry_safe(cq_event, next_cq_event,
6366 &phba->sli4_hba.sp_cqe_event_pool, list) {
6367 list_del(&cq_event->list);
6368 kfree(cq_event);
6369 }
6370}
6371
6372/**
6373 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6374 * @phba: pointer to lpfc hba data structure.
6375 *
6376 * This routine is the lock free version of the API invoked to allocate a
6377 * completion-queue event from the free pool.
6378 *
6379 * Return: Pointer to the newly allocated completion-queue event if successful
6380 * NULL otherwise.
6381 **/
6382struct lpfc_cq_event *
6383__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6384{
6385 struct lpfc_cq_event *cq_event = NULL;
6386
6387 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6388 struct lpfc_cq_event, list);
6389 return cq_event;
6390}
6391
6392/**
6393 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6394 * @phba: pointer to lpfc hba data structure.
6395 *
6396 * This routine is the lock version of the API invoked to allocate a
6397 * completion-queue event from the free pool.
6398 *
6399 * Return: Pointer to the newly allocated completion-queue event if successful
6400 * NULL otherwise.
6401 **/
6402struct lpfc_cq_event *
6403lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6404{
6405 struct lpfc_cq_event *cq_event;
6406 unsigned long iflags;
6407
6408 spin_lock_irqsave(&phba->hbalock, iflags);
6409 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6410 spin_unlock_irqrestore(&phba->hbalock, iflags);
6411 return cq_event;
6412}
6413
6414/**
6415 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6416 * @phba: pointer to lpfc hba data structure.
6417 * @cq_event: pointer to the completion queue event to be freed.
6418 *
6419 * This routine is the lock free version of the API invoked to release a
6420 * completion-queue event back into the free pool.
6421 **/
6422void
6423__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6424 struct lpfc_cq_event *cq_event)
6425{
6426 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6427}
6428
6429/**
6430 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6431 * @phba: pointer to lpfc hba data structure.
6432 * @cq_event: pointer to the completion queue event to be freed.
6433 *
6434 * This routine is the lock version of the API invoked to release a
6435 * completion-queue event back into the free pool.
6436 **/
6437void
6438lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6439 struct lpfc_cq_event *cq_event)
6440{
6441 unsigned long iflags;
6442 spin_lock_irqsave(&phba->hbalock, iflags);
6443 __lpfc_sli4_cq_event_release(phba, cq_event);
6444 spin_unlock_irqrestore(&phba->hbalock, iflags);
6445}
6446
6447/**
6448 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6449 * @phba: pointer to lpfc hba data structure.
6450 *
6451 * This routine is to free all the pending completion-queue events to the
6452 * back into the free pool for device reset.
6453 **/
6454static void
6455lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6456{
6457 LIST_HEAD(cqelist);
6458 struct lpfc_cq_event *cqe;
6459 unsigned long iflags;
6460
6461 /* Retrieve all the pending WCQEs from pending WCQE lists */
6462 spin_lock_irqsave(&phba->hbalock, iflags);
6463 /* Pending FCP XRI abort events */
6464 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6465 &cqelist);
6466 /* Pending ELS XRI abort events */
6467 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6468 &cqelist);
6469 /* Pending asynnc events */
6470 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6471 &cqelist);
6472 spin_unlock_irqrestore(&phba->hbalock, iflags);
6473
6474 while (!list_empty(&cqelist)) {
6475 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6476 lpfc_sli4_cq_event_release(phba, cqe);
6477 }
6478}
6479
6480/**
6481 * lpfc_pci_function_reset - Reset pci function.
6482 * @phba: pointer to lpfc hba data structure.
6483 *
6484 * This routine is invoked to request a PCI function reset. It will destroys
6485 * all resources assigned to the PCI function which originates this request.
6486 *
6487 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006488 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006489 * -ENOMEM - No availble memory
6490 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006491 **/
6492int
6493lpfc_pci_function_reset(struct lpfc_hba *phba)
6494{
6495 LPFC_MBOXQ_t *mboxq;
6496 uint32_t rc = 0;
6497 uint32_t shdr_status, shdr_add_status;
6498 union lpfc_sli4_cfg_shdr *shdr;
6499
6500 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6501 if (!mboxq) {
6502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6503 "0494 Unable to allocate memory for issuing "
6504 "SLI_FUNCTION_RESET mailbox command\n");
6505 return -ENOMEM;
6506 }
6507
6508 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6509 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6510 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6511 LPFC_SLI4_MBX_EMBED);
6512 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6513 shdr = (union lpfc_sli4_cfg_shdr *)
6514 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6515 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6516 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6517 if (rc != MBX_TIMEOUT)
6518 mempool_free(mboxq, phba->mbox_mem_pool);
6519 if (shdr_status || shdr_add_status || rc) {
6520 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6521 "0495 SLI_FUNCTION_RESET mailbox failed with "
6522 "status x%x add_status x%x, mbx status x%x\n",
6523 shdr_status, shdr_add_status, rc);
6524 rc = -ENXIO;
6525 }
6526 return rc;
6527}
6528
6529/**
6530 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6531 * @phba: pointer to lpfc hba data structure.
6532 * @cnt: number of nop mailbox commands to send.
6533 *
6534 * This routine is invoked to send a number @cnt of NOP mailbox command and
6535 * wait for each command to complete.
6536 *
6537 * Return: the number of NOP mailbox command completed.
6538 **/
6539static int
6540lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6541{
6542 LPFC_MBOXQ_t *mboxq;
6543 int length, cmdsent;
6544 uint32_t mbox_tmo;
6545 uint32_t rc = 0;
6546 uint32_t shdr_status, shdr_add_status;
6547 union lpfc_sli4_cfg_shdr *shdr;
6548
6549 if (cnt == 0) {
6550 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6551 "2518 Requested to send 0 NOP mailbox cmd\n");
6552 return cnt;
6553 }
6554
6555 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6556 if (!mboxq) {
6557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6558 "2519 Unable to allocate memory for issuing "
6559 "NOP mailbox command\n");
6560 return 0;
6561 }
6562
6563 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6564 length = (sizeof(struct lpfc_mbx_nop) -
6565 sizeof(struct lpfc_sli4_cfg_mhdr));
6566 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6567 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6568
6569 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6570 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6571 if (!phba->sli4_hba.intr_enable)
6572 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6573 else
6574 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6575 if (rc == MBX_TIMEOUT)
6576 break;
6577 /* Check return status */
6578 shdr = (union lpfc_sli4_cfg_shdr *)
6579 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6580 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6581 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6582 &shdr->response);
6583 if (shdr_status || shdr_add_status || rc) {
6584 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6585 "2520 NOP mailbox command failed "
6586 "status x%x add_status x%x mbx "
6587 "status x%x\n", shdr_status,
6588 shdr_add_status, rc);
6589 break;
6590 }
6591 }
6592
6593 if (rc != MBX_TIMEOUT)
6594 mempool_free(mboxq, phba->mbox_mem_pool);
6595
6596 return cmdsent;
6597}
6598
6599/**
6600 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6601 * @phba: pointer to lpfc hba data structure.
6602 * @fcfi: fcf index.
6603 *
6604 * This routine is invoked to unregister a FCFI from device.
6605 **/
6606void
6607lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6608{
6609 LPFC_MBOXQ_t *mbox;
6610 uint32_t mbox_tmo;
6611 int rc;
6612 unsigned long flags;
6613
6614 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6615
6616 if (!mbox)
6617 return;
6618
6619 lpfc_unreg_fcfi(mbox, fcfi);
6620
6621 if (!phba->sli4_hba.intr_enable)
6622 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6623 else {
6624 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6625 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6626 }
6627 if (rc != MBX_TIMEOUT)
6628 mempool_free(mbox, phba->mbox_mem_pool);
6629 if (rc != MBX_SUCCESS)
6630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6631 "2517 Unregister FCFI command failed "
6632 "status %d, mbxStatus x%x\n", rc,
6633 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6634 else {
6635 spin_lock_irqsave(&phba->hbalock, flags);
6636 /* Mark the FCFI is no longer registered */
6637 phba->fcf.fcf_flag &=
James Smartecfd03c2010-02-12 14:41:27 -05006638 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
James Smartda0436e2009-05-22 14:51:39 -04006639 spin_unlock_irqrestore(&phba->hbalock, flags);
6640 }
6641}
6642
6643/**
6644 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6645 * @phba: pointer to lpfc hba data structure.
6646 *
6647 * This routine is invoked to set up the PCI device memory space for device
6648 * with SLI-4 interface spec.
6649 *
6650 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006651 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006652 * other values - error
6653 **/
6654static int
6655lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6656{
6657 struct pci_dev *pdev;
6658 unsigned long bar0map_len, bar1map_len, bar2map_len;
6659 int error = -ENODEV;
6660
6661 /* Obtain PCI device reference */
6662 if (!phba->pcidev)
6663 return error;
6664 else
6665 pdev = phba->pcidev;
6666
6667 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006668 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6669 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6670 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6671 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006672 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006673 }
6674 }
James Smartda0436e2009-05-22 14:51:39 -04006675
6676 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6677 * number of bytes required by each mapping. They are actually
James Smart1dfb5a42010-02-12 14:40:50 -05006678 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006679 */
James Smart1dfb5a42010-02-12 14:40:50 -05006680 if (pci_resource_start(pdev, 0)) {
6681 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6682 bar0map_len = pci_resource_len(pdev, 0);
6683 } else {
6684 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6685 bar0map_len = pci_resource_len(pdev, 1);
6686 }
6687 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6688 bar1map_len = pci_resource_len(pdev, 2);
James Smartda0436e2009-05-22 14:51:39 -04006689
James Smart1dfb5a42010-02-12 14:40:50 -05006690 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6691 bar2map_len = pci_resource_len(pdev, 4);
James Smartda0436e2009-05-22 14:51:39 -04006692
6693 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6694 phba->sli4_hba.conf_regs_memmap_p =
6695 ioremap(phba->pci_bar0_map, bar0map_len);
6696 if (!phba->sli4_hba.conf_regs_memmap_p) {
6697 dev_printk(KERN_ERR, &pdev->dev,
6698 "ioremap failed for SLI4 PCI config registers.\n");
6699 goto out;
6700 }
6701
6702 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6703 phba->sli4_hba.ctrl_regs_memmap_p =
6704 ioremap(phba->pci_bar1_map, bar1map_len);
6705 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6706 dev_printk(KERN_ERR, &pdev->dev,
6707 "ioremap failed for SLI4 HBA control registers.\n");
6708 goto out_iounmap_conf;
6709 }
6710
6711 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6712 phba->sli4_hba.drbl_regs_memmap_p =
6713 ioremap(phba->pci_bar2_map, bar2map_len);
6714 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6715 dev_printk(KERN_ERR, &pdev->dev,
6716 "ioremap failed for SLI4 HBA doorbell registers.\n");
6717 goto out_iounmap_ctrl;
6718 }
6719
6720 /* Set up BAR0 PCI config space register memory map */
6721 lpfc_sli4_bar0_register_memmap(phba);
6722
6723 /* Set up BAR1 register memory map */
6724 lpfc_sli4_bar1_register_memmap(phba);
6725
6726 /* Set up BAR2 register memory map */
6727 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6728 if (error)
6729 goto out_iounmap_all;
6730
6731 return 0;
6732
6733out_iounmap_all:
6734 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6735out_iounmap_ctrl:
6736 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6737out_iounmap_conf:
6738 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6739out:
6740 return error;
6741}
6742
6743/**
6744 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6745 * @phba: pointer to lpfc hba data structure.
6746 *
6747 * This routine is invoked to unset the PCI device memory space for device
6748 * with SLI-4 interface spec.
6749 **/
6750static void
6751lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6752{
6753 struct pci_dev *pdev;
6754
6755 /* Obtain PCI device reference */
6756 if (!phba->pcidev)
6757 return;
6758 else
6759 pdev = phba->pcidev;
6760
6761 /* Free coherent DMA memory allocated */
6762
6763 /* Unmap I/O memory space */
6764 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6765 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6766 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6767
6768 return;
6769}
6770
6771/**
James Smart3772a992009-05-22 14:50:54 -04006772 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6773 * @phba: pointer to lpfc hba data structure.
6774 *
6775 * This routine is invoked to enable the MSI-X interrupt vectors to device
6776 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6777 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6778 * invoked, enables either all or nothing, depending on the current
6779 * availability of PCI vector resources. The device driver is responsible
6780 * for calling the individual request_irq() to register each MSI-X vector
6781 * with a interrupt handler, which is done in this function. Note that
6782 * later when device is unloading, the driver should always call free_irq()
6783 * on all MSI-X vectors it has done request_irq() on before calling
6784 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6785 * will be left with MSI-X enabled and leaks its vectors.
6786 *
6787 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006788 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006789 * other values - error
6790 **/
6791static int
6792lpfc_sli_enable_msix(struct lpfc_hba *phba)
6793{
6794 int rc, i;
6795 LPFC_MBOXQ_t *pmb;
6796
6797 /* Set up MSI-X multi-message vectors */
6798 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6799 phba->msix_entries[i].entry = i;
6800
6801 /* Configure MSI-X capability structure */
6802 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6803 ARRAY_SIZE(phba->msix_entries));
6804 if (rc) {
6805 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6806 "0420 PCI enable MSI-X failed (%d)\n", rc);
6807 goto msi_fail_out;
6808 }
6809 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6810 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6811 "0477 MSI-X entry[%d]: vector=x%x "
6812 "message=%d\n", i,
6813 phba->msix_entries[i].vector,
6814 phba->msix_entries[i].entry);
6815 /*
6816 * Assign MSI-X vectors to interrupt handlers
6817 */
6818
6819 /* vector-0 is associated to slow-path handler */
6820 rc = request_irq(phba->msix_entries[0].vector,
6821 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6822 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6823 if (rc) {
6824 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6825 "0421 MSI-X slow-path request_irq failed "
6826 "(%d)\n", rc);
6827 goto msi_fail_out;
6828 }
6829
6830 /* vector-1 is associated to fast-path handler */
6831 rc = request_irq(phba->msix_entries[1].vector,
6832 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6833 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6834
6835 if (rc) {
6836 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6837 "0429 MSI-X fast-path request_irq failed "
6838 "(%d)\n", rc);
6839 goto irq_fail_out;
6840 }
6841
6842 /*
6843 * Configure HBA MSI-X attention conditions to messages
6844 */
6845 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6846
6847 if (!pmb) {
6848 rc = -ENOMEM;
6849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6850 "0474 Unable to allocate memory for issuing "
6851 "MBOX_CONFIG_MSI command\n");
6852 goto mem_fail_out;
6853 }
6854 rc = lpfc_config_msi(phba, pmb);
6855 if (rc)
6856 goto mbx_fail_out;
6857 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6858 if (rc != MBX_SUCCESS) {
6859 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6860 "0351 Config MSI mailbox command failed, "
6861 "mbxCmd x%x, mbxStatus x%x\n",
6862 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6863 goto mbx_fail_out;
6864 }
6865
6866 /* Free memory allocated for mailbox command */
6867 mempool_free(pmb, phba->mbox_mem_pool);
6868 return rc;
6869
6870mbx_fail_out:
6871 /* Free memory allocated for mailbox command */
6872 mempool_free(pmb, phba->mbox_mem_pool);
6873
6874mem_fail_out:
6875 /* free the irq already requested */
6876 free_irq(phba->msix_entries[1].vector, phba);
6877
6878irq_fail_out:
6879 /* free the irq already requested */
6880 free_irq(phba->msix_entries[0].vector, phba);
6881
6882msi_fail_out:
6883 /* Unconfigure MSI-X capability structure */
6884 pci_disable_msix(phba->pcidev);
6885 return rc;
6886}
6887
6888/**
6889 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6890 * @phba: pointer to lpfc hba data structure.
6891 *
6892 * This routine is invoked to release the MSI-X vectors and then disable the
6893 * MSI-X interrupt mode to device with SLI-3 interface spec.
6894 **/
6895static void
6896lpfc_sli_disable_msix(struct lpfc_hba *phba)
6897{
6898 int i;
6899
6900 /* Free up MSI-X multi-message vectors */
6901 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6902 free_irq(phba->msix_entries[i].vector, phba);
6903 /* Disable MSI-X */
6904 pci_disable_msix(phba->pcidev);
6905
6906 return;
6907}
6908
6909/**
6910 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6911 * @phba: pointer to lpfc hba data structure.
6912 *
6913 * This routine is invoked to enable the MSI interrupt mode to device with
6914 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6915 * enable the MSI vector. The device driver is responsible for calling the
6916 * request_irq() to register MSI vector with a interrupt the handler, which
6917 * is done in this function.
6918 *
6919 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006920 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006921 * other values - error
6922 */
6923static int
6924lpfc_sli_enable_msi(struct lpfc_hba *phba)
6925{
6926 int rc;
6927
6928 rc = pci_enable_msi(phba->pcidev);
6929 if (!rc)
6930 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6931 "0462 PCI enable MSI mode success.\n");
6932 else {
6933 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6934 "0471 PCI enable MSI mode failed (%d)\n", rc);
6935 return rc;
6936 }
6937
6938 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6939 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6940 if (rc) {
6941 pci_disable_msi(phba->pcidev);
6942 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6943 "0478 MSI request_irq failed (%d)\n", rc);
6944 }
6945 return rc;
6946}
6947
6948/**
6949 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6950 * @phba: pointer to lpfc hba data structure.
6951 *
6952 * This routine is invoked to disable the MSI interrupt mode to device with
6953 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6954 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6955 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6956 * its vector.
6957 */
6958static void
6959lpfc_sli_disable_msi(struct lpfc_hba *phba)
6960{
6961 free_irq(phba->pcidev->irq, phba);
6962 pci_disable_msi(phba->pcidev);
6963 return;
6964}
6965
6966/**
6967 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6968 * @phba: pointer to lpfc hba data structure.
6969 *
6970 * This routine is invoked to enable device interrupt and associate driver's
6971 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6972 * spec. Depends on the interrupt mode configured to the driver, the driver
6973 * will try to fallback from the configured interrupt mode to an interrupt
6974 * mode which is supported by the platform, kernel, and device in the order
6975 * of:
6976 * MSI-X -> MSI -> IRQ.
6977 *
6978 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006979 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006980 * other values - error
6981 **/
6982static uint32_t
6983lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6984{
6985 uint32_t intr_mode = LPFC_INTR_ERROR;
6986 int retval;
6987
6988 if (cfg_mode == 2) {
6989 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6990 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6991 if (!retval) {
6992 /* Now, try to enable MSI-X interrupt mode */
6993 retval = lpfc_sli_enable_msix(phba);
6994 if (!retval) {
6995 /* Indicate initialization to MSI-X mode */
6996 phba->intr_type = MSIX;
6997 intr_mode = 2;
6998 }
6999 }
7000 }
7001
7002 /* Fallback to MSI if MSI-X initialization failed */
7003 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7004 retval = lpfc_sli_enable_msi(phba);
7005 if (!retval) {
7006 /* Indicate initialization to MSI mode */
7007 phba->intr_type = MSI;
7008 intr_mode = 1;
7009 }
7010 }
7011
7012 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7013 if (phba->intr_type == NONE) {
7014 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7015 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7016 if (!retval) {
7017 /* Indicate initialization to INTx mode */
7018 phba->intr_type = INTx;
7019 intr_mode = 0;
7020 }
7021 }
7022 return intr_mode;
7023}
7024
7025/**
7026 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7027 * @phba: pointer to lpfc hba data structure.
7028 *
7029 * This routine is invoked to disable device interrupt and disassociate the
7030 * driver's interrupt handler(s) from interrupt vector(s) to device with
7031 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7032 * release the interrupt vector(s) for the message signaled interrupt.
7033 **/
7034static void
7035lpfc_sli_disable_intr(struct lpfc_hba *phba)
7036{
7037 /* Disable the currently initialized interrupt mode */
7038 if (phba->intr_type == MSIX)
7039 lpfc_sli_disable_msix(phba);
7040 else if (phba->intr_type == MSI)
7041 lpfc_sli_disable_msi(phba);
7042 else if (phba->intr_type == INTx)
7043 free_irq(phba->pcidev->irq, phba);
7044
7045 /* Reset interrupt management states */
7046 phba->intr_type = NONE;
7047 phba->sli.slistat.sli_intr = 0;
7048
7049 return;
7050}
7051
7052/**
James Smartda0436e2009-05-22 14:51:39 -04007053 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7054 * @phba: pointer to lpfc hba data structure.
7055 *
7056 * This routine is invoked to enable the MSI-X interrupt vectors to device
7057 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7058 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7059 * enables either all or nothing, depending on the current availability of
7060 * PCI vector resources. The device driver is responsible for calling the
7061 * individual request_irq() to register each MSI-X vector with a interrupt
7062 * handler, which is done in this function. Note that later when device is
7063 * unloading, the driver should always call free_irq() on all MSI-X vectors
7064 * it has done request_irq() on before calling pci_disable_msix(). Failure
7065 * to do so results in a BUG_ON() and a device will be left with MSI-X
7066 * enabled and leaks its vectors.
7067 *
7068 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007069 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007070 * other values - error
7071 **/
7072static int
7073lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7074{
James Smart75baf692010-06-08 18:31:21 -04007075 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04007076
7077 /* Set up MSI-X multi-message vectors */
7078 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7079 phba->sli4_hba.msix_entries[index].entry = index;
7080
7081 /* Configure MSI-X capability structure */
James Smart75baf692010-06-08 18:31:21 -04007082 vectors = phba->sli4_hba.cfg_eqn;
7083enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04007084 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04007085 vectors);
7086 if (rc > 1) {
7087 vectors = rc;
7088 goto enable_msix_vectors;
7089 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04007090 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7091 "0484 PCI enable MSI-X failed (%d)\n", rc);
7092 goto msi_fail_out;
7093 }
James Smart75baf692010-06-08 18:31:21 -04007094
James Smartda0436e2009-05-22 14:51:39 -04007095 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04007096 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04007097 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7098 "0489 MSI-X entry[%d]: vector=x%x "
7099 "message=%d\n", index,
7100 phba->sli4_hba.msix_entries[index].vector,
7101 phba->sli4_hba.msix_entries[index].entry);
7102 /*
7103 * Assign MSI-X vectors to interrupt handlers
7104 */
7105
7106 /* The first vector must associated to slow-path handler for MQ */
7107 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7108 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7109 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7110 if (rc) {
7111 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7112 "0485 MSI-X slow-path request_irq failed "
7113 "(%d)\n", rc);
7114 goto msi_fail_out;
7115 }
7116
7117 /* The rest of the vector(s) are associated to fast-path handler(s) */
James Smart75baf692010-06-08 18:31:21 -04007118 for (index = 1; index < vectors; index++) {
James Smartda0436e2009-05-22 14:51:39 -04007119 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7120 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7121 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7122 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7123 LPFC_FP_DRIVER_HANDLER_NAME,
7124 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7125 if (rc) {
7126 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7127 "0486 MSI-X fast-path (%d) "
7128 "request_irq failed (%d)\n", index, rc);
7129 goto cfg_fail_out;
7130 }
7131 }
James Smart75baf692010-06-08 18:31:21 -04007132 phba->sli4_hba.msix_vec_nr = vectors;
James Smartda0436e2009-05-22 14:51:39 -04007133
7134 return rc;
7135
7136cfg_fail_out:
7137 /* free the irq already requested */
7138 for (--index; index >= 1; index--)
7139 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7140 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7141
7142 /* free the irq already requested */
7143 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7144
7145msi_fail_out:
7146 /* Unconfigure MSI-X capability structure */
7147 pci_disable_msix(phba->pcidev);
7148 return rc;
7149}
7150
7151/**
7152 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7153 * @phba: pointer to lpfc hba data structure.
7154 *
7155 * This routine is invoked to release the MSI-X vectors and then disable the
7156 * MSI-X interrupt mode to device with SLI-4 interface spec.
7157 **/
7158static void
7159lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7160{
7161 int index;
7162
7163 /* Free up MSI-X multi-message vectors */
7164 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7165
James Smart75baf692010-06-08 18:31:21 -04007166 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
James Smartda0436e2009-05-22 14:51:39 -04007167 free_irq(phba->sli4_hba.msix_entries[index].vector,
7168 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
James Smart75baf692010-06-08 18:31:21 -04007169
James Smartda0436e2009-05-22 14:51:39 -04007170 /* Disable MSI-X */
7171 pci_disable_msix(phba->pcidev);
7172
7173 return;
7174}
7175
7176/**
7177 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7178 * @phba: pointer to lpfc hba data structure.
7179 *
7180 * This routine is invoked to enable the MSI interrupt mode to device with
7181 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7182 * to enable the MSI vector. The device driver is responsible for calling
7183 * the request_irq() to register MSI vector with a interrupt the handler,
7184 * which is done in this function.
7185 *
7186 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007187 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007188 * other values - error
7189 **/
7190static int
7191lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7192{
7193 int rc, index;
7194
7195 rc = pci_enable_msi(phba->pcidev);
7196 if (!rc)
7197 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7198 "0487 PCI enable MSI mode success.\n");
7199 else {
7200 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7201 "0488 PCI enable MSI mode failed (%d)\n", rc);
7202 return rc;
7203 }
7204
7205 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7206 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7207 if (rc) {
7208 pci_disable_msi(phba->pcidev);
7209 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7210 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04007211 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007212 }
7213
7214 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7215 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7216 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7217 }
7218
James Smart75baf692010-06-08 18:31:21 -04007219 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007220}
7221
7222/**
7223 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7224 * @phba: pointer to lpfc hba data structure.
7225 *
7226 * This routine is invoked to disable the MSI interrupt mode to device with
7227 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7228 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7229 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7230 * its vector.
7231 **/
7232static void
7233lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7234{
7235 free_irq(phba->pcidev->irq, phba);
7236 pci_disable_msi(phba->pcidev);
7237 return;
7238}
7239
7240/**
7241 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7242 * @phba: pointer to lpfc hba data structure.
7243 *
7244 * This routine is invoked to enable device interrupt and associate driver's
7245 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7246 * interface spec. Depends on the interrupt mode configured to the driver,
7247 * the driver will try to fallback from the configured interrupt mode to an
7248 * interrupt mode which is supported by the platform, kernel, and device in
7249 * the order of:
7250 * MSI-X -> MSI -> IRQ.
7251 *
7252 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007253 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007254 * other values - error
7255 **/
7256static uint32_t
7257lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7258{
7259 uint32_t intr_mode = LPFC_INTR_ERROR;
7260 int retval, index;
7261
7262 if (cfg_mode == 2) {
7263 /* Preparation before conf_msi mbox cmd */
7264 retval = 0;
7265 if (!retval) {
7266 /* Now, try to enable MSI-X interrupt mode */
7267 retval = lpfc_sli4_enable_msix(phba);
7268 if (!retval) {
7269 /* Indicate initialization to MSI-X mode */
7270 phba->intr_type = MSIX;
7271 intr_mode = 2;
7272 }
7273 }
7274 }
7275
7276 /* Fallback to MSI if MSI-X initialization failed */
7277 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7278 retval = lpfc_sli4_enable_msi(phba);
7279 if (!retval) {
7280 /* Indicate initialization to MSI mode */
7281 phba->intr_type = MSI;
7282 intr_mode = 1;
7283 }
7284 }
7285
7286 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7287 if (phba->intr_type == NONE) {
7288 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7289 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7290 if (!retval) {
7291 /* Indicate initialization to INTx mode */
7292 phba->intr_type = INTx;
7293 intr_mode = 0;
7294 for (index = 0; index < phba->cfg_fcp_eq_count;
7295 index++) {
7296 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7297 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7298 }
7299 }
7300 }
7301 return intr_mode;
7302}
7303
7304/**
7305 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7306 * @phba: pointer to lpfc hba data structure.
7307 *
7308 * This routine is invoked to disable device interrupt and disassociate
7309 * the driver's interrupt handler(s) from interrupt vector(s) to device
7310 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7311 * will release the interrupt vector(s) for the message signaled interrupt.
7312 **/
7313static void
7314lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7315{
7316 /* Disable the currently initialized interrupt mode */
7317 if (phba->intr_type == MSIX)
7318 lpfc_sli4_disable_msix(phba);
7319 else if (phba->intr_type == MSI)
7320 lpfc_sli4_disable_msi(phba);
7321 else if (phba->intr_type == INTx)
7322 free_irq(phba->pcidev->irq, phba);
7323
7324 /* Reset interrupt management states */
7325 phba->intr_type = NONE;
7326 phba->sli.slistat.sli_intr = 0;
7327
7328 return;
7329}
7330
7331/**
James Smart3772a992009-05-22 14:50:54 -04007332 * lpfc_unset_hba - Unset SLI3 hba device initialization
7333 * @phba: pointer to lpfc hba data structure.
7334 *
7335 * This routine is invoked to unset the HBA device initialization steps to
7336 * a device with SLI-3 interface spec.
7337 **/
7338static void
7339lpfc_unset_hba(struct lpfc_hba *phba)
7340{
7341 struct lpfc_vport *vport = phba->pport;
7342 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7343
7344 spin_lock_irq(shost->host_lock);
7345 vport->load_flag |= FC_UNLOADING;
7346 spin_unlock_irq(shost->host_lock);
7347
7348 lpfc_stop_hba_timers(phba);
7349
7350 phba->pport->work_port_events = 0;
7351
7352 lpfc_sli_hba_down(phba);
7353
7354 lpfc_sli_brdrestart(phba);
7355
7356 lpfc_sli_disable_intr(phba);
7357
7358 return;
7359}
7360
7361/**
James Smartda0436e2009-05-22 14:51:39 -04007362 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7363 * @phba: pointer to lpfc hba data structure.
7364 *
7365 * This routine is invoked to unset the HBA device initialization steps to
7366 * a device with SLI-4 interface spec.
7367 **/
7368static void
7369lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7370{
7371 struct lpfc_vport *vport = phba->pport;
7372 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7373
7374 spin_lock_irq(shost->host_lock);
7375 vport->load_flag |= FC_UNLOADING;
7376 spin_unlock_irq(shost->host_lock);
7377
7378 phba->pport->work_port_events = 0;
7379
7380 lpfc_sli4_hba_down(phba);
7381
7382 lpfc_sli4_disable_intr(phba);
7383
7384 return;
7385}
7386
7387/**
7388 * lpfc_sli4_hba_unset - Unset the fcoe hba
7389 * @phba: Pointer to HBA context object.
7390 *
7391 * This function is called in the SLI4 code path to reset the HBA's FCoE
7392 * function. The caller is not required to hold any lock. This routine
7393 * issues PCI function reset mailbox command to reset the FCoE function.
7394 * At the end of the function, it calls lpfc_hba_down_post function to
7395 * free any pending commands.
7396 **/
7397static void
7398lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7399{
7400 int wait_cnt = 0;
7401 LPFC_MBOXQ_t *mboxq;
7402
7403 lpfc_stop_hba_timers(phba);
7404 phba->sli4_hba.intr_enable = 0;
7405
7406 /*
7407 * Gracefully wait out the potential current outstanding asynchronous
7408 * mailbox command.
7409 */
7410
7411 /* First, block any pending async mailbox command from posted */
7412 spin_lock_irq(&phba->hbalock);
7413 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7414 spin_unlock_irq(&phba->hbalock);
7415 /* Now, trying to wait it out if we can */
7416 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7417 msleep(10);
7418 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7419 break;
7420 }
7421 /* Forcefully release the outstanding mailbox command if timed out */
7422 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7423 spin_lock_irq(&phba->hbalock);
7424 mboxq = phba->sli.mbox_active;
7425 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7426 __lpfc_mbox_cmpl_put(phba, mboxq);
7427 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7428 phba->sli.mbox_active = NULL;
7429 spin_unlock_irq(&phba->hbalock);
7430 }
7431
7432 /* Tear down the queues in the HBA */
7433 lpfc_sli4_queue_unset(phba);
7434
7435 /* Disable PCI subsystem interrupt */
7436 lpfc_sli4_disable_intr(phba);
7437
7438 /* Stop kthread signal shall trigger work_done one more time */
7439 kthread_stop(phba->worker_thread);
7440
7441 /* Stop the SLI4 device port */
7442 phba->pport->work_port_events = 0;
7443}
7444
James Smart28baac72010-02-12 14:42:03 -05007445 /**
7446 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7447 * @phba: Pointer to HBA context object.
7448 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7449 *
7450 * This function is called in the SLI4 code path to read the port's
7451 * sli4 capabilities.
7452 *
7453 * This function may be be called from any context that can block-wait
7454 * for the completion. The expectation is that this routine is called
7455 * typically from probe_one or from the online routine.
7456 **/
7457int
7458lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7459{
7460 int rc;
7461 struct lpfc_mqe *mqe;
7462 struct lpfc_pc_sli4_params *sli4_params;
7463 uint32_t mbox_tmo;
7464
7465 rc = 0;
7466 mqe = &mboxq->u.mqe;
7467
7468 /* Read the port's SLI4 Parameters port capabilities */
7469 lpfc_sli4_params(mboxq);
7470 if (!phba->sli4_hba.intr_enable)
7471 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7472 else {
7473 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7474 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7475 }
7476
7477 if (unlikely(rc))
7478 return 1;
7479
7480 sli4_params = &phba->sli4_hba.pc_sli4_params;
7481 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7482 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7483 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7484 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7485 &mqe->un.sli4_params);
7486 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7487 &mqe->un.sli4_params);
7488 sli4_params->proto_types = mqe->un.sli4_params.word3;
7489 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7490 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7491 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7492 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7493 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7494 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7495 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7496 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7497 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7498 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7499 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7500 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7501 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7502 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7503 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7504 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7505 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7506 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7507 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7508 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7509 return rc;
7510}
7511
James Smartda0436e2009-05-22 14:51:39 -04007512/**
James Smart3772a992009-05-22 14:50:54 -04007513 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7514 * @pdev: pointer to PCI device
7515 * @pid: pointer to PCI device identifier
7516 *
7517 * This routine is to be called to attach a device with SLI-3 interface spec
7518 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7519 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7520 * information of the device and driver to see if the driver state that it can
7521 * support this kind of device. If the match is successful, the driver core
7522 * invokes this routine. If this routine determines it can claim the HBA, it
7523 * does all the initialization that it needs to do to handle the HBA properly.
7524 *
7525 * Return code
7526 * 0 - driver can claim the device
7527 * negative value - driver can not claim the device
7528 **/
7529static int __devinit
7530lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7531{
7532 struct lpfc_hba *phba;
7533 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007534 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007535 int error;
7536 uint32_t cfg_mode, intr_mode;
7537
7538 /* Allocate memory for HBA structure */
7539 phba = lpfc_hba_alloc(pdev);
7540 if (!phba)
7541 return -ENOMEM;
7542
7543 /* Perform generic PCI device enabling operation */
7544 error = lpfc_enable_pci_dev(phba);
7545 if (error) {
7546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7547 "1401 Failed to enable pci device.\n");
7548 goto out_free_phba;
7549 }
7550
7551 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7552 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7553 if (error)
7554 goto out_disable_pci_dev;
7555
7556 /* Set up SLI-3 specific device PCI memory space */
7557 error = lpfc_sli_pci_mem_setup(phba);
7558 if (error) {
7559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7560 "1402 Failed to set up pci memory space.\n");
7561 goto out_disable_pci_dev;
7562 }
7563
7564 /* Set up phase-1 common device driver resources */
7565 error = lpfc_setup_driver_resource_phase1(phba);
7566 if (error) {
7567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7568 "1403 Failed to set up driver resource.\n");
7569 goto out_unset_pci_mem_s3;
7570 }
7571
7572 /* Set up SLI-3 specific device driver resources */
7573 error = lpfc_sli_driver_resource_setup(phba);
7574 if (error) {
7575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7576 "1404 Failed to set up driver resource.\n");
7577 goto out_unset_pci_mem_s3;
7578 }
7579
7580 /* Initialize and populate the iocb list per host */
7581 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7582 if (error) {
7583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7584 "1405 Failed to initialize iocb list.\n");
7585 goto out_unset_driver_resource_s3;
7586 }
7587
7588 /* Set up common device driver resources */
7589 error = lpfc_setup_driver_resource_phase2(phba);
7590 if (error) {
7591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7592 "1406 Failed to set up driver resource.\n");
7593 goto out_free_iocb_list;
7594 }
7595
7596 /* Create SCSI host to the physical port */
7597 error = lpfc_create_shost(phba);
7598 if (error) {
7599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7600 "1407 Failed to create scsi host.\n");
7601 goto out_unset_driver_resource;
7602 }
7603
7604 /* Configure sysfs attributes */
7605 vport = phba->pport;
7606 error = lpfc_alloc_sysfs_attr(vport);
7607 if (error) {
7608 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7609 "1476 Failed to allocate sysfs attr\n");
7610 goto out_destroy_shost;
7611 }
7612
James Smart6669f9b2009-10-02 15:16:45 -04007613 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04007614 /* Now, trying to enable interrupt and bring up the device */
7615 cfg_mode = phba->cfg_use_msi;
7616 while (true) {
7617 /* Put device to a known state before enabling interrupt */
7618 lpfc_stop_port(phba);
7619 /* Configure and enable interrupt */
7620 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7621 if (intr_mode == LPFC_INTR_ERROR) {
7622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7623 "0431 Failed to enable interrupt.\n");
7624 error = -ENODEV;
7625 goto out_free_sysfs_attr;
7626 }
7627 /* SLI-3 HBA setup */
7628 if (lpfc_sli_hba_setup(phba)) {
7629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7630 "1477 Failed to set up hba\n");
7631 error = -ENODEV;
7632 goto out_remove_device;
7633 }
7634
7635 /* Wait 50ms for the interrupts of previous mailbox commands */
7636 msleep(50);
7637 /* Check active interrupts on message signaled interrupts */
7638 if (intr_mode == 0 ||
7639 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7640 /* Log the current active interrupt mode */
7641 phba->intr_mode = intr_mode;
7642 lpfc_log_intr_mode(phba, intr_mode);
7643 break;
7644 } else {
7645 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7646 "0447 Configure interrupt mode (%d) "
7647 "failed active interrupt test.\n",
7648 intr_mode);
7649 /* Disable the current interrupt mode */
7650 lpfc_sli_disable_intr(phba);
7651 /* Try next level of interrupt mode */
7652 cfg_mode = --intr_mode;
7653 }
7654 }
7655
7656 /* Perform post initialization setup */
7657 lpfc_post_init_setup(phba);
7658
7659 /* Check if there are static vports to be created. */
7660 lpfc_create_static_vport(phba);
7661
7662 return 0;
7663
7664out_remove_device:
7665 lpfc_unset_hba(phba);
7666out_free_sysfs_attr:
7667 lpfc_free_sysfs_attr(vport);
7668out_destroy_shost:
7669 lpfc_destroy_shost(phba);
7670out_unset_driver_resource:
7671 lpfc_unset_driver_resource_phase2(phba);
7672out_free_iocb_list:
7673 lpfc_free_iocb_list(phba);
7674out_unset_driver_resource_s3:
7675 lpfc_sli_driver_resource_unset(phba);
7676out_unset_pci_mem_s3:
7677 lpfc_sli_pci_mem_unset(phba);
7678out_disable_pci_dev:
7679 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007680 if (shost)
7681 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007682out_free_phba:
7683 lpfc_hba_free(phba);
7684 return error;
7685}
7686
7687/**
7688 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007689 * @pdev: pointer to PCI device
7690 *
James Smart3772a992009-05-22 14:50:54 -04007691 * This routine is to be called to disattach a device with SLI-3 interface
7692 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7693 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7694 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007695 **/
dea31012005-04-17 16:05:31 -05007696static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007697lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007698{
James Smart2e0fef82007-06-17 19:56:36 -05007699 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7700 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007701 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007702 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007703 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007704 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7705
James Smart549e55c2007-08-02 11:09:51 -04007706 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007707 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007708 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007709
James Smart858c9f62007-06-17 19:56:39 -05007710 lpfc_free_sysfs_attr(vport);
7711
James Smarteada2722008-12-04 22:39:13 -05007712 /* Release all the vports against this physical port */
7713 vports = lpfc_create_vport_work_array(phba);
7714 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007715 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007716 fc_vport_terminate(vports[i]->fc_vport);
7717 lpfc_destroy_vport_work_array(phba, vports);
7718
7719 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007720 fc_remove_host(shost);
7721 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007722 lpfc_cleanup(vport);
7723
James Smart2e0fef82007-06-17 19:56:36 -05007724 /*
7725 * Bring down the SLI Layer. This step disable all interrupts,
7726 * clears the rings, discards all mailbox commands, and resets
7727 * the HBA.
7728 */
James Smarta257bf92009-04-06 18:48:10 -04007729
7730 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007731 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007732 /* Stop kthread signal shall trigger work_done one more time */
7733 kthread_stop(phba->worker_thread);
7734 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007735 lpfc_sli_brdrestart(phba);
7736
James Smart3772a992009-05-22 14:50:54 -04007737 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007738 spin_lock_irq(&phba->hbalock);
7739 list_del_init(&vport->listentry);
7740 spin_unlock_irq(&phba->hbalock);
7741
James Smart858c9f62007-06-17 19:56:39 -05007742 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007743
James Smart5b75da22008-12-04 22:39:35 -05007744 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007745 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007746
7747 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007748 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007749
7750 /*
7751 * Call scsi_free before mem_free since scsi bufs are released to their
7752 * corresponding pools here.
7753 */
7754 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007755 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007756
James Smart34b02dc2008-08-24 21:49:55 -04007757 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7758 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007759
James Smart2e0fef82007-06-17 19:56:36 -05007760 /* Free resources associated with SLI2 interface */
7761 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007762 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007763
7764 /* unmap adapter SLIM and Control Registers */
7765 iounmap(phba->ctrl_regs_memmap_p);
7766 iounmap(phba->slim_memmap_p);
7767
James Smart3772a992009-05-22 14:50:54 -04007768 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007769
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007770 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007771 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007772}
7773
Linas Vepstas8d63f372007-02-14 14:28:36 -06007774/**
James Smart3772a992009-05-22 14:50:54 -04007775 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007776 * @pdev: pointer to PCI device
7777 * @msg: power management message
7778 *
James Smart3772a992009-05-22 14:50:54 -04007779 * This routine is to be called from the kernel's PCI subsystem to support
7780 * system Power Management (PM) to device with SLI-3 interface spec. When
7781 * PM invokes this method, it quiesces the device by stopping the driver's
7782 * worker thread for the device, turning off device's interrupt and DMA,
7783 * and bring the device offline. Note that as the driver implements the
7784 * minimum PM requirements to a power-aware driver's PM support for the
7785 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7786 * to the suspend() method call will be treated as SUSPEND and the driver will
7787 * fully reinitialize its device during resume() method call, the driver will
7788 * set device to PCI_D3hot state in PCI config space instead of setting it
7789 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007790 *
7791 * Return code
James Smart3772a992009-05-22 14:50:54 -04007792 * 0 - driver suspended the device
7793 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007794 **/
7795static int
James Smart3772a992009-05-22 14:50:54 -04007796lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007797{
7798 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7799 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7800
7801 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7802 "0473 PCI device Power Management suspend.\n");
7803
7804 /* Bring down the device */
7805 lpfc_offline_prep(phba);
7806 lpfc_offline(phba);
7807 kthread_stop(phba->worker_thread);
7808
7809 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007810 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007811
7812 /* Save device state to PCI config space */
7813 pci_save_state(pdev);
7814 pci_set_power_state(pdev, PCI_D3hot);
7815
7816 return 0;
7817}
7818
7819/**
James Smart3772a992009-05-22 14:50:54 -04007820 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007821 * @pdev: pointer to PCI device
7822 *
James Smart3772a992009-05-22 14:50:54 -04007823 * This routine is to be called from the kernel's PCI subsystem to support
7824 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7825 * invokes this method, it restores the device's PCI config space state and
7826 * fully reinitializes the device and brings it online. Note that as the
7827 * driver implements the minimum PM requirements to a power-aware driver's
7828 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7829 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7830 * driver will fully reinitialize its device during resume() method call,
7831 * the device will be set to PCI_D0 directly in PCI config space before
7832 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007833 *
7834 * Return code
James Smart3772a992009-05-22 14:50:54 -04007835 * 0 - driver suspended the device
7836 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007837 **/
7838static int
James Smart3772a992009-05-22 14:50:54 -04007839lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007840{
7841 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7842 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007843 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007844 int error;
7845
7846 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7847 "0452 PCI device Power Management resume.\n");
7848
7849 /* Restore device state from PCI config space */
7850 pci_set_power_state(pdev, PCI_D0);
7851 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007852
James Smart1dfb5a42010-02-12 14:40:50 -05007853 /*
7854 * As the new kernel behavior of pci_restore_state() API call clears
7855 * device saved_state flag, need to save the restored state again.
7856 */
7857 pci_save_state(pdev);
7858
James Smart3a55b532008-12-04 22:38:54 -05007859 if (pdev->is_busmaster)
7860 pci_set_master(pdev);
7861
7862 /* Startup the kernel thread for this host adapter. */
7863 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7864 "lpfc_worker_%d", phba->brd_no);
7865 if (IS_ERR(phba->worker_thread)) {
7866 error = PTR_ERR(phba->worker_thread);
7867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7868 "0434 PM resume failed to start worker "
7869 "thread: error=x%x.\n", error);
7870 return error;
7871 }
7872
James Smart5b75da22008-12-04 22:39:35 -05007873 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007874 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007875 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007877 "0430 PM resume Failed to enable interrupt\n");
7878 return -EIO;
7879 } else
7880 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007881
7882 /* Restart HBA and bring it online */
7883 lpfc_sli_brdrestart(phba);
7884 lpfc_online(phba);
7885
James Smart5b75da22008-12-04 22:39:35 -05007886 /* Log the current active interrupt mode */
7887 lpfc_log_intr_mode(phba, phba->intr_mode);
7888
James Smart3a55b532008-12-04 22:38:54 -05007889 return 0;
7890}
7891
7892/**
James Smart891478a2009-11-18 15:40:23 -05007893 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7894 * @phba: pointer to lpfc hba data structure.
7895 *
7896 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04007897 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05007898 **/
7899static void
7900lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7901{
James Smarte2af0d22010-03-15 11:25:32 -04007902 struct lpfc_sli *psli = &phba->sli;
7903 struct lpfc_sli_ring *pring;
7904
James Smart891478a2009-11-18 15:40:23 -05007905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7906 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04007907
7908 /*
7909 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
7910 * and let the SCSI mid-layer to retry them to recover.
7911 */
7912 pring = &psli->ring[psli->fcp_ring];
7913 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05007914}
7915
7916/**
James Smart0d878412009-10-02 15:16:56 -04007917 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7918 * @phba: pointer to lpfc hba data structure.
7919 *
7920 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7921 * disables the device interrupt and pci device, and aborts the internal FCP
7922 * pending I/Os.
7923 **/
7924static void
7925lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7926{
James Smart0d878412009-10-02 15:16:56 -04007927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007928 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04007929
James Smart75baf692010-06-08 18:31:21 -04007930 /* Block any management I/Os to the device */
7931 lpfc_block_mgmt_io(phba);
7932
James Smarte2af0d22010-03-15 11:25:32 -04007933 /* Block all SCSI devices' I/Os on the host */
7934 lpfc_scsi_dev_block(phba);
7935
7936 /* stop all timers */
7937 lpfc_stop_hba_timers(phba);
7938
James Smart0d878412009-10-02 15:16:56 -04007939 /* Disable interrupt and pci device */
7940 lpfc_sli_disable_intr(phba);
7941 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04007942
James Smarte2af0d22010-03-15 11:25:32 -04007943 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
7944 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04007945}
7946
7947/**
7948 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7949 * @phba: pointer to lpfc hba data structure.
7950 *
7951 * This routine is called to prepare the SLI3 device for PCI slot permanently
7952 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7953 * pending I/Os.
7954 **/
7955static void
James Smart75baf692010-06-08 18:31:21 -04007956lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04007957{
7958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007959 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04007960 /* Block all SCSI devices' I/Os on the host */
7961 lpfc_scsi_dev_block(phba);
7962
7963 /* stop all timers */
7964 lpfc_stop_hba_timers(phba);
7965
James Smart0d878412009-10-02 15:16:56 -04007966 /* Clean up all driver's outstanding SCSI I/Os */
7967 lpfc_sli_flush_fcp_rings(phba);
7968}
7969
7970/**
James Smart3772a992009-05-22 14:50:54 -04007971 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007972 * @pdev: pointer to PCI device.
7973 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007974 *
James Smart3772a992009-05-22 14:50:54 -04007975 * This routine is called from the PCI subsystem for I/O error handling to
7976 * device with SLI-3 interface spec. This function is called by the PCI
7977 * subsystem after a PCI bus error affecting this device has been detected.
7978 * When this function is invoked, it will need to stop all the I/Os and
7979 * interrupt(s) to the device. Once that is done, it will return
7980 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7981 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007982 *
7983 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007984 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007985 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7986 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007987 **/
James Smart3772a992009-05-22 14:50:54 -04007988static pci_ers_result_t
7989lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007990{
James Smart51ef4c22007-08-02 11:10:31 -04007991 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7992 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007993
James Smart0d878412009-10-02 15:16:56 -04007994 switch (state) {
7995 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007996 /* Non-fatal error, prepare for recovery */
7997 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007998 return PCI_ERS_RESULT_CAN_RECOVER;
7999 case pci_channel_io_frozen:
8000 /* Fatal error, prepare for slot reset */
8001 lpfc_sli_prep_dev_for_reset(phba);
8002 return PCI_ERS_RESULT_NEED_RESET;
8003 case pci_channel_io_perm_failure:
8004 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04008005 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008006 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04008007 default:
8008 /* Unknown state, prepare and request slot reset */
8009 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8010 "0472 Unknown PCI error state: x%x\n", state);
8011 lpfc_sli_prep_dev_for_reset(phba);
8012 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04008013 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008014}
8015
8016/**
James Smart3772a992009-05-22 14:50:54 -04008017 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04008018 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008019 *
James Smart3772a992009-05-22 14:50:54 -04008020 * This routine is called from the PCI subsystem for error handling to
8021 * device with SLI-3 interface spec. This is called after PCI bus has been
8022 * reset to restart the PCI card from scratch, as if from a cold-boot.
8023 * During the PCI subsystem error recovery, after driver returns
8024 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8025 * recovery and then call this routine before calling the .resume method
8026 * to recover the device. This function will initialize the HBA device,
8027 * enable the interrupt, but it will just put the HBA to offline state
8028 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04008029 *
8030 * Return codes
James Smart3772a992009-05-22 14:50:54 -04008031 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8032 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06008033 */
James Smart3772a992009-05-22 14:50:54 -04008034static pci_ers_result_t
8035lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008036{
James Smart51ef4c22007-08-02 11:10:31 -04008037 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8038 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008039 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05008040 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008041
8042 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11008043 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06008044 printk(KERN_ERR "lpfc: Cannot re-enable "
8045 "PCI device after reset.\n");
8046 return PCI_ERS_RESULT_DISCONNECT;
8047 }
8048
James Smart97207482008-12-04 22:39:19 -05008049 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008050
8051 /*
8052 * As the new kernel behavior of pci_restore_state() API call clears
8053 * device saved_state flag, need to save the restored state again.
8054 */
8055 pci_save_state(pdev);
8056
James Smart97207482008-12-04 22:39:19 -05008057 if (pdev->is_busmaster)
8058 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008059
James Smart92d7f7b2007-06-17 19:56:38 -05008060 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008061 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05008062 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008063
James Smart5b75da22008-12-04 22:39:35 -05008064 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008065 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008066 if (intr_mode == LPFC_INTR_ERROR) {
8067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8068 "0427 Cannot re-enable interrupt after "
8069 "slot reset.\n");
8070 return PCI_ERS_RESULT_DISCONNECT;
8071 } else
8072 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008073
James Smart75baf692010-06-08 18:31:21 -04008074 /* Take device offline, it will perform cleanup */
8075 lpfc_offline_prep(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008076 lpfc_offline(phba);
8077 lpfc_sli_brdrestart(phba);
8078
James Smart5b75da22008-12-04 22:39:35 -05008079 /* Log the current active interrupt mode */
8080 lpfc_log_intr_mode(phba, phba->intr_mode);
8081
Linas Vepstas8d63f372007-02-14 14:28:36 -06008082 return PCI_ERS_RESULT_RECOVERED;
8083}
8084
8085/**
James Smart3772a992009-05-22 14:50:54 -04008086 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04008087 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06008088 *
James Smart3772a992009-05-22 14:50:54 -04008089 * This routine is called from the PCI subsystem for error handling to device
8090 * with SLI-3 interface spec. It is called when kernel error recovery tells
8091 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8092 * error recovery. After this call, traffic can start to flow from this device
8093 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008094 */
James Smart3772a992009-05-22 14:50:54 -04008095static void
8096lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008097{
James Smart51ef4c22007-08-02 11:10:31 -04008098 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8099 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008100
James Smarte2af0d22010-03-15 11:25:32 -04008101 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04008102 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04008103
8104 /* Clean up Advanced Error Reporting (AER) if needed */
8105 if (phba->hba_flag & HBA_AER_ENABLED)
8106 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008107}
8108
James Smart3772a992009-05-22 14:50:54 -04008109/**
James Smartda0436e2009-05-22 14:51:39 -04008110 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8111 * @phba: pointer to lpfc hba data structure.
8112 *
8113 * returns the number of ELS/CT IOCBs to reserve
8114 **/
8115int
8116lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8117{
8118 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8119
James Smartf1126682009-06-10 17:22:44 -04008120 if (phba->sli_rev == LPFC_SLI_REV4) {
8121 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04008122 return 10;
James Smartf1126682009-06-10 17:22:44 -04008123 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04008124 return 25;
James Smartf1126682009-06-10 17:22:44 -04008125 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04008126 return 50;
James Smartf1126682009-06-10 17:22:44 -04008127 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04008128 return 100;
James Smartf1126682009-06-10 17:22:44 -04008129 else
James Smart6a9c52c2009-10-02 15:16:51 -04008130 return 150;
James Smartf1126682009-06-10 17:22:44 -04008131 } else
8132 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008133}
8134
8135/**
8136 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
8137 * @pdev: pointer to PCI device
8138 * @pid: pointer to PCI device identifier
8139 *
8140 * This routine is called from the kernel's PCI subsystem to device with
8141 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8142 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8143 * information of the device and driver to see if the driver state that it
8144 * can support this kind of device. If the match is successful, the driver
8145 * core invokes this routine. If this routine determines it can claim the HBA,
8146 * it does all the initialization that it needs to do to handle the HBA
8147 * properly.
8148 *
8149 * Return code
8150 * 0 - driver can claim the device
8151 * negative value - driver can not claim the device
8152 **/
8153static int __devinit
8154lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8155{
8156 struct lpfc_hba *phba;
8157 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008158 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008159 int error;
8160 uint32_t cfg_mode, intr_mode;
8161 int mcnt;
8162
8163 /* Allocate memory for HBA structure */
8164 phba = lpfc_hba_alloc(pdev);
8165 if (!phba)
8166 return -ENOMEM;
8167
8168 /* Perform generic PCI device enabling operation */
8169 error = lpfc_enable_pci_dev(phba);
8170 if (error) {
8171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8172 "1409 Failed to enable pci device.\n");
8173 goto out_free_phba;
8174 }
8175
8176 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
8177 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
8178 if (error)
8179 goto out_disable_pci_dev;
8180
8181 /* Set up SLI-4 specific device PCI memory space */
8182 error = lpfc_sli4_pci_mem_setup(phba);
8183 if (error) {
8184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8185 "1410 Failed to set up pci memory space.\n");
8186 goto out_disable_pci_dev;
8187 }
8188
8189 /* Set up phase-1 common device driver resources */
8190 error = lpfc_setup_driver_resource_phase1(phba);
8191 if (error) {
8192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8193 "1411 Failed to set up driver resource.\n");
8194 goto out_unset_pci_mem_s4;
8195 }
8196
8197 /* Set up SLI-4 Specific device driver resources */
8198 error = lpfc_sli4_driver_resource_setup(phba);
8199 if (error) {
8200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8201 "1412 Failed to set up driver resource.\n");
8202 goto out_unset_pci_mem_s4;
8203 }
8204
8205 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04008206
8207 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8208 "2821 initialize iocb list %d.\n",
8209 phba->cfg_iocb_cnt*1024);
8210 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
8211
James Smartda0436e2009-05-22 14:51:39 -04008212 if (error) {
8213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8214 "1413 Failed to initialize iocb list.\n");
8215 goto out_unset_driver_resource_s4;
8216 }
8217
8218 /* Set up common device driver resources */
8219 error = lpfc_setup_driver_resource_phase2(phba);
8220 if (error) {
8221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8222 "1414 Failed to set up driver resource.\n");
8223 goto out_free_iocb_list;
8224 }
8225
8226 /* Create SCSI host to the physical port */
8227 error = lpfc_create_shost(phba);
8228 if (error) {
8229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8230 "1415 Failed to create scsi host.\n");
8231 goto out_unset_driver_resource;
8232 }
8233
8234 /* Configure sysfs attributes */
8235 vport = phba->pport;
8236 error = lpfc_alloc_sysfs_attr(vport);
8237 if (error) {
8238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8239 "1416 Failed to allocate sysfs attr\n");
8240 goto out_destroy_shost;
8241 }
8242
James Smart6669f9b2009-10-02 15:16:45 -04008243 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04008244 /* Now, trying to enable interrupt and bring up the device */
8245 cfg_mode = phba->cfg_use_msi;
8246 while (true) {
8247 /* Put device to a known state before enabling interrupt */
8248 lpfc_stop_port(phba);
8249 /* Configure and enable interrupt */
8250 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
8251 if (intr_mode == LPFC_INTR_ERROR) {
8252 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8253 "0426 Failed to enable interrupt.\n");
8254 error = -ENODEV;
8255 goto out_free_sysfs_attr;
8256 }
James Smartdef9c7a2009-12-21 17:02:28 -05008257 /* Default to single FCP EQ for non-MSI-X */
8258 if (phba->intr_type != MSIX)
8259 phba->cfg_fcp_eq_count = 1;
James Smart75baf692010-06-08 18:31:21 -04008260 else if (phba->sli4_hba.msix_vec_nr < phba->cfg_fcp_eq_count)
8261 phba->cfg_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
James Smartda0436e2009-05-22 14:51:39 -04008262 /* Set up SLI-4 HBA */
8263 if (lpfc_sli4_hba_setup(phba)) {
8264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8265 "1421 Failed to set up hba\n");
8266 error = -ENODEV;
8267 goto out_disable_intr;
8268 }
8269
8270 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
8271 if (intr_mode != 0)
8272 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
8273 LPFC_ACT_INTR_CNT);
8274
8275 /* Check active interrupts received only for MSI/MSI-X */
8276 if (intr_mode == 0 ||
8277 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
8278 /* Log the current active interrupt mode */
8279 phba->intr_mode = intr_mode;
8280 lpfc_log_intr_mode(phba, intr_mode);
8281 break;
8282 }
8283 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8284 "0451 Configure interrupt mode (%d) "
8285 "failed active interrupt test.\n",
8286 intr_mode);
8287 /* Unset the preivous SLI-4 HBA setup */
8288 lpfc_sli4_unset_hba(phba);
8289 /* Try next level of interrupt mode */
8290 cfg_mode = --intr_mode;
8291 }
8292
8293 /* Perform post initialization setup */
8294 lpfc_post_init_setup(phba);
8295
James Smart1c6834a2009-07-19 10:01:26 -04008296 /* Check if there are static vports to be created. */
8297 lpfc_create_static_vport(phba);
8298
James Smartda0436e2009-05-22 14:51:39 -04008299 return 0;
8300
8301out_disable_intr:
8302 lpfc_sli4_disable_intr(phba);
8303out_free_sysfs_attr:
8304 lpfc_free_sysfs_attr(vport);
8305out_destroy_shost:
8306 lpfc_destroy_shost(phba);
8307out_unset_driver_resource:
8308 lpfc_unset_driver_resource_phase2(phba);
8309out_free_iocb_list:
8310 lpfc_free_iocb_list(phba);
8311out_unset_driver_resource_s4:
8312 lpfc_sli4_driver_resource_unset(phba);
8313out_unset_pci_mem_s4:
8314 lpfc_sli4_pci_mem_unset(phba);
8315out_disable_pci_dev:
8316 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008317 if (shost)
8318 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04008319out_free_phba:
8320 lpfc_hba_free(phba);
8321 return error;
8322}
8323
8324/**
8325 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8326 * @pdev: pointer to PCI device
8327 *
8328 * This routine is called from the kernel's PCI subsystem to device with
8329 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8330 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8331 * device to be removed from the PCI subsystem properly.
8332 **/
8333static void __devexit
8334lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8335{
8336 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8337 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8338 struct lpfc_vport **vports;
8339 struct lpfc_hba *phba = vport->phba;
8340 int i;
8341
8342 /* Mark the device unloading flag */
8343 spin_lock_irq(&phba->hbalock);
8344 vport->load_flag |= FC_UNLOADING;
8345 spin_unlock_irq(&phba->hbalock);
8346
8347 /* Free the HBA sysfs attributes */
8348 lpfc_free_sysfs_attr(vport);
8349
8350 /* Release all the vports against this physical port */
8351 vports = lpfc_create_vport_work_array(phba);
8352 if (vports != NULL)
8353 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8354 fc_vport_terminate(vports[i]->fc_vport);
8355 lpfc_destroy_vport_work_array(phba, vports);
8356
8357 /* Remove FC host and then SCSI host with the physical port */
8358 fc_remove_host(shost);
8359 scsi_remove_host(shost);
8360
8361 /* Perform cleanup on the physical port */
8362 lpfc_cleanup(vport);
8363
8364 /*
8365 * Bring down the SLI Layer. This step disables all interrupts,
8366 * clears the rings, discards all mailbox commands, and resets
8367 * the HBA FCoE function.
8368 */
8369 lpfc_debugfs_terminate(vport);
8370 lpfc_sli4_hba_unset(phba);
8371
8372 spin_lock_irq(&phba->hbalock);
8373 list_del_init(&vport->listentry);
8374 spin_unlock_irq(&phba->hbalock);
8375
8376 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
8377 * buffers are released to their corresponding pools here.
8378 */
8379 lpfc_scsi_free(phba);
8380 lpfc_sli4_driver_resource_unset(phba);
8381
8382 /* Unmap adapter Control and Doorbell registers */
8383 lpfc_sli4_pci_mem_unset(phba);
8384
8385 /* Release PCI resources and disable device's PCI function */
8386 scsi_host_put(shost);
8387 lpfc_disable_pci_dev(phba);
8388
8389 /* Finally, free the driver's device data structure */
8390 lpfc_hba_free(phba);
8391
8392 return;
8393}
8394
8395/**
8396 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
8397 * @pdev: pointer to PCI device
8398 * @msg: power management message
8399 *
8400 * This routine is called from the kernel's PCI subsystem to support system
8401 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8402 * this method, it quiesces the device by stopping the driver's worker
8403 * thread for the device, turning off device's interrupt and DMA, and bring
8404 * the device offline. Note that as the driver implements the minimum PM
8405 * requirements to a power-aware driver's PM support for suspend/resume -- all
8406 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8407 * method call will be treated as SUSPEND and the driver will fully
8408 * reinitialize its device during resume() method call, the driver will set
8409 * device to PCI_D3hot state in PCI config space instead of setting it
8410 * according to the @msg provided by the PM.
8411 *
8412 * Return code
8413 * 0 - driver suspended the device
8414 * Error otherwise
8415 **/
8416static int
8417lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
8418{
8419 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8420 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8421
8422 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04008423 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04008424
8425 /* Bring down the device */
8426 lpfc_offline_prep(phba);
8427 lpfc_offline(phba);
8428 kthread_stop(phba->worker_thread);
8429
8430 /* Disable interrupt from device */
8431 lpfc_sli4_disable_intr(phba);
8432
8433 /* Save device state to PCI config space */
8434 pci_save_state(pdev);
8435 pci_set_power_state(pdev, PCI_D3hot);
8436
8437 return 0;
8438}
8439
8440/**
8441 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8442 * @pdev: pointer to PCI device
8443 *
8444 * This routine is called from the kernel's PCI subsystem to support system
8445 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8446 * this method, it restores the device's PCI config space state and fully
8447 * reinitializes the device and brings it online. Note that as the driver
8448 * implements the minimum PM requirements to a power-aware driver's PM for
8449 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8450 * to the suspend() method call will be treated as SUSPEND and the driver
8451 * will fully reinitialize its device during resume() method call, the device
8452 * will be set to PCI_D0 directly in PCI config space before restoring the
8453 * state.
8454 *
8455 * Return code
8456 * 0 - driver suspended the device
8457 * Error otherwise
8458 **/
8459static int
8460lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8461{
8462 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8463 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8464 uint32_t intr_mode;
8465 int error;
8466
8467 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8468 "0292 PCI device Power Management resume.\n");
8469
8470 /* Restore device state from PCI config space */
8471 pci_set_power_state(pdev, PCI_D0);
8472 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008473
8474 /*
8475 * As the new kernel behavior of pci_restore_state() API call clears
8476 * device saved_state flag, need to save the restored state again.
8477 */
8478 pci_save_state(pdev);
8479
James Smartda0436e2009-05-22 14:51:39 -04008480 if (pdev->is_busmaster)
8481 pci_set_master(pdev);
8482
8483 /* Startup the kernel thread for this host adapter. */
8484 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8485 "lpfc_worker_%d", phba->brd_no);
8486 if (IS_ERR(phba->worker_thread)) {
8487 error = PTR_ERR(phba->worker_thread);
8488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8489 "0293 PM resume failed to start worker "
8490 "thread: error=x%x.\n", error);
8491 return error;
8492 }
8493
8494 /* Configure and enable interrupt */
8495 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8496 if (intr_mode == LPFC_INTR_ERROR) {
8497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8498 "0294 PM resume Failed to enable interrupt\n");
8499 return -EIO;
8500 } else
8501 phba->intr_mode = intr_mode;
8502
8503 /* Restart HBA and bring it online */
8504 lpfc_sli_brdrestart(phba);
8505 lpfc_online(phba);
8506
8507 /* Log the current active interrupt mode */
8508 lpfc_log_intr_mode(phba, phba->intr_mode);
8509
8510 return 0;
8511}
8512
8513/**
James Smart75baf692010-06-08 18:31:21 -04008514 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
8515 * @phba: pointer to lpfc hba data structure.
8516 *
8517 * This routine is called to prepare the SLI4 device for PCI slot recover. It
8518 * aborts all the outstanding SCSI I/Os to the pci device.
8519 **/
8520static void
8521lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
8522{
8523 struct lpfc_sli *psli = &phba->sli;
8524 struct lpfc_sli_ring *pring;
8525
8526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8527 "2828 PCI channel I/O abort preparing for recovery\n");
8528 /*
8529 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8530 * and let the SCSI mid-layer to retry them to recover.
8531 */
8532 pring = &psli->ring[psli->fcp_ring];
8533 lpfc_sli_abort_iocb_ring(phba, pring);
8534}
8535
8536/**
8537 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
8538 * @phba: pointer to lpfc hba data structure.
8539 *
8540 * This routine is called to prepare the SLI4 device for PCI slot reset. It
8541 * disables the device interrupt and pci device, and aborts the internal FCP
8542 * pending I/Os.
8543 **/
8544static void
8545lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
8546{
8547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8548 "2826 PCI channel disable preparing for reset\n");
8549
8550 /* Block any management I/Os to the device */
8551 lpfc_block_mgmt_io(phba);
8552
8553 /* Block all SCSI devices' I/Os on the host */
8554 lpfc_scsi_dev_block(phba);
8555
8556 /* stop all timers */
8557 lpfc_stop_hba_timers(phba);
8558
8559 /* Disable interrupt and pci device */
8560 lpfc_sli4_disable_intr(phba);
8561 pci_disable_device(phba->pcidev);
8562
8563 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8564 lpfc_sli_flush_fcp_rings(phba);
8565}
8566
8567/**
8568 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
8569 * @phba: pointer to lpfc hba data structure.
8570 *
8571 * This routine is called to prepare the SLI4 device for PCI slot permanently
8572 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8573 * pending I/Os.
8574 **/
8575static void
8576lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
8577{
8578 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8579 "2827 PCI channel permanent disable for failure\n");
8580
8581 /* Block all SCSI devices' I/Os on the host */
8582 lpfc_scsi_dev_block(phba);
8583
8584 /* stop all timers */
8585 lpfc_stop_hba_timers(phba);
8586
8587 /* Clean up all driver's outstanding SCSI I/Os */
8588 lpfc_sli_flush_fcp_rings(phba);
8589}
8590
8591/**
James Smartda0436e2009-05-22 14:51:39 -04008592 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
8593 * @pdev: pointer to PCI device.
8594 * @state: the current PCI connection state.
8595 *
8596 * This routine is called from the PCI subsystem for error handling to device
8597 * with SLI-4 interface spec. This function is called by the PCI subsystem
8598 * after a PCI bus error affecting this device has been detected. When this
8599 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8600 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8601 * for the PCI subsystem to perform proper recovery as desired.
8602 *
8603 * Return codes
8604 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8605 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8606 **/
8607static pci_ers_result_t
8608lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8609{
James Smart75baf692010-06-08 18:31:21 -04008610 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8611 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8612
8613 switch (state) {
8614 case pci_channel_io_normal:
8615 /* Non-fatal error, prepare for recovery */
8616 lpfc_sli4_prep_dev_for_recover(phba);
8617 return PCI_ERS_RESULT_CAN_RECOVER;
8618 case pci_channel_io_frozen:
8619 /* Fatal error, prepare for slot reset */
8620 lpfc_sli4_prep_dev_for_reset(phba);
8621 return PCI_ERS_RESULT_NEED_RESET;
8622 case pci_channel_io_perm_failure:
8623 /* Permanent failure, prepare for device down */
8624 lpfc_sli4_prep_dev_for_perm_failure(phba);
8625 return PCI_ERS_RESULT_DISCONNECT;
8626 default:
8627 /* Unknown state, prepare and request slot reset */
8628 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8629 "2825 Unknown PCI error state: x%x\n", state);
8630 lpfc_sli4_prep_dev_for_reset(phba);
8631 return PCI_ERS_RESULT_NEED_RESET;
8632 }
James Smartda0436e2009-05-22 14:51:39 -04008633}
8634
8635/**
8636 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
8637 * @pdev: pointer to PCI device.
8638 *
8639 * This routine is called from the PCI subsystem for error handling to device
8640 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8641 * restart the PCI card from scratch, as if from a cold-boot. During the
8642 * PCI subsystem error recovery, after the driver returns
8643 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8644 * recovery and then call this routine before calling the .resume method to
8645 * recover the device. This function will initialize the HBA device, enable
8646 * the interrupt, but it will just put the HBA to offline state without
8647 * passing any I/O traffic.
8648 *
8649 * Return codes
8650 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8651 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8652 */
8653static pci_ers_result_t
8654lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8655{
James Smart75baf692010-06-08 18:31:21 -04008656 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8657 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8658 struct lpfc_sli *psli = &phba->sli;
8659 uint32_t intr_mode;
8660
8661 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
8662 if (pci_enable_device_mem(pdev)) {
8663 printk(KERN_ERR "lpfc: Cannot re-enable "
8664 "PCI device after reset.\n");
8665 return PCI_ERS_RESULT_DISCONNECT;
8666 }
8667
8668 pci_restore_state(pdev);
8669 if (pdev->is_busmaster)
8670 pci_set_master(pdev);
8671
8672 spin_lock_irq(&phba->hbalock);
8673 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
8674 spin_unlock_irq(&phba->hbalock);
8675
8676 /* Configure and enable interrupt */
8677 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8678 if (intr_mode == LPFC_INTR_ERROR) {
8679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8680 "2824 Cannot re-enable interrupt after "
8681 "slot reset.\n");
8682 return PCI_ERS_RESULT_DISCONNECT;
8683 } else
8684 phba->intr_mode = intr_mode;
8685
8686 /* Log the current active interrupt mode */
8687 lpfc_log_intr_mode(phba, phba->intr_mode);
8688
James Smartda0436e2009-05-22 14:51:39 -04008689 return PCI_ERS_RESULT_RECOVERED;
8690}
8691
8692/**
8693 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8694 * @pdev: pointer to PCI device
8695 *
8696 * This routine is called from the PCI subsystem for error handling to device
8697 * with SLI-4 interface spec. It is called when kernel error recovery tells
8698 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8699 * error recovery. After this call, traffic can start to flow from this device
8700 * again.
8701 **/
8702static void
8703lpfc_io_resume_s4(struct pci_dev *pdev)
8704{
James Smart75baf692010-06-08 18:31:21 -04008705 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8706 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8707
8708 /*
8709 * In case of slot reset, as function reset is performed through
8710 * mailbox command which needs DMA to be enabled, this operation
8711 * has to be moved to the io resume phase. Taking device offline
8712 * will perform the necessary cleanup.
8713 */
8714 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
8715 /* Perform device reset */
8716 lpfc_offline_prep(phba);
8717 lpfc_offline(phba);
8718 lpfc_sli_brdrestart(phba);
8719 /* Bring the device back online */
8720 lpfc_online(phba);
8721 }
8722
8723 /* Clean up Advanced Error Reporting (AER) if needed */
8724 if (phba->hba_flag & HBA_AER_ENABLED)
8725 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -04008726}
8727
8728/**
James Smart3772a992009-05-22 14:50:54 -04008729 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8730 * @pdev: pointer to PCI device
8731 * @pid: pointer to PCI device identifier
8732 *
8733 * This routine is to be registered to the kernel's PCI subsystem. When an
8734 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8735 * at PCI device-specific information of the device and driver to see if the
8736 * driver state that it can support this kind of device. If the match is
8737 * successful, the driver core invokes this routine. This routine dispatches
8738 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8739 * do all the initialization that it needs to do to handle the HBA device
8740 * properly.
8741 *
8742 * Return code
8743 * 0 - driver can claim the device
8744 * negative value - driver can not claim the device
8745 **/
8746static int __devinit
8747lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8748{
8749 int rc;
James Smart8fa38512009-07-19 10:01:03 -04008750 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04008751
James Smart28baac72010-02-12 14:42:03 -05008752 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04008753 return -ENODEV;
8754
James Smart8fa38512009-07-19 10:01:03 -04008755 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05008756 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04008757 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008758 else
James Smart3772a992009-05-22 14:50:54 -04008759 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008760
James Smart3772a992009-05-22 14:50:54 -04008761 return rc;
8762}
8763
8764/**
8765 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8766 * @pdev: pointer to PCI device
8767 *
8768 * This routine is to be registered to the kernel's PCI subsystem. When an
8769 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8770 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8771 * remove routine, which will perform all the necessary cleanup for the
8772 * device to be removed from the PCI subsystem properly.
8773 **/
8774static void __devexit
8775lpfc_pci_remove_one(struct pci_dev *pdev)
8776{
8777 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8778 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8779
8780 switch (phba->pci_dev_grp) {
8781 case LPFC_PCI_DEV_LP:
8782 lpfc_pci_remove_one_s3(pdev);
8783 break;
James Smartda0436e2009-05-22 14:51:39 -04008784 case LPFC_PCI_DEV_OC:
8785 lpfc_pci_remove_one_s4(pdev);
8786 break;
James Smart3772a992009-05-22 14:50:54 -04008787 default:
8788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8789 "1424 Invalid PCI device group: 0x%x\n",
8790 phba->pci_dev_grp);
8791 break;
8792 }
8793 return;
8794}
8795
8796/**
8797 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8798 * @pdev: pointer to PCI device
8799 * @msg: power management message
8800 *
8801 * This routine is to be registered to the kernel's PCI subsystem to support
8802 * system Power Management (PM). When PM invokes this method, it dispatches
8803 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8804 * suspend the device.
8805 *
8806 * Return code
8807 * 0 - driver suspended the device
8808 * Error otherwise
8809 **/
8810static int
8811lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8812{
8813 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8814 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8815 int rc = -ENODEV;
8816
8817 switch (phba->pci_dev_grp) {
8818 case LPFC_PCI_DEV_LP:
8819 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8820 break;
James Smartda0436e2009-05-22 14:51:39 -04008821 case LPFC_PCI_DEV_OC:
8822 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8823 break;
James Smart3772a992009-05-22 14:50:54 -04008824 default:
8825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8826 "1425 Invalid PCI device group: 0x%x\n",
8827 phba->pci_dev_grp);
8828 break;
8829 }
8830 return rc;
8831}
8832
8833/**
8834 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8835 * @pdev: pointer to PCI device
8836 *
8837 * This routine is to be registered to the kernel's PCI subsystem to support
8838 * system Power Management (PM). When PM invokes this method, it dispatches
8839 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8840 * resume the device.
8841 *
8842 * Return code
8843 * 0 - driver suspended the device
8844 * Error otherwise
8845 **/
8846static int
8847lpfc_pci_resume_one(struct pci_dev *pdev)
8848{
8849 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8850 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8851 int rc = -ENODEV;
8852
8853 switch (phba->pci_dev_grp) {
8854 case LPFC_PCI_DEV_LP:
8855 rc = lpfc_pci_resume_one_s3(pdev);
8856 break;
James Smartda0436e2009-05-22 14:51:39 -04008857 case LPFC_PCI_DEV_OC:
8858 rc = lpfc_pci_resume_one_s4(pdev);
8859 break;
James Smart3772a992009-05-22 14:50:54 -04008860 default:
8861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8862 "1426 Invalid PCI device group: 0x%x\n",
8863 phba->pci_dev_grp);
8864 break;
8865 }
8866 return rc;
8867}
8868
8869/**
8870 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8871 * @pdev: pointer to PCI device.
8872 * @state: the current PCI connection state.
8873 *
8874 * This routine is registered to the PCI subsystem for error handling. This
8875 * function is called by the PCI subsystem after a PCI bus error affecting
8876 * this device has been detected. When this routine is invoked, it dispatches
8877 * the action to the proper SLI-3 or SLI-4 device error detected handling
8878 * routine, which will perform the proper error detected operation.
8879 *
8880 * Return codes
8881 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8882 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8883 **/
8884static pci_ers_result_t
8885lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8886{
8887 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8888 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8889 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8890
8891 switch (phba->pci_dev_grp) {
8892 case LPFC_PCI_DEV_LP:
8893 rc = lpfc_io_error_detected_s3(pdev, state);
8894 break;
James Smartda0436e2009-05-22 14:51:39 -04008895 case LPFC_PCI_DEV_OC:
8896 rc = lpfc_io_error_detected_s4(pdev, state);
8897 break;
James Smart3772a992009-05-22 14:50:54 -04008898 default:
8899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8900 "1427 Invalid PCI device group: 0x%x\n",
8901 phba->pci_dev_grp);
8902 break;
8903 }
8904 return rc;
8905}
8906
8907/**
8908 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8909 * @pdev: pointer to PCI device.
8910 *
8911 * This routine is registered to the PCI subsystem for error handling. This
8912 * function is called after PCI bus has been reset to restart the PCI card
8913 * from scratch, as if from a cold-boot. When this routine is invoked, it
8914 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8915 * routine, which will perform the proper device reset.
8916 *
8917 * Return codes
8918 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8919 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8920 **/
8921static pci_ers_result_t
8922lpfc_io_slot_reset(struct pci_dev *pdev)
8923{
8924 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8925 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8926 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8927
8928 switch (phba->pci_dev_grp) {
8929 case LPFC_PCI_DEV_LP:
8930 rc = lpfc_io_slot_reset_s3(pdev);
8931 break;
James Smartda0436e2009-05-22 14:51:39 -04008932 case LPFC_PCI_DEV_OC:
8933 rc = lpfc_io_slot_reset_s4(pdev);
8934 break;
James Smart3772a992009-05-22 14:50:54 -04008935 default:
8936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8937 "1428 Invalid PCI device group: 0x%x\n",
8938 phba->pci_dev_grp);
8939 break;
8940 }
8941 return rc;
8942}
8943
8944/**
8945 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8946 * @pdev: pointer to PCI device
8947 *
8948 * This routine is registered to the PCI subsystem for error handling. It
8949 * is called when kernel error recovery tells the lpfc driver that it is
8950 * OK to resume normal PCI operation after PCI bus error recovery. When
8951 * this routine is invoked, it dispatches the action to the proper SLI-3
8952 * or SLI-4 device io_resume routine, which will resume the device operation.
8953 **/
8954static void
8955lpfc_io_resume(struct pci_dev *pdev)
8956{
8957 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8958 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8959
8960 switch (phba->pci_dev_grp) {
8961 case LPFC_PCI_DEV_LP:
8962 lpfc_io_resume_s3(pdev);
8963 break;
James Smartda0436e2009-05-22 14:51:39 -04008964 case LPFC_PCI_DEV_OC:
8965 lpfc_io_resume_s4(pdev);
8966 break;
James Smart3772a992009-05-22 14:50:54 -04008967 default:
8968 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8969 "1429 Invalid PCI device group: 0x%x\n",
8970 phba->pci_dev_grp);
8971 break;
8972 }
8973 return;
8974}
8975
dea31012005-04-17 16:05:31 -05008976static struct pci_device_id lpfc_id_table[] = {
8977 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8978 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008979 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8980 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008981 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8982 PCI_ANY_ID, PCI_ANY_ID, },
8983 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8984 PCI_ANY_ID, PCI_ANY_ID, },
8985 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8986 PCI_ANY_ID, PCI_ANY_ID, },
8987 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8988 PCI_ANY_ID, PCI_ANY_ID, },
8989 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8990 PCI_ANY_ID, PCI_ANY_ID, },
8991 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8992 PCI_ANY_ID, PCI_ANY_ID, },
8993 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8994 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008995 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8996 PCI_ANY_ID, PCI_ANY_ID, },
8997 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8998 PCI_ANY_ID, PCI_ANY_ID, },
8999 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
9000 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009001 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
9002 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009003 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
9004 PCI_ANY_ID, PCI_ANY_ID, },
9005 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
9006 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009007 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
9008 PCI_ANY_ID, PCI_ANY_ID, },
9009 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
9010 PCI_ANY_ID, PCI_ANY_ID, },
9011 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
9012 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04009013 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
9014 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009015 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
9016 PCI_ANY_ID, PCI_ANY_ID, },
9017 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
9018 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009019 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
9020 PCI_ANY_ID, PCI_ANY_ID, },
9021 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
9022 PCI_ANY_ID, PCI_ANY_ID, },
9023 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
9024 PCI_ANY_ID, PCI_ANY_ID, },
9025 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
9026 PCI_ANY_ID, PCI_ANY_ID, },
9027 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
9028 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009029 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
9030 PCI_ANY_ID, PCI_ANY_ID, },
9031 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
9032 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04009033 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
9034 PCI_ANY_ID, PCI_ANY_ID, },
9035 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
9036 PCI_ANY_ID, PCI_ANY_ID, },
9037 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
9038 PCI_ANY_ID, PCI_ANY_ID, },
9039 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
9040 PCI_ANY_ID, PCI_ANY_ID, },
9041 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
9042 PCI_ANY_ID, PCI_ANY_ID, },
9043 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
9044 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04009045 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
9046 PCI_ANY_ID, PCI_ANY_ID, },
9047 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
9048 PCI_ANY_ID, PCI_ANY_ID, },
9049 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
9050 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04009051 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
9052 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05009053 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
9054 PCI_ANY_ID, PCI_ANY_ID, },
9055 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04009056 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -04009057 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
9058 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009059 { 0 }
9060};
9061
9062MODULE_DEVICE_TABLE(pci, lpfc_id_table);
9063
Linas Vepstas8d63f372007-02-14 14:28:36 -06009064static struct pci_error_handlers lpfc_err_handler = {
9065 .error_detected = lpfc_io_error_detected,
9066 .slot_reset = lpfc_io_slot_reset,
9067 .resume = lpfc_io_resume,
9068};
9069
dea31012005-04-17 16:05:31 -05009070static struct pci_driver lpfc_driver = {
9071 .name = LPFC_DRIVER_NAME,
9072 .id_table = lpfc_id_table,
9073 .probe = lpfc_pci_probe_one,
9074 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05009075 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04009076 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05009077 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05009078};
9079
James Smarte59058c2008-08-24 21:49:00 -04009080/**
James Smart3621a712009-04-06 18:47:14 -04009081 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04009082 *
9083 * This routine is to be invoked when the lpfc module is loaded into the
9084 * kernel. The special kernel macro module_init() is used to indicate the
9085 * role of this routine to the kernel as lpfc module entry point.
9086 *
9087 * Return codes
9088 * 0 - successful
9089 * -ENOMEM - FC attach transport failed
9090 * all others - failed
9091 */
dea31012005-04-17 16:05:31 -05009092static int __init
9093lpfc_init(void)
9094{
9095 int error = 0;
9096
9097 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009098 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05009099
James Smart7ee5d432007-10-27 13:37:17 -04009100 if (lpfc_enable_npiv) {
9101 lpfc_transport_functions.vport_create = lpfc_vport_create;
9102 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
9103 }
dea31012005-04-17 16:05:31 -05009104 lpfc_transport_template =
9105 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04009106 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05009107 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04009108 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04009109 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04009110 fc_attach_transport(&lpfc_vport_transport_functions);
9111 if (lpfc_vport_transport_template == NULL) {
9112 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04009113 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04009114 }
James Smart7ee5d432007-10-27 13:37:17 -04009115 }
dea31012005-04-17 16:05:31 -05009116 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05009117 if (error) {
dea31012005-04-17 16:05:31 -05009118 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04009119 if (lpfc_enable_npiv)
9120 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05009121 }
dea31012005-04-17 16:05:31 -05009122
9123 return error;
9124}
9125
James Smarte59058c2008-08-24 21:49:00 -04009126/**
James Smart3621a712009-04-06 18:47:14 -04009127 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04009128 *
9129 * This routine is invoked when the lpfc module is removed from the kernel.
9130 * The special kernel macro module_exit() is used to indicate the role of
9131 * this routine to the kernel as lpfc module exit point.
9132 */
dea31012005-04-17 16:05:31 -05009133static void __exit
9134lpfc_exit(void)
9135{
9136 pci_unregister_driver(&lpfc_driver);
9137 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04009138 if (lpfc_enable_npiv)
9139 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05009140 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04009141 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
9142 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05009143 (1L << _dump_buf_data_order), _dump_buf_data);
9144 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
9145 }
9146
9147 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04009148 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
9149 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05009150 (1L << _dump_buf_dif_order), _dump_buf_dif);
9151 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
9152 }
dea31012005-04-17 16:05:31 -05009153}
9154
9155module_init(lpfc_init);
9156module_exit(lpfc_exit);
9157MODULE_LICENSE("GPL");
9158MODULE_DESCRIPTION(LPFC_MODULE_DESC);
9159MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
9160MODULE_VERSION("0:" LPFC_DRIVER_VERSION);