blob: d654c0e3db4d214b8436112f97fc709218fa2689 [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 Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 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>
dea31012005-04-17 16:05:31 -050031
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040032#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport_fc.h>
36
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_hw.h"
39#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040040#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040041#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_disc.h"
43#include "lpfc_scsi.h"
44#include "lpfc.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050047#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_version.h"
49
James Smart81301a92008-12-04 22:39:46 -050050char *_dump_buf_data;
51unsigned long _dump_buf_data_order;
52char *_dump_buf_dif;
53unsigned long _dump_buf_dif_order;
54spinlock_t _dump_buf_lock;
55
dea31012005-04-17 16:05:31 -050056static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
57static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040058static int lpfc_sli4_queue_create(struct lpfc_hba *);
59static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
60static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
61static int lpfc_setup_endian_order(struct lpfc_hba *);
62static int lpfc_sli4_read_config(struct lpfc_hba *);
63static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
64static void lpfc_free_sgl_list(struct lpfc_hba *);
65static int lpfc_init_sgl_list(struct lpfc_hba *);
66static int lpfc_init_active_sgl_array(struct lpfc_hba *);
67static void lpfc_free_active_sgl(struct lpfc_hba *);
68static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
69static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
70static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
71static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
72static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050073
74static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050075static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050076static DEFINE_IDR(lpfc_hba_index);
77
James Smarte59058c2008-08-24 21:49:00 -040078/**
James Smart3621a712009-04-06 18:47:14 -040079 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040080 * @phba: pointer to lpfc hba data structure.
81 *
82 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
83 * mailbox command. It retrieves the revision information from the HBA and
84 * collects the Vital Product Data (VPD) about the HBA for preparing the
85 * configuration of the HBA.
86 *
87 * Return codes:
88 * 0 - success.
89 * -ERESTART - requests the SLI layer to reset the HBA and try again.
90 * Any other value - indicates an error.
91 **/
dea31012005-04-17 16:05:31 -050092int
James Smart2e0fef82007-06-17 19:56:36 -050093lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050094{
95 lpfc_vpd_t *vp = &phba->vpd;
96 int i = 0, rc;
97 LPFC_MBOXQ_t *pmb;
98 MAILBOX_t *mb;
99 char *lpfc_vpd_data = NULL;
100 uint16_t offset = 0;
101 static char licensed[56] =
102 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400103 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500104
105 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
106 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500107 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500108 return -ENOMEM;
109 }
110
James Smart04c68492009-05-22 14:52:52 -0400111 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500112 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500113
114 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400115 if (init_key) {
116 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500117
James Smart65a29c12006-07-06 15:50:50 -0400118 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
119 *ptext = cpu_to_be32(*ptext);
120 init_key = 0;
121 }
dea31012005-04-17 16:05:31 -0500122
123 lpfc_read_nv(phba, pmb);
124 memset((char*)mb->un.varRDnvp.rsvd3, 0,
125 sizeof (mb->un.varRDnvp.rsvd3));
126 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
127 sizeof (licensed));
128
129 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
130
131 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400133 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500134 "error, mbxCmd x%x READ_NVPARM, "
135 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500136 mb->mbxCommand, mb->mbxStatus);
137 mempool_free(pmb, phba->mbox_mem_pool);
138 return -ERESTART;
139 }
140 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500141 sizeof(phba->wwnn));
142 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
143 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500144 }
145
James Smart92d7f7b2007-06-17 19:56:38 -0500146 phba->sli3_options = 0x0;
147
dea31012005-04-17 16:05:31 -0500148 /* Setup and issue mailbox READ REV command */
149 lpfc_read_rev(phba, pmb);
150 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
151 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400153 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500154 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500155 mb->mbxCommand, mb->mbxStatus);
156 mempool_free( pmb, phba->mbox_mem_pool);
157 return -ERESTART;
158 }
159
James Smart92d7f7b2007-06-17 19:56:38 -0500160
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500161 /*
162 * The value of rr must be 1 since the driver set the cv field to 1.
163 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500164 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500165 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500166 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400168 "0440 Adapter failed to init, READ_REV has "
169 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500170 mempool_free(pmb, phba->mbox_mem_pool);
171 return -ERESTART;
dea31012005-04-17 16:05:31 -0500172 }
173
James Smart495a7142008-06-14 22:52:59 -0400174 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
175 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500176 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400177 }
James Smarted957682007-06-17 19:56:37 -0500178
dea31012005-04-17 16:05:31 -0500179 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500180 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500181 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500182 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
183 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
184 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
185 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500186 vp->rev.biuRev = mb->un.varRdRev.biuRev;
187 vp->rev.smRev = mb->un.varRdRev.smRev;
188 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
189 vp->rev.endecRev = mb->un.varRdRev.endecRev;
190 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
191 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
192 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
193 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
194 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
195 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
196
James Smart92d7f7b2007-06-17 19:56:38 -0500197 /* If the sli feature level is less then 9, we must
198 * tear down all RPIs and VPIs on link down if NPIV
199 * is enabled.
200 */
201 if (vp->rev.feaLevelHigh < 9)
202 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
203
dea31012005-04-17 16:05:31 -0500204 if (lpfc_is_LC_HBA(phba->pcidev->device))
205 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
206 sizeof (phba->RandomData));
207
dea31012005-04-17 16:05:31 -0500208 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500209 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
210 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400211 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500212
213 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400214 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500215 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
216
217 if (rc != MBX_SUCCESS) {
218 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400219 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500220 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500221 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500222 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500223 }
James Smart04c68492009-05-22 14:52:52 -0400224 /* dump mem may return a zero when finished or we got a
225 * mailbox error, either way we are done.
226 */
227 if (mb->un.varDmp.word_cnt == 0)
228 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500229 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
230 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400231 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
232 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500233 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500234 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500235 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
236 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500237
238 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500239out_free_mbox:
240 mempool_free(pmb, phba->mbox_mem_pool);
241 return 0;
242}
243
James Smarte59058c2008-08-24 21:49:00 -0400244/**
James Smart3621a712009-04-06 18:47:14 -0400245 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400246 * @phba: pointer to lpfc hba data structure.
247 * @pmboxq: pointer to the driver internal queue element for mailbox command.
248 *
249 * This is the completion handler for driver's configuring asynchronous event
250 * mailbox command to the device. If the mailbox command returns successfully,
251 * it will set internal async event support flag to 1; otherwise, it will
252 * set internal async event support flag to 0.
253 **/
James Smart57127f12007-10-27 13:37:05 -0400254static void
255lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
256{
James Smart04c68492009-05-22 14:52:52 -0400257 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400258 phba->temp_sensor_support = 1;
259 else
260 phba->temp_sensor_support = 0;
261 mempool_free(pmboxq, phba->mbox_mem_pool);
262 return;
263}
264
James Smarte59058c2008-08-24 21:49:00 -0400265/**
James Smart3621a712009-04-06 18:47:14 -0400266 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500267 * @phba: pointer to lpfc hba data structure.
268 * @pmboxq: pointer to the driver internal queue element for mailbox command.
269 *
270 * This is the completion handler for dump mailbox command for getting
271 * wake up parameters. When this command complete, the response contain
272 * Option rom version of the HBA. This function translate the version number
273 * into a human readable string and store it in OptionROMVersion.
274 **/
275static void
276lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
277{
278 struct prog_id *prg;
279 uint32_t prog_id_word;
280 char dist = ' ';
281 /* character array used for decoding dist type. */
282 char dist_char[] = "nabx";
283
James Smart04c68492009-05-22 14:52:52 -0400284 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500285 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500286 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500287 }
James Smart97207482008-12-04 22:39:19 -0500288
289 prg = (struct prog_id *) &prog_id_word;
290
291 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400292 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500293
294 /* Decode the Option rom version word to a readable string */
295 if (prg->dist < 4)
296 dist = dist_char[prg->dist];
297
298 if ((prg->dist == 3) && (prg->num == 0))
299 sprintf(phba->OptionROMVersion, "%d.%d%d",
300 prg->ver, prg->rev, prg->lev);
301 else
302 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
303 prg->ver, prg->rev, prg->lev,
304 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500305 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500306 return;
307}
308
309/**
James Smart3621a712009-04-06 18:47:14 -0400310 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400311 * @phba: pointer to lpfc hba data structure.
312 *
313 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
314 * command call. It performs all internal resource and state setups on the
315 * port: post IOCB buffers, enable appropriate host interrupt attentions,
316 * ELS ring timers, etc.
317 *
318 * Return codes
319 * 0 - success.
320 * Any other value - error.
321 **/
dea31012005-04-17 16:05:31 -0500322int
James Smart2e0fef82007-06-17 19:56:36 -0500323lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500324{
James Smart2e0fef82007-06-17 19:56:36 -0500325 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400326 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500327 LPFC_MBOXQ_t *pmb;
328 MAILBOX_t *mb;
329 struct lpfc_dmabuf *mp;
330 struct lpfc_sli *psli = &phba->sli;
331 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500332 int i, j;
333 int rc;
dea31012005-04-17 16:05:31 -0500334
James Smart7af67052007-10-27 13:38:11 -0400335 spin_lock_irq(&phba->hbalock);
336 /*
337 * If the Config port completed correctly the HBA is not
338 * over heated any more.
339 */
340 if (phba->over_temp_state == HBA_OVER_TEMP)
341 phba->over_temp_state = HBA_NORMAL_TEMP;
342 spin_unlock_irq(&phba->hbalock);
343
dea31012005-04-17 16:05:31 -0500344 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
345 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500346 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500347 return -ENOMEM;
348 }
James Smart04c68492009-05-22 14:52:52 -0400349 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500350
dea31012005-04-17 16:05:31 -0500351 /* Get login parameters for NID. */
James Smart92d7f7b2007-06-17 19:56:38 -0500352 lpfc_read_sparam(phba, pmb, 0);
James Smarted957682007-06-17 19:56:37 -0500353 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500354 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400356 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500357 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500358 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500359 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500360 mp = (struct lpfc_dmabuf *) pmb->context1;
361 mempool_free( pmb, phba->mbox_mem_pool);
362 lpfc_mbuf_free(phba, mp->virt, mp->phys);
363 kfree(mp);
364 return -EIO;
365 }
366
367 mp = (struct lpfc_dmabuf *) pmb->context1;
368
James Smart2e0fef82007-06-17 19:56:36 -0500369 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500370 lpfc_mbuf_free(phba, mp->virt, mp->phys);
371 kfree(mp);
372 pmb->context1 = NULL;
373
James Smarta12e07b2006-12-02 13:35:30 -0500374 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500375 u64_to_wwn(phba->cfg_soft_wwnn,
376 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400377 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500378 u64_to_wwn(phba->cfg_soft_wwpn,
379 vport->fc_sparam.portName.u.wwn);
380 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500381 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500382 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500383 sizeof (struct lpfc_name));
James Smarta257bf92009-04-06 18:48:10 -0400384
385 /* Update the fc_host data structures with new wwn. */
386 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
387 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400388 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400389
dea31012005-04-17 16:05:31 -0500390 /* If no serial number in VPD data, use low 6 bytes of WWNN */
391 /* This should be consolidated into parse_vpd ? - mr */
392 if (phba->SerialNumber[0] == 0) {
393 uint8_t *outptr;
394
James Smart2e0fef82007-06-17 19:56:36 -0500395 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500396 for (i = 0; i < 12; i++) {
397 status = *outptr++;
398 j = ((status & 0xf0) >> 4);
399 if (j <= 9)
400 phba->SerialNumber[i] =
401 (char)((uint8_t) 0x30 + (uint8_t) j);
402 else
403 phba->SerialNumber[i] =
404 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
405 i++;
406 j = (status & 0xf);
407 if (j <= 9)
408 phba->SerialNumber[i] =
409 (char)((uint8_t) 0x30 + (uint8_t) j);
410 else
411 phba->SerialNumber[i] =
412 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
413 }
414 }
415
dea31012005-04-17 16:05:31 -0500416 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500417 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500418 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400420 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500421 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500422 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500423 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500424 mempool_free( pmb, phba->mbox_mem_pool);
425 return -EIO;
426 }
427
James Smarta0c87cb2009-07-19 10:01:10 -0400428 /* Check if the port is disabled */
429 lpfc_sli_read_link_ste(phba);
430
dea31012005-04-17 16:05:31 -0500431 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
432 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
433 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400434 (mb->un.varRdConfig.max_xri + 1) -
435 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500436
437 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500438
439 /* Get the default values for Model Name and Description */
440 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
441
442 if ((phba->cfg_link_speed > LINK_SPEED_10G)
443 || ((phba->cfg_link_speed == LINK_SPEED_1G)
444 && !(phba->lmt & LMT_1Gb))
445 || ((phba->cfg_link_speed == LINK_SPEED_2G)
446 && !(phba->lmt & LMT_2Gb))
447 || ((phba->cfg_link_speed == LINK_SPEED_4G)
448 && !(phba->lmt & LMT_4Gb))
449 || ((phba->cfg_link_speed == LINK_SPEED_8G)
450 && !(phba->lmt & LMT_8Gb))
451 || ((phba->cfg_link_speed == LINK_SPEED_10G)
452 && !(phba->lmt & LMT_10Gb))) {
453 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500454 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400455 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500456 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500457 phba->cfg_link_speed);
458 phba->cfg_link_speed = LINK_SPEED_AUTO;
459 }
460
James Smart2e0fef82007-06-17 19:56:36 -0500461 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500462
James Smart0b727fe2007-10-27 13:37:25 -0400463 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500464 if (psli->ring[psli->extra_ring].cmdringaddr)
465 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500466 if (psli->ring[psli->fcp_ring].cmdringaddr)
467 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
468 if (psli->ring[psli->next_ring].cmdringaddr)
469 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
470
471 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500472 if (phba->sli_rev != 3)
473 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500474
James Smart93996272008-08-24 21:50:30 -0400475 /*
476 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
477 */
478 if (phba->intr_type == MSIX) {
479 rc = lpfc_config_msi(phba, pmb);
480 if (rc) {
481 mempool_free(pmb, phba->mbox_mem_pool);
482 return -EIO;
483 }
484 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
485 if (rc != MBX_SUCCESS) {
486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
487 "0352 Config MSI mailbox command "
488 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400489 pmb->u.mb.mbxCommand,
490 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400491 mempool_free(pmb, phba->mbox_mem_pool);
492 return -EIO;
493 }
494 }
495
James Smart04c68492009-05-22 14:52:52 -0400496 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400497 /* Initialize ERATT handling flag */
498 phba->hba_flag &= ~HBA_ERATT_HANDLED;
499
dea31012005-04-17 16:05:31 -0500500 /* Enable appropriate host interrupts */
dea31012005-04-17 16:05:31 -0500501 status = readl(phba->HCregaddr);
502 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
503 if (psli->num_rings > 0)
504 status |= HC_R0INT_ENA;
505 if (psli->num_rings > 1)
506 status |= HC_R1INT_ENA;
507 if (psli->num_rings > 2)
508 status |= HC_R2INT_ENA;
509 if (psli->num_rings > 3)
510 status |= HC_R3INT_ENA;
511
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500512 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
513 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400514 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500515
dea31012005-04-17 16:05:31 -0500516 writel(status, phba->HCregaddr);
517 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500518 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500519
James Smart93996272008-08-24 21:50:30 -0400520 /* Set up ring-0 (ELS) timer */
521 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500522 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400523 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500524 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
525 phba->hb_outstanding = 0;
526 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400527 /* Set up error attention (ERATT) polling timer */
528 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500529
James Smarta0c87cb2009-07-19 10:01:10 -0400530 if (phba->hba_flag & LINK_DISABLED) {
531 lpfc_printf_log(phba,
532 KERN_ERR, LOG_INIT,
533 "2598 Adapter Link is disabled.\n");
534 lpfc_down_link(phba, pmb);
535 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
536 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
537 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
538 lpfc_printf_log(phba,
539 KERN_ERR, LOG_INIT,
540 "2599 Adapter failed to issue DOWN_LINK"
541 " mbox command rc 0x%x\n", rc);
542
543 mempool_free(pmb, phba->mbox_mem_pool);
544 return -EIO;
545 }
546 } else {
547 lpfc_init_link(phba, pmb, phba->cfg_topology,
548 phba->cfg_link_speed);
549 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
550 lpfc_set_loopback_flag(phba);
551 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
552 if (rc != MBX_SUCCESS) {
553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400554 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500555 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500556 mb->mbxCommand, mb->mbxStatus);
557
James Smarta0c87cb2009-07-19 10:01:10 -0400558 /* Clear all interrupt enable conditions */
559 writel(0, phba->HCregaddr);
560 readl(phba->HCregaddr); /* flush */
561 /* Clear all pending interrupts */
562 writel(0xffffffff, phba->HAregaddr);
563 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500564
James Smarta0c87cb2009-07-19 10:01:10 -0400565 phba->link_state = LPFC_HBA_ERROR;
566 if (rc != MBX_BUSY)
567 mempool_free(pmb, phba->mbox_mem_pool);
568 return -EIO;
569 }
dea31012005-04-17 16:05:31 -0500570 }
571 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400572 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
573 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
574 pmb->mbox_cmpl = lpfc_config_async_cmpl;
575 pmb->vport = phba->pport;
576 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500577
James Smart57127f12007-10-27 13:37:05 -0400578 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
579 lpfc_printf_log(phba,
580 KERN_ERR,
581 LOG_INIT,
582 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400583 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400584 rc);
585 mempool_free(pmb, phba->mbox_mem_pool);
586 }
James Smart97207482008-12-04 22:39:19 -0500587
588 /* Get Option rom version */
589 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
590 lpfc_dump_wakeup_param(phba, pmb);
591 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
592 pmb->vport = phba->pport;
593 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
594
595 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400597 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500598 mempool_free(pmb, phba->mbox_mem_pool);
599 }
600
James Smartd7c255b2008-08-24 21:50:00 -0400601 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400602}
603
James Smarte59058c2008-08-24 21:49:00 -0400604/**
James Smart3621a712009-04-06 18:47:14 -0400605 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400606 * @phba: pointer to lpfc HBA data structure.
607 *
608 * This routine will do LPFC uninitialization before the HBA is reset when
609 * bringing down the SLI Layer.
610 *
611 * Return codes
612 * 0 - success.
613 * Any other value - error.
614 **/
dea31012005-04-17 16:05:31 -0500615int
James Smart2e0fef82007-06-17 19:56:36 -0500616lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500617{
James Smart1b32f6a2008-02-08 18:49:39 -0500618 struct lpfc_vport **vports;
619 int i;
James Smart3772a992009-05-22 14:50:54 -0400620
621 if (phba->sli_rev <= LPFC_SLI_REV3) {
622 /* Disable interrupts */
623 writel(0, phba->HCregaddr);
624 readl(phba->HCregaddr); /* flush */
625 }
dea31012005-04-17 16:05:31 -0500626
James Smart1b32f6a2008-02-08 18:49:39 -0500627 if (phba->pport->load_flag & FC_UNLOADING)
628 lpfc_cleanup_discovery_resources(phba->pport);
629 else {
630 vports = lpfc_create_vport_work_array(phba);
631 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400632 for (i = 0; i <= phba->max_vports &&
633 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500634 lpfc_cleanup_discovery_resources(vports[i]);
635 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500636 }
637 return 0;
dea31012005-04-17 16:05:31 -0500638}
639
James Smarte59058c2008-08-24 21:49:00 -0400640/**
James Smart3772a992009-05-22 14:50:54 -0400641 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400642 * @phba: pointer to lpfc HBA data structure.
643 *
644 * This routine will do uninitialization after the HBA is reset when bring
645 * down the SLI Layer.
646 *
647 * Return codes
648 * 0 - sucess.
649 * Any other value - error.
650 **/
James Smart3772a992009-05-22 14:50:54 -0400651static int
652lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500653{
654 struct lpfc_sli *psli = &phba->sli;
655 struct lpfc_sli_ring *pring;
656 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500657 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500658 int i;
659
James Smart92d7f7b2007-06-17 19:56:38 -0500660 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
661 lpfc_sli_hbqbuf_free_all(phba);
662 else {
663 /* Cleanup preposted buffers on the ELS ring */
664 pring = &psli->ring[LPFC_ELS_RING];
665 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
666 list_del(&mp->list);
667 pring->postbufq_cnt--;
668 lpfc_mbuf_free(phba, mp->virt, mp->phys);
669 kfree(mp);
670 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500671 }
672
James Smart09372822008-01-11 01:52:54 -0500673 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500674 for (i = 0; i < psli->num_rings; i++) {
675 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500676
677 /* At this point in time the HBA is either reset or DOA. Either
678 * way, nothing should be on txcmplq as it will NEVER complete.
679 */
680 list_splice_init(&pring->txcmplq, &completions);
681 pring->txcmplq_cnt = 0;
682 spin_unlock_irq(&phba->hbalock);
683
James Smarta257bf92009-04-06 18:48:10 -0400684 /* Cancel all the IOCBs from the completions list */
685 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
686 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500687
Jamie Wellnitz41415862006-02-28 19:25:27 -0500688 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500689 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500690 }
James Smart09372822008-01-11 01:52:54 -0500691 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500692
693 return 0;
694}
James Smartda0436e2009-05-22 14:51:39 -0400695/**
696 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
697 * @phba: pointer to lpfc HBA data structure.
698 *
699 * This routine will do uninitialization after the HBA is reset when bring
700 * down the SLI Layer.
701 *
702 * Return codes
703 * 0 - sucess.
704 * Any other value - error.
705 **/
706static int
707lpfc_hba_down_post_s4(struct lpfc_hba *phba)
708{
709 struct lpfc_scsi_buf *psb, *psb_next;
710 LIST_HEAD(aborts);
711 int ret;
712 unsigned long iflag = 0;
713 ret = lpfc_hba_down_post_s3(phba);
714 if (ret)
715 return ret;
716 /* At this point in time the HBA is either reset or DOA. Either
717 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
718 * on the lpfc_sgl_list so that it can either be freed if the
719 * driver is unloading or reposted if the driver is restarting
720 * the port.
721 */
722 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
723 /* scsl_buf_list */
724 /* abts_sgl_list_lock required because worker thread uses this
725 * list.
726 */
727 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
728 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
729 &phba->sli4_hba.lpfc_sgl_list);
730 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
731 /* abts_scsi_buf_list_lock required because worker thread uses this
732 * list.
733 */
734 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
735 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
736 &aborts);
737 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
738 spin_unlock_irq(&phba->hbalock);
739
740 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
741 psb->pCmd = NULL;
742 psb->status = IOSTAT_SUCCESS;
743 }
744 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
745 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
746 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
747 return 0;
748}
749
750/**
751 * lpfc_hba_down_post - Wrapper func for hba down post routine
752 * @phba: pointer to lpfc HBA data structure.
753 *
754 * This routine wraps the actual SLI3 or SLI4 routine for performing
755 * uninitialization after the HBA is reset when bring down the SLI Layer.
756 *
757 * Return codes
758 * 0 - sucess.
759 * Any other value - error.
760 **/
761int
762lpfc_hba_down_post(struct lpfc_hba *phba)
763{
764 return (*phba->lpfc_hba_down_post)(phba);
765}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500766
James Smarte59058c2008-08-24 21:49:00 -0400767/**
James Smart3621a712009-04-06 18:47:14 -0400768 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400769 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
770 *
771 * This is the HBA-timer timeout handler registered to the lpfc driver. When
772 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
773 * work-port-events bitmap and the worker thread is notified. This timeout
774 * event will be used by the worker thread to invoke the actual timeout
775 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
776 * be performed in the timeout handler and the HBA timeout event bit shall
777 * be cleared by the worker thread after it has taken the event bitmap out.
778 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100779static void
James Smart858c9f62007-06-17 19:56:39 -0500780lpfc_hb_timeout(unsigned long ptr)
781{
782 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400783 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500784 unsigned long iflag;
785
786 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400787
788 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500789 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400790 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
791 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500792 phba->pport->work_port_events |= WORKER_HB_TMO;
793 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
794
James Smart93996272008-08-24 21:50:30 -0400795 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400796 if (!tmo_posted)
797 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500798 return;
799}
800
James Smarte59058c2008-08-24 21:49:00 -0400801/**
James Smart3621a712009-04-06 18:47:14 -0400802 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400803 * @phba: pointer to lpfc hba data structure.
804 * @pmboxq: pointer to the driver internal queue element for mailbox command.
805 *
806 * This is the callback function to the lpfc heart-beat mailbox command.
807 * If configured, the lpfc driver issues the heart-beat mailbox command to
808 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
809 * heart-beat mailbox command is issued, the driver shall set up heart-beat
810 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
811 * heart-beat outstanding state. Once the mailbox command comes back and
812 * no error conditions detected, the heart-beat mailbox command timer is
813 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
814 * state is cleared for the next heart-beat. If the timer expired with the
815 * heart-beat outstanding state set, the driver will put the HBA offline.
816 **/
James Smart858c9f62007-06-17 19:56:39 -0500817static void
818lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
819{
820 unsigned long drvr_flag;
821
822 spin_lock_irqsave(&phba->hbalock, drvr_flag);
823 phba->hb_outstanding = 0;
824 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
825
James Smart93996272008-08-24 21:50:30 -0400826 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500827 mempool_free(pmboxq, phba->mbox_mem_pool);
828 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
829 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400830 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500831 mod_timer(&phba->hb_tmofunc,
832 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
833 return;
834}
835
James Smarte59058c2008-08-24 21:49:00 -0400836/**
James Smart3621a712009-04-06 18:47:14 -0400837 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400838 * @phba: pointer to lpfc hba data structure.
839 *
840 * This is the actual HBA-timer timeout handler to be invoked by the worker
841 * thread whenever the HBA timer fired and HBA-timeout event posted. This
842 * handler performs any periodic operations needed for the device. If such
843 * periodic event has already been attended to either in the interrupt handler
844 * or by processing slow-ring or fast-ring events within the HBA-timer
845 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
846 * the timer for the next timeout period. If lpfc heart-beat mailbox command
847 * is configured and there is no heart-beat mailbox command outstanding, a
848 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
849 * has been a heart-beat mailbox command outstanding, the HBA shall be put
850 * to offline.
851 **/
James Smart858c9f62007-06-17 19:56:39 -0500852void
853lpfc_hb_timeout_handler(struct lpfc_hba *phba)
854{
855 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500856 struct lpfc_dmabuf *buf_ptr;
James Smart858c9f62007-06-17 19:56:39 -0500857 int retval;
858 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500859 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500860
861 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400862 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500863 (phba->pport->fc_flag & FC_OFFLINE_MODE))
864 return;
865
866 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -0500867
868 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
869 jiffies)) {
870 spin_unlock_irq(&phba->pport->work_port_lock);
871 if (!phba->hb_outstanding)
872 mod_timer(&phba->hb_tmofunc,
873 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
874 else
875 mod_timer(&phba->hb_tmofunc,
876 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
877 return;
878 }
879 spin_unlock_irq(&phba->pport->work_port_lock);
880
James Smart0ff10d42008-01-11 01:52:36 -0500881 if (phba->elsbuf_cnt &&
882 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
883 spin_lock_irq(&phba->hbalock);
884 list_splice_init(&phba->elsbuf, &completions);
885 phba->elsbuf_cnt = 0;
886 phba->elsbuf_prev_cnt = 0;
887 spin_unlock_irq(&phba->hbalock);
888
889 while (!list_empty(&completions)) {
890 list_remove_head(&completions, buf_ptr,
891 struct lpfc_dmabuf, list);
892 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
893 kfree(buf_ptr);
894 }
895 }
896 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
897
James Smart858c9f62007-06-17 19:56:39 -0500898 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -0500899 if (phba->cfg_enable_hba_heartbeat) {
900 if (!phba->hb_outstanding) {
901 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
902 if (!pmboxq) {
903 mod_timer(&phba->hb_tmofunc,
904 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
905 return;
906 }
907
908 lpfc_heart_beat(phba, pmboxq);
909 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
910 pmboxq->vport = phba->pport;
911 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
912
913 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
914 mempool_free(pmboxq, phba->mbox_mem_pool);
915 mod_timer(&phba->hb_tmofunc,
916 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
917 return;
918 }
James Smart858c9f62007-06-17 19:56:39 -0500919 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -0500920 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
921 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -0500922 return;
James Smart13815c82008-01-11 01:52:48 -0500923 } else {
924 /*
925 * If heart beat timeout called with hb_outstanding set
926 * we need to take the HBA offline.
927 */
928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
929 "0459 Adapter heartbeat failure, "
930 "taking this port offline.\n");
931
932 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400933 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart13815c82008-01-11 01:52:48 -0500934 spin_unlock_irq(&phba->hbalock);
935
936 lpfc_offline_prep(phba);
937 lpfc_offline(phba);
938 lpfc_unblock_mgmt_io(phba);
939 phba->link_state = LPFC_HBA_ERROR;
940 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -0500941 }
James Smart858c9f62007-06-17 19:56:39 -0500942 }
943}
944
James Smarte59058c2008-08-24 21:49:00 -0400945/**
James Smart3621a712009-04-06 18:47:14 -0400946 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -0400947 * @phba: pointer to lpfc hba data structure.
948 *
949 * This routine is called to bring the HBA offline when HBA hardware error
950 * other than Port Error 6 has been detected.
951 **/
James Smart09372822008-01-11 01:52:54 -0500952static void
953lpfc_offline_eratt(struct lpfc_hba *phba)
954{
955 struct lpfc_sli *psli = &phba->sli;
956
957 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400958 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -0500959 spin_unlock_irq(&phba->hbalock);
960 lpfc_offline_prep(phba);
961
962 lpfc_offline(phba);
963 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400964 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500965 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400966 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500967 lpfc_hba_down_post(phba);
968 lpfc_sli_brdready(phba, HS_MBRDY);
969 lpfc_unblock_mgmt_io(phba);
970 phba->link_state = LPFC_HBA_ERROR;
971 return;
972}
973
James Smarte59058c2008-08-24 21:49:00 -0400974/**
James Smartda0436e2009-05-22 14:51:39 -0400975 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
976 * @phba: pointer to lpfc hba data structure.
977 *
978 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
979 * other than Port Error 6 has been detected.
980 **/
981static void
982lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
983{
984 lpfc_offline_prep(phba);
985 lpfc_offline(phba);
986 lpfc_sli4_brdreset(phba);
987 lpfc_hba_down_post(phba);
988 lpfc_sli4_post_status_check(phba);
989 lpfc_unblock_mgmt_io(phba);
990 phba->link_state = LPFC_HBA_ERROR;
991}
992
993/**
James Smarta257bf92009-04-06 18:48:10 -0400994 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
995 * @phba: pointer to lpfc hba data structure.
996 *
997 * This routine is invoked to handle the deferred HBA hardware error
998 * conditions. This type of error is indicated by HBA by setting ER1
999 * and another ER bit in the host status register. The driver will
1000 * wait until the ER1 bit clears before handling the error condition.
1001 **/
1002static void
1003lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1004{
1005 uint32_t old_host_status = phba->work_hs;
1006 struct lpfc_sli_ring *pring;
1007 struct lpfc_sli *psli = &phba->sli;
1008
James Smartf4b4c682009-05-22 14:53:12 -04001009 /* If the pci channel is offline, ignore possible errors,
1010 * since we cannot communicate with the pci card anyway.
1011 */
1012 if (pci_channel_offline(phba->pcidev)) {
1013 spin_lock_irq(&phba->hbalock);
1014 phba->hba_flag &= ~DEFER_ERATT;
1015 spin_unlock_irq(&phba->hbalock);
1016 return;
1017 }
1018
James Smarta257bf92009-04-06 18:48:10 -04001019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1020 "0479 Deferred Adapter Hardware Error "
1021 "Data: x%x x%x x%x\n",
1022 phba->work_hs,
1023 phba->work_status[0], phba->work_status[1]);
1024
1025 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001026 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001027 spin_unlock_irq(&phba->hbalock);
1028
1029
1030 /*
1031 * Firmware stops when it triggred erratt. That could cause the I/Os
1032 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1033 * SCSI layer retry it after re-establishing link.
1034 */
1035 pring = &psli->ring[psli->fcp_ring];
1036 lpfc_sli_abort_iocb_ring(phba, pring);
1037
1038 /*
1039 * There was a firmware error. Take the hba offline and then
1040 * attempt to restart it.
1041 */
1042 lpfc_offline_prep(phba);
1043 lpfc_offline(phba);
1044
1045 /* Wait for the ER1 bit to clear.*/
1046 while (phba->work_hs & HS_FFER1) {
1047 msleep(100);
1048 phba->work_hs = readl(phba->HSregaddr);
1049 /* If driver is unloading let the worker thread continue */
1050 if (phba->pport->load_flag & FC_UNLOADING) {
1051 phba->work_hs = 0;
1052 break;
1053 }
1054 }
1055
1056 /*
1057 * This is to ptrotect against a race condition in which
1058 * first write to the host attention register clear the
1059 * host status register.
1060 */
1061 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1062 phba->work_hs = old_host_status & ~HS_FFER1;
1063
James Smart3772a992009-05-22 14:50:54 -04001064 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001065 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001066 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001067 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1068 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1069}
1070
James Smart3772a992009-05-22 14:50:54 -04001071static void
1072lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1073{
1074 struct lpfc_board_event_header board_event;
1075 struct Scsi_Host *shost;
1076
1077 board_event.event_type = FC_REG_BOARD_EVENT;
1078 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1079 shost = lpfc_shost_from_vport(phba->pport);
1080 fc_host_post_vendor_event(shost, fc_get_event_number(),
1081 sizeof(board_event),
1082 (char *) &board_event,
1083 LPFC_NL_VENDOR_ID);
1084}
1085
James Smarta257bf92009-04-06 18:48:10 -04001086/**
James Smart3772a992009-05-22 14:50:54 -04001087 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001088 * @phba: pointer to lpfc hba data structure.
1089 *
1090 * This routine is invoked to handle the following HBA hardware error
1091 * conditions:
1092 * 1 - HBA error attention interrupt
1093 * 2 - DMA ring index out of range
1094 * 3 - Mailbox command came back as unknown
1095 **/
James Smart3772a992009-05-22 14:50:54 -04001096static void
1097lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001098{
James Smart2e0fef82007-06-17 19:56:36 -05001099 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001100 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001101 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001102 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001103 unsigned long temperature;
1104 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001105 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001106
Linas Vepstas8d63f372007-02-14 14:28:36 -06001107 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001108 * since we cannot communicate with the pci card anyway.
1109 */
1110 if (pci_channel_offline(phba->pcidev)) {
1111 spin_lock_irq(&phba->hbalock);
1112 phba->hba_flag &= ~DEFER_ERATT;
1113 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001114 return;
James Smart3772a992009-05-22 14:50:54 -04001115 }
1116
James Smart13815c82008-01-11 01:52:48 -05001117 /* If resets are disabled then leave the HBA alone and return */
1118 if (!phba->cfg_enable_hba_reset)
1119 return;
dea31012005-04-17 16:05:31 -05001120
James Smartea2151b2008-09-07 11:52:10 -04001121 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001122 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001123
James Smarta257bf92009-04-06 18:48:10 -04001124 if (phba->hba_flag & DEFER_ERATT)
1125 lpfc_handle_deferred_eratt(phba);
1126
James Smart97eab632008-04-07 10:16:05 -04001127 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -05001128 /* Re-establishing Link */
1129 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001130 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -05001131 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001132 phba->work_hs,
dea31012005-04-17 16:05:31 -05001133 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001134
James Smart92d7f7b2007-06-17 19:56:38 -05001135 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001136 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001137 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001138
1139 /*
1140 * Firmware stops when it triggled erratt with HS_FFER6.
1141 * That could cause the I/Os dropped by the firmware.
1142 * Error iocb (I/O) on txcmplq and let the SCSI layer
1143 * retry it after re-establishing link.
1144 */
1145 pring = &psli->ring[psli->fcp_ring];
1146 lpfc_sli_abort_iocb_ring(phba, pring);
1147
dea31012005-04-17 16:05:31 -05001148 /*
1149 * There was a firmware error. Take the hba offline and then
1150 * attempt to restart it.
1151 */
James Smart46fa3112007-04-25 09:51:45 -04001152 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001153 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001154 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001155 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001156 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001157 return;
1158 }
James Smart46fa3112007-04-25 09:51:45 -04001159 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001160 } else if (phba->work_hs & HS_CRIT_TEMP) {
1161 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1162 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1163 temp_event_data.event_code = LPFC_CRIT_TEMP;
1164 temp_event_data.data = (uint32_t)temperature;
1165
1166 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001167 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001168 "(%ld), taking this port offline "
1169 "Data: x%x x%x x%x\n",
1170 temperature, phba->work_hs,
1171 phba->work_status[0], phba->work_status[1]);
1172
1173 shost = lpfc_shost_from_vport(phba->pport);
1174 fc_host_post_vendor_event(shost, fc_get_event_number(),
1175 sizeof(temp_event_data),
1176 (char *) &temp_event_data,
1177 SCSI_NL_VID_TYPE_PCI
1178 | PCI_VENDOR_ID_EMULEX);
1179
James Smart7af67052007-10-27 13:38:11 -04001180 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001181 phba->over_temp_state = HBA_OVER_TEMP;
1182 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001183 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001184
dea31012005-04-17 16:05:31 -05001185 } else {
1186 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001187 * failure is a value other than FFER6. Do not call the offline
1188 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001189 */
1190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001191 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001192 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001193 phba->work_hs,
dea31012005-04-17 16:05:31 -05001194 phba->work_status[0], phba->work_status[1]);
1195
James Smartd2873e42006-08-18 17:46:43 -04001196 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001197 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001198 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001199 sizeof(event_data), (char *) &event_data,
1200 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1201
James Smart09372822008-01-11 01:52:54 -05001202 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001203 }
James Smart93996272008-08-24 21:50:30 -04001204 return;
dea31012005-04-17 16:05:31 -05001205}
1206
James Smarte59058c2008-08-24 21:49:00 -04001207/**
James Smartda0436e2009-05-22 14:51:39 -04001208 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1209 * @phba: pointer to lpfc hba data structure.
1210 *
1211 * This routine is invoked to handle the SLI4 HBA hardware error attention
1212 * conditions.
1213 **/
1214static void
1215lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1216{
1217 struct lpfc_vport *vport = phba->pport;
1218 uint32_t event_data;
1219 struct Scsi_Host *shost;
1220
1221 /* If the pci channel is offline, ignore possible errors, since
1222 * we cannot communicate with the pci card anyway.
1223 */
1224 if (pci_channel_offline(phba->pcidev))
1225 return;
1226 /* If resets are disabled then leave the HBA alone and return */
1227 if (!phba->cfg_enable_hba_reset)
1228 return;
1229
1230 /* Send an internal error event to mgmt application */
1231 lpfc_board_errevt_to_mgmt(phba);
1232
1233 /* For now, the actual action for SLI4 device handling is not
1234 * specified yet, just treated it as adaptor hardware failure
1235 */
1236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1237 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1238 phba->work_status[0], phba->work_status[1]);
1239
1240 event_data = FC_REG_DUMP_EVENT;
1241 shost = lpfc_shost_from_vport(vport);
1242 fc_host_post_vendor_event(shost, fc_get_event_number(),
1243 sizeof(event_data), (char *) &event_data,
1244 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1245
1246 lpfc_sli4_offline_eratt(phba);
1247}
1248
1249/**
1250 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1251 * @phba: pointer to lpfc HBA data structure.
1252 *
1253 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1254 * routine from the API jump table function pointer from the lpfc_hba struct.
1255 *
1256 * Return codes
1257 * 0 - sucess.
1258 * Any other value - error.
1259 **/
1260void
1261lpfc_handle_eratt(struct lpfc_hba *phba)
1262{
1263 (*phba->lpfc_handle_eratt)(phba);
1264}
1265
1266/**
James Smart3621a712009-04-06 18:47:14 -04001267 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001268 * @phba: pointer to lpfc hba data structure.
1269 *
1270 * This routine is invoked from the worker thread to handle a HBA host
1271 * attention link event.
1272 **/
dea31012005-04-17 16:05:31 -05001273void
James Smart2e0fef82007-06-17 19:56:36 -05001274lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001275{
James Smart2e0fef82007-06-17 19:56:36 -05001276 struct lpfc_vport *vport = phba->pport;
1277 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001278 LPFC_MBOXQ_t *pmb;
1279 volatile uint32_t control;
1280 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001281 int rc = 0;
dea31012005-04-17 16:05:31 -05001282
1283 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001284 if (!pmb) {
1285 rc = 1;
dea31012005-04-17 16:05:31 -05001286 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001287 }
dea31012005-04-17 16:05:31 -05001288
1289 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001290 if (!mp) {
1291 rc = 2;
dea31012005-04-17 16:05:31 -05001292 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001293 }
dea31012005-04-17 16:05:31 -05001294
1295 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001296 if (!mp->virt) {
1297 rc = 3;
dea31012005-04-17 16:05:31 -05001298 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001299 }
dea31012005-04-17 16:05:31 -05001300
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001301 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001302 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001303
1304 psli->slistat.link_event++;
1305 lpfc_read_la(phba, pmb, mp);
1306 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001307 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001308 /* Block ELS IOCBs until we have processed this mbox command */
1309 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001310 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001311 if (rc == MBX_NOT_FINISHED) {
1312 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001313 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001314 }
dea31012005-04-17 16:05:31 -05001315
1316 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001317 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001318 writel(HA_LATT, phba->HAregaddr);
1319 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001320 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001321
1322 return;
1323
James Smart14691152006-12-02 13:34:28 -05001324lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001325 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001326 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001327lpfc_handle_latt_free_mp:
1328 kfree(mp);
1329lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001330 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001331lpfc_handle_latt_err_exit:
1332 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001333 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001334 psli->sli_flag |= LPFC_PROCESS_LA;
1335 control = readl(phba->HCregaddr);
1336 control |= HC_LAINT_ENA;
1337 writel(control, phba->HCregaddr);
1338 readl(phba->HCregaddr); /* flush */
1339
1340 /* Clear Link Attention in HA REG */
1341 writel(HA_LATT, phba->HAregaddr);
1342 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001343 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001344 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001345 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001346
James Smart09372822008-01-11 01:52:54 -05001347 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1348 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001349
1350 return;
1351}
1352
James Smarte59058c2008-08-24 21:49:00 -04001353/**
James Smart3621a712009-04-06 18:47:14 -04001354 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001355 * @phba: pointer to lpfc hba data structure.
1356 * @vpd: pointer to the vital product data.
1357 * @len: length of the vital product data in bytes.
1358 *
1359 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1360 * an array of characters. In this routine, the ModelName, ProgramType, and
1361 * ModelDesc, etc. fields of the phba data structure will be populated.
1362 *
1363 * Return codes
1364 * 0 - pointer to the VPD passed in is NULL
1365 * 1 - success
1366 **/
James Smart3772a992009-05-22 14:50:54 -04001367int
James Smart2e0fef82007-06-17 19:56:36 -05001368lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001369{
1370 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001371 int Length;
dea31012005-04-17 16:05:31 -05001372 int i, j;
1373 int finished = 0;
1374 int index = 0;
1375
1376 if (!vpd)
1377 return 0;
1378
1379 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001380 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001381 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001382 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1383 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001384 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001385 switch (vpd[index]) {
1386 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001387 case 0x91:
dea31012005-04-17 16:05:31 -05001388 index += 1;
1389 lenlo = vpd[index];
1390 index += 1;
1391 lenhi = vpd[index];
1392 index += 1;
1393 i = ((((unsigned short)lenhi) << 8) + lenlo);
1394 index += i;
1395 break;
1396 case 0x90:
1397 index += 1;
1398 lenlo = vpd[index];
1399 index += 1;
1400 lenhi = vpd[index];
1401 index += 1;
1402 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001403 if (Length > len - index)
1404 Length = len - index;
dea31012005-04-17 16:05:31 -05001405 while (Length > 0) {
1406 /* Look for Serial Number */
1407 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1408 index += 2;
1409 i = vpd[index];
1410 index += 1;
1411 j = 0;
1412 Length -= (3+i);
1413 while(i--) {
1414 phba->SerialNumber[j++] = vpd[index++];
1415 if (j == 31)
1416 break;
1417 }
1418 phba->SerialNumber[j] = 0;
1419 continue;
1420 }
1421 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1422 phba->vpd_flag |= VPD_MODEL_DESC;
1423 index += 2;
1424 i = vpd[index];
1425 index += 1;
1426 j = 0;
1427 Length -= (3+i);
1428 while(i--) {
1429 phba->ModelDesc[j++] = vpd[index++];
1430 if (j == 255)
1431 break;
1432 }
1433 phba->ModelDesc[j] = 0;
1434 continue;
1435 }
1436 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1437 phba->vpd_flag |= VPD_MODEL_NAME;
1438 index += 2;
1439 i = vpd[index];
1440 index += 1;
1441 j = 0;
1442 Length -= (3+i);
1443 while(i--) {
1444 phba->ModelName[j++] = vpd[index++];
1445 if (j == 79)
1446 break;
1447 }
1448 phba->ModelName[j] = 0;
1449 continue;
1450 }
1451 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1452 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1453 index += 2;
1454 i = vpd[index];
1455 index += 1;
1456 j = 0;
1457 Length -= (3+i);
1458 while(i--) {
1459 phba->ProgramType[j++] = vpd[index++];
1460 if (j == 255)
1461 break;
1462 }
1463 phba->ProgramType[j] = 0;
1464 continue;
1465 }
1466 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1467 phba->vpd_flag |= VPD_PORT;
1468 index += 2;
1469 i = vpd[index];
1470 index += 1;
1471 j = 0;
1472 Length -= (3+i);
1473 while(i--) {
1474 phba->Port[j++] = vpd[index++];
1475 if (j == 19)
1476 break;
1477 }
1478 phba->Port[j] = 0;
1479 continue;
1480 }
1481 else {
1482 index += 2;
1483 i = vpd[index];
1484 index += 1;
1485 index += i;
1486 Length -= (3 + i);
1487 }
1488 }
1489 finished = 0;
1490 break;
1491 case 0x78:
1492 finished = 1;
1493 break;
1494 default:
1495 index ++;
1496 break;
1497 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001498 }
dea31012005-04-17 16:05:31 -05001499
1500 return(1);
1501}
1502
James Smarte59058c2008-08-24 21:49:00 -04001503/**
James Smart3621a712009-04-06 18:47:14 -04001504 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001505 * @phba: pointer to lpfc hba data structure.
1506 * @mdp: pointer to the data structure to hold the derived model name.
1507 * @descp: pointer to the data structure to hold the derived description.
1508 *
1509 * This routine retrieves HBA's description based on its registered PCI device
1510 * ID. The @descp passed into this function points to an array of 256 chars. It
1511 * shall be returned with the model name, maximum speed, and the host bus type.
1512 * The @mdp passed into this function points to an array of 80 chars. When the
1513 * function returns, the @mdp will be filled with the model name.
1514 **/
dea31012005-04-17 16:05:31 -05001515static void
James Smart2e0fef82007-06-17 19:56:36 -05001516lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001517{
1518 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001519 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001520 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001521 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001522 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001523 struct {
1524 char * name;
1525 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001526 char * bus;
James Smart18a3b592006-12-02 13:35:08 -05001527 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001528
1529 if (mdp && mdp[0] != '\0'
1530 && descp && descp[0] != '\0')
1531 return;
1532
1533 if (phba->lmt & LMT_10Gb)
1534 max_speed = 10;
1535 else if (phba->lmt & LMT_8Gb)
1536 max_speed = 8;
1537 else if (phba->lmt & LMT_4Gb)
1538 max_speed = 4;
1539 else if (phba->lmt & LMT_2Gb)
1540 max_speed = 2;
1541 else
1542 max_speed = 1;
dea31012005-04-17 16:05:31 -05001543
1544 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001545
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001546 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001547 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -05001548 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001549 break;
dea31012005-04-17 16:05:31 -05001550 case PCI_DEVICE_ID_SUPERFLY:
1551 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -05001552 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001553 else
James Smart18a3b592006-12-02 13:35:08 -05001554 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001555 break;
1556 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -05001557 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001558 break;
1559 case PCI_DEVICE_ID_CENTAUR:
1560 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -05001561 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001562 else
James Smart18a3b592006-12-02 13:35:08 -05001563 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001564 break;
1565 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -05001566 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001567 break;
1568 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -05001569 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001570 break;
1571 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -05001572 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001573 break;
1574 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -05001575 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001576 break;
1577 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -05001578 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001579 break;
1580 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -05001581 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001582 break;
1583 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -05001584 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001585 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001586 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001587 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001588 break;
1589 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001590 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001591 break;
1592 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -05001593 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001594 break;
1595 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001596 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001597 break;
1598 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001599 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001600 break;
dea31012005-04-17 16:05:31 -05001601 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -05001602 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001603 break;
1604 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -05001605 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001606 break;
1607 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -05001608 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001609 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001610 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001611 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001612 break;
1613 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta257bf92009-04-06 18:48:10 -04001614 m = (typeof(m)){"LP2105", max_speed, "PCIe"};
1615 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001616 break;
dea31012005-04-17 16:05:31 -05001617 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -05001618 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001619 break;
1620 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -05001621 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001622 break;
1623 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -05001624 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001625 break;
1626 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -05001627 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001628 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001629 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -05001630 m = (typeof(m)){"LP11000-S", max_speed,
1631 "PCI-X2"};
1632 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001633 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -05001634 m = (typeof(m)){"LPe11000-S", max_speed,
1635 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001636 break;
James Smartb87eab32007-04-25 09:53:28 -04001637 case PCI_DEVICE_ID_SAT:
1638 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1639 break;
1640 case PCI_DEVICE_ID_SAT_MID:
1641 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1642 break;
1643 case PCI_DEVICE_ID_SAT_SMB:
1644 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1645 break;
1646 case PCI_DEVICE_ID_SAT_DCSP:
1647 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1648 break;
1649 case PCI_DEVICE_ID_SAT_SCSP:
1650 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1651 break;
1652 case PCI_DEVICE_ID_SAT_S:
1653 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1654 break;
James Smart84774a42008-08-24 21:50:06 -04001655 case PCI_DEVICE_ID_HORNET:
1656 m = (typeof(m)){"LP21000", max_speed, "PCIe"};
1657 GE = 1;
1658 break;
1659 case PCI_DEVICE_ID_PROTEUS_VF:
1660 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1661 break;
1662 case PCI_DEVICE_ID_PROTEUS_PF:
1663 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1664 break;
1665 case PCI_DEVICE_ID_PROTEUS_S:
1666 m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
1667 break;
James Smartda0436e2009-05-22 14:51:39 -04001668 case PCI_DEVICE_ID_TIGERSHARK:
1669 oneConnect = 1;
1670 m = (typeof(m)) {"OCe10100-F", max_speed, "PCIe"};
1671 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001672 default:
Randy Dunlap041976f2006-06-25 01:58:51 -07001673 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001674 break;
dea31012005-04-17 16:05:31 -05001675 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001676
1677 if (mdp && mdp[0] == '\0')
1678 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001679 /* oneConnect hba requires special processing, they are all initiators
1680 * and we put the port number on the end
1681 */
1682 if (descp && descp[0] == '\0') {
1683 if (oneConnect)
1684 snprintf(descp, 255,
1685 "Emulex OneConnect %s, FCoE Initiator, Port %s",
1686 m.name,
1687 phba->Port);
1688 else
1689 snprintf(descp, 255,
1690 "Emulex %s %d%s %s %s",
1691 m.name, m.max_speed,
1692 (GE) ? "GE" : "Gb",
1693 m.bus,
1694 (GE) ? "FCoE Adapter" :
1695 "Fibre Channel Adapter");
1696 }
dea31012005-04-17 16:05:31 -05001697}
1698
James Smarte59058c2008-08-24 21:49:00 -04001699/**
James Smart3621a712009-04-06 18:47:14 -04001700 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001701 * @phba: pointer to lpfc hba data structure.
1702 * @pring: pointer to a IOCB ring.
1703 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1704 *
1705 * This routine posts a given number of IOCBs with the associated DMA buffer
1706 * descriptors specified by the cnt argument to the given IOCB ring.
1707 *
1708 * Return codes
1709 * The number of IOCBs NOT able to be posted to the IOCB ring.
1710 **/
dea31012005-04-17 16:05:31 -05001711int
James Smart495a7142008-06-14 22:52:59 -04001712lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001713{
1714 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001715 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001716 struct lpfc_dmabuf *mp1, *mp2;
1717
1718 cnt += pring->missbufcnt;
1719
1720 /* While there are buffers to post */
1721 while (cnt > 0) {
1722 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001723 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001724 if (iocb == NULL) {
1725 pring->missbufcnt = cnt;
1726 return cnt;
1727 }
dea31012005-04-17 16:05:31 -05001728 icmd = &iocb->iocb;
1729
1730 /* 2 buffers can be posted per command */
1731 /* Allocate buffer to post */
1732 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1733 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001734 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1735 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001736 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001737 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001738 pring->missbufcnt = cnt;
1739 return cnt;
1740 }
1741
1742 INIT_LIST_HEAD(&mp1->list);
1743 /* Allocate buffer to post */
1744 if (cnt > 1) {
1745 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1746 if (mp2)
1747 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1748 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001749 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001750 kfree(mp2);
dea31012005-04-17 16:05:31 -05001751 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1752 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001753 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001754 pring->missbufcnt = cnt;
1755 return cnt;
1756 }
1757
1758 INIT_LIST_HEAD(&mp2->list);
1759 } else {
1760 mp2 = NULL;
1761 }
1762
1763 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1764 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1765 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1766 icmd->ulpBdeCount = 1;
1767 cnt--;
1768 if (mp2) {
1769 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1770 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1771 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1772 cnt--;
1773 icmd->ulpBdeCount = 2;
1774 }
1775
1776 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1777 icmd->ulpLe = 1;
1778
James Smart3772a992009-05-22 14:50:54 -04001779 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1780 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001781 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1782 kfree(mp1);
1783 cnt++;
1784 if (mp2) {
1785 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1786 kfree(mp2);
1787 cnt++;
1788 }
James Bottomley604a3e32005-10-29 10:28:33 -05001789 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001790 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001791 return cnt;
1792 }
dea31012005-04-17 16:05:31 -05001793 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001794 if (mp2)
dea31012005-04-17 16:05:31 -05001795 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001796 }
1797 pring->missbufcnt = 0;
1798 return 0;
1799}
1800
James Smarte59058c2008-08-24 21:49:00 -04001801/**
James Smart3621a712009-04-06 18:47:14 -04001802 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001803 * @phba: pointer to lpfc hba data structure.
1804 *
1805 * This routine posts initial receive IOCB buffers to the ELS ring. The
1806 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1807 * set to 64 IOCBs.
1808 *
1809 * Return codes
1810 * 0 - success (currently always success)
1811 **/
dea31012005-04-17 16:05:31 -05001812static int
James Smart2e0fef82007-06-17 19:56:36 -05001813lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001814{
1815 struct lpfc_sli *psli = &phba->sli;
1816
1817 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001818 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001819 /* Ring 2 - FCP no buffers needed */
1820
1821 return 0;
1822}
1823
1824#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1825
James Smarte59058c2008-08-24 21:49:00 -04001826/**
James Smart3621a712009-04-06 18:47:14 -04001827 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001828 * @HashResultPointer: pointer to an array as hash table.
1829 *
1830 * This routine sets up the initial values to the array of hash table entries
1831 * for the LC HBAs.
1832 **/
dea31012005-04-17 16:05:31 -05001833static void
1834lpfc_sha_init(uint32_t * HashResultPointer)
1835{
1836 HashResultPointer[0] = 0x67452301;
1837 HashResultPointer[1] = 0xEFCDAB89;
1838 HashResultPointer[2] = 0x98BADCFE;
1839 HashResultPointer[3] = 0x10325476;
1840 HashResultPointer[4] = 0xC3D2E1F0;
1841}
1842
James Smarte59058c2008-08-24 21:49:00 -04001843/**
James Smart3621a712009-04-06 18:47:14 -04001844 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001845 * @HashResultPointer: pointer to an initial/result hash table.
1846 * @HashWorkingPointer: pointer to an working hash table.
1847 *
1848 * This routine iterates an initial hash table pointed by @HashResultPointer
1849 * with the values from the working hash table pointeed by @HashWorkingPointer.
1850 * The results are putting back to the initial hash table, returned through
1851 * the @HashResultPointer as the result hash table.
1852 **/
dea31012005-04-17 16:05:31 -05001853static void
1854lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1855{
1856 int t;
1857 uint32_t TEMP;
1858 uint32_t A, B, C, D, E;
1859 t = 16;
1860 do {
1861 HashWorkingPointer[t] =
1862 S(1,
1863 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1864 8] ^
1865 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1866 } while (++t <= 79);
1867 t = 0;
1868 A = HashResultPointer[0];
1869 B = HashResultPointer[1];
1870 C = HashResultPointer[2];
1871 D = HashResultPointer[3];
1872 E = HashResultPointer[4];
1873
1874 do {
1875 if (t < 20) {
1876 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1877 } else if (t < 40) {
1878 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1879 } else if (t < 60) {
1880 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1881 } else {
1882 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1883 }
1884 TEMP += S(5, A) + E + HashWorkingPointer[t];
1885 E = D;
1886 D = C;
1887 C = S(30, B);
1888 B = A;
1889 A = TEMP;
1890 } while (++t <= 79);
1891
1892 HashResultPointer[0] += A;
1893 HashResultPointer[1] += B;
1894 HashResultPointer[2] += C;
1895 HashResultPointer[3] += D;
1896 HashResultPointer[4] += E;
1897
1898}
1899
James Smarte59058c2008-08-24 21:49:00 -04001900/**
James Smart3621a712009-04-06 18:47:14 -04001901 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04001902 * @RandomChallenge: pointer to the entry of host challenge random number array.
1903 * @HashWorking: pointer to the entry of the working hash array.
1904 *
1905 * This routine calculates the working hash array referred by @HashWorking
1906 * from the challenge random numbers associated with the host, referred by
1907 * @RandomChallenge. The result is put into the entry of the working hash
1908 * array and returned by reference through @HashWorking.
1909 **/
dea31012005-04-17 16:05:31 -05001910static void
1911lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1912{
1913 *HashWorking = (*RandomChallenge ^ *HashWorking);
1914}
1915
James Smarte59058c2008-08-24 21:49:00 -04001916/**
James Smart3621a712009-04-06 18:47:14 -04001917 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04001918 * @phba: pointer to lpfc hba data structure.
1919 * @hbainit: pointer to an array of unsigned 32-bit integers.
1920 *
1921 * This routine performs the special handling for LC HBA initialization.
1922 **/
dea31012005-04-17 16:05:31 -05001923void
1924lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1925{
1926 int t;
1927 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001928 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001929
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001930 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001931 if (!HashWorking)
1932 return;
1933
dea31012005-04-17 16:05:31 -05001934 HashWorking[0] = HashWorking[78] = *pwwnn++;
1935 HashWorking[1] = HashWorking[79] = *pwwnn;
1936
1937 for (t = 0; t < 7; t++)
1938 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1939
1940 lpfc_sha_init(hbainit);
1941 lpfc_sha_iterate(hbainit, HashWorking);
1942 kfree(HashWorking);
1943}
1944
James Smarte59058c2008-08-24 21:49:00 -04001945/**
James Smart3621a712009-04-06 18:47:14 -04001946 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04001947 * @vport: pointer to a virtual N_Port data structure.
1948 *
1949 * This routine performs the necessary cleanups before deleting the @vport.
1950 * It invokes the discovery state machine to perform necessary state
1951 * transitions and to release the ndlps associated with the @vport. Note,
1952 * the physical port is treated as @vport 0.
1953 **/
James Smart87af33f2007-10-27 13:37:43 -04001954void
James Smart2e0fef82007-06-17 19:56:36 -05001955lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001956{
James Smart87af33f2007-10-27 13:37:43 -04001957 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001958 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04001959 int i = 0;
dea31012005-04-17 16:05:31 -05001960
James Smart87af33f2007-10-27 13:37:43 -04001961 if (phba->link_state > LPFC_LINK_DOWN)
1962 lpfc_port_link_failure(vport);
1963
1964 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001965 if (!NLP_CHK_NODE_ACT(ndlp)) {
1966 ndlp = lpfc_enable_node(vport, ndlp,
1967 NLP_STE_UNUSED_NODE);
1968 if (!ndlp)
1969 continue;
1970 spin_lock_irq(&phba->ndlp_lock);
1971 NLP_SET_FREE_REQ(ndlp);
1972 spin_unlock_irq(&phba->ndlp_lock);
1973 /* Trigger the release of the ndlp memory */
1974 lpfc_nlp_put(ndlp);
1975 continue;
1976 }
1977 spin_lock_irq(&phba->ndlp_lock);
1978 if (NLP_CHK_FREE_REQ(ndlp)) {
1979 /* The ndlp should not be in memory free mode already */
1980 spin_unlock_irq(&phba->ndlp_lock);
1981 continue;
1982 } else
1983 /* Indicate request for freeing ndlp memory */
1984 NLP_SET_FREE_REQ(ndlp);
1985 spin_unlock_irq(&phba->ndlp_lock);
1986
James Smart58da1ff2008-04-07 10:15:56 -04001987 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1988 ndlp->nlp_DID == Fabric_DID) {
1989 /* Just free up ndlp with Fabric_DID for vports */
1990 lpfc_nlp_put(ndlp);
1991 continue;
1992 }
1993
James Smart87af33f2007-10-27 13:37:43 -04001994 if (ndlp->nlp_type & NLP_FABRIC)
1995 lpfc_disc_state_machine(vport, ndlp, NULL,
1996 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05001997
James Smart87af33f2007-10-27 13:37:43 -04001998 lpfc_disc_state_machine(vport, ndlp, NULL,
1999 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002000
James Smart87af33f2007-10-27 13:37:43 -04002001 }
2002
James Smarta8adb832007-10-27 13:37:53 -04002003 /* At this point, ALL ndlp's should be gone
2004 * because of the previous NLP_EVT_DEVICE_RM.
2005 * Lets wait for this to happen, if needed.
2006 */
James Smart87af33f2007-10-27 13:37:43 -04002007 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002008 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002009 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002010 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002011 list_for_each_entry_safe(ndlp, next_ndlp,
2012 &vport->fc_nodes, nlp_listp) {
2013 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2014 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002015 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002016 "usgmap:x%x refcnt:%d\n",
2017 ndlp->nlp_DID, (void *)ndlp,
2018 ndlp->nlp_usg_map,
2019 atomic_read(
2020 &ndlp->kref.refcount));
2021 }
James Smarta8adb832007-10-27 13:37:53 -04002022 break;
James Smart87af33f2007-10-27 13:37:43 -04002023 }
James Smarta8adb832007-10-27 13:37:53 -04002024
2025 /* Wait for any activity on ndlps to settle */
2026 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002027 }
dea31012005-04-17 16:05:31 -05002028}
2029
James Smarte59058c2008-08-24 21:49:00 -04002030/**
James Smart3621a712009-04-06 18:47:14 -04002031 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002032 * @vport: pointer to a virtual N_Port data structure.
2033 *
2034 * This routine stops all the timers associated with a @vport. This function
2035 * is invoked before disabling or deleting a @vport. Note that the physical
2036 * port is treated as @vport 0.
2037 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002038void
2039lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002040{
James Smart92d7f7b2007-06-17 19:56:38 -05002041 del_timer_sync(&vport->els_tmofunc);
2042 del_timer_sync(&vport->fc_fdmitmo);
2043 lpfc_can_disctmo(vport);
2044 return;
dea31012005-04-17 16:05:31 -05002045}
2046
James Smarte59058c2008-08-24 21:49:00 -04002047/**
James Smart3772a992009-05-22 14:50:54 -04002048 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002049 * @phba: pointer to lpfc hba data structure.
2050 *
2051 * This routine stops all the timers associated with a HBA. This function is
2052 * invoked before either putting a HBA offline or unloading the driver.
2053 **/
James Smart3772a992009-05-22 14:50:54 -04002054void
2055lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002056{
James Smart51ef4c22007-08-02 11:10:31 -04002057 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002058 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002059 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002060 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002061 del_timer_sync(&phba->hb_tmofunc);
2062 phba->hb_outstanding = 0;
2063
2064 switch (phba->pci_dev_grp) {
2065 case LPFC_PCI_DEV_LP:
2066 /* Stop any LightPulse device specific driver timers */
2067 del_timer_sync(&phba->fcp_poll_timer);
2068 break;
2069 case LPFC_PCI_DEV_OC:
2070 /* Stop any OneConnect device sepcific driver timers */
2071 break;
2072 default:
2073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2074 "0297 Invalid device group (x%x)\n",
2075 phba->pci_dev_grp);
2076 break;
2077 }
James Smart2e0fef82007-06-17 19:56:36 -05002078 return;
dea31012005-04-17 16:05:31 -05002079}
2080
James Smarte59058c2008-08-24 21:49:00 -04002081/**
James Smart3621a712009-04-06 18:47:14 -04002082 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002083 * @phba: pointer to lpfc hba data structure.
2084 *
2085 * This routine marks a HBA's management interface as blocked. Once the HBA's
2086 * management interface is marked as blocked, all the user space access to
2087 * the HBA, whether they are from sysfs interface or libdfc interface will
2088 * all be blocked. The HBA is set to block the management interface when the
2089 * driver prepares the HBA interface for online or offline.
2090 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002091static void
2092lpfc_block_mgmt_io(struct lpfc_hba * phba)
2093{
2094 unsigned long iflag;
2095
2096 spin_lock_irqsave(&phba->hbalock, iflag);
2097 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2098 spin_unlock_irqrestore(&phba->hbalock, iflag);
2099}
2100
James Smarte59058c2008-08-24 21:49:00 -04002101/**
James Smart3621a712009-04-06 18:47:14 -04002102 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002103 * @phba: pointer to lpfc hba data structure.
2104 *
2105 * This routine initializes the HBA and brings a HBA online. During this
2106 * process, the management interface is blocked to prevent user space access
2107 * to the HBA interfering with the driver initialization.
2108 *
2109 * Return codes
2110 * 0 - successful
2111 * 1 - failed
2112 **/
dea31012005-04-17 16:05:31 -05002113int
James Smart2e0fef82007-06-17 19:56:36 -05002114lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002115{
Julia Lawall372bd282008-12-16 16:15:08 +01002116 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002117 struct lpfc_vport **vports;
2118 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002119
dea31012005-04-17 16:05:31 -05002120 if (!phba)
2121 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002122 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002123
James Smart2e0fef82007-06-17 19:56:36 -05002124 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002125 return 0;
2126
James Smarted957682007-06-17 19:56:37 -05002127 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002128 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002129
James Smart46fa3112007-04-25 09:51:45 -04002130 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002131
James Smart46fa3112007-04-25 09:51:45 -04002132 if (!lpfc_sli_queue_setup(phba)) {
2133 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002134 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002135 }
2136
James Smartda0436e2009-05-22 14:51:39 -04002137 if (phba->sli_rev == LPFC_SLI_REV4) {
2138 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2139 lpfc_unblock_mgmt_io(phba);
2140 return 1;
2141 }
2142 } else {
2143 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2144 lpfc_unblock_mgmt_io(phba);
2145 return 1;
2146 }
James Smart46fa3112007-04-25 09:51:45 -04002147 }
dea31012005-04-17 16:05:31 -05002148
James Smart549e55c2007-08-02 11:09:51 -04002149 vports = lpfc_create_vport_work_array(phba);
2150 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002151 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002152 struct Scsi_Host *shost;
2153 shost = lpfc_shost_from_vport(vports[i]);
2154 spin_lock_irq(shost->host_lock);
2155 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2156 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2157 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002158 if (phba->sli_rev == LPFC_SLI_REV4)
2159 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002160 spin_unlock_irq(shost->host_lock);
2161 }
James Smart09372822008-01-11 01:52:54 -05002162 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002163
James Smart46fa3112007-04-25 09:51:45 -04002164 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002165 return 0;
2166}
2167
James Smarte59058c2008-08-24 21:49:00 -04002168/**
James Smart3621a712009-04-06 18:47:14 -04002169 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002170 * @phba: pointer to lpfc hba data structure.
2171 *
2172 * This routine marks a HBA's management interface as not blocked. Once the
2173 * HBA's management interface is marked as not blocked, all the user space
2174 * access to the HBA, whether they are from sysfs interface or libdfc
2175 * interface will be allowed. The HBA is set to block the management interface
2176 * when the driver prepares the HBA interface for online or offline and then
2177 * set to unblock the management interface afterwards.
2178 **/
James Smart46fa3112007-04-25 09:51:45 -04002179void
James Smart46fa3112007-04-25 09:51:45 -04002180lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2181{
2182 unsigned long iflag;
2183
James Smart2e0fef82007-06-17 19:56:36 -05002184 spin_lock_irqsave(&phba->hbalock, iflag);
2185 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2186 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002187}
2188
James Smarte59058c2008-08-24 21:49:00 -04002189/**
James Smart3621a712009-04-06 18:47:14 -04002190 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002191 * @phba: pointer to lpfc hba data structure.
2192 *
2193 * This routine is invoked to prepare a HBA to be brought offline. It performs
2194 * unregistration login to all the nodes on all vports and flushes the mailbox
2195 * queue to make it ready to be brought offline.
2196 **/
James Smart46fa3112007-04-25 09:51:45 -04002197void
2198lpfc_offline_prep(struct lpfc_hba * phba)
2199{
James Smart2e0fef82007-06-17 19:56:36 -05002200 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002201 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002202 struct lpfc_vport **vports;
2203 int i;
dea31012005-04-17 16:05:31 -05002204
James Smart2e0fef82007-06-17 19:56:36 -05002205 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002206 return;
dea31012005-04-17 16:05:31 -05002207
James Smart46fa3112007-04-25 09:51:45 -04002208 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002209
2210 lpfc_linkdown(phba);
2211
James Smart87af33f2007-10-27 13:37:43 -04002212 /* Issue an unreg_login to all nodes on all vports */
2213 vports = lpfc_create_vport_work_array(phba);
2214 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002215 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart87af33f2007-10-27 13:37:43 -04002216 struct Scsi_Host *shost;
2217
James Smarta8adb832007-10-27 13:37:53 -04002218 if (vports[i]->load_flag & FC_UNLOADING)
2219 continue;
James Smartda0436e2009-05-22 14:51:39 -04002220 vports[i]->vfi_state &= ~LPFC_VFI_REGISTERED;
James Smart87af33f2007-10-27 13:37:43 -04002221 shost = lpfc_shost_from_vport(vports[i]);
2222 list_for_each_entry_safe(ndlp, next_ndlp,
2223 &vports[i]->fc_nodes,
2224 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002225 if (!NLP_CHK_NODE_ACT(ndlp))
2226 continue;
James Smart87af33f2007-10-27 13:37:43 -04002227 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2228 continue;
2229 if (ndlp->nlp_type & NLP_FABRIC) {
2230 lpfc_disc_state_machine(vports[i], ndlp,
2231 NULL, NLP_EVT_DEVICE_RECOVERY);
2232 lpfc_disc_state_machine(vports[i], ndlp,
2233 NULL, NLP_EVT_DEVICE_RM);
2234 }
2235 spin_lock_irq(shost->host_lock);
2236 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2237 spin_unlock_irq(shost->host_lock);
2238 lpfc_unreg_rpi(vports[i], ndlp);
2239 }
2240 }
2241 }
James Smart09372822008-01-11 01:52:54 -05002242 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002243
James Smart04c68492009-05-22 14:52:52 -04002244 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002245}
2246
James Smarte59058c2008-08-24 21:49:00 -04002247/**
James Smart3621a712009-04-06 18:47:14 -04002248 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002249 * @phba: pointer to lpfc hba data structure.
2250 *
2251 * This routine actually brings a HBA offline. It stops all the timers
2252 * associated with the HBA, brings down the SLI layer, and eventually
2253 * marks the HBA as in offline state for the upper layer protocol.
2254 **/
James Smart46fa3112007-04-25 09:51:45 -04002255void
James Smart2e0fef82007-06-17 19:56:36 -05002256lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002257{
James Smart549e55c2007-08-02 11:09:51 -04002258 struct Scsi_Host *shost;
2259 struct lpfc_vport **vports;
2260 int i;
James Smart46fa3112007-04-25 09:51:45 -04002261
James Smart549e55c2007-08-02 11:09:51 -04002262 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002263 return;
James Smart688a8862006-07-06 15:49:56 -04002264
James Smartda0436e2009-05-22 14:51:39 -04002265 /* stop port and all timers associated with this hba */
2266 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002267 vports = lpfc_create_vport_work_array(phba);
2268 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002269 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002270 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002271 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002272 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002273 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002274 /* Bring down the SLI Layer and cleanup. The HBA is offline
2275 now. */
2276 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002277 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002278 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002279 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002280 vports = lpfc_create_vport_work_array(phba);
2281 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002282 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002283 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002284 spin_lock_irq(shost->host_lock);
2285 vports[i]->work_port_events = 0;
2286 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2287 spin_unlock_irq(shost->host_lock);
2288 }
James Smart09372822008-01-11 01:52:54 -05002289 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002290}
2291
James Smarte59058c2008-08-24 21:49:00 -04002292/**
James Smart3621a712009-04-06 18:47:14 -04002293 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002294 * @phba: pointer to lpfc hba data structure.
2295 *
2296 * This routine is to free all the SCSI buffers and IOCBs from the driver
2297 * list back to kernel. It is called from lpfc_pci_remove_one to free
2298 * the internal resources before the device is removed from the system.
2299 *
2300 * Return codes
2301 * 0 - successful (for now, it always returns 0)
2302 **/
dea31012005-04-17 16:05:31 -05002303static int
James Smart2e0fef82007-06-17 19:56:36 -05002304lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002305{
2306 struct lpfc_scsi_buf *sb, *sb_next;
2307 struct lpfc_iocbq *io, *io_next;
2308
James Smart2e0fef82007-06-17 19:56:36 -05002309 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002310 /* Release all the lpfc_scsi_bufs maintained by this host. */
2311 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2312 list_del(&sb->list);
2313 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002314 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002315 kfree(sb);
2316 phba->total_scsi_bufs--;
2317 }
2318
2319 /* Release all the lpfc_iocbq entries maintained by this host. */
2320 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2321 list_del(&io->list);
2322 kfree(io);
2323 phba->total_iocbq_bufs--;
2324 }
2325
James Smart2e0fef82007-06-17 19:56:36 -05002326 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002327
2328 return 0;
2329}
2330
James Smarte59058c2008-08-24 21:49:00 -04002331/**
James Smart3621a712009-04-06 18:47:14 -04002332 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002333 * @phba: pointer to lpfc hba data structure.
2334 * @instance: a unique integer ID to this FC port.
2335 * @dev: pointer to the device data structure.
2336 *
2337 * This routine creates a FC port for the upper layer protocol. The FC port
2338 * can be created on top of either a physical port or a virtual port provided
2339 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2340 * and associates the FC port created before adding the shost into the SCSI
2341 * layer.
2342 *
2343 * Return codes
2344 * @vport - pointer to the virtual N_Port data structure.
2345 * NULL - port create failed.
2346 **/
James Smart2e0fef82007-06-17 19:56:36 -05002347struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002348lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002349{
James Smart2e0fef82007-06-17 19:56:36 -05002350 struct lpfc_vport *vport;
2351 struct Scsi_Host *shost;
2352 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002353
James Smart3de2a652007-08-02 11:09:59 -04002354 if (dev != &phba->pcidev->dev)
2355 shost = scsi_host_alloc(&lpfc_vport_template,
2356 sizeof(struct lpfc_vport));
2357 else
2358 shost = scsi_host_alloc(&lpfc_template,
2359 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002360 if (!shost)
2361 goto out;
James Smart47a86172007-04-25 09:53:22 -04002362
James Smart2e0fef82007-06-17 19:56:36 -05002363 vport = (struct lpfc_vport *) shost->hostdata;
2364 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002365 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002366 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002367 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002368
James Smart3de2a652007-08-02 11:09:59 -04002369 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002370 shost->unique_id = instance;
2371 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002372 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002373 shost->this_id = -1;
2374 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002375 if (phba->sli_rev == LPFC_SLI_REV4) {
2376 shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2377 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2378 }
James Smart81301a92008-12-04 22:39:46 -05002379
James Smart47a86172007-04-25 09:53:22 -04002380 /*
James Smart2e0fef82007-06-17 19:56:36 -05002381 * Set initial can_queue value since 0 is no longer supported and
2382 * scsi_add_host will fail. This will be adjusted later based on the
2383 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002384 */
James Smart2e0fef82007-06-17 19:56:36 -05002385 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002386 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002387 shost->transportt = lpfc_vport_transport_template;
2388 vport->port_type = LPFC_NPIV_PORT;
2389 } else {
2390 shost->transportt = lpfc_transport_template;
2391 vport->port_type = LPFC_PHYSICAL_PORT;
2392 }
James Smart47a86172007-04-25 09:53:22 -04002393
James Smart2e0fef82007-06-17 19:56:36 -05002394 /* Initialize all internally managed lists. */
2395 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002396 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002397 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002398
James Smart2e0fef82007-06-17 19:56:36 -05002399 init_timer(&vport->fc_disctmo);
2400 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002401 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002402
James Smart2e0fef82007-06-17 19:56:36 -05002403 init_timer(&vport->fc_fdmitmo);
2404 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002405 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002406
James Smart2e0fef82007-06-17 19:56:36 -05002407 init_timer(&vport->els_tmofunc);
2408 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002409 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002410
James Bottomleyd139b9b2009-11-05 13:33:12 -06002411 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002412 if (error)
2413 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002414
James Smart549e55c2007-08-02 11:09:51 -04002415 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002416 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002417 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002418 return vport;
James Smart47a86172007-04-25 09:53:22 -04002419
James Smart2e0fef82007-06-17 19:56:36 -05002420out_put_shost:
2421 scsi_host_put(shost);
2422out:
2423 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002424}
2425
James Smarte59058c2008-08-24 21:49:00 -04002426/**
James Smart3621a712009-04-06 18:47:14 -04002427 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002428 * @vport: pointer to an lpfc virtual N_Port data structure.
2429 *
2430 * This routine destroys a FC port from the upper layer protocol. All the
2431 * resources associated with the port are released.
2432 **/
James Smart2e0fef82007-06-17 19:56:36 -05002433void
2434destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002435{
James Smart92d7f7b2007-06-17 19:56:38 -05002436 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2437 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002438
James Smart858c9f62007-06-17 19:56:39 -05002439 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002440 fc_remove_host(shost);
2441 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002442
James Smart92d7f7b2007-06-17 19:56:38 -05002443 spin_lock_irq(&phba->hbalock);
2444 list_del_init(&vport->listentry);
2445 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002446
James Smart92d7f7b2007-06-17 19:56:38 -05002447 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002448 return;
James Smart47a86172007-04-25 09:53:22 -04002449}
2450
James Smarte59058c2008-08-24 21:49:00 -04002451/**
James Smart3621a712009-04-06 18:47:14 -04002452 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002453 *
2454 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2455 * uses the kernel idr facility to perform the task.
2456 *
2457 * Return codes:
2458 * instance - a unique integer ID allocated as the new instance.
2459 * -1 - lpfc get instance failed.
2460 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002461int
2462lpfc_get_instance(void)
2463{
2464 int instance = 0;
2465
2466 /* Assign an unused number */
2467 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2468 return -1;
2469 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2470 return -1;
2471 return instance;
2472}
2473
James Smarte59058c2008-08-24 21:49:00 -04002474/**
James Smart3621a712009-04-06 18:47:14 -04002475 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002476 * @shost: pointer to SCSI host data structure.
2477 * @time: elapsed time of the scan in jiffies.
2478 *
2479 * This routine is called by the SCSI layer with a SCSI host to determine
2480 * whether the scan host is finished.
2481 *
2482 * Note: there is no scan_start function as adapter initialization will have
2483 * asynchronously kicked off the link initialization.
2484 *
2485 * Return codes
2486 * 0 - SCSI host scan is not over yet.
2487 * 1 - SCSI host scan is over.
2488 **/
James Smart47a86172007-04-25 09:53:22 -04002489int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2490{
James Smart2e0fef82007-06-17 19:56:36 -05002491 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2492 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002493 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002494
James Smart858c9f62007-06-17 19:56:39 -05002495 spin_lock_irq(shost->host_lock);
2496
James Smart51ef4c22007-08-02 11:10:31 -04002497 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002498 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002499 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002500 }
James Smart2e0fef82007-06-17 19:56:36 -05002501 if (time >= 30 * HZ) {
2502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002503 "0461 Scanning longer than 30 "
2504 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002505 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002506 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002507 }
2508 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2509 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002510 "0465 Link down longer than 15 "
2511 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002512 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002513 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002514 }
2515
James Smart2e0fef82007-06-17 19:56:36 -05002516 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002517 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002518 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002519 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002520 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002521 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002522 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002523 goto finished;
2524
2525 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002526
2527finished:
James Smart858c9f62007-06-17 19:56:39 -05002528 spin_unlock_irq(shost->host_lock);
2529 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002530}
2531
James Smarte59058c2008-08-24 21:49:00 -04002532/**
James Smart3621a712009-04-06 18:47:14 -04002533 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002534 * @shost: pointer to SCSI host data structure.
2535 *
2536 * This routine initializes a given SCSI host attributes on a FC port. The
2537 * SCSI host can be either on top of a physical port or a virtual port.
2538 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002539void lpfc_host_attrib_init(struct Scsi_Host *shost)
2540{
2541 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2542 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002543 /*
James Smart2e0fef82007-06-17 19:56:36 -05002544 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002545 */
2546
James Smart2e0fef82007-06-17 19:56:36 -05002547 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2548 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002549 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2550
2551 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002552 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002553 fc_host_supported_fc4s(shost)[2] = 1;
2554 fc_host_supported_fc4s(shost)[7] = 1;
2555
James Smart92d7f7b2007-06-17 19:56:38 -05002556 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2557 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002558
2559 fc_host_supported_speeds(shost) = 0;
2560 if (phba->lmt & LMT_10Gb)
2561 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002562 if (phba->lmt & LMT_8Gb)
2563 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002564 if (phba->lmt & LMT_4Gb)
2565 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2566 if (phba->lmt & LMT_2Gb)
2567 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2568 if (phba->lmt & LMT_1Gb)
2569 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2570
2571 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002572 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2573 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002574
2575 /* This value is also unchanging */
2576 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002577 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002578 fc_host_active_fc4s(shost)[2] = 1;
2579 fc_host_active_fc4s(shost)[7] = 1;
2580
James Smart92d7f7b2007-06-17 19:56:38 -05002581 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002582 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002583 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002584 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002585}
dea31012005-04-17 16:05:31 -05002586
James Smarte59058c2008-08-24 21:49:00 -04002587/**
James Smartda0436e2009-05-22 14:51:39 -04002588 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002589 * @phba: pointer to lpfc hba data structure.
2590 *
James Smartda0436e2009-05-22 14:51:39 -04002591 * This routine is invoked to stop an SLI3 device port, it stops the device
2592 * from generating interrupts and stops the device driver's timers for the
2593 * device.
James Smarte59058c2008-08-24 21:49:00 -04002594 **/
James Smartdb2378e2008-02-08 18:49:51 -05002595static void
James Smartda0436e2009-05-22 14:51:39 -04002596lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002597{
James Smartda0436e2009-05-22 14:51:39 -04002598 /* Clear all interrupt enable conditions */
2599 writel(0, phba->HCregaddr);
2600 readl(phba->HCregaddr); /* flush */
2601 /* Clear all pending interrupts */
2602 writel(0xffffffff, phba->HAregaddr);
2603 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002604
James Smartda0436e2009-05-22 14:51:39 -04002605 /* Reset some HBA SLI setup states */
2606 lpfc_stop_hba_timers(phba);
2607 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002608}
2609
James Smarte59058c2008-08-24 21:49:00 -04002610/**
James Smartda0436e2009-05-22 14:51:39 -04002611 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002612 * @phba: pointer to lpfc hba data structure.
2613 *
James Smartda0436e2009-05-22 14:51:39 -04002614 * This routine is invoked to stop an SLI4 device port, it stops the device
2615 * from generating interrupts and stops the device driver's timers for the
2616 * device.
2617 **/
2618static void
2619lpfc_stop_port_s4(struct lpfc_hba *phba)
2620{
2621 /* Reset some HBA SLI4 setup states */
2622 lpfc_stop_hba_timers(phba);
2623 phba->pport->work_port_events = 0;
2624 phba->sli4_hba.intr_enable = 0;
2625 /* Hard clear it for now, shall have more graceful way to wait later */
2626 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2627}
2628
2629/**
2630 * lpfc_stop_port - Wrapper function for stopping hba port
2631 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002632 *
James Smartda0436e2009-05-22 14:51:39 -04002633 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2634 * the API jump table function pointer from the lpfc_hba struct.
2635 **/
2636void
2637lpfc_stop_port(struct lpfc_hba *phba)
2638{
2639 phba->lpfc_stop_port(phba);
2640}
2641
2642/**
2643 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2644 * @phba: pointer to lpfc hba data structure.
2645 *
2646 * This routine is invoked to remove the driver default fcf record from
2647 * the port. This routine currently acts on FCF Index 0.
2648 *
2649 **/
2650void
2651lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2652{
2653 int rc = 0;
2654 LPFC_MBOXQ_t *mboxq;
2655 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2656 uint32_t mbox_tmo, req_len;
2657 uint32_t shdr_status, shdr_add_status;
2658
2659 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2660 if (!mboxq) {
2661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2662 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2663 return;
2664 }
2665
2666 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2667 sizeof(struct lpfc_sli4_cfg_mhdr);
2668 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2669 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2670 req_len, LPFC_SLI4_MBX_EMBED);
2671 /*
2672 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2673 * supports multiple FCF indices.
2674 */
2675 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2676 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2677 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2678 phba->fcf.fcf_indx);
2679
2680 if (!phba->sli4_hba.intr_enable)
2681 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2682 else {
2683 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2684 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2685 }
2686 /* The IOCTL status is embedded in the mailbox subheader. */
2687 shdr_status = bf_get(lpfc_mbox_hdr_status,
2688 &del_fcf_record->header.cfg_shdr.response);
2689 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2690 &del_fcf_record->header.cfg_shdr.response);
2691 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2693 "2516 DEL FCF of default FCF Index failed "
2694 "mbx status x%x, status x%x add_status x%x\n",
2695 rc, shdr_status, shdr_add_status);
2696 }
2697 if (rc != MBX_TIMEOUT)
2698 mempool_free(mboxq, phba->mbox_mem_pool);
2699}
2700
2701/**
2702 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2703 * @phba: pointer to lpfc hba data structure.
2704 * @acqe_link: pointer to the async link completion queue entry.
2705 *
2706 * This routine is to parse the SLI4 link-attention link fault code and
2707 * translate it into the base driver's read link attention mailbox command
2708 * status.
2709 *
2710 * Return: Link-attention status in terms of base driver's coding.
2711 **/
2712static uint16_t
2713lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2714 struct lpfc_acqe_link *acqe_link)
2715{
2716 uint16_t latt_fault;
2717
2718 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2719 case LPFC_ASYNC_LINK_FAULT_NONE:
2720 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2721 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2722 latt_fault = 0;
2723 break;
2724 default:
2725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2726 "0398 Invalid link fault code: x%x\n",
2727 bf_get(lpfc_acqe_link_fault, acqe_link));
2728 latt_fault = MBXERR_ERROR;
2729 break;
2730 }
2731 return latt_fault;
2732}
2733
2734/**
2735 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2736 * @phba: pointer to lpfc hba data structure.
2737 * @acqe_link: pointer to the async link completion queue entry.
2738 *
2739 * This routine is to parse the SLI4 link attention type and translate it
2740 * into the base driver's link attention type coding.
2741 *
2742 * Return: Link attention type in terms of base driver's coding.
2743 **/
2744static uint8_t
2745lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2746 struct lpfc_acqe_link *acqe_link)
2747{
2748 uint8_t att_type;
2749
2750 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2751 case LPFC_ASYNC_LINK_STATUS_DOWN:
2752 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2753 att_type = AT_LINK_DOWN;
2754 break;
2755 case LPFC_ASYNC_LINK_STATUS_UP:
2756 /* Ignore physical link up events - wait for logical link up */
2757 att_type = AT_RESERVED;
2758 break;
2759 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2760 att_type = AT_LINK_UP;
2761 break;
2762 default:
2763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2764 "0399 Invalid link attention type: x%x\n",
2765 bf_get(lpfc_acqe_link_status, acqe_link));
2766 att_type = AT_RESERVED;
2767 break;
2768 }
2769 return att_type;
2770}
2771
2772/**
2773 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2774 * @phba: pointer to lpfc hba data structure.
2775 * @acqe_link: pointer to the async link completion queue entry.
2776 *
2777 * This routine is to parse the SLI4 link-attention link speed and translate
2778 * it into the base driver's link-attention link speed coding.
2779 *
2780 * Return: Link-attention link speed in terms of base driver's coding.
2781 **/
2782static uint8_t
2783lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2784 struct lpfc_acqe_link *acqe_link)
2785{
2786 uint8_t link_speed;
2787
2788 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2789 case LPFC_ASYNC_LINK_SPEED_ZERO:
2790 link_speed = LA_UNKNW_LINK;
2791 break;
2792 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2793 link_speed = LA_UNKNW_LINK;
2794 break;
2795 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2796 link_speed = LA_UNKNW_LINK;
2797 break;
2798 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2799 link_speed = LA_1GHZ_LINK;
2800 break;
2801 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2802 link_speed = LA_10GHZ_LINK;
2803 break;
2804 default:
2805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2806 "0483 Invalid link-attention link speed: x%x\n",
2807 bf_get(lpfc_acqe_link_speed, acqe_link));
2808 link_speed = LA_UNKNW_LINK;
2809 break;
2810 }
2811 return link_speed;
2812}
2813
2814/**
2815 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2816 * @phba: pointer to lpfc hba data structure.
2817 * @acqe_link: pointer to the async link completion queue entry.
2818 *
2819 * This routine is to handle the SLI4 asynchronous link event.
2820 **/
2821static void
2822lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
2823 struct lpfc_acqe_link *acqe_link)
2824{
2825 struct lpfc_dmabuf *mp;
2826 LPFC_MBOXQ_t *pmb;
2827 MAILBOX_t *mb;
2828 READ_LA_VAR *la;
2829 uint8_t att_type;
2830
2831 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
2832 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
2833 return;
James Smart32b97932009-07-19 10:01:21 -04002834 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04002835 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2836 if (!pmb) {
2837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2838 "0395 The mboxq allocation failed\n");
2839 return;
2840 }
2841 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2842 if (!mp) {
2843 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2844 "0396 The lpfc_dmabuf allocation failed\n");
2845 goto out_free_pmb;
2846 }
2847 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2848 if (!mp->virt) {
2849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2850 "0397 The mbuf allocation failed\n");
2851 goto out_free_dmabuf;
2852 }
2853
2854 /* Cleanup any outstanding ELS commands */
2855 lpfc_els_flush_all_cmd(phba);
2856
2857 /* Block ELS IOCBs until we have done process link event */
2858 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2859
2860 /* Update link event statistics */
2861 phba->sli.slistat.link_event++;
2862
2863 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
2864 lpfc_read_la(phba, pmb, mp);
2865 pmb->vport = phba->pport;
2866
2867 /* Parse and translate status field */
2868 mb = &pmb->u.mb;
2869 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
2870
2871 /* Parse and translate link attention fields */
2872 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2873 la->eventTag = acqe_link->event_tag;
2874 la->attType = att_type;
2875 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
2876
2877 /* Fake the the following irrelvant fields */
2878 la->topology = TOPOLOGY_PT_PT;
2879 la->granted_AL_PA = 0;
2880 la->il = 0;
2881 la->pb = 0;
2882 la->fa = 0;
2883 la->mm = 0;
2884
2885 /* Keep the link status for extra SLI4 state machine reference */
2886 phba->sli4_hba.link_state.speed =
2887 bf_get(lpfc_acqe_link_speed, acqe_link);
2888 phba->sli4_hba.link_state.duplex =
2889 bf_get(lpfc_acqe_link_duplex, acqe_link);
2890 phba->sli4_hba.link_state.status =
2891 bf_get(lpfc_acqe_link_status, acqe_link);
2892 phba->sli4_hba.link_state.physical =
2893 bf_get(lpfc_acqe_link_physical, acqe_link);
2894 phba->sli4_hba.link_state.fault =
2895 bf_get(lpfc_acqe_link_fault, acqe_link);
2896
2897 /* Invoke the lpfc_handle_latt mailbox command callback function */
2898 lpfc_mbx_cmpl_read_la(phba, pmb);
2899
2900 return;
2901
2902out_free_dmabuf:
2903 kfree(mp);
2904out_free_pmb:
2905 mempool_free(pmb, phba->mbox_mem_pool);
2906}
2907
2908/**
2909 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
2910 * @phba: pointer to lpfc hba data structure.
2911 * @acqe_link: pointer to the async fcoe completion queue entry.
2912 *
2913 * This routine is to handle the SLI4 asynchronous fcoe event.
2914 **/
2915static void
2916lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
2917 struct lpfc_acqe_fcoe *acqe_fcoe)
2918{
2919 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
2920 int rc;
2921
James Smart4d9ab992009-10-02 15:16:39 -04002922 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04002923 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04002924 switch (event_type) {
2925 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
2926 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smarte4e74272009-07-19 10:01:38 -04002927 "2546 New FCF found index 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04002928 acqe_fcoe->fcf_index,
2929 acqe_fcoe->event_tag);
2930 /*
James Smart32b97932009-07-19 10:01:21 -04002931 * If the current FCF is in discovered state, or
2932 * FCF discovery is in progress do nothing.
James Smartda0436e2009-05-22 14:51:39 -04002933 */
2934 spin_lock_irq(&phba->hbalock);
James Smart32b97932009-07-19 10:01:21 -04002935 if ((phba->fcf.fcf_flag & FCF_DISCOVERED) ||
2936 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
James Smartda0436e2009-05-22 14:51:39 -04002937 spin_unlock_irq(&phba->hbalock);
2938 break;
2939 }
2940 spin_unlock_irq(&phba->hbalock);
2941
2942 /* Read the FCF table and re-discover SAN. */
2943 rc = lpfc_sli4_read_fcf_record(phba,
2944 LPFC_FCOE_FCF_GET_FIRST);
2945 if (rc)
2946 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2947 "2547 Read FCF record failed 0x%x\n",
2948 rc);
2949 break;
2950
2951 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
2952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04002953 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04002954 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
2955 acqe_fcoe->event_tag);
2956 break;
2957
2958 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
2959 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2960 "2549 FCF disconnected fron network index 0x%x"
James Smarte4e74272009-07-19 10:01:38 -04002961 " tag 0x%x\n", acqe_fcoe->fcf_index,
James Smartda0436e2009-05-22 14:51:39 -04002962 acqe_fcoe->event_tag);
2963 /* If the event is not for currently used fcf do nothing */
2964 if (phba->fcf.fcf_indx != acqe_fcoe->fcf_index)
2965 break;
2966 /*
2967 * Currently, driver support only one FCF - so treat this as
2968 * a link down.
2969 */
2970 lpfc_linkdown(phba);
2971 /* Unregister FCF if no devices connected to it */
2972 lpfc_unregister_unused_fcf(phba);
2973 break;
2974
2975 default:
2976 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2977 "0288 Unknown FCoE event type 0x%x event tag "
2978 "0x%x\n", event_type, acqe_fcoe->event_tag);
2979 break;
2980 }
2981}
2982
2983/**
2984 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
2985 * @phba: pointer to lpfc hba data structure.
2986 * @acqe_link: pointer to the async dcbx completion queue entry.
2987 *
2988 * This routine is to handle the SLI4 asynchronous dcbx event.
2989 **/
2990static void
2991lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
2992 struct lpfc_acqe_dcbx *acqe_dcbx)
2993{
James Smart4d9ab992009-10-02 15:16:39 -04002994 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04002995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2996 "0290 The SLI4 DCBX asynchronous event is not "
2997 "handled yet\n");
2998}
2999
3000/**
3001 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3002 * @phba: pointer to lpfc hba data structure.
3003 *
3004 * This routine is invoked by the worker thread to process all the pending
3005 * SLI4 asynchronous events.
3006 **/
3007void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3008{
3009 struct lpfc_cq_event *cq_event;
3010
3011 /* First, declare the async event has been handled */
3012 spin_lock_irq(&phba->hbalock);
3013 phba->hba_flag &= ~ASYNC_EVENT;
3014 spin_unlock_irq(&phba->hbalock);
3015 /* Now, handle all the async events */
3016 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3017 /* Get the first event from the head of the event queue */
3018 spin_lock_irq(&phba->hbalock);
3019 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3020 cq_event, struct lpfc_cq_event, list);
3021 spin_unlock_irq(&phba->hbalock);
3022 /* Process the asynchronous event */
3023 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3024 case LPFC_TRAILER_CODE_LINK:
3025 lpfc_sli4_async_link_evt(phba,
3026 &cq_event->cqe.acqe_link);
3027 break;
3028 case LPFC_TRAILER_CODE_FCOE:
3029 lpfc_sli4_async_fcoe_evt(phba,
3030 &cq_event->cqe.acqe_fcoe);
3031 break;
3032 case LPFC_TRAILER_CODE_DCBX:
3033 lpfc_sli4_async_dcbx_evt(phba,
3034 &cq_event->cqe.acqe_dcbx);
3035 break;
3036 default:
3037 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3038 "1804 Invalid asynchrous event code: "
3039 "x%x\n", bf_get(lpfc_trailer_code,
3040 &cq_event->cqe.mcqe_cmpl));
3041 break;
3042 }
3043 /* Free the completion event processed to the free pool */
3044 lpfc_sli4_cq_event_release(phba, cq_event);
3045 }
3046}
3047
3048/**
3049 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3050 * @phba: pointer to lpfc hba data structure.
3051 * @dev_grp: The HBA PCI-Device group number.
3052 *
3053 * This routine is invoked to set up the per HBA PCI-Device group function
3054 * API jump table entries.
3055 *
3056 * Return: 0 if success, otherwise -ENODEV
3057 **/
3058int
3059lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003060{
3061 int rc;
3062
James Smartda0436e2009-05-22 14:51:39 -04003063 /* Set up lpfc PCI-device group */
3064 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003065
James Smartda0436e2009-05-22 14:51:39 -04003066 /* The LPFC_PCI_DEV_OC uses SLI4 */
3067 if (dev_grp == LPFC_PCI_DEV_OC)
3068 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003069
James Smartda0436e2009-05-22 14:51:39 -04003070 /* Set up device INIT API function jump table */
3071 rc = lpfc_init_api_table_setup(phba, dev_grp);
3072 if (rc)
3073 return -ENODEV;
3074 /* Set up SCSI API function jump table */
3075 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3076 if (rc)
3077 return -ENODEV;
3078 /* Set up SLI API function jump table */
3079 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3080 if (rc)
3081 return -ENODEV;
3082 /* Set up MBOX API function jump table */
3083 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3084 if (rc)
3085 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003086
James Smartda0436e2009-05-22 14:51:39 -04003087 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003088}
3089
3090/**
James Smart3621a712009-04-06 18:47:14 -04003091 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003092 * @phba: pointer to lpfc hba data structure.
3093 * @intr_mode: active interrupt mode adopted.
3094 *
3095 * This routine it invoked to log the currently used active interrupt mode
3096 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003097 **/
3098static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003099{
3100 switch (intr_mode) {
3101 case 0:
3102 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3103 "0470 Enable INTx interrupt mode.\n");
3104 break;
3105 case 1:
3106 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3107 "0481 Enabled MSI interrupt mode.\n");
3108 break;
3109 case 2:
3110 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3111 "0480 Enabled MSI-X interrupt mode.\n");
3112 break;
3113 default:
3114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3115 "0482 Illegal interrupt mode.\n");
3116 break;
3117 }
3118 return;
3119}
3120
James Smart5b75da22008-12-04 22:39:35 -05003121/**
James Smart3772a992009-05-22 14:50:54 -04003122 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003123 * @phba: pointer to lpfc hba data structure.
3124 *
James Smart3772a992009-05-22 14:50:54 -04003125 * This routine is invoked to enable the PCI device that is common to all
3126 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003127 *
3128 * Return codes
James Smart3772a992009-05-22 14:50:54 -04003129 * 0 - sucessful
3130 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003131 **/
James Smart3772a992009-05-22 14:50:54 -04003132static int
3133lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003134{
James Smart3772a992009-05-22 14:50:54 -04003135 struct pci_dev *pdev;
3136 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003137
James Smart3772a992009-05-22 14:50:54 -04003138 /* Obtain PCI device reference */
3139 if (!phba->pcidev)
3140 goto out_error;
3141 else
3142 pdev = phba->pcidev;
3143 /* Select PCI BARs */
3144 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3145 /* Enable PCI device */
3146 if (pci_enable_device_mem(pdev))
3147 goto out_error;
3148 /* Request PCI resource for the device */
3149 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3150 goto out_disable_device;
3151 /* Set up device as PCI master and save state for EEH */
3152 pci_set_master(pdev);
3153 pci_try_set_mwi(pdev);
3154 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003155
James Smart3772a992009-05-22 14:50:54 -04003156 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003157
James Smart3772a992009-05-22 14:50:54 -04003158out_disable_device:
3159 pci_disable_device(pdev);
3160out_error:
3161 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003162}
3163
3164/**
James Smart3772a992009-05-22 14:50:54 -04003165 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003166 * @phba: pointer to lpfc hba data structure.
3167 *
James Smart3772a992009-05-22 14:50:54 -04003168 * This routine is invoked to disable the PCI device that is common to all
3169 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003170 **/
3171static void
James Smart3772a992009-05-22 14:50:54 -04003172lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003173{
James Smart3772a992009-05-22 14:50:54 -04003174 struct pci_dev *pdev;
3175 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003176
James Smart3772a992009-05-22 14:50:54 -04003177 /* Obtain PCI device reference */
3178 if (!phba->pcidev)
3179 return;
3180 else
3181 pdev = phba->pcidev;
3182 /* Select PCI BARs */
3183 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3184 /* Release PCI resource and disable PCI device */
3185 pci_release_selected_regions(pdev, bars);
3186 pci_disable_device(pdev);
3187 /* Null out PCI private reference to driver */
3188 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003189
3190 return;
3191}
3192
3193/**
James Smart3772a992009-05-22 14:50:54 -04003194 * lpfc_reset_hba - Reset a hba
3195 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003196 *
James Smart3772a992009-05-22 14:50:54 -04003197 * This routine is invoked to reset a hba device. It brings the HBA
3198 * offline, performs a board restart, and then brings the board back
3199 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3200 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003201 **/
James Smart3772a992009-05-22 14:50:54 -04003202void
3203lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003204{
James Smart3772a992009-05-22 14:50:54 -04003205 /* If resets are disabled then set error state and return. */
3206 if (!phba->cfg_enable_hba_reset) {
3207 phba->link_state = LPFC_HBA_ERROR;
3208 return;
3209 }
3210 lpfc_offline_prep(phba);
3211 lpfc_offline(phba);
3212 lpfc_sli_brdrestart(phba);
3213 lpfc_online(phba);
3214 lpfc_unblock_mgmt_io(phba);
3215}
dea31012005-04-17 16:05:31 -05003216
James Smart3772a992009-05-22 14:50:54 -04003217/**
3218 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3219 * @phba: pointer to lpfc hba data structure.
3220 *
3221 * This routine is invoked to set up the driver internal resources specific to
3222 * support the SLI-3 HBA device it attached to.
3223 *
3224 * Return codes
3225 * 0 - sucessful
3226 * other values - error
3227 **/
3228static int
3229lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3230{
3231 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003232
James Smart3772a992009-05-22 14:50:54 -04003233 /*
3234 * Initialize timers used by driver
3235 */
dea31012005-04-17 16:05:31 -05003236
James Smart3772a992009-05-22 14:50:54 -04003237 /* Heartbeat timer */
3238 init_timer(&phba->hb_tmofunc);
3239 phba->hb_tmofunc.function = lpfc_hb_timeout;
3240 phba->hb_tmofunc.data = (unsigned long)phba;
3241
3242 psli = &phba->sli;
3243 /* MBOX heartbeat timer */
3244 init_timer(&psli->mbox_tmo);
3245 psli->mbox_tmo.function = lpfc_mbox_timeout;
3246 psli->mbox_tmo.data = (unsigned long) phba;
3247 /* FCP polling mode timer */
3248 init_timer(&phba->fcp_poll_timer);
3249 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3250 phba->fcp_poll_timer.data = (unsigned long) phba;
3251 /* Fabric block timer */
3252 init_timer(&phba->fabric_block_timer);
3253 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3254 phba->fabric_block_timer.data = (unsigned long) phba;
3255 /* EA polling mode timer */
3256 init_timer(&phba->eratt_poll);
3257 phba->eratt_poll.function = lpfc_poll_eratt;
3258 phba->eratt_poll.data = (unsigned long) phba;
3259
3260 /* Host attention work mask setup */
3261 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3262 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3263
3264 /* Get all the module params for configuring this host */
3265 lpfc_get_cfgparam(phba);
3266 /*
3267 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3268 * used to create the sg_dma_buf_pool must be dynamically calculated.
3269 * 2 segments are added since the IOCB needs a command and response bde.
3270 */
3271 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3272 sizeof(struct fcp_rsp) +
3273 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3274
3275 if (phba->cfg_enable_bg) {
3276 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3277 phba->cfg_sg_dma_buf_size +=
3278 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3279 }
3280
3281 /* Also reinitialize the host templates with new values. */
3282 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3283 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3284
3285 phba->max_vpi = LPFC_MAX_VPI;
3286 /* This will be set to correct value after config_port mbox */
3287 phba->max_vports = 0;
3288
3289 /*
3290 * Initialize the SLI Layer to run with lpfc HBAs.
3291 */
3292 lpfc_sli_setup(phba);
3293 lpfc_sli_queue_setup(phba);
3294
3295 /* Allocate device driver memory */
3296 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3297 return -ENOMEM;
3298
3299 return 0;
3300}
3301
3302/**
3303 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3304 * @phba: pointer to lpfc hba data structure.
3305 *
3306 * This routine is invoked to unset the driver internal resources set up
3307 * specific for supporting the SLI-3 HBA device it attached to.
3308 **/
3309static void
3310lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3311{
3312 /* Free device driver memory allocated */
3313 lpfc_mem_free_all(phba);
3314
3315 return;
3316}
3317
3318/**
James Smartda0436e2009-05-22 14:51:39 -04003319 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3320 * @phba: pointer to lpfc hba data structure.
3321 *
3322 * This routine is invoked to set up the driver internal resources specific to
3323 * support the SLI-4 HBA device it attached to.
3324 *
3325 * Return codes
3326 * 0 - sucessful
3327 * other values - error
3328 **/
3329static int
3330lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3331{
3332 struct lpfc_sli *psli;
3333 int rc;
3334 int i, hbq_count;
3335
3336 /* Before proceed, wait for POST done and device ready */
3337 rc = lpfc_sli4_post_status_check(phba);
3338 if (rc)
3339 return -ENODEV;
3340
3341 /*
3342 * Initialize timers used by driver
3343 */
3344
3345 /* Heartbeat timer */
3346 init_timer(&phba->hb_tmofunc);
3347 phba->hb_tmofunc.function = lpfc_hb_timeout;
3348 phba->hb_tmofunc.data = (unsigned long)phba;
3349
3350 psli = &phba->sli;
3351 /* MBOX heartbeat timer */
3352 init_timer(&psli->mbox_tmo);
3353 psli->mbox_tmo.function = lpfc_mbox_timeout;
3354 psli->mbox_tmo.data = (unsigned long) phba;
3355 /* Fabric block timer */
3356 init_timer(&phba->fabric_block_timer);
3357 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3358 phba->fabric_block_timer.data = (unsigned long) phba;
3359 /* EA polling mode timer */
3360 init_timer(&phba->eratt_poll);
3361 phba->eratt_poll.function = lpfc_poll_eratt;
3362 phba->eratt_poll.data = (unsigned long) phba;
3363 /*
3364 * We need to do a READ_CONFIG mailbox command here before
3365 * calling lpfc_get_cfgparam. For VFs this will report the
3366 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3367 * All of the resources allocated
3368 * for this Port are tied to these values.
3369 */
3370 /* Get all the module params for configuring this host */
3371 lpfc_get_cfgparam(phba);
3372 phba->max_vpi = LPFC_MAX_VPI;
3373 /* This will be set to correct value after the read_config mbox */
3374 phba->max_vports = 0;
3375
3376 /* Program the default value of vlan_id and fc_map */
3377 phba->valid_vlan = 0;
3378 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3379 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3380 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3381
3382 /*
3383 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3384 * used to create the sg_dma_buf_pool must be dynamically calculated.
3385 * 2 segments are added since the IOCB needs a command and response bde.
3386 * To insure that the scsi sgl does not cross a 4k page boundary only
3387 * sgl sizes of 1k, 2k, 4k, and 8k are supported.
3388 * Table of sgl sizes and seg_cnt:
3389 * sgl size, sg_seg_cnt total seg
3390 * 1k 50 52
3391 * 2k 114 116
3392 * 4k 242 244
3393 * 8k 498 500
3394 * cmd(32) + rsp(160) + (52 * sizeof(sli4_sge)) = 1024
3395 * cmd(32) + rsp(160) + (116 * sizeof(sli4_sge)) = 2048
3396 * cmd(32) + rsp(160) + (244 * sizeof(sli4_sge)) = 4096
3397 * cmd(32) + rsp(160) + (500 * sizeof(sli4_sge)) = 8192
3398 */
3399 if (phba->cfg_sg_seg_cnt <= LPFC_DEFAULT_SG_SEG_CNT)
3400 phba->cfg_sg_seg_cnt = 50;
3401 else if (phba->cfg_sg_seg_cnt <= 114)
3402 phba->cfg_sg_seg_cnt = 114;
3403 else if (phba->cfg_sg_seg_cnt <= 242)
3404 phba->cfg_sg_seg_cnt = 242;
3405 else
3406 phba->cfg_sg_seg_cnt = 498;
3407
3408 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd)
3409 + sizeof(struct fcp_rsp);
3410 phba->cfg_sg_dma_buf_size +=
3411 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
3412
3413 /* Initialize buffer queue management fields */
3414 hbq_count = lpfc_sli_hbq_count();
3415 for (i = 0; i < hbq_count; ++i)
3416 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3417 INIT_LIST_HEAD(&phba->rb_pend_list);
3418 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3419 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3420
3421 /*
3422 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3423 */
3424 /* Initialize the Abort scsi buffer list used by driver */
3425 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3426 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3427 /* This abort list used by worker thread */
3428 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3429
3430 /*
3431 * Initialize dirver internal slow-path work queues
3432 */
3433
3434 /* Driver internel slow-path CQ Event pool */
3435 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3436 /* Response IOCB work queue list */
3437 INIT_LIST_HEAD(&phba->sli4_hba.sp_rspiocb_work_queue);
3438 /* Asynchronous event CQ Event work queue list */
3439 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3440 /* Fast-path XRI aborted CQ Event work queue list */
3441 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3442 /* Slow-path XRI aborted CQ Event work queue list */
3443 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3444 /* Receive queue CQ Event work queue list */
3445 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3446
3447 /* Initialize the driver internal SLI layer lists. */
3448 lpfc_sli_setup(phba);
3449 lpfc_sli_queue_setup(phba);
3450
3451 /* Allocate device driver memory */
3452 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3453 if (rc)
3454 return -ENOMEM;
3455
3456 /* Create the bootstrap mailbox command */
3457 rc = lpfc_create_bootstrap_mbox(phba);
3458 if (unlikely(rc))
3459 goto out_free_mem;
3460
3461 /* Set up the host's endian order with the device. */
3462 rc = lpfc_setup_endian_order(phba);
3463 if (unlikely(rc))
3464 goto out_free_bsmbx;
3465
3466 /* Set up the hba's configuration parameters. */
3467 rc = lpfc_sli4_read_config(phba);
3468 if (unlikely(rc))
3469 goto out_free_bsmbx;
3470
3471 /* Perform a function reset */
3472 rc = lpfc_pci_function_reset(phba);
3473 if (unlikely(rc))
3474 goto out_free_bsmbx;
3475
3476 /* Create all the SLI4 queues */
3477 rc = lpfc_sli4_queue_create(phba);
3478 if (rc)
3479 goto out_free_bsmbx;
3480
3481 /* Create driver internal CQE event pool */
3482 rc = lpfc_sli4_cq_event_pool_create(phba);
3483 if (rc)
3484 goto out_destroy_queue;
3485
3486 /* Initialize and populate the iocb list per host */
3487 rc = lpfc_init_sgl_list(phba);
3488 if (rc) {
3489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3490 "1400 Failed to initialize sgl list.\n");
3491 goto out_destroy_cq_event_pool;
3492 }
3493 rc = lpfc_init_active_sgl_array(phba);
3494 if (rc) {
3495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3496 "1430 Failed to initialize sgl list.\n");
3497 goto out_free_sgl_list;
3498 }
3499
3500 rc = lpfc_sli4_init_rpi_hdrs(phba);
3501 if (rc) {
3502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3503 "1432 Failed to initialize rpi headers.\n");
3504 goto out_free_active_sgl;
3505 }
3506
3507 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3508 phba->cfg_fcp_eq_count), GFP_KERNEL);
3509 if (!phba->sli4_hba.fcp_eq_hdl) {
3510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3511 "2572 Failed allocate memory for fast-path "
3512 "per-EQ handle array\n");
3513 goto out_remove_rpi_hdrs;
3514 }
3515
3516 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3517 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3518 if (!phba->sli4_hba.msix_entries) {
3519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3520 "2573 Failed allocate memory for msi-x "
3521 "interrupt vector entries\n");
3522 goto out_free_fcp_eq_hdl;
3523 }
3524
3525 return rc;
3526
3527out_free_fcp_eq_hdl:
3528 kfree(phba->sli4_hba.fcp_eq_hdl);
3529out_remove_rpi_hdrs:
3530 lpfc_sli4_remove_rpi_hdrs(phba);
3531out_free_active_sgl:
3532 lpfc_free_active_sgl(phba);
3533out_free_sgl_list:
3534 lpfc_free_sgl_list(phba);
3535out_destroy_cq_event_pool:
3536 lpfc_sli4_cq_event_pool_destroy(phba);
3537out_destroy_queue:
3538 lpfc_sli4_queue_destroy(phba);
3539out_free_bsmbx:
3540 lpfc_destroy_bootstrap_mbox(phba);
3541out_free_mem:
3542 lpfc_mem_free(phba);
3543 return rc;
3544}
3545
3546/**
3547 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3548 * @phba: pointer to lpfc hba data structure.
3549 *
3550 * This routine is invoked to unset the driver internal resources set up
3551 * specific for supporting the SLI-4 HBA device it attached to.
3552 **/
3553static void
3554lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3555{
3556 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3557
3558 /* unregister default FCFI from the HBA */
3559 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3560
3561 /* Free the default FCR table */
3562 lpfc_sli_remove_dflt_fcf(phba);
3563
3564 /* Free memory allocated for msi-x interrupt vector entries */
3565 kfree(phba->sli4_hba.msix_entries);
3566
3567 /* Free memory allocated for fast-path work queue handles */
3568 kfree(phba->sli4_hba.fcp_eq_hdl);
3569
3570 /* Free the allocated rpi headers. */
3571 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04003572 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04003573
3574 /* Free the ELS sgl list */
3575 lpfc_free_active_sgl(phba);
3576 lpfc_free_sgl_list(phba);
3577
3578 /* Free the SCSI sgl management array */
3579 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3580
3581 /* Free the SLI4 queues */
3582 lpfc_sli4_queue_destroy(phba);
3583
3584 /* Free the completion queue EQ event pool */
3585 lpfc_sli4_cq_event_release_all(phba);
3586 lpfc_sli4_cq_event_pool_destroy(phba);
3587
3588 /* Reset SLI4 HBA FCoE function */
3589 lpfc_pci_function_reset(phba);
3590
3591 /* Free the bsmbx region. */
3592 lpfc_destroy_bootstrap_mbox(phba);
3593
3594 /* Free the SLI Layer memory with SLI4 HBAs */
3595 lpfc_mem_free_all(phba);
3596
3597 /* Free the current connect table */
3598 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04003599 &phba->fcf_conn_rec_list, list) {
3600 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04003601 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04003602 }
James Smartda0436e2009-05-22 14:51:39 -04003603
3604 return;
3605}
3606
3607/**
James Smart3772a992009-05-22 14:50:54 -04003608 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3609 * @phba: The hba struct for which this call is being executed.
3610 * @dev_grp: The HBA PCI-Device group number.
3611 *
3612 * This routine sets up the device INIT interface API function jump table
3613 * in @phba struct.
3614 *
3615 * Returns: 0 - success, -ENODEV - failure.
3616 **/
3617int
3618lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3619{
3620 switch (dev_grp) {
3621 case LPFC_PCI_DEV_LP:
3622 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3623 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3624 phba->lpfc_stop_port = lpfc_stop_port_s3;
3625 break;
James Smartda0436e2009-05-22 14:51:39 -04003626 case LPFC_PCI_DEV_OC:
3627 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3628 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3629 phba->lpfc_stop_port = lpfc_stop_port_s4;
3630 break;
James Smart3772a992009-05-22 14:50:54 -04003631 default:
3632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3633 "1431 Invalid HBA PCI-device group: 0x%x\n",
3634 dev_grp);
3635 return -ENODEV;
3636 break;
3637 }
3638 return 0;
3639}
3640
3641/**
3642 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3643 * @phba: pointer to lpfc hba data structure.
3644 *
3645 * This routine is invoked to set up the driver internal resources before the
3646 * device specific resource setup to support the HBA device it attached to.
3647 *
3648 * Return codes
3649 * 0 - sucessful
3650 * other values - error
3651 **/
3652static int
3653lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3654{
3655 /*
3656 * Driver resources common to all SLI revisions
3657 */
James Smartea2151b2008-09-07 11:52:10 -04003658 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003659 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003660
James Smarte47c9092008-02-08 18:49:26 -05003661 /* Initialize ndlp management spinlock */
3662 spin_lock_init(&phba->ndlp_lock);
3663
James Smart2e0fef82007-06-17 19:56:36 -05003664 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04003665 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04003666 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05003667
James Smart3772a992009-05-22 14:50:54 -04003668 /* Initialize the wait queue head for the kernel thread */
3669 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05003670
James Smart3772a992009-05-22 14:50:54 -04003671 /* Initialize the scsi buffer list used by driver for scsi IO */
3672 spin_lock_init(&phba->scsi_buf_list_lock);
3673 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05003674
James Smart3772a992009-05-22 14:50:54 -04003675 /* Initialize the fabric iocb list */
3676 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05003677
James Smart3772a992009-05-22 14:50:54 -04003678 /* Initialize list to save ELS buffers */
3679 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05003680
James Smart3772a992009-05-22 14:50:54 -04003681 /* Initialize FCF connection rec list */
3682 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05003683
James Smart3772a992009-05-22 14:50:54 -04003684 return 0;
3685}
dea31012005-04-17 16:05:31 -05003686
James Smart3772a992009-05-22 14:50:54 -04003687/**
3688 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
3689 * @phba: pointer to lpfc hba data structure.
3690 *
3691 * This routine is invoked to set up the driver internal resources after the
3692 * device specific resource setup to support the HBA device it attached to.
3693 *
3694 * Return codes
3695 * 0 - sucessful
3696 * other values - error
3697 **/
3698static int
3699lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
3700{
3701 int error;
3702
3703 /* Startup the kernel thread for this host adapter. */
3704 phba->worker_thread = kthread_run(lpfc_do_work, phba,
3705 "lpfc_worker_%d", phba->brd_no);
3706 if (IS_ERR(phba->worker_thread)) {
3707 error = PTR_ERR(phba->worker_thread);
3708 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003709 }
3710
James Smart3772a992009-05-22 14:50:54 -04003711 return 0;
3712}
3713
3714/**
3715 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
3716 * @phba: pointer to lpfc hba data structure.
3717 *
3718 * This routine is invoked to unset the driver internal resources set up after
3719 * the device specific resource setup for supporting the HBA device it
3720 * attached to.
3721 **/
3722static void
3723lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
3724{
3725 /* Stop kernel worker thread */
3726 kthread_stop(phba->worker_thread);
3727}
3728
3729/**
3730 * lpfc_free_iocb_list - Free iocb list.
3731 * @phba: pointer to lpfc hba data structure.
3732 *
3733 * This routine is invoked to free the driver's IOCB list and memory.
3734 **/
3735static void
3736lpfc_free_iocb_list(struct lpfc_hba *phba)
3737{
3738 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
3739
3740 spin_lock_irq(&phba->hbalock);
3741 list_for_each_entry_safe(iocbq_entry, iocbq_next,
3742 &phba->lpfc_iocb_list, list) {
3743 list_del(&iocbq_entry->list);
3744 kfree(iocbq_entry);
3745 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003746 }
James Smart3772a992009-05-22 14:50:54 -04003747 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003748
James Smart3772a992009-05-22 14:50:54 -04003749 return;
3750}
dea31012005-04-17 16:05:31 -05003751
James Smart3772a992009-05-22 14:50:54 -04003752/**
3753 * lpfc_init_iocb_list - Allocate and initialize iocb list.
3754 * @phba: pointer to lpfc hba data structure.
3755 *
3756 * This routine is invoked to allocate and initizlize the driver's IOCB
3757 * list and set up the IOCB tag array accordingly.
3758 *
3759 * Return codes
3760 * 0 - sucessful
3761 * other values - error
3762 **/
3763static int
3764lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
3765{
3766 struct lpfc_iocbq *iocbq_entry = NULL;
3767 uint16_t iotag;
3768 int i;
dea31012005-04-17 16:05:31 -05003769
3770 /* Initialize and populate the iocb list per host. */
3771 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04003772 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003773 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003774 if (iocbq_entry == NULL) {
3775 printk(KERN_ERR "%s: only allocated %d iocbs of "
3776 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003777 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05003778 goto out_free_iocbq;
3779 }
3780
James Bottomley604a3e32005-10-29 10:28:33 -05003781 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
3782 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04003783 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05003784 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04003785 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05003786 goto out_free_iocbq;
3787 }
James Smart3772a992009-05-22 14:50:54 -04003788 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05003789
3790 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003791 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
3792 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05003793 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003794 }
3795
James Smart3772a992009-05-22 14:50:54 -04003796 return 0;
3797
3798out_free_iocbq:
3799 lpfc_free_iocb_list(phba);
3800
3801 return -ENOMEM;
3802}
3803
3804/**
James Smartda0436e2009-05-22 14:51:39 -04003805 * lpfc_free_sgl_list - Free sgl list.
3806 * @phba: pointer to lpfc hba data structure.
3807 *
3808 * This routine is invoked to free the driver's sgl list and memory.
3809 **/
3810static void
3811lpfc_free_sgl_list(struct lpfc_hba *phba)
3812{
3813 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
3814 LIST_HEAD(sglq_list);
3815 int rc = 0;
3816
3817 spin_lock_irq(&phba->hbalock);
3818 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
3819 spin_unlock_irq(&phba->hbalock);
3820
3821 list_for_each_entry_safe(sglq_entry, sglq_next,
3822 &sglq_list, list) {
3823 list_del(&sglq_entry->list);
3824 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
3825 kfree(sglq_entry);
3826 phba->sli4_hba.total_sglq_bufs--;
3827 }
3828 rc = lpfc_sli4_remove_all_sgl_pages(phba);
3829 if (rc) {
3830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3831 "2005 Unable to deregister pages from HBA: %x", rc);
3832 }
3833 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3834}
3835
3836/**
3837 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
3838 * @phba: pointer to lpfc hba data structure.
3839 *
3840 * This routine is invoked to allocate the driver's active sgl memory.
3841 * This array will hold the sglq_entry's for active IOs.
3842 **/
3843static int
3844lpfc_init_active_sgl_array(struct lpfc_hba *phba)
3845{
3846 int size;
3847 size = sizeof(struct lpfc_sglq *);
3848 size *= phba->sli4_hba.max_cfg_param.max_xri;
3849
3850 phba->sli4_hba.lpfc_sglq_active_list =
3851 kzalloc(size, GFP_KERNEL);
3852 if (!phba->sli4_hba.lpfc_sglq_active_list)
3853 return -ENOMEM;
3854 return 0;
3855}
3856
3857/**
3858 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3859 * @phba: pointer to lpfc hba data structure.
3860 *
3861 * This routine is invoked to walk through the array of active sglq entries
3862 * and free all of the resources.
3863 * This is just a place holder for now.
3864 **/
3865static void
3866lpfc_free_active_sgl(struct lpfc_hba *phba)
3867{
3868 kfree(phba->sli4_hba.lpfc_sglq_active_list);
3869}
3870
3871/**
3872 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3873 * @phba: pointer to lpfc hba data structure.
3874 *
3875 * This routine is invoked to allocate and initizlize the driver's sgl
3876 * list and set up the sgl xritag tag array accordingly.
3877 *
3878 * Return codes
3879 * 0 - sucessful
3880 * other values - error
3881 **/
3882static int
3883lpfc_init_sgl_list(struct lpfc_hba *phba)
3884{
3885 struct lpfc_sglq *sglq_entry = NULL;
3886 int i;
3887 int els_xri_cnt;
3888
3889 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3890 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3891 "2400 lpfc_init_sgl_list els %d.\n",
3892 els_xri_cnt);
3893 /* Initialize and populate the sglq list per host/VF. */
3894 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
3895 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
3896
3897 /* Sanity check on XRI management */
3898 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
3899 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3900 "2562 No room left for SCSI XRI allocation: "
3901 "max_xri=%d, els_xri=%d\n",
3902 phba->sli4_hba.max_cfg_param.max_xri,
3903 els_xri_cnt);
3904 return -ENOMEM;
3905 }
3906
3907 /* Allocate memory for the ELS XRI management array */
3908 phba->sli4_hba.lpfc_els_sgl_array =
3909 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
3910 GFP_KERNEL);
3911
3912 if (!phba->sli4_hba.lpfc_els_sgl_array) {
3913 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3914 "2401 Failed to allocate memory for ELS "
3915 "XRI management array of size %d.\n",
3916 els_xri_cnt);
3917 return -ENOMEM;
3918 }
3919
3920 /* Keep the SCSI XRI into the XRI management array */
3921 phba->sli4_hba.scsi_xri_max =
3922 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3923 phba->sli4_hba.scsi_xri_cnt = 0;
3924
3925 phba->sli4_hba.lpfc_scsi_psb_array =
3926 kzalloc((sizeof(struct lpfc_scsi_buf *) *
3927 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
3928
3929 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
3930 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3931 "2563 Failed to allocate memory for SCSI "
3932 "XRI management array of size %d.\n",
3933 phba->sli4_hba.scsi_xri_max);
3934 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3935 return -ENOMEM;
3936 }
3937
3938 for (i = 0; i < els_xri_cnt; i++) {
3939 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
3940 if (sglq_entry == NULL) {
3941 printk(KERN_ERR "%s: only allocated %d sgls of "
3942 "expected %d count. Unloading driver.\n",
3943 __func__, i, els_xri_cnt);
3944 goto out_free_mem;
3945 }
3946
3947 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
3948 if (sglq_entry->sli4_xritag == NO_XRI) {
3949 kfree(sglq_entry);
3950 printk(KERN_ERR "%s: failed to allocate XRI.\n"
3951 "Unloading driver.\n", __func__);
3952 goto out_free_mem;
3953 }
3954 sglq_entry->buff_type = GEN_BUFF_TYPE;
3955 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
3956 if (sglq_entry->virt == NULL) {
3957 kfree(sglq_entry);
3958 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
3959 "Unloading driver.\n", __func__);
3960 goto out_free_mem;
3961 }
3962 sglq_entry->sgl = sglq_entry->virt;
3963 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3964
3965 /* The list order is used by later block SGL registraton */
3966 spin_lock_irq(&phba->hbalock);
3967 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
3968 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
3969 phba->sli4_hba.total_sglq_bufs++;
3970 spin_unlock_irq(&phba->hbalock);
3971 }
3972 return 0;
3973
3974out_free_mem:
3975 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3976 lpfc_free_sgl_list(phba);
3977 return -ENOMEM;
3978}
3979
3980/**
3981 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
3982 * @phba: pointer to lpfc hba data structure.
3983 *
3984 * This routine is invoked to post rpi header templates to the
3985 * HBA consistent with the SLI-4 interface spec. This routine
3986 * posts a PAGE_SIZE memory region to the port to hold up to
3987 * PAGE_SIZE modulo 64 rpi context headers.
3988 * No locks are held here because this is an initialization routine
3989 * called only from probe or lpfc_online when interrupts are not
3990 * enabled and the driver is reinitializing the device.
3991 *
3992 * Return codes
3993 * 0 - sucessful
3994 * ENOMEM - No availble memory
3995 * EIO - The mailbox failed to complete successfully.
3996 **/
3997int
3998lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
3999{
4000 int rc = 0;
4001 int longs;
4002 uint16_t rpi_count;
4003 struct lpfc_rpi_hdr *rpi_hdr;
4004
4005 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4006
4007 /*
4008 * Provision an rpi bitmask range for discovery. The total count
4009 * is the difference between max and base + 1.
4010 */
4011 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4012 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4013
4014 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4015 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4016 GFP_KERNEL);
4017 if (!phba->sli4_hba.rpi_bmask)
4018 return -ENOMEM;
4019
4020 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4021 if (!rpi_hdr) {
4022 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4023 "0391 Error during rpi post operation\n");
4024 lpfc_sli4_remove_rpis(phba);
4025 rc = -ENODEV;
4026 }
4027
4028 return rc;
4029}
4030
4031/**
4032 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4033 * @phba: pointer to lpfc hba data structure.
4034 *
4035 * This routine is invoked to allocate a single 4KB memory region to
4036 * support rpis and stores them in the phba. This single region
4037 * provides support for up to 64 rpis. The region is used globally
4038 * by the device.
4039 *
4040 * Returns:
4041 * A valid rpi hdr on success.
4042 * A NULL pointer on any failure.
4043 **/
4044struct lpfc_rpi_hdr *
4045lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4046{
4047 uint16_t rpi_limit, curr_rpi_range;
4048 struct lpfc_dmabuf *dmabuf;
4049 struct lpfc_rpi_hdr *rpi_hdr;
4050
4051 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4052 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4053
4054 spin_lock_irq(&phba->hbalock);
4055 curr_rpi_range = phba->sli4_hba.next_rpi;
4056 spin_unlock_irq(&phba->hbalock);
4057
4058 /*
4059 * The port has a limited number of rpis. The increment here
4060 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4061 * and to allow the full max_rpi range per port.
4062 */
4063 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4064 return NULL;
4065
4066 /*
4067 * First allocate the protocol header region for the port. The
4068 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4069 */
4070 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4071 if (!dmabuf)
4072 return NULL;
4073
4074 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4075 LPFC_HDR_TEMPLATE_SIZE,
4076 &dmabuf->phys,
4077 GFP_KERNEL);
4078 if (!dmabuf->virt) {
4079 rpi_hdr = NULL;
4080 goto err_free_dmabuf;
4081 }
4082
4083 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4084 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4085 rpi_hdr = NULL;
4086 goto err_free_coherent;
4087 }
4088
4089 /* Save the rpi header data for cleanup later. */
4090 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4091 if (!rpi_hdr)
4092 goto err_free_coherent;
4093
4094 rpi_hdr->dmabuf = dmabuf;
4095 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4096 rpi_hdr->page_count = 1;
4097 spin_lock_irq(&phba->hbalock);
4098 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4099 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4100
4101 /*
4102 * The next_rpi stores the next module-64 rpi value to post
4103 * in any subsequent rpi memory region postings.
4104 */
4105 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4106 spin_unlock_irq(&phba->hbalock);
4107 return rpi_hdr;
4108
4109 err_free_coherent:
4110 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4111 dmabuf->virt, dmabuf->phys);
4112 err_free_dmabuf:
4113 kfree(dmabuf);
4114 return NULL;
4115}
4116
4117/**
4118 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4119 * @phba: pointer to lpfc hba data structure.
4120 *
4121 * This routine is invoked to remove all memory resources allocated
4122 * to support rpis. This routine presumes the caller has released all
4123 * rpis consumed by fabric or port logins and is prepared to have
4124 * the header pages removed.
4125 **/
4126void
4127lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4128{
4129 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4130
4131 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4132 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4133 list_del(&rpi_hdr->list);
4134 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4135 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4136 kfree(rpi_hdr->dmabuf);
4137 kfree(rpi_hdr);
4138 }
4139
4140 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4141 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4142}
4143
4144/**
James Smart3772a992009-05-22 14:50:54 -04004145 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4146 * @pdev: pointer to pci device data structure.
4147 *
4148 * This routine is invoked to allocate the driver hba data structure for an
4149 * HBA device. If the allocation is successful, the phba reference to the
4150 * PCI device data structure is set.
4151 *
4152 * Return codes
4153 * pointer to @phba - sucessful
4154 * NULL - error
4155 **/
4156static struct lpfc_hba *
4157lpfc_hba_alloc(struct pci_dev *pdev)
4158{
4159 struct lpfc_hba *phba;
4160
4161 /* Allocate memory for HBA structure */
4162 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4163 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004164 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004165 return NULL;
4166 }
4167
4168 /* Set reference to PCI device in HBA structure */
4169 phba->pcidev = pdev;
4170
4171 /* Assign an unused board number */
4172 phba->brd_no = lpfc_get_instance();
4173 if (phba->brd_no < 0) {
4174 kfree(phba);
4175 return NULL;
4176 }
4177
James Smartf1c3b0f2009-07-19 10:01:32 -04004178 mutex_init(&phba->ct_event_mutex);
4179 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4180
James Smart3772a992009-05-22 14:50:54 -04004181 return phba;
4182}
4183
4184/**
4185 * lpfc_hba_free - Free driver hba data structure with a device.
4186 * @phba: pointer to lpfc hba data structure.
4187 *
4188 * This routine is invoked to free the driver hba data structure with an
4189 * HBA device.
4190 **/
4191static void
4192lpfc_hba_free(struct lpfc_hba *phba)
4193{
4194 /* Release the driver assigned board number */
4195 idr_remove(&lpfc_hba_index, phba->brd_no);
4196
4197 kfree(phba);
4198 return;
4199}
4200
4201/**
4202 * lpfc_create_shost - Create hba physical port with associated scsi host.
4203 * @phba: pointer to lpfc hba data structure.
4204 *
4205 * This routine is invoked to create HBA physical port and associate a SCSI
4206 * host with it.
4207 *
4208 * Return codes
4209 * 0 - sucessful
4210 * other values - error
4211 **/
4212static int
4213lpfc_create_shost(struct lpfc_hba *phba)
4214{
4215 struct lpfc_vport *vport;
4216 struct Scsi_Host *shost;
4217
4218 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004219 phba->fc_edtov = FF_DEF_EDTOV;
4220 phba->fc_ratov = FF_DEF_RATOV;
4221 phba->fc_altov = FF_DEF_ALTOV;
4222 phba->fc_arbtov = FF_DEF_ARBTOV;
4223
James Smart3de2a652007-08-02 11:09:59 -04004224 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004225 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004226 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004227
4228 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004229 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004230 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004231 /* Put reference to SCSI host to driver's device private data */
4232 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004233
James Smart3772a992009-05-22 14:50:54 -04004234 return 0;
4235}
dea31012005-04-17 16:05:31 -05004236
James Smart3772a992009-05-22 14:50:54 -04004237/**
4238 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4239 * @phba: pointer to lpfc hba data structure.
4240 *
4241 * This routine is invoked to destroy HBA physical port and the associated
4242 * SCSI host.
4243 **/
4244static void
4245lpfc_destroy_shost(struct lpfc_hba *phba)
4246{
4247 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004248
James Smart3772a992009-05-22 14:50:54 -04004249 /* Destroy physical port that associated with the SCSI host */
4250 destroy_port(vport);
4251
4252 return;
4253}
4254
4255/**
4256 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4257 * @phba: pointer to lpfc hba data structure.
4258 * @shost: the shost to be used to detect Block guard settings.
4259 *
4260 * This routine sets up the local Block guard protocol settings for @shost.
4261 * This routine also allocates memory for debugging bg buffers.
4262 **/
4263static void
4264lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4265{
4266 int pagecnt = 10;
4267 if (lpfc_prot_mask && lpfc_prot_guard) {
4268 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4269 "1478 Registering BlockGuard with the "
4270 "SCSI layer\n");
4271 scsi_host_set_prot(shost, lpfc_prot_mask);
4272 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004273 }
James Smart81301a92008-12-04 22:39:46 -05004274 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004275 while (pagecnt) {
4276 spin_lock_init(&_dump_buf_lock);
4277 _dump_buf_data =
4278 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4279 if (_dump_buf_data) {
4280 printk(KERN_ERR "BLKGRD allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004281 "_dump_buf_data at 0x%p\n",
4282 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004283 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004284 memset(_dump_buf_data, 0,
4285 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004286 break;
James Smart3772a992009-05-22 14:50:54 -04004287 } else
James Smart81301a92008-12-04 22:39:46 -05004288 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004289 }
James Smart81301a92008-12-04 22:39:46 -05004290 if (!_dump_buf_data_order)
4291 printk(KERN_ERR "BLKGRD ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004292 "memory for hexdump\n");
4293 } else
James Smart81301a92008-12-04 22:39:46 -05004294 printk(KERN_ERR "BLKGRD already allocated _dump_buf_data=0x%p"
4295 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004296 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004297 while (pagecnt) {
4298 _dump_buf_dif =
4299 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4300 if (_dump_buf_dif) {
4301 printk(KERN_ERR "BLKGRD allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004302 "_dump_buf_dif at 0x%p\n",
4303 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004304 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004305 memset(_dump_buf_dif, 0,
4306 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004307 break;
James Smart3772a992009-05-22 14:50:54 -04004308 } else
James Smart81301a92008-12-04 22:39:46 -05004309 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004310 }
James Smart81301a92008-12-04 22:39:46 -05004311 if (!_dump_buf_dif_order)
4312 printk(KERN_ERR "BLKGRD ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004313 "memory for hexdump\n");
4314 } else
James Smart81301a92008-12-04 22:39:46 -05004315 printk(KERN_ERR "BLKGRD already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004316 _dump_buf_dif);
4317}
4318
4319/**
4320 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4321 * @phba: pointer to lpfc hba data structure.
4322 *
4323 * This routine is invoked to perform all the necessary post initialization
4324 * setup for the device.
4325 **/
4326static void
4327lpfc_post_init_setup(struct lpfc_hba *phba)
4328{
4329 struct Scsi_Host *shost;
4330 struct lpfc_adapter_event_header adapter_event;
4331
4332 /* Get the default values for Model Name and Description */
4333 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4334
4335 /*
4336 * hba setup may have changed the hba_queue_depth so we need to
4337 * adjust the value of can_queue.
4338 */
4339 shost = pci_get_drvdata(phba->pcidev);
4340 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4341 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4342 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004343
4344 lpfc_host_attrib_init(shost);
4345
James Smart2e0fef82007-06-17 19:56:36 -05004346 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4347 spin_lock_irq(shost->host_lock);
4348 lpfc_poll_start_timer(phba);
4349 spin_unlock_irq(shost->host_lock);
4350 }
James Smart8f6d98d2006-08-01 07:34:00 -04004351
James Smart93996272008-08-24 21:50:30 -04004352 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4353 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004354 /* Send board arrival event to upper layer */
4355 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4356 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4357 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004358 sizeof(adapter_event),
4359 (char *) &adapter_event,
4360 LPFC_NL_VENDOR_ID);
4361 return;
4362}
4363
4364/**
4365 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4366 * @phba: pointer to lpfc hba data structure.
4367 *
4368 * This routine is invoked to set up the PCI device memory space for device
4369 * with SLI-3 interface spec.
4370 *
4371 * Return codes
4372 * 0 - sucessful
4373 * other values - error
4374 **/
4375static int
4376lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4377{
4378 struct pci_dev *pdev;
4379 unsigned long bar0map_len, bar2map_len;
4380 int i, hbq_count;
4381 void *ptr;
4382 int error = -ENODEV;
4383
4384 /* Obtain PCI device reference */
4385 if (!phba->pcidev)
4386 return error;
4387 else
4388 pdev = phba->pcidev;
4389
4390 /* Set the device DMA mask size */
4391 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
4392 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
4393 return error;
4394
4395 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4396 * required by each mapping.
4397 */
4398 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4399 bar0map_len = pci_resource_len(pdev, 0);
4400
4401 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4402 bar2map_len = pci_resource_len(pdev, 2);
4403
4404 /* Map HBA SLIM to a kernel virtual address. */
4405 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4406 if (!phba->slim_memmap_p) {
4407 dev_printk(KERN_ERR, &pdev->dev,
4408 "ioremap failed for SLIM memory.\n");
4409 goto out;
4410 }
4411
4412 /* Map HBA Control Registers to a kernel virtual address. */
4413 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4414 if (!phba->ctrl_regs_memmap_p) {
4415 dev_printk(KERN_ERR, &pdev->dev,
4416 "ioremap failed for HBA control registers.\n");
4417 goto out_iounmap_slim;
4418 }
4419
4420 /* Allocate memory for SLI-2 structures */
4421 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4422 SLI2_SLIM_SIZE,
4423 &phba->slim2p.phys,
4424 GFP_KERNEL);
4425 if (!phba->slim2p.virt)
4426 goto out_iounmap;
4427
4428 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4429 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4430 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4431 phba->IOCBs = (phba->slim2p.virt +
4432 offsetof(struct lpfc_sli2_slim, IOCBs));
4433
4434 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4435 lpfc_sli_hbq_size(),
4436 &phba->hbqslimp.phys,
4437 GFP_KERNEL);
4438 if (!phba->hbqslimp.virt)
4439 goto out_free_slim;
4440
4441 hbq_count = lpfc_sli_hbq_count();
4442 ptr = phba->hbqslimp.virt;
4443 for (i = 0; i < hbq_count; ++i) {
4444 phba->hbqs[i].hbq_virt = ptr;
4445 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4446 ptr += (lpfc_hbq_defs[i]->entry_count *
4447 sizeof(struct lpfc_hbq_entry));
4448 }
4449 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4450 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4451
4452 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4453
4454 INIT_LIST_HEAD(&phba->rb_pend_list);
4455
4456 phba->MBslimaddr = phba->slim_memmap_p;
4457 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4458 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4459 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4460 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004461
dea31012005-04-17 16:05:31 -05004462 return 0;
4463
dea31012005-04-17 16:05:31 -05004464out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004465 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4466 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004467out_iounmap:
4468 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004469out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004470 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004471out:
4472 return error;
4473}
4474
James Smarte59058c2008-08-24 21:49:00 -04004475/**
James Smart3772a992009-05-22 14:50:54 -04004476 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4477 * @phba: pointer to lpfc hba data structure.
4478 *
4479 * This routine is invoked to unset the PCI device memory space for device
4480 * with SLI-3 interface spec.
4481 **/
4482static void
4483lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4484{
4485 struct pci_dev *pdev;
4486
4487 /* Obtain PCI device reference */
4488 if (!phba->pcidev)
4489 return;
4490 else
4491 pdev = phba->pcidev;
4492
4493 /* Free coherent DMA memory allocated */
4494 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4495 phba->hbqslimp.virt, phba->hbqslimp.phys);
4496 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4497 phba->slim2p.virt, phba->slim2p.phys);
4498
4499 /* I/O memory unmap */
4500 iounmap(phba->ctrl_regs_memmap_p);
4501 iounmap(phba->slim_memmap_p);
4502
4503 return;
4504}
4505
4506/**
James Smartda0436e2009-05-22 14:51:39 -04004507 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4508 * @phba: pointer to lpfc hba data structure.
4509 *
4510 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4511 * done and check status.
4512 *
4513 * Return 0 if successful, otherwise -ENODEV.
4514 **/
4515int
4516lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4517{
4518 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg, scratchpad;
4519 uint32_t onlnreg0, onlnreg1;
4520 int i, port_error = -ENODEV;
4521
4522 if (!phba->sli4_hba.STAregaddr)
4523 return -ENODEV;
4524
James Smartda0436e2009-05-22 14:51:39 -04004525 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4526 for (i = 0; i < 3000; i++) {
4527 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4528 /* Encounter fatal POST error, break out */
4529 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4530 port_error = -ENODEV;
4531 break;
4532 }
4533 if (LPFC_POST_STAGE_ARMFW_READY ==
4534 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4535 port_error = 0;
4536 break;
4537 }
4538 msleep(10);
4539 }
4540
4541 if (port_error)
4542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4543 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4544 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4545 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4546 bf_get(lpfc_hst_state_perr, &sta_reg),
4547 bf_get(lpfc_hst_state_sfi, &sta_reg),
4548 bf_get(lpfc_hst_state_nip, &sta_reg),
4549 bf_get(lpfc_hst_state_ipc, &sta_reg),
4550 bf_get(lpfc_hst_state_xrom, &sta_reg),
4551 bf_get(lpfc_hst_state_dl, &sta_reg),
4552 bf_get(lpfc_hst_state_port_status, &sta_reg));
4553
4554 /* Log device information */
4555 scratchpad.word0 = readl(phba->sli4_hba.SCRATCHPADregaddr);
4556 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4557 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4558 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4559 bf_get(lpfc_scratchpad_chiptype, &scratchpad),
4560 bf_get(lpfc_scratchpad_slirev, &scratchpad),
4561 bf_get(lpfc_scratchpad_featurelevel1, &scratchpad),
4562 bf_get(lpfc_scratchpad_featurelevel2, &scratchpad));
4563
James Smart8fa38512009-07-19 10:01:03 -04004564 /* With uncoverable error, log the error message and return error */
4565 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
4566 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
4567 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
4568 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4569 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4570 if (uerrlo_reg.word0 || uerrhi_reg.word0) {
4571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4572 "1422 HBA Unrecoverable error: "
4573 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4574 "online0_reg=0x%x, online1_reg=0x%x\n",
4575 uerrlo_reg.word0, uerrhi_reg.word0,
4576 onlnreg0, onlnreg1);
4577 }
4578 return -ENODEV;
4579 }
4580
James Smartda0436e2009-05-22 14:51:39 -04004581 return port_error;
4582}
4583
4584/**
4585 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4586 * @phba: pointer to lpfc hba data structure.
4587 *
4588 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4589 * memory map.
4590 **/
4591static void
4592lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4593{
4594 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4595 LPFC_UERR_STATUS_LO;
4596 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4597 LPFC_UERR_STATUS_HI;
4598 phba->sli4_hba.ONLINE0regaddr = phba->sli4_hba.conf_regs_memmap_p +
4599 LPFC_ONLINE0;
4600 phba->sli4_hba.ONLINE1regaddr = phba->sli4_hba.conf_regs_memmap_p +
4601 LPFC_ONLINE1;
4602 phba->sli4_hba.SCRATCHPADregaddr = phba->sli4_hba.conf_regs_memmap_p +
4603 LPFC_SCRATCHPAD;
4604}
4605
4606/**
4607 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4608 * @phba: pointer to lpfc hba data structure.
4609 *
4610 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4611 * memory map.
4612 **/
4613static void
4614lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4615{
4616
4617 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4618 LPFC_HST_STATE;
4619 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4620 LPFC_HST_ISR0;
4621 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4622 LPFC_HST_IMR0;
4623 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4624 LPFC_HST_ISCR0;
4625 return;
4626}
4627
4628/**
4629 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
4630 * @phba: pointer to lpfc hba data structure.
4631 * @vf: virtual function number
4632 *
4633 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4634 * based on the given viftual function number, @vf.
4635 *
4636 * Return 0 if successful, otherwise -ENODEV.
4637 **/
4638static int
4639lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
4640{
4641 if (vf > LPFC_VIR_FUNC_MAX)
4642 return -ENODEV;
4643
4644 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4645 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4646 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4647 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4648 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4649 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
4650 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4651 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
4652 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4653 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
4654 return 0;
4655}
4656
4657/**
4658 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
4659 * @phba: pointer to lpfc hba data structure.
4660 *
4661 * This routine is invoked to create the bootstrap mailbox
4662 * region consistent with the SLI-4 interface spec. This
4663 * routine allocates all memory necessary to communicate
4664 * mailbox commands to the port and sets up all alignment
4665 * needs. No locks are expected to be held when calling
4666 * this routine.
4667 *
4668 * Return codes
4669 * 0 - sucessful
4670 * ENOMEM - could not allocated memory.
4671 **/
4672static int
4673lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
4674{
4675 uint32_t bmbx_size;
4676 struct lpfc_dmabuf *dmabuf;
4677 struct dma_address *dma_address;
4678 uint32_t pa_addr;
4679 uint64_t phys_addr;
4680
4681 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4682 if (!dmabuf)
4683 return -ENOMEM;
4684
4685 /*
4686 * The bootstrap mailbox region is comprised of 2 parts
4687 * plus an alignment restriction of 16 bytes.
4688 */
4689 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
4690 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4691 bmbx_size,
4692 &dmabuf->phys,
4693 GFP_KERNEL);
4694 if (!dmabuf->virt) {
4695 kfree(dmabuf);
4696 return -ENOMEM;
4697 }
4698 memset(dmabuf->virt, 0, bmbx_size);
4699
4700 /*
4701 * Initialize the bootstrap mailbox pointers now so that the register
4702 * operations are simple later. The mailbox dma address is required
4703 * to be 16-byte aligned. Also align the virtual memory as each
4704 * maibox is copied into the bmbx mailbox region before issuing the
4705 * command to the port.
4706 */
4707 phba->sli4_hba.bmbx.dmabuf = dmabuf;
4708 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
4709
4710 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
4711 LPFC_ALIGN_16_BYTE);
4712 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
4713 LPFC_ALIGN_16_BYTE);
4714
4715 /*
4716 * Set the high and low physical addresses now. The SLI4 alignment
4717 * requirement is 16 bytes and the mailbox is posted to the port
4718 * as two 30-bit addresses. The other data is a bit marking whether
4719 * the 30-bit address is the high or low address.
4720 * Upcast bmbx aphys to 64bits so shift instruction compiles
4721 * clean on 32 bit machines.
4722 */
4723 dma_address = &phba->sli4_hba.bmbx.dma_address;
4724 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
4725 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
4726 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
4727 LPFC_BMBX_BIT1_ADDR_HI);
4728
4729 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
4730 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
4731 LPFC_BMBX_BIT1_ADDR_LO);
4732 return 0;
4733}
4734
4735/**
4736 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
4737 * @phba: pointer to lpfc hba data structure.
4738 *
4739 * This routine is invoked to teardown the bootstrap mailbox
4740 * region and release all host resources. This routine requires
4741 * the caller to ensure all mailbox commands recovered, no
4742 * additional mailbox comands are sent, and interrupts are disabled
4743 * before calling this routine.
4744 *
4745 **/
4746static void
4747lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
4748{
4749 dma_free_coherent(&phba->pcidev->dev,
4750 phba->sli4_hba.bmbx.bmbx_size,
4751 phba->sli4_hba.bmbx.dmabuf->virt,
4752 phba->sli4_hba.bmbx.dmabuf->phys);
4753
4754 kfree(phba->sli4_hba.bmbx.dmabuf);
4755 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
4756}
4757
4758/**
4759 * lpfc_sli4_read_config - Get the config parameters.
4760 * @phba: pointer to lpfc hba data structure.
4761 *
4762 * This routine is invoked to read the configuration parameters from the HBA.
4763 * The configuration parameters are used to set the base and maximum values
4764 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
4765 * allocation for the port.
4766 *
4767 * Return codes
4768 * 0 - sucessful
4769 * ENOMEM - No availble memory
4770 * EIO - The mailbox failed to complete successfully.
4771 **/
4772static int
4773lpfc_sli4_read_config(struct lpfc_hba *phba)
4774{
4775 LPFC_MBOXQ_t *pmb;
4776 struct lpfc_mbx_read_config *rd_config;
4777 uint32_t rc = 0;
4778
4779 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4780 if (!pmb) {
4781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4782 "2011 Unable to allocate memory for issuing "
4783 "SLI_CONFIG_SPECIAL mailbox command\n");
4784 return -ENOMEM;
4785 }
4786
4787 lpfc_read_config(phba, pmb);
4788
4789 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4790 if (rc != MBX_SUCCESS) {
4791 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4792 "2012 Mailbox failed , mbxCmd x%x "
4793 "READ_CONFIG, mbxStatus x%x\n",
4794 bf_get(lpfc_mqe_command, &pmb->u.mqe),
4795 bf_get(lpfc_mqe_status, &pmb->u.mqe));
4796 rc = -EIO;
4797 } else {
4798 rd_config = &pmb->u.mqe.un.rd_config;
4799 phba->sli4_hba.max_cfg_param.max_xri =
4800 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
4801 phba->sli4_hba.max_cfg_param.xri_base =
4802 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
4803 phba->sli4_hba.max_cfg_param.max_vpi =
4804 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
4805 phba->sli4_hba.max_cfg_param.vpi_base =
4806 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
4807 phba->sli4_hba.max_cfg_param.max_rpi =
4808 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
4809 phba->sli4_hba.max_cfg_param.rpi_base =
4810 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
4811 phba->sli4_hba.max_cfg_param.max_vfi =
4812 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
4813 phba->sli4_hba.max_cfg_param.vfi_base =
4814 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
4815 phba->sli4_hba.max_cfg_param.max_fcfi =
4816 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
4817 phba->sli4_hba.max_cfg_param.fcfi_base =
4818 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
4819 phba->sli4_hba.max_cfg_param.max_eq =
4820 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
4821 phba->sli4_hba.max_cfg_param.max_rq =
4822 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
4823 phba->sli4_hba.max_cfg_param.max_wq =
4824 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
4825 phba->sli4_hba.max_cfg_param.max_cq =
4826 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
4827 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
4828 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
4829 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
4830 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
4831 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4832 phba->max_vpi = phba->sli4_hba.max_cfg_param.max_vpi;
4833 phba->max_vports = phba->max_vpi;
4834 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4835 "2003 cfg params XRI(B:%d M:%d), "
4836 "VPI(B:%d M:%d) "
4837 "VFI(B:%d M:%d) "
4838 "RPI(B:%d M:%d) "
4839 "FCFI(B:%d M:%d)\n",
4840 phba->sli4_hba.max_cfg_param.xri_base,
4841 phba->sli4_hba.max_cfg_param.max_xri,
4842 phba->sli4_hba.max_cfg_param.vpi_base,
4843 phba->sli4_hba.max_cfg_param.max_vpi,
4844 phba->sli4_hba.max_cfg_param.vfi_base,
4845 phba->sli4_hba.max_cfg_param.max_vfi,
4846 phba->sli4_hba.max_cfg_param.rpi_base,
4847 phba->sli4_hba.max_cfg_param.max_rpi,
4848 phba->sli4_hba.max_cfg_param.fcfi_base,
4849 phba->sli4_hba.max_cfg_param.max_fcfi);
4850 }
4851 mempool_free(pmb, phba->mbox_mem_pool);
4852
4853 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
4854 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
4855 phba->cfg_hba_queue_depth =
4856 phba->sli4_hba.max_cfg_param.max_xri;
4857 return rc;
4858}
4859
4860/**
4861 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
4862 * @phba: pointer to lpfc hba data structure.
4863 *
4864 * This routine is invoked to setup the host-side endian order to the
4865 * HBA consistent with the SLI-4 interface spec.
4866 *
4867 * Return codes
4868 * 0 - sucessful
4869 * ENOMEM - No availble memory
4870 * EIO - The mailbox failed to complete successfully.
4871 **/
4872static int
4873lpfc_setup_endian_order(struct lpfc_hba *phba)
4874{
4875 LPFC_MBOXQ_t *mboxq;
4876 uint32_t rc = 0;
4877 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
4878 HOST_ENDIAN_HIGH_WORD1};
4879
4880 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4881 if (!mboxq) {
4882 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4883 "0492 Unable to allocate memory for issuing "
4884 "SLI_CONFIG_SPECIAL mailbox command\n");
4885 return -ENOMEM;
4886 }
4887
4888 /*
4889 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
4890 * words to contain special data values and no other data.
4891 */
4892 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
4893 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
4894 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4895 if (rc != MBX_SUCCESS) {
4896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4897 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
4898 "status x%x\n",
4899 rc);
4900 rc = -EIO;
4901 }
4902
4903 mempool_free(mboxq, phba->mbox_mem_pool);
4904 return rc;
4905}
4906
4907/**
4908 * lpfc_sli4_queue_create - Create all the SLI4 queues
4909 * @phba: pointer to lpfc hba data structure.
4910 *
4911 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
4912 * operation. For each SLI4 queue type, the parameters such as queue entry
4913 * count (queue depth) shall be taken from the module parameter. For now,
4914 * we just use some constant number as place holder.
4915 *
4916 * Return codes
4917 * 0 - sucessful
4918 * ENOMEM - No availble memory
4919 * EIO - The mailbox failed to complete successfully.
4920 **/
4921static int
4922lpfc_sli4_queue_create(struct lpfc_hba *phba)
4923{
4924 struct lpfc_queue *qdesc;
4925 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
4926 int cfg_fcp_wq_count;
4927 int cfg_fcp_eq_count;
4928
4929 /*
4930 * Sanity check for confiugred queue parameters against the run-time
4931 * device parameters
4932 */
4933
4934 /* Sanity check on FCP fast-path WQ parameters */
4935 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
4936 if (cfg_fcp_wq_count >
4937 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
4938 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
4939 LPFC_SP_WQN_DEF;
4940 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
4941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4942 "2581 Not enough WQs (%d) from "
4943 "the pci function for supporting "
4944 "FCP WQs (%d)\n",
4945 phba->sli4_hba.max_cfg_param.max_wq,
4946 phba->cfg_fcp_wq_count);
4947 goto out_error;
4948 }
4949 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4950 "2582 Not enough WQs (%d) from the pci "
4951 "function for supporting the requested "
4952 "FCP WQs (%d), the actual FCP WQs can "
4953 "be supported: %d\n",
4954 phba->sli4_hba.max_cfg_param.max_wq,
4955 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
4956 }
4957 /* The actual number of FCP work queues adopted */
4958 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
4959
4960 /* Sanity check on FCP fast-path EQ parameters */
4961 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
4962 if (cfg_fcp_eq_count >
4963 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
4964 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
4965 LPFC_SP_EQN_DEF;
4966 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
4967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4968 "2574 Not enough EQs (%d) from the "
4969 "pci function for supporting FCP "
4970 "EQs (%d)\n",
4971 phba->sli4_hba.max_cfg_param.max_eq,
4972 phba->cfg_fcp_eq_count);
4973 goto out_error;
4974 }
4975 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4976 "2575 Not enough EQs (%d) from the pci "
4977 "function for supporting the requested "
4978 "FCP EQs (%d), the actual FCP EQs can "
4979 "be supported: %d\n",
4980 phba->sli4_hba.max_cfg_param.max_eq,
4981 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
4982 }
4983 /* It does not make sense to have more EQs than WQs */
4984 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
4985 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4986 "2593 The number of FCP EQs (%d) is more "
4987 "than the number of FCP WQs (%d), take "
4988 "the number of FCP EQs same as than of "
4989 "WQs (%d)\n", cfg_fcp_eq_count,
4990 phba->cfg_fcp_wq_count,
4991 phba->cfg_fcp_wq_count);
4992 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
4993 }
4994 /* The actual number of FCP event queues adopted */
4995 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
4996 /* The overall number of event queues used */
4997 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
4998
4999 /*
5000 * Create Event Queues (EQs)
5001 */
5002
5003 /* Get EQ depth from module parameter, fake the default for now */
5004 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5005 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5006
5007 /* Create slow path event queue */
5008 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5009 phba->sli4_hba.eq_ecount);
5010 if (!qdesc) {
5011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5012 "0496 Failed allocate slow-path EQ\n");
5013 goto out_error;
5014 }
5015 phba->sli4_hba.sp_eq = qdesc;
5016
5017 /* Create fast-path FCP Event Queue(s) */
5018 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5019 phba->cfg_fcp_eq_count), GFP_KERNEL);
5020 if (!phba->sli4_hba.fp_eq) {
5021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5022 "2576 Failed allocate memory for fast-path "
5023 "EQ record array\n");
5024 goto out_free_sp_eq;
5025 }
5026 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5027 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5028 phba->sli4_hba.eq_ecount);
5029 if (!qdesc) {
5030 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5031 "0497 Failed allocate fast-path EQ\n");
5032 goto out_free_fp_eq;
5033 }
5034 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5035 }
5036
5037 /*
5038 * Create Complete Queues (CQs)
5039 */
5040
5041 /* Get CQ depth from module parameter, fake the default for now */
5042 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5043 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5044
5045 /* Create slow-path Mailbox Command Complete Queue */
5046 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5047 phba->sli4_hba.cq_ecount);
5048 if (!qdesc) {
5049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5050 "0500 Failed allocate slow-path mailbox CQ\n");
5051 goto out_free_fp_eq;
5052 }
5053 phba->sli4_hba.mbx_cq = qdesc;
5054
5055 /* Create slow-path ELS Complete Queue */
5056 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5057 phba->sli4_hba.cq_ecount);
5058 if (!qdesc) {
5059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5060 "0501 Failed allocate slow-path ELS CQ\n");
5061 goto out_free_mbx_cq;
5062 }
5063 phba->sli4_hba.els_cq = qdesc;
5064
James Smartda0436e2009-05-22 14:51:39 -04005065
5066 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5067 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5068 phba->cfg_fcp_eq_count), GFP_KERNEL);
5069 if (!phba->sli4_hba.fcp_cq) {
5070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5071 "2577 Failed allocate memory for fast-path "
5072 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005073 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005074 }
5075 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5076 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5077 phba->sli4_hba.cq_ecount);
5078 if (!qdesc) {
5079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5080 "0499 Failed allocate fast-path FCP "
5081 "CQ (%d)\n", fcp_cqidx);
5082 goto out_free_fcp_cq;
5083 }
5084 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5085 }
5086
5087 /* Create Mailbox Command Queue */
5088 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5089 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5090
5091 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5092 phba->sli4_hba.mq_ecount);
5093 if (!qdesc) {
5094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5095 "0505 Failed allocate slow-path MQ\n");
5096 goto out_free_fcp_cq;
5097 }
5098 phba->sli4_hba.mbx_wq = qdesc;
5099
5100 /*
5101 * Create all the Work Queues (WQs)
5102 */
5103 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5104 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5105
5106 /* Create slow-path ELS Work Queue */
5107 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5108 phba->sli4_hba.wq_ecount);
5109 if (!qdesc) {
5110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5111 "0504 Failed allocate slow-path ELS WQ\n");
5112 goto out_free_mbx_wq;
5113 }
5114 phba->sli4_hba.els_wq = qdesc;
5115
5116 /* Create fast-path FCP Work Queue(s) */
5117 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5118 phba->cfg_fcp_wq_count), GFP_KERNEL);
5119 if (!phba->sli4_hba.fcp_wq) {
5120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5121 "2578 Failed allocate memory for fast-path "
5122 "WQ record array\n");
5123 goto out_free_els_wq;
5124 }
5125 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5126 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5127 phba->sli4_hba.wq_ecount);
5128 if (!qdesc) {
5129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5130 "0503 Failed allocate fast-path FCP "
5131 "WQ (%d)\n", fcp_wqidx);
5132 goto out_free_fcp_wq;
5133 }
5134 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5135 }
5136
5137 /*
5138 * Create Receive Queue (RQ)
5139 */
5140 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5141 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5142
5143 /* Create Receive Queue for header */
5144 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5145 phba->sli4_hba.rq_ecount);
5146 if (!qdesc) {
5147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5148 "0506 Failed allocate receive HRQ\n");
5149 goto out_free_fcp_wq;
5150 }
5151 phba->sli4_hba.hdr_rq = qdesc;
5152
5153 /* Create Receive Queue for data */
5154 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5155 phba->sli4_hba.rq_ecount);
5156 if (!qdesc) {
5157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5158 "0507 Failed allocate receive DRQ\n");
5159 goto out_free_hdr_rq;
5160 }
5161 phba->sli4_hba.dat_rq = qdesc;
5162
5163 return 0;
5164
5165out_free_hdr_rq:
5166 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5167 phba->sli4_hba.hdr_rq = NULL;
5168out_free_fcp_wq:
5169 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5170 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5171 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5172 }
5173 kfree(phba->sli4_hba.fcp_wq);
5174out_free_els_wq:
5175 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5176 phba->sli4_hba.els_wq = NULL;
5177out_free_mbx_wq:
5178 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5179 phba->sli4_hba.mbx_wq = NULL;
5180out_free_fcp_cq:
5181 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5182 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5183 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5184 }
5185 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005186out_free_els_cq:
5187 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5188 phba->sli4_hba.els_cq = NULL;
5189out_free_mbx_cq:
5190 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5191 phba->sli4_hba.mbx_cq = NULL;
5192out_free_fp_eq:
5193 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5194 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5195 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5196 }
5197 kfree(phba->sli4_hba.fp_eq);
5198out_free_sp_eq:
5199 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5200 phba->sli4_hba.sp_eq = NULL;
5201out_error:
5202 return -ENOMEM;
5203}
5204
5205/**
5206 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5207 * @phba: pointer to lpfc hba data structure.
5208 *
5209 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5210 * operation.
5211 *
5212 * Return codes
5213 * 0 - sucessful
5214 * ENOMEM - No availble memory
5215 * EIO - The mailbox failed to complete successfully.
5216 **/
5217static void
5218lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5219{
5220 int fcp_qidx;
5221
5222 /* Release mailbox command work queue */
5223 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5224 phba->sli4_hba.mbx_wq = NULL;
5225
5226 /* Release ELS work queue */
5227 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5228 phba->sli4_hba.els_wq = NULL;
5229
5230 /* Release FCP work queue */
5231 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5232 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5233 kfree(phba->sli4_hba.fcp_wq);
5234 phba->sli4_hba.fcp_wq = NULL;
5235
5236 /* Release unsolicited receive queue */
5237 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5238 phba->sli4_hba.hdr_rq = NULL;
5239 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5240 phba->sli4_hba.dat_rq = NULL;
5241
James Smartda0436e2009-05-22 14:51:39 -04005242 /* Release ELS complete queue */
5243 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5244 phba->sli4_hba.els_cq = NULL;
5245
5246 /* Release mailbox command complete queue */
5247 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5248 phba->sli4_hba.mbx_cq = NULL;
5249
5250 /* Release FCP response complete queue */
5251 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5252 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5253 kfree(phba->sli4_hba.fcp_cq);
5254 phba->sli4_hba.fcp_cq = NULL;
5255
5256 /* Release fast-path event queue */
5257 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5258 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5259 kfree(phba->sli4_hba.fp_eq);
5260 phba->sli4_hba.fp_eq = NULL;
5261
5262 /* Release slow-path event queue */
5263 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5264 phba->sli4_hba.sp_eq = NULL;
5265
5266 return;
5267}
5268
5269/**
5270 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5271 * @phba: pointer to lpfc hba data structure.
5272 *
5273 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5274 * operation.
5275 *
5276 * Return codes
5277 * 0 - sucessful
5278 * ENOMEM - No availble memory
5279 * EIO - The mailbox failed to complete successfully.
5280 **/
5281int
5282lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5283{
5284 int rc = -ENOMEM;
5285 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5286 int fcp_cq_index = 0;
5287
5288 /*
5289 * Set up Event Queues (EQs)
5290 */
5291
5292 /* Set up slow-path event queue */
5293 if (!phba->sli4_hba.sp_eq) {
5294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5295 "0520 Slow-path EQ not allocated\n");
5296 goto out_error;
5297 }
5298 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5299 LPFC_SP_DEF_IMAX);
5300 if (rc) {
5301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5302 "0521 Failed setup of slow-path EQ: "
5303 "rc = 0x%x\n", rc);
5304 goto out_error;
5305 }
5306 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5307 "2583 Slow-path EQ setup: queue-id=%d\n",
5308 phba->sli4_hba.sp_eq->queue_id);
5309
5310 /* Set up fast-path event queue */
5311 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5312 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5314 "0522 Fast-path EQ (%d) not "
5315 "allocated\n", fcp_eqidx);
5316 goto out_destroy_fp_eq;
5317 }
5318 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5319 phba->cfg_fcp_imax);
5320 if (rc) {
5321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5322 "0523 Failed setup of fast-path EQ "
5323 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5324 goto out_destroy_fp_eq;
5325 }
5326 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5327 "2584 Fast-path EQ setup: "
5328 "queue[%d]-id=%d\n", fcp_eqidx,
5329 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5330 }
5331
5332 /*
5333 * Set up Complete Queues (CQs)
5334 */
5335
5336 /* Set up slow-path MBOX Complete Queue as the first CQ */
5337 if (!phba->sli4_hba.mbx_cq) {
5338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5339 "0528 Mailbox CQ not allocated\n");
5340 goto out_destroy_fp_eq;
5341 }
5342 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5343 LPFC_MCQ, LPFC_MBOX);
5344 if (rc) {
5345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5346 "0529 Failed setup of slow-path mailbox CQ: "
5347 "rc = 0x%x\n", rc);
5348 goto out_destroy_fp_eq;
5349 }
5350 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5351 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5352 phba->sli4_hba.mbx_cq->queue_id,
5353 phba->sli4_hba.sp_eq->queue_id);
5354
5355 /* Set up slow-path ELS Complete Queue */
5356 if (!phba->sli4_hba.els_cq) {
5357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5358 "0530 ELS CQ not allocated\n");
5359 goto out_destroy_mbx_cq;
5360 }
5361 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5362 LPFC_WCQ, LPFC_ELS);
5363 if (rc) {
5364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5365 "0531 Failed setup of slow-path ELS CQ: "
5366 "rc = 0x%x\n", rc);
5367 goto out_destroy_mbx_cq;
5368 }
5369 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5370 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5371 phba->sli4_hba.els_cq->queue_id,
5372 phba->sli4_hba.sp_eq->queue_id);
5373
James Smartda0436e2009-05-22 14:51:39 -04005374 /* Set up fast-path FCP Response Complete Queue */
5375 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5376 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5378 "0526 Fast-path FCP CQ (%d) not "
5379 "allocated\n", fcp_cqidx);
5380 goto out_destroy_fcp_cq;
5381 }
5382 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5383 phba->sli4_hba.fp_eq[fcp_cqidx],
5384 LPFC_WCQ, LPFC_FCP);
5385 if (rc) {
5386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5387 "0527 Failed setup of fast-path FCP "
5388 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5389 goto out_destroy_fcp_cq;
5390 }
5391 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5392 "2588 FCP CQ setup: cq[%d]-id=%d, "
5393 "parent eq[%d]-id=%d\n",
5394 fcp_cqidx,
5395 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5396 fcp_cqidx,
5397 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5398 }
5399
5400 /*
5401 * Set up all the Work Queues (WQs)
5402 */
5403
5404 /* Set up Mailbox Command Queue */
5405 if (!phba->sli4_hba.mbx_wq) {
5406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5407 "0538 Slow-path MQ not allocated\n");
5408 goto out_destroy_fcp_cq;
5409 }
5410 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5411 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5412 if (rc) {
5413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5414 "0539 Failed setup of slow-path MQ: "
5415 "rc = 0x%x\n", rc);
5416 goto out_destroy_fcp_cq;
5417 }
5418 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5419 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5420 phba->sli4_hba.mbx_wq->queue_id,
5421 phba->sli4_hba.mbx_cq->queue_id);
5422
5423 /* Set up slow-path ELS Work Queue */
5424 if (!phba->sli4_hba.els_wq) {
5425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5426 "0536 Slow-path ELS WQ not allocated\n");
5427 goto out_destroy_mbx_wq;
5428 }
5429 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5430 phba->sli4_hba.els_cq, LPFC_ELS);
5431 if (rc) {
5432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5433 "0537 Failed setup of slow-path ELS WQ: "
5434 "rc = 0x%x\n", rc);
5435 goto out_destroy_mbx_wq;
5436 }
5437 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5438 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5439 phba->sli4_hba.els_wq->queue_id,
5440 phba->sli4_hba.els_cq->queue_id);
5441
5442 /* Set up fast-path FCP Work Queue */
5443 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5444 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5446 "0534 Fast-path FCP WQ (%d) not "
5447 "allocated\n", fcp_wqidx);
5448 goto out_destroy_fcp_wq;
5449 }
5450 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5451 phba->sli4_hba.fcp_cq[fcp_cq_index],
5452 LPFC_FCP);
5453 if (rc) {
5454 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5455 "0535 Failed setup of fast-path FCP "
5456 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5457 goto out_destroy_fcp_wq;
5458 }
5459 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5460 "2591 FCP WQ setup: wq[%d]-id=%d, "
5461 "parent cq[%d]-id=%d\n",
5462 fcp_wqidx,
5463 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5464 fcp_cq_index,
5465 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5466 /* Round robin FCP Work Queue's Completion Queue assignment */
5467 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5468 }
5469
5470 /*
5471 * Create Receive Queue (RQ)
5472 */
5473 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5475 "0540 Receive Queue not allocated\n");
5476 goto out_destroy_fcp_wq;
5477 }
5478 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04005479 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04005480 if (rc) {
5481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5482 "0541 Failed setup of Receive Queue: "
5483 "rc = 0x%x\n", rc);
5484 goto out_destroy_fcp_wq;
5485 }
5486 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5487 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5488 "parent cq-id=%d\n",
5489 phba->sli4_hba.hdr_rq->queue_id,
5490 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04005491 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04005492 return 0;
5493
5494out_destroy_fcp_wq:
5495 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5496 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5497 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5498out_destroy_mbx_wq:
5499 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5500out_destroy_fcp_cq:
5501 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5502 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04005503 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5504out_destroy_mbx_cq:
5505 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5506out_destroy_fp_eq:
5507 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5508 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5509 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5510out_error:
5511 return rc;
5512}
5513
5514/**
5515 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5516 * @phba: pointer to lpfc hba data structure.
5517 *
5518 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5519 * operation.
5520 *
5521 * Return codes
5522 * 0 - sucessful
5523 * ENOMEM - No availble memory
5524 * EIO - The mailbox failed to complete successfully.
5525 **/
5526void
5527lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5528{
5529 int fcp_qidx;
5530
5531 /* Unset mailbox command work queue */
5532 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5533 /* Unset ELS work queue */
5534 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5535 /* Unset unsolicited receive queue */
5536 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5537 /* Unset FCP work queue */
5538 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5539 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5540 /* Unset mailbox command complete queue */
5541 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5542 /* Unset ELS complete queue */
5543 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04005544 /* Unset FCP response complete queue */
5545 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5546 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5547 /* Unset fast-path event queue */
5548 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5549 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5550 /* Unset slow-path event queue */
5551 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5552}
5553
5554/**
5555 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5556 * @phba: pointer to lpfc hba data structure.
5557 *
5558 * This routine is invoked to allocate and set up a pool of completion queue
5559 * events. The body of the completion queue event is a completion queue entry
5560 * CQE. For now, this pool is used for the interrupt service routine to queue
5561 * the following HBA completion queue events for the worker thread to process:
5562 * - Mailbox asynchronous events
5563 * - Receive queue completion unsolicited events
5564 * Later, this can be used for all the slow-path events.
5565 *
5566 * Return codes
5567 * 0 - sucessful
5568 * -ENOMEM - No availble memory
5569 **/
5570static int
5571lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5572{
5573 struct lpfc_cq_event *cq_event;
5574 int i;
5575
5576 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5577 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5578 if (!cq_event)
5579 goto out_pool_create_fail;
5580 list_add_tail(&cq_event->list,
5581 &phba->sli4_hba.sp_cqe_event_pool);
5582 }
5583 return 0;
5584
5585out_pool_create_fail:
5586 lpfc_sli4_cq_event_pool_destroy(phba);
5587 return -ENOMEM;
5588}
5589
5590/**
5591 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5592 * @phba: pointer to lpfc hba data structure.
5593 *
5594 * This routine is invoked to free the pool of completion queue events at
5595 * driver unload time. Note that, it is the responsibility of the driver
5596 * cleanup routine to free all the outstanding completion-queue events
5597 * allocated from this pool back into the pool before invoking this routine
5598 * to destroy the pool.
5599 **/
5600static void
5601lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5602{
5603 struct lpfc_cq_event *cq_event, *next_cq_event;
5604
5605 list_for_each_entry_safe(cq_event, next_cq_event,
5606 &phba->sli4_hba.sp_cqe_event_pool, list) {
5607 list_del(&cq_event->list);
5608 kfree(cq_event);
5609 }
5610}
5611
5612/**
5613 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5614 * @phba: pointer to lpfc hba data structure.
5615 *
5616 * This routine is the lock free version of the API invoked to allocate a
5617 * completion-queue event from the free pool.
5618 *
5619 * Return: Pointer to the newly allocated completion-queue event if successful
5620 * NULL otherwise.
5621 **/
5622struct lpfc_cq_event *
5623__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5624{
5625 struct lpfc_cq_event *cq_event = NULL;
5626
5627 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5628 struct lpfc_cq_event, list);
5629 return cq_event;
5630}
5631
5632/**
5633 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5634 * @phba: pointer to lpfc hba data structure.
5635 *
5636 * This routine is the lock version of the API invoked to allocate a
5637 * completion-queue event from the free pool.
5638 *
5639 * Return: Pointer to the newly allocated completion-queue event if successful
5640 * NULL otherwise.
5641 **/
5642struct lpfc_cq_event *
5643lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5644{
5645 struct lpfc_cq_event *cq_event;
5646 unsigned long iflags;
5647
5648 spin_lock_irqsave(&phba->hbalock, iflags);
5649 cq_event = __lpfc_sli4_cq_event_alloc(phba);
5650 spin_unlock_irqrestore(&phba->hbalock, iflags);
5651 return cq_event;
5652}
5653
5654/**
5655 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5656 * @phba: pointer to lpfc hba data structure.
5657 * @cq_event: pointer to the completion queue event to be freed.
5658 *
5659 * This routine is the lock free version of the API invoked to release a
5660 * completion-queue event back into the free pool.
5661 **/
5662void
5663__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5664 struct lpfc_cq_event *cq_event)
5665{
5666 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
5667}
5668
5669/**
5670 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5671 * @phba: pointer to lpfc hba data structure.
5672 * @cq_event: pointer to the completion queue event to be freed.
5673 *
5674 * This routine is the lock version of the API invoked to release a
5675 * completion-queue event back into the free pool.
5676 **/
5677void
5678lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5679 struct lpfc_cq_event *cq_event)
5680{
5681 unsigned long iflags;
5682 spin_lock_irqsave(&phba->hbalock, iflags);
5683 __lpfc_sli4_cq_event_release(phba, cq_event);
5684 spin_unlock_irqrestore(&phba->hbalock, iflags);
5685}
5686
5687/**
5688 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
5689 * @phba: pointer to lpfc hba data structure.
5690 *
5691 * This routine is to free all the pending completion-queue events to the
5692 * back into the free pool for device reset.
5693 **/
5694static void
5695lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
5696{
5697 LIST_HEAD(cqelist);
5698 struct lpfc_cq_event *cqe;
5699 unsigned long iflags;
5700
5701 /* Retrieve all the pending WCQEs from pending WCQE lists */
5702 spin_lock_irqsave(&phba->hbalock, iflags);
5703 /* Pending FCP XRI abort events */
5704 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
5705 &cqelist);
5706 /* Pending ELS XRI abort events */
5707 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
5708 &cqelist);
5709 /* Pending asynnc events */
5710 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
5711 &cqelist);
5712 spin_unlock_irqrestore(&phba->hbalock, iflags);
5713
5714 while (!list_empty(&cqelist)) {
5715 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
5716 lpfc_sli4_cq_event_release(phba, cqe);
5717 }
5718}
5719
5720/**
5721 * lpfc_pci_function_reset - Reset pci function.
5722 * @phba: pointer to lpfc hba data structure.
5723 *
5724 * This routine is invoked to request a PCI function reset. It will destroys
5725 * all resources assigned to the PCI function which originates this request.
5726 *
5727 * Return codes
5728 * 0 - sucessful
5729 * ENOMEM - No availble memory
5730 * EIO - The mailbox failed to complete successfully.
5731 **/
5732int
5733lpfc_pci_function_reset(struct lpfc_hba *phba)
5734{
5735 LPFC_MBOXQ_t *mboxq;
5736 uint32_t rc = 0;
5737 uint32_t shdr_status, shdr_add_status;
5738 union lpfc_sli4_cfg_shdr *shdr;
5739
5740 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5741 if (!mboxq) {
5742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5743 "0494 Unable to allocate memory for issuing "
5744 "SLI_FUNCTION_RESET mailbox command\n");
5745 return -ENOMEM;
5746 }
5747
5748 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
5749 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5750 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
5751 LPFC_SLI4_MBX_EMBED);
5752 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5753 shdr = (union lpfc_sli4_cfg_shdr *)
5754 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5755 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5756 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5757 if (rc != MBX_TIMEOUT)
5758 mempool_free(mboxq, phba->mbox_mem_pool);
5759 if (shdr_status || shdr_add_status || rc) {
5760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5761 "0495 SLI_FUNCTION_RESET mailbox failed with "
5762 "status x%x add_status x%x, mbx status x%x\n",
5763 shdr_status, shdr_add_status, rc);
5764 rc = -ENXIO;
5765 }
5766 return rc;
5767}
5768
5769/**
5770 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
5771 * @phba: pointer to lpfc hba data structure.
5772 * @cnt: number of nop mailbox commands to send.
5773 *
5774 * This routine is invoked to send a number @cnt of NOP mailbox command and
5775 * wait for each command to complete.
5776 *
5777 * Return: the number of NOP mailbox command completed.
5778 **/
5779static int
5780lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
5781{
5782 LPFC_MBOXQ_t *mboxq;
5783 int length, cmdsent;
5784 uint32_t mbox_tmo;
5785 uint32_t rc = 0;
5786 uint32_t shdr_status, shdr_add_status;
5787 union lpfc_sli4_cfg_shdr *shdr;
5788
5789 if (cnt == 0) {
5790 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5791 "2518 Requested to send 0 NOP mailbox cmd\n");
5792 return cnt;
5793 }
5794
5795 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5796 if (!mboxq) {
5797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5798 "2519 Unable to allocate memory for issuing "
5799 "NOP mailbox command\n");
5800 return 0;
5801 }
5802
5803 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
5804 length = (sizeof(struct lpfc_mbx_nop) -
5805 sizeof(struct lpfc_sli4_cfg_mhdr));
5806 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5807 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
5808
5809 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5810 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
5811 if (!phba->sli4_hba.intr_enable)
5812 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5813 else
5814 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
5815 if (rc == MBX_TIMEOUT)
5816 break;
5817 /* Check return status */
5818 shdr = (union lpfc_sli4_cfg_shdr *)
5819 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5820 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5821 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
5822 &shdr->response);
5823 if (shdr_status || shdr_add_status || rc) {
5824 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5825 "2520 NOP mailbox command failed "
5826 "status x%x add_status x%x mbx "
5827 "status x%x\n", shdr_status,
5828 shdr_add_status, rc);
5829 break;
5830 }
5831 }
5832
5833 if (rc != MBX_TIMEOUT)
5834 mempool_free(mboxq, phba->mbox_mem_pool);
5835
5836 return cmdsent;
5837}
5838
5839/**
5840 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
5841 * @phba: pointer to lpfc hba data structure.
5842 * @fcfi: fcf index.
5843 *
5844 * This routine is invoked to unregister a FCFI from device.
5845 **/
5846void
5847lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
5848{
5849 LPFC_MBOXQ_t *mbox;
5850 uint32_t mbox_tmo;
5851 int rc;
5852 unsigned long flags;
5853
5854 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5855
5856 if (!mbox)
5857 return;
5858
5859 lpfc_unreg_fcfi(mbox, fcfi);
5860
5861 if (!phba->sli4_hba.intr_enable)
5862 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5863 else {
5864 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5865 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5866 }
5867 if (rc != MBX_TIMEOUT)
5868 mempool_free(mbox, phba->mbox_mem_pool);
5869 if (rc != MBX_SUCCESS)
5870 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5871 "2517 Unregister FCFI command failed "
5872 "status %d, mbxStatus x%x\n", rc,
5873 bf_get(lpfc_mqe_status, &mbox->u.mqe));
5874 else {
5875 spin_lock_irqsave(&phba->hbalock, flags);
5876 /* Mark the FCFI is no longer registered */
5877 phba->fcf.fcf_flag &=
5878 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_DISCOVERED);
5879 spin_unlock_irqrestore(&phba->hbalock, flags);
5880 }
5881}
5882
5883/**
5884 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
5885 * @phba: pointer to lpfc hba data structure.
5886 *
5887 * This routine is invoked to set up the PCI device memory space for device
5888 * with SLI-4 interface spec.
5889 *
5890 * Return codes
5891 * 0 - sucessful
5892 * other values - error
5893 **/
5894static int
5895lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
5896{
5897 struct pci_dev *pdev;
5898 unsigned long bar0map_len, bar1map_len, bar2map_len;
5899 int error = -ENODEV;
5900
5901 /* Obtain PCI device reference */
5902 if (!phba->pcidev)
5903 return error;
5904 else
5905 pdev = phba->pcidev;
5906
5907 /* Set the device DMA mask size */
5908 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
5909 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5910 return error;
5911
5912 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
5913 * number of bytes required by each mapping. They are actually
5914 * mapping to the PCI BAR regions 1, 2, and 4 by the SLI4 device.
5915 */
5916 phba->pci_bar0_map = pci_resource_start(pdev, LPFC_SLI4_BAR0);
5917 bar0map_len = pci_resource_len(pdev, LPFC_SLI4_BAR0);
5918
5919 phba->pci_bar1_map = pci_resource_start(pdev, LPFC_SLI4_BAR1);
5920 bar1map_len = pci_resource_len(pdev, LPFC_SLI4_BAR1);
5921
5922 phba->pci_bar2_map = pci_resource_start(pdev, LPFC_SLI4_BAR2);
5923 bar2map_len = pci_resource_len(pdev, LPFC_SLI4_BAR2);
5924
5925 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
5926 phba->sli4_hba.conf_regs_memmap_p =
5927 ioremap(phba->pci_bar0_map, bar0map_len);
5928 if (!phba->sli4_hba.conf_regs_memmap_p) {
5929 dev_printk(KERN_ERR, &pdev->dev,
5930 "ioremap failed for SLI4 PCI config registers.\n");
5931 goto out;
5932 }
5933
5934 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
5935 phba->sli4_hba.ctrl_regs_memmap_p =
5936 ioremap(phba->pci_bar1_map, bar1map_len);
5937 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
5938 dev_printk(KERN_ERR, &pdev->dev,
5939 "ioremap failed for SLI4 HBA control registers.\n");
5940 goto out_iounmap_conf;
5941 }
5942
5943 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
5944 phba->sli4_hba.drbl_regs_memmap_p =
5945 ioremap(phba->pci_bar2_map, bar2map_len);
5946 if (!phba->sli4_hba.drbl_regs_memmap_p) {
5947 dev_printk(KERN_ERR, &pdev->dev,
5948 "ioremap failed for SLI4 HBA doorbell registers.\n");
5949 goto out_iounmap_ctrl;
5950 }
5951
5952 /* Set up BAR0 PCI config space register memory map */
5953 lpfc_sli4_bar0_register_memmap(phba);
5954
5955 /* Set up BAR1 register memory map */
5956 lpfc_sli4_bar1_register_memmap(phba);
5957
5958 /* Set up BAR2 register memory map */
5959 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
5960 if (error)
5961 goto out_iounmap_all;
5962
5963 return 0;
5964
5965out_iounmap_all:
5966 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
5967out_iounmap_ctrl:
5968 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
5969out_iounmap_conf:
5970 iounmap(phba->sli4_hba.conf_regs_memmap_p);
5971out:
5972 return error;
5973}
5974
5975/**
5976 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
5977 * @phba: pointer to lpfc hba data structure.
5978 *
5979 * This routine is invoked to unset the PCI device memory space for device
5980 * with SLI-4 interface spec.
5981 **/
5982static void
5983lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
5984{
5985 struct pci_dev *pdev;
5986
5987 /* Obtain PCI device reference */
5988 if (!phba->pcidev)
5989 return;
5990 else
5991 pdev = phba->pcidev;
5992
5993 /* Free coherent DMA memory allocated */
5994
5995 /* Unmap I/O memory space */
5996 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
5997 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
5998 iounmap(phba->sli4_hba.conf_regs_memmap_p);
5999
6000 return;
6001}
6002
6003/**
James Smart3772a992009-05-22 14:50:54 -04006004 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6005 * @phba: pointer to lpfc hba data structure.
6006 *
6007 * This routine is invoked to enable the MSI-X interrupt vectors to device
6008 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6009 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6010 * invoked, enables either all or nothing, depending on the current
6011 * availability of PCI vector resources. The device driver is responsible
6012 * for calling the individual request_irq() to register each MSI-X vector
6013 * with a interrupt handler, which is done in this function. Note that
6014 * later when device is unloading, the driver should always call free_irq()
6015 * on all MSI-X vectors it has done request_irq() on before calling
6016 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6017 * will be left with MSI-X enabled and leaks its vectors.
6018 *
6019 * Return codes
6020 * 0 - sucessful
6021 * other values - error
6022 **/
6023static int
6024lpfc_sli_enable_msix(struct lpfc_hba *phba)
6025{
6026 int rc, i;
6027 LPFC_MBOXQ_t *pmb;
6028
6029 /* Set up MSI-X multi-message vectors */
6030 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6031 phba->msix_entries[i].entry = i;
6032
6033 /* Configure MSI-X capability structure */
6034 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6035 ARRAY_SIZE(phba->msix_entries));
6036 if (rc) {
6037 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6038 "0420 PCI enable MSI-X failed (%d)\n", rc);
6039 goto msi_fail_out;
6040 }
6041 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6043 "0477 MSI-X entry[%d]: vector=x%x "
6044 "message=%d\n", i,
6045 phba->msix_entries[i].vector,
6046 phba->msix_entries[i].entry);
6047 /*
6048 * Assign MSI-X vectors to interrupt handlers
6049 */
6050
6051 /* vector-0 is associated to slow-path handler */
6052 rc = request_irq(phba->msix_entries[0].vector,
6053 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6054 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6055 if (rc) {
6056 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6057 "0421 MSI-X slow-path request_irq failed "
6058 "(%d)\n", rc);
6059 goto msi_fail_out;
6060 }
6061
6062 /* vector-1 is associated to fast-path handler */
6063 rc = request_irq(phba->msix_entries[1].vector,
6064 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6065 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6066
6067 if (rc) {
6068 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6069 "0429 MSI-X fast-path request_irq failed "
6070 "(%d)\n", rc);
6071 goto irq_fail_out;
6072 }
6073
6074 /*
6075 * Configure HBA MSI-X attention conditions to messages
6076 */
6077 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6078
6079 if (!pmb) {
6080 rc = -ENOMEM;
6081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6082 "0474 Unable to allocate memory for issuing "
6083 "MBOX_CONFIG_MSI command\n");
6084 goto mem_fail_out;
6085 }
6086 rc = lpfc_config_msi(phba, pmb);
6087 if (rc)
6088 goto mbx_fail_out;
6089 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6090 if (rc != MBX_SUCCESS) {
6091 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6092 "0351 Config MSI mailbox command failed, "
6093 "mbxCmd x%x, mbxStatus x%x\n",
6094 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6095 goto mbx_fail_out;
6096 }
6097
6098 /* Free memory allocated for mailbox command */
6099 mempool_free(pmb, phba->mbox_mem_pool);
6100 return rc;
6101
6102mbx_fail_out:
6103 /* Free memory allocated for mailbox command */
6104 mempool_free(pmb, phba->mbox_mem_pool);
6105
6106mem_fail_out:
6107 /* free the irq already requested */
6108 free_irq(phba->msix_entries[1].vector, phba);
6109
6110irq_fail_out:
6111 /* free the irq already requested */
6112 free_irq(phba->msix_entries[0].vector, phba);
6113
6114msi_fail_out:
6115 /* Unconfigure MSI-X capability structure */
6116 pci_disable_msix(phba->pcidev);
6117 return rc;
6118}
6119
6120/**
6121 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6122 * @phba: pointer to lpfc hba data structure.
6123 *
6124 * This routine is invoked to release the MSI-X vectors and then disable the
6125 * MSI-X interrupt mode to device with SLI-3 interface spec.
6126 **/
6127static void
6128lpfc_sli_disable_msix(struct lpfc_hba *phba)
6129{
6130 int i;
6131
6132 /* Free up MSI-X multi-message vectors */
6133 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6134 free_irq(phba->msix_entries[i].vector, phba);
6135 /* Disable MSI-X */
6136 pci_disable_msix(phba->pcidev);
6137
6138 return;
6139}
6140
6141/**
6142 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6143 * @phba: pointer to lpfc hba data structure.
6144 *
6145 * This routine is invoked to enable the MSI interrupt mode to device with
6146 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6147 * enable the MSI vector. The device driver is responsible for calling the
6148 * request_irq() to register MSI vector with a interrupt the handler, which
6149 * is done in this function.
6150 *
6151 * Return codes
6152 * 0 - sucessful
6153 * other values - error
6154 */
6155static int
6156lpfc_sli_enable_msi(struct lpfc_hba *phba)
6157{
6158 int rc;
6159
6160 rc = pci_enable_msi(phba->pcidev);
6161 if (!rc)
6162 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6163 "0462 PCI enable MSI mode success.\n");
6164 else {
6165 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6166 "0471 PCI enable MSI mode failed (%d)\n", rc);
6167 return rc;
6168 }
6169
6170 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6171 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6172 if (rc) {
6173 pci_disable_msi(phba->pcidev);
6174 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6175 "0478 MSI request_irq failed (%d)\n", rc);
6176 }
6177 return rc;
6178}
6179
6180/**
6181 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6182 * @phba: pointer to lpfc hba data structure.
6183 *
6184 * This routine is invoked to disable the MSI interrupt mode to device with
6185 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6186 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6187 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6188 * its vector.
6189 */
6190static void
6191lpfc_sli_disable_msi(struct lpfc_hba *phba)
6192{
6193 free_irq(phba->pcidev->irq, phba);
6194 pci_disable_msi(phba->pcidev);
6195 return;
6196}
6197
6198/**
6199 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6200 * @phba: pointer to lpfc hba data structure.
6201 *
6202 * This routine is invoked to enable device interrupt and associate driver's
6203 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6204 * spec. Depends on the interrupt mode configured to the driver, the driver
6205 * will try to fallback from the configured interrupt mode to an interrupt
6206 * mode which is supported by the platform, kernel, and device in the order
6207 * of:
6208 * MSI-X -> MSI -> IRQ.
6209 *
6210 * Return codes
6211 * 0 - sucessful
6212 * other values - error
6213 **/
6214static uint32_t
6215lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6216{
6217 uint32_t intr_mode = LPFC_INTR_ERROR;
6218 int retval;
6219
6220 if (cfg_mode == 2) {
6221 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6222 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6223 if (!retval) {
6224 /* Now, try to enable MSI-X interrupt mode */
6225 retval = lpfc_sli_enable_msix(phba);
6226 if (!retval) {
6227 /* Indicate initialization to MSI-X mode */
6228 phba->intr_type = MSIX;
6229 intr_mode = 2;
6230 }
6231 }
6232 }
6233
6234 /* Fallback to MSI if MSI-X initialization failed */
6235 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6236 retval = lpfc_sli_enable_msi(phba);
6237 if (!retval) {
6238 /* Indicate initialization to MSI mode */
6239 phba->intr_type = MSI;
6240 intr_mode = 1;
6241 }
6242 }
6243
6244 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6245 if (phba->intr_type == NONE) {
6246 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6247 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6248 if (!retval) {
6249 /* Indicate initialization to INTx mode */
6250 phba->intr_type = INTx;
6251 intr_mode = 0;
6252 }
6253 }
6254 return intr_mode;
6255}
6256
6257/**
6258 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6259 * @phba: pointer to lpfc hba data structure.
6260 *
6261 * This routine is invoked to disable device interrupt and disassociate the
6262 * driver's interrupt handler(s) from interrupt vector(s) to device with
6263 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6264 * release the interrupt vector(s) for the message signaled interrupt.
6265 **/
6266static void
6267lpfc_sli_disable_intr(struct lpfc_hba *phba)
6268{
6269 /* Disable the currently initialized interrupt mode */
6270 if (phba->intr_type == MSIX)
6271 lpfc_sli_disable_msix(phba);
6272 else if (phba->intr_type == MSI)
6273 lpfc_sli_disable_msi(phba);
6274 else if (phba->intr_type == INTx)
6275 free_irq(phba->pcidev->irq, phba);
6276
6277 /* Reset interrupt management states */
6278 phba->intr_type = NONE;
6279 phba->sli.slistat.sli_intr = 0;
6280
6281 return;
6282}
6283
6284/**
James Smartda0436e2009-05-22 14:51:39 -04006285 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6286 * @phba: pointer to lpfc hba data structure.
6287 *
6288 * This routine is invoked to enable the MSI-X interrupt vectors to device
6289 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6290 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6291 * enables either all or nothing, depending on the current availability of
6292 * PCI vector resources. The device driver is responsible for calling the
6293 * individual request_irq() to register each MSI-X vector with a interrupt
6294 * handler, which is done in this function. Note that later when device is
6295 * unloading, the driver should always call free_irq() on all MSI-X vectors
6296 * it has done request_irq() on before calling pci_disable_msix(). Failure
6297 * to do so results in a BUG_ON() and a device will be left with MSI-X
6298 * enabled and leaks its vectors.
6299 *
6300 * Return codes
6301 * 0 - sucessful
6302 * other values - error
6303 **/
6304static int
6305lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6306{
6307 int rc, index;
6308
6309 /* Set up MSI-X multi-message vectors */
6310 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6311 phba->sli4_hba.msix_entries[index].entry = index;
6312
6313 /* Configure MSI-X capability structure */
6314 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6315 phba->sli4_hba.cfg_eqn);
6316 if (rc) {
6317 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6318 "0484 PCI enable MSI-X failed (%d)\n", rc);
6319 goto msi_fail_out;
6320 }
6321 /* Log MSI-X vector assignment */
6322 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6323 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6324 "0489 MSI-X entry[%d]: vector=x%x "
6325 "message=%d\n", index,
6326 phba->sli4_hba.msix_entries[index].vector,
6327 phba->sli4_hba.msix_entries[index].entry);
6328 /*
6329 * Assign MSI-X vectors to interrupt handlers
6330 */
6331
6332 /* The first vector must associated to slow-path handler for MQ */
6333 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6334 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6335 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6336 if (rc) {
6337 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6338 "0485 MSI-X slow-path request_irq failed "
6339 "(%d)\n", rc);
6340 goto msi_fail_out;
6341 }
6342
6343 /* The rest of the vector(s) are associated to fast-path handler(s) */
6344 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6345 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6346 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6347 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6348 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6349 LPFC_FP_DRIVER_HANDLER_NAME,
6350 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6351 if (rc) {
6352 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6353 "0486 MSI-X fast-path (%d) "
6354 "request_irq failed (%d)\n", index, rc);
6355 goto cfg_fail_out;
6356 }
6357 }
6358
6359 return rc;
6360
6361cfg_fail_out:
6362 /* free the irq already requested */
6363 for (--index; index >= 1; index--)
6364 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6365 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6366
6367 /* free the irq already requested */
6368 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6369
6370msi_fail_out:
6371 /* Unconfigure MSI-X capability structure */
6372 pci_disable_msix(phba->pcidev);
6373 return rc;
6374}
6375
6376/**
6377 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6378 * @phba: pointer to lpfc hba data structure.
6379 *
6380 * This routine is invoked to release the MSI-X vectors and then disable the
6381 * MSI-X interrupt mode to device with SLI-4 interface spec.
6382 **/
6383static void
6384lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6385{
6386 int index;
6387
6388 /* Free up MSI-X multi-message vectors */
6389 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6390
6391 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6392 free_irq(phba->sli4_hba.msix_entries[index].vector,
6393 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6394 /* Disable MSI-X */
6395 pci_disable_msix(phba->pcidev);
6396
6397 return;
6398}
6399
6400/**
6401 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6402 * @phba: pointer to lpfc hba data structure.
6403 *
6404 * This routine is invoked to enable the MSI interrupt mode to device with
6405 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6406 * to enable the MSI vector. The device driver is responsible for calling
6407 * the request_irq() to register MSI vector with a interrupt the handler,
6408 * which is done in this function.
6409 *
6410 * Return codes
6411 * 0 - sucessful
6412 * other values - error
6413 **/
6414static int
6415lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6416{
6417 int rc, index;
6418
6419 rc = pci_enable_msi(phba->pcidev);
6420 if (!rc)
6421 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6422 "0487 PCI enable MSI mode success.\n");
6423 else {
6424 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6425 "0488 PCI enable MSI mode failed (%d)\n", rc);
6426 return rc;
6427 }
6428
6429 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6430 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6431 if (rc) {
6432 pci_disable_msi(phba->pcidev);
6433 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6434 "0490 MSI request_irq failed (%d)\n", rc);
6435 }
6436
6437 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6438 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6439 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6440 }
6441
6442 return rc;
6443}
6444
6445/**
6446 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6447 * @phba: pointer to lpfc hba data structure.
6448 *
6449 * This routine is invoked to disable the MSI interrupt mode to device with
6450 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6451 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6452 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6453 * its vector.
6454 **/
6455static void
6456lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6457{
6458 free_irq(phba->pcidev->irq, phba);
6459 pci_disable_msi(phba->pcidev);
6460 return;
6461}
6462
6463/**
6464 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6465 * @phba: pointer to lpfc hba data structure.
6466 *
6467 * This routine is invoked to enable device interrupt and associate driver's
6468 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6469 * interface spec. Depends on the interrupt mode configured to the driver,
6470 * the driver will try to fallback from the configured interrupt mode to an
6471 * interrupt mode which is supported by the platform, kernel, and device in
6472 * the order of:
6473 * MSI-X -> MSI -> IRQ.
6474 *
6475 * Return codes
6476 * 0 - sucessful
6477 * other values - error
6478 **/
6479static uint32_t
6480lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6481{
6482 uint32_t intr_mode = LPFC_INTR_ERROR;
6483 int retval, index;
6484
6485 if (cfg_mode == 2) {
6486 /* Preparation before conf_msi mbox cmd */
6487 retval = 0;
6488 if (!retval) {
6489 /* Now, try to enable MSI-X interrupt mode */
6490 retval = lpfc_sli4_enable_msix(phba);
6491 if (!retval) {
6492 /* Indicate initialization to MSI-X mode */
6493 phba->intr_type = MSIX;
6494 intr_mode = 2;
6495 }
6496 }
6497 }
6498
6499 /* Fallback to MSI if MSI-X initialization failed */
6500 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6501 retval = lpfc_sli4_enable_msi(phba);
6502 if (!retval) {
6503 /* Indicate initialization to MSI mode */
6504 phba->intr_type = MSI;
6505 intr_mode = 1;
6506 }
6507 }
6508
6509 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6510 if (phba->intr_type == NONE) {
6511 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6512 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6513 if (!retval) {
6514 /* Indicate initialization to INTx mode */
6515 phba->intr_type = INTx;
6516 intr_mode = 0;
6517 for (index = 0; index < phba->cfg_fcp_eq_count;
6518 index++) {
6519 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6520 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6521 }
6522 }
6523 }
6524 return intr_mode;
6525}
6526
6527/**
6528 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6529 * @phba: pointer to lpfc hba data structure.
6530 *
6531 * This routine is invoked to disable device interrupt and disassociate
6532 * the driver's interrupt handler(s) from interrupt vector(s) to device
6533 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6534 * will release the interrupt vector(s) for the message signaled interrupt.
6535 **/
6536static void
6537lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6538{
6539 /* Disable the currently initialized interrupt mode */
6540 if (phba->intr_type == MSIX)
6541 lpfc_sli4_disable_msix(phba);
6542 else if (phba->intr_type == MSI)
6543 lpfc_sli4_disable_msi(phba);
6544 else if (phba->intr_type == INTx)
6545 free_irq(phba->pcidev->irq, phba);
6546
6547 /* Reset interrupt management states */
6548 phba->intr_type = NONE;
6549 phba->sli.slistat.sli_intr = 0;
6550
6551 return;
6552}
6553
6554/**
James Smart3772a992009-05-22 14:50:54 -04006555 * lpfc_unset_hba - Unset SLI3 hba device initialization
6556 * @phba: pointer to lpfc hba data structure.
6557 *
6558 * This routine is invoked to unset the HBA device initialization steps to
6559 * a device with SLI-3 interface spec.
6560 **/
6561static void
6562lpfc_unset_hba(struct lpfc_hba *phba)
6563{
6564 struct lpfc_vport *vport = phba->pport;
6565 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6566
6567 spin_lock_irq(shost->host_lock);
6568 vport->load_flag |= FC_UNLOADING;
6569 spin_unlock_irq(shost->host_lock);
6570
6571 lpfc_stop_hba_timers(phba);
6572
6573 phba->pport->work_port_events = 0;
6574
6575 lpfc_sli_hba_down(phba);
6576
6577 lpfc_sli_brdrestart(phba);
6578
6579 lpfc_sli_disable_intr(phba);
6580
6581 return;
6582}
6583
6584/**
James Smartda0436e2009-05-22 14:51:39 -04006585 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6586 * @phba: pointer to lpfc hba data structure.
6587 *
6588 * This routine is invoked to unset the HBA device initialization steps to
6589 * a device with SLI-4 interface spec.
6590 **/
6591static void
6592lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6593{
6594 struct lpfc_vport *vport = phba->pport;
6595 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6596
6597 spin_lock_irq(shost->host_lock);
6598 vport->load_flag |= FC_UNLOADING;
6599 spin_unlock_irq(shost->host_lock);
6600
6601 phba->pport->work_port_events = 0;
6602
6603 lpfc_sli4_hba_down(phba);
6604
6605 lpfc_sli4_disable_intr(phba);
6606
6607 return;
6608}
6609
6610/**
6611 * lpfc_sli4_hba_unset - Unset the fcoe hba
6612 * @phba: Pointer to HBA context object.
6613 *
6614 * This function is called in the SLI4 code path to reset the HBA's FCoE
6615 * function. The caller is not required to hold any lock. This routine
6616 * issues PCI function reset mailbox command to reset the FCoE function.
6617 * At the end of the function, it calls lpfc_hba_down_post function to
6618 * free any pending commands.
6619 **/
6620static void
6621lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6622{
6623 int wait_cnt = 0;
6624 LPFC_MBOXQ_t *mboxq;
6625
6626 lpfc_stop_hba_timers(phba);
6627 phba->sli4_hba.intr_enable = 0;
6628
6629 /*
6630 * Gracefully wait out the potential current outstanding asynchronous
6631 * mailbox command.
6632 */
6633
6634 /* First, block any pending async mailbox command from posted */
6635 spin_lock_irq(&phba->hbalock);
6636 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6637 spin_unlock_irq(&phba->hbalock);
6638 /* Now, trying to wait it out if we can */
6639 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6640 msleep(10);
6641 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
6642 break;
6643 }
6644 /* Forcefully release the outstanding mailbox command if timed out */
6645 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6646 spin_lock_irq(&phba->hbalock);
6647 mboxq = phba->sli.mbox_active;
6648 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6649 __lpfc_mbox_cmpl_put(phba, mboxq);
6650 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6651 phba->sli.mbox_active = NULL;
6652 spin_unlock_irq(&phba->hbalock);
6653 }
6654
6655 /* Tear down the queues in the HBA */
6656 lpfc_sli4_queue_unset(phba);
6657
6658 /* Disable PCI subsystem interrupt */
6659 lpfc_sli4_disable_intr(phba);
6660
6661 /* Stop kthread signal shall trigger work_done one more time */
6662 kthread_stop(phba->worker_thread);
6663
6664 /* Stop the SLI4 device port */
6665 phba->pport->work_port_events = 0;
6666}
6667
6668/**
James Smart3772a992009-05-22 14:50:54 -04006669 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
6670 * @pdev: pointer to PCI device
6671 * @pid: pointer to PCI device identifier
6672 *
6673 * This routine is to be called to attach a device with SLI-3 interface spec
6674 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6675 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
6676 * information of the device and driver to see if the driver state that it can
6677 * support this kind of device. If the match is successful, the driver core
6678 * invokes this routine. If this routine determines it can claim the HBA, it
6679 * does all the initialization that it needs to do to handle the HBA properly.
6680 *
6681 * Return code
6682 * 0 - driver can claim the device
6683 * negative value - driver can not claim the device
6684 **/
6685static int __devinit
6686lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
6687{
6688 struct lpfc_hba *phba;
6689 struct lpfc_vport *vport = NULL;
6690 int error;
6691 uint32_t cfg_mode, intr_mode;
6692
6693 /* Allocate memory for HBA structure */
6694 phba = lpfc_hba_alloc(pdev);
6695 if (!phba)
6696 return -ENOMEM;
6697
6698 /* Perform generic PCI device enabling operation */
6699 error = lpfc_enable_pci_dev(phba);
6700 if (error) {
6701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6702 "1401 Failed to enable pci device.\n");
6703 goto out_free_phba;
6704 }
6705
6706 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
6707 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
6708 if (error)
6709 goto out_disable_pci_dev;
6710
6711 /* Set up SLI-3 specific device PCI memory space */
6712 error = lpfc_sli_pci_mem_setup(phba);
6713 if (error) {
6714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6715 "1402 Failed to set up pci memory space.\n");
6716 goto out_disable_pci_dev;
6717 }
6718
6719 /* Set up phase-1 common device driver resources */
6720 error = lpfc_setup_driver_resource_phase1(phba);
6721 if (error) {
6722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6723 "1403 Failed to set up driver resource.\n");
6724 goto out_unset_pci_mem_s3;
6725 }
6726
6727 /* Set up SLI-3 specific device driver resources */
6728 error = lpfc_sli_driver_resource_setup(phba);
6729 if (error) {
6730 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6731 "1404 Failed to set up driver resource.\n");
6732 goto out_unset_pci_mem_s3;
6733 }
6734
6735 /* Initialize and populate the iocb list per host */
6736 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
6737 if (error) {
6738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6739 "1405 Failed to initialize iocb list.\n");
6740 goto out_unset_driver_resource_s3;
6741 }
6742
6743 /* Set up common device driver resources */
6744 error = lpfc_setup_driver_resource_phase2(phba);
6745 if (error) {
6746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6747 "1406 Failed to set up driver resource.\n");
6748 goto out_free_iocb_list;
6749 }
6750
6751 /* Create SCSI host to the physical port */
6752 error = lpfc_create_shost(phba);
6753 if (error) {
6754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6755 "1407 Failed to create scsi host.\n");
6756 goto out_unset_driver_resource;
6757 }
6758
6759 /* Configure sysfs attributes */
6760 vport = phba->pport;
6761 error = lpfc_alloc_sysfs_attr(vport);
6762 if (error) {
6763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6764 "1476 Failed to allocate sysfs attr\n");
6765 goto out_destroy_shost;
6766 }
6767
6768 /* Now, trying to enable interrupt and bring up the device */
6769 cfg_mode = phba->cfg_use_msi;
6770 while (true) {
6771 /* Put device to a known state before enabling interrupt */
6772 lpfc_stop_port(phba);
6773 /* Configure and enable interrupt */
6774 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
6775 if (intr_mode == LPFC_INTR_ERROR) {
6776 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6777 "0431 Failed to enable interrupt.\n");
6778 error = -ENODEV;
6779 goto out_free_sysfs_attr;
6780 }
6781 /* SLI-3 HBA setup */
6782 if (lpfc_sli_hba_setup(phba)) {
6783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6784 "1477 Failed to set up hba\n");
6785 error = -ENODEV;
6786 goto out_remove_device;
6787 }
6788
6789 /* Wait 50ms for the interrupts of previous mailbox commands */
6790 msleep(50);
6791 /* Check active interrupts on message signaled interrupts */
6792 if (intr_mode == 0 ||
6793 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
6794 /* Log the current active interrupt mode */
6795 phba->intr_mode = intr_mode;
6796 lpfc_log_intr_mode(phba, intr_mode);
6797 break;
6798 } else {
6799 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6800 "0447 Configure interrupt mode (%d) "
6801 "failed active interrupt test.\n",
6802 intr_mode);
6803 /* Disable the current interrupt mode */
6804 lpfc_sli_disable_intr(phba);
6805 /* Try next level of interrupt mode */
6806 cfg_mode = --intr_mode;
6807 }
6808 }
6809
6810 /* Perform post initialization setup */
6811 lpfc_post_init_setup(phba);
6812
6813 /* Check if there are static vports to be created. */
6814 lpfc_create_static_vport(phba);
6815
6816 return 0;
6817
6818out_remove_device:
6819 lpfc_unset_hba(phba);
6820out_free_sysfs_attr:
6821 lpfc_free_sysfs_attr(vport);
6822out_destroy_shost:
6823 lpfc_destroy_shost(phba);
6824out_unset_driver_resource:
6825 lpfc_unset_driver_resource_phase2(phba);
6826out_free_iocb_list:
6827 lpfc_free_iocb_list(phba);
6828out_unset_driver_resource_s3:
6829 lpfc_sli_driver_resource_unset(phba);
6830out_unset_pci_mem_s3:
6831 lpfc_sli_pci_mem_unset(phba);
6832out_disable_pci_dev:
6833 lpfc_disable_pci_dev(phba);
6834out_free_phba:
6835 lpfc_hba_free(phba);
6836 return error;
6837}
6838
6839/**
6840 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04006841 * @pdev: pointer to PCI device
6842 *
James Smart3772a992009-05-22 14:50:54 -04006843 * This routine is to be called to disattach a device with SLI-3 interface
6844 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6845 * removed from PCI bus, it performs all the necessary cleanup for the HBA
6846 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04006847 **/
dea31012005-04-17 16:05:31 -05006848static void __devexit
James Smart3772a992009-05-22 14:50:54 -04006849lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05006850{
James Smart2e0fef82007-06-17 19:56:36 -05006851 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6852 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05006853 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05006854 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05006855 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05006856 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
6857
James Smart549e55c2007-08-02 11:09:51 -04006858 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04006859 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04006860 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006861
James Smart858c9f62007-06-17 19:56:39 -05006862 lpfc_free_sysfs_attr(vport);
6863
James Smarteada2722008-12-04 22:39:13 -05006864 /* Release all the vports against this physical port */
6865 vports = lpfc_create_vport_work_array(phba);
6866 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04006867 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05006868 fc_vport_terminate(vports[i]->fc_vport);
6869 lpfc_destroy_vport_work_array(phba, vports);
6870
6871 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05006872 fc_remove_host(shost);
6873 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04006874 lpfc_cleanup(vport);
6875
James Smart2e0fef82007-06-17 19:56:36 -05006876 /*
6877 * Bring down the SLI Layer. This step disable all interrupts,
6878 * clears the rings, discards all mailbox commands, and resets
6879 * the HBA.
6880 */
James Smarta257bf92009-04-06 18:48:10 -04006881
6882 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05006883 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04006884 /* Stop kthread signal shall trigger work_done one more time */
6885 kthread_stop(phba->worker_thread);
6886 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05006887 lpfc_sli_brdrestart(phba);
6888
James Smart3772a992009-05-22 14:50:54 -04006889 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05006890 spin_lock_irq(&phba->hbalock);
6891 list_del_init(&vport->listentry);
6892 spin_unlock_irq(&phba->hbalock);
6893
James Smart858c9f62007-06-17 19:56:39 -05006894 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006895
James Smart5b75da22008-12-04 22:39:35 -05006896 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04006897 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05006898
6899 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05006900 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05006901
6902 /*
6903 * Call scsi_free before mem_free since scsi bufs are released to their
6904 * corresponding pools here.
6905 */
6906 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04006907 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05006908
James Smart34b02dc2008-08-24 21:49:55 -04006909 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6910 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05006911
James Smart2e0fef82007-06-17 19:56:36 -05006912 /* Free resources associated with SLI2 interface */
6913 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04006914 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05006915
6916 /* unmap adapter SLIM and Control Registers */
6917 iounmap(phba->ctrl_regs_memmap_p);
6918 iounmap(phba->slim_memmap_p);
6919
James Smart3772a992009-05-22 14:50:54 -04006920 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05006921
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05006922 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05006923 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05006924}
6925
Linas Vepstas8d63f372007-02-14 14:28:36 -06006926/**
James Smart3772a992009-05-22 14:50:54 -04006927 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05006928 * @pdev: pointer to PCI device
6929 * @msg: power management message
6930 *
James Smart3772a992009-05-22 14:50:54 -04006931 * This routine is to be called from the kernel's PCI subsystem to support
6932 * system Power Management (PM) to device with SLI-3 interface spec. When
6933 * PM invokes this method, it quiesces the device by stopping the driver's
6934 * worker thread for the device, turning off device's interrupt and DMA,
6935 * and bring the device offline. Note that as the driver implements the
6936 * minimum PM requirements to a power-aware driver's PM support for the
6937 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
6938 * to the suspend() method call will be treated as SUSPEND and the driver will
6939 * fully reinitialize its device during resume() method call, the driver will
6940 * set device to PCI_D3hot state in PCI config space instead of setting it
6941 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05006942 *
6943 * Return code
James Smart3772a992009-05-22 14:50:54 -04006944 * 0 - driver suspended the device
6945 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05006946 **/
6947static int
James Smart3772a992009-05-22 14:50:54 -04006948lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05006949{
6950 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6951 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
6952
6953 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6954 "0473 PCI device Power Management suspend.\n");
6955
6956 /* Bring down the device */
6957 lpfc_offline_prep(phba);
6958 lpfc_offline(phba);
6959 kthread_stop(phba->worker_thread);
6960
6961 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04006962 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05006963
6964 /* Save device state to PCI config space */
6965 pci_save_state(pdev);
6966 pci_set_power_state(pdev, PCI_D3hot);
6967
6968 return 0;
6969}
6970
6971/**
James Smart3772a992009-05-22 14:50:54 -04006972 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05006973 * @pdev: pointer to PCI device
6974 *
James Smart3772a992009-05-22 14:50:54 -04006975 * This routine is to be called from the kernel's PCI subsystem to support
6976 * system Power Management (PM) to device with SLI-3 interface spec. When PM
6977 * invokes this method, it restores the device's PCI config space state and
6978 * fully reinitializes the device and brings it online. Note that as the
6979 * driver implements the minimum PM requirements to a power-aware driver's
6980 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
6981 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
6982 * driver will fully reinitialize its device during resume() method call,
6983 * the device will be set to PCI_D0 directly in PCI config space before
6984 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05006985 *
6986 * Return code
James Smart3772a992009-05-22 14:50:54 -04006987 * 0 - driver suspended the device
6988 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05006989 **/
6990static int
James Smart3772a992009-05-22 14:50:54 -04006991lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05006992{
6993 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6994 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05006995 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05006996 int error;
6997
6998 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6999 "0452 PCI device Power Management resume.\n");
7000
7001 /* Restore device state from PCI config space */
7002 pci_set_power_state(pdev, PCI_D0);
7003 pci_restore_state(pdev);
7004 if (pdev->is_busmaster)
7005 pci_set_master(pdev);
7006
7007 /* Startup the kernel thread for this host adapter. */
7008 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7009 "lpfc_worker_%d", phba->brd_no);
7010 if (IS_ERR(phba->worker_thread)) {
7011 error = PTR_ERR(phba->worker_thread);
7012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7013 "0434 PM resume failed to start worker "
7014 "thread: error=x%x.\n", error);
7015 return error;
7016 }
7017
James Smart5b75da22008-12-04 22:39:35 -05007018 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007019 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007020 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007022 "0430 PM resume Failed to enable interrupt\n");
7023 return -EIO;
7024 } else
7025 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007026
7027 /* Restart HBA and bring it online */
7028 lpfc_sli_brdrestart(phba);
7029 lpfc_online(phba);
7030
James Smart5b75da22008-12-04 22:39:35 -05007031 /* Log the current active interrupt mode */
7032 lpfc_log_intr_mode(phba, phba->intr_mode);
7033
James Smart3a55b532008-12-04 22:38:54 -05007034 return 0;
7035}
7036
7037/**
James Smart3772a992009-05-22 14:50:54 -04007038 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007039 * @pdev: pointer to PCI device.
7040 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007041 *
James Smart3772a992009-05-22 14:50:54 -04007042 * This routine is called from the PCI subsystem for I/O error handling to
7043 * device with SLI-3 interface spec. This function is called by the PCI
7044 * subsystem after a PCI bus error affecting this device has been detected.
7045 * When this function is invoked, it will need to stop all the I/Os and
7046 * interrupt(s) to the device. Once that is done, it will return
7047 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7048 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007049 *
7050 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007051 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7052 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007053 **/
James Smart3772a992009-05-22 14:50:54 -04007054static pci_ers_result_t
7055lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007056{
James Smart51ef4c22007-08-02 11:10:31 -04007057 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7058 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007059 struct lpfc_sli *psli = &phba->sli;
7060 struct lpfc_sli_ring *pring;
7061
James Smarta8e497d2008-08-24 21:50:11 -04007062 if (state == pci_channel_io_perm_failure) {
7063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7064 "0472 PCI channel I/O permanent failure\n");
7065 /* Block all SCSI devices' I/Os on the host */
7066 lpfc_scsi_dev_block(phba);
7067 /* Clean up all driver's outstanding SCSI I/Os */
7068 lpfc_sli_flush_fcp_rings(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007069 return PCI_ERS_RESULT_DISCONNECT;
James Smarta8e497d2008-08-24 21:50:11 -04007070 }
Linas Vepstas5daa49e2007-03-08 16:19:11 -06007071
Linas Vepstas8d63f372007-02-14 14:28:36 -06007072 pci_disable_device(pdev);
7073 /*
7074 * There may be I/Os dropped by the firmware.
7075 * Error iocb (I/O) on txcmplq and let the SCSI layer
7076 * retry it after re-establishing link.
7077 */
7078 pring = &psli->ring[psli->fcp_ring];
7079 lpfc_sli_abort_iocb_ring(phba, pring);
7080
James Smart5b75da22008-12-04 22:39:35 -05007081 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007082 lpfc_sli_disable_intr(phba);
James Smart51ef4c22007-08-02 11:10:31 -04007083
Linas Vepstas8d63f372007-02-14 14:28:36 -06007084 /* Request a slot reset. */
7085 return PCI_ERS_RESULT_NEED_RESET;
7086}
7087
7088/**
James Smart3772a992009-05-22 14:50:54 -04007089 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007090 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007091 *
James Smart3772a992009-05-22 14:50:54 -04007092 * This routine is called from the PCI subsystem for error handling to
7093 * device with SLI-3 interface spec. This is called after PCI bus has been
7094 * reset to restart the PCI card from scratch, as if from a cold-boot.
7095 * During the PCI subsystem error recovery, after driver returns
7096 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7097 * recovery and then call this routine before calling the .resume method
7098 * to recover the device. This function will initialize the HBA device,
7099 * enable the interrupt, but it will just put the HBA to offline state
7100 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007101 *
7102 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007103 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7104 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007105 */
James Smart3772a992009-05-22 14:50:54 -04007106static pci_ers_result_t
7107lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007108{
James Smart51ef4c22007-08-02 11:10:31 -04007109 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7110 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007111 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007112 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007113
7114 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007115 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007116 printk(KERN_ERR "lpfc: Cannot re-enable "
7117 "PCI device after reset.\n");
7118 return PCI_ERS_RESULT_DISCONNECT;
7119 }
7120
James Smart97207482008-12-04 22:39:19 -05007121 pci_restore_state(pdev);
7122 if (pdev->is_busmaster)
7123 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007124
James Smart92d7f7b2007-06-17 19:56:38 -05007125 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007126 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007127 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007128
James Smart5b75da22008-12-04 22:39:35 -05007129 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007130 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007131 if (intr_mode == LPFC_INTR_ERROR) {
7132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7133 "0427 Cannot re-enable interrupt after "
7134 "slot reset.\n");
7135 return PCI_ERS_RESULT_DISCONNECT;
7136 } else
7137 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007138
7139 /* Take device offline; this will perform cleanup */
7140 lpfc_offline(phba);
7141 lpfc_sli_brdrestart(phba);
7142
James Smart5b75da22008-12-04 22:39:35 -05007143 /* Log the current active interrupt mode */
7144 lpfc_log_intr_mode(phba, phba->intr_mode);
7145
Linas Vepstas8d63f372007-02-14 14:28:36 -06007146 return PCI_ERS_RESULT_RECOVERED;
7147}
7148
7149/**
James Smart3772a992009-05-22 14:50:54 -04007150 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007151 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007152 *
James Smart3772a992009-05-22 14:50:54 -04007153 * This routine is called from the PCI subsystem for error handling to device
7154 * with SLI-3 interface spec. It is called when kernel error recovery tells
7155 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7156 * error recovery. After this call, traffic can start to flow from this device
7157 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007158 */
James Smart3772a992009-05-22 14:50:54 -04007159static void
7160lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007161{
James Smart51ef4c22007-08-02 11:10:31 -04007162 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7163 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007164
James Smart58da1ff2008-04-07 10:15:56 -04007165 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007166}
7167
James Smart3772a992009-05-22 14:50:54 -04007168/**
James Smartda0436e2009-05-22 14:51:39 -04007169 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7170 * @phba: pointer to lpfc hba data structure.
7171 *
7172 * returns the number of ELS/CT IOCBs to reserve
7173 **/
7174int
7175lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7176{
7177 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7178
James Smartf1126682009-06-10 17:22:44 -04007179 if (phba->sli_rev == LPFC_SLI_REV4) {
7180 if (max_xri <= 100)
7181 return 4;
7182 else if (max_xri <= 256)
7183 return 8;
7184 else if (max_xri <= 512)
7185 return 16;
7186 else if (max_xri <= 1024)
7187 return 32;
7188 else
7189 return 48;
7190 } else
7191 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007192}
7193
7194/**
7195 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7196 * @pdev: pointer to PCI device
7197 * @pid: pointer to PCI device identifier
7198 *
7199 * This routine is called from the kernel's PCI subsystem to device with
7200 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7201 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7202 * information of the device and driver to see if the driver state that it
7203 * can support this kind of device. If the match is successful, the driver
7204 * core invokes this routine. If this routine determines it can claim the HBA,
7205 * it does all the initialization that it needs to do to handle the HBA
7206 * properly.
7207 *
7208 * Return code
7209 * 0 - driver can claim the device
7210 * negative value - driver can not claim the device
7211 **/
7212static int __devinit
7213lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7214{
7215 struct lpfc_hba *phba;
7216 struct lpfc_vport *vport = NULL;
7217 int error;
7218 uint32_t cfg_mode, intr_mode;
7219 int mcnt;
7220
7221 /* Allocate memory for HBA structure */
7222 phba = lpfc_hba_alloc(pdev);
7223 if (!phba)
7224 return -ENOMEM;
7225
7226 /* Perform generic PCI device enabling operation */
7227 error = lpfc_enable_pci_dev(phba);
7228 if (error) {
7229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7230 "1409 Failed to enable pci device.\n");
7231 goto out_free_phba;
7232 }
7233
7234 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7235 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7236 if (error)
7237 goto out_disable_pci_dev;
7238
7239 /* Set up SLI-4 specific device PCI memory space */
7240 error = lpfc_sli4_pci_mem_setup(phba);
7241 if (error) {
7242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7243 "1410 Failed to set up pci memory space.\n");
7244 goto out_disable_pci_dev;
7245 }
7246
7247 /* Set up phase-1 common device driver resources */
7248 error = lpfc_setup_driver_resource_phase1(phba);
7249 if (error) {
7250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7251 "1411 Failed to set up driver resource.\n");
7252 goto out_unset_pci_mem_s4;
7253 }
7254
7255 /* Set up SLI-4 Specific device driver resources */
7256 error = lpfc_sli4_driver_resource_setup(phba);
7257 if (error) {
7258 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7259 "1412 Failed to set up driver resource.\n");
7260 goto out_unset_pci_mem_s4;
7261 }
7262
7263 /* Initialize and populate the iocb list per host */
7264 error = lpfc_init_iocb_list(phba,
7265 phba->sli4_hba.max_cfg_param.max_xri);
7266 if (error) {
7267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7268 "1413 Failed to initialize iocb list.\n");
7269 goto out_unset_driver_resource_s4;
7270 }
7271
7272 /* Set up common device driver resources */
7273 error = lpfc_setup_driver_resource_phase2(phba);
7274 if (error) {
7275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7276 "1414 Failed to set up driver resource.\n");
7277 goto out_free_iocb_list;
7278 }
7279
7280 /* Create SCSI host to the physical port */
7281 error = lpfc_create_shost(phba);
7282 if (error) {
7283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7284 "1415 Failed to create scsi host.\n");
7285 goto out_unset_driver_resource;
7286 }
7287
7288 /* Configure sysfs attributes */
7289 vport = phba->pport;
7290 error = lpfc_alloc_sysfs_attr(vport);
7291 if (error) {
7292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7293 "1416 Failed to allocate sysfs attr\n");
7294 goto out_destroy_shost;
7295 }
7296
7297 /* Now, trying to enable interrupt and bring up the device */
7298 cfg_mode = phba->cfg_use_msi;
7299 while (true) {
7300 /* Put device to a known state before enabling interrupt */
7301 lpfc_stop_port(phba);
7302 /* Configure and enable interrupt */
7303 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7304 if (intr_mode == LPFC_INTR_ERROR) {
7305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7306 "0426 Failed to enable interrupt.\n");
7307 error = -ENODEV;
7308 goto out_free_sysfs_attr;
7309 }
7310 /* Set up SLI-4 HBA */
7311 if (lpfc_sli4_hba_setup(phba)) {
7312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7313 "1421 Failed to set up hba\n");
7314 error = -ENODEV;
7315 goto out_disable_intr;
7316 }
7317
7318 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7319 if (intr_mode != 0)
7320 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7321 LPFC_ACT_INTR_CNT);
7322
7323 /* Check active interrupts received only for MSI/MSI-X */
7324 if (intr_mode == 0 ||
7325 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7326 /* Log the current active interrupt mode */
7327 phba->intr_mode = intr_mode;
7328 lpfc_log_intr_mode(phba, intr_mode);
7329 break;
7330 }
7331 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7332 "0451 Configure interrupt mode (%d) "
7333 "failed active interrupt test.\n",
7334 intr_mode);
7335 /* Unset the preivous SLI-4 HBA setup */
7336 lpfc_sli4_unset_hba(phba);
7337 /* Try next level of interrupt mode */
7338 cfg_mode = --intr_mode;
7339 }
7340
7341 /* Perform post initialization setup */
7342 lpfc_post_init_setup(phba);
7343
James Smart1c6834a2009-07-19 10:01:26 -04007344 /* Check if there are static vports to be created. */
7345 lpfc_create_static_vport(phba);
7346
James Smartda0436e2009-05-22 14:51:39 -04007347 return 0;
7348
7349out_disable_intr:
7350 lpfc_sli4_disable_intr(phba);
7351out_free_sysfs_attr:
7352 lpfc_free_sysfs_attr(vport);
7353out_destroy_shost:
7354 lpfc_destroy_shost(phba);
7355out_unset_driver_resource:
7356 lpfc_unset_driver_resource_phase2(phba);
7357out_free_iocb_list:
7358 lpfc_free_iocb_list(phba);
7359out_unset_driver_resource_s4:
7360 lpfc_sli4_driver_resource_unset(phba);
7361out_unset_pci_mem_s4:
7362 lpfc_sli4_pci_mem_unset(phba);
7363out_disable_pci_dev:
7364 lpfc_disable_pci_dev(phba);
7365out_free_phba:
7366 lpfc_hba_free(phba);
7367 return error;
7368}
7369
7370/**
7371 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
7372 * @pdev: pointer to PCI device
7373 *
7374 * This routine is called from the kernel's PCI subsystem to device with
7375 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7376 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7377 * device to be removed from the PCI subsystem properly.
7378 **/
7379static void __devexit
7380lpfc_pci_remove_one_s4(struct pci_dev *pdev)
7381{
7382 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7383 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7384 struct lpfc_vport **vports;
7385 struct lpfc_hba *phba = vport->phba;
7386 int i;
7387
7388 /* Mark the device unloading flag */
7389 spin_lock_irq(&phba->hbalock);
7390 vport->load_flag |= FC_UNLOADING;
7391 spin_unlock_irq(&phba->hbalock);
7392
7393 /* Free the HBA sysfs attributes */
7394 lpfc_free_sysfs_attr(vport);
7395
7396 /* Release all the vports against this physical port */
7397 vports = lpfc_create_vport_work_array(phba);
7398 if (vports != NULL)
7399 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7400 fc_vport_terminate(vports[i]->fc_vport);
7401 lpfc_destroy_vport_work_array(phba, vports);
7402
7403 /* Remove FC host and then SCSI host with the physical port */
7404 fc_remove_host(shost);
7405 scsi_remove_host(shost);
7406
7407 /* Perform cleanup on the physical port */
7408 lpfc_cleanup(vport);
7409
7410 /*
7411 * Bring down the SLI Layer. This step disables all interrupts,
7412 * clears the rings, discards all mailbox commands, and resets
7413 * the HBA FCoE function.
7414 */
7415 lpfc_debugfs_terminate(vport);
7416 lpfc_sli4_hba_unset(phba);
7417
7418 spin_lock_irq(&phba->hbalock);
7419 list_del_init(&vport->listentry);
7420 spin_unlock_irq(&phba->hbalock);
7421
7422 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7423 * buffers are released to their corresponding pools here.
7424 */
7425 lpfc_scsi_free(phba);
7426 lpfc_sli4_driver_resource_unset(phba);
7427
7428 /* Unmap adapter Control and Doorbell registers */
7429 lpfc_sli4_pci_mem_unset(phba);
7430
7431 /* Release PCI resources and disable device's PCI function */
7432 scsi_host_put(shost);
7433 lpfc_disable_pci_dev(phba);
7434
7435 /* Finally, free the driver's device data structure */
7436 lpfc_hba_free(phba);
7437
7438 return;
7439}
7440
7441/**
7442 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
7443 * @pdev: pointer to PCI device
7444 * @msg: power management message
7445 *
7446 * This routine is called from the kernel's PCI subsystem to support system
7447 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7448 * this method, it quiesces the device by stopping the driver's worker
7449 * thread for the device, turning off device's interrupt and DMA, and bring
7450 * the device offline. Note that as the driver implements the minimum PM
7451 * requirements to a power-aware driver's PM support for suspend/resume -- all
7452 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7453 * method call will be treated as SUSPEND and the driver will fully
7454 * reinitialize its device during resume() method call, the driver will set
7455 * device to PCI_D3hot state in PCI config space instead of setting it
7456 * according to the @msg provided by the PM.
7457 *
7458 * Return code
7459 * 0 - driver suspended the device
7460 * Error otherwise
7461 **/
7462static int
7463lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
7464{
7465 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7466 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7467
7468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7469 "0298 PCI device Power Management suspend.\n");
7470
7471 /* Bring down the device */
7472 lpfc_offline_prep(phba);
7473 lpfc_offline(phba);
7474 kthread_stop(phba->worker_thread);
7475
7476 /* Disable interrupt from device */
7477 lpfc_sli4_disable_intr(phba);
7478
7479 /* Save device state to PCI config space */
7480 pci_save_state(pdev);
7481 pci_set_power_state(pdev, PCI_D3hot);
7482
7483 return 0;
7484}
7485
7486/**
7487 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
7488 * @pdev: pointer to PCI device
7489 *
7490 * This routine is called from the kernel's PCI subsystem to support system
7491 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
7492 * this method, it restores the device's PCI config space state and fully
7493 * reinitializes the device and brings it online. Note that as the driver
7494 * implements the minimum PM requirements to a power-aware driver's PM for
7495 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7496 * to the suspend() method call will be treated as SUSPEND and the driver
7497 * will fully reinitialize its device during resume() method call, the device
7498 * will be set to PCI_D0 directly in PCI config space before restoring the
7499 * state.
7500 *
7501 * Return code
7502 * 0 - driver suspended the device
7503 * Error otherwise
7504 **/
7505static int
7506lpfc_pci_resume_one_s4(struct pci_dev *pdev)
7507{
7508 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7509 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7510 uint32_t intr_mode;
7511 int error;
7512
7513 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7514 "0292 PCI device Power Management resume.\n");
7515
7516 /* Restore device state from PCI config space */
7517 pci_set_power_state(pdev, PCI_D0);
7518 pci_restore_state(pdev);
7519 if (pdev->is_busmaster)
7520 pci_set_master(pdev);
7521
7522 /* Startup the kernel thread for this host adapter. */
7523 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7524 "lpfc_worker_%d", phba->brd_no);
7525 if (IS_ERR(phba->worker_thread)) {
7526 error = PTR_ERR(phba->worker_thread);
7527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7528 "0293 PM resume failed to start worker "
7529 "thread: error=x%x.\n", error);
7530 return error;
7531 }
7532
7533 /* Configure and enable interrupt */
7534 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
7535 if (intr_mode == LPFC_INTR_ERROR) {
7536 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7537 "0294 PM resume Failed to enable interrupt\n");
7538 return -EIO;
7539 } else
7540 phba->intr_mode = intr_mode;
7541
7542 /* Restart HBA and bring it online */
7543 lpfc_sli_brdrestart(phba);
7544 lpfc_online(phba);
7545
7546 /* Log the current active interrupt mode */
7547 lpfc_log_intr_mode(phba, phba->intr_mode);
7548
7549 return 0;
7550}
7551
7552/**
7553 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
7554 * @pdev: pointer to PCI device.
7555 * @state: the current PCI connection state.
7556 *
7557 * This routine is called from the PCI subsystem for error handling to device
7558 * with SLI-4 interface spec. This function is called by the PCI subsystem
7559 * after a PCI bus error affecting this device has been detected. When this
7560 * function is invoked, it will need to stop all the I/Os and interrupt(s)
7561 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
7562 * for the PCI subsystem to perform proper recovery as desired.
7563 *
7564 * Return codes
7565 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7566 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7567 **/
7568static pci_ers_result_t
7569lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
7570{
7571 return PCI_ERS_RESULT_NEED_RESET;
7572}
7573
7574/**
7575 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
7576 * @pdev: pointer to PCI device.
7577 *
7578 * This routine is called from the PCI subsystem for error handling to device
7579 * with SLI-4 interface spec. It is called after PCI bus has been reset to
7580 * restart the PCI card from scratch, as if from a cold-boot. During the
7581 * PCI subsystem error recovery, after the driver returns
7582 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7583 * recovery and then call this routine before calling the .resume method to
7584 * recover the device. This function will initialize the HBA device, enable
7585 * the interrupt, but it will just put the HBA to offline state without
7586 * passing any I/O traffic.
7587 *
7588 * Return codes
7589 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7590 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7591 */
7592static pci_ers_result_t
7593lpfc_io_slot_reset_s4(struct pci_dev *pdev)
7594{
7595 return PCI_ERS_RESULT_RECOVERED;
7596}
7597
7598/**
7599 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
7600 * @pdev: pointer to PCI device
7601 *
7602 * This routine is called from the PCI subsystem for error handling to device
7603 * with SLI-4 interface spec. It is called when kernel error recovery tells
7604 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7605 * error recovery. After this call, traffic can start to flow from this device
7606 * again.
7607 **/
7608static void
7609lpfc_io_resume_s4(struct pci_dev *pdev)
7610{
7611 return;
7612}
7613
7614/**
James Smart3772a992009-05-22 14:50:54 -04007615 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
7616 * @pdev: pointer to PCI device
7617 * @pid: pointer to PCI device identifier
7618 *
7619 * This routine is to be registered to the kernel's PCI subsystem. When an
7620 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
7621 * at PCI device-specific information of the device and driver to see if the
7622 * driver state that it can support this kind of device. If the match is
7623 * successful, the driver core invokes this routine. This routine dispatches
7624 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
7625 * do all the initialization that it needs to do to handle the HBA device
7626 * properly.
7627 *
7628 * Return code
7629 * 0 - driver can claim the device
7630 * negative value - driver can not claim the device
7631 **/
7632static int __devinit
7633lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
7634{
7635 int rc;
James Smart8fa38512009-07-19 10:01:03 -04007636 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04007637
James Smart8fa38512009-07-19 10:01:03 -04007638 if (pci_read_config_dword(pdev, LPFC_SLIREV_CONF_WORD, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04007639 return -ENODEV;
7640
James Smart8fa38512009-07-19 10:01:03 -04007641 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
7642 (bf_get(lpfc_sli_intf_rev, &intf) == LPFC_SLIREV_CONF_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04007643 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007644 else
James Smart3772a992009-05-22 14:50:54 -04007645 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007646
James Smart3772a992009-05-22 14:50:54 -04007647 return rc;
7648}
7649
7650/**
7651 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
7652 * @pdev: pointer to PCI device
7653 *
7654 * This routine is to be registered to the kernel's PCI subsystem. When an
7655 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
7656 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
7657 * remove routine, which will perform all the necessary cleanup for the
7658 * device to be removed from the PCI subsystem properly.
7659 **/
7660static void __devexit
7661lpfc_pci_remove_one(struct pci_dev *pdev)
7662{
7663 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7664 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7665
7666 switch (phba->pci_dev_grp) {
7667 case LPFC_PCI_DEV_LP:
7668 lpfc_pci_remove_one_s3(pdev);
7669 break;
James Smartda0436e2009-05-22 14:51:39 -04007670 case LPFC_PCI_DEV_OC:
7671 lpfc_pci_remove_one_s4(pdev);
7672 break;
James Smart3772a992009-05-22 14:50:54 -04007673 default:
7674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7675 "1424 Invalid PCI device group: 0x%x\n",
7676 phba->pci_dev_grp);
7677 break;
7678 }
7679 return;
7680}
7681
7682/**
7683 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
7684 * @pdev: pointer to PCI device
7685 * @msg: power management message
7686 *
7687 * This routine is to be registered to the kernel's PCI subsystem to support
7688 * system Power Management (PM). When PM invokes this method, it dispatches
7689 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
7690 * suspend the device.
7691 *
7692 * Return code
7693 * 0 - driver suspended the device
7694 * Error otherwise
7695 **/
7696static int
7697lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
7698{
7699 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7700 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7701 int rc = -ENODEV;
7702
7703 switch (phba->pci_dev_grp) {
7704 case LPFC_PCI_DEV_LP:
7705 rc = lpfc_pci_suspend_one_s3(pdev, msg);
7706 break;
James Smartda0436e2009-05-22 14:51:39 -04007707 case LPFC_PCI_DEV_OC:
7708 rc = lpfc_pci_suspend_one_s4(pdev, msg);
7709 break;
James Smart3772a992009-05-22 14:50:54 -04007710 default:
7711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7712 "1425 Invalid PCI device group: 0x%x\n",
7713 phba->pci_dev_grp);
7714 break;
7715 }
7716 return rc;
7717}
7718
7719/**
7720 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
7721 * @pdev: pointer to PCI device
7722 *
7723 * This routine is to be registered to the kernel's PCI subsystem to support
7724 * system Power Management (PM). When PM invokes this method, it dispatches
7725 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
7726 * resume the device.
7727 *
7728 * Return code
7729 * 0 - driver suspended the device
7730 * Error otherwise
7731 **/
7732static int
7733lpfc_pci_resume_one(struct pci_dev *pdev)
7734{
7735 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7736 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7737 int rc = -ENODEV;
7738
7739 switch (phba->pci_dev_grp) {
7740 case LPFC_PCI_DEV_LP:
7741 rc = lpfc_pci_resume_one_s3(pdev);
7742 break;
James Smartda0436e2009-05-22 14:51:39 -04007743 case LPFC_PCI_DEV_OC:
7744 rc = lpfc_pci_resume_one_s4(pdev);
7745 break;
James Smart3772a992009-05-22 14:50:54 -04007746 default:
7747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7748 "1426 Invalid PCI device group: 0x%x\n",
7749 phba->pci_dev_grp);
7750 break;
7751 }
7752 return rc;
7753}
7754
7755/**
7756 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
7757 * @pdev: pointer to PCI device.
7758 * @state: the current PCI connection state.
7759 *
7760 * This routine is registered to the PCI subsystem for error handling. This
7761 * function is called by the PCI subsystem after a PCI bus error affecting
7762 * this device has been detected. When this routine is invoked, it dispatches
7763 * the action to the proper SLI-3 or SLI-4 device error detected handling
7764 * routine, which will perform the proper error detected operation.
7765 *
7766 * Return codes
7767 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7768 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7769 **/
7770static pci_ers_result_t
7771lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7772{
7773 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7774 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7775 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7776
7777 switch (phba->pci_dev_grp) {
7778 case LPFC_PCI_DEV_LP:
7779 rc = lpfc_io_error_detected_s3(pdev, state);
7780 break;
James Smartda0436e2009-05-22 14:51:39 -04007781 case LPFC_PCI_DEV_OC:
7782 rc = lpfc_io_error_detected_s4(pdev, state);
7783 break;
James Smart3772a992009-05-22 14:50:54 -04007784 default:
7785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7786 "1427 Invalid PCI device group: 0x%x\n",
7787 phba->pci_dev_grp);
7788 break;
7789 }
7790 return rc;
7791}
7792
7793/**
7794 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
7795 * @pdev: pointer to PCI device.
7796 *
7797 * This routine is registered to the PCI subsystem for error handling. This
7798 * function is called after PCI bus has been reset to restart the PCI card
7799 * from scratch, as if from a cold-boot. When this routine is invoked, it
7800 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
7801 * routine, which will perform the proper device reset.
7802 *
7803 * Return codes
7804 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7805 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7806 **/
7807static pci_ers_result_t
7808lpfc_io_slot_reset(struct pci_dev *pdev)
7809{
7810 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7811 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7812 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7813
7814 switch (phba->pci_dev_grp) {
7815 case LPFC_PCI_DEV_LP:
7816 rc = lpfc_io_slot_reset_s3(pdev);
7817 break;
James Smartda0436e2009-05-22 14:51:39 -04007818 case LPFC_PCI_DEV_OC:
7819 rc = lpfc_io_slot_reset_s4(pdev);
7820 break;
James Smart3772a992009-05-22 14:50:54 -04007821 default:
7822 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7823 "1428 Invalid PCI device group: 0x%x\n",
7824 phba->pci_dev_grp);
7825 break;
7826 }
7827 return rc;
7828}
7829
7830/**
7831 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
7832 * @pdev: pointer to PCI device
7833 *
7834 * This routine is registered to the PCI subsystem for error handling. It
7835 * is called when kernel error recovery tells the lpfc driver that it is
7836 * OK to resume normal PCI operation after PCI bus error recovery. When
7837 * this routine is invoked, it dispatches the action to the proper SLI-3
7838 * or SLI-4 device io_resume routine, which will resume the device operation.
7839 **/
7840static void
7841lpfc_io_resume(struct pci_dev *pdev)
7842{
7843 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7844 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7845
7846 switch (phba->pci_dev_grp) {
7847 case LPFC_PCI_DEV_LP:
7848 lpfc_io_resume_s3(pdev);
7849 break;
James Smartda0436e2009-05-22 14:51:39 -04007850 case LPFC_PCI_DEV_OC:
7851 lpfc_io_resume_s4(pdev);
7852 break;
James Smart3772a992009-05-22 14:50:54 -04007853 default:
7854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7855 "1429 Invalid PCI device group: 0x%x\n",
7856 phba->pci_dev_grp);
7857 break;
7858 }
7859 return;
7860}
7861
dea31012005-04-17 16:05:31 -05007862static struct pci_device_id lpfc_id_table[] = {
7863 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
7864 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04007865 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
7866 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007867 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
7868 PCI_ANY_ID, PCI_ANY_ID, },
7869 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
7870 PCI_ANY_ID, PCI_ANY_ID, },
7871 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
7872 PCI_ANY_ID, PCI_ANY_ID, },
7873 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
7874 PCI_ANY_ID, PCI_ANY_ID, },
7875 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
7876 PCI_ANY_ID, PCI_ANY_ID, },
7877 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
7878 PCI_ANY_ID, PCI_ANY_ID, },
7879 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
7880 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007881 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
7882 PCI_ANY_ID, PCI_ANY_ID, },
7883 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
7884 PCI_ANY_ID, PCI_ANY_ID, },
7885 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
7886 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007887 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
7888 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007889 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
7890 PCI_ANY_ID, PCI_ANY_ID, },
7891 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
7892 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007893 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
7894 PCI_ANY_ID, PCI_ANY_ID, },
7895 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
7896 PCI_ANY_ID, PCI_ANY_ID, },
7897 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
7898 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04007899 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
7900 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007901 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
7902 PCI_ANY_ID, PCI_ANY_ID, },
7903 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
7904 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007905 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
7906 PCI_ANY_ID, PCI_ANY_ID, },
7907 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
7908 PCI_ANY_ID, PCI_ANY_ID, },
7909 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
7910 PCI_ANY_ID, PCI_ANY_ID, },
7911 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
7912 PCI_ANY_ID, PCI_ANY_ID, },
7913 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
7914 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007915 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
7916 PCI_ANY_ID, PCI_ANY_ID, },
7917 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
7918 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04007919 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
7920 PCI_ANY_ID, PCI_ANY_ID, },
7921 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
7922 PCI_ANY_ID, PCI_ANY_ID, },
7923 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
7924 PCI_ANY_ID, PCI_ANY_ID, },
7925 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
7926 PCI_ANY_ID, PCI_ANY_ID, },
7927 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
7928 PCI_ANY_ID, PCI_ANY_ID, },
7929 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
7930 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04007931 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
7932 PCI_ANY_ID, PCI_ANY_ID, },
7933 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
7934 PCI_ANY_ID, PCI_ANY_ID, },
7935 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
7936 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04007937 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
7938 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007939 { 0 }
7940};
7941
7942MODULE_DEVICE_TABLE(pci, lpfc_id_table);
7943
Linas Vepstas8d63f372007-02-14 14:28:36 -06007944static struct pci_error_handlers lpfc_err_handler = {
7945 .error_detected = lpfc_io_error_detected,
7946 .slot_reset = lpfc_io_slot_reset,
7947 .resume = lpfc_io_resume,
7948};
7949
dea31012005-04-17 16:05:31 -05007950static struct pci_driver lpfc_driver = {
7951 .name = LPFC_DRIVER_NAME,
7952 .id_table = lpfc_id_table,
7953 .probe = lpfc_pci_probe_one,
7954 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05007955 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04007956 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05007957 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05007958};
7959
James Smarte59058c2008-08-24 21:49:00 -04007960/**
James Smart3621a712009-04-06 18:47:14 -04007961 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04007962 *
7963 * This routine is to be invoked when the lpfc module is loaded into the
7964 * kernel. The special kernel macro module_init() is used to indicate the
7965 * role of this routine to the kernel as lpfc module entry point.
7966 *
7967 * Return codes
7968 * 0 - successful
7969 * -ENOMEM - FC attach transport failed
7970 * all others - failed
7971 */
dea31012005-04-17 16:05:31 -05007972static int __init
7973lpfc_init(void)
7974{
7975 int error = 0;
7976
7977 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007978 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05007979
James Smart7ee5d432007-10-27 13:37:17 -04007980 if (lpfc_enable_npiv) {
7981 lpfc_transport_functions.vport_create = lpfc_vport_create;
7982 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
7983 }
dea31012005-04-17 16:05:31 -05007984 lpfc_transport_template =
7985 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04007986 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05007987 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04007988 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04007989 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04007990 fc_attach_transport(&lpfc_vport_transport_functions);
7991 if (lpfc_vport_transport_template == NULL) {
7992 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04007993 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04007994 }
James Smart7ee5d432007-10-27 13:37:17 -04007995 }
dea31012005-04-17 16:05:31 -05007996 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05007997 if (error) {
dea31012005-04-17 16:05:31 -05007998 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04007999 if (lpfc_enable_npiv)
8000 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008001 }
dea31012005-04-17 16:05:31 -05008002
8003 return error;
8004}
8005
James Smarte59058c2008-08-24 21:49:00 -04008006/**
James Smart3621a712009-04-06 18:47:14 -04008007 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008008 *
8009 * This routine is invoked when the lpfc module is removed from the kernel.
8010 * The special kernel macro module_exit() is used to indicate the role of
8011 * this routine to the kernel as lpfc module exit point.
8012 */
dea31012005-04-17 16:05:31 -05008013static void __exit
8014lpfc_exit(void)
8015{
8016 pci_unregister_driver(&lpfc_driver);
8017 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008018 if (lpfc_enable_npiv)
8019 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008020 if (_dump_buf_data) {
8021 printk(KERN_ERR "BLKGRD freeing %lu pages for _dump_buf_data "
8022 "at 0x%p\n",
8023 (1L << _dump_buf_data_order), _dump_buf_data);
8024 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8025 }
8026
8027 if (_dump_buf_dif) {
8028 printk(KERN_ERR "BLKGRD freeing %lu pages for _dump_buf_dif "
8029 "at 0x%p\n",
8030 (1L << _dump_buf_dif_order), _dump_buf_dif);
8031 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8032 }
dea31012005-04-17 16:05:31 -05008033}
8034
8035module_init(lpfc_init);
8036module_exit(lpfc_exit);
8037MODULE_LICENSE("GPL");
8038MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8039MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8040MODULE_VERSION("0:" LPFC_DRIVER_VERSION);