blob: 0b5f76caa6e452f0117ea6ca935234db3d04dee3 [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
James Smart76a95d72010-11-20 23:11:48 -0500449 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_16G)
450 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500451 && !(phba->lmt & LMT_1Gb))
James Smart76a95d72010-11-20 23:11:48 -0500452 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500453 && !(phba->lmt & LMT_2Gb))
James Smart76a95d72010-11-20 23:11:48 -0500454 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500455 && !(phba->lmt & LMT_4Gb))
James Smart76a95d72010-11-20 23:11:48 -0500456 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500457 && !(phba->lmt & LMT_8Gb))
James Smart76a95d72010-11-20 23:11:48 -0500458 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G)
459 && !(phba->lmt & LMT_10Gb))
460 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G)
461 && !(phba->lmt & LMT_16Gb))) {
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500462 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500463 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400464 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500465 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500466 phba->cfg_link_speed);
James Smart76a95d72010-11-20 23:11:48 -0500467 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
dea31012005-04-17 16:05:31 -0500468 }
469
James Smart2e0fef82007-06-17 19:56:36 -0500470 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500471
James Smart0b727fe2007-10-27 13:37:25 -0400472 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500473 if (psli->ring[psli->extra_ring].cmdringaddr)
474 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500475 if (psli->ring[psli->fcp_ring].cmdringaddr)
476 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
477 if (psli->ring[psli->next_ring].cmdringaddr)
478 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
479
480 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500481 if (phba->sli_rev != 3)
482 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500483
James Smart93996272008-08-24 21:50:30 -0400484 /*
485 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
486 */
487 if (phba->intr_type == MSIX) {
488 rc = lpfc_config_msi(phba, pmb);
489 if (rc) {
490 mempool_free(pmb, phba->mbox_mem_pool);
491 return -EIO;
492 }
493 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
494 if (rc != MBX_SUCCESS) {
495 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
496 "0352 Config MSI mailbox command "
497 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400498 pmb->u.mb.mbxCommand,
499 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400500 mempool_free(pmb, phba->mbox_mem_pool);
501 return -EIO;
502 }
503 }
504
James Smart04c68492009-05-22 14:52:52 -0400505 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400506 /* Initialize ERATT handling flag */
507 phba->hba_flag &= ~HBA_ERATT_HANDLED;
508
dea31012005-04-17 16:05:31 -0500509 /* Enable appropriate host interrupts */
dea31012005-04-17 16:05:31 -0500510 status = readl(phba->HCregaddr);
511 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
512 if (psli->num_rings > 0)
513 status |= HC_R0INT_ENA;
514 if (psli->num_rings > 1)
515 status |= HC_R1INT_ENA;
516 if (psli->num_rings > 2)
517 status |= HC_R2INT_ENA;
518 if (psli->num_rings > 3)
519 status |= HC_R3INT_ENA;
520
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500521 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
522 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400523 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500524
dea31012005-04-17 16:05:31 -0500525 writel(status, phba->HCregaddr);
526 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500527 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500528
James Smart93996272008-08-24 21:50:30 -0400529 /* Set up ring-0 (ELS) timer */
530 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500531 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400532 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500533 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
534 phba->hb_outstanding = 0;
535 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400536 /* Set up error attention (ERATT) polling timer */
537 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500538
James Smarta0c87cb2009-07-19 10:01:10 -0400539 if (phba->hba_flag & LINK_DISABLED) {
540 lpfc_printf_log(phba,
541 KERN_ERR, LOG_INIT,
542 "2598 Adapter Link is disabled.\n");
543 lpfc_down_link(phba, pmb);
544 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
545 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
546 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
547 lpfc_printf_log(phba,
548 KERN_ERR, LOG_INIT,
549 "2599 Adapter failed to issue DOWN_LINK"
550 " mbox command rc 0x%x\n", rc);
551
552 mempool_free(pmb, phba->mbox_mem_pool);
553 return -EIO;
554 }
James Smarte40a02c2010-02-26 14:13:54 -0500555 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smarta0c87cb2009-07-19 10:01:10 -0400556 lpfc_init_link(phba, pmb, phba->cfg_topology,
557 phba->cfg_link_speed);
558 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
559 lpfc_set_loopback_flag(phba);
560 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
561 if (rc != MBX_SUCCESS) {
562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400563 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500564 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500565 mb->mbxCommand, mb->mbxStatus);
566
James Smarta0c87cb2009-07-19 10:01:10 -0400567 /* Clear all interrupt enable conditions */
568 writel(0, phba->HCregaddr);
569 readl(phba->HCregaddr); /* flush */
570 /* Clear all pending interrupts */
571 writel(0xffffffff, phba->HAregaddr);
572 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500573
James Smarta0c87cb2009-07-19 10:01:10 -0400574 phba->link_state = LPFC_HBA_ERROR;
575 if (rc != MBX_BUSY)
576 mempool_free(pmb, phba->mbox_mem_pool);
577 return -EIO;
578 }
dea31012005-04-17 16:05:31 -0500579 }
580 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400581 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500582 if (!pmb) {
583 phba->link_state = LPFC_HBA_ERROR;
584 return -ENOMEM;
585 }
586
James Smart57127f12007-10-27 13:37:05 -0400587 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
588 pmb->mbox_cmpl = lpfc_config_async_cmpl;
589 pmb->vport = phba->pport;
590 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500591
James Smart57127f12007-10-27 13:37:05 -0400592 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
593 lpfc_printf_log(phba,
594 KERN_ERR,
595 LOG_INIT,
596 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400597 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400598 rc);
599 mempool_free(pmb, phba->mbox_mem_pool);
600 }
James Smart97207482008-12-04 22:39:19 -0500601
602 /* Get Option rom version */
603 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500604 if (!pmb) {
605 phba->link_state = LPFC_HBA_ERROR;
606 return -ENOMEM;
607 }
608
James Smart97207482008-12-04 22:39:19 -0500609 lpfc_dump_wakeup_param(phba, pmb);
610 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
611 pmb->vport = phba->pport;
612 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
613
614 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400616 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500617 mempool_free(pmb, phba->mbox_mem_pool);
618 }
619
James Smartd7c255b2008-08-24 21:50:00 -0400620 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400621}
622
James Smarte59058c2008-08-24 21:49:00 -0400623/**
James Smart84d1b002010-02-12 14:42:33 -0500624 * lpfc_hba_init_link - Initialize the FC link
625 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400626 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500627 *
628 * This routine will issue the INIT_LINK mailbox command call.
629 * It is available to other drivers through the lpfc_hba data
630 * structure for use as a delayed link up mechanism with the
631 * module parameter lpfc_suppress_link_up.
632 *
633 * Return code
634 * 0 - success
635 * Any other value - error
636 **/
637int
James Smart6e7288d2010-06-07 15:23:35 -0400638lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500639{
640 struct lpfc_vport *vport = phba->pport;
641 LPFC_MBOXQ_t *pmb;
642 MAILBOX_t *mb;
643 int rc;
644
645 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
646 if (!pmb) {
647 phba->link_state = LPFC_HBA_ERROR;
648 return -ENOMEM;
649 }
650 mb = &pmb->u.mb;
651 pmb->vport = vport;
652
James Smart76a95d72010-11-20 23:11:48 -0500653 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500654 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
655 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400656 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500657 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
659 "0498 Adapter failed to init, mbxCmd x%x "
660 "INIT_LINK, mbxStatus x%x\n",
661 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500662 if (phba->sli_rev <= LPFC_SLI_REV3) {
663 /* Clear all interrupt enable conditions */
664 writel(0, phba->HCregaddr);
665 readl(phba->HCregaddr); /* flush */
666 /* Clear all pending interrupts */
667 writel(0xffffffff, phba->HAregaddr);
668 readl(phba->HAregaddr); /* flush */
669 }
James Smart84d1b002010-02-12 14:42:33 -0500670 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400671 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500672 mempool_free(pmb, phba->mbox_mem_pool);
673 return -EIO;
674 }
James Smarte40a02c2010-02-26 14:13:54 -0500675 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400676 if (flag == MBX_POLL)
677 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500678
679 return 0;
680}
681
682/**
683 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400684 * @phba: pointer to lpfc hba data structure.
685 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500686 *
687 * This routine will issue the DOWN_LINK mailbox command call.
688 * It is available to other drivers through the lpfc_hba data
689 * structure for use to stop the link.
690 *
691 * Return code
692 * 0 - success
693 * Any other value - error
694 **/
695int
James Smart6e7288d2010-06-07 15:23:35 -0400696lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500697{
698 LPFC_MBOXQ_t *pmb;
699 int rc;
700
701 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
702 if (!pmb) {
703 phba->link_state = LPFC_HBA_ERROR;
704 return -ENOMEM;
705 }
706
707 lpfc_printf_log(phba,
708 KERN_ERR, LOG_INIT,
709 "0491 Adapter Link is disabled.\n");
710 lpfc_down_link(phba, pmb);
711 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400712 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500713 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
714 lpfc_printf_log(phba,
715 KERN_ERR, LOG_INIT,
716 "2522 Adapter failed to issue DOWN_LINK"
717 " mbox command rc 0x%x\n", rc);
718
719 mempool_free(pmb, phba->mbox_mem_pool);
720 return -EIO;
721 }
James Smart6e7288d2010-06-07 15:23:35 -0400722 if (flag == MBX_POLL)
723 mempool_free(pmb, phba->mbox_mem_pool);
724
James Smart84d1b002010-02-12 14:42:33 -0500725 return 0;
726}
727
728/**
James Smart3621a712009-04-06 18:47:14 -0400729 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400730 * @phba: pointer to lpfc HBA data structure.
731 *
732 * This routine will do LPFC uninitialization before the HBA is reset when
733 * bringing down the SLI Layer.
734 *
735 * Return codes
736 * 0 - success.
737 * Any other value - error.
738 **/
dea31012005-04-17 16:05:31 -0500739int
James Smart2e0fef82007-06-17 19:56:36 -0500740lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500741{
James Smart1b32f6a2008-02-08 18:49:39 -0500742 struct lpfc_vport **vports;
743 int i;
James Smart3772a992009-05-22 14:50:54 -0400744
745 if (phba->sli_rev <= LPFC_SLI_REV3) {
746 /* Disable interrupts */
747 writel(0, phba->HCregaddr);
748 readl(phba->HCregaddr); /* flush */
749 }
dea31012005-04-17 16:05:31 -0500750
James Smart1b32f6a2008-02-08 18:49:39 -0500751 if (phba->pport->load_flag & FC_UNLOADING)
752 lpfc_cleanup_discovery_resources(phba->pport);
753 else {
754 vports = lpfc_create_vport_work_array(phba);
755 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400756 for (i = 0; i <= phba->max_vports &&
757 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500758 lpfc_cleanup_discovery_resources(vports[i]);
759 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500760 }
761 return 0;
dea31012005-04-17 16:05:31 -0500762}
763
James Smarte59058c2008-08-24 21:49:00 -0400764/**
James Smart3772a992009-05-22 14:50:54 -0400765 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400766 * @phba: pointer to lpfc HBA data structure.
767 *
768 * This routine will do uninitialization after the HBA is reset when bring
769 * down the SLI Layer.
770 *
771 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200772 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400773 * Any other value - error.
774 **/
James Smart3772a992009-05-22 14:50:54 -0400775static int
776lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500777{
778 struct lpfc_sli *psli = &phba->sli;
779 struct lpfc_sli_ring *pring;
780 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500781 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500782 int i;
783
James Smart92d7f7b2007-06-17 19:56:38 -0500784 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
785 lpfc_sli_hbqbuf_free_all(phba);
786 else {
787 /* Cleanup preposted buffers on the ELS ring */
788 pring = &psli->ring[LPFC_ELS_RING];
789 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
790 list_del(&mp->list);
791 pring->postbufq_cnt--;
792 lpfc_mbuf_free(phba, mp->virt, mp->phys);
793 kfree(mp);
794 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500795 }
796
James Smart09372822008-01-11 01:52:54 -0500797 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500798 for (i = 0; i < psli->num_rings; i++) {
799 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500800
801 /* At this point in time the HBA is either reset or DOA. Either
802 * way, nothing should be on txcmplq as it will NEVER complete.
803 */
804 list_splice_init(&pring->txcmplq, &completions);
805 pring->txcmplq_cnt = 0;
806 spin_unlock_irq(&phba->hbalock);
807
James Smarta257bf92009-04-06 18:48:10 -0400808 /* Cancel all the IOCBs from the completions list */
809 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
810 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500811
Jamie Wellnitz41415862006-02-28 19:25:27 -0500812 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500813 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500814 }
James Smart09372822008-01-11 01:52:54 -0500815 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500816
817 return 0;
818}
James Smart5af5eee2010-10-22 11:06:38 -0400819
James Smartda0436e2009-05-22 14:51:39 -0400820/**
821 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
822 * @phba: pointer to lpfc HBA data structure.
823 *
824 * This routine will do uninitialization after the HBA is reset when bring
825 * down the SLI Layer.
826 *
827 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200828 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400829 * Any other value - error.
830 **/
831static int
832lpfc_hba_down_post_s4(struct lpfc_hba *phba)
833{
834 struct lpfc_scsi_buf *psb, *psb_next;
835 LIST_HEAD(aborts);
836 int ret;
837 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500838 struct lpfc_sglq *sglq_entry = NULL;
839
James Smartda0436e2009-05-22 14:51:39 -0400840 ret = lpfc_hba_down_post_s3(phba);
841 if (ret)
842 return ret;
843 /* At this point in time the HBA is either reset or DOA. Either
844 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
845 * on the lpfc_sgl_list so that it can either be freed if the
846 * driver is unloading or reposted if the driver is restarting
847 * the port.
848 */
849 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
850 /* scsl_buf_list */
851 /* abts_sgl_list_lock required because worker thread uses this
852 * list.
853 */
854 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500855 list_for_each_entry(sglq_entry,
856 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
857 sglq_entry->state = SGL_FREED;
858
James Smartda0436e2009-05-22 14:51:39 -0400859 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
860 &phba->sli4_hba.lpfc_sgl_list);
861 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
862 /* abts_scsi_buf_list_lock required because worker thread uses this
863 * list.
864 */
865 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
866 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
867 &aborts);
868 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
869 spin_unlock_irq(&phba->hbalock);
870
871 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
872 psb->pCmd = NULL;
873 psb->status = IOSTAT_SUCCESS;
874 }
875 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
876 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
877 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
878 return 0;
879}
880
881/**
882 * lpfc_hba_down_post - Wrapper func for hba down post routine
883 * @phba: pointer to lpfc HBA data structure.
884 *
885 * This routine wraps the actual SLI3 or SLI4 routine for performing
886 * uninitialization after the HBA is reset when bring down the SLI Layer.
887 *
888 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200889 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400890 * Any other value - error.
891 **/
892int
893lpfc_hba_down_post(struct lpfc_hba *phba)
894{
895 return (*phba->lpfc_hba_down_post)(phba);
896}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500897
James Smarte59058c2008-08-24 21:49:00 -0400898/**
James Smart3621a712009-04-06 18:47:14 -0400899 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400900 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
901 *
902 * This is the HBA-timer timeout handler registered to the lpfc driver. When
903 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
904 * work-port-events bitmap and the worker thread is notified. This timeout
905 * event will be used by the worker thread to invoke the actual timeout
906 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
907 * be performed in the timeout handler and the HBA timeout event bit shall
908 * be cleared by the worker thread after it has taken the event bitmap out.
909 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100910static void
James Smart858c9f62007-06-17 19:56:39 -0500911lpfc_hb_timeout(unsigned long ptr)
912{
913 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400914 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500915 unsigned long iflag;
916
917 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400918
919 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500920 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400921 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
922 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500923 phba->pport->work_port_events |= WORKER_HB_TMO;
924 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
925
James Smart93996272008-08-24 21:50:30 -0400926 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400927 if (!tmo_posted)
928 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500929 return;
930}
931
James Smarte59058c2008-08-24 21:49:00 -0400932/**
James Smart19ca7602010-11-20 23:11:55 -0500933 * lpfc_rrq_timeout - The RRQ-timer timeout handler
934 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
935 *
936 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
937 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
938 * work-port-events bitmap and the worker thread is notified. This timeout
939 * event will be used by the worker thread to invoke the actual timeout
940 * handler routine, lpfc_rrq_handler. Any periodical operations will
941 * be performed in the timeout handler and the RRQ timeout event bit shall
942 * be cleared by the worker thread after it has taken the event bitmap out.
943 **/
944static void
945lpfc_rrq_timeout(unsigned long ptr)
946{
947 struct lpfc_hba *phba;
948 uint32_t tmo_posted;
949 unsigned long iflag;
950
951 phba = (struct lpfc_hba *)ptr;
952 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
953 tmo_posted = phba->hba_flag & HBA_RRQ_ACTIVE;
954 if (!tmo_posted)
955 phba->hba_flag |= HBA_RRQ_ACTIVE;
956 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
957 if (!tmo_posted)
958 lpfc_worker_wake_up(phba);
959}
960
961/**
James Smart3621a712009-04-06 18:47:14 -0400962 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400963 * @phba: pointer to lpfc hba data structure.
964 * @pmboxq: pointer to the driver internal queue element for mailbox command.
965 *
966 * This is the callback function to the lpfc heart-beat mailbox command.
967 * If configured, the lpfc driver issues the heart-beat mailbox command to
968 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
969 * heart-beat mailbox command is issued, the driver shall set up heart-beat
970 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
971 * heart-beat outstanding state. Once the mailbox command comes back and
972 * no error conditions detected, the heart-beat mailbox command timer is
973 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
974 * state is cleared for the next heart-beat. If the timer expired with the
975 * heart-beat outstanding state set, the driver will put the HBA offline.
976 **/
James Smart858c9f62007-06-17 19:56:39 -0500977static void
978lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
979{
980 unsigned long drvr_flag;
981
982 spin_lock_irqsave(&phba->hbalock, drvr_flag);
983 phba->hb_outstanding = 0;
984 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
985
James Smart93996272008-08-24 21:50:30 -0400986 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500987 mempool_free(pmboxq, phba->mbox_mem_pool);
988 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
989 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400990 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500991 mod_timer(&phba->hb_tmofunc,
992 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
993 return;
994}
995
James Smarte59058c2008-08-24 21:49:00 -0400996/**
James Smart3621a712009-04-06 18:47:14 -0400997 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400998 * @phba: pointer to lpfc hba data structure.
999 *
1000 * This is the actual HBA-timer timeout handler to be invoked by the worker
1001 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1002 * handler performs any periodic operations needed for the device. If such
1003 * periodic event has already been attended to either in the interrupt handler
1004 * or by processing slow-ring or fast-ring events within the HBA-timer
1005 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1006 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1007 * is configured and there is no heart-beat mailbox command outstanding, a
1008 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1009 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1010 * to offline.
1011 **/
James Smart858c9f62007-06-17 19:56:39 -05001012void
1013lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1014{
James Smart45ed1192009-10-02 15:17:02 -04001015 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001016 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001017 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001018 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001019 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001020 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001021
James Smart45ed1192009-10-02 15:17:02 -04001022 vports = lpfc_create_vport_work_array(phba);
1023 if (vports != NULL)
1024 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1025 lpfc_rcv_seq_check_edtov(vports[i]);
1026 lpfc_destroy_vport_work_array(phba, vports);
1027
James Smart858c9f62007-06-17 19:56:39 -05001028 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001029 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001030 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1031 return;
1032
1033 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001034
1035 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1036 jiffies)) {
1037 spin_unlock_irq(&phba->pport->work_port_lock);
1038 if (!phba->hb_outstanding)
1039 mod_timer(&phba->hb_tmofunc,
1040 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1041 else
1042 mod_timer(&phba->hb_tmofunc,
1043 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1044 return;
1045 }
1046 spin_unlock_irq(&phba->pport->work_port_lock);
1047
James Smart0ff10d42008-01-11 01:52:36 -05001048 if (phba->elsbuf_cnt &&
1049 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1050 spin_lock_irq(&phba->hbalock);
1051 list_splice_init(&phba->elsbuf, &completions);
1052 phba->elsbuf_cnt = 0;
1053 phba->elsbuf_prev_cnt = 0;
1054 spin_unlock_irq(&phba->hbalock);
1055
1056 while (!list_empty(&completions)) {
1057 list_remove_head(&completions, buf_ptr,
1058 struct lpfc_dmabuf, list);
1059 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1060 kfree(buf_ptr);
1061 }
1062 }
1063 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1064
James Smart858c9f62007-06-17 19:56:39 -05001065 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001066 if (phba->cfg_enable_hba_heartbeat) {
1067 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001068 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1069 (list_empty(&psli->mboxq))) {
1070 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1071 GFP_KERNEL);
1072 if (!pmboxq) {
1073 mod_timer(&phba->hb_tmofunc,
1074 jiffies +
1075 HZ * LPFC_HB_MBOX_INTERVAL);
1076 return;
1077 }
James Smart13815c82008-01-11 01:52:48 -05001078
James Smartbc739052010-08-04 16:11:18 -04001079 lpfc_heart_beat(phba, pmboxq);
1080 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1081 pmboxq->vport = phba->pport;
1082 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1083 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001084
James Smartbc739052010-08-04 16:11:18 -04001085 if (retval != MBX_BUSY &&
1086 retval != MBX_SUCCESS) {
1087 mempool_free(pmboxq,
1088 phba->mbox_mem_pool);
1089 mod_timer(&phba->hb_tmofunc,
1090 jiffies +
1091 HZ * LPFC_HB_MBOX_INTERVAL);
1092 return;
1093 }
1094 phba->skipped_hb = 0;
1095 phba->hb_outstanding = 1;
1096 } else if (time_before_eq(phba->last_completion_time,
1097 phba->skipped_hb)) {
1098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1099 "2857 Last completion time not "
1100 " updated in %d ms\n",
1101 jiffies_to_msecs(jiffies
1102 - phba->last_completion_time));
1103 } else
1104 phba->skipped_hb = jiffies;
1105
James Smart858c9f62007-06-17 19:56:39 -05001106 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001107 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001108 return;
James Smart13815c82008-01-11 01:52:48 -05001109 } else {
1110 /*
1111 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001112 * we need to give the hb mailbox cmd a chance to
1113 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001114 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001115 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1116 "0459 Adapter heartbeat still out"
1117 "standing:last compl time was %d ms.\n",
1118 jiffies_to_msecs(jiffies
1119 - phba->last_completion_time));
1120 mod_timer(&phba->hb_tmofunc,
1121 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001122 }
James Smart858c9f62007-06-17 19:56:39 -05001123 }
1124}
1125
James Smarte59058c2008-08-24 21:49:00 -04001126/**
James Smart3621a712009-04-06 18:47:14 -04001127 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001128 * @phba: pointer to lpfc hba data structure.
1129 *
1130 * This routine is called to bring the HBA offline when HBA hardware error
1131 * other than Port Error 6 has been detected.
1132 **/
James Smart09372822008-01-11 01:52:54 -05001133static void
1134lpfc_offline_eratt(struct lpfc_hba *phba)
1135{
1136 struct lpfc_sli *psli = &phba->sli;
1137
1138 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001139 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001140 spin_unlock_irq(&phba->hbalock);
1141 lpfc_offline_prep(phba);
1142
1143 lpfc_offline(phba);
1144 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001145 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001146 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001147 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001148 lpfc_hba_down_post(phba);
1149 lpfc_sli_brdready(phba, HS_MBRDY);
1150 lpfc_unblock_mgmt_io(phba);
1151 phba->link_state = LPFC_HBA_ERROR;
1152 return;
1153}
1154
James Smarte59058c2008-08-24 21:49:00 -04001155/**
James Smartda0436e2009-05-22 14:51:39 -04001156 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1157 * @phba: pointer to lpfc hba data structure.
1158 *
1159 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1160 * other than Port Error 6 has been detected.
1161 **/
1162static void
1163lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1164{
1165 lpfc_offline_prep(phba);
1166 lpfc_offline(phba);
1167 lpfc_sli4_brdreset(phba);
1168 lpfc_hba_down_post(phba);
1169 lpfc_sli4_post_status_check(phba);
1170 lpfc_unblock_mgmt_io(phba);
1171 phba->link_state = LPFC_HBA_ERROR;
1172}
1173
1174/**
James Smarta257bf92009-04-06 18:48:10 -04001175 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1176 * @phba: pointer to lpfc hba data structure.
1177 *
1178 * This routine is invoked to handle the deferred HBA hardware error
1179 * conditions. This type of error is indicated by HBA by setting ER1
1180 * and another ER bit in the host status register. The driver will
1181 * wait until the ER1 bit clears before handling the error condition.
1182 **/
1183static void
1184lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1185{
1186 uint32_t old_host_status = phba->work_hs;
1187 struct lpfc_sli_ring *pring;
1188 struct lpfc_sli *psli = &phba->sli;
1189
James Smartf4b4c682009-05-22 14:53:12 -04001190 /* If the pci channel is offline, ignore possible errors,
1191 * since we cannot communicate with the pci card anyway.
1192 */
1193 if (pci_channel_offline(phba->pcidev)) {
1194 spin_lock_irq(&phba->hbalock);
1195 phba->hba_flag &= ~DEFER_ERATT;
1196 spin_unlock_irq(&phba->hbalock);
1197 return;
1198 }
1199
James Smarta257bf92009-04-06 18:48:10 -04001200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1201 "0479 Deferred Adapter Hardware Error "
1202 "Data: x%x x%x x%x\n",
1203 phba->work_hs,
1204 phba->work_status[0], phba->work_status[1]);
1205
1206 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001207 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001208 spin_unlock_irq(&phba->hbalock);
1209
1210
1211 /*
1212 * Firmware stops when it triggred erratt. That could cause the I/Os
1213 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1214 * SCSI layer retry it after re-establishing link.
1215 */
1216 pring = &psli->ring[psli->fcp_ring];
1217 lpfc_sli_abort_iocb_ring(phba, pring);
1218
1219 /*
1220 * There was a firmware error. Take the hba offline and then
1221 * attempt to restart it.
1222 */
1223 lpfc_offline_prep(phba);
1224 lpfc_offline(phba);
1225
1226 /* Wait for the ER1 bit to clear.*/
1227 while (phba->work_hs & HS_FFER1) {
1228 msleep(100);
1229 phba->work_hs = readl(phba->HSregaddr);
1230 /* If driver is unloading let the worker thread continue */
1231 if (phba->pport->load_flag & FC_UNLOADING) {
1232 phba->work_hs = 0;
1233 break;
1234 }
1235 }
1236
1237 /*
1238 * This is to ptrotect against a race condition in which
1239 * first write to the host attention register clear the
1240 * host status register.
1241 */
1242 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1243 phba->work_hs = old_host_status & ~HS_FFER1;
1244
James Smart3772a992009-05-22 14:50:54 -04001245 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001246 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001247 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001248 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1249 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1250}
1251
James Smart3772a992009-05-22 14:50:54 -04001252static void
1253lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1254{
1255 struct lpfc_board_event_header board_event;
1256 struct Scsi_Host *shost;
1257
1258 board_event.event_type = FC_REG_BOARD_EVENT;
1259 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1260 shost = lpfc_shost_from_vport(phba->pport);
1261 fc_host_post_vendor_event(shost, fc_get_event_number(),
1262 sizeof(board_event),
1263 (char *) &board_event,
1264 LPFC_NL_VENDOR_ID);
1265}
1266
James Smarta257bf92009-04-06 18:48:10 -04001267/**
James Smart3772a992009-05-22 14:50:54 -04001268 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001269 * @phba: pointer to lpfc hba data structure.
1270 *
1271 * This routine is invoked to handle the following HBA hardware error
1272 * conditions:
1273 * 1 - HBA error attention interrupt
1274 * 2 - DMA ring index out of range
1275 * 3 - Mailbox command came back as unknown
1276 **/
James Smart3772a992009-05-22 14:50:54 -04001277static void
1278lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001279{
James Smart2e0fef82007-06-17 19:56:36 -05001280 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001281 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001282 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001283 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001284 unsigned long temperature;
1285 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001286 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001287
Linas Vepstas8d63f372007-02-14 14:28:36 -06001288 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001289 * since we cannot communicate with the pci card anyway.
1290 */
1291 if (pci_channel_offline(phba->pcidev)) {
1292 spin_lock_irq(&phba->hbalock);
1293 phba->hba_flag &= ~DEFER_ERATT;
1294 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001295 return;
James Smart3772a992009-05-22 14:50:54 -04001296 }
1297
James Smart13815c82008-01-11 01:52:48 -05001298 /* If resets are disabled then leave the HBA alone and return */
1299 if (!phba->cfg_enable_hba_reset)
1300 return;
dea31012005-04-17 16:05:31 -05001301
James Smartea2151b2008-09-07 11:52:10 -04001302 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001303 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001304
James Smarta257bf92009-04-06 18:48:10 -04001305 if (phba->hba_flag & DEFER_ERATT)
1306 lpfc_handle_deferred_eratt(phba);
1307
James Smartdcf2a4e2010-09-29 11:18:53 -04001308 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1309 if (phba->work_hs & HS_FFER6)
1310 /* Re-establishing Link */
1311 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1312 "1301 Re-establishing Link "
1313 "Data: x%x x%x x%x\n",
1314 phba->work_hs, phba->work_status[0],
1315 phba->work_status[1]);
1316 if (phba->work_hs & HS_FFER8)
1317 /* Device Zeroization */
1318 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1319 "2861 Host Authentication device "
1320 "zeroization Data:x%x x%x x%x\n",
1321 phba->work_hs, phba->work_status[0],
1322 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001323
James Smart92d7f7b2007-06-17 19:56:38 -05001324 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001325 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001326 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001327
1328 /*
1329 * Firmware stops when it triggled erratt with HS_FFER6.
1330 * That could cause the I/Os dropped by the firmware.
1331 * Error iocb (I/O) on txcmplq and let the SCSI layer
1332 * retry it after re-establishing link.
1333 */
1334 pring = &psli->ring[psli->fcp_ring];
1335 lpfc_sli_abort_iocb_ring(phba, pring);
1336
dea31012005-04-17 16:05:31 -05001337 /*
1338 * There was a firmware error. Take the hba offline and then
1339 * attempt to restart it.
1340 */
James Smart46fa3112007-04-25 09:51:45 -04001341 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001342 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001343 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001344 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001345 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001346 return;
1347 }
James Smart46fa3112007-04-25 09:51:45 -04001348 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001349 } else if (phba->work_hs & HS_CRIT_TEMP) {
1350 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1351 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1352 temp_event_data.event_code = LPFC_CRIT_TEMP;
1353 temp_event_data.data = (uint32_t)temperature;
1354
1355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001356 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001357 "(%ld), taking this port offline "
1358 "Data: x%x x%x x%x\n",
1359 temperature, phba->work_hs,
1360 phba->work_status[0], phba->work_status[1]);
1361
1362 shost = lpfc_shost_from_vport(phba->pport);
1363 fc_host_post_vendor_event(shost, fc_get_event_number(),
1364 sizeof(temp_event_data),
1365 (char *) &temp_event_data,
1366 SCSI_NL_VID_TYPE_PCI
1367 | PCI_VENDOR_ID_EMULEX);
1368
James Smart7af67052007-10-27 13:38:11 -04001369 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001370 phba->over_temp_state = HBA_OVER_TEMP;
1371 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001372 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001373
dea31012005-04-17 16:05:31 -05001374 } else {
1375 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001376 * failure is a value other than FFER6. Do not call the offline
1377 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001378 */
1379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001380 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001381 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001382 phba->work_hs,
dea31012005-04-17 16:05:31 -05001383 phba->work_status[0], phba->work_status[1]);
1384
James Smartd2873e42006-08-18 17:46:43 -04001385 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001386 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001387 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001388 sizeof(event_data), (char *) &event_data,
1389 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1390
James Smart09372822008-01-11 01:52:54 -05001391 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001392 }
James Smart93996272008-08-24 21:50:30 -04001393 return;
dea31012005-04-17 16:05:31 -05001394}
1395
James Smarte59058c2008-08-24 21:49:00 -04001396/**
James Smartda0436e2009-05-22 14:51:39 -04001397 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1398 * @phba: pointer to lpfc hba data structure.
1399 *
1400 * This routine is invoked to handle the SLI4 HBA hardware error attention
1401 * conditions.
1402 **/
1403static void
1404lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1405{
1406 struct lpfc_vport *vport = phba->pport;
1407 uint32_t event_data;
1408 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001409 uint32_t if_type;
1410 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04001411
1412 /* If the pci channel is offline, ignore possible errors, since
1413 * we cannot communicate with the pci card anyway.
1414 */
1415 if (pci_channel_offline(phba->pcidev))
1416 return;
1417 /* If resets are disabled then leave the HBA alone and return */
1418 if (!phba->cfg_enable_hba_reset)
1419 return;
1420
1421 /* Send an internal error event to mgmt application */
1422 lpfc_board_errevt_to_mgmt(phba);
1423
1424 /* For now, the actual action for SLI4 device handling is not
1425 * specified yet, just treated it as adaptor hardware failure
1426 */
James Smartda0436e2009-05-22 14:51:39 -04001427 event_data = FC_REG_DUMP_EVENT;
1428 shost = lpfc_shost_from_vport(vport);
1429 fc_host_post_vendor_event(shost, fc_get_event_number(),
1430 sizeof(event_data), (char *) &event_data,
1431 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1432
James Smart2fcee4b2010-12-15 17:57:46 -05001433 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1434 switch (if_type) {
1435 case LPFC_SLI_INTF_IF_TYPE_0:
1436 lpfc_sli4_offline_eratt(phba);
1437 break;
1438 case LPFC_SLI_INTF_IF_TYPE_2:
1439 portstat_reg.word0 =
1440 readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
1441
1442 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1443 /* TODO: Register for Overtemp async events. */
1444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1445 "2889 Port Overtemperature event, "
1446 "taking port\n");
1447 spin_lock_irq(&phba->hbalock);
1448 phba->over_temp_state = HBA_OVER_TEMP;
1449 spin_unlock_irq(&phba->hbalock);
1450 lpfc_sli4_offline_eratt(phba);
1451 return;
1452 }
1453 if (bf_get(lpfc_sliport_status_rn, &portstat_reg)) {
1454 /*
1455 * TODO: Attempt port recovery via a port reset.
1456 * When fully implemented, the driver should
1457 * attempt to recover the port here and return.
1458 * For now, log an error and take the port offline.
1459 */
1460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1461 "2887 Port Error: Attempting "
1462 "Port Recovery\n");
1463 }
1464 lpfc_sli4_offline_eratt(phba);
1465 break;
1466 case LPFC_SLI_INTF_IF_TYPE_1:
1467 default:
1468 break;
1469 }
James Smartda0436e2009-05-22 14:51:39 -04001470}
1471
1472/**
1473 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1474 * @phba: pointer to lpfc HBA data structure.
1475 *
1476 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1477 * routine from the API jump table function pointer from the lpfc_hba struct.
1478 *
1479 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001480 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001481 * Any other value - error.
1482 **/
1483void
1484lpfc_handle_eratt(struct lpfc_hba *phba)
1485{
1486 (*phba->lpfc_handle_eratt)(phba);
1487}
1488
1489/**
James Smart3621a712009-04-06 18:47:14 -04001490 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001491 * @phba: pointer to lpfc hba data structure.
1492 *
1493 * This routine is invoked from the worker thread to handle a HBA host
1494 * attention link event.
1495 **/
dea31012005-04-17 16:05:31 -05001496void
James Smart2e0fef82007-06-17 19:56:36 -05001497lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001498{
James Smart2e0fef82007-06-17 19:56:36 -05001499 struct lpfc_vport *vport = phba->pport;
1500 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001501 LPFC_MBOXQ_t *pmb;
1502 volatile uint32_t control;
1503 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001504 int rc = 0;
dea31012005-04-17 16:05:31 -05001505
1506 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001507 if (!pmb) {
1508 rc = 1;
dea31012005-04-17 16:05:31 -05001509 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001510 }
dea31012005-04-17 16:05:31 -05001511
1512 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001513 if (!mp) {
1514 rc = 2;
dea31012005-04-17 16:05:31 -05001515 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001516 }
dea31012005-04-17 16:05:31 -05001517
1518 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001519 if (!mp->virt) {
1520 rc = 3;
dea31012005-04-17 16:05:31 -05001521 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001522 }
dea31012005-04-17 16:05:31 -05001523
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001524 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001525 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001526
1527 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001528 lpfc_read_topology(phba, pmb, mp);
1529 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001530 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001531 /* Block ELS IOCBs until we have processed this mbox command */
1532 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001533 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001534 if (rc == MBX_NOT_FINISHED) {
1535 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001536 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001537 }
dea31012005-04-17 16:05:31 -05001538
1539 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001540 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001541 writel(HA_LATT, phba->HAregaddr);
1542 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001543 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001544
1545 return;
1546
James Smart14691152006-12-02 13:34:28 -05001547lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001548 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001549 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001550lpfc_handle_latt_free_mp:
1551 kfree(mp);
1552lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001553 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001554lpfc_handle_latt_err_exit:
1555 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001556 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001557 psli->sli_flag |= LPFC_PROCESS_LA;
1558 control = readl(phba->HCregaddr);
1559 control |= HC_LAINT_ENA;
1560 writel(control, phba->HCregaddr);
1561 readl(phba->HCregaddr); /* flush */
1562
1563 /* Clear Link Attention in HA REG */
1564 writel(HA_LATT, phba->HAregaddr);
1565 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001566 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001567 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001568 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001569
James Smart09372822008-01-11 01:52:54 -05001570 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1571 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001572
1573 return;
1574}
1575
James Smarte59058c2008-08-24 21:49:00 -04001576/**
James Smart3621a712009-04-06 18:47:14 -04001577 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001578 * @phba: pointer to lpfc hba data structure.
1579 * @vpd: pointer to the vital product data.
1580 * @len: length of the vital product data in bytes.
1581 *
1582 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1583 * an array of characters. In this routine, the ModelName, ProgramType, and
1584 * ModelDesc, etc. fields of the phba data structure will be populated.
1585 *
1586 * Return codes
1587 * 0 - pointer to the VPD passed in is NULL
1588 * 1 - success
1589 **/
James Smart3772a992009-05-22 14:50:54 -04001590int
James Smart2e0fef82007-06-17 19:56:36 -05001591lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001592{
1593 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001594 int Length;
dea31012005-04-17 16:05:31 -05001595 int i, j;
1596 int finished = 0;
1597 int index = 0;
1598
1599 if (!vpd)
1600 return 0;
1601
1602 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001603 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001604 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001605 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1606 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001607 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001608 switch (vpd[index]) {
1609 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001610 case 0x91:
dea31012005-04-17 16:05:31 -05001611 index += 1;
1612 lenlo = vpd[index];
1613 index += 1;
1614 lenhi = vpd[index];
1615 index += 1;
1616 i = ((((unsigned short)lenhi) << 8) + lenlo);
1617 index += i;
1618 break;
1619 case 0x90:
1620 index += 1;
1621 lenlo = vpd[index];
1622 index += 1;
1623 lenhi = vpd[index];
1624 index += 1;
1625 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001626 if (Length > len - index)
1627 Length = len - index;
dea31012005-04-17 16:05:31 -05001628 while (Length > 0) {
1629 /* Look for Serial Number */
1630 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1631 index += 2;
1632 i = vpd[index];
1633 index += 1;
1634 j = 0;
1635 Length -= (3+i);
1636 while(i--) {
1637 phba->SerialNumber[j++] = vpd[index++];
1638 if (j == 31)
1639 break;
1640 }
1641 phba->SerialNumber[j] = 0;
1642 continue;
1643 }
1644 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1645 phba->vpd_flag |= VPD_MODEL_DESC;
1646 index += 2;
1647 i = vpd[index];
1648 index += 1;
1649 j = 0;
1650 Length -= (3+i);
1651 while(i--) {
1652 phba->ModelDesc[j++] = vpd[index++];
1653 if (j == 255)
1654 break;
1655 }
1656 phba->ModelDesc[j] = 0;
1657 continue;
1658 }
1659 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1660 phba->vpd_flag |= VPD_MODEL_NAME;
1661 index += 2;
1662 i = vpd[index];
1663 index += 1;
1664 j = 0;
1665 Length -= (3+i);
1666 while(i--) {
1667 phba->ModelName[j++] = vpd[index++];
1668 if (j == 79)
1669 break;
1670 }
1671 phba->ModelName[j] = 0;
1672 continue;
1673 }
1674 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1675 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1676 index += 2;
1677 i = vpd[index];
1678 index += 1;
1679 j = 0;
1680 Length -= (3+i);
1681 while(i--) {
1682 phba->ProgramType[j++] = vpd[index++];
1683 if (j == 255)
1684 break;
1685 }
1686 phba->ProgramType[j] = 0;
1687 continue;
1688 }
1689 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1690 phba->vpd_flag |= VPD_PORT;
1691 index += 2;
1692 i = vpd[index];
1693 index += 1;
1694 j = 0;
1695 Length -= (3+i);
1696 while(i--) {
1697 phba->Port[j++] = vpd[index++];
1698 if (j == 19)
1699 break;
1700 }
1701 phba->Port[j] = 0;
1702 continue;
1703 }
1704 else {
1705 index += 2;
1706 i = vpd[index];
1707 index += 1;
1708 index += i;
1709 Length -= (3 + i);
1710 }
1711 }
1712 finished = 0;
1713 break;
1714 case 0x78:
1715 finished = 1;
1716 break;
1717 default:
1718 index ++;
1719 break;
1720 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001721 }
dea31012005-04-17 16:05:31 -05001722
1723 return(1);
1724}
1725
James Smarte59058c2008-08-24 21:49:00 -04001726/**
James Smart3621a712009-04-06 18:47:14 -04001727 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001728 * @phba: pointer to lpfc hba data structure.
1729 * @mdp: pointer to the data structure to hold the derived model name.
1730 * @descp: pointer to the data structure to hold the derived description.
1731 *
1732 * This routine retrieves HBA's description based on its registered PCI device
1733 * ID. The @descp passed into this function points to an array of 256 chars. It
1734 * shall be returned with the model name, maximum speed, and the host bus type.
1735 * The @mdp passed into this function points to an array of 80 chars. When the
1736 * function returns, the @mdp will be filled with the model name.
1737 **/
dea31012005-04-17 16:05:31 -05001738static void
James Smart2e0fef82007-06-17 19:56:36 -05001739lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001740{
1741 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001742 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001743 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001744 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001745 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001746 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001747 char *name;
1748 char *bus;
1749 char *function;
1750 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001751
1752 if (mdp && mdp[0] != '\0'
1753 && descp && descp[0] != '\0')
1754 return;
1755
1756 if (phba->lmt & LMT_10Gb)
1757 max_speed = 10;
1758 else if (phba->lmt & LMT_8Gb)
1759 max_speed = 8;
1760 else if (phba->lmt & LMT_4Gb)
1761 max_speed = 4;
1762 else if (phba->lmt & LMT_2Gb)
1763 max_speed = 2;
1764 else
1765 max_speed = 1;
dea31012005-04-17 16:05:31 -05001766
1767 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001768
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001769 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001770 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001771 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001772 break;
dea31012005-04-17 16:05:31 -05001773 case PCI_DEVICE_ID_SUPERFLY:
1774 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001775 m = (typeof(m)){"LP7000", "PCI",
1776 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001777 else
James Smarta747c9c2009-11-18 15:41:10 -05001778 m = (typeof(m)){"LP7000E", "PCI",
1779 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001780 break;
1781 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001782 m = (typeof(m)){"LP8000", "PCI",
1783 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001784 break;
1785 case PCI_DEVICE_ID_CENTAUR:
1786 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001787 m = (typeof(m)){"LP9002", "PCI",
1788 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001789 else
James Smarta747c9c2009-11-18 15:41:10 -05001790 m = (typeof(m)){"LP9000", "PCI",
1791 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001792 break;
1793 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001794 m = (typeof(m)){"LP952", "PCI",
1795 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001796 break;
1797 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001798 m = (typeof(m)){"LP9802", "PCI-X",
1799 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001800 break;
1801 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001802 m = (typeof(m)){"LP10000", "PCI-X",
1803 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001804 break;
1805 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001806 m = (typeof(m)){"LPX1000", "PCI-X",
1807 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001808 break;
1809 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001810 m = (typeof(m)){"LP982", "PCI-X",
1811 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001812 break;
1813 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001814 m = (typeof(m)){"LP1050", "PCI-X",
1815 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001816 break;
1817 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001818 m = (typeof(m)){"LP11000", "PCI-X2",
1819 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001820 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001821 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001822 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1823 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001824 break;
1825 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001826 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1827 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001828 break;
1829 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001830 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001831 break;
1832 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001833 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001834 break;
1835 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001836 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001837 break;
dea31012005-04-17 16:05:31 -05001838 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001839 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001840 break;
1841 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001842 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001843 break;
1844 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001845 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001846 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001847 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001848 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001849 break;
1850 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001851 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001852 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001853 break;
dea31012005-04-17 16:05:31 -05001854 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001855 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001856 break;
1857 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001858 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001859 break;
1860 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001861 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001862 break;
1863 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001864 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001865 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001866 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001867 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001868 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001869 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001870 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001871 break;
James Smartb87eab32007-04-25 09:53:28 -04001872 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001873 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001874 break;
1875 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001876 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001877 break;
1878 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001879 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001880 break;
1881 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001882 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001883 break;
1884 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001885 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001886 break;
1887 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001888 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001889 break;
James Smart84774a42008-08-24 21:50:06 -04001890 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001891 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001892 GE = 1;
1893 break;
1894 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001895 m = (typeof(m)){"LPev12000", "PCIe IOV",
1896 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001897 break;
1898 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001899 m = (typeof(m)){"LPev12000", "PCIe IOV",
1900 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001901 break;
1902 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001903 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1904 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001905 break;
James Smartda0436e2009-05-22 14:51:39 -04001906 case PCI_DEVICE_ID_TIGERSHARK:
1907 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001908 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04001909 break;
James Smarta747c9c2009-11-18 15:41:10 -05001910 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04001911 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001912 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1913 break;
1914 case PCI_DEVICE_ID_FALCON:
1915 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1916 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04001917 break;
James Smart98fc5dd2010-06-07 15:24:29 -04001918 case PCI_DEVICE_ID_BALIUS:
1919 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
1920 "Fibre Channel Adapter"};
1921 break;
James Smart085c6472010-11-20 23:11:37 -05001922 case PCI_DEVICE_ID_LANCER_FC:
1923 oneConnect = 1;
1924 m = (typeof(m)){"Undefined", "PCIe", "Fibre Channel Adapter"};
1925 break;
1926 case PCI_DEVICE_ID_LANCER_FCOE:
1927 oneConnect = 1;
1928 m = (typeof(m)){"Undefined", "PCIe", "FCoE"};
1929 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001930 default:
James Smarta747c9c2009-11-18 15:41:10 -05001931 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001932 break;
dea31012005-04-17 16:05:31 -05001933 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001934
1935 if (mdp && mdp[0] == '\0')
1936 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001937 /* oneConnect hba requires special processing, they are all initiators
1938 * and we put the port number on the end
1939 */
1940 if (descp && descp[0] == '\0') {
1941 if (oneConnect)
1942 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05001943 "Emulex OneConnect %s, %s Initiator, Port %s",
1944 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04001945 phba->Port);
1946 else
1947 snprintf(descp, 255,
1948 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05001949 m.name, max_speed, (GE) ? "GE" : "Gb",
1950 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04001951 }
dea31012005-04-17 16:05:31 -05001952}
1953
James Smarte59058c2008-08-24 21:49:00 -04001954/**
James Smart3621a712009-04-06 18:47:14 -04001955 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001956 * @phba: pointer to lpfc hba data structure.
1957 * @pring: pointer to a IOCB ring.
1958 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1959 *
1960 * This routine posts a given number of IOCBs with the associated DMA buffer
1961 * descriptors specified by the cnt argument to the given IOCB ring.
1962 *
1963 * Return codes
1964 * The number of IOCBs NOT able to be posted to the IOCB ring.
1965 **/
dea31012005-04-17 16:05:31 -05001966int
James Smart495a7142008-06-14 22:52:59 -04001967lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001968{
1969 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001970 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001971 struct lpfc_dmabuf *mp1, *mp2;
1972
1973 cnt += pring->missbufcnt;
1974
1975 /* While there are buffers to post */
1976 while (cnt > 0) {
1977 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001978 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001979 if (iocb == NULL) {
1980 pring->missbufcnt = cnt;
1981 return cnt;
1982 }
dea31012005-04-17 16:05:31 -05001983 icmd = &iocb->iocb;
1984
1985 /* 2 buffers can be posted per command */
1986 /* Allocate buffer to post */
1987 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1988 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001989 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1990 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001991 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001992 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001993 pring->missbufcnt = cnt;
1994 return cnt;
1995 }
1996
1997 INIT_LIST_HEAD(&mp1->list);
1998 /* Allocate buffer to post */
1999 if (cnt > 1) {
2000 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2001 if (mp2)
2002 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2003 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002004 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002005 kfree(mp2);
dea31012005-04-17 16:05:31 -05002006 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2007 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002008 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002009 pring->missbufcnt = cnt;
2010 return cnt;
2011 }
2012
2013 INIT_LIST_HEAD(&mp2->list);
2014 } else {
2015 mp2 = NULL;
2016 }
2017
2018 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2019 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2020 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2021 icmd->ulpBdeCount = 1;
2022 cnt--;
2023 if (mp2) {
2024 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2025 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2026 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2027 cnt--;
2028 icmd->ulpBdeCount = 2;
2029 }
2030
2031 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2032 icmd->ulpLe = 1;
2033
James Smart3772a992009-05-22 14:50:54 -04002034 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2035 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002036 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2037 kfree(mp1);
2038 cnt++;
2039 if (mp2) {
2040 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2041 kfree(mp2);
2042 cnt++;
2043 }
James Bottomley604a3e32005-10-29 10:28:33 -05002044 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002045 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002046 return cnt;
2047 }
dea31012005-04-17 16:05:31 -05002048 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002049 if (mp2)
dea31012005-04-17 16:05:31 -05002050 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002051 }
2052 pring->missbufcnt = 0;
2053 return 0;
2054}
2055
James Smarte59058c2008-08-24 21:49:00 -04002056/**
James Smart3621a712009-04-06 18:47:14 -04002057 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002058 * @phba: pointer to lpfc hba data structure.
2059 *
2060 * This routine posts initial receive IOCB buffers to the ELS ring. The
2061 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2062 * set to 64 IOCBs.
2063 *
2064 * Return codes
2065 * 0 - success (currently always success)
2066 **/
dea31012005-04-17 16:05:31 -05002067static int
James Smart2e0fef82007-06-17 19:56:36 -05002068lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002069{
2070 struct lpfc_sli *psli = &phba->sli;
2071
2072 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002073 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002074 /* Ring 2 - FCP no buffers needed */
2075
2076 return 0;
2077}
2078
2079#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2080
James Smarte59058c2008-08-24 21:49:00 -04002081/**
James Smart3621a712009-04-06 18:47:14 -04002082 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002083 * @HashResultPointer: pointer to an array as hash table.
2084 *
2085 * This routine sets up the initial values to the array of hash table entries
2086 * for the LC HBAs.
2087 **/
dea31012005-04-17 16:05:31 -05002088static void
2089lpfc_sha_init(uint32_t * HashResultPointer)
2090{
2091 HashResultPointer[0] = 0x67452301;
2092 HashResultPointer[1] = 0xEFCDAB89;
2093 HashResultPointer[2] = 0x98BADCFE;
2094 HashResultPointer[3] = 0x10325476;
2095 HashResultPointer[4] = 0xC3D2E1F0;
2096}
2097
James Smarte59058c2008-08-24 21:49:00 -04002098/**
James Smart3621a712009-04-06 18:47:14 -04002099 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002100 * @HashResultPointer: pointer to an initial/result hash table.
2101 * @HashWorkingPointer: pointer to an working hash table.
2102 *
2103 * This routine iterates an initial hash table pointed by @HashResultPointer
2104 * with the values from the working hash table pointeed by @HashWorkingPointer.
2105 * The results are putting back to the initial hash table, returned through
2106 * the @HashResultPointer as the result hash table.
2107 **/
dea31012005-04-17 16:05:31 -05002108static void
2109lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2110{
2111 int t;
2112 uint32_t TEMP;
2113 uint32_t A, B, C, D, E;
2114 t = 16;
2115 do {
2116 HashWorkingPointer[t] =
2117 S(1,
2118 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2119 8] ^
2120 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2121 } while (++t <= 79);
2122 t = 0;
2123 A = HashResultPointer[0];
2124 B = HashResultPointer[1];
2125 C = HashResultPointer[2];
2126 D = HashResultPointer[3];
2127 E = HashResultPointer[4];
2128
2129 do {
2130 if (t < 20) {
2131 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2132 } else if (t < 40) {
2133 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2134 } else if (t < 60) {
2135 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2136 } else {
2137 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2138 }
2139 TEMP += S(5, A) + E + HashWorkingPointer[t];
2140 E = D;
2141 D = C;
2142 C = S(30, B);
2143 B = A;
2144 A = TEMP;
2145 } while (++t <= 79);
2146
2147 HashResultPointer[0] += A;
2148 HashResultPointer[1] += B;
2149 HashResultPointer[2] += C;
2150 HashResultPointer[3] += D;
2151 HashResultPointer[4] += E;
2152
2153}
2154
James Smarte59058c2008-08-24 21:49:00 -04002155/**
James Smart3621a712009-04-06 18:47:14 -04002156 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002157 * @RandomChallenge: pointer to the entry of host challenge random number array.
2158 * @HashWorking: pointer to the entry of the working hash array.
2159 *
2160 * This routine calculates the working hash array referred by @HashWorking
2161 * from the challenge random numbers associated with the host, referred by
2162 * @RandomChallenge. The result is put into the entry of the working hash
2163 * array and returned by reference through @HashWorking.
2164 **/
dea31012005-04-17 16:05:31 -05002165static void
2166lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2167{
2168 *HashWorking = (*RandomChallenge ^ *HashWorking);
2169}
2170
James Smarte59058c2008-08-24 21:49:00 -04002171/**
James Smart3621a712009-04-06 18:47:14 -04002172 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002173 * @phba: pointer to lpfc hba data structure.
2174 * @hbainit: pointer to an array of unsigned 32-bit integers.
2175 *
2176 * This routine performs the special handling for LC HBA initialization.
2177 **/
dea31012005-04-17 16:05:31 -05002178void
2179lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2180{
2181 int t;
2182 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002183 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002184
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002185 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002186 if (!HashWorking)
2187 return;
2188
dea31012005-04-17 16:05:31 -05002189 HashWorking[0] = HashWorking[78] = *pwwnn++;
2190 HashWorking[1] = HashWorking[79] = *pwwnn;
2191
2192 for (t = 0; t < 7; t++)
2193 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2194
2195 lpfc_sha_init(hbainit);
2196 lpfc_sha_iterate(hbainit, HashWorking);
2197 kfree(HashWorking);
2198}
2199
James Smarte59058c2008-08-24 21:49:00 -04002200/**
James Smart3621a712009-04-06 18:47:14 -04002201 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002202 * @vport: pointer to a virtual N_Port data structure.
2203 *
2204 * This routine performs the necessary cleanups before deleting the @vport.
2205 * It invokes the discovery state machine to perform necessary state
2206 * transitions and to release the ndlps associated with the @vport. Note,
2207 * the physical port is treated as @vport 0.
2208 **/
James Smart87af33f2007-10-27 13:37:43 -04002209void
James Smart2e0fef82007-06-17 19:56:36 -05002210lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002211{
James Smart87af33f2007-10-27 13:37:43 -04002212 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002213 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002214 int i = 0;
dea31012005-04-17 16:05:31 -05002215
James Smart87af33f2007-10-27 13:37:43 -04002216 if (phba->link_state > LPFC_LINK_DOWN)
2217 lpfc_port_link_failure(vport);
2218
2219 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002220 if (!NLP_CHK_NODE_ACT(ndlp)) {
2221 ndlp = lpfc_enable_node(vport, ndlp,
2222 NLP_STE_UNUSED_NODE);
2223 if (!ndlp)
2224 continue;
2225 spin_lock_irq(&phba->ndlp_lock);
2226 NLP_SET_FREE_REQ(ndlp);
2227 spin_unlock_irq(&phba->ndlp_lock);
2228 /* Trigger the release of the ndlp memory */
2229 lpfc_nlp_put(ndlp);
2230 continue;
2231 }
2232 spin_lock_irq(&phba->ndlp_lock);
2233 if (NLP_CHK_FREE_REQ(ndlp)) {
2234 /* The ndlp should not be in memory free mode already */
2235 spin_unlock_irq(&phba->ndlp_lock);
2236 continue;
2237 } else
2238 /* Indicate request for freeing ndlp memory */
2239 NLP_SET_FREE_REQ(ndlp);
2240 spin_unlock_irq(&phba->ndlp_lock);
2241
James Smart58da1ff2008-04-07 10:15:56 -04002242 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2243 ndlp->nlp_DID == Fabric_DID) {
2244 /* Just free up ndlp with Fabric_DID for vports */
2245 lpfc_nlp_put(ndlp);
2246 continue;
2247 }
2248
James Smart87af33f2007-10-27 13:37:43 -04002249 if (ndlp->nlp_type & NLP_FABRIC)
2250 lpfc_disc_state_machine(vport, ndlp, NULL,
2251 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002252
James Smart87af33f2007-10-27 13:37:43 -04002253 lpfc_disc_state_machine(vport, ndlp, NULL,
2254 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002255
James Smart87af33f2007-10-27 13:37:43 -04002256 }
2257
James Smarta8adb832007-10-27 13:37:53 -04002258 /* At this point, ALL ndlp's should be gone
2259 * because of the previous NLP_EVT_DEVICE_RM.
2260 * Lets wait for this to happen, if needed.
2261 */
James Smart87af33f2007-10-27 13:37:43 -04002262 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002263 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002264 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002265 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002266 list_for_each_entry_safe(ndlp, next_ndlp,
2267 &vport->fc_nodes, nlp_listp) {
2268 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2269 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002270 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002271 "usgmap:x%x refcnt:%d\n",
2272 ndlp->nlp_DID, (void *)ndlp,
2273 ndlp->nlp_usg_map,
2274 atomic_read(
2275 &ndlp->kref.refcount));
2276 }
James Smarta8adb832007-10-27 13:37:53 -04002277 break;
James Smart87af33f2007-10-27 13:37:43 -04002278 }
James Smarta8adb832007-10-27 13:37:53 -04002279
2280 /* Wait for any activity on ndlps to settle */
2281 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002282 }
dea31012005-04-17 16:05:31 -05002283}
2284
James Smarte59058c2008-08-24 21:49:00 -04002285/**
James Smart3621a712009-04-06 18:47:14 -04002286 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002287 * @vport: pointer to a virtual N_Port data structure.
2288 *
2289 * This routine stops all the timers associated with a @vport. This function
2290 * is invoked before disabling or deleting a @vport. Note that the physical
2291 * port is treated as @vport 0.
2292 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002293void
2294lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002295{
James Smart92d7f7b2007-06-17 19:56:38 -05002296 del_timer_sync(&vport->els_tmofunc);
2297 del_timer_sync(&vport->fc_fdmitmo);
2298 lpfc_can_disctmo(vport);
2299 return;
dea31012005-04-17 16:05:31 -05002300}
2301
James Smarte59058c2008-08-24 21:49:00 -04002302/**
James Smartecfd03c2010-02-12 14:41:27 -05002303 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2304 * @phba: pointer to lpfc hba data structure.
2305 *
2306 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2307 * caller of this routine should already hold the host lock.
2308 **/
2309void
2310__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2311{
James Smart5ac6b302010-10-22 11:05:36 -04002312 /* Clear pending FCF rediscovery wait flag */
2313 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2314
James Smartecfd03c2010-02-12 14:41:27 -05002315 /* Now, try to stop the timer */
2316 del_timer(&phba->fcf.redisc_wait);
2317}
2318
2319/**
2320 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2321 * @phba: pointer to lpfc hba data structure.
2322 *
2323 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2324 * checks whether the FCF rediscovery wait timer is pending with the host
2325 * lock held before proceeding with disabling the timer and clearing the
2326 * wait timer pendig flag.
2327 **/
2328void
2329lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2330{
2331 spin_lock_irq(&phba->hbalock);
2332 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2333 /* FCF rediscovery timer already fired or stopped */
2334 spin_unlock_irq(&phba->hbalock);
2335 return;
2336 }
2337 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002338 /* Clear failover in progress flags */
2339 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002340 spin_unlock_irq(&phba->hbalock);
2341}
2342
2343/**
James Smart3772a992009-05-22 14:50:54 -04002344 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002345 * @phba: pointer to lpfc hba data structure.
2346 *
2347 * This routine stops all the timers associated with a HBA. This function is
2348 * invoked before either putting a HBA offline or unloading the driver.
2349 **/
James Smart3772a992009-05-22 14:50:54 -04002350void
2351lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002352{
James Smart51ef4c22007-08-02 11:10:31 -04002353 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002354 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002355 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002356 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002357 del_timer_sync(&phba->hb_tmofunc);
2358 phba->hb_outstanding = 0;
2359
2360 switch (phba->pci_dev_grp) {
2361 case LPFC_PCI_DEV_LP:
2362 /* Stop any LightPulse device specific driver timers */
2363 del_timer_sync(&phba->fcp_poll_timer);
2364 break;
2365 case LPFC_PCI_DEV_OC:
2366 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002367 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002368 break;
2369 default:
2370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2371 "0297 Invalid device group (x%x)\n",
2372 phba->pci_dev_grp);
2373 break;
2374 }
James Smart2e0fef82007-06-17 19:56:36 -05002375 return;
dea31012005-04-17 16:05:31 -05002376}
2377
James Smarte59058c2008-08-24 21:49:00 -04002378/**
James Smart3621a712009-04-06 18:47:14 -04002379 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002380 * @phba: pointer to lpfc hba data structure.
2381 *
2382 * This routine marks a HBA's management interface as blocked. Once the HBA's
2383 * management interface is marked as blocked, all the user space access to
2384 * the HBA, whether they are from sysfs interface or libdfc interface will
2385 * all be blocked. The HBA is set to block the management interface when the
2386 * driver prepares the HBA interface for online or offline.
2387 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002388static void
2389lpfc_block_mgmt_io(struct lpfc_hba * phba)
2390{
2391 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002392 uint8_t actcmd = MBX_HEARTBEAT;
2393 unsigned long timeout;
2394
Adrian Bunka6ababd2007-11-05 18:07:33 +01002395
2396 spin_lock_irqsave(&phba->hbalock, iflag);
2397 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart6e7288d2010-06-07 15:23:35 -04002398 if (phba->sli.mbox_active)
2399 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
Adrian Bunka6ababd2007-11-05 18:07:33 +01002400 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6e7288d2010-06-07 15:23:35 -04002401 /* Determine how long we might wait for the active mailbox
2402 * command to be gracefully completed by firmware.
2403 */
2404 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
2405 jiffies;
2406 /* Wait for the outstnading mailbox command to complete */
2407 while (phba->sli.mbox_active) {
2408 /* Check active mailbox complete status every 2ms */
2409 msleep(2);
2410 if (time_after(jiffies, timeout)) {
2411 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2412 "2813 Mgmt IO is Blocked %x "
2413 "- mbox cmd %x still active\n",
2414 phba->sli.sli_flag, actcmd);
2415 break;
2416 }
2417 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002418}
2419
James Smarte59058c2008-08-24 21:49:00 -04002420/**
James Smart3621a712009-04-06 18:47:14 -04002421 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002422 * @phba: pointer to lpfc hba data structure.
2423 *
2424 * This routine initializes the HBA and brings a HBA online. During this
2425 * process, the management interface is blocked to prevent user space access
2426 * to the HBA interfering with the driver initialization.
2427 *
2428 * Return codes
2429 * 0 - successful
2430 * 1 - failed
2431 **/
dea31012005-04-17 16:05:31 -05002432int
James Smart2e0fef82007-06-17 19:56:36 -05002433lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002434{
Julia Lawall372bd282008-12-16 16:15:08 +01002435 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002436 struct lpfc_vport **vports;
2437 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002438
dea31012005-04-17 16:05:31 -05002439 if (!phba)
2440 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002441 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002442
James Smart2e0fef82007-06-17 19:56:36 -05002443 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002444 return 0;
2445
James Smarted957682007-06-17 19:56:37 -05002446 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002447 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002448
James Smart46fa3112007-04-25 09:51:45 -04002449 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002450
James Smart46fa3112007-04-25 09:51:45 -04002451 if (!lpfc_sli_queue_setup(phba)) {
2452 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002453 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002454 }
2455
James Smartda0436e2009-05-22 14:51:39 -04002456 if (phba->sli_rev == LPFC_SLI_REV4) {
2457 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2458 lpfc_unblock_mgmt_io(phba);
2459 return 1;
2460 }
2461 } else {
2462 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2463 lpfc_unblock_mgmt_io(phba);
2464 return 1;
2465 }
James Smart46fa3112007-04-25 09:51:45 -04002466 }
dea31012005-04-17 16:05:31 -05002467
James Smart549e55c2007-08-02 11:09:51 -04002468 vports = lpfc_create_vport_work_array(phba);
2469 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002470 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002471 struct Scsi_Host *shost;
2472 shost = lpfc_shost_from_vport(vports[i]);
2473 spin_lock_irq(shost->host_lock);
2474 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2475 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2476 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002477 if (phba->sli_rev == LPFC_SLI_REV4)
2478 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002479 spin_unlock_irq(shost->host_lock);
2480 }
James Smart09372822008-01-11 01:52:54 -05002481 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002482
James Smart46fa3112007-04-25 09:51:45 -04002483 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002484 return 0;
2485}
2486
James Smarte59058c2008-08-24 21:49:00 -04002487/**
James Smart3621a712009-04-06 18:47:14 -04002488 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002489 * @phba: pointer to lpfc hba data structure.
2490 *
2491 * This routine marks a HBA's management interface as not blocked. Once the
2492 * HBA's management interface is marked as not blocked, all the user space
2493 * access to the HBA, whether they are from sysfs interface or libdfc
2494 * interface will be allowed. The HBA is set to block the management interface
2495 * when the driver prepares the HBA interface for online or offline and then
2496 * set to unblock the management interface afterwards.
2497 **/
James Smart46fa3112007-04-25 09:51:45 -04002498void
James Smart46fa3112007-04-25 09:51:45 -04002499lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2500{
2501 unsigned long iflag;
2502
James Smart2e0fef82007-06-17 19:56:36 -05002503 spin_lock_irqsave(&phba->hbalock, iflag);
2504 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2505 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002506}
2507
James Smarte59058c2008-08-24 21:49:00 -04002508/**
James Smart3621a712009-04-06 18:47:14 -04002509 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002510 * @phba: pointer to lpfc hba data structure.
2511 *
2512 * This routine is invoked to prepare a HBA to be brought offline. It performs
2513 * unregistration login to all the nodes on all vports and flushes the mailbox
2514 * queue to make it ready to be brought offline.
2515 **/
James Smart46fa3112007-04-25 09:51:45 -04002516void
2517lpfc_offline_prep(struct lpfc_hba * phba)
2518{
James Smart2e0fef82007-06-17 19:56:36 -05002519 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002520 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002521 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002522 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002523 int i;
dea31012005-04-17 16:05:31 -05002524
James Smart2e0fef82007-06-17 19:56:36 -05002525 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002526 return;
dea31012005-04-17 16:05:31 -05002527
James Smart46fa3112007-04-25 09:51:45 -04002528 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002529
2530 lpfc_linkdown(phba);
2531
James Smart87af33f2007-10-27 13:37:43 -04002532 /* Issue an unreg_login to all nodes on all vports */
2533 vports = lpfc_create_vport_work_array(phba);
2534 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002535 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002536 if (vports[i]->load_flag & FC_UNLOADING)
2537 continue;
James Smart72100cc2010-02-12 14:43:01 -05002538 shost = lpfc_shost_from_vport(vports[i]);
2539 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002540 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002541 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2542 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002543 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002544
James Smart87af33f2007-10-27 13:37:43 -04002545 shost = lpfc_shost_from_vport(vports[i]);
2546 list_for_each_entry_safe(ndlp, next_ndlp,
2547 &vports[i]->fc_nodes,
2548 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002549 if (!NLP_CHK_NODE_ACT(ndlp))
2550 continue;
James Smart87af33f2007-10-27 13:37:43 -04002551 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2552 continue;
2553 if (ndlp->nlp_type & NLP_FABRIC) {
2554 lpfc_disc_state_machine(vports[i], ndlp,
2555 NULL, NLP_EVT_DEVICE_RECOVERY);
2556 lpfc_disc_state_machine(vports[i], ndlp,
2557 NULL, NLP_EVT_DEVICE_RM);
2558 }
2559 spin_lock_irq(shost->host_lock);
2560 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2561 spin_unlock_irq(shost->host_lock);
2562 lpfc_unreg_rpi(vports[i], ndlp);
2563 }
2564 }
2565 }
James Smart09372822008-01-11 01:52:54 -05002566 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002567
James Smart04c68492009-05-22 14:52:52 -04002568 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002569}
2570
James Smarte59058c2008-08-24 21:49:00 -04002571/**
James Smart3621a712009-04-06 18:47:14 -04002572 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002573 * @phba: pointer to lpfc hba data structure.
2574 *
2575 * This routine actually brings a HBA offline. It stops all the timers
2576 * associated with the HBA, brings down the SLI layer, and eventually
2577 * marks the HBA as in offline state for the upper layer protocol.
2578 **/
James Smart46fa3112007-04-25 09:51:45 -04002579void
James Smart2e0fef82007-06-17 19:56:36 -05002580lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002581{
James Smart549e55c2007-08-02 11:09:51 -04002582 struct Scsi_Host *shost;
2583 struct lpfc_vport **vports;
2584 int i;
James Smart46fa3112007-04-25 09:51:45 -04002585
James Smart549e55c2007-08-02 11:09:51 -04002586 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002587 return;
James Smart688a8862006-07-06 15:49:56 -04002588
James Smartda0436e2009-05-22 14:51:39 -04002589 /* stop port and all timers associated with this hba */
2590 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002591 vports = lpfc_create_vport_work_array(phba);
2592 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002593 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002594 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002595 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002596 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002597 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002598 /* Bring down the SLI Layer and cleanup. The HBA is offline
2599 now. */
2600 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002601 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002602 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002603 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002604 vports = lpfc_create_vport_work_array(phba);
2605 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002606 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002607 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002608 spin_lock_irq(shost->host_lock);
2609 vports[i]->work_port_events = 0;
2610 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2611 spin_unlock_irq(shost->host_lock);
2612 }
James Smart09372822008-01-11 01:52:54 -05002613 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002614}
2615
James Smarte59058c2008-08-24 21:49:00 -04002616/**
James Smart3621a712009-04-06 18:47:14 -04002617 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002618 * @phba: pointer to lpfc hba data structure.
2619 *
2620 * This routine is to free all the SCSI buffers and IOCBs from the driver
2621 * list back to kernel. It is called from lpfc_pci_remove_one to free
2622 * the internal resources before the device is removed from the system.
2623 *
2624 * Return codes
2625 * 0 - successful (for now, it always returns 0)
2626 **/
dea31012005-04-17 16:05:31 -05002627static int
James Smart2e0fef82007-06-17 19:56:36 -05002628lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002629{
2630 struct lpfc_scsi_buf *sb, *sb_next;
2631 struct lpfc_iocbq *io, *io_next;
2632
James Smart2e0fef82007-06-17 19:56:36 -05002633 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002634 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002635 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002636 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2637 list_del(&sb->list);
2638 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002639 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002640 kfree(sb);
2641 phba->total_scsi_bufs--;
2642 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002643 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002644
2645 /* Release all the lpfc_iocbq entries maintained by this host. */
2646 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2647 list_del(&io->list);
2648 kfree(io);
2649 phba->total_iocbq_bufs--;
2650 }
James Smart2e0fef82007-06-17 19:56:36 -05002651 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002652 return 0;
2653}
2654
James Smarte59058c2008-08-24 21:49:00 -04002655/**
James Smart3621a712009-04-06 18:47:14 -04002656 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002657 * @phba: pointer to lpfc hba data structure.
2658 * @instance: a unique integer ID to this FC port.
2659 * @dev: pointer to the device data structure.
2660 *
2661 * This routine creates a FC port for the upper layer protocol. The FC port
2662 * can be created on top of either a physical port or a virtual port provided
2663 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2664 * and associates the FC port created before adding the shost into the SCSI
2665 * layer.
2666 *
2667 * Return codes
2668 * @vport - pointer to the virtual N_Port data structure.
2669 * NULL - port create failed.
2670 **/
James Smart2e0fef82007-06-17 19:56:36 -05002671struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002672lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002673{
James Smart2e0fef82007-06-17 19:56:36 -05002674 struct lpfc_vport *vport;
2675 struct Scsi_Host *shost;
2676 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002677
James Smart3de2a652007-08-02 11:09:59 -04002678 if (dev != &phba->pcidev->dev)
2679 shost = scsi_host_alloc(&lpfc_vport_template,
2680 sizeof(struct lpfc_vport));
2681 else
2682 shost = scsi_host_alloc(&lpfc_template,
2683 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002684 if (!shost)
2685 goto out;
James Smart47a86172007-04-25 09:53:22 -04002686
James Smart2e0fef82007-06-17 19:56:36 -05002687 vport = (struct lpfc_vport *) shost->hostdata;
2688 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002689 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002690 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002691 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002692
James Smart3de2a652007-08-02 11:09:59 -04002693 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002694 shost->unique_id = instance;
2695 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002696 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002697 shost->this_id = -1;
2698 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002699 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002700 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04002701 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04002702 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2703 }
James Smart81301a92008-12-04 22:39:46 -05002704
James Smart47a86172007-04-25 09:53:22 -04002705 /*
James Smart2e0fef82007-06-17 19:56:36 -05002706 * Set initial can_queue value since 0 is no longer supported and
2707 * scsi_add_host will fail. This will be adjusted later based on the
2708 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002709 */
James Smart2e0fef82007-06-17 19:56:36 -05002710 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002711 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002712 shost->transportt = lpfc_vport_transport_template;
2713 vport->port_type = LPFC_NPIV_PORT;
2714 } else {
2715 shost->transportt = lpfc_transport_template;
2716 vport->port_type = LPFC_PHYSICAL_PORT;
2717 }
James Smart47a86172007-04-25 09:53:22 -04002718
James Smart2e0fef82007-06-17 19:56:36 -05002719 /* Initialize all internally managed lists. */
2720 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002721 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002722 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002723
James Smart2e0fef82007-06-17 19:56:36 -05002724 init_timer(&vport->fc_disctmo);
2725 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002726 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002727
James Smart2e0fef82007-06-17 19:56:36 -05002728 init_timer(&vport->fc_fdmitmo);
2729 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002730 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002731
James Smart2e0fef82007-06-17 19:56:36 -05002732 init_timer(&vport->els_tmofunc);
2733 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002734 vport->els_tmofunc.data = (unsigned long)vport;
James Bottomleyd139b9b2009-11-05 13:33:12 -06002735 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002736 if (error)
2737 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002738
James Smart549e55c2007-08-02 11:09:51 -04002739 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002740 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002741 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002742 return vport;
James Smart47a86172007-04-25 09:53:22 -04002743
James Smart2e0fef82007-06-17 19:56:36 -05002744out_put_shost:
2745 scsi_host_put(shost);
2746out:
2747 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002748}
2749
James Smarte59058c2008-08-24 21:49:00 -04002750/**
James Smart3621a712009-04-06 18:47:14 -04002751 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002752 * @vport: pointer to an lpfc virtual N_Port data structure.
2753 *
2754 * This routine destroys a FC port from the upper layer protocol. All the
2755 * resources associated with the port are released.
2756 **/
James Smart2e0fef82007-06-17 19:56:36 -05002757void
2758destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002759{
James Smart92d7f7b2007-06-17 19:56:38 -05002760 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2761 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002762
James Smart858c9f62007-06-17 19:56:39 -05002763 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002764 fc_remove_host(shost);
2765 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002766
James Smart92d7f7b2007-06-17 19:56:38 -05002767 spin_lock_irq(&phba->hbalock);
2768 list_del_init(&vport->listentry);
2769 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002770
James Smart92d7f7b2007-06-17 19:56:38 -05002771 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002772 return;
James Smart47a86172007-04-25 09:53:22 -04002773}
2774
James Smarte59058c2008-08-24 21:49:00 -04002775/**
James Smart3621a712009-04-06 18:47:14 -04002776 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002777 *
2778 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2779 * uses the kernel idr facility to perform the task.
2780 *
2781 * Return codes:
2782 * instance - a unique integer ID allocated as the new instance.
2783 * -1 - lpfc get instance failed.
2784 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002785int
2786lpfc_get_instance(void)
2787{
2788 int instance = 0;
2789
2790 /* Assign an unused number */
2791 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2792 return -1;
2793 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2794 return -1;
2795 return instance;
2796}
2797
James Smarte59058c2008-08-24 21:49:00 -04002798/**
James Smart3621a712009-04-06 18:47:14 -04002799 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002800 * @shost: pointer to SCSI host data structure.
2801 * @time: elapsed time of the scan in jiffies.
2802 *
2803 * This routine is called by the SCSI layer with a SCSI host to determine
2804 * whether the scan host is finished.
2805 *
2806 * Note: there is no scan_start function as adapter initialization will have
2807 * asynchronously kicked off the link initialization.
2808 *
2809 * Return codes
2810 * 0 - SCSI host scan is not over yet.
2811 * 1 - SCSI host scan is over.
2812 **/
James Smart47a86172007-04-25 09:53:22 -04002813int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2814{
James Smart2e0fef82007-06-17 19:56:36 -05002815 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2816 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002817 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002818
James Smart858c9f62007-06-17 19:56:39 -05002819 spin_lock_irq(shost->host_lock);
2820
James Smart51ef4c22007-08-02 11:10:31 -04002821 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002822 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002823 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002824 }
James Smart2e0fef82007-06-17 19:56:36 -05002825 if (time >= 30 * HZ) {
2826 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002827 "0461 Scanning longer than 30 "
2828 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002829 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002830 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002831 }
2832 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2833 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002834 "0465 Link down longer than 15 "
2835 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002836 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002837 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002838 }
2839
James Smart2e0fef82007-06-17 19:56:36 -05002840 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002841 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002842 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002843 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002844 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002845 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002846 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002847 goto finished;
2848
2849 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002850
2851finished:
James Smart858c9f62007-06-17 19:56:39 -05002852 spin_unlock_irq(shost->host_lock);
2853 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002854}
2855
James Smarte59058c2008-08-24 21:49:00 -04002856/**
James Smart3621a712009-04-06 18:47:14 -04002857 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002858 * @shost: pointer to SCSI host data structure.
2859 *
2860 * This routine initializes a given SCSI host attributes on a FC port. The
2861 * SCSI host can be either on top of a physical port or a virtual port.
2862 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002863void lpfc_host_attrib_init(struct Scsi_Host *shost)
2864{
2865 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2866 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002867 /*
James Smart2e0fef82007-06-17 19:56:36 -05002868 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002869 */
2870
James Smart2e0fef82007-06-17 19:56:36 -05002871 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2872 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002873 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2874
2875 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002876 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002877 fc_host_supported_fc4s(shost)[2] = 1;
2878 fc_host_supported_fc4s(shost)[7] = 1;
2879
James Smart92d7f7b2007-06-17 19:56:38 -05002880 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2881 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002882
2883 fc_host_supported_speeds(shost) = 0;
2884 if (phba->lmt & LMT_10Gb)
2885 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002886 if (phba->lmt & LMT_8Gb)
2887 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002888 if (phba->lmt & LMT_4Gb)
2889 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2890 if (phba->lmt & LMT_2Gb)
2891 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2892 if (phba->lmt & LMT_1Gb)
2893 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2894
2895 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002896 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2897 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002898
Mike Christie0af5d702010-09-15 16:52:31 -05002899 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
2900
James Smart47a86172007-04-25 09:53:22 -04002901 /* This value is also unchanging */
2902 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002903 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002904 fc_host_active_fc4s(shost)[2] = 1;
2905 fc_host_active_fc4s(shost)[7] = 1;
2906
James Smart92d7f7b2007-06-17 19:56:38 -05002907 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002908 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002909 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002910 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002911}
dea31012005-04-17 16:05:31 -05002912
James Smarte59058c2008-08-24 21:49:00 -04002913/**
James Smartda0436e2009-05-22 14:51:39 -04002914 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002915 * @phba: pointer to lpfc hba data structure.
2916 *
James Smartda0436e2009-05-22 14:51:39 -04002917 * This routine is invoked to stop an SLI3 device port, it stops the device
2918 * from generating interrupts and stops the device driver's timers for the
2919 * device.
James Smarte59058c2008-08-24 21:49:00 -04002920 **/
James Smartdb2378e2008-02-08 18:49:51 -05002921static void
James Smartda0436e2009-05-22 14:51:39 -04002922lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002923{
James Smartda0436e2009-05-22 14:51:39 -04002924 /* Clear all interrupt enable conditions */
2925 writel(0, phba->HCregaddr);
2926 readl(phba->HCregaddr); /* flush */
2927 /* Clear all pending interrupts */
2928 writel(0xffffffff, phba->HAregaddr);
2929 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002930
James Smartda0436e2009-05-22 14:51:39 -04002931 /* Reset some HBA SLI setup states */
2932 lpfc_stop_hba_timers(phba);
2933 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002934}
2935
James Smarte59058c2008-08-24 21:49:00 -04002936/**
James Smartda0436e2009-05-22 14:51:39 -04002937 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002938 * @phba: pointer to lpfc hba data structure.
2939 *
James Smartda0436e2009-05-22 14:51:39 -04002940 * This routine is invoked to stop an SLI4 device port, it stops the device
2941 * from generating interrupts and stops the device driver's timers for the
2942 * device.
2943 **/
2944static void
2945lpfc_stop_port_s4(struct lpfc_hba *phba)
2946{
2947 /* Reset some HBA SLI4 setup states */
2948 lpfc_stop_hba_timers(phba);
2949 phba->pport->work_port_events = 0;
2950 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04002951}
2952
2953/**
2954 * lpfc_stop_port - Wrapper function for stopping hba port
2955 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002956 *
James Smartda0436e2009-05-22 14:51:39 -04002957 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2958 * the API jump table function pointer from the lpfc_hba struct.
2959 **/
2960void
2961lpfc_stop_port(struct lpfc_hba *phba)
2962{
2963 phba->lpfc_stop_port(phba);
2964}
2965
2966/**
James Smartecfd03c2010-02-12 14:41:27 -05002967 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2968 * @phba: Pointer to hba for which this call is being executed.
2969 *
2970 * This routine starts the timer waiting for the FCF rediscovery to complete.
2971 **/
2972void
2973lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2974{
2975 unsigned long fcf_redisc_wait_tmo =
2976 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2977 /* Start fcf rediscovery wait period timer */
2978 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2979 spin_lock_irq(&phba->hbalock);
2980 /* Allow action to new fcf asynchronous event */
2981 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2982 /* Mark the FCF rediscovery pending state */
2983 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2984 spin_unlock_irq(&phba->hbalock);
2985}
2986
2987/**
2988 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2989 * @ptr: Map to lpfc_hba data structure pointer.
2990 *
2991 * This routine is invoked when waiting for FCF table rediscover has been
2992 * timed out. If new FCF record(s) has (have) been discovered during the
2993 * wait period, a new FCF event shall be added to the FCOE async event
2994 * list, and then worker thread shall be waked up for processing from the
2995 * worker thread context.
2996 **/
2997void
2998lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2999{
3000 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3001
3002 /* Don't send FCF rediscovery event if timer cancelled */
3003 spin_lock_irq(&phba->hbalock);
3004 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3005 spin_unlock_irq(&phba->hbalock);
3006 return;
3007 }
3008 /* Clear FCF rediscovery timer pending flag */
3009 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3010 /* FCF rediscovery event to worker thread */
3011 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3012 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003013 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003014 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003015 /* wake up worker thread */
3016 lpfc_worker_wake_up(phba);
3017}
3018
3019/**
James Smartda0436e2009-05-22 14:51:39 -04003020 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3021 * @phba: pointer to lpfc hba data structure.
3022 * @acqe_link: pointer to the async link completion queue entry.
3023 *
3024 * This routine is to parse the SLI4 link-attention link fault code and
3025 * translate it into the base driver's read link attention mailbox command
3026 * status.
3027 *
3028 * Return: Link-attention status in terms of base driver's coding.
3029 **/
3030static uint16_t
3031lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3032 struct lpfc_acqe_link *acqe_link)
3033{
3034 uint16_t latt_fault;
3035
3036 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3037 case LPFC_ASYNC_LINK_FAULT_NONE:
3038 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3039 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3040 latt_fault = 0;
3041 break;
3042 default:
3043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3044 "0398 Invalid link fault code: x%x\n",
3045 bf_get(lpfc_acqe_link_fault, acqe_link));
3046 latt_fault = MBXERR_ERROR;
3047 break;
3048 }
3049 return latt_fault;
3050}
3051
3052/**
3053 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3054 * @phba: pointer to lpfc hba data structure.
3055 * @acqe_link: pointer to the async link completion queue entry.
3056 *
3057 * This routine is to parse the SLI4 link attention type and translate it
3058 * into the base driver's link attention type coding.
3059 *
3060 * Return: Link attention type in terms of base driver's coding.
3061 **/
3062static uint8_t
3063lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3064 struct lpfc_acqe_link *acqe_link)
3065{
3066 uint8_t att_type;
3067
3068 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3069 case LPFC_ASYNC_LINK_STATUS_DOWN:
3070 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003071 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003072 break;
3073 case LPFC_ASYNC_LINK_STATUS_UP:
3074 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003075 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003076 break;
3077 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003078 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003079 break;
3080 default:
3081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3082 "0399 Invalid link attention type: x%x\n",
3083 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003084 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003085 break;
3086 }
3087 return att_type;
3088}
3089
3090/**
3091 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3092 * @phba: pointer to lpfc hba data structure.
3093 * @acqe_link: pointer to the async link completion queue entry.
3094 *
3095 * This routine is to parse the SLI4 link-attention link speed and translate
3096 * it into the base driver's link-attention link speed coding.
3097 *
3098 * Return: Link-attention link speed in terms of base driver's coding.
3099 **/
3100static uint8_t
3101lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3102 struct lpfc_acqe_link *acqe_link)
3103{
3104 uint8_t link_speed;
3105
3106 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3107 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003108 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003109 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003110 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003111 break;
3112 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003113 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003114 break;
3115 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003116 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003117 break;
3118 default:
3119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3120 "0483 Invalid link-attention link speed: x%x\n",
3121 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003122 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003123 break;
3124 }
3125 return link_speed;
3126}
3127
3128/**
James Smart70f3c072010-12-15 17:57:33 -05003129 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003130 * @phba: pointer to lpfc hba data structure.
3131 * @acqe_link: pointer to the async link completion queue entry.
3132 *
James Smart70f3c072010-12-15 17:57:33 -05003133 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003134 **/
3135static void
3136lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3137 struct lpfc_acqe_link *acqe_link)
3138{
3139 struct lpfc_dmabuf *mp;
3140 LPFC_MBOXQ_t *pmb;
3141 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003142 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003143 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003144 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003145
3146 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003147 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003148 return;
James Smart32b97932009-07-19 10:01:21 -04003149 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003150 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3151 if (!pmb) {
3152 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3153 "0395 The mboxq allocation failed\n");
3154 return;
3155 }
3156 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3157 if (!mp) {
3158 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3159 "0396 The lpfc_dmabuf allocation failed\n");
3160 goto out_free_pmb;
3161 }
3162 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3163 if (!mp->virt) {
3164 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3165 "0397 The mbuf allocation failed\n");
3166 goto out_free_dmabuf;
3167 }
3168
3169 /* Cleanup any outstanding ELS commands */
3170 lpfc_els_flush_all_cmd(phba);
3171
3172 /* Block ELS IOCBs until we have done process link event */
3173 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3174
3175 /* Update link event statistics */
3176 phba->sli.slistat.link_event++;
3177
James Smart76a95d72010-11-20 23:11:48 -05003178 /* Create lpfc_handle_latt mailbox command from link ACQE */
3179 lpfc_read_topology(phba, pmb, mp);
3180 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003181 pmb->vport = phba->pport;
3182
James Smartda0436e2009-05-22 14:51:39 -04003183 /* Keep the link status for extra SLI4 state machine reference */
3184 phba->sli4_hba.link_state.speed =
3185 bf_get(lpfc_acqe_link_speed, acqe_link);
3186 phba->sli4_hba.link_state.duplex =
3187 bf_get(lpfc_acqe_link_duplex, acqe_link);
3188 phba->sli4_hba.link_state.status =
3189 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003190 phba->sli4_hba.link_state.type =
3191 bf_get(lpfc_acqe_link_type, acqe_link);
3192 phba->sli4_hba.link_state.number =
3193 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003194 phba->sli4_hba.link_state.fault =
3195 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003196 phba->sli4_hba.link_state.logical_speed =
James Smart70f3c072010-12-15 17:57:33 -05003197 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3198 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3199 "2900 Async FCoE Link event - Speed:%dGBit duplex:x%x "
3200 "LA Type:x%x Port Type:%d Port Number:%d Logical "
3201 "speed:%dMbps Fault:%d\n",
3202 phba->sli4_hba.link_state.speed,
3203 phba->sli4_hba.link_state.topology,
3204 phba->sli4_hba.link_state.status,
3205 phba->sli4_hba.link_state.type,
3206 phba->sli4_hba.link_state.number,
3207 phba->sli4_hba.link_state.logical_speed * 10,
3208 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003209 /*
3210 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3211 * topology info. Note: Optional for non FC-AL ports.
3212 */
3213 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3214 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3215 if (rc == MBX_NOT_FINISHED)
3216 goto out_free_dmabuf;
3217 return;
3218 }
3219 /*
3220 * For FCoE Mode: fill in all the topology information we need and call
3221 * the READ_TOPOLOGY completion routine to continue without actually
3222 * sending the READ_TOPOLOGY mailbox command to the port.
3223 */
3224 /* Parse and translate status field */
3225 mb = &pmb->u.mb;
3226 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3227
3228 /* Parse and translate link attention fields */
3229 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3230 la->eventTag = acqe_link->event_tag;
3231 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3232 bf_set(lpfc_mbx_read_top_link_spd, la,
3233 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3234
3235 /* Fake the the following irrelvant fields */
3236 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3237 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3238 bf_set(lpfc_mbx_read_top_il, la, 0);
3239 bf_set(lpfc_mbx_read_top_pb, la, 0);
3240 bf_set(lpfc_mbx_read_top_fa, la, 0);
3241 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003242
3243 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003244 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003245
3246 return;
3247
3248out_free_dmabuf:
3249 kfree(mp);
3250out_free_pmb:
3251 mempool_free(pmb, phba->mbox_mem_pool);
3252}
3253
3254/**
James Smart70f3c072010-12-15 17:57:33 -05003255 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3256 * @phba: pointer to lpfc hba data structure.
3257 * @acqe_fc: pointer to the async fc completion queue entry.
3258 *
3259 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3260 * that the event was received and then issue a read_topology mailbox command so
3261 * that the rest of the driver will treat it the same as SLI3.
3262 **/
3263static void
3264lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3265{
3266 struct lpfc_dmabuf *mp;
3267 LPFC_MBOXQ_t *pmb;
3268 int rc;
3269
3270 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3271 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3273 "2895 Non FC link Event detected.(%d)\n",
3274 bf_get(lpfc_trailer_type, acqe_fc));
3275 return;
3276 }
3277 /* Keep the link status for extra SLI4 state machine reference */
3278 phba->sli4_hba.link_state.speed =
3279 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3280 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3281 phba->sli4_hba.link_state.topology =
3282 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3283 phba->sli4_hba.link_state.status =
3284 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3285 phba->sli4_hba.link_state.type =
3286 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3287 phba->sli4_hba.link_state.number =
3288 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3289 phba->sli4_hba.link_state.fault =
3290 bf_get(lpfc_acqe_link_fault, acqe_fc);
3291 phba->sli4_hba.link_state.logical_speed =
3292 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3293 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3294 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3295 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3296 "%dMbps Fault:%d\n",
3297 phba->sli4_hba.link_state.speed,
3298 phba->sli4_hba.link_state.topology,
3299 phba->sli4_hba.link_state.status,
3300 phba->sli4_hba.link_state.type,
3301 phba->sli4_hba.link_state.number,
3302 phba->sli4_hba.link_state.logical_speed * 10,
3303 phba->sli4_hba.link_state.fault);
3304 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3305 if (!pmb) {
3306 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3307 "2897 The mboxq allocation failed\n");
3308 return;
3309 }
3310 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3311 if (!mp) {
3312 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3313 "2898 The lpfc_dmabuf allocation failed\n");
3314 goto out_free_pmb;
3315 }
3316 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3317 if (!mp->virt) {
3318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3319 "2899 The mbuf allocation failed\n");
3320 goto out_free_dmabuf;
3321 }
3322
3323 /* Cleanup any outstanding ELS commands */
3324 lpfc_els_flush_all_cmd(phba);
3325
3326 /* Block ELS IOCBs until we have done process link event */
3327 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3328
3329 /* Update link event statistics */
3330 phba->sli.slistat.link_event++;
3331
3332 /* Create lpfc_handle_latt mailbox command from link ACQE */
3333 lpfc_read_topology(phba, pmb, mp);
3334 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3335 pmb->vport = phba->pport;
3336
3337 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3338 if (rc == MBX_NOT_FINISHED)
3339 goto out_free_dmabuf;
3340 return;
3341
3342out_free_dmabuf:
3343 kfree(mp);
3344out_free_pmb:
3345 mempool_free(pmb, phba->mbox_mem_pool);
3346}
3347
3348/**
3349 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3350 * @phba: pointer to lpfc hba data structure.
3351 * @acqe_fc: pointer to the async SLI completion queue entry.
3352 *
3353 * This routine is to handle the SLI4 asynchronous SLI events.
3354 **/
3355static void
3356lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3357{
3358 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3359 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3360 "x%08x SLI Event Type:%d",
3361 acqe_sli->event_data1, acqe_sli->event_data2,
3362 bf_get(lpfc_trailer_type, acqe_sli));
3363 return;
3364}
3365
3366/**
James Smartfc2b9892010-02-26 14:15:29 -05003367 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3368 * @vport: pointer to vport data structure.
3369 *
3370 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3371 * response to a CVL event.
3372 *
3373 * Return the pointer to the ndlp with the vport if successful, otherwise
3374 * return NULL.
3375 **/
3376static struct lpfc_nodelist *
3377lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3378{
3379 struct lpfc_nodelist *ndlp;
3380 struct Scsi_Host *shost;
3381 struct lpfc_hba *phba;
3382
3383 if (!vport)
3384 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003385 phba = vport->phba;
3386 if (!phba)
3387 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003388 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3389 if (!ndlp) {
3390 /* Cannot find existing Fabric ndlp, so allocate a new one */
3391 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3392 if (!ndlp)
3393 return 0;
3394 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3395 /* Set the node type */
3396 ndlp->nlp_type |= NLP_FABRIC;
3397 /* Put ndlp onto node list */
3398 lpfc_enqueue_node(vport, ndlp);
3399 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3400 /* re-setup ndlp without removing from node list */
3401 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3402 if (!ndlp)
3403 return 0;
3404 }
James Smart63e801c2010-11-20 23:14:19 -05003405 if ((phba->pport->port_state < LPFC_FLOGI) &&
3406 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003407 return NULL;
3408 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003409 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3410 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003411 return NULL;
3412 shost = lpfc_shost_from_vport(vport);
3413 if (!shost)
3414 return NULL;
3415 lpfc_linkdown_port(vport);
3416 lpfc_cleanup_pending_mbox(vport);
3417 spin_lock_irq(shost->host_lock);
3418 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3419 spin_unlock_irq(shost->host_lock);
3420
3421 return ndlp;
3422}
3423
3424/**
3425 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3426 * @vport: pointer to lpfc hba data structure.
3427 *
3428 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3429 * response to a FCF dead event.
3430 **/
3431static void
3432lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3433{
3434 struct lpfc_vport **vports;
3435 int i;
3436
3437 vports = lpfc_create_vport_work_array(phba);
3438 if (vports)
3439 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3440 lpfc_sli4_perform_vport_cvl(vports[i]);
3441 lpfc_destroy_vport_work_array(phba, vports);
3442}
3443
3444/**
James Smart76a95d72010-11-20 23:11:48 -05003445 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04003446 * @phba: pointer to lpfc hba data structure.
3447 * @acqe_link: pointer to the async fcoe completion queue entry.
3448 *
3449 * This routine is to handle the SLI4 asynchronous fcoe event.
3450 **/
3451static void
James Smart76a95d72010-11-20 23:11:48 -05003452lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05003453 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04003454{
James Smart70f3c072010-12-15 17:57:33 -05003455 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003456 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003457 struct lpfc_vport *vport;
3458 struct lpfc_nodelist *ndlp;
3459 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003460 int active_vlink_present;
3461 struct lpfc_vport **vports;
3462 int i;
James Smartda0436e2009-05-22 14:51:39 -04003463
James Smart70f3c072010-12-15 17:57:33 -05003464 phba->fc_eventTag = acqe_fip->event_tag;
3465 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003466 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05003467 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3468 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3469 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04003470 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3471 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003472 "2546 New FCF event, evt_tag:x%x, "
3473 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003474 acqe_fip->event_tag,
3475 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04003476 else
3477 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3478 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003479 "2788 FCF param modified event, "
3480 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003481 acqe_fip->event_tag,
3482 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04003483 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003484 /*
3485 * During period of FCF discovery, read the FCF
3486 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04003487 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05003488 */
3489 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3490 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003491 "2779 Read FCF (x%x) for updating "
3492 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05003493 acqe_fip->index);
3494 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003495 }
James Smart38b92ef2010-08-04 16:11:39 -04003496
3497 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003498 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003499 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04003500 spin_unlock_irq(&phba->hbalock);
3501 break;
3502 }
3503 /* If fast FCF failover rescan event is pending, do nothing */
3504 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3505 spin_unlock_irq(&phba->hbalock);
3506 break;
3507 }
3508
3509 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003510 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3511 spin_unlock_irq(&phba->hbalock);
3512 break;
3513 }
3514 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003515
James Smart0c9ab6f2010-02-26 14:15:57 -05003516 /* Otherwise, scan the entire FCF table and re-discover SAN */
3517 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003518 "2770 Start FCF table scan per async FCF "
3519 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003520 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003521 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3522 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003523 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003524 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3525 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04003526 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003527 break;
3528
James Smart70f3c072010-12-15 17:57:33 -05003529 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04003530 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003531 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003532 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3533 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003534 break;
3535
James Smart70f3c072010-12-15 17:57:33 -05003536 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003537 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003538 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05003539 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04003540 /*
3541 * If we are in the middle of FCF failover process, clear
3542 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04003543 */
James Smartfc2b9892010-02-26 14:15:29 -05003544 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003545 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003546 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003547 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05003548 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003549 break;
3550 }
James Smart38b92ef2010-08-04 16:11:39 -04003551 spin_unlock_irq(&phba->hbalock);
3552
3553 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05003554 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04003555 break;
3556
3557 /*
3558 * Otherwise, request the port to rediscover the entire FCF
3559 * table for a fast recovery from case that the current FCF
3560 * is no longer valid as we are not in the middle of FCF
3561 * failover process already.
3562 */
3563 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05003564 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003565 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003566 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003567
James Smart0c9ab6f2010-02-26 14:15:57 -05003568 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3569 "2771 Start FCF fast failover process due to "
3570 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05003571 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003572 rc = lpfc_sli4_redisc_fcf_table(phba);
3573 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003574 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3575 LOG_DISCOVERY,
3576 "2772 Issue FCF rediscover mabilbox "
3577 "command failed, fail through to FCF "
3578 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003579 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003580 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003581 spin_unlock_irq(&phba->hbalock);
3582 /*
3583 * Last resort will fail over by treating this
3584 * as a link down to FCF registration.
3585 */
3586 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003587 } else {
3588 /* Reset FCF roundrobin bmask for new discovery */
3589 memset(phba->fcf.fcf_rr_bmask, 0,
3590 sizeof(*phba->fcf.fcf_rr_bmask));
3591 /*
3592 * Handling fast FCF failover to a DEAD FCF event is
3593 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05003594 */
3595 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003596 }
James Smartda0436e2009-05-22 14:51:39 -04003597 break;
James Smart70f3c072010-12-15 17:57:33 -05003598 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003599 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003600 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05003601 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6669f9b2009-10-02 15:16:45 -04003602 vport = lpfc_find_vport_by_vpid(phba,
James Smart70f3c072010-12-15 17:57:33 -05003603 acqe_fip->index - phba->vpi_base);
James Smartfc2b9892010-02-26 14:15:29 -05003604 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003605 if (!ndlp)
3606 break;
James Smart695a8142010-01-26 23:08:03 -05003607 active_vlink_present = 0;
3608
3609 vports = lpfc_create_vport_work_array(phba);
3610 if (vports) {
3611 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3612 i++) {
3613 if ((!(vports[i]->fc_flag &
3614 FC_VPORT_CVL_RCVD)) &&
3615 (vports[i]->port_state > LPFC_FDISC)) {
3616 active_vlink_present = 1;
3617 break;
3618 }
3619 }
3620 lpfc_destroy_vport_work_array(phba, vports);
3621 }
3622
3623 if (active_vlink_present) {
3624 /*
3625 * If there are other active VLinks present,
3626 * re-instantiate the Vlink using FDISC.
3627 */
James Smart6669f9b2009-10-02 15:16:45 -04003628 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003629 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003630 spin_lock_irq(shost->host_lock);
3631 ndlp->nlp_flag |= NLP_DELAY_TMO;
3632 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003633 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3634 vport->port_state = LPFC_FDISC;
3635 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003636 /*
3637 * Otherwise, we request port to rediscover
3638 * the entire FCF table for a fast recovery
3639 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003640 * is no longer valid if we are not already
3641 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003642 */
James Smartfc2b9892010-02-26 14:15:29 -05003643 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003644 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003645 spin_unlock_irq(&phba->hbalock);
3646 break;
3647 }
3648 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003649 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003650 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003651 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3652 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003653 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05003654 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003655 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003656 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003657 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3658 LOG_DISCOVERY,
3659 "2774 Issue FCF rediscover "
3660 "mabilbox command failed, "
3661 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003662 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003663 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003664 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003665 /*
3666 * Last resort will be re-try on the
3667 * the current registered FCF entry.
3668 */
3669 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003670 } else
3671 /*
3672 * Reset FCF roundrobin bmask for new
3673 * discovery.
3674 */
3675 memset(phba->fcf.fcf_rr_bmask, 0,
3676 sizeof(*phba->fcf.fcf_rr_bmask));
James Smart6669f9b2009-10-02 15:16:45 -04003677 }
3678 break;
James Smartda0436e2009-05-22 14:51:39 -04003679 default:
3680 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3681 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05003682 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003683 break;
3684 }
3685}
3686
3687/**
3688 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3689 * @phba: pointer to lpfc hba data structure.
3690 * @acqe_link: pointer to the async dcbx completion queue entry.
3691 *
3692 * This routine is to handle the SLI4 asynchronous dcbx event.
3693 **/
3694static void
3695lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3696 struct lpfc_acqe_dcbx *acqe_dcbx)
3697{
James Smart4d9ab992009-10-02 15:16:39 -04003698 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003699 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3700 "0290 The SLI4 DCBX asynchronous event is not "
3701 "handled yet\n");
3702}
3703
3704/**
James Smartb19a0612010-04-06 14:48:51 -04003705 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3706 * @phba: pointer to lpfc hba data structure.
3707 * @acqe_link: pointer to the async grp5 completion queue entry.
3708 *
3709 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3710 * is an asynchronous notified of a logical link speed change. The Port
3711 * reports the logical link speed in units of 10Mbps.
3712 **/
3713static void
3714lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3715 struct lpfc_acqe_grp5 *acqe_grp5)
3716{
3717 uint16_t prev_ll_spd;
3718
3719 phba->fc_eventTag = acqe_grp5->event_tag;
3720 phba->fcoe_eventtag = acqe_grp5->event_tag;
3721 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3722 phba->sli4_hba.link_state.logical_speed =
3723 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3724 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3725 "2789 GRP5 Async Event: Updating logical link speed "
3726 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3727 (phba->sli4_hba.link_state.logical_speed*10));
3728}
3729
3730/**
James Smartda0436e2009-05-22 14:51:39 -04003731 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3732 * @phba: pointer to lpfc hba data structure.
3733 *
3734 * This routine is invoked by the worker thread to process all the pending
3735 * SLI4 asynchronous events.
3736 **/
3737void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3738{
3739 struct lpfc_cq_event *cq_event;
3740
3741 /* First, declare the async event has been handled */
3742 spin_lock_irq(&phba->hbalock);
3743 phba->hba_flag &= ~ASYNC_EVENT;
3744 spin_unlock_irq(&phba->hbalock);
3745 /* Now, handle all the async events */
3746 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3747 /* Get the first event from the head of the event queue */
3748 spin_lock_irq(&phba->hbalock);
3749 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3750 cq_event, struct lpfc_cq_event, list);
3751 spin_unlock_irq(&phba->hbalock);
3752 /* Process the asynchronous event */
3753 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3754 case LPFC_TRAILER_CODE_LINK:
3755 lpfc_sli4_async_link_evt(phba,
3756 &cq_event->cqe.acqe_link);
3757 break;
3758 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05003759 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003760 break;
3761 case LPFC_TRAILER_CODE_DCBX:
3762 lpfc_sli4_async_dcbx_evt(phba,
3763 &cq_event->cqe.acqe_dcbx);
3764 break;
James Smartb19a0612010-04-06 14:48:51 -04003765 case LPFC_TRAILER_CODE_GRP5:
3766 lpfc_sli4_async_grp5_evt(phba,
3767 &cq_event->cqe.acqe_grp5);
3768 break;
James Smart70f3c072010-12-15 17:57:33 -05003769 case LPFC_TRAILER_CODE_FC:
3770 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
3771 break;
3772 case LPFC_TRAILER_CODE_SLI:
3773 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
3774 break;
James Smartda0436e2009-05-22 14:51:39 -04003775 default:
3776 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3777 "1804 Invalid asynchrous event code: "
3778 "x%x\n", bf_get(lpfc_trailer_code,
3779 &cq_event->cqe.mcqe_cmpl));
3780 break;
3781 }
3782 /* Free the completion event processed to the free pool */
3783 lpfc_sli4_cq_event_release(phba, cq_event);
3784 }
3785}
3786
3787/**
James Smartecfd03c2010-02-12 14:41:27 -05003788 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3789 * @phba: pointer to lpfc hba data structure.
3790 *
3791 * This routine is invoked by the worker thread to process FCF table
3792 * rediscovery pending completion event.
3793 **/
3794void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3795{
3796 int rc;
3797
3798 spin_lock_irq(&phba->hbalock);
3799 /* Clear FCF rediscovery timeout event */
3800 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3801 /* Clear driver fast failover FCF record flag */
3802 phba->fcf.failover_rec.flag = 0;
3803 /* Set state for FCF fast failover */
3804 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3805 spin_unlock_irq(&phba->hbalock);
3806
3807 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05003808 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003809 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003810 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05003811 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003812 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3813 "2747 Issue FCF scan read FCF mailbox "
3814 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05003815}
3816
3817/**
James Smartda0436e2009-05-22 14:51:39 -04003818 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3819 * @phba: pointer to lpfc hba data structure.
3820 * @dev_grp: The HBA PCI-Device group number.
3821 *
3822 * This routine is invoked to set up the per HBA PCI-Device group function
3823 * API jump table entries.
3824 *
3825 * Return: 0 if success, otherwise -ENODEV
3826 **/
3827int
3828lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003829{
3830 int rc;
3831
James Smartda0436e2009-05-22 14:51:39 -04003832 /* Set up lpfc PCI-device group */
3833 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003834
James Smartda0436e2009-05-22 14:51:39 -04003835 /* The LPFC_PCI_DEV_OC uses SLI4 */
3836 if (dev_grp == LPFC_PCI_DEV_OC)
3837 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003838
James Smartda0436e2009-05-22 14:51:39 -04003839 /* Set up device INIT API function jump table */
3840 rc = lpfc_init_api_table_setup(phba, dev_grp);
3841 if (rc)
3842 return -ENODEV;
3843 /* Set up SCSI API function jump table */
3844 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3845 if (rc)
3846 return -ENODEV;
3847 /* Set up SLI API function jump table */
3848 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3849 if (rc)
3850 return -ENODEV;
3851 /* Set up MBOX API function jump table */
3852 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3853 if (rc)
3854 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003855
James Smartda0436e2009-05-22 14:51:39 -04003856 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003857}
3858
3859/**
James Smart3621a712009-04-06 18:47:14 -04003860 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003861 * @phba: pointer to lpfc hba data structure.
3862 * @intr_mode: active interrupt mode adopted.
3863 *
3864 * This routine it invoked to log the currently used active interrupt mode
3865 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003866 **/
3867static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003868{
3869 switch (intr_mode) {
3870 case 0:
3871 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3872 "0470 Enable INTx interrupt mode.\n");
3873 break;
3874 case 1:
3875 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3876 "0481 Enabled MSI interrupt mode.\n");
3877 break;
3878 case 2:
3879 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3880 "0480 Enabled MSI-X interrupt mode.\n");
3881 break;
3882 default:
3883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3884 "0482 Illegal interrupt mode.\n");
3885 break;
3886 }
3887 return;
3888}
3889
James Smart5b75da22008-12-04 22:39:35 -05003890/**
James Smart3772a992009-05-22 14:50:54 -04003891 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003892 * @phba: pointer to lpfc hba data structure.
3893 *
James Smart3772a992009-05-22 14:50:54 -04003894 * This routine is invoked to enable the PCI device that is common to all
3895 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003896 *
3897 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003898 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003899 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003900 **/
James Smart3772a992009-05-22 14:50:54 -04003901static int
3902lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003903{
James Smart3772a992009-05-22 14:50:54 -04003904 struct pci_dev *pdev;
3905 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003906
James Smart3772a992009-05-22 14:50:54 -04003907 /* Obtain PCI device reference */
3908 if (!phba->pcidev)
3909 goto out_error;
3910 else
3911 pdev = phba->pcidev;
3912 /* Select PCI BARs */
3913 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3914 /* Enable PCI device */
3915 if (pci_enable_device_mem(pdev))
3916 goto out_error;
3917 /* Request PCI resource for the device */
3918 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3919 goto out_disable_device;
3920 /* Set up device as PCI master and save state for EEH */
3921 pci_set_master(pdev);
3922 pci_try_set_mwi(pdev);
3923 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003924
James Smart3772a992009-05-22 14:50:54 -04003925 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003926
James Smart3772a992009-05-22 14:50:54 -04003927out_disable_device:
3928 pci_disable_device(pdev);
3929out_error:
3930 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003931}
3932
3933/**
James Smart3772a992009-05-22 14:50:54 -04003934 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003935 * @phba: pointer to lpfc hba data structure.
3936 *
James Smart3772a992009-05-22 14:50:54 -04003937 * This routine is invoked to disable the PCI device that is common to all
3938 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003939 **/
3940static void
James Smart3772a992009-05-22 14:50:54 -04003941lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003942{
James Smart3772a992009-05-22 14:50:54 -04003943 struct pci_dev *pdev;
3944 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003945
James Smart3772a992009-05-22 14:50:54 -04003946 /* Obtain PCI device reference */
3947 if (!phba->pcidev)
3948 return;
3949 else
3950 pdev = phba->pcidev;
3951 /* Select PCI BARs */
3952 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3953 /* Release PCI resource and disable PCI device */
3954 pci_release_selected_regions(pdev, bars);
3955 pci_disable_device(pdev);
3956 /* Null out PCI private reference to driver */
3957 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003958
3959 return;
3960}
3961
3962/**
James Smart3772a992009-05-22 14:50:54 -04003963 * lpfc_reset_hba - Reset a hba
3964 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003965 *
James Smart3772a992009-05-22 14:50:54 -04003966 * This routine is invoked to reset a hba device. It brings the HBA
3967 * offline, performs a board restart, and then brings the board back
3968 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3969 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003970 **/
James Smart3772a992009-05-22 14:50:54 -04003971void
3972lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003973{
James Smart3772a992009-05-22 14:50:54 -04003974 /* If resets are disabled then set error state and return. */
3975 if (!phba->cfg_enable_hba_reset) {
3976 phba->link_state = LPFC_HBA_ERROR;
3977 return;
3978 }
3979 lpfc_offline_prep(phba);
3980 lpfc_offline(phba);
3981 lpfc_sli_brdrestart(phba);
3982 lpfc_online(phba);
3983 lpfc_unblock_mgmt_io(phba);
3984}
dea31012005-04-17 16:05:31 -05003985
James Smart3772a992009-05-22 14:50:54 -04003986/**
3987 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3988 * @phba: pointer to lpfc hba data structure.
3989 *
3990 * This routine is invoked to set up the driver internal resources specific to
3991 * support the SLI-3 HBA device it attached to.
3992 *
3993 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003994 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003995 * other values - error
3996 **/
3997static int
3998lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3999{
4000 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004001
James Smart3772a992009-05-22 14:50:54 -04004002 /*
4003 * Initialize timers used by driver
4004 */
dea31012005-04-17 16:05:31 -05004005
James Smart3772a992009-05-22 14:50:54 -04004006 /* Heartbeat timer */
4007 init_timer(&phba->hb_tmofunc);
4008 phba->hb_tmofunc.function = lpfc_hb_timeout;
4009 phba->hb_tmofunc.data = (unsigned long)phba;
4010
4011 psli = &phba->sli;
4012 /* MBOX heartbeat timer */
4013 init_timer(&psli->mbox_tmo);
4014 psli->mbox_tmo.function = lpfc_mbox_timeout;
4015 psli->mbox_tmo.data = (unsigned long) phba;
4016 /* FCP polling mode timer */
4017 init_timer(&phba->fcp_poll_timer);
4018 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4019 phba->fcp_poll_timer.data = (unsigned long) phba;
4020 /* Fabric block timer */
4021 init_timer(&phba->fabric_block_timer);
4022 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4023 phba->fabric_block_timer.data = (unsigned long) phba;
4024 /* EA polling mode timer */
4025 init_timer(&phba->eratt_poll);
4026 phba->eratt_poll.function = lpfc_poll_eratt;
4027 phba->eratt_poll.data = (unsigned long) phba;
4028
4029 /* Host attention work mask setup */
4030 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4031 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4032
4033 /* Get all the module params for configuring this host */
4034 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004035 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4036 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4037 /* check for menlo minimum sg count */
4038 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4039 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4040 }
4041
James Smart3772a992009-05-22 14:50:54 -04004042 /*
4043 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4044 * used to create the sg_dma_buf_pool must be dynamically calculated.
4045 * 2 segments are added since the IOCB needs a command and response bde.
4046 */
4047 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4048 sizeof(struct fcp_rsp) +
4049 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4050
4051 if (phba->cfg_enable_bg) {
4052 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4053 phba->cfg_sg_dma_buf_size +=
4054 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4055 }
4056
4057 /* Also reinitialize the host templates with new values. */
4058 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4059 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4060
4061 phba->max_vpi = LPFC_MAX_VPI;
4062 /* This will be set to correct value after config_port mbox */
4063 phba->max_vports = 0;
4064
4065 /*
4066 * Initialize the SLI Layer to run with lpfc HBAs.
4067 */
4068 lpfc_sli_setup(phba);
4069 lpfc_sli_queue_setup(phba);
4070
4071 /* Allocate device driver memory */
4072 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4073 return -ENOMEM;
4074
4075 return 0;
4076}
4077
4078/**
4079 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4080 * @phba: pointer to lpfc hba data structure.
4081 *
4082 * This routine is invoked to unset the driver internal resources set up
4083 * specific for supporting the SLI-3 HBA device it attached to.
4084 **/
4085static void
4086lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4087{
4088 /* Free device driver memory allocated */
4089 lpfc_mem_free_all(phba);
4090
4091 return;
4092}
4093
4094/**
James Smartda0436e2009-05-22 14:51:39 -04004095 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4096 * @phba: pointer to lpfc hba data structure.
4097 *
4098 * This routine is invoked to set up the driver internal resources specific to
4099 * support the SLI-4 HBA device it attached to.
4100 *
4101 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004102 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004103 * other values - error
4104 **/
4105static int
4106lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4107{
4108 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004109 LPFC_MBOXQ_t *mboxq;
4110 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4111 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4112 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004113 int longs, sli_family;
James Smartda0436e2009-05-22 14:51:39 -04004114
4115 /* Before proceed, wait for POST done and device ready */
4116 rc = lpfc_sli4_post_status_check(phba);
4117 if (rc)
4118 return -ENODEV;
4119
4120 /*
4121 * Initialize timers used by driver
4122 */
4123
4124 /* Heartbeat timer */
4125 init_timer(&phba->hb_tmofunc);
4126 phba->hb_tmofunc.function = lpfc_hb_timeout;
4127 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004128 init_timer(&phba->rrq_tmr);
4129 phba->rrq_tmr.function = lpfc_rrq_timeout;
4130 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004131
4132 psli = &phba->sli;
4133 /* MBOX heartbeat timer */
4134 init_timer(&psli->mbox_tmo);
4135 psli->mbox_tmo.function = lpfc_mbox_timeout;
4136 psli->mbox_tmo.data = (unsigned long) phba;
4137 /* Fabric block timer */
4138 init_timer(&phba->fabric_block_timer);
4139 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4140 phba->fabric_block_timer.data = (unsigned long) phba;
4141 /* EA polling mode timer */
4142 init_timer(&phba->eratt_poll);
4143 phba->eratt_poll.function = lpfc_poll_eratt;
4144 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004145 /* FCF rediscover timer */
4146 init_timer(&phba->fcf.redisc_wait);
4147 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4148 phba->fcf.redisc_wait.data = (unsigned long)phba;
4149
James Smartda0436e2009-05-22 14:51:39 -04004150 /*
4151 * We need to do a READ_CONFIG mailbox command here before
4152 * calling lpfc_get_cfgparam. For VFs this will report the
4153 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4154 * All of the resources allocated
4155 * for this Port are tied to these values.
4156 */
4157 /* Get all the module params for configuring this host */
4158 lpfc_get_cfgparam(phba);
4159 phba->max_vpi = LPFC_MAX_VPI;
4160 /* This will be set to correct value after the read_config mbox */
4161 phba->max_vports = 0;
4162
4163 /* Program the default value of vlan_id and fc_map */
4164 phba->valid_vlan = 0;
4165 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4166 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4167 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4168
4169 /*
4170 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4171 * used to create the sg_dma_buf_pool must be dynamically calculated.
4172 * 2 segments are added since the IOCB needs a command and response bde.
4173 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004174 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004175 */
James Smart28baac72010-02-12 14:42:03 -05004176 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4177 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004178
4179 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4180 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4181 switch (sli_family) {
4182 case LPFC_SLI_INTF_FAMILY_BE2:
4183 case LPFC_SLI_INTF_FAMILY_BE3:
4184 /* There is a single hint for BE - 2 pages per BPL. */
4185 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4186 LPFC_SLI_INTF_SLI_HINT1_1)
4187 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4188 break;
4189 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4190 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4191 default:
4192 break;
4193 }
James Smart28baac72010-02-12 14:42:03 -05004194 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4195 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4196 dma_buf_size = dma_buf_size << 1)
4197 ;
4198 if (dma_buf_size == max_buf_size)
4199 phba->cfg_sg_seg_cnt = (dma_buf_size -
4200 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4201 (2 * sizeof(struct sli4_sge))) /
4202 sizeof(struct sli4_sge);
4203 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004204
4205 /* Initialize buffer queue management fields */
4206 hbq_count = lpfc_sli_hbq_count();
4207 for (i = 0; i < hbq_count; ++i)
4208 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4209 INIT_LIST_HEAD(&phba->rb_pend_list);
4210 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4211 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4212
4213 /*
4214 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4215 */
4216 /* Initialize the Abort scsi buffer list used by driver */
4217 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4218 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4219 /* This abort list used by worker thread */
4220 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4221
4222 /*
4223 * Initialize dirver internal slow-path work queues
4224 */
4225
4226 /* Driver internel slow-path CQ Event pool */
4227 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4228 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004229 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004230 /* Asynchronous event CQ Event work queue list */
4231 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4232 /* Fast-path XRI aborted CQ Event work queue list */
4233 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4234 /* Slow-path XRI aborted CQ Event work queue list */
4235 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4236 /* Receive queue CQ Event work queue list */
4237 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4238
4239 /* Initialize the driver internal SLI layer lists. */
4240 lpfc_sli_setup(phba);
4241 lpfc_sli_queue_setup(phba);
4242
4243 /* Allocate device driver memory */
4244 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4245 if (rc)
4246 return -ENOMEM;
4247
James Smart2fcee4b2010-12-15 17:57:46 -05004248 /* IF Type 2 ports get initialized now. */
4249 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4250 LPFC_SLI_INTF_IF_TYPE_2) {
4251 rc = lpfc_pci_function_reset(phba);
4252 if (unlikely(rc))
4253 return -ENODEV;
4254 }
4255
James Smartda0436e2009-05-22 14:51:39 -04004256 /* Create the bootstrap mailbox command */
4257 rc = lpfc_create_bootstrap_mbox(phba);
4258 if (unlikely(rc))
4259 goto out_free_mem;
4260
4261 /* Set up the host's endian order with the device. */
4262 rc = lpfc_setup_endian_order(phba);
4263 if (unlikely(rc))
4264 goto out_free_bsmbx;
4265
4266 /* Set up the hba's configuration parameters. */
4267 rc = lpfc_sli4_read_config(phba);
4268 if (unlikely(rc))
4269 goto out_free_bsmbx;
4270
James Smart2fcee4b2010-12-15 17:57:46 -05004271 /* IF Type 0 ports get initialized now. */
4272 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4273 LPFC_SLI_INTF_IF_TYPE_0) {
4274 rc = lpfc_pci_function_reset(phba);
4275 if (unlikely(rc))
4276 goto out_free_bsmbx;
4277 }
James Smartda0436e2009-05-22 14:51:39 -04004278
James Smartcb5172e2010-03-15 11:25:07 -04004279 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4280 GFP_KERNEL);
4281 if (!mboxq) {
4282 rc = -ENOMEM;
4283 goto out_free_bsmbx;
4284 }
4285
James Smartfedd3b72011-02-16 12:39:24 -05004286 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004287 lpfc_supported_pages(mboxq);
4288 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004289 if (!rc) {
4290 mqe = &mboxq->u.mqe;
4291 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4292 LPFC_MAX_SUPPORTED_PAGES);
4293 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4294 switch (pn_page[i]) {
4295 case LPFC_SLI4_PARAMETERS:
4296 phba->sli4_hba.pc_sli4_params.supported = 1;
4297 break;
4298 default:
4299 break;
4300 }
4301 }
4302 /* Read the port's SLI4 Parameters capabilities if supported. */
4303 if (phba->sli4_hba.pc_sli4_params.supported)
4304 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4305 if (rc) {
4306 mempool_free(mboxq, phba->mbox_mem_pool);
4307 rc = -EIO;
4308 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04004309 }
4310 }
James Smartfedd3b72011-02-16 12:39:24 -05004311 /*
4312 * Get sli4 parameters that override parameters from Port capabilities.
4313 * If this call fails it is not a critical error so continue loading.
4314 */
4315 lpfc_get_sli4_parameters(phba, mboxq);
James Smartcb5172e2010-03-15 11:25:07 -04004316 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04004317 /* Create all the SLI4 queues */
4318 rc = lpfc_sli4_queue_create(phba);
4319 if (rc)
4320 goto out_free_bsmbx;
4321
4322 /* Create driver internal CQE event pool */
4323 rc = lpfc_sli4_cq_event_pool_create(phba);
4324 if (rc)
4325 goto out_destroy_queue;
4326
4327 /* Initialize and populate the iocb list per host */
4328 rc = lpfc_init_sgl_list(phba);
4329 if (rc) {
4330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4331 "1400 Failed to initialize sgl list.\n");
4332 goto out_destroy_cq_event_pool;
4333 }
4334 rc = lpfc_init_active_sgl_array(phba);
4335 if (rc) {
4336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4337 "1430 Failed to initialize sgl list.\n");
4338 goto out_free_sgl_list;
4339 }
4340
4341 rc = lpfc_sli4_init_rpi_hdrs(phba);
4342 if (rc) {
4343 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4344 "1432 Failed to initialize rpi headers.\n");
4345 goto out_free_active_sgl;
4346 }
4347
James Smarta93ff372010-10-22 11:06:08 -04004348 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05004349 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4350 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4351 GFP_KERNEL);
4352 if (!phba->fcf.fcf_rr_bmask) {
4353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4354 "2759 Failed allocate memory for FCF round "
4355 "robin failover bmask\n");
4356 goto out_remove_rpi_hdrs;
4357 }
4358
James Smartda0436e2009-05-22 14:51:39 -04004359 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4360 phba->cfg_fcp_eq_count), GFP_KERNEL);
4361 if (!phba->sli4_hba.fcp_eq_hdl) {
4362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4363 "2572 Failed allocate memory for fast-path "
4364 "per-EQ handle array\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004365 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04004366 }
4367
4368 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4369 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4370 if (!phba->sli4_hba.msix_entries) {
4371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4372 "2573 Failed allocate memory for msi-x "
4373 "interrupt vector entries\n");
4374 goto out_free_fcp_eq_hdl;
4375 }
4376
4377 return rc;
4378
4379out_free_fcp_eq_hdl:
4380 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004381out_free_fcf_rr_bmask:
4382 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004383out_remove_rpi_hdrs:
4384 lpfc_sli4_remove_rpi_hdrs(phba);
4385out_free_active_sgl:
4386 lpfc_free_active_sgl(phba);
4387out_free_sgl_list:
4388 lpfc_free_sgl_list(phba);
4389out_destroy_cq_event_pool:
4390 lpfc_sli4_cq_event_pool_destroy(phba);
4391out_destroy_queue:
4392 lpfc_sli4_queue_destroy(phba);
4393out_free_bsmbx:
4394 lpfc_destroy_bootstrap_mbox(phba);
4395out_free_mem:
4396 lpfc_mem_free(phba);
4397 return rc;
4398}
4399
4400/**
4401 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4402 * @phba: pointer to lpfc hba data structure.
4403 *
4404 * This routine is invoked to unset the driver internal resources set up
4405 * specific for supporting the SLI-4 HBA device it attached to.
4406 **/
4407static void
4408lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4409{
4410 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4411
James Smartda0436e2009-05-22 14:51:39 -04004412 /* Free memory allocated for msi-x interrupt vector entries */
4413 kfree(phba->sli4_hba.msix_entries);
4414
4415 /* Free memory allocated for fast-path work queue handles */
4416 kfree(phba->sli4_hba.fcp_eq_hdl);
4417
4418 /* Free the allocated rpi headers. */
4419 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004420 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004421
James Smart0c9ab6f2010-02-26 14:15:57 -05004422 /* Free eligible FCF index bmask */
4423 kfree(phba->fcf.fcf_rr_bmask);
4424
James Smartda0436e2009-05-22 14:51:39 -04004425 /* Free the ELS sgl list */
4426 lpfc_free_active_sgl(phba);
4427 lpfc_free_sgl_list(phba);
4428
4429 /* Free the SCSI sgl management array */
4430 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4431
4432 /* Free the SLI4 queues */
4433 lpfc_sli4_queue_destroy(phba);
4434
4435 /* Free the completion queue EQ event pool */
4436 lpfc_sli4_cq_event_release_all(phba);
4437 lpfc_sli4_cq_event_pool_destroy(phba);
4438
James Smartda0436e2009-05-22 14:51:39 -04004439 /* Free the bsmbx region. */
4440 lpfc_destroy_bootstrap_mbox(phba);
4441
4442 /* Free the SLI Layer memory with SLI4 HBAs */
4443 lpfc_mem_free_all(phba);
4444
4445 /* Free the current connect table */
4446 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004447 &phba->fcf_conn_rec_list, list) {
4448 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004449 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004450 }
James Smartda0436e2009-05-22 14:51:39 -04004451
4452 return;
4453}
4454
4455/**
James Smart3772a992009-05-22 14:50:54 -04004456 * lpfc_init_api_table_setup - Set up init api fucntion jump table
4457 * @phba: The hba struct for which this call is being executed.
4458 * @dev_grp: The HBA PCI-Device group number.
4459 *
4460 * This routine sets up the device INIT interface API function jump table
4461 * in @phba struct.
4462 *
4463 * Returns: 0 - success, -ENODEV - failure.
4464 **/
4465int
4466lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4467{
James Smart84d1b002010-02-12 14:42:33 -05004468 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4469 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart3772a992009-05-22 14:50:54 -04004470 switch (dev_grp) {
4471 case LPFC_PCI_DEV_LP:
4472 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4473 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4474 phba->lpfc_stop_port = lpfc_stop_port_s3;
4475 break;
James Smartda0436e2009-05-22 14:51:39 -04004476 case LPFC_PCI_DEV_OC:
4477 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4478 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4479 phba->lpfc_stop_port = lpfc_stop_port_s4;
4480 break;
James Smart3772a992009-05-22 14:50:54 -04004481 default:
4482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4483 "1431 Invalid HBA PCI-device group: 0x%x\n",
4484 dev_grp);
4485 return -ENODEV;
4486 break;
4487 }
4488 return 0;
4489}
4490
4491/**
4492 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4493 * @phba: pointer to lpfc hba data structure.
4494 *
4495 * This routine is invoked to set up the driver internal resources before the
4496 * device specific resource setup to support the HBA device it attached to.
4497 *
4498 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004499 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004500 * other values - error
4501 **/
4502static int
4503lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4504{
4505 /*
4506 * Driver resources common to all SLI revisions
4507 */
James Smartea2151b2008-09-07 11:52:10 -04004508 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004509 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004510
James Smarte47c9092008-02-08 18:49:26 -05004511 /* Initialize ndlp management spinlock */
4512 spin_lock_init(&phba->ndlp_lock);
4513
James Smart2e0fef82007-06-17 19:56:36 -05004514 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004515 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004516 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004517
James Smart3772a992009-05-22 14:50:54 -04004518 /* Initialize the wait queue head for the kernel thread */
4519 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004520
James Smart3772a992009-05-22 14:50:54 -04004521 /* Initialize the scsi buffer list used by driver for scsi IO */
4522 spin_lock_init(&phba->scsi_buf_list_lock);
4523 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004524
James Smart3772a992009-05-22 14:50:54 -04004525 /* Initialize the fabric iocb list */
4526 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004527
James Smart3772a992009-05-22 14:50:54 -04004528 /* Initialize list to save ELS buffers */
4529 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004530
James Smart3772a992009-05-22 14:50:54 -04004531 /* Initialize FCF connection rec list */
4532 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004533
James Smart3772a992009-05-22 14:50:54 -04004534 return 0;
4535}
dea31012005-04-17 16:05:31 -05004536
James Smart3772a992009-05-22 14:50:54 -04004537/**
4538 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4539 * @phba: pointer to lpfc hba data structure.
4540 *
4541 * This routine is invoked to set up the driver internal resources after the
4542 * device specific resource setup to support the HBA device it attached to.
4543 *
4544 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004545 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004546 * other values - error
4547 **/
4548static int
4549lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4550{
4551 int error;
4552
4553 /* Startup the kernel thread for this host adapter. */
4554 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4555 "lpfc_worker_%d", phba->brd_no);
4556 if (IS_ERR(phba->worker_thread)) {
4557 error = PTR_ERR(phba->worker_thread);
4558 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004559 }
4560
James Smart3772a992009-05-22 14:50:54 -04004561 return 0;
4562}
4563
4564/**
4565 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4566 * @phba: pointer to lpfc hba data structure.
4567 *
4568 * This routine is invoked to unset the driver internal resources set up after
4569 * the device specific resource setup for supporting the HBA device it
4570 * attached to.
4571 **/
4572static void
4573lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4574{
4575 /* Stop kernel worker thread */
4576 kthread_stop(phba->worker_thread);
4577}
4578
4579/**
4580 * lpfc_free_iocb_list - Free iocb list.
4581 * @phba: pointer to lpfc hba data structure.
4582 *
4583 * This routine is invoked to free the driver's IOCB list and memory.
4584 **/
4585static void
4586lpfc_free_iocb_list(struct lpfc_hba *phba)
4587{
4588 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4589
4590 spin_lock_irq(&phba->hbalock);
4591 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4592 &phba->lpfc_iocb_list, list) {
4593 list_del(&iocbq_entry->list);
4594 kfree(iocbq_entry);
4595 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004596 }
James Smart3772a992009-05-22 14:50:54 -04004597 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004598
James Smart3772a992009-05-22 14:50:54 -04004599 return;
4600}
dea31012005-04-17 16:05:31 -05004601
James Smart3772a992009-05-22 14:50:54 -04004602/**
4603 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4604 * @phba: pointer to lpfc hba data structure.
4605 *
4606 * This routine is invoked to allocate and initizlize the driver's IOCB
4607 * list and set up the IOCB tag array accordingly.
4608 *
4609 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004610 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004611 * other values - error
4612 **/
4613static int
4614lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4615{
4616 struct lpfc_iocbq *iocbq_entry = NULL;
4617 uint16_t iotag;
4618 int i;
dea31012005-04-17 16:05:31 -05004619
4620 /* Initialize and populate the iocb list per host. */
4621 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004622 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004623 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004624 if (iocbq_entry == NULL) {
4625 printk(KERN_ERR "%s: only allocated %d iocbs of "
4626 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004627 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004628 goto out_free_iocbq;
4629 }
4630
James Bottomley604a3e32005-10-29 10:28:33 -05004631 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4632 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004633 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004634 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004635 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004636 goto out_free_iocbq;
4637 }
James Smart3772a992009-05-22 14:50:54 -04004638 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004639
4640 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004641 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4642 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004643 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004644 }
4645
James Smart3772a992009-05-22 14:50:54 -04004646 return 0;
4647
4648out_free_iocbq:
4649 lpfc_free_iocb_list(phba);
4650
4651 return -ENOMEM;
4652}
4653
4654/**
James Smartda0436e2009-05-22 14:51:39 -04004655 * lpfc_free_sgl_list - Free sgl list.
4656 * @phba: pointer to lpfc hba data structure.
4657 *
4658 * This routine is invoked to free the driver's sgl list and memory.
4659 **/
4660static void
4661lpfc_free_sgl_list(struct lpfc_hba *phba)
4662{
4663 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4664 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04004665
4666 spin_lock_irq(&phba->hbalock);
4667 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4668 spin_unlock_irq(&phba->hbalock);
4669
4670 list_for_each_entry_safe(sglq_entry, sglq_next,
4671 &sglq_list, list) {
4672 list_del(&sglq_entry->list);
4673 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4674 kfree(sglq_entry);
4675 phba->sli4_hba.total_sglq_bufs--;
4676 }
James Smartda0436e2009-05-22 14:51:39 -04004677 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4678}
4679
4680/**
4681 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4682 * @phba: pointer to lpfc hba data structure.
4683 *
4684 * This routine is invoked to allocate the driver's active sgl memory.
4685 * This array will hold the sglq_entry's for active IOs.
4686 **/
4687static int
4688lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4689{
4690 int size;
4691 size = sizeof(struct lpfc_sglq *);
4692 size *= phba->sli4_hba.max_cfg_param.max_xri;
4693
4694 phba->sli4_hba.lpfc_sglq_active_list =
4695 kzalloc(size, GFP_KERNEL);
4696 if (!phba->sli4_hba.lpfc_sglq_active_list)
4697 return -ENOMEM;
4698 return 0;
4699}
4700
4701/**
4702 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4703 * @phba: pointer to lpfc hba data structure.
4704 *
4705 * This routine is invoked to walk through the array of active sglq entries
4706 * and free all of the resources.
4707 * This is just a place holder for now.
4708 **/
4709static void
4710lpfc_free_active_sgl(struct lpfc_hba *phba)
4711{
4712 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4713}
4714
4715/**
4716 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4717 * @phba: pointer to lpfc hba data structure.
4718 *
4719 * This routine is invoked to allocate and initizlize the driver's sgl
4720 * list and set up the sgl xritag tag array accordingly.
4721 *
4722 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004723 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004724 * other values - error
4725 **/
4726static int
4727lpfc_init_sgl_list(struct lpfc_hba *phba)
4728{
4729 struct lpfc_sglq *sglq_entry = NULL;
4730 int i;
4731 int els_xri_cnt;
4732
4733 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4734 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4735 "2400 lpfc_init_sgl_list els %d.\n",
4736 els_xri_cnt);
4737 /* Initialize and populate the sglq list per host/VF. */
4738 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4739 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4740
4741 /* Sanity check on XRI management */
4742 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4743 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4744 "2562 No room left for SCSI XRI allocation: "
4745 "max_xri=%d, els_xri=%d\n",
4746 phba->sli4_hba.max_cfg_param.max_xri,
4747 els_xri_cnt);
4748 return -ENOMEM;
4749 }
4750
4751 /* Allocate memory for the ELS XRI management array */
4752 phba->sli4_hba.lpfc_els_sgl_array =
4753 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4754 GFP_KERNEL);
4755
4756 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4758 "2401 Failed to allocate memory for ELS "
4759 "XRI management array of size %d.\n",
4760 els_xri_cnt);
4761 return -ENOMEM;
4762 }
4763
4764 /* Keep the SCSI XRI into the XRI management array */
4765 phba->sli4_hba.scsi_xri_max =
4766 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4767 phba->sli4_hba.scsi_xri_cnt = 0;
4768
4769 phba->sli4_hba.lpfc_scsi_psb_array =
4770 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4771 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4772
4773 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4774 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4775 "2563 Failed to allocate memory for SCSI "
4776 "XRI management array of size %d.\n",
4777 phba->sli4_hba.scsi_xri_max);
4778 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4779 return -ENOMEM;
4780 }
4781
4782 for (i = 0; i < els_xri_cnt; i++) {
4783 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4784 if (sglq_entry == NULL) {
4785 printk(KERN_ERR "%s: only allocated %d sgls of "
4786 "expected %d count. Unloading driver.\n",
4787 __func__, i, els_xri_cnt);
4788 goto out_free_mem;
4789 }
4790
4791 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4792 if (sglq_entry->sli4_xritag == NO_XRI) {
4793 kfree(sglq_entry);
4794 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4795 "Unloading driver.\n", __func__);
4796 goto out_free_mem;
4797 }
4798 sglq_entry->buff_type = GEN_BUFF_TYPE;
4799 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4800 if (sglq_entry->virt == NULL) {
4801 kfree(sglq_entry);
4802 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4803 "Unloading driver.\n", __func__);
4804 goto out_free_mem;
4805 }
4806 sglq_entry->sgl = sglq_entry->virt;
4807 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4808
4809 /* The list order is used by later block SGL registraton */
4810 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05004811 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04004812 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4813 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4814 phba->sli4_hba.total_sglq_bufs++;
4815 spin_unlock_irq(&phba->hbalock);
4816 }
4817 return 0;
4818
4819out_free_mem:
4820 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4821 lpfc_free_sgl_list(phba);
4822 return -ENOMEM;
4823}
4824
4825/**
4826 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4827 * @phba: pointer to lpfc hba data structure.
4828 *
4829 * This routine is invoked to post rpi header templates to the
4830 * HBA consistent with the SLI-4 interface spec. This routine
4831 * posts a PAGE_SIZE memory region to the port to hold up to
4832 * PAGE_SIZE modulo 64 rpi context headers.
4833 * No locks are held here because this is an initialization routine
4834 * called only from probe or lpfc_online when interrupts are not
4835 * enabled and the driver is reinitializing the device.
4836 *
4837 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004838 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004839 * -ENOMEM - No availble memory
4840 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04004841 **/
4842int
4843lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4844{
4845 int rc = 0;
4846 int longs;
4847 uint16_t rpi_count;
4848 struct lpfc_rpi_hdr *rpi_hdr;
4849
4850 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4851
4852 /*
4853 * Provision an rpi bitmask range for discovery. The total count
4854 * is the difference between max and base + 1.
4855 */
4856 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4857 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4858
4859 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4860 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4861 GFP_KERNEL);
4862 if (!phba->sli4_hba.rpi_bmask)
4863 return -ENOMEM;
4864
4865 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4866 if (!rpi_hdr) {
4867 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4868 "0391 Error during rpi post operation\n");
4869 lpfc_sli4_remove_rpis(phba);
4870 rc = -ENODEV;
4871 }
4872
4873 return rc;
4874}
4875
4876/**
4877 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4878 * @phba: pointer to lpfc hba data structure.
4879 *
4880 * This routine is invoked to allocate a single 4KB memory region to
4881 * support rpis and stores them in the phba. This single region
4882 * provides support for up to 64 rpis. The region is used globally
4883 * by the device.
4884 *
4885 * Returns:
4886 * A valid rpi hdr on success.
4887 * A NULL pointer on any failure.
4888 **/
4889struct lpfc_rpi_hdr *
4890lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4891{
4892 uint16_t rpi_limit, curr_rpi_range;
4893 struct lpfc_dmabuf *dmabuf;
4894 struct lpfc_rpi_hdr *rpi_hdr;
4895
4896 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4897 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4898
4899 spin_lock_irq(&phba->hbalock);
4900 curr_rpi_range = phba->sli4_hba.next_rpi;
4901 spin_unlock_irq(&phba->hbalock);
4902
4903 /*
4904 * The port has a limited number of rpis. The increment here
4905 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4906 * and to allow the full max_rpi range per port.
4907 */
4908 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4909 return NULL;
4910
4911 /*
4912 * First allocate the protocol header region for the port. The
4913 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4914 */
4915 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4916 if (!dmabuf)
4917 return NULL;
4918
4919 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4920 LPFC_HDR_TEMPLATE_SIZE,
4921 &dmabuf->phys,
4922 GFP_KERNEL);
4923 if (!dmabuf->virt) {
4924 rpi_hdr = NULL;
4925 goto err_free_dmabuf;
4926 }
4927
4928 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4929 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4930 rpi_hdr = NULL;
4931 goto err_free_coherent;
4932 }
4933
4934 /* Save the rpi header data for cleanup later. */
4935 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4936 if (!rpi_hdr)
4937 goto err_free_coherent;
4938
4939 rpi_hdr->dmabuf = dmabuf;
4940 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4941 rpi_hdr->page_count = 1;
4942 spin_lock_irq(&phba->hbalock);
4943 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4944 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4945
4946 /*
4947 * The next_rpi stores the next module-64 rpi value to post
4948 * in any subsequent rpi memory region postings.
4949 */
4950 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4951 spin_unlock_irq(&phba->hbalock);
4952 return rpi_hdr;
4953
4954 err_free_coherent:
4955 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4956 dmabuf->virt, dmabuf->phys);
4957 err_free_dmabuf:
4958 kfree(dmabuf);
4959 return NULL;
4960}
4961
4962/**
4963 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4964 * @phba: pointer to lpfc hba data structure.
4965 *
4966 * This routine is invoked to remove all memory resources allocated
4967 * to support rpis. This routine presumes the caller has released all
4968 * rpis consumed by fabric or port logins and is prepared to have
4969 * the header pages removed.
4970 **/
4971void
4972lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4973{
4974 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4975
4976 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4977 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4978 list_del(&rpi_hdr->list);
4979 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4980 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4981 kfree(rpi_hdr->dmabuf);
4982 kfree(rpi_hdr);
4983 }
4984
4985 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4986 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4987}
4988
4989/**
James Smart3772a992009-05-22 14:50:54 -04004990 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4991 * @pdev: pointer to pci device data structure.
4992 *
4993 * This routine is invoked to allocate the driver hba data structure for an
4994 * HBA device. If the allocation is successful, the phba reference to the
4995 * PCI device data structure is set.
4996 *
4997 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004998 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004999 * NULL - error
5000 **/
5001static struct lpfc_hba *
5002lpfc_hba_alloc(struct pci_dev *pdev)
5003{
5004 struct lpfc_hba *phba;
5005
5006 /* Allocate memory for HBA structure */
5007 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5008 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005009 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005010 return NULL;
5011 }
5012
5013 /* Set reference to PCI device in HBA structure */
5014 phba->pcidev = pdev;
5015
5016 /* Assign an unused board number */
5017 phba->brd_no = lpfc_get_instance();
5018 if (phba->brd_no < 0) {
5019 kfree(phba);
5020 return NULL;
5021 }
5022
James Smart4fede782010-01-26 23:08:55 -05005023 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005024 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5025
James Smart3772a992009-05-22 14:50:54 -04005026 return phba;
5027}
5028
5029/**
5030 * lpfc_hba_free - Free driver hba data structure with a device.
5031 * @phba: pointer to lpfc hba data structure.
5032 *
5033 * This routine is invoked to free the driver hba data structure with an
5034 * HBA device.
5035 **/
5036static void
5037lpfc_hba_free(struct lpfc_hba *phba)
5038{
5039 /* Release the driver assigned board number */
5040 idr_remove(&lpfc_hba_index, phba->brd_no);
5041
5042 kfree(phba);
5043 return;
5044}
5045
5046/**
5047 * lpfc_create_shost - Create hba physical port with associated scsi host.
5048 * @phba: pointer to lpfc hba data structure.
5049 *
5050 * This routine is invoked to create HBA physical port and associate a SCSI
5051 * host with it.
5052 *
5053 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005054 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005055 * other values - error
5056 **/
5057static int
5058lpfc_create_shost(struct lpfc_hba *phba)
5059{
5060 struct lpfc_vport *vport;
5061 struct Scsi_Host *shost;
5062
5063 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005064 phba->fc_edtov = FF_DEF_EDTOV;
5065 phba->fc_ratov = FF_DEF_RATOV;
5066 phba->fc_altov = FF_DEF_ALTOV;
5067 phba->fc_arbtov = FF_DEF_ARBTOV;
5068
James Smartd7c47992010-06-08 18:31:54 -04005069 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005070 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005071 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005072 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005073
5074 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005075 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005076 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005077 /* Put reference to SCSI host to driver's device private data */
5078 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005079
James Smart3772a992009-05-22 14:50:54 -04005080 return 0;
5081}
dea31012005-04-17 16:05:31 -05005082
James Smart3772a992009-05-22 14:50:54 -04005083/**
5084 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5085 * @phba: pointer to lpfc hba data structure.
5086 *
5087 * This routine is invoked to destroy HBA physical port and the associated
5088 * SCSI host.
5089 **/
5090static void
5091lpfc_destroy_shost(struct lpfc_hba *phba)
5092{
5093 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005094
James Smart3772a992009-05-22 14:50:54 -04005095 /* Destroy physical port that associated with the SCSI host */
5096 destroy_port(vport);
5097
5098 return;
5099}
5100
5101/**
5102 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5103 * @phba: pointer to lpfc hba data structure.
5104 * @shost: the shost to be used to detect Block guard settings.
5105 *
5106 * This routine sets up the local Block guard protocol settings for @shost.
5107 * This routine also allocates memory for debugging bg buffers.
5108 **/
5109static void
5110lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5111{
5112 int pagecnt = 10;
5113 if (lpfc_prot_mask && lpfc_prot_guard) {
5114 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5115 "1478 Registering BlockGuard with the "
5116 "SCSI layer\n");
5117 scsi_host_set_prot(shost, lpfc_prot_mask);
5118 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005119 }
James Smart81301a92008-12-04 22:39:46 -05005120 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005121 while (pagecnt) {
5122 spin_lock_init(&_dump_buf_lock);
5123 _dump_buf_data =
5124 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5125 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005126 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5127 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005128 "_dump_buf_data at 0x%p\n",
5129 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005130 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005131 memset(_dump_buf_data, 0,
5132 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005133 break;
James Smart3772a992009-05-22 14:50:54 -04005134 } else
James Smart81301a92008-12-04 22:39:46 -05005135 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005136 }
James Smart81301a92008-12-04 22:39:46 -05005137 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005138 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5139 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005140 "memory for hexdump\n");
5141 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005142 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5143 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005144 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005145 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005146 while (pagecnt) {
5147 _dump_buf_dif =
5148 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5149 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005150 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5151 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005152 "_dump_buf_dif at 0x%p\n",
5153 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005154 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005155 memset(_dump_buf_dif, 0,
5156 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005157 break;
James Smart3772a992009-05-22 14:50:54 -04005158 } else
James Smart81301a92008-12-04 22:39:46 -05005159 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005160 }
James Smart81301a92008-12-04 22:39:46 -05005161 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005162 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5163 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005164 "memory for hexdump\n");
5165 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005166 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5167 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005168 _dump_buf_dif);
5169}
5170
5171/**
5172 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5173 * @phba: pointer to lpfc hba data structure.
5174 *
5175 * This routine is invoked to perform all the necessary post initialization
5176 * setup for the device.
5177 **/
5178static void
5179lpfc_post_init_setup(struct lpfc_hba *phba)
5180{
5181 struct Scsi_Host *shost;
5182 struct lpfc_adapter_event_header adapter_event;
5183
5184 /* Get the default values for Model Name and Description */
5185 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5186
5187 /*
5188 * hba setup may have changed the hba_queue_depth so we need to
5189 * adjust the value of can_queue.
5190 */
5191 shost = pci_get_drvdata(phba->pcidev);
5192 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5193 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5194 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005195
5196 lpfc_host_attrib_init(shost);
5197
James Smart2e0fef82007-06-17 19:56:36 -05005198 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5199 spin_lock_irq(shost->host_lock);
5200 lpfc_poll_start_timer(phba);
5201 spin_unlock_irq(shost->host_lock);
5202 }
James Smart8f6d98d2006-08-01 07:34:00 -04005203
James Smart93996272008-08-24 21:50:30 -04005204 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5205 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005206 /* Send board arrival event to upper layer */
5207 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5208 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5209 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005210 sizeof(adapter_event),
5211 (char *) &adapter_event,
5212 LPFC_NL_VENDOR_ID);
5213 return;
5214}
5215
5216/**
5217 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5218 * @phba: pointer to lpfc hba data structure.
5219 *
5220 * This routine is invoked to set up the PCI device memory space for device
5221 * with SLI-3 interface spec.
5222 *
5223 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005224 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005225 * other values - error
5226 **/
5227static int
5228lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5229{
5230 struct pci_dev *pdev;
5231 unsigned long bar0map_len, bar2map_len;
5232 int i, hbq_count;
5233 void *ptr;
5234 int error = -ENODEV;
5235
5236 /* Obtain PCI device reference */
5237 if (!phba->pcidev)
5238 return error;
5239 else
5240 pdev = phba->pcidev;
5241
5242 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005243 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5244 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5245 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5246 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005247 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005248 }
5249 }
James Smart3772a992009-05-22 14:50:54 -04005250
5251 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5252 * required by each mapping.
5253 */
5254 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5255 bar0map_len = pci_resource_len(pdev, 0);
5256
5257 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5258 bar2map_len = pci_resource_len(pdev, 2);
5259
5260 /* Map HBA SLIM to a kernel virtual address. */
5261 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5262 if (!phba->slim_memmap_p) {
5263 dev_printk(KERN_ERR, &pdev->dev,
5264 "ioremap failed for SLIM memory.\n");
5265 goto out;
5266 }
5267
5268 /* Map HBA Control Registers to a kernel virtual address. */
5269 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5270 if (!phba->ctrl_regs_memmap_p) {
5271 dev_printk(KERN_ERR, &pdev->dev,
5272 "ioremap failed for HBA control registers.\n");
5273 goto out_iounmap_slim;
5274 }
5275
5276 /* Allocate memory for SLI-2 structures */
5277 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5278 SLI2_SLIM_SIZE,
5279 &phba->slim2p.phys,
5280 GFP_KERNEL);
5281 if (!phba->slim2p.virt)
5282 goto out_iounmap;
5283
5284 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5285 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005286 phba->mbox_ext = (phba->slim2p.virt +
5287 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005288 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5289 phba->IOCBs = (phba->slim2p.virt +
5290 offsetof(struct lpfc_sli2_slim, IOCBs));
5291
5292 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5293 lpfc_sli_hbq_size(),
5294 &phba->hbqslimp.phys,
5295 GFP_KERNEL);
5296 if (!phba->hbqslimp.virt)
5297 goto out_free_slim;
5298
5299 hbq_count = lpfc_sli_hbq_count();
5300 ptr = phba->hbqslimp.virt;
5301 for (i = 0; i < hbq_count; ++i) {
5302 phba->hbqs[i].hbq_virt = ptr;
5303 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5304 ptr += (lpfc_hbq_defs[i]->entry_count *
5305 sizeof(struct lpfc_hbq_entry));
5306 }
5307 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5308 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5309
5310 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5311
5312 INIT_LIST_HEAD(&phba->rb_pend_list);
5313
5314 phba->MBslimaddr = phba->slim_memmap_p;
5315 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5316 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5317 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5318 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005319
dea31012005-04-17 16:05:31 -05005320 return 0;
5321
dea31012005-04-17 16:05:31 -05005322out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005323 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5324 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005325out_iounmap:
5326 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005327out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005328 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005329out:
5330 return error;
5331}
5332
James Smarte59058c2008-08-24 21:49:00 -04005333/**
James Smart3772a992009-05-22 14:50:54 -04005334 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5335 * @phba: pointer to lpfc hba data structure.
5336 *
5337 * This routine is invoked to unset the PCI device memory space for device
5338 * with SLI-3 interface spec.
5339 **/
5340static void
5341lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5342{
5343 struct pci_dev *pdev;
5344
5345 /* Obtain PCI device reference */
5346 if (!phba->pcidev)
5347 return;
5348 else
5349 pdev = phba->pcidev;
5350
5351 /* Free coherent DMA memory allocated */
5352 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5353 phba->hbqslimp.virt, phba->hbqslimp.phys);
5354 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5355 phba->slim2p.virt, phba->slim2p.phys);
5356
5357 /* I/O memory unmap */
5358 iounmap(phba->ctrl_regs_memmap_p);
5359 iounmap(phba->slim_memmap_p);
5360
5361 return;
5362}
5363
5364/**
James Smartda0436e2009-05-22 14:51:39 -04005365 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5366 * @phba: pointer to lpfc hba data structure.
5367 *
5368 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5369 * done and check status.
5370 *
5371 * Return 0 if successful, otherwise -ENODEV.
5372 **/
5373int
5374lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5375{
James Smart2fcee4b2010-12-15 17:57:46 -05005376 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5377 struct lpfc_register reg_data;
5378 int i, port_error = 0;
5379 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04005380
James Smart2fcee4b2010-12-15 17:57:46 -05005381 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04005382 return -ENODEV;
5383
James Smartda0436e2009-05-22 14:51:39 -04005384 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5385 for (i = 0; i < 3000; i++) {
James Smart2fcee4b2010-12-15 17:57:46 -05005386 portsmphr_reg.word0 = readl(phba->sli4_hba.PSMPHRregaddr);
5387 if (bf_get(lpfc_port_smphr_perr, &portsmphr_reg)) {
5388 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04005389 port_error = -ENODEV;
5390 break;
5391 }
James Smart2fcee4b2010-12-15 17:57:46 -05005392 if (LPFC_POST_STAGE_PORT_READY ==
5393 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04005394 break;
James Smartda0436e2009-05-22 14:51:39 -04005395 msleep(10);
5396 }
5397
James Smart2fcee4b2010-12-15 17:57:46 -05005398 /*
5399 * If there was a port error during POST, then don't proceed with
5400 * other register reads as the data may not be valid. Just exit.
5401 */
5402 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04005403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005404 "1408 Port Failed POST - portsmphr=0x%x, "
5405 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5406 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5407 portsmphr_reg.word0,
5408 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5409 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5410 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5411 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5412 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5413 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5414 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5415 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5416 } else {
James Smart28baac72010-02-12 14:42:03 -05005417 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005418 "2534 Device Info: SLIFamily=0x%x, "
5419 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5420 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05005421 bf_get(lpfc_sli_intf_sli_family,
5422 &phba->sli4_hba.sli_intf),
5423 bf_get(lpfc_sli_intf_slirev,
5424 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005425 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05005426 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005427 bf_get(lpfc_sli_intf_sli_hint1,
5428 &phba->sli4_hba.sli_intf),
5429 bf_get(lpfc_sli_intf_sli_hint2,
5430 &phba->sli4_hba.sli_intf),
5431 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05005432 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05005433 /*
5434 * Check for other Port errors during the initialization
5435 * process. Fail the load if the port did not come up
5436 * correctly.
5437 */
5438 if_type = bf_get(lpfc_sli_intf_if_type,
5439 &phba->sli4_hba.sli_intf);
5440 switch (if_type) {
5441 case LPFC_SLI_INTF_IF_TYPE_0:
5442 phba->sli4_hba.ue_mask_lo =
5443 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5444 phba->sli4_hba.ue_mask_hi =
5445 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5446 uerrlo_reg.word0 =
5447 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5448 uerrhi_reg.word0 =
5449 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
5450 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5451 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5453 "1422 Unrecoverable Error "
5454 "Detected during POST "
5455 "uerr_lo_reg=0x%x, "
5456 "uerr_hi_reg=0x%x, "
5457 "ue_mask_lo_reg=0x%x, "
5458 "ue_mask_hi_reg=0x%x\n",
5459 uerrlo_reg.word0,
5460 uerrhi_reg.word0,
5461 phba->sli4_hba.ue_mask_lo,
5462 phba->sli4_hba.ue_mask_hi);
5463 port_error = -ENODEV;
5464 }
5465 break;
5466 case LPFC_SLI_INTF_IF_TYPE_2:
5467 /* Final checks. The port status should be clean. */
5468 reg_data.word0 =
5469 readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
5470 if (bf_get(lpfc_sliport_status_err, &reg_data)) {
5471 phba->work_status[0] =
5472 readl(phba->sli4_hba.u.if_type2.
5473 ERR1regaddr);
5474 phba->work_status[1] =
5475 readl(phba->sli4_hba.u.if_type2.
5476 ERR2regaddr);
5477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5478 "2888 Port Error Detected "
5479 "during POST: "
5480 "port status reg 0x%x, "
5481 "port_smphr reg 0x%x, "
5482 "error 1=0x%x, error 2=0x%x\n",
5483 reg_data.word0,
5484 portsmphr_reg.word0,
5485 phba->work_status[0],
5486 phba->work_status[1]);
5487 port_error = -ENODEV;
5488 }
5489 break;
5490 case LPFC_SLI_INTF_IF_TYPE_1:
5491 default:
5492 break;
5493 }
James Smart28baac72010-02-12 14:42:03 -05005494 }
James Smartda0436e2009-05-22 14:51:39 -04005495 return port_error;
5496}
5497
5498/**
5499 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5500 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05005501 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04005502 *
5503 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5504 * memory map.
5505 **/
5506static void
James Smart2fcee4b2010-12-15 17:57:46 -05005507lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04005508{
James Smart2fcee4b2010-12-15 17:57:46 -05005509 switch (if_type) {
5510 case LPFC_SLI_INTF_IF_TYPE_0:
5511 phba->sli4_hba.u.if_type0.UERRLOregaddr =
5512 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
5513 phba->sli4_hba.u.if_type0.UERRHIregaddr =
5514 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
5515 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
5516 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
5517 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
5518 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
5519 phba->sli4_hba.SLIINTFregaddr =
5520 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5521 break;
5522 case LPFC_SLI_INTF_IF_TYPE_2:
5523 phba->sli4_hba.u.if_type2.ERR1regaddr =
5524 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_ERR_1;
5525 phba->sli4_hba.u.if_type2.ERR2regaddr =
5526 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_ERR_2;
5527 phba->sli4_hba.u.if_type2.CTRLregaddr =
5528 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_CNTRL;
5529 phba->sli4_hba.u.if_type2.STATUSregaddr =
5530 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_STATUS;
5531 phba->sli4_hba.SLIINTFregaddr =
5532 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5533 phba->sli4_hba.PSMPHRregaddr =
5534 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_IF2_SMPHR;
5535 phba->sli4_hba.RQDBregaddr =
5536 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
5537 phba->sli4_hba.WQDBregaddr =
5538 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
5539 phba->sli4_hba.EQCQDBregaddr =
5540 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
5541 phba->sli4_hba.MQDBregaddr =
5542 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
5543 phba->sli4_hba.BMBXregaddr =
5544 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
5545 break;
5546 case LPFC_SLI_INTF_IF_TYPE_1:
5547 default:
5548 dev_printk(KERN_ERR, &phba->pcidev->dev,
5549 "FATAL - unsupported SLI4 interface type - %d\n",
5550 if_type);
5551 break;
5552 }
James Smartda0436e2009-05-22 14:51:39 -04005553}
5554
5555/**
5556 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5557 * @phba: pointer to lpfc hba data structure.
5558 *
5559 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5560 * memory map.
5561 **/
5562static void
5563lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5564{
James Smart2fcee4b2010-12-15 17:57:46 -05005565 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5566 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04005567 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005568 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04005569 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005570 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04005571 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005572 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04005573}
5574
5575/**
5576 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5577 * @phba: pointer to lpfc hba data structure.
5578 * @vf: virtual function number
5579 *
5580 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5581 * based on the given viftual function number, @vf.
5582 *
5583 * Return 0 if successful, otherwise -ENODEV.
5584 **/
5585static int
5586lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5587{
5588 if (vf > LPFC_VIR_FUNC_MAX)
5589 return -ENODEV;
5590
5591 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5592 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5593 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5594 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5595 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5596 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5597 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5598 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5599 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5600 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5601 return 0;
5602}
5603
5604/**
5605 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5606 * @phba: pointer to lpfc hba data structure.
5607 *
5608 * This routine is invoked to create the bootstrap mailbox
5609 * region consistent with the SLI-4 interface spec. This
5610 * routine allocates all memory necessary to communicate
5611 * mailbox commands to the port and sets up all alignment
5612 * needs. No locks are expected to be held when calling
5613 * this routine.
5614 *
5615 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005616 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005617 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005618 **/
5619static int
5620lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5621{
5622 uint32_t bmbx_size;
5623 struct lpfc_dmabuf *dmabuf;
5624 struct dma_address *dma_address;
5625 uint32_t pa_addr;
5626 uint64_t phys_addr;
5627
5628 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5629 if (!dmabuf)
5630 return -ENOMEM;
5631
5632 /*
5633 * The bootstrap mailbox region is comprised of 2 parts
5634 * plus an alignment restriction of 16 bytes.
5635 */
5636 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5637 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5638 bmbx_size,
5639 &dmabuf->phys,
5640 GFP_KERNEL);
5641 if (!dmabuf->virt) {
5642 kfree(dmabuf);
5643 return -ENOMEM;
5644 }
5645 memset(dmabuf->virt, 0, bmbx_size);
5646
5647 /*
5648 * Initialize the bootstrap mailbox pointers now so that the register
5649 * operations are simple later. The mailbox dma address is required
5650 * to be 16-byte aligned. Also align the virtual memory as each
5651 * maibox is copied into the bmbx mailbox region before issuing the
5652 * command to the port.
5653 */
5654 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5655 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5656
5657 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5658 LPFC_ALIGN_16_BYTE);
5659 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5660 LPFC_ALIGN_16_BYTE);
5661
5662 /*
5663 * Set the high and low physical addresses now. The SLI4 alignment
5664 * requirement is 16 bytes and the mailbox is posted to the port
5665 * as two 30-bit addresses. The other data is a bit marking whether
5666 * the 30-bit address is the high or low address.
5667 * Upcast bmbx aphys to 64bits so shift instruction compiles
5668 * clean on 32 bit machines.
5669 */
5670 dma_address = &phba->sli4_hba.bmbx.dma_address;
5671 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5672 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5673 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5674 LPFC_BMBX_BIT1_ADDR_HI);
5675
5676 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5677 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5678 LPFC_BMBX_BIT1_ADDR_LO);
5679 return 0;
5680}
5681
5682/**
5683 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5684 * @phba: pointer to lpfc hba data structure.
5685 *
5686 * This routine is invoked to teardown the bootstrap mailbox
5687 * region and release all host resources. This routine requires
5688 * the caller to ensure all mailbox commands recovered, no
5689 * additional mailbox comands are sent, and interrupts are disabled
5690 * before calling this routine.
5691 *
5692 **/
5693static void
5694lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5695{
5696 dma_free_coherent(&phba->pcidev->dev,
5697 phba->sli4_hba.bmbx.bmbx_size,
5698 phba->sli4_hba.bmbx.dmabuf->virt,
5699 phba->sli4_hba.bmbx.dmabuf->phys);
5700
5701 kfree(phba->sli4_hba.bmbx.dmabuf);
5702 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5703}
5704
5705/**
5706 * lpfc_sli4_read_config - Get the config parameters.
5707 * @phba: pointer to lpfc hba data structure.
5708 *
5709 * This routine is invoked to read the configuration parameters from the HBA.
5710 * The configuration parameters are used to set the base and maximum values
5711 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5712 * allocation for the port.
5713 *
5714 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005715 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005716 * -ENOMEM - No availble memory
5717 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005718 **/
5719static int
5720lpfc_sli4_read_config(struct lpfc_hba *phba)
5721{
5722 LPFC_MBOXQ_t *pmb;
5723 struct lpfc_mbx_read_config *rd_config;
5724 uint32_t rc = 0;
5725
5726 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5727 if (!pmb) {
5728 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5729 "2011 Unable to allocate memory for issuing "
5730 "SLI_CONFIG_SPECIAL mailbox command\n");
5731 return -ENOMEM;
5732 }
5733
5734 lpfc_read_config(phba, pmb);
5735
5736 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5737 if (rc != MBX_SUCCESS) {
5738 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5739 "2012 Mailbox failed , mbxCmd x%x "
5740 "READ_CONFIG, mbxStatus x%x\n",
5741 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5742 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5743 rc = -EIO;
5744 } else {
5745 rd_config = &pmb->u.mqe.un.rd_config;
5746 phba->sli4_hba.max_cfg_param.max_xri =
5747 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5748 phba->sli4_hba.max_cfg_param.xri_base =
5749 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5750 phba->sli4_hba.max_cfg_param.max_vpi =
5751 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5752 phba->sli4_hba.max_cfg_param.vpi_base =
5753 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5754 phba->sli4_hba.max_cfg_param.max_rpi =
5755 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5756 phba->sli4_hba.max_cfg_param.rpi_base =
5757 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5758 phba->sli4_hba.max_cfg_param.max_vfi =
5759 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5760 phba->sli4_hba.max_cfg_param.vfi_base =
5761 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5762 phba->sli4_hba.max_cfg_param.max_fcfi =
5763 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5764 phba->sli4_hba.max_cfg_param.fcfi_base =
5765 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5766 phba->sli4_hba.max_cfg_param.max_eq =
5767 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5768 phba->sli4_hba.max_cfg_param.max_rq =
5769 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5770 phba->sli4_hba.max_cfg_param.max_wq =
5771 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5772 phba->sli4_hba.max_cfg_param.max_cq =
5773 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5774 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5775 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5776 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5777 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5778 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005779 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5780 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005781 phba->max_vports = phba->max_vpi;
5782 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5783 "2003 cfg params XRI(B:%d M:%d), "
5784 "VPI(B:%d M:%d) "
5785 "VFI(B:%d M:%d) "
5786 "RPI(B:%d M:%d) "
5787 "FCFI(B:%d M:%d)\n",
5788 phba->sli4_hba.max_cfg_param.xri_base,
5789 phba->sli4_hba.max_cfg_param.max_xri,
5790 phba->sli4_hba.max_cfg_param.vpi_base,
5791 phba->sli4_hba.max_cfg_param.max_vpi,
5792 phba->sli4_hba.max_cfg_param.vfi_base,
5793 phba->sli4_hba.max_cfg_param.max_vfi,
5794 phba->sli4_hba.max_cfg_param.rpi_base,
5795 phba->sli4_hba.max_cfg_param.max_rpi,
5796 phba->sli4_hba.max_cfg_param.fcfi_base,
5797 phba->sli4_hba.max_cfg_param.max_fcfi);
5798 }
5799 mempool_free(pmb, phba->mbox_mem_pool);
5800
5801 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04005802 if (phba->cfg_hba_queue_depth >
5803 (phba->sli4_hba.max_cfg_param.max_xri -
5804 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04005805 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04005806 phba->sli4_hba.max_cfg_param.max_xri -
5807 lpfc_sli4_get_els_iocb_cnt(phba);
James Smartda0436e2009-05-22 14:51:39 -04005808 return rc;
5809}
5810
5811/**
James Smart2fcee4b2010-12-15 17:57:46 -05005812 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04005813 * @phba: pointer to lpfc hba data structure.
5814 *
James Smart2fcee4b2010-12-15 17:57:46 -05005815 * This routine is invoked to setup the port-side endian order when
5816 * the port if_type is 0. This routine has no function for other
5817 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04005818 *
5819 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005820 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005821 * -ENOMEM - No availble memory
5822 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005823 **/
5824static int
5825lpfc_setup_endian_order(struct lpfc_hba *phba)
5826{
5827 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05005828 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005829 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5830 HOST_ENDIAN_HIGH_WORD1};
5831
James Smart2fcee4b2010-12-15 17:57:46 -05005832 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
5833 switch (if_type) {
5834 case LPFC_SLI_INTF_IF_TYPE_0:
5835 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5836 GFP_KERNEL);
5837 if (!mboxq) {
5838 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5839 "0492 Unable to allocate memory for "
5840 "issuing SLI_CONFIG_SPECIAL mailbox "
5841 "command\n");
5842 return -ENOMEM;
5843 }
James Smartda0436e2009-05-22 14:51:39 -04005844
James Smart2fcee4b2010-12-15 17:57:46 -05005845 /*
5846 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
5847 * two words to contain special data values and no other data.
5848 */
5849 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5850 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5851 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5852 if (rc != MBX_SUCCESS) {
5853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5854 "0493 SLI_CONFIG_SPECIAL mailbox "
5855 "failed with status x%x\n",
5856 rc);
5857 rc = -EIO;
5858 }
5859 mempool_free(mboxq, phba->mbox_mem_pool);
5860 break;
5861 case LPFC_SLI_INTF_IF_TYPE_2:
5862 case LPFC_SLI_INTF_IF_TYPE_1:
5863 default:
5864 break;
James Smartda0436e2009-05-22 14:51:39 -04005865 }
James Smartda0436e2009-05-22 14:51:39 -04005866 return rc;
5867}
5868
5869/**
5870 * lpfc_sli4_queue_create - Create all the SLI4 queues
5871 * @phba: pointer to lpfc hba data structure.
5872 *
5873 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5874 * operation. For each SLI4 queue type, the parameters such as queue entry
5875 * count (queue depth) shall be taken from the module parameter. For now,
5876 * we just use some constant number as place holder.
5877 *
5878 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005879 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005880 * -ENOMEM - No availble memory
5881 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005882 **/
5883static int
5884lpfc_sli4_queue_create(struct lpfc_hba *phba)
5885{
5886 struct lpfc_queue *qdesc;
5887 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5888 int cfg_fcp_wq_count;
5889 int cfg_fcp_eq_count;
5890
5891 /*
5892 * Sanity check for confiugred queue parameters against the run-time
5893 * device parameters
5894 */
5895
5896 /* Sanity check on FCP fast-path WQ parameters */
5897 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5898 if (cfg_fcp_wq_count >
5899 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5900 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5901 LPFC_SP_WQN_DEF;
5902 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5904 "2581 Not enough WQs (%d) from "
5905 "the pci function for supporting "
5906 "FCP WQs (%d)\n",
5907 phba->sli4_hba.max_cfg_param.max_wq,
5908 phba->cfg_fcp_wq_count);
5909 goto out_error;
5910 }
5911 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5912 "2582 Not enough WQs (%d) from the pci "
5913 "function for supporting the requested "
5914 "FCP WQs (%d), the actual FCP WQs can "
5915 "be supported: %d\n",
5916 phba->sli4_hba.max_cfg_param.max_wq,
5917 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5918 }
5919 /* The actual number of FCP work queues adopted */
5920 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5921
5922 /* Sanity check on FCP fast-path EQ parameters */
5923 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5924 if (cfg_fcp_eq_count >
5925 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5926 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5927 LPFC_SP_EQN_DEF;
5928 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5930 "2574 Not enough EQs (%d) from the "
5931 "pci function for supporting FCP "
5932 "EQs (%d)\n",
5933 phba->sli4_hba.max_cfg_param.max_eq,
5934 phba->cfg_fcp_eq_count);
5935 goto out_error;
5936 }
5937 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5938 "2575 Not enough EQs (%d) from the pci "
5939 "function for supporting the requested "
5940 "FCP EQs (%d), the actual FCP EQs can "
5941 "be supported: %d\n",
5942 phba->sli4_hba.max_cfg_param.max_eq,
5943 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5944 }
5945 /* It does not make sense to have more EQs than WQs */
5946 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5947 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005948 "2593 The FCP EQ count(%d) cannot be greater "
5949 "than the FCP WQ count(%d), limiting the "
5950 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005951 phba->cfg_fcp_wq_count,
5952 phba->cfg_fcp_wq_count);
5953 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5954 }
5955 /* The actual number of FCP event queues adopted */
5956 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5957 /* The overall number of event queues used */
5958 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5959
5960 /*
5961 * Create Event Queues (EQs)
5962 */
5963
5964 /* Get EQ depth from module parameter, fake the default for now */
5965 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5966 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5967
5968 /* Create slow path event queue */
5969 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5970 phba->sli4_hba.eq_ecount);
5971 if (!qdesc) {
5972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5973 "0496 Failed allocate slow-path EQ\n");
5974 goto out_error;
5975 }
5976 phba->sli4_hba.sp_eq = qdesc;
5977
5978 /* Create fast-path FCP Event Queue(s) */
5979 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5980 phba->cfg_fcp_eq_count), GFP_KERNEL);
5981 if (!phba->sli4_hba.fp_eq) {
5982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5983 "2576 Failed allocate memory for fast-path "
5984 "EQ record array\n");
5985 goto out_free_sp_eq;
5986 }
5987 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5988 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5989 phba->sli4_hba.eq_ecount);
5990 if (!qdesc) {
5991 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5992 "0497 Failed allocate fast-path EQ\n");
5993 goto out_free_fp_eq;
5994 }
5995 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5996 }
5997
5998 /*
5999 * Create Complete Queues (CQs)
6000 */
6001
6002 /* Get CQ depth from module parameter, fake the default for now */
6003 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6004 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6005
6006 /* Create slow-path Mailbox Command Complete Queue */
6007 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6008 phba->sli4_hba.cq_ecount);
6009 if (!qdesc) {
6010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6011 "0500 Failed allocate slow-path mailbox CQ\n");
6012 goto out_free_fp_eq;
6013 }
6014 phba->sli4_hba.mbx_cq = qdesc;
6015
6016 /* Create slow-path ELS Complete Queue */
6017 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6018 phba->sli4_hba.cq_ecount);
6019 if (!qdesc) {
6020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6021 "0501 Failed allocate slow-path ELS CQ\n");
6022 goto out_free_mbx_cq;
6023 }
6024 phba->sli4_hba.els_cq = qdesc;
6025
James Smartda0436e2009-05-22 14:51:39 -04006026
6027 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
6028 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6029 phba->cfg_fcp_eq_count), GFP_KERNEL);
6030 if (!phba->sli4_hba.fcp_cq) {
6031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6032 "2577 Failed allocate memory for fast-path "
6033 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04006034 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04006035 }
6036 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6037 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6038 phba->sli4_hba.cq_ecount);
6039 if (!qdesc) {
6040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6041 "0499 Failed allocate fast-path FCP "
6042 "CQ (%d)\n", fcp_cqidx);
6043 goto out_free_fcp_cq;
6044 }
6045 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
6046 }
6047
6048 /* Create Mailbox Command Queue */
6049 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6050 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6051
6052 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6053 phba->sli4_hba.mq_ecount);
6054 if (!qdesc) {
6055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6056 "0505 Failed allocate slow-path MQ\n");
6057 goto out_free_fcp_cq;
6058 }
6059 phba->sli4_hba.mbx_wq = qdesc;
6060
6061 /*
6062 * Create all the Work Queues (WQs)
6063 */
6064 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6065 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6066
6067 /* Create slow-path ELS Work Queue */
6068 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6069 phba->sli4_hba.wq_ecount);
6070 if (!qdesc) {
6071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6072 "0504 Failed allocate slow-path ELS WQ\n");
6073 goto out_free_mbx_wq;
6074 }
6075 phba->sli4_hba.els_wq = qdesc;
6076
6077 /* Create fast-path FCP Work Queue(s) */
6078 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6079 phba->cfg_fcp_wq_count), GFP_KERNEL);
6080 if (!phba->sli4_hba.fcp_wq) {
6081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6082 "2578 Failed allocate memory for fast-path "
6083 "WQ record array\n");
6084 goto out_free_els_wq;
6085 }
6086 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6087 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6088 phba->sli4_hba.wq_ecount);
6089 if (!qdesc) {
6090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6091 "0503 Failed allocate fast-path FCP "
6092 "WQ (%d)\n", fcp_wqidx);
6093 goto out_free_fcp_wq;
6094 }
6095 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6096 }
6097
6098 /*
6099 * Create Receive Queue (RQ)
6100 */
6101 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6102 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6103
6104 /* Create Receive Queue for header */
6105 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6106 phba->sli4_hba.rq_ecount);
6107 if (!qdesc) {
6108 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6109 "0506 Failed allocate receive HRQ\n");
6110 goto out_free_fcp_wq;
6111 }
6112 phba->sli4_hba.hdr_rq = qdesc;
6113
6114 /* Create Receive Queue for data */
6115 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6116 phba->sli4_hba.rq_ecount);
6117 if (!qdesc) {
6118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6119 "0507 Failed allocate receive DRQ\n");
6120 goto out_free_hdr_rq;
6121 }
6122 phba->sli4_hba.dat_rq = qdesc;
6123
6124 return 0;
6125
6126out_free_hdr_rq:
6127 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6128 phba->sli4_hba.hdr_rq = NULL;
6129out_free_fcp_wq:
6130 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6131 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6132 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6133 }
6134 kfree(phba->sli4_hba.fcp_wq);
6135out_free_els_wq:
6136 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6137 phba->sli4_hba.els_wq = NULL;
6138out_free_mbx_wq:
6139 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6140 phba->sli4_hba.mbx_wq = NULL;
6141out_free_fcp_cq:
6142 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6143 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6144 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6145 }
6146 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04006147out_free_els_cq:
6148 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6149 phba->sli4_hba.els_cq = NULL;
6150out_free_mbx_cq:
6151 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6152 phba->sli4_hba.mbx_cq = NULL;
6153out_free_fp_eq:
6154 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6155 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6156 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6157 }
6158 kfree(phba->sli4_hba.fp_eq);
6159out_free_sp_eq:
6160 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6161 phba->sli4_hba.sp_eq = NULL;
6162out_error:
6163 return -ENOMEM;
6164}
6165
6166/**
6167 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6168 * @phba: pointer to lpfc hba data structure.
6169 *
6170 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6171 * operation.
6172 *
6173 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006174 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006175 * -ENOMEM - No availble memory
6176 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006177 **/
6178static void
6179lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6180{
6181 int fcp_qidx;
6182
6183 /* Release mailbox command work queue */
6184 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6185 phba->sli4_hba.mbx_wq = NULL;
6186
6187 /* Release ELS work queue */
6188 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6189 phba->sli4_hba.els_wq = NULL;
6190
6191 /* Release FCP work queue */
6192 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6193 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
6194 kfree(phba->sli4_hba.fcp_wq);
6195 phba->sli4_hba.fcp_wq = NULL;
6196
6197 /* Release unsolicited receive queue */
6198 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6199 phba->sli4_hba.hdr_rq = NULL;
6200 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6201 phba->sli4_hba.dat_rq = NULL;
6202
James Smartda0436e2009-05-22 14:51:39 -04006203 /* Release ELS complete queue */
6204 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6205 phba->sli4_hba.els_cq = NULL;
6206
6207 /* Release mailbox command complete queue */
6208 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6209 phba->sli4_hba.mbx_cq = NULL;
6210
6211 /* Release FCP response complete queue */
6212 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6213 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
6214 kfree(phba->sli4_hba.fcp_cq);
6215 phba->sli4_hba.fcp_cq = NULL;
6216
6217 /* Release fast-path event queue */
6218 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6219 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
6220 kfree(phba->sli4_hba.fp_eq);
6221 phba->sli4_hba.fp_eq = NULL;
6222
6223 /* Release slow-path event queue */
6224 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6225 phba->sli4_hba.sp_eq = NULL;
6226
6227 return;
6228}
6229
6230/**
6231 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6232 * @phba: pointer to lpfc hba data structure.
6233 *
6234 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6235 * operation.
6236 *
6237 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006238 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006239 * -ENOMEM - No availble memory
6240 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006241 **/
6242int
6243lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6244{
6245 int rc = -ENOMEM;
6246 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6247 int fcp_cq_index = 0;
6248
6249 /*
6250 * Set up Event Queues (EQs)
6251 */
6252
6253 /* Set up slow-path event queue */
6254 if (!phba->sli4_hba.sp_eq) {
6255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6256 "0520 Slow-path EQ not allocated\n");
6257 goto out_error;
6258 }
6259 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6260 LPFC_SP_DEF_IMAX);
6261 if (rc) {
6262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6263 "0521 Failed setup of slow-path EQ: "
6264 "rc = 0x%x\n", rc);
6265 goto out_error;
6266 }
6267 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6268 "2583 Slow-path EQ setup: queue-id=%d\n",
6269 phba->sli4_hba.sp_eq->queue_id);
6270
6271 /* Set up fast-path event queue */
6272 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6273 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6275 "0522 Fast-path EQ (%d) not "
6276 "allocated\n", fcp_eqidx);
6277 goto out_destroy_fp_eq;
6278 }
6279 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6280 phba->cfg_fcp_imax);
6281 if (rc) {
6282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6283 "0523 Failed setup of fast-path EQ "
6284 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6285 goto out_destroy_fp_eq;
6286 }
6287 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6288 "2584 Fast-path EQ setup: "
6289 "queue[%d]-id=%d\n", fcp_eqidx,
6290 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6291 }
6292
6293 /*
6294 * Set up Complete Queues (CQs)
6295 */
6296
6297 /* Set up slow-path MBOX Complete Queue as the first CQ */
6298 if (!phba->sli4_hba.mbx_cq) {
6299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6300 "0528 Mailbox CQ not allocated\n");
6301 goto out_destroy_fp_eq;
6302 }
6303 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6304 LPFC_MCQ, LPFC_MBOX);
6305 if (rc) {
6306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6307 "0529 Failed setup of slow-path mailbox CQ: "
6308 "rc = 0x%x\n", rc);
6309 goto out_destroy_fp_eq;
6310 }
6311 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6312 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6313 phba->sli4_hba.mbx_cq->queue_id,
6314 phba->sli4_hba.sp_eq->queue_id);
6315
6316 /* Set up slow-path ELS Complete Queue */
6317 if (!phba->sli4_hba.els_cq) {
6318 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6319 "0530 ELS CQ not allocated\n");
6320 goto out_destroy_mbx_cq;
6321 }
6322 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6323 LPFC_WCQ, LPFC_ELS);
6324 if (rc) {
6325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6326 "0531 Failed setup of slow-path ELS CQ: "
6327 "rc = 0x%x\n", rc);
6328 goto out_destroy_mbx_cq;
6329 }
6330 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6331 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6332 phba->sli4_hba.els_cq->queue_id,
6333 phba->sli4_hba.sp_eq->queue_id);
6334
James Smartda0436e2009-05-22 14:51:39 -04006335 /* Set up fast-path FCP Response Complete Queue */
6336 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6337 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6339 "0526 Fast-path FCP CQ (%d) not "
6340 "allocated\n", fcp_cqidx);
6341 goto out_destroy_fcp_cq;
6342 }
6343 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
6344 phba->sli4_hba.fp_eq[fcp_cqidx],
6345 LPFC_WCQ, LPFC_FCP);
6346 if (rc) {
6347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6348 "0527 Failed setup of fast-path FCP "
6349 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6350 goto out_destroy_fcp_cq;
6351 }
6352 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6353 "2588 FCP CQ setup: cq[%d]-id=%d, "
6354 "parent eq[%d]-id=%d\n",
6355 fcp_cqidx,
6356 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6357 fcp_cqidx,
6358 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
6359 }
6360
6361 /*
6362 * Set up all the Work Queues (WQs)
6363 */
6364
6365 /* Set up Mailbox Command Queue */
6366 if (!phba->sli4_hba.mbx_wq) {
6367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6368 "0538 Slow-path MQ not allocated\n");
6369 goto out_destroy_fcp_cq;
6370 }
6371 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6372 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6373 if (rc) {
6374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6375 "0539 Failed setup of slow-path MQ: "
6376 "rc = 0x%x\n", rc);
6377 goto out_destroy_fcp_cq;
6378 }
6379 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6380 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6381 phba->sli4_hba.mbx_wq->queue_id,
6382 phba->sli4_hba.mbx_cq->queue_id);
6383
6384 /* Set up slow-path ELS Work Queue */
6385 if (!phba->sli4_hba.els_wq) {
6386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6387 "0536 Slow-path ELS WQ not allocated\n");
6388 goto out_destroy_mbx_wq;
6389 }
6390 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6391 phba->sli4_hba.els_cq, LPFC_ELS);
6392 if (rc) {
6393 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6394 "0537 Failed setup of slow-path ELS WQ: "
6395 "rc = 0x%x\n", rc);
6396 goto out_destroy_mbx_wq;
6397 }
6398 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6399 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6400 phba->sli4_hba.els_wq->queue_id,
6401 phba->sli4_hba.els_cq->queue_id);
6402
6403 /* Set up fast-path FCP Work Queue */
6404 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6405 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6407 "0534 Fast-path FCP WQ (%d) not "
6408 "allocated\n", fcp_wqidx);
6409 goto out_destroy_fcp_wq;
6410 }
6411 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6412 phba->sli4_hba.fcp_cq[fcp_cq_index],
6413 LPFC_FCP);
6414 if (rc) {
6415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6416 "0535 Failed setup of fast-path FCP "
6417 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6418 goto out_destroy_fcp_wq;
6419 }
6420 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6421 "2591 FCP WQ setup: wq[%d]-id=%d, "
6422 "parent cq[%d]-id=%d\n",
6423 fcp_wqidx,
6424 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6425 fcp_cq_index,
6426 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6427 /* Round robin FCP Work Queue's Completion Queue assignment */
6428 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
6429 }
6430
6431 /*
6432 * Create Receive Queue (RQ)
6433 */
6434 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6436 "0540 Receive Queue not allocated\n");
6437 goto out_destroy_fcp_wq;
6438 }
6439 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006440 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006441 if (rc) {
6442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6443 "0541 Failed setup of Receive Queue: "
6444 "rc = 0x%x\n", rc);
6445 goto out_destroy_fcp_wq;
6446 }
6447 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6448 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6449 "parent cq-id=%d\n",
6450 phba->sli4_hba.hdr_rq->queue_id,
6451 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006452 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006453 return 0;
6454
6455out_destroy_fcp_wq:
6456 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6457 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6458 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6459out_destroy_mbx_wq:
6460 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6461out_destroy_fcp_cq:
6462 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6463 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04006464 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6465out_destroy_mbx_cq:
6466 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6467out_destroy_fp_eq:
6468 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6469 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6470 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6471out_error:
6472 return rc;
6473}
6474
6475/**
6476 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6477 * @phba: pointer to lpfc hba data structure.
6478 *
6479 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6480 * operation.
6481 *
6482 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006483 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006484 * -ENOMEM - No availble memory
6485 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006486 **/
6487void
6488lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6489{
6490 int fcp_qidx;
6491
6492 /* Unset mailbox command work queue */
6493 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6494 /* Unset ELS work queue */
6495 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6496 /* Unset unsolicited receive queue */
6497 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6498 /* Unset FCP work queue */
6499 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6500 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6501 /* Unset mailbox command complete queue */
6502 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6503 /* Unset ELS complete queue */
6504 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006505 /* Unset FCP response complete queue */
6506 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6507 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6508 /* Unset fast-path event queue */
6509 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6510 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6511 /* Unset slow-path event queue */
6512 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6513}
6514
6515/**
6516 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6517 * @phba: pointer to lpfc hba data structure.
6518 *
6519 * This routine is invoked to allocate and set up a pool of completion queue
6520 * events. The body of the completion queue event is a completion queue entry
6521 * CQE. For now, this pool is used for the interrupt service routine to queue
6522 * the following HBA completion queue events for the worker thread to process:
6523 * - Mailbox asynchronous events
6524 * - Receive queue completion unsolicited events
6525 * Later, this can be used for all the slow-path events.
6526 *
6527 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006528 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006529 * -ENOMEM - No availble memory
6530 **/
6531static int
6532lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6533{
6534 struct lpfc_cq_event *cq_event;
6535 int i;
6536
6537 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6538 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6539 if (!cq_event)
6540 goto out_pool_create_fail;
6541 list_add_tail(&cq_event->list,
6542 &phba->sli4_hba.sp_cqe_event_pool);
6543 }
6544 return 0;
6545
6546out_pool_create_fail:
6547 lpfc_sli4_cq_event_pool_destroy(phba);
6548 return -ENOMEM;
6549}
6550
6551/**
6552 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6553 * @phba: pointer to lpfc hba data structure.
6554 *
6555 * This routine is invoked to free the pool of completion queue events at
6556 * driver unload time. Note that, it is the responsibility of the driver
6557 * cleanup routine to free all the outstanding completion-queue events
6558 * allocated from this pool back into the pool before invoking this routine
6559 * to destroy the pool.
6560 **/
6561static void
6562lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6563{
6564 struct lpfc_cq_event *cq_event, *next_cq_event;
6565
6566 list_for_each_entry_safe(cq_event, next_cq_event,
6567 &phba->sli4_hba.sp_cqe_event_pool, list) {
6568 list_del(&cq_event->list);
6569 kfree(cq_event);
6570 }
6571}
6572
6573/**
6574 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6575 * @phba: pointer to lpfc hba data structure.
6576 *
6577 * This routine is the lock free version of the API invoked to allocate a
6578 * completion-queue event from the free pool.
6579 *
6580 * Return: Pointer to the newly allocated completion-queue event if successful
6581 * NULL otherwise.
6582 **/
6583struct lpfc_cq_event *
6584__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6585{
6586 struct lpfc_cq_event *cq_event = NULL;
6587
6588 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6589 struct lpfc_cq_event, list);
6590 return cq_event;
6591}
6592
6593/**
6594 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6595 * @phba: pointer to lpfc hba data structure.
6596 *
6597 * This routine is the lock version of the API invoked to allocate a
6598 * completion-queue event from the free pool.
6599 *
6600 * Return: Pointer to the newly allocated completion-queue event if successful
6601 * NULL otherwise.
6602 **/
6603struct lpfc_cq_event *
6604lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6605{
6606 struct lpfc_cq_event *cq_event;
6607 unsigned long iflags;
6608
6609 spin_lock_irqsave(&phba->hbalock, iflags);
6610 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6611 spin_unlock_irqrestore(&phba->hbalock, iflags);
6612 return cq_event;
6613}
6614
6615/**
6616 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6617 * @phba: pointer to lpfc hba data structure.
6618 * @cq_event: pointer to the completion queue event to be freed.
6619 *
6620 * This routine is the lock free version of the API invoked to release a
6621 * completion-queue event back into the free pool.
6622 **/
6623void
6624__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6625 struct lpfc_cq_event *cq_event)
6626{
6627 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6628}
6629
6630/**
6631 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6632 * @phba: pointer to lpfc hba data structure.
6633 * @cq_event: pointer to the completion queue event to be freed.
6634 *
6635 * This routine is the lock version of the API invoked to release a
6636 * completion-queue event back into the free pool.
6637 **/
6638void
6639lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6640 struct lpfc_cq_event *cq_event)
6641{
6642 unsigned long iflags;
6643 spin_lock_irqsave(&phba->hbalock, iflags);
6644 __lpfc_sli4_cq_event_release(phba, cq_event);
6645 spin_unlock_irqrestore(&phba->hbalock, iflags);
6646}
6647
6648/**
6649 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6650 * @phba: pointer to lpfc hba data structure.
6651 *
6652 * This routine is to free all the pending completion-queue events to the
6653 * back into the free pool for device reset.
6654 **/
6655static void
6656lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6657{
6658 LIST_HEAD(cqelist);
6659 struct lpfc_cq_event *cqe;
6660 unsigned long iflags;
6661
6662 /* Retrieve all the pending WCQEs from pending WCQE lists */
6663 spin_lock_irqsave(&phba->hbalock, iflags);
6664 /* Pending FCP XRI abort events */
6665 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6666 &cqelist);
6667 /* Pending ELS XRI abort events */
6668 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6669 &cqelist);
6670 /* Pending asynnc events */
6671 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6672 &cqelist);
6673 spin_unlock_irqrestore(&phba->hbalock, iflags);
6674
6675 while (!list_empty(&cqelist)) {
6676 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6677 lpfc_sli4_cq_event_release(phba, cqe);
6678 }
6679}
6680
6681/**
6682 * lpfc_pci_function_reset - Reset pci function.
6683 * @phba: pointer to lpfc hba data structure.
6684 *
6685 * This routine is invoked to request a PCI function reset. It will destroys
6686 * all resources assigned to the PCI function which originates this request.
6687 *
6688 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006689 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006690 * -ENOMEM - No availble memory
6691 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006692 **/
6693int
6694lpfc_pci_function_reset(struct lpfc_hba *phba)
6695{
6696 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006697 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04006698 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05006699 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04006700 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05006701 struct lpfc_register reg_data;
James Smartda0436e2009-05-22 14:51:39 -04006702
James Smart2fcee4b2010-12-15 17:57:46 -05006703 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6704 switch (if_type) {
6705 case LPFC_SLI_INTF_IF_TYPE_0:
6706 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6707 GFP_KERNEL);
6708 if (!mboxq) {
6709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6710 "0494 Unable to allocate memory for "
6711 "issuing SLI_FUNCTION_RESET mailbox "
6712 "command\n");
6713 return -ENOMEM;
6714 }
6715
6716 /* Setup PCI function reset mailbox-ioctl command */
6717 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6718 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6719 LPFC_SLI4_MBX_EMBED);
6720 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6721 shdr = (union lpfc_sli4_cfg_shdr *)
6722 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6723 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6724 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6725 &shdr->response);
6726 if (rc != MBX_TIMEOUT)
6727 mempool_free(mboxq, phba->mbox_mem_pool);
6728 if (shdr_status || shdr_add_status || rc) {
6729 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6730 "0495 SLI_FUNCTION_RESET mailbox "
6731 "failed with status x%x add_status x%x,"
6732 " mbx status x%x\n",
6733 shdr_status, shdr_add_status, rc);
6734 rc = -ENXIO;
6735 }
6736 break;
6737 case LPFC_SLI_INTF_IF_TYPE_2:
6738 for (num_resets = 0;
6739 num_resets < MAX_IF_TYPE_2_RESETS;
6740 num_resets++) {
6741 reg_data.word0 = 0;
6742 bf_set(lpfc_sliport_ctrl_end, &reg_data,
6743 LPFC_SLIPORT_LITTLE_ENDIAN);
6744 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
6745 LPFC_SLIPORT_INIT_PORT);
6746 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
6747 CTRLregaddr);
6748
6749 /*
6750 * Poll the Port Status Register and wait for RDY for
6751 * up to 10 seconds. If the port doesn't respond, treat
6752 * it as an error. If the port responds with RN, start
6753 * the loop again.
6754 */
6755 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
6756 reg_data.word0 =
6757 readl(phba->sli4_hba.u.if_type2.
6758 STATUSregaddr);
6759 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
6760 break;
6761 if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
6762 reset_again++;
6763 break;
6764 }
6765 msleep(10);
6766 }
6767
6768 /*
6769 * If the port responds to the init request with
6770 * reset needed, delay for a bit and restart the loop.
6771 */
6772 if (reset_again) {
6773 msleep(10);
6774 reset_again = 0;
6775 continue;
6776 }
6777
6778 /* Detect any port errors. */
6779 reg_data.word0 = readl(phba->sli4_hba.u.if_type2.
6780 STATUSregaddr);
6781 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
6782 (rdy_chk >= 1000)) {
6783 phba->work_status[0] = readl(
6784 phba->sli4_hba.u.if_type2.ERR1regaddr);
6785 phba->work_status[1] = readl(
6786 phba->sli4_hba.u.if_type2.ERR2regaddr);
6787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6788 "2890 Port Error Detected "
6789 "during Port Reset: "
6790 "port status reg 0x%x, "
6791 "error 1=0x%x, error 2=0x%x\n",
6792 reg_data.word0,
6793 phba->work_status[0],
6794 phba->work_status[1]);
6795 rc = -ENODEV;
6796 }
6797
6798 /*
6799 * Terminate the outer loop provided the Port indicated
6800 * ready within 10 seconds.
6801 */
6802 if (rdy_chk < 1000)
6803 break;
6804 }
6805 break;
6806 case LPFC_SLI_INTF_IF_TYPE_1:
6807 default:
6808 break;
James Smartda0436e2009-05-22 14:51:39 -04006809 }
6810
James Smart2fcee4b2010-12-15 17:57:46 -05006811 /* Catch the not-ready port failure after a port reset. */
6812 if (num_resets >= MAX_IF_TYPE_2_RESETS)
6813 rc = -ENODEV;
6814
James Smartda0436e2009-05-22 14:51:39 -04006815 return rc;
6816}
6817
6818/**
6819 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6820 * @phba: pointer to lpfc hba data structure.
6821 * @cnt: number of nop mailbox commands to send.
6822 *
6823 * This routine is invoked to send a number @cnt of NOP mailbox command and
6824 * wait for each command to complete.
6825 *
6826 * Return: the number of NOP mailbox command completed.
6827 **/
6828static int
6829lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6830{
6831 LPFC_MBOXQ_t *mboxq;
6832 int length, cmdsent;
6833 uint32_t mbox_tmo;
6834 uint32_t rc = 0;
6835 uint32_t shdr_status, shdr_add_status;
6836 union lpfc_sli4_cfg_shdr *shdr;
6837
6838 if (cnt == 0) {
6839 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6840 "2518 Requested to send 0 NOP mailbox cmd\n");
6841 return cnt;
6842 }
6843
6844 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6845 if (!mboxq) {
6846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6847 "2519 Unable to allocate memory for issuing "
6848 "NOP mailbox command\n");
6849 return 0;
6850 }
6851
6852 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6853 length = (sizeof(struct lpfc_mbx_nop) -
6854 sizeof(struct lpfc_sli4_cfg_mhdr));
6855 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6856 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6857
6858 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6859 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6860 if (!phba->sli4_hba.intr_enable)
6861 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6862 else
6863 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6864 if (rc == MBX_TIMEOUT)
6865 break;
6866 /* Check return status */
6867 shdr = (union lpfc_sli4_cfg_shdr *)
6868 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6869 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6870 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6871 &shdr->response);
6872 if (shdr_status || shdr_add_status || rc) {
6873 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6874 "2520 NOP mailbox command failed "
6875 "status x%x add_status x%x mbx "
6876 "status x%x\n", shdr_status,
6877 shdr_add_status, rc);
6878 break;
6879 }
6880 }
6881
6882 if (rc != MBX_TIMEOUT)
6883 mempool_free(mboxq, phba->mbox_mem_pool);
6884
6885 return cmdsent;
6886}
6887
6888/**
James Smartda0436e2009-05-22 14:51:39 -04006889 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6890 * @phba: pointer to lpfc hba data structure.
6891 *
6892 * This routine is invoked to set up the PCI device memory space for device
6893 * with SLI-4 interface spec.
6894 *
6895 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006896 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006897 * other values - error
6898 **/
6899static int
6900lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6901{
6902 struct pci_dev *pdev;
6903 unsigned long bar0map_len, bar1map_len, bar2map_len;
6904 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05006905 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006906
6907 /* Obtain PCI device reference */
6908 if (!phba->pcidev)
6909 return error;
6910 else
6911 pdev = phba->pcidev;
6912
6913 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006914 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6915 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6916 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6917 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006918 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006919 }
6920 }
James Smartda0436e2009-05-22 14:51:39 -04006921
James Smart2fcee4b2010-12-15 17:57:46 -05006922 /*
6923 * The BARs and register set definitions and offset locations are
6924 * dependent on the if_type.
6925 */
6926 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
6927 &phba->sli4_hba.sli_intf.word0)) {
6928 return error;
6929 }
6930
6931 /* There is no SLI3 failback for SLI4 devices. */
6932 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
6933 LPFC_SLI_INTF_VALID) {
6934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6935 "2894 SLI_INTF reg contents invalid "
6936 "sli_intf reg 0x%x\n",
6937 phba->sli4_hba.sli_intf.word0);
6938 return error;
6939 }
6940
6941 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6942 /*
6943 * Get the bus address of SLI4 device Bar regions and the
6944 * number of bytes required by each mapping. The mapping of the
6945 * particular PCI BARs regions is dependent on the type of
6946 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006947 */
James Smart1dfb5a42010-02-12 14:40:50 -05006948 if (pci_resource_start(pdev, 0)) {
6949 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6950 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05006951
6952 /*
6953 * Map SLI4 PCI Config Space Register base to a kernel virtual
6954 * addr
6955 */
6956 phba->sli4_hba.conf_regs_memmap_p =
6957 ioremap(phba->pci_bar0_map, bar0map_len);
6958 if (!phba->sli4_hba.conf_regs_memmap_p) {
6959 dev_printk(KERN_ERR, &pdev->dev,
6960 "ioremap failed for SLI4 PCI config "
6961 "registers.\n");
6962 goto out;
6963 }
6964 /* Set up BAR0 PCI config space register memory map */
6965 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05006966 } else {
6967 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6968 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05006969 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
6970 dev_printk(KERN_ERR, &pdev->dev,
6971 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
6972 goto out;
6973 }
6974 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04006975 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05006976 if (!phba->sli4_hba.conf_regs_memmap_p) {
6977 dev_printk(KERN_ERR, &pdev->dev,
6978 "ioremap failed for SLI4 PCI config "
6979 "registers.\n");
6980 goto out;
6981 }
6982 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04006983 }
6984
James Smart2fcee4b2010-12-15 17:57:46 -05006985 if (pci_resource_start(pdev, 2)) {
6986 /*
6987 * Map SLI4 if type 0 HBA Control Register base to a kernel
6988 * virtual address and setup the registers.
6989 */
6990 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6991 bar1map_len = pci_resource_len(pdev, 2);
6992 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04006993 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05006994 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6995 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04006996 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05006997 goto out_iounmap_conf;
6998 }
6999 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007000 }
7001
James Smart2fcee4b2010-12-15 17:57:46 -05007002 if (pci_resource_start(pdev, 4)) {
7003 /*
7004 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7005 * virtual address and setup the registers.
7006 */
7007 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7008 bar2map_len = pci_resource_len(pdev, 4);
7009 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007010 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007011 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7012 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007013 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007014 goto out_iounmap_ctrl;
7015 }
7016 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7017 if (error)
7018 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007019 }
7020
James Smartda0436e2009-05-22 14:51:39 -04007021 return 0;
7022
7023out_iounmap_all:
7024 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7025out_iounmap_ctrl:
7026 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7027out_iounmap_conf:
7028 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7029out:
7030 return error;
7031}
7032
7033/**
7034 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7035 * @phba: pointer to lpfc hba data structure.
7036 *
7037 * This routine is invoked to unset the PCI device memory space for device
7038 * with SLI-4 interface spec.
7039 **/
7040static void
7041lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7042{
7043 struct pci_dev *pdev;
7044
7045 /* Obtain PCI device reference */
7046 if (!phba->pcidev)
7047 return;
7048 else
7049 pdev = phba->pcidev;
7050
7051 /* Free coherent DMA memory allocated */
7052
7053 /* Unmap I/O memory space */
7054 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7055 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7056 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7057
7058 return;
7059}
7060
7061/**
James Smart3772a992009-05-22 14:50:54 -04007062 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7063 * @phba: pointer to lpfc hba data structure.
7064 *
7065 * This routine is invoked to enable the MSI-X interrupt vectors to device
7066 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7067 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7068 * invoked, enables either all or nothing, depending on the current
7069 * availability of PCI vector resources. The device driver is responsible
7070 * for calling the individual request_irq() to register each MSI-X vector
7071 * with a interrupt handler, which is done in this function. Note that
7072 * later when device is unloading, the driver should always call free_irq()
7073 * on all MSI-X vectors it has done request_irq() on before calling
7074 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7075 * will be left with MSI-X enabled and leaks its vectors.
7076 *
7077 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007078 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007079 * other values - error
7080 **/
7081static int
7082lpfc_sli_enable_msix(struct lpfc_hba *phba)
7083{
7084 int rc, i;
7085 LPFC_MBOXQ_t *pmb;
7086
7087 /* Set up MSI-X multi-message vectors */
7088 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7089 phba->msix_entries[i].entry = i;
7090
7091 /* Configure MSI-X capability structure */
7092 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7093 ARRAY_SIZE(phba->msix_entries));
7094 if (rc) {
7095 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7096 "0420 PCI enable MSI-X failed (%d)\n", rc);
7097 goto msi_fail_out;
7098 }
7099 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7100 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7101 "0477 MSI-X entry[%d]: vector=x%x "
7102 "message=%d\n", i,
7103 phba->msix_entries[i].vector,
7104 phba->msix_entries[i].entry);
7105 /*
7106 * Assign MSI-X vectors to interrupt handlers
7107 */
7108
7109 /* vector-0 is associated to slow-path handler */
7110 rc = request_irq(phba->msix_entries[0].vector,
7111 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7112 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7113 if (rc) {
7114 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7115 "0421 MSI-X slow-path request_irq failed "
7116 "(%d)\n", rc);
7117 goto msi_fail_out;
7118 }
7119
7120 /* vector-1 is associated to fast-path handler */
7121 rc = request_irq(phba->msix_entries[1].vector,
7122 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7123 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7124
7125 if (rc) {
7126 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7127 "0429 MSI-X fast-path request_irq failed "
7128 "(%d)\n", rc);
7129 goto irq_fail_out;
7130 }
7131
7132 /*
7133 * Configure HBA MSI-X attention conditions to messages
7134 */
7135 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7136
7137 if (!pmb) {
7138 rc = -ENOMEM;
7139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7140 "0474 Unable to allocate memory for issuing "
7141 "MBOX_CONFIG_MSI command\n");
7142 goto mem_fail_out;
7143 }
7144 rc = lpfc_config_msi(phba, pmb);
7145 if (rc)
7146 goto mbx_fail_out;
7147 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7148 if (rc != MBX_SUCCESS) {
7149 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7150 "0351 Config MSI mailbox command failed, "
7151 "mbxCmd x%x, mbxStatus x%x\n",
7152 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7153 goto mbx_fail_out;
7154 }
7155
7156 /* Free memory allocated for mailbox command */
7157 mempool_free(pmb, phba->mbox_mem_pool);
7158 return rc;
7159
7160mbx_fail_out:
7161 /* Free memory allocated for mailbox command */
7162 mempool_free(pmb, phba->mbox_mem_pool);
7163
7164mem_fail_out:
7165 /* free the irq already requested */
7166 free_irq(phba->msix_entries[1].vector, phba);
7167
7168irq_fail_out:
7169 /* free the irq already requested */
7170 free_irq(phba->msix_entries[0].vector, phba);
7171
7172msi_fail_out:
7173 /* Unconfigure MSI-X capability structure */
7174 pci_disable_msix(phba->pcidev);
7175 return rc;
7176}
7177
7178/**
7179 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7180 * @phba: pointer to lpfc hba data structure.
7181 *
7182 * This routine is invoked to release the MSI-X vectors and then disable the
7183 * MSI-X interrupt mode to device with SLI-3 interface spec.
7184 **/
7185static void
7186lpfc_sli_disable_msix(struct lpfc_hba *phba)
7187{
7188 int i;
7189
7190 /* Free up MSI-X multi-message vectors */
7191 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7192 free_irq(phba->msix_entries[i].vector, phba);
7193 /* Disable MSI-X */
7194 pci_disable_msix(phba->pcidev);
7195
7196 return;
7197}
7198
7199/**
7200 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7201 * @phba: pointer to lpfc hba data structure.
7202 *
7203 * This routine is invoked to enable the MSI interrupt mode to device with
7204 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7205 * enable the MSI vector. The device driver is responsible for calling the
7206 * request_irq() to register MSI vector with a interrupt the handler, which
7207 * is done in this function.
7208 *
7209 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007210 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007211 * other values - error
7212 */
7213static int
7214lpfc_sli_enable_msi(struct lpfc_hba *phba)
7215{
7216 int rc;
7217
7218 rc = pci_enable_msi(phba->pcidev);
7219 if (!rc)
7220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7221 "0462 PCI enable MSI mode success.\n");
7222 else {
7223 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7224 "0471 PCI enable MSI mode failed (%d)\n", rc);
7225 return rc;
7226 }
7227
7228 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7229 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7230 if (rc) {
7231 pci_disable_msi(phba->pcidev);
7232 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7233 "0478 MSI request_irq failed (%d)\n", rc);
7234 }
7235 return rc;
7236}
7237
7238/**
7239 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7240 * @phba: pointer to lpfc hba data structure.
7241 *
7242 * This routine is invoked to disable the MSI interrupt mode to device with
7243 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7244 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7245 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7246 * its vector.
7247 */
7248static void
7249lpfc_sli_disable_msi(struct lpfc_hba *phba)
7250{
7251 free_irq(phba->pcidev->irq, phba);
7252 pci_disable_msi(phba->pcidev);
7253 return;
7254}
7255
7256/**
7257 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
7258 * @phba: pointer to lpfc hba data structure.
7259 *
7260 * This routine is invoked to enable device interrupt and associate driver's
7261 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
7262 * spec. Depends on the interrupt mode configured to the driver, the driver
7263 * will try to fallback from the configured interrupt mode to an interrupt
7264 * mode which is supported by the platform, kernel, and device in the order
7265 * of:
7266 * MSI-X -> MSI -> IRQ.
7267 *
7268 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007269 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007270 * other values - error
7271 **/
7272static uint32_t
7273lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7274{
7275 uint32_t intr_mode = LPFC_INTR_ERROR;
7276 int retval;
7277
7278 if (cfg_mode == 2) {
7279 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
7280 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
7281 if (!retval) {
7282 /* Now, try to enable MSI-X interrupt mode */
7283 retval = lpfc_sli_enable_msix(phba);
7284 if (!retval) {
7285 /* Indicate initialization to MSI-X mode */
7286 phba->intr_type = MSIX;
7287 intr_mode = 2;
7288 }
7289 }
7290 }
7291
7292 /* Fallback to MSI if MSI-X initialization failed */
7293 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7294 retval = lpfc_sli_enable_msi(phba);
7295 if (!retval) {
7296 /* Indicate initialization to MSI mode */
7297 phba->intr_type = MSI;
7298 intr_mode = 1;
7299 }
7300 }
7301
7302 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7303 if (phba->intr_type == NONE) {
7304 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7305 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7306 if (!retval) {
7307 /* Indicate initialization to INTx mode */
7308 phba->intr_type = INTx;
7309 intr_mode = 0;
7310 }
7311 }
7312 return intr_mode;
7313}
7314
7315/**
7316 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7317 * @phba: pointer to lpfc hba data structure.
7318 *
7319 * This routine is invoked to disable device interrupt and disassociate the
7320 * driver's interrupt handler(s) from interrupt vector(s) to device with
7321 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7322 * release the interrupt vector(s) for the message signaled interrupt.
7323 **/
7324static void
7325lpfc_sli_disable_intr(struct lpfc_hba *phba)
7326{
7327 /* Disable the currently initialized interrupt mode */
7328 if (phba->intr_type == MSIX)
7329 lpfc_sli_disable_msix(phba);
7330 else if (phba->intr_type == MSI)
7331 lpfc_sli_disable_msi(phba);
7332 else if (phba->intr_type == INTx)
7333 free_irq(phba->pcidev->irq, phba);
7334
7335 /* Reset interrupt management states */
7336 phba->intr_type = NONE;
7337 phba->sli.slistat.sli_intr = 0;
7338
7339 return;
7340}
7341
7342/**
James Smartda0436e2009-05-22 14:51:39 -04007343 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7344 * @phba: pointer to lpfc hba data structure.
7345 *
7346 * This routine is invoked to enable the MSI-X interrupt vectors to device
7347 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7348 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7349 * enables either all or nothing, depending on the current availability of
7350 * PCI vector resources. The device driver is responsible for calling the
7351 * individual request_irq() to register each MSI-X vector with a interrupt
7352 * handler, which is done in this function. Note that later when device is
7353 * unloading, the driver should always call free_irq() on all MSI-X vectors
7354 * it has done request_irq() on before calling pci_disable_msix(). Failure
7355 * to do so results in a BUG_ON() and a device will be left with MSI-X
7356 * enabled and leaks its vectors.
7357 *
7358 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007359 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007360 * other values - error
7361 **/
7362static int
7363lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7364{
James Smart75baf692010-06-08 18:31:21 -04007365 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04007366
7367 /* Set up MSI-X multi-message vectors */
7368 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7369 phba->sli4_hba.msix_entries[index].entry = index;
7370
7371 /* Configure MSI-X capability structure */
James Smart75baf692010-06-08 18:31:21 -04007372 vectors = phba->sli4_hba.cfg_eqn;
7373enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04007374 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04007375 vectors);
7376 if (rc > 1) {
7377 vectors = rc;
7378 goto enable_msix_vectors;
7379 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04007380 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7381 "0484 PCI enable MSI-X failed (%d)\n", rc);
7382 goto msi_fail_out;
7383 }
James Smart75baf692010-06-08 18:31:21 -04007384
James Smartda0436e2009-05-22 14:51:39 -04007385 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04007386 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04007387 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7388 "0489 MSI-X entry[%d]: vector=x%x "
7389 "message=%d\n", index,
7390 phba->sli4_hba.msix_entries[index].vector,
7391 phba->sli4_hba.msix_entries[index].entry);
7392 /*
7393 * Assign MSI-X vectors to interrupt handlers
7394 */
7395
7396 /* The first vector must associated to slow-path handler for MQ */
7397 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7398 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7399 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7400 if (rc) {
7401 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7402 "0485 MSI-X slow-path request_irq failed "
7403 "(%d)\n", rc);
7404 goto msi_fail_out;
7405 }
7406
7407 /* The rest of the vector(s) are associated to fast-path handler(s) */
James Smart75baf692010-06-08 18:31:21 -04007408 for (index = 1; index < vectors; index++) {
James Smartda0436e2009-05-22 14:51:39 -04007409 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7410 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7411 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7412 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7413 LPFC_FP_DRIVER_HANDLER_NAME,
7414 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7415 if (rc) {
7416 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7417 "0486 MSI-X fast-path (%d) "
7418 "request_irq failed (%d)\n", index, rc);
7419 goto cfg_fail_out;
7420 }
7421 }
James Smart75baf692010-06-08 18:31:21 -04007422 phba->sli4_hba.msix_vec_nr = vectors;
James Smartda0436e2009-05-22 14:51:39 -04007423
7424 return rc;
7425
7426cfg_fail_out:
7427 /* free the irq already requested */
7428 for (--index; index >= 1; index--)
7429 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7430 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7431
7432 /* free the irq already requested */
7433 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7434
7435msi_fail_out:
7436 /* Unconfigure MSI-X capability structure */
7437 pci_disable_msix(phba->pcidev);
7438 return rc;
7439}
7440
7441/**
7442 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7443 * @phba: pointer to lpfc hba data structure.
7444 *
7445 * This routine is invoked to release the MSI-X vectors and then disable the
7446 * MSI-X interrupt mode to device with SLI-4 interface spec.
7447 **/
7448static void
7449lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7450{
7451 int index;
7452
7453 /* Free up MSI-X multi-message vectors */
7454 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7455
James Smart75baf692010-06-08 18:31:21 -04007456 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
James Smartda0436e2009-05-22 14:51:39 -04007457 free_irq(phba->sli4_hba.msix_entries[index].vector,
7458 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
James Smart75baf692010-06-08 18:31:21 -04007459
James Smartda0436e2009-05-22 14:51:39 -04007460 /* Disable MSI-X */
7461 pci_disable_msix(phba->pcidev);
7462
7463 return;
7464}
7465
7466/**
7467 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7468 * @phba: pointer to lpfc hba data structure.
7469 *
7470 * This routine is invoked to enable the MSI interrupt mode to device with
7471 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7472 * to enable the MSI vector. The device driver is responsible for calling
7473 * the request_irq() to register MSI vector with a interrupt the handler,
7474 * which is done in this function.
7475 *
7476 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007477 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007478 * other values - error
7479 **/
7480static int
7481lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7482{
7483 int rc, index;
7484
7485 rc = pci_enable_msi(phba->pcidev);
7486 if (!rc)
7487 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7488 "0487 PCI enable MSI mode success.\n");
7489 else {
7490 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7491 "0488 PCI enable MSI mode failed (%d)\n", rc);
7492 return rc;
7493 }
7494
7495 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7496 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7497 if (rc) {
7498 pci_disable_msi(phba->pcidev);
7499 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7500 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04007501 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007502 }
7503
7504 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7505 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7506 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7507 }
7508
James Smart75baf692010-06-08 18:31:21 -04007509 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007510}
7511
7512/**
7513 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7514 * @phba: pointer to lpfc hba data structure.
7515 *
7516 * This routine is invoked to disable the MSI interrupt mode to device with
7517 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7518 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7519 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7520 * its vector.
7521 **/
7522static void
7523lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7524{
7525 free_irq(phba->pcidev->irq, phba);
7526 pci_disable_msi(phba->pcidev);
7527 return;
7528}
7529
7530/**
7531 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7532 * @phba: pointer to lpfc hba data structure.
7533 *
7534 * This routine is invoked to enable device interrupt and associate driver's
7535 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7536 * interface spec. Depends on the interrupt mode configured to the driver,
7537 * the driver will try to fallback from the configured interrupt mode to an
7538 * interrupt mode which is supported by the platform, kernel, and device in
7539 * the order of:
7540 * MSI-X -> MSI -> IRQ.
7541 *
7542 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007543 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007544 * other values - error
7545 **/
7546static uint32_t
7547lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7548{
7549 uint32_t intr_mode = LPFC_INTR_ERROR;
7550 int retval, index;
7551
7552 if (cfg_mode == 2) {
7553 /* Preparation before conf_msi mbox cmd */
7554 retval = 0;
7555 if (!retval) {
7556 /* Now, try to enable MSI-X interrupt mode */
7557 retval = lpfc_sli4_enable_msix(phba);
7558 if (!retval) {
7559 /* Indicate initialization to MSI-X mode */
7560 phba->intr_type = MSIX;
7561 intr_mode = 2;
7562 }
7563 }
7564 }
7565
7566 /* Fallback to MSI if MSI-X initialization failed */
7567 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7568 retval = lpfc_sli4_enable_msi(phba);
7569 if (!retval) {
7570 /* Indicate initialization to MSI mode */
7571 phba->intr_type = MSI;
7572 intr_mode = 1;
7573 }
7574 }
7575
7576 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7577 if (phba->intr_type == NONE) {
7578 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7579 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7580 if (!retval) {
7581 /* Indicate initialization to INTx mode */
7582 phba->intr_type = INTx;
7583 intr_mode = 0;
7584 for (index = 0; index < phba->cfg_fcp_eq_count;
7585 index++) {
7586 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7587 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7588 }
7589 }
7590 }
7591 return intr_mode;
7592}
7593
7594/**
7595 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7596 * @phba: pointer to lpfc hba data structure.
7597 *
7598 * This routine is invoked to disable device interrupt and disassociate
7599 * the driver's interrupt handler(s) from interrupt vector(s) to device
7600 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7601 * will release the interrupt vector(s) for the message signaled interrupt.
7602 **/
7603static void
7604lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7605{
7606 /* Disable the currently initialized interrupt mode */
7607 if (phba->intr_type == MSIX)
7608 lpfc_sli4_disable_msix(phba);
7609 else if (phba->intr_type == MSI)
7610 lpfc_sli4_disable_msi(phba);
7611 else if (phba->intr_type == INTx)
7612 free_irq(phba->pcidev->irq, phba);
7613
7614 /* Reset interrupt management states */
7615 phba->intr_type = NONE;
7616 phba->sli.slistat.sli_intr = 0;
7617
7618 return;
7619}
7620
7621/**
James Smart3772a992009-05-22 14:50:54 -04007622 * lpfc_unset_hba - Unset SLI3 hba device initialization
7623 * @phba: pointer to lpfc hba data structure.
7624 *
7625 * This routine is invoked to unset the HBA device initialization steps to
7626 * a device with SLI-3 interface spec.
7627 **/
7628static void
7629lpfc_unset_hba(struct lpfc_hba *phba)
7630{
7631 struct lpfc_vport *vport = phba->pport;
7632 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7633
7634 spin_lock_irq(shost->host_lock);
7635 vport->load_flag |= FC_UNLOADING;
7636 spin_unlock_irq(shost->host_lock);
7637
7638 lpfc_stop_hba_timers(phba);
7639
7640 phba->pport->work_port_events = 0;
7641
7642 lpfc_sli_hba_down(phba);
7643
7644 lpfc_sli_brdrestart(phba);
7645
7646 lpfc_sli_disable_intr(phba);
7647
7648 return;
7649}
7650
7651/**
James Smartda0436e2009-05-22 14:51:39 -04007652 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7653 * @phba: pointer to lpfc hba data structure.
7654 *
7655 * This routine is invoked to unset the HBA device initialization steps to
7656 * a device with SLI-4 interface spec.
7657 **/
7658static void
7659lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7660{
7661 struct lpfc_vport *vport = phba->pport;
7662 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7663
7664 spin_lock_irq(shost->host_lock);
7665 vport->load_flag |= FC_UNLOADING;
7666 spin_unlock_irq(shost->host_lock);
7667
7668 phba->pport->work_port_events = 0;
7669
James Smart3677a3a2010-09-29 11:19:14 -04007670 /* Stop the SLI4 device port */
7671 lpfc_stop_port(phba);
James Smartda0436e2009-05-22 14:51:39 -04007672
7673 lpfc_sli4_disable_intr(phba);
7674
James Smart3677a3a2010-09-29 11:19:14 -04007675 /* Reset SLI4 HBA FCoE function */
7676 lpfc_pci_function_reset(phba);
7677
James Smartda0436e2009-05-22 14:51:39 -04007678 return;
7679}
7680
7681/**
James Smart5af5eee2010-10-22 11:06:38 -04007682 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
7683 * @phba: Pointer to HBA context object.
7684 *
7685 * This function is called in the SLI4 code path to wait for completion
7686 * of device's XRIs exchange busy. It will check the XRI exchange busy
7687 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
7688 * that, it will check the XRI exchange busy on outstanding FCP and ELS
7689 * I/Os every 30 seconds, log error message, and wait forever. Only when
7690 * all XRI exchange busy complete, the driver unload shall proceed with
7691 * invoking the function reset ioctl mailbox command to the CNA and the
7692 * the rest of the driver unload resource release.
7693 **/
7694static void
7695lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
7696{
7697 int wait_time = 0;
7698 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
7699 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
7700
7701 while (!fcp_xri_cmpl || !els_xri_cmpl) {
7702 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
7703 if (!fcp_xri_cmpl)
7704 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7705 "2877 FCP XRI exchange busy "
7706 "wait time: %d seconds.\n",
7707 wait_time/1000);
7708 if (!els_xri_cmpl)
7709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7710 "2878 ELS XRI exchange busy "
7711 "wait time: %d seconds.\n",
7712 wait_time/1000);
7713 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
7714 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
7715 } else {
7716 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
7717 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
7718 }
7719 fcp_xri_cmpl =
7720 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
7721 els_xri_cmpl =
7722 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
7723 }
7724}
7725
7726/**
James Smartda0436e2009-05-22 14:51:39 -04007727 * lpfc_sli4_hba_unset - Unset the fcoe hba
7728 * @phba: Pointer to HBA context object.
7729 *
7730 * This function is called in the SLI4 code path to reset the HBA's FCoE
7731 * function. The caller is not required to hold any lock. This routine
7732 * issues PCI function reset mailbox command to reset the FCoE function.
7733 * At the end of the function, it calls lpfc_hba_down_post function to
7734 * free any pending commands.
7735 **/
7736static void
7737lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7738{
7739 int wait_cnt = 0;
7740 LPFC_MBOXQ_t *mboxq;
7741
7742 lpfc_stop_hba_timers(phba);
7743 phba->sli4_hba.intr_enable = 0;
7744
7745 /*
7746 * Gracefully wait out the potential current outstanding asynchronous
7747 * mailbox command.
7748 */
7749
7750 /* First, block any pending async mailbox command from posted */
7751 spin_lock_irq(&phba->hbalock);
7752 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7753 spin_unlock_irq(&phba->hbalock);
7754 /* Now, trying to wait it out if we can */
7755 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7756 msleep(10);
7757 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7758 break;
7759 }
7760 /* Forcefully release the outstanding mailbox command if timed out */
7761 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7762 spin_lock_irq(&phba->hbalock);
7763 mboxq = phba->sli.mbox_active;
7764 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7765 __lpfc_mbox_cmpl_put(phba, mboxq);
7766 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7767 phba->sli.mbox_active = NULL;
7768 spin_unlock_irq(&phba->hbalock);
7769 }
7770
James Smart5af5eee2010-10-22 11:06:38 -04007771 /* Abort all iocbs associated with the hba */
7772 lpfc_sli_hba_iocb_abort(phba);
7773
7774 /* Wait for completion of device XRI exchange busy */
7775 lpfc_sli4_xri_exchange_busy_wait(phba);
7776
James Smartda0436e2009-05-22 14:51:39 -04007777 /* Disable PCI subsystem interrupt */
7778 lpfc_sli4_disable_intr(phba);
7779
7780 /* Stop kthread signal shall trigger work_done one more time */
7781 kthread_stop(phba->worker_thread);
7782
James Smart3677a3a2010-09-29 11:19:14 -04007783 /* Reset SLI4 HBA FCoE function */
7784 lpfc_pci_function_reset(phba);
7785
James Smartda0436e2009-05-22 14:51:39 -04007786 /* Stop the SLI4 device port */
7787 phba->pport->work_port_events = 0;
7788}
7789
James Smart28baac72010-02-12 14:42:03 -05007790 /**
7791 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7792 * @phba: Pointer to HBA context object.
7793 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7794 *
7795 * This function is called in the SLI4 code path to read the port's
7796 * sli4 capabilities.
7797 *
7798 * This function may be be called from any context that can block-wait
7799 * for the completion. The expectation is that this routine is called
7800 * typically from probe_one or from the online routine.
7801 **/
7802int
7803lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7804{
7805 int rc;
7806 struct lpfc_mqe *mqe;
7807 struct lpfc_pc_sli4_params *sli4_params;
7808 uint32_t mbox_tmo;
7809
7810 rc = 0;
7811 mqe = &mboxq->u.mqe;
7812
7813 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05007814 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05007815 if (!phba->sli4_hba.intr_enable)
7816 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7817 else {
7818 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7819 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7820 }
7821
7822 if (unlikely(rc))
7823 return 1;
7824
7825 sli4_params = &phba->sli4_hba.pc_sli4_params;
7826 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7827 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7828 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7829 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7830 &mqe->un.sli4_params);
7831 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7832 &mqe->un.sli4_params);
7833 sli4_params->proto_types = mqe->un.sli4_params.word3;
7834 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7835 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7836 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7837 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7838 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7839 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7840 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7841 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7842 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7843 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7844 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7845 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7846 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7847 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7848 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7849 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7850 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7851 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7852 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7853 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7854 return rc;
7855}
7856
James Smartda0436e2009-05-22 14:51:39 -04007857/**
James Smartfedd3b72011-02-16 12:39:24 -05007858 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
7859 * @phba: Pointer to HBA context object.
7860 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7861 *
7862 * This function is called in the SLI4 code path to read the port's
7863 * sli4 capabilities.
7864 *
7865 * This function may be be called from any context that can block-wait
7866 * for the completion. The expectation is that this routine is called
7867 * typically from probe_one or from the online routine.
7868 **/
7869int
7870lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7871{
7872 int rc;
7873 struct lpfc_mqe *mqe = &mboxq->u.mqe;
7874 struct lpfc_pc_sli4_params *sli4_params;
7875 int length;
7876 struct lpfc_sli4_parameters *mbx_sli4_parameters;
7877
7878 /* Read the port's SLI4 Config Parameters */
7879 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
7880 sizeof(struct lpfc_sli4_cfg_mhdr));
7881 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7882 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
7883 length, LPFC_SLI4_MBX_EMBED);
7884 if (!phba->sli4_hba.intr_enable)
7885 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7886 else
7887 rc = lpfc_sli_issue_mbox_wait(phba, mboxq,
7888 lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG));
7889 if (unlikely(rc))
7890 return rc;
7891 sli4_params = &phba->sli4_hba.pc_sli4_params;
7892 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
7893 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
7894 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
7895 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
7896 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
7897 mbx_sli4_parameters);
7898 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
7899 mbx_sli4_parameters);
7900 if (bf_get(cfg_phwq, mbx_sli4_parameters))
7901 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
7902 else
7903 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
7904 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
7905 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
7906 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
7907 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
7908 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
7909 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
7910 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
7911 mbx_sli4_parameters);
7912 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
7913 mbx_sli4_parameters);
7914 return 0;
7915}
7916
7917/**
James Smart3772a992009-05-22 14:50:54 -04007918 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7919 * @pdev: pointer to PCI device
7920 * @pid: pointer to PCI device identifier
7921 *
7922 * This routine is to be called to attach a device with SLI-3 interface spec
7923 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7924 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7925 * information of the device and driver to see if the driver state that it can
7926 * support this kind of device. If the match is successful, the driver core
7927 * invokes this routine. If this routine determines it can claim the HBA, it
7928 * does all the initialization that it needs to do to handle the HBA properly.
7929 *
7930 * Return code
7931 * 0 - driver can claim the device
7932 * negative value - driver can not claim the device
7933 **/
7934static int __devinit
7935lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7936{
7937 struct lpfc_hba *phba;
7938 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007939 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007940 int error;
7941 uint32_t cfg_mode, intr_mode;
7942
7943 /* Allocate memory for HBA structure */
7944 phba = lpfc_hba_alloc(pdev);
7945 if (!phba)
7946 return -ENOMEM;
7947
7948 /* Perform generic PCI device enabling operation */
7949 error = lpfc_enable_pci_dev(phba);
7950 if (error) {
7951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7952 "1401 Failed to enable pci device.\n");
7953 goto out_free_phba;
7954 }
7955
7956 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7957 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7958 if (error)
7959 goto out_disable_pci_dev;
7960
7961 /* Set up SLI-3 specific device PCI memory space */
7962 error = lpfc_sli_pci_mem_setup(phba);
7963 if (error) {
7964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7965 "1402 Failed to set up pci memory space.\n");
7966 goto out_disable_pci_dev;
7967 }
7968
7969 /* Set up phase-1 common device driver resources */
7970 error = lpfc_setup_driver_resource_phase1(phba);
7971 if (error) {
7972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7973 "1403 Failed to set up driver resource.\n");
7974 goto out_unset_pci_mem_s3;
7975 }
7976
7977 /* Set up SLI-3 specific device driver resources */
7978 error = lpfc_sli_driver_resource_setup(phba);
7979 if (error) {
7980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7981 "1404 Failed to set up driver resource.\n");
7982 goto out_unset_pci_mem_s3;
7983 }
7984
7985 /* Initialize and populate the iocb list per host */
7986 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7987 if (error) {
7988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7989 "1405 Failed to initialize iocb list.\n");
7990 goto out_unset_driver_resource_s3;
7991 }
7992
7993 /* Set up common device driver resources */
7994 error = lpfc_setup_driver_resource_phase2(phba);
7995 if (error) {
7996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7997 "1406 Failed to set up driver resource.\n");
7998 goto out_free_iocb_list;
7999 }
8000
8001 /* Create SCSI host to the physical port */
8002 error = lpfc_create_shost(phba);
8003 if (error) {
8004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8005 "1407 Failed to create scsi host.\n");
8006 goto out_unset_driver_resource;
8007 }
8008
8009 /* Configure sysfs attributes */
8010 vport = phba->pport;
8011 error = lpfc_alloc_sysfs_attr(vport);
8012 if (error) {
8013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8014 "1476 Failed to allocate sysfs attr\n");
8015 goto out_destroy_shost;
8016 }
8017
James Smart6669f9b2009-10-02 15:16:45 -04008018 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008019 /* Now, trying to enable interrupt and bring up the device */
8020 cfg_mode = phba->cfg_use_msi;
8021 while (true) {
8022 /* Put device to a known state before enabling interrupt */
8023 lpfc_stop_port(phba);
8024 /* Configure and enable interrupt */
8025 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8026 if (intr_mode == LPFC_INTR_ERROR) {
8027 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8028 "0431 Failed to enable interrupt.\n");
8029 error = -ENODEV;
8030 goto out_free_sysfs_attr;
8031 }
8032 /* SLI-3 HBA setup */
8033 if (lpfc_sli_hba_setup(phba)) {
8034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8035 "1477 Failed to set up hba\n");
8036 error = -ENODEV;
8037 goto out_remove_device;
8038 }
8039
8040 /* Wait 50ms for the interrupts of previous mailbox commands */
8041 msleep(50);
8042 /* Check active interrupts on message signaled interrupts */
8043 if (intr_mode == 0 ||
8044 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8045 /* Log the current active interrupt mode */
8046 phba->intr_mode = intr_mode;
8047 lpfc_log_intr_mode(phba, intr_mode);
8048 break;
8049 } else {
8050 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8051 "0447 Configure interrupt mode (%d) "
8052 "failed active interrupt test.\n",
8053 intr_mode);
8054 /* Disable the current interrupt mode */
8055 lpfc_sli_disable_intr(phba);
8056 /* Try next level of interrupt mode */
8057 cfg_mode = --intr_mode;
8058 }
8059 }
8060
8061 /* Perform post initialization setup */
8062 lpfc_post_init_setup(phba);
8063
8064 /* Check if there are static vports to be created. */
8065 lpfc_create_static_vport(phba);
8066
8067 return 0;
8068
8069out_remove_device:
8070 lpfc_unset_hba(phba);
8071out_free_sysfs_attr:
8072 lpfc_free_sysfs_attr(vport);
8073out_destroy_shost:
8074 lpfc_destroy_shost(phba);
8075out_unset_driver_resource:
8076 lpfc_unset_driver_resource_phase2(phba);
8077out_free_iocb_list:
8078 lpfc_free_iocb_list(phba);
8079out_unset_driver_resource_s3:
8080 lpfc_sli_driver_resource_unset(phba);
8081out_unset_pci_mem_s3:
8082 lpfc_sli_pci_mem_unset(phba);
8083out_disable_pci_dev:
8084 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008085 if (shost)
8086 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008087out_free_phba:
8088 lpfc_hba_free(phba);
8089 return error;
8090}
8091
8092/**
8093 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008094 * @pdev: pointer to PCI device
8095 *
James Smart3772a992009-05-22 14:50:54 -04008096 * This routine is to be called to disattach a device with SLI-3 interface
8097 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8098 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8099 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008100 **/
dea31012005-04-17 16:05:31 -05008101static void __devexit
James Smart3772a992009-05-22 14:50:54 -04008102lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008103{
James Smart2e0fef82007-06-17 19:56:36 -05008104 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8105 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008106 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008107 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008108 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008109 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8110
James Smart549e55c2007-08-02 11:09:51 -04008111 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008112 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008113 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008114
James Smart858c9f62007-06-17 19:56:39 -05008115 lpfc_free_sysfs_attr(vport);
8116
James Smarteada2722008-12-04 22:39:13 -05008117 /* Release all the vports against this physical port */
8118 vports = lpfc_create_vport_work_array(phba);
8119 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04008120 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05008121 fc_vport_terminate(vports[i]->fc_vport);
8122 lpfc_destroy_vport_work_array(phba, vports);
8123
8124 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008125 fc_remove_host(shost);
8126 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008127 lpfc_cleanup(vport);
8128
James Smart2e0fef82007-06-17 19:56:36 -05008129 /*
8130 * Bring down the SLI Layer. This step disable all interrupts,
8131 * clears the rings, discards all mailbox commands, and resets
8132 * the HBA.
8133 */
James Smarta257bf92009-04-06 18:48:10 -04008134
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008135 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008136 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008137 /* Stop kthread signal shall trigger work_done one more time */
8138 kthread_stop(phba->worker_thread);
8139 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008140 lpfc_sli_brdrestart(phba);
8141
James Smart3772a992009-05-22 14:50:54 -04008142 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008143 spin_lock_irq(&phba->hbalock);
8144 list_del_init(&vport->listentry);
8145 spin_unlock_irq(&phba->hbalock);
8146
James Smart858c9f62007-06-17 19:56:39 -05008147 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008148
James Smart5b75da22008-12-04 22:39:35 -05008149 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008150 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05008151
8152 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05008153 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05008154
8155 /*
8156 * Call scsi_free before mem_free since scsi bufs are released to their
8157 * corresponding pools here.
8158 */
8159 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04008160 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008161
James Smart34b02dc2008-08-24 21:49:55 -04008162 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8163 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05008164
James Smart2e0fef82007-06-17 19:56:36 -05008165 /* Free resources associated with SLI2 interface */
8166 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04008167 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05008168
8169 /* unmap adapter SLIM and Control Registers */
8170 iounmap(phba->ctrl_regs_memmap_p);
8171 iounmap(phba->slim_memmap_p);
8172
James Smart3772a992009-05-22 14:50:54 -04008173 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008174
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008175 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05008176 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05008177}
8178
Linas Vepstas8d63f372007-02-14 14:28:36 -06008179/**
James Smart3772a992009-05-22 14:50:54 -04008180 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008181 * @pdev: pointer to PCI device
8182 * @msg: power management message
8183 *
James Smart3772a992009-05-22 14:50:54 -04008184 * This routine is to be called from the kernel's PCI subsystem to support
8185 * system Power Management (PM) to device with SLI-3 interface spec. When
8186 * PM invokes this method, it quiesces the device by stopping the driver's
8187 * worker thread for the device, turning off device's interrupt and DMA,
8188 * and bring the device offline. Note that as the driver implements the
8189 * minimum PM requirements to a power-aware driver's PM support for the
8190 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8191 * to the suspend() method call will be treated as SUSPEND and the driver will
8192 * fully reinitialize its device during resume() method call, the driver will
8193 * set device to PCI_D3hot state in PCI config space instead of setting it
8194 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05008195 *
8196 * Return code
James Smart3772a992009-05-22 14:50:54 -04008197 * 0 - driver suspended the device
8198 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008199 **/
8200static int
James Smart3772a992009-05-22 14:50:54 -04008201lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05008202{
8203 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8204 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8205
8206 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8207 "0473 PCI device Power Management suspend.\n");
8208
8209 /* Bring down the device */
8210 lpfc_offline_prep(phba);
8211 lpfc_offline(phba);
8212 kthread_stop(phba->worker_thread);
8213
8214 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04008215 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05008216
8217 /* Save device state to PCI config space */
8218 pci_save_state(pdev);
8219 pci_set_power_state(pdev, PCI_D3hot);
8220
8221 return 0;
8222}
8223
8224/**
James Smart3772a992009-05-22 14:50:54 -04008225 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008226 * @pdev: pointer to PCI device
8227 *
James Smart3772a992009-05-22 14:50:54 -04008228 * This routine is to be called from the kernel's PCI subsystem to support
8229 * system Power Management (PM) to device with SLI-3 interface spec. When PM
8230 * invokes this method, it restores the device's PCI config space state and
8231 * fully reinitializes the device and brings it online. Note that as the
8232 * driver implements the minimum PM requirements to a power-aware driver's
8233 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
8234 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
8235 * driver will fully reinitialize its device during resume() method call,
8236 * the device will be set to PCI_D0 directly in PCI config space before
8237 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05008238 *
8239 * Return code
James Smart3772a992009-05-22 14:50:54 -04008240 * 0 - driver suspended the device
8241 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008242 **/
8243static int
James Smart3772a992009-05-22 14:50:54 -04008244lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05008245{
8246 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8247 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05008248 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008249 int error;
8250
8251 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8252 "0452 PCI device Power Management resume.\n");
8253
8254 /* Restore device state from PCI config space */
8255 pci_set_power_state(pdev, PCI_D0);
8256 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04008257
James Smart1dfb5a42010-02-12 14:40:50 -05008258 /*
8259 * As the new kernel behavior of pci_restore_state() API call clears
8260 * device saved_state flag, need to save the restored state again.
8261 */
8262 pci_save_state(pdev);
8263
James Smart3a55b532008-12-04 22:38:54 -05008264 if (pdev->is_busmaster)
8265 pci_set_master(pdev);
8266
8267 /* Startup the kernel thread for this host adapter. */
8268 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8269 "lpfc_worker_%d", phba->brd_no);
8270 if (IS_ERR(phba->worker_thread)) {
8271 error = PTR_ERR(phba->worker_thread);
8272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8273 "0434 PM resume failed to start worker "
8274 "thread: error=x%x.\n", error);
8275 return error;
8276 }
8277
James Smart5b75da22008-12-04 22:39:35 -05008278 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008279 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008280 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05008281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05008282 "0430 PM resume Failed to enable interrupt\n");
8283 return -EIO;
8284 } else
8285 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008286
8287 /* Restart HBA and bring it online */
8288 lpfc_sli_brdrestart(phba);
8289 lpfc_online(phba);
8290
James Smart5b75da22008-12-04 22:39:35 -05008291 /* Log the current active interrupt mode */
8292 lpfc_log_intr_mode(phba, phba->intr_mode);
8293
James Smart3a55b532008-12-04 22:38:54 -05008294 return 0;
8295}
8296
8297/**
James Smart891478a2009-11-18 15:40:23 -05008298 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
8299 * @phba: pointer to lpfc hba data structure.
8300 *
8301 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04008302 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05008303 **/
8304static void
8305lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
8306{
James Smarte2af0d22010-03-15 11:25:32 -04008307 struct lpfc_sli *psli = &phba->sli;
8308 struct lpfc_sli_ring *pring;
8309
James Smart891478a2009-11-18 15:40:23 -05008310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8311 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04008312
8313 /*
8314 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8315 * and let the SCSI mid-layer to retry them to recover.
8316 */
8317 pring = &psli->ring[psli->fcp_ring];
8318 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05008319}
8320
8321/**
James Smart0d878412009-10-02 15:16:56 -04008322 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
8323 * @phba: pointer to lpfc hba data structure.
8324 *
8325 * This routine is called to prepare the SLI3 device for PCI slot reset. It
8326 * disables the device interrupt and pci device, and aborts the internal FCP
8327 * pending I/Os.
8328 **/
8329static void
8330lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
8331{
James Smart0d878412009-10-02 15:16:56 -04008332 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008333 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04008334
James Smart75baf692010-06-08 18:31:21 -04008335 /* Block any management I/Os to the device */
8336 lpfc_block_mgmt_io(phba);
8337
James Smarte2af0d22010-03-15 11:25:32 -04008338 /* Block all SCSI devices' I/Os on the host */
8339 lpfc_scsi_dev_block(phba);
8340
8341 /* stop all timers */
8342 lpfc_stop_hba_timers(phba);
8343
James Smart0d878412009-10-02 15:16:56 -04008344 /* Disable interrupt and pci device */
8345 lpfc_sli_disable_intr(phba);
8346 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04008347
James Smarte2af0d22010-03-15 11:25:32 -04008348 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8349 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04008350}
8351
8352/**
8353 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
8354 * @phba: pointer to lpfc hba data structure.
8355 *
8356 * This routine is called to prepare the SLI3 device for PCI slot permanently
8357 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8358 * pending I/Os.
8359 **/
8360static void
James Smart75baf692010-06-08 18:31:21 -04008361lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04008362{
8363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008364 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04008365 /* Block all SCSI devices' I/Os on the host */
8366 lpfc_scsi_dev_block(phba);
8367
8368 /* stop all timers */
8369 lpfc_stop_hba_timers(phba);
8370
James Smart0d878412009-10-02 15:16:56 -04008371 /* Clean up all driver's outstanding SCSI I/Os */
8372 lpfc_sli_flush_fcp_rings(phba);
8373}
8374
8375/**
James Smart3772a992009-05-22 14:50:54 -04008376 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04008377 * @pdev: pointer to PCI device.
8378 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008379 *
James Smart3772a992009-05-22 14:50:54 -04008380 * This routine is called from the PCI subsystem for I/O error handling to
8381 * device with SLI-3 interface spec. This function is called by the PCI
8382 * subsystem after a PCI bus error affecting this device has been detected.
8383 * When this function is invoked, it will need to stop all the I/Os and
8384 * interrupt(s) to the device. Once that is done, it will return
8385 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
8386 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04008387 *
8388 * Return codes
James Smart0d878412009-10-02 15:16:56 -04008389 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04008390 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8391 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04008392 **/
James Smart3772a992009-05-22 14:50:54 -04008393static pci_ers_result_t
8394lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008395{
James Smart51ef4c22007-08-02 11:10:31 -04008396 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8397 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008398
James Smart0d878412009-10-02 15:16:56 -04008399 switch (state) {
8400 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05008401 /* Non-fatal error, prepare for recovery */
8402 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04008403 return PCI_ERS_RESULT_CAN_RECOVER;
8404 case pci_channel_io_frozen:
8405 /* Fatal error, prepare for slot reset */
8406 lpfc_sli_prep_dev_for_reset(phba);
8407 return PCI_ERS_RESULT_NEED_RESET;
8408 case pci_channel_io_perm_failure:
8409 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04008410 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008411 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04008412 default:
8413 /* Unknown state, prepare and request slot reset */
8414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8415 "0472 Unknown PCI error state: x%x\n", state);
8416 lpfc_sli_prep_dev_for_reset(phba);
8417 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04008418 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008419}
8420
8421/**
James Smart3772a992009-05-22 14:50:54 -04008422 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04008423 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008424 *
James Smart3772a992009-05-22 14:50:54 -04008425 * This routine is called from the PCI subsystem for error handling to
8426 * device with SLI-3 interface spec. This is called after PCI bus has been
8427 * reset to restart the PCI card from scratch, as if from a cold-boot.
8428 * During the PCI subsystem error recovery, after driver returns
8429 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8430 * recovery and then call this routine before calling the .resume method
8431 * to recover the device. This function will initialize the HBA device,
8432 * enable the interrupt, but it will just put the HBA to offline state
8433 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04008434 *
8435 * Return codes
James Smart3772a992009-05-22 14:50:54 -04008436 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8437 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06008438 */
James Smart3772a992009-05-22 14:50:54 -04008439static pci_ers_result_t
8440lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008441{
James Smart51ef4c22007-08-02 11:10:31 -04008442 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8443 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008444 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05008445 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008446
8447 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11008448 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06008449 printk(KERN_ERR "lpfc: Cannot re-enable "
8450 "PCI device after reset.\n");
8451 return PCI_ERS_RESULT_DISCONNECT;
8452 }
8453
James Smart97207482008-12-04 22:39:19 -05008454 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008455
8456 /*
8457 * As the new kernel behavior of pci_restore_state() API call clears
8458 * device saved_state flag, need to save the restored state again.
8459 */
8460 pci_save_state(pdev);
8461
James Smart97207482008-12-04 22:39:19 -05008462 if (pdev->is_busmaster)
8463 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008464
James Smart92d7f7b2007-06-17 19:56:38 -05008465 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008466 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05008467 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008468
James Smart5b75da22008-12-04 22:39:35 -05008469 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008470 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008471 if (intr_mode == LPFC_INTR_ERROR) {
8472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8473 "0427 Cannot re-enable interrupt after "
8474 "slot reset.\n");
8475 return PCI_ERS_RESULT_DISCONNECT;
8476 } else
8477 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008478
James Smart75baf692010-06-08 18:31:21 -04008479 /* Take device offline, it will perform cleanup */
8480 lpfc_offline_prep(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008481 lpfc_offline(phba);
8482 lpfc_sli_brdrestart(phba);
8483
James Smart5b75da22008-12-04 22:39:35 -05008484 /* Log the current active interrupt mode */
8485 lpfc_log_intr_mode(phba, phba->intr_mode);
8486
Linas Vepstas8d63f372007-02-14 14:28:36 -06008487 return PCI_ERS_RESULT_RECOVERED;
8488}
8489
8490/**
James Smart3772a992009-05-22 14:50:54 -04008491 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04008492 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06008493 *
James Smart3772a992009-05-22 14:50:54 -04008494 * This routine is called from the PCI subsystem for error handling to device
8495 * with SLI-3 interface spec. It is called when kernel error recovery tells
8496 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8497 * error recovery. After this call, traffic can start to flow from this device
8498 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008499 */
James Smart3772a992009-05-22 14:50:54 -04008500static void
8501lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008502{
James Smart51ef4c22007-08-02 11:10:31 -04008503 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8504 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008505
James Smarte2af0d22010-03-15 11:25:32 -04008506 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04008507 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04008508
8509 /* Clean up Advanced Error Reporting (AER) if needed */
8510 if (phba->hba_flag & HBA_AER_ENABLED)
8511 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008512}
8513
James Smart3772a992009-05-22 14:50:54 -04008514/**
James Smartda0436e2009-05-22 14:51:39 -04008515 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8516 * @phba: pointer to lpfc hba data structure.
8517 *
8518 * returns the number of ELS/CT IOCBs to reserve
8519 **/
8520int
8521lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8522{
8523 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8524
James Smartf1126682009-06-10 17:22:44 -04008525 if (phba->sli_rev == LPFC_SLI_REV4) {
8526 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04008527 return 10;
James Smartf1126682009-06-10 17:22:44 -04008528 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04008529 return 25;
James Smartf1126682009-06-10 17:22:44 -04008530 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04008531 return 50;
James Smartf1126682009-06-10 17:22:44 -04008532 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04008533 return 100;
James Smartf1126682009-06-10 17:22:44 -04008534 else
James Smart6a9c52c2009-10-02 15:16:51 -04008535 return 150;
James Smartf1126682009-06-10 17:22:44 -04008536 } else
8537 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008538}
8539
8540/**
8541 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
8542 * @pdev: pointer to PCI device
8543 * @pid: pointer to PCI device identifier
8544 *
8545 * This routine is called from the kernel's PCI subsystem to device with
8546 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8547 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8548 * information of the device and driver to see if the driver state that it
8549 * can support this kind of device. If the match is successful, the driver
8550 * core invokes this routine. If this routine determines it can claim the HBA,
8551 * it does all the initialization that it needs to do to handle the HBA
8552 * properly.
8553 *
8554 * Return code
8555 * 0 - driver can claim the device
8556 * negative value - driver can not claim the device
8557 **/
8558static int __devinit
8559lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8560{
8561 struct lpfc_hba *phba;
8562 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008563 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008564 int error;
8565 uint32_t cfg_mode, intr_mode;
8566 int mcnt;
8567
8568 /* Allocate memory for HBA structure */
8569 phba = lpfc_hba_alloc(pdev);
8570 if (!phba)
8571 return -ENOMEM;
8572
8573 /* Perform generic PCI device enabling operation */
8574 error = lpfc_enable_pci_dev(phba);
8575 if (error) {
8576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8577 "1409 Failed to enable pci device.\n");
8578 goto out_free_phba;
8579 }
8580
8581 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
8582 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
8583 if (error)
8584 goto out_disable_pci_dev;
8585
8586 /* Set up SLI-4 specific device PCI memory space */
8587 error = lpfc_sli4_pci_mem_setup(phba);
8588 if (error) {
8589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8590 "1410 Failed to set up pci memory space.\n");
8591 goto out_disable_pci_dev;
8592 }
8593
8594 /* Set up phase-1 common device driver resources */
8595 error = lpfc_setup_driver_resource_phase1(phba);
8596 if (error) {
8597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8598 "1411 Failed to set up driver resource.\n");
8599 goto out_unset_pci_mem_s4;
8600 }
8601
8602 /* Set up SLI-4 Specific device driver resources */
8603 error = lpfc_sli4_driver_resource_setup(phba);
8604 if (error) {
8605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8606 "1412 Failed to set up driver resource.\n");
8607 goto out_unset_pci_mem_s4;
8608 }
8609
8610 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04008611
8612 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8613 "2821 initialize iocb list %d.\n",
8614 phba->cfg_iocb_cnt*1024);
8615 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
8616
James Smartda0436e2009-05-22 14:51:39 -04008617 if (error) {
8618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8619 "1413 Failed to initialize iocb list.\n");
8620 goto out_unset_driver_resource_s4;
8621 }
8622
James Smart19ca7602010-11-20 23:11:55 -05008623 INIT_LIST_HEAD(&phba->active_rrq_list);
8624
James Smartda0436e2009-05-22 14:51:39 -04008625 /* Set up common device driver resources */
8626 error = lpfc_setup_driver_resource_phase2(phba);
8627 if (error) {
8628 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8629 "1414 Failed to set up driver resource.\n");
8630 goto out_free_iocb_list;
8631 }
8632
8633 /* Create SCSI host to the physical port */
8634 error = lpfc_create_shost(phba);
8635 if (error) {
8636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8637 "1415 Failed to create scsi host.\n");
8638 goto out_unset_driver_resource;
8639 }
8640
8641 /* Configure sysfs attributes */
8642 vport = phba->pport;
8643 error = lpfc_alloc_sysfs_attr(vport);
8644 if (error) {
8645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8646 "1416 Failed to allocate sysfs attr\n");
8647 goto out_destroy_shost;
8648 }
8649
James Smart6669f9b2009-10-02 15:16:45 -04008650 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04008651 /* Now, trying to enable interrupt and bring up the device */
8652 cfg_mode = phba->cfg_use_msi;
8653 while (true) {
8654 /* Put device to a known state before enabling interrupt */
8655 lpfc_stop_port(phba);
8656 /* Configure and enable interrupt */
8657 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
8658 if (intr_mode == LPFC_INTR_ERROR) {
8659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8660 "0426 Failed to enable interrupt.\n");
8661 error = -ENODEV;
8662 goto out_free_sysfs_attr;
8663 }
James Smartdef9c7a2009-12-21 17:02:28 -05008664 /* Default to single FCP EQ for non-MSI-X */
8665 if (phba->intr_type != MSIX)
8666 phba->cfg_fcp_eq_count = 1;
James Smart75baf692010-06-08 18:31:21 -04008667 else if (phba->sli4_hba.msix_vec_nr < phba->cfg_fcp_eq_count)
8668 phba->cfg_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
James Smartda0436e2009-05-22 14:51:39 -04008669 /* Set up SLI-4 HBA */
8670 if (lpfc_sli4_hba_setup(phba)) {
8671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8672 "1421 Failed to set up hba\n");
8673 error = -ENODEV;
8674 goto out_disable_intr;
8675 }
8676
8677 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
8678 if (intr_mode != 0)
8679 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
8680 LPFC_ACT_INTR_CNT);
8681
8682 /* Check active interrupts received only for MSI/MSI-X */
8683 if (intr_mode == 0 ||
8684 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
8685 /* Log the current active interrupt mode */
8686 phba->intr_mode = intr_mode;
8687 lpfc_log_intr_mode(phba, intr_mode);
8688 break;
8689 }
8690 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8691 "0451 Configure interrupt mode (%d) "
8692 "failed active interrupt test.\n",
8693 intr_mode);
James Smart2fcee4b2010-12-15 17:57:46 -05008694 /* Unset the previous SLI-4 HBA setup. */
8695 /*
8696 * TODO: Is this operation compatible with IF TYPE 2
8697 * devices? All port state is deleted and cleared.
8698 */
James Smartda0436e2009-05-22 14:51:39 -04008699 lpfc_sli4_unset_hba(phba);
8700 /* Try next level of interrupt mode */
8701 cfg_mode = --intr_mode;
8702 }
8703
8704 /* Perform post initialization setup */
8705 lpfc_post_init_setup(phba);
8706
James Smart1c6834a2009-07-19 10:01:26 -04008707 /* Check if there are static vports to be created. */
8708 lpfc_create_static_vport(phba);
8709
James Smartda0436e2009-05-22 14:51:39 -04008710 return 0;
8711
8712out_disable_intr:
8713 lpfc_sli4_disable_intr(phba);
8714out_free_sysfs_attr:
8715 lpfc_free_sysfs_attr(vport);
8716out_destroy_shost:
8717 lpfc_destroy_shost(phba);
8718out_unset_driver_resource:
8719 lpfc_unset_driver_resource_phase2(phba);
8720out_free_iocb_list:
8721 lpfc_free_iocb_list(phba);
8722out_unset_driver_resource_s4:
8723 lpfc_sli4_driver_resource_unset(phba);
8724out_unset_pci_mem_s4:
8725 lpfc_sli4_pci_mem_unset(phba);
8726out_disable_pci_dev:
8727 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008728 if (shost)
8729 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04008730out_free_phba:
8731 lpfc_hba_free(phba);
8732 return error;
8733}
8734
8735/**
8736 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8737 * @pdev: pointer to PCI device
8738 *
8739 * This routine is called from the kernel's PCI subsystem to device with
8740 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8741 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8742 * device to be removed from the PCI subsystem properly.
8743 **/
8744static void __devexit
8745lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8746{
8747 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8748 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8749 struct lpfc_vport **vports;
8750 struct lpfc_hba *phba = vport->phba;
8751 int i;
8752
8753 /* Mark the device unloading flag */
8754 spin_lock_irq(&phba->hbalock);
8755 vport->load_flag |= FC_UNLOADING;
8756 spin_unlock_irq(&phba->hbalock);
8757
8758 /* Free the HBA sysfs attributes */
8759 lpfc_free_sysfs_attr(vport);
8760
8761 /* Release all the vports against this physical port */
8762 vports = lpfc_create_vport_work_array(phba);
8763 if (vports != NULL)
8764 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8765 fc_vport_terminate(vports[i]->fc_vport);
8766 lpfc_destroy_vport_work_array(phba, vports);
8767
8768 /* Remove FC host and then SCSI host with the physical port */
8769 fc_remove_host(shost);
8770 scsi_remove_host(shost);
8771
8772 /* Perform cleanup on the physical port */
8773 lpfc_cleanup(vport);
8774
8775 /*
8776 * Bring down the SLI Layer. This step disables all interrupts,
8777 * clears the rings, discards all mailbox commands, and resets
8778 * the HBA FCoE function.
8779 */
8780 lpfc_debugfs_terminate(vport);
8781 lpfc_sli4_hba_unset(phba);
8782
8783 spin_lock_irq(&phba->hbalock);
8784 list_del_init(&vport->listentry);
8785 spin_unlock_irq(&phba->hbalock);
8786
James Smart3677a3a2010-09-29 11:19:14 -04008787 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04008788 * buffers are released to their corresponding pools here.
8789 */
8790 lpfc_scsi_free(phba);
8791 lpfc_sli4_driver_resource_unset(phba);
8792
8793 /* Unmap adapter Control and Doorbell registers */
8794 lpfc_sli4_pci_mem_unset(phba);
8795
8796 /* Release PCI resources and disable device's PCI function */
8797 scsi_host_put(shost);
8798 lpfc_disable_pci_dev(phba);
8799
8800 /* Finally, free the driver's device data structure */
8801 lpfc_hba_free(phba);
8802
8803 return;
8804}
8805
8806/**
8807 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
8808 * @pdev: pointer to PCI device
8809 * @msg: power management message
8810 *
8811 * This routine is called from the kernel's PCI subsystem to support system
8812 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8813 * this method, it quiesces the device by stopping the driver's worker
8814 * thread for the device, turning off device's interrupt and DMA, and bring
8815 * the device offline. Note that as the driver implements the minimum PM
8816 * requirements to a power-aware driver's PM support for suspend/resume -- all
8817 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8818 * method call will be treated as SUSPEND and the driver will fully
8819 * reinitialize its device during resume() method call, the driver will set
8820 * device to PCI_D3hot state in PCI config space instead of setting it
8821 * according to the @msg provided by the PM.
8822 *
8823 * Return code
8824 * 0 - driver suspended the device
8825 * Error otherwise
8826 **/
8827static int
8828lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
8829{
8830 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8831 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8832
8833 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04008834 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04008835
8836 /* Bring down the device */
8837 lpfc_offline_prep(phba);
8838 lpfc_offline(phba);
8839 kthread_stop(phba->worker_thread);
8840
8841 /* Disable interrupt from device */
8842 lpfc_sli4_disable_intr(phba);
8843
8844 /* Save device state to PCI config space */
8845 pci_save_state(pdev);
8846 pci_set_power_state(pdev, PCI_D3hot);
8847
8848 return 0;
8849}
8850
8851/**
8852 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8853 * @pdev: pointer to PCI device
8854 *
8855 * This routine is called from the kernel's PCI subsystem to support system
8856 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8857 * this method, it restores the device's PCI config space state and fully
8858 * reinitializes the device and brings it online. Note that as the driver
8859 * implements the minimum PM requirements to a power-aware driver's PM for
8860 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8861 * to the suspend() method call will be treated as SUSPEND and the driver
8862 * will fully reinitialize its device during resume() method call, the device
8863 * will be set to PCI_D0 directly in PCI config space before restoring the
8864 * state.
8865 *
8866 * Return code
8867 * 0 - driver suspended the device
8868 * Error otherwise
8869 **/
8870static int
8871lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8872{
8873 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8874 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8875 uint32_t intr_mode;
8876 int error;
8877
8878 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8879 "0292 PCI device Power Management resume.\n");
8880
8881 /* Restore device state from PCI config space */
8882 pci_set_power_state(pdev, PCI_D0);
8883 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008884
8885 /*
8886 * As the new kernel behavior of pci_restore_state() API call clears
8887 * device saved_state flag, need to save the restored state again.
8888 */
8889 pci_save_state(pdev);
8890
James Smartda0436e2009-05-22 14:51:39 -04008891 if (pdev->is_busmaster)
8892 pci_set_master(pdev);
8893
8894 /* Startup the kernel thread for this host adapter. */
8895 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8896 "lpfc_worker_%d", phba->brd_no);
8897 if (IS_ERR(phba->worker_thread)) {
8898 error = PTR_ERR(phba->worker_thread);
8899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8900 "0293 PM resume failed to start worker "
8901 "thread: error=x%x.\n", error);
8902 return error;
8903 }
8904
8905 /* Configure and enable interrupt */
8906 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8907 if (intr_mode == LPFC_INTR_ERROR) {
8908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8909 "0294 PM resume Failed to enable interrupt\n");
8910 return -EIO;
8911 } else
8912 phba->intr_mode = intr_mode;
8913
8914 /* Restart HBA and bring it online */
8915 lpfc_sli_brdrestart(phba);
8916 lpfc_online(phba);
8917
8918 /* Log the current active interrupt mode */
8919 lpfc_log_intr_mode(phba, phba->intr_mode);
8920
8921 return 0;
8922}
8923
8924/**
James Smart75baf692010-06-08 18:31:21 -04008925 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
8926 * @phba: pointer to lpfc hba data structure.
8927 *
8928 * This routine is called to prepare the SLI4 device for PCI slot recover. It
8929 * aborts all the outstanding SCSI I/Os to the pci device.
8930 **/
8931static void
8932lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
8933{
8934 struct lpfc_sli *psli = &phba->sli;
8935 struct lpfc_sli_ring *pring;
8936
8937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8938 "2828 PCI channel I/O abort preparing for recovery\n");
8939 /*
8940 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8941 * and let the SCSI mid-layer to retry them to recover.
8942 */
8943 pring = &psli->ring[psli->fcp_ring];
8944 lpfc_sli_abort_iocb_ring(phba, pring);
8945}
8946
8947/**
8948 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
8949 * @phba: pointer to lpfc hba data structure.
8950 *
8951 * This routine is called to prepare the SLI4 device for PCI slot reset. It
8952 * disables the device interrupt and pci device, and aborts the internal FCP
8953 * pending I/Os.
8954 **/
8955static void
8956lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
8957{
8958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8959 "2826 PCI channel disable preparing for reset\n");
8960
8961 /* Block any management I/Os to the device */
8962 lpfc_block_mgmt_io(phba);
8963
8964 /* Block all SCSI devices' I/Os on the host */
8965 lpfc_scsi_dev_block(phba);
8966
8967 /* stop all timers */
8968 lpfc_stop_hba_timers(phba);
8969
8970 /* Disable interrupt and pci device */
8971 lpfc_sli4_disable_intr(phba);
8972 pci_disable_device(phba->pcidev);
8973
8974 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8975 lpfc_sli_flush_fcp_rings(phba);
8976}
8977
8978/**
8979 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
8980 * @phba: pointer to lpfc hba data structure.
8981 *
8982 * This routine is called to prepare the SLI4 device for PCI slot permanently
8983 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8984 * pending I/Os.
8985 **/
8986static void
8987lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
8988{
8989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8990 "2827 PCI channel permanent disable for failure\n");
8991
8992 /* Block all SCSI devices' I/Os on the host */
8993 lpfc_scsi_dev_block(phba);
8994
8995 /* stop all timers */
8996 lpfc_stop_hba_timers(phba);
8997
8998 /* Clean up all driver's outstanding SCSI I/Os */
8999 lpfc_sli_flush_fcp_rings(phba);
9000}
9001
9002/**
James Smartda0436e2009-05-22 14:51:39 -04009003 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9004 * @pdev: pointer to PCI device.
9005 * @state: the current PCI connection state.
9006 *
9007 * This routine is called from the PCI subsystem for error handling to device
9008 * with SLI-4 interface spec. This function is called by the PCI subsystem
9009 * after a PCI bus error affecting this device has been detected. When this
9010 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9011 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9012 * for the PCI subsystem to perform proper recovery as desired.
9013 *
9014 * Return codes
9015 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9016 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9017 **/
9018static pci_ers_result_t
9019lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9020{
James Smart75baf692010-06-08 18:31:21 -04009021 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9022 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9023
9024 switch (state) {
9025 case pci_channel_io_normal:
9026 /* Non-fatal error, prepare for recovery */
9027 lpfc_sli4_prep_dev_for_recover(phba);
9028 return PCI_ERS_RESULT_CAN_RECOVER;
9029 case pci_channel_io_frozen:
9030 /* Fatal error, prepare for slot reset */
9031 lpfc_sli4_prep_dev_for_reset(phba);
9032 return PCI_ERS_RESULT_NEED_RESET;
9033 case pci_channel_io_perm_failure:
9034 /* Permanent failure, prepare for device down */
9035 lpfc_sli4_prep_dev_for_perm_failure(phba);
9036 return PCI_ERS_RESULT_DISCONNECT;
9037 default:
9038 /* Unknown state, prepare and request slot reset */
9039 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9040 "2825 Unknown PCI error state: x%x\n", state);
9041 lpfc_sli4_prep_dev_for_reset(phba);
9042 return PCI_ERS_RESULT_NEED_RESET;
9043 }
James Smartda0436e2009-05-22 14:51:39 -04009044}
9045
9046/**
9047 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9048 * @pdev: pointer to PCI device.
9049 *
9050 * This routine is called from the PCI subsystem for error handling to device
9051 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9052 * restart the PCI card from scratch, as if from a cold-boot. During the
9053 * PCI subsystem error recovery, after the driver returns
9054 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9055 * recovery and then call this routine before calling the .resume method to
9056 * recover the device. This function will initialize the HBA device, enable
9057 * the interrupt, but it will just put the HBA to offline state without
9058 * passing any I/O traffic.
9059 *
9060 * Return codes
9061 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9062 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9063 */
9064static pci_ers_result_t
9065lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9066{
James Smart75baf692010-06-08 18:31:21 -04009067 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9068 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9069 struct lpfc_sli *psli = &phba->sli;
9070 uint32_t intr_mode;
9071
9072 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9073 if (pci_enable_device_mem(pdev)) {
9074 printk(KERN_ERR "lpfc: Cannot re-enable "
9075 "PCI device after reset.\n");
9076 return PCI_ERS_RESULT_DISCONNECT;
9077 }
9078
9079 pci_restore_state(pdev);
9080 if (pdev->is_busmaster)
9081 pci_set_master(pdev);
9082
9083 spin_lock_irq(&phba->hbalock);
9084 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9085 spin_unlock_irq(&phba->hbalock);
9086
9087 /* Configure and enable interrupt */
9088 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9089 if (intr_mode == LPFC_INTR_ERROR) {
9090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9091 "2824 Cannot re-enable interrupt after "
9092 "slot reset.\n");
9093 return PCI_ERS_RESULT_DISCONNECT;
9094 } else
9095 phba->intr_mode = intr_mode;
9096
9097 /* Log the current active interrupt mode */
9098 lpfc_log_intr_mode(phba, phba->intr_mode);
9099
James Smartda0436e2009-05-22 14:51:39 -04009100 return PCI_ERS_RESULT_RECOVERED;
9101}
9102
9103/**
9104 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
9105 * @pdev: pointer to PCI device
9106 *
9107 * This routine is called from the PCI subsystem for error handling to device
9108 * with SLI-4 interface spec. It is called when kernel error recovery tells
9109 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9110 * error recovery. After this call, traffic can start to flow from this device
9111 * again.
9112 **/
9113static void
9114lpfc_io_resume_s4(struct pci_dev *pdev)
9115{
James Smart75baf692010-06-08 18:31:21 -04009116 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9117 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9118
9119 /*
9120 * In case of slot reset, as function reset is performed through
9121 * mailbox command which needs DMA to be enabled, this operation
9122 * has to be moved to the io resume phase. Taking device offline
9123 * will perform the necessary cleanup.
9124 */
9125 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
9126 /* Perform device reset */
9127 lpfc_offline_prep(phba);
9128 lpfc_offline(phba);
9129 lpfc_sli_brdrestart(phba);
9130 /* Bring the device back online */
9131 lpfc_online(phba);
9132 }
9133
9134 /* Clean up Advanced Error Reporting (AER) if needed */
9135 if (phba->hba_flag & HBA_AER_ENABLED)
9136 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -04009137}
9138
9139/**
James Smart3772a992009-05-22 14:50:54 -04009140 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
9141 * @pdev: pointer to PCI device
9142 * @pid: pointer to PCI device identifier
9143 *
9144 * This routine is to be registered to the kernel's PCI subsystem. When an
9145 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
9146 * at PCI device-specific information of the device and driver to see if the
9147 * driver state that it can support this kind of device. If the match is
9148 * successful, the driver core invokes this routine. This routine dispatches
9149 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
9150 * do all the initialization that it needs to do to handle the HBA device
9151 * properly.
9152 *
9153 * Return code
9154 * 0 - driver can claim the device
9155 * negative value - driver can not claim the device
9156 **/
9157static int __devinit
9158lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
9159{
9160 int rc;
James Smart8fa38512009-07-19 10:01:03 -04009161 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04009162
James Smart28baac72010-02-12 14:42:03 -05009163 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04009164 return -ENODEV;
9165
James Smart8fa38512009-07-19 10:01:03 -04009166 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05009167 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04009168 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009169 else
James Smart3772a992009-05-22 14:50:54 -04009170 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009171
James Smart3772a992009-05-22 14:50:54 -04009172 return rc;
9173}
9174
9175/**
9176 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
9177 * @pdev: pointer to PCI device
9178 *
9179 * This routine is to be registered to the kernel's PCI subsystem. When an
9180 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
9181 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
9182 * remove routine, which will perform all the necessary cleanup for the
9183 * device to be removed from the PCI subsystem properly.
9184 **/
9185static void __devexit
9186lpfc_pci_remove_one(struct pci_dev *pdev)
9187{
9188 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9189 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9190
9191 switch (phba->pci_dev_grp) {
9192 case LPFC_PCI_DEV_LP:
9193 lpfc_pci_remove_one_s3(pdev);
9194 break;
James Smartda0436e2009-05-22 14:51:39 -04009195 case LPFC_PCI_DEV_OC:
9196 lpfc_pci_remove_one_s4(pdev);
9197 break;
James Smart3772a992009-05-22 14:50:54 -04009198 default:
9199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9200 "1424 Invalid PCI device group: 0x%x\n",
9201 phba->pci_dev_grp);
9202 break;
9203 }
9204 return;
9205}
9206
9207/**
9208 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
9209 * @pdev: pointer to PCI device
9210 * @msg: power management message
9211 *
9212 * This routine is to be registered to the kernel's PCI subsystem to support
9213 * system Power Management (PM). When PM invokes this method, it dispatches
9214 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
9215 * suspend the device.
9216 *
9217 * Return code
9218 * 0 - driver suspended the device
9219 * Error otherwise
9220 **/
9221static int
9222lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
9223{
9224 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9225 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9226 int rc = -ENODEV;
9227
9228 switch (phba->pci_dev_grp) {
9229 case LPFC_PCI_DEV_LP:
9230 rc = lpfc_pci_suspend_one_s3(pdev, msg);
9231 break;
James Smartda0436e2009-05-22 14:51:39 -04009232 case LPFC_PCI_DEV_OC:
9233 rc = lpfc_pci_suspend_one_s4(pdev, msg);
9234 break;
James Smart3772a992009-05-22 14:50:54 -04009235 default:
9236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9237 "1425 Invalid PCI device group: 0x%x\n",
9238 phba->pci_dev_grp);
9239 break;
9240 }
9241 return rc;
9242}
9243
9244/**
9245 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
9246 * @pdev: pointer to PCI device
9247 *
9248 * This routine is to be registered to the kernel's PCI subsystem to support
9249 * system Power Management (PM). When PM invokes this method, it dispatches
9250 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
9251 * resume the device.
9252 *
9253 * Return code
9254 * 0 - driver suspended the device
9255 * Error otherwise
9256 **/
9257static int
9258lpfc_pci_resume_one(struct pci_dev *pdev)
9259{
9260 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9261 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9262 int rc = -ENODEV;
9263
9264 switch (phba->pci_dev_grp) {
9265 case LPFC_PCI_DEV_LP:
9266 rc = lpfc_pci_resume_one_s3(pdev);
9267 break;
James Smartda0436e2009-05-22 14:51:39 -04009268 case LPFC_PCI_DEV_OC:
9269 rc = lpfc_pci_resume_one_s4(pdev);
9270 break;
James Smart3772a992009-05-22 14:50:54 -04009271 default:
9272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9273 "1426 Invalid PCI device group: 0x%x\n",
9274 phba->pci_dev_grp);
9275 break;
9276 }
9277 return rc;
9278}
9279
9280/**
9281 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
9282 * @pdev: pointer to PCI device.
9283 * @state: the current PCI connection state.
9284 *
9285 * This routine is registered to the PCI subsystem for error handling. This
9286 * function is called by the PCI subsystem after a PCI bus error affecting
9287 * this device has been detected. When this routine is invoked, it dispatches
9288 * the action to the proper SLI-3 or SLI-4 device error detected handling
9289 * routine, which will perform the proper error detected operation.
9290 *
9291 * Return codes
9292 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9293 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9294 **/
9295static pci_ers_result_t
9296lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9297{
9298 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9299 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9300 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9301
9302 switch (phba->pci_dev_grp) {
9303 case LPFC_PCI_DEV_LP:
9304 rc = lpfc_io_error_detected_s3(pdev, state);
9305 break;
James Smartda0436e2009-05-22 14:51:39 -04009306 case LPFC_PCI_DEV_OC:
9307 rc = lpfc_io_error_detected_s4(pdev, state);
9308 break;
James Smart3772a992009-05-22 14:50:54 -04009309 default:
9310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9311 "1427 Invalid PCI device group: 0x%x\n",
9312 phba->pci_dev_grp);
9313 break;
9314 }
9315 return rc;
9316}
9317
9318/**
9319 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
9320 * @pdev: pointer to PCI device.
9321 *
9322 * This routine is registered to the PCI subsystem for error handling. This
9323 * function is called after PCI bus has been reset to restart the PCI card
9324 * from scratch, as if from a cold-boot. When this routine is invoked, it
9325 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
9326 * routine, which will perform the proper device reset.
9327 *
9328 * Return codes
9329 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9330 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9331 **/
9332static pci_ers_result_t
9333lpfc_io_slot_reset(struct pci_dev *pdev)
9334{
9335 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9336 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9337 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9338
9339 switch (phba->pci_dev_grp) {
9340 case LPFC_PCI_DEV_LP:
9341 rc = lpfc_io_slot_reset_s3(pdev);
9342 break;
James Smartda0436e2009-05-22 14:51:39 -04009343 case LPFC_PCI_DEV_OC:
9344 rc = lpfc_io_slot_reset_s4(pdev);
9345 break;
James Smart3772a992009-05-22 14:50:54 -04009346 default:
9347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9348 "1428 Invalid PCI device group: 0x%x\n",
9349 phba->pci_dev_grp);
9350 break;
9351 }
9352 return rc;
9353}
9354
9355/**
9356 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
9357 * @pdev: pointer to PCI device
9358 *
9359 * This routine is registered to the PCI subsystem for error handling. It
9360 * is called when kernel error recovery tells the lpfc driver that it is
9361 * OK to resume normal PCI operation after PCI bus error recovery. When
9362 * this routine is invoked, it dispatches the action to the proper SLI-3
9363 * or SLI-4 device io_resume routine, which will resume the device operation.
9364 **/
9365static void
9366lpfc_io_resume(struct pci_dev *pdev)
9367{
9368 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9369 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9370
9371 switch (phba->pci_dev_grp) {
9372 case LPFC_PCI_DEV_LP:
9373 lpfc_io_resume_s3(pdev);
9374 break;
James Smartda0436e2009-05-22 14:51:39 -04009375 case LPFC_PCI_DEV_OC:
9376 lpfc_io_resume_s4(pdev);
9377 break;
James Smart3772a992009-05-22 14:50:54 -04009378 default:
9379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9380 "1429 Invalid PCI device group: 0x%x\n",
9381 phba->pci_dev_grp);
9382 break;
9383 }
9384 return;
9385}
9386
dea31012005-04-17 16:05:31 -05009387static struct pci_device_id lpfc_id_table[] = {
9388 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
9389 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04009390 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
9391 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009392 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
9393 PCI_ANY_ID, PCI_ANY_ID, },
9394 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
9395 PCI_ANY_ID, PCI_ANY_ID, },
9396 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
9397 PCI_ANY_ID, PCI_ANY_ID, },
9398 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
9399 PCI_ANY_ID, PCI_ANY_ID, },
9400 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
9401 PCI_ANY_ID, PCI_ANY_ID, },
9402 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
9403 PCI_ANY_ID, PCI_ANY_ID, },
9404 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
9405 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009406 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
9407 PCI_ANY_ID, PCI_ANY_ID, },
9408 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
9409 PCI_ANY_ID, PCI_ANY_ID, },
9410 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
9411 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009412 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
9413 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009414 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
9415 PCI_ANY_ID, PCI_ANY_ID, },
9416 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
9417 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009418 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
9419 PCI_ANY_ID, PCI_ANY_ID, },
9420 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
9421 PCI_ANY_ID, PCI_ANY_ID, },
9422 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
9423 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04009424 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
9425 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009426 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
9427 PCI_ANY_ID, PCI_ANY_ID, },
9428 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
9429 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009430 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
9431 PCI_ANY_ID, PCI_ANY_ID, },
9432 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
9433 PCI_ANY_ID, PCI_ANY_ID, },
9434 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
9435 PCI_ANY_ID, PCI_ANY_ID, },
9436 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
9437 PCI_ANY_ID, PCI_ANY_ID, },
9438 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
9439 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009440 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
9441 PCI_ANY_ID, PCI_ANY_ID, },
9442 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
9443 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04009444 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
9445 PCI_ANY_ID, PCI_ANY_ID, },
9446 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
9447 PCI_ANY_ID, PCI_ANY_ID, },
9448 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
9449 PCI_ANY_ID, PCI_ANY_ID, },
9450 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
9451 PCI_ANY_ID, PCI_ANY_ID, },
9452 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
9453 PCI_ANY_ID, PCI_ANY_ID, },
9454 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
9455 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04009456 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
9457 PCI_ANY_ID, PCI_ANY_ID, },
9458 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
9459 PCI_ANY_ID, PCI_ANY_ID, },
9460 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
9461 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04009462 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
9463 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05009464 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
9465 PCI_ANY_ID, PCI_ANY_ID, },
9466 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04009467 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -04009468 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
9469 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -05009470 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
9471 PCI_ANY_ID, PCI_ANY_ID, },
9472 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
9473 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009474 { 0 }
9475};
9476
9477MODULE_DEVICE_TABLE(pci, lpfc_id_table);
9478
Linas Vepstas8d63f372007-02-14 14:28:36 -06009479static struct pci_error_handlers lpfc_err_handler = {
9480 .error_detected = lpfc_io_error_detected,
9481 .slot_reset = lpfc_io_slot_reset,
9482 .resume = lpfc_io_resume,
9483};
9484
dea31012005-04-17 16:05:31 -05009485static struct pci_driver lpfc_driver = {
9486 .name = LPFC_DRIVER_NAME,
9487 .id_table = lpfc_id_table,
9488 .probe = lpfc_pci_probe_one,
9489 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05009490 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04009491 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05009492 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05009493};
9494
James Smarte59058c2008-08-24 21:49:00 -04009495/**
James Smart3621a712009-04-06 18:47:14 -04009496 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04009497 *
9498 * This routine is to be invoked when the lpfc module is loaded into the
9499 * kernel. The special kernel macro module_init() is used to indicate the
9500 * role of this routine to the kernel as lpfc module entry point.
9501 *
9502 * Return codes
9503 * 0 - successful
9504 * -ENOMEM - FC attach transport failed
9505 * all others - failed
9506 */
dea31012005-04-17 16:05:31 -05009507static int __init
9508lpfc_init(void)
9509{
9510 int error = 0;
9511
9512 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009513 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05009514
James Smart7ee5d432007-10-27 13:37:17 -04009515 if (lpfc_enable_npiv) {
9516 lpfc_transport_functions.vport_create = lpfc_vport_create;
9517 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
9518 }
dea31012005-04-17 16:05:31 -05009519 lpfc_transport_template =
9520 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04009521 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05009522 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04009523 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04009524 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04009525 fc_attach_transport(&lpfc_vport_transport_functions);
9526 if (lpfc_vport_transport_template == NULL) {
9527 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04009528 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04009529 }
James Smart7ee5d432007-10-27 13:37:17 -04009530 }
dea31012005-04-17 16:05:31 -05009531 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05009532 if (error) {
dea31012005-04-17 16:05:31 -05009533 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04009534 if (lpfc_enable_npiv)
9535 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05009536 }
dea31012005-04-17 16:05:31 -05009537
9538 return error;
9539}
9540
James Smarte59058c2008-08-24 21:49:00 -04009541/**
James Smart3621a712009-04-06 18:47:14 -04009542 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04009543 *
9544 * This routine is invoked when the lpfc module is removed from the kernel.
9545 * The special kernel macro module_exit() is used to indicate the role of
9546 * this routine to the kernel as lpfc module exit point.
9547 */
dea31012005-04-17 16:05:31 -05009548static void __exit
9549lpfc_exit(void)
9550{
9551 pci_unregister_driver(&lpfc_driver);
9552 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04009553 if (lpfc_enable_npiv)
9554 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05009555 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04009556 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
9557 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05009558 (1L << _dump_buf_data_order), _dump_buf_data);
9559 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
9560 }
9561
9562 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04009563 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
9564 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05009565 (1L << _dump_buf_dif_order), _dump_buf_dif);
9566 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
9567 }
dea31012005-04-17 16:05:31 -05009568}
9569
9570module_init(lpfc_init);
9571module_exit(lpfc_exit);
9572MODULE_LICENSE("GPL");
9573MODULE_DESCRIPTION(LPFC_MODULE_DESC);
9574MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
9575MODULE_VERSION("0:" LPFC_DRIVER_VERSION);