blob: 900b5628ceb9ee3916d77bd8c71f6eecdb0b1575 [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 /* Check if the port is disabled */
531 lpfc_sli_read_serdes_param(phba);
532
533 if (phba->hba_flag & LINK_DISABLED) {
534 lpfc_printf_log(phba,
535 KERN_ERR, LOG_INIT,
536 "2598 Adapter Link is disabled.\n");
537 lpfc_down_link(phba, pmb);
538 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
539 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
540 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
541 lpfc_printf_log(phba,
542 KERN_ERR, LOG_INIT,
543 "2599 Adapter failed to issue DOWN_LINK"
544 " mbox command rc 0x%x\n", rc);
545
546 mempool_free(pmb, phba->mbox_mem_pool);
547 return -EIO;
548 }
549 } else {
550 lpfc_init_link(phba, pmb, phba->cfg_topology,
551 phba->cfg_link_speed);
552 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
553 lpfc_set_loopback_flag(phba);
554 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
555 if (rc != MBX_SUCCESS) {
556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400557 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500558 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500559 mb->mbxCommand, mb->mbxStatus);
560
James Smarta0c87cb2009-07-19 10:01:10 -0400561 /* Clear all interrupt enable conditions */
562 writel(0, phba->HCregaddr);
563 readl(phba->HCregaddr); /* flush */
564 /* Clear all pending interrupts */
565 writel(0xffffffff, phba->HAregaddr);
566 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500567
James Smarta0c87cb2009-07-19 10:01:10 -0400568 phba->link_state = LPFC_HBA_ERROR;
569 if (rc != MBX_BUSY)
570 mempool_free(pmb, phba->mbox_mem_pool);
571 return -EIO;
572 }
dea31012005-04-17 16:05:31 -0500573 }
574 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400575 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
576 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
577 pmb->mbox_cmpl = lpfc_config_async_cmpl;
578 pmb->vport = phba->pport;
579 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500580
James Smart57127f12007-10-27 13:37:05 -0400581 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
582 lpfc_printf_log(phba,
583 KERN_ERR,
584 LOG_INIT,
585 "0456 Adapter failed to issue "
586 "ASYNCEVT_ENABLE mbox status x%x \n.",
587 rc);
588 mempool_free(pmb, phba->mbox_mem_pool);
589 }
James Smart97207482008-12-04 22:39:19 -0500590
591 /* Get Option rom version */
592 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
593 lpfc_dump_wakeup_param(phba, pmb);
594 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
595 pmb->vport = phba->pport;
596 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
597
598 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
600 "to get Option ROM version status x%x\n.", rc);
601 mempool_free(pmb, phba->mbox_mem_pool);
602 }
603
James Smartd7c255b2008-08-24 21:50:00 -0400604 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400605}
606
James Smarte59058c2008-08-24 21:49:00 -0400607/**
James Smart3621a712009-04-06 18:47:14 -0400608 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400609 * @phba: pointer to lpfc HBA data structure.
610 *
611 * This routine will do LPFC uninitialization before the HBA is reset when
612 * bringing down the SLI Layer.
613 *
614 * Return codes
615 * 0 - success.
616 * Any other value - error.
617 **/
dea31012005-04-17 16:05:31 -0500618int
James Smart2e0fef82007-06-17 19:56:36 -0500619lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500620{
James Smart1b32f6a2008-02-08 18:49:39 -0500621 struct lpfc_vport **vports;
622 int i;
James Smart3772a992009-05-22 14:50:54 -0400623
624 if (phba->sli_rev <= LPFC_SLI_REV3) {
625 /* Disable interrupts */
626 writel(0, phba->HCregaddr);
627 readl(phba->HCregaddr); /* flush */
628 }
dea31012005-04-17 16:05:31 -0500629
James Smart1b32f6a2008-02-08 18:49:39 -0500630 if (phba->pport->load_flag & FC_UNLOADING)
631 lpfc_cleanup_discovery_resources(phba->pport);
632 else {
633 vports = lpfc_create_vport_work_array(phba);
634 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400635 for (i = 0; i <= phba->max_vports &&
636 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500637 lpfc_cleanup_discovery_resources(vports[i]);
638 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500639 }
640 return 0;
dea31012005-04-17 16:05:31 -0500641}
642
James Smarte59058c2008-08-24 21:49:00 -0400643/**
James Smart3772a992009-05-22 14:50:54 -0400644 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400645 * @phba: pointer to lpfc HBA data structure.
646 *
647 * This routine will do uninitialization after the HBA is reset when bring
648 * down the SLI Layer.
649 *
650 * Return codes
651 * 0 - sucess.
652 * Any other value - error.
653 **/
James Smart3772a992009-05-22 14:50:54 -0400654static int
655lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500656{
657 struct lpfc_sli *psli = &phba->sli;
658 struct lpfc_sli_ring *pring;
659 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500660 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500661 int i;
662
James Smart92d7f7b2007-06-17 19:56:38 -0500663 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
664 lpfc_sli_hbqbuf_free_all(phba);
665 else {
666 /* Cleanup preposted buffers on the ELS ring */
667 pring = &psli->ring[LPFC_ELS_RING];
668 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
669 list_del(&mp->list);
670 pring->postbufq_cnt--;
671 lpfc_mbuf_free(phba, mp->virt, mp->phys);
672 kfree(mp);
673 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500674 }
675
James Smart09372822008-01-11 01:52:54 -0500676 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500677 for (i = 0; i < psli->num_rings; i++) {
678 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500679
680 /* At this point in time the HBA is either reset or DOA. Either
681 * way, nothing should be on txcmplq as it will NEVER complete.
682 */
683 list_splice_init(&pring->txcmplq, &completions);
684 pring->txcmplq_cnt = 0;
685 spin_unlock_irq(&phba->hbalock);
686
James Smarta257bf92009-04-06 18:48:10 -0400687 /* Cancel all the IOCBs from the completions list */
688 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
689 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500690
Jamie Wellnitz41415862006-02-28 19:25:27 -0500691 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500692 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500693 }
James Smart09372822008-01-11 01:52:54 -0500694 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500695
696 return 0;
697}
James Smartda0436e2009-05-22 14:51:39 -0400698/**
699 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
700 * @phba: pointer to lpfc HBA data structure.
701 *
702 * This routine will do uninitialization after the HBA is reset when bring
703 * down the SLI Layer.
704 *
705 * Return codes
706 * 0 - sucess.
707 * Any other value - error.
708 **/
709static int
710lpfc_hba_down_post_s4(struct lpfc_hba *phba)
711{
712 struct lpfc_scsi_buf *psb, *psb_next;
713 LIST_HEAD(aborts);
714 int ret;
715 unsigned long iflag = 0;
716 ret = lpfc_hba_down_post_s3(phba);
717 if (ret)
718 return ret;
719 /* At this point in time the HBA is either reset or DOA. Either
720 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
721 * on the lpfc_sgl_list so that it can either be freed if the
722 * driver is unloading or reposted if the driver is restarting
723 * the port.
724 */
725 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
726 /* scsl_buf_list */
727 /* abts_sgl_list_lock required because worker thread uses this
728 * list.
729 */
730 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
731 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
732 &phba->sli4_hba.lpfc_sgl_list);
733 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
734 /* abts_scsi_buf_list_lock required because worker thread uses this
735 * list.
736 */
737 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
738 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
739 &aborts);
740 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
741 spin_unlock_irq(&phba->hbalock);
742
743 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
744 psb->pCmd = NULL;
745 psb->status = IOSTAT_SUCCESS;
746 }
747 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
748 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
749 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
750 return 0;
751}
752
753/**
754 * lpfc_hba_down_post - Wrapper func for hba down post routine
755 * @phba: pointer to lpfc HBA data structure.
756 *
757 * This routine wraps the actual SLI3 or SLI4 routine for performing
758 * uninitialization after the HBA is reset when bring down the SLI Layer.
759 *
760 * Return codes
761 * 0 - sucess.
762 * Any other value - error.
763 **/
764int
765lpfc_hba_down_post(struct lpfc_hba *phba)
766{
767 return (*phba->lpfc_hba_down_post)(phba);
768}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500769
James Smarte59058c2008-08-24 21:49:00 -0400770/**
James Smart3621a712009-04-06 18:47:14 -0400771 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400772 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
773 *
774 * This is the HBA-timer timeout handler registered to the lpfc driver. When
775 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
776 * work-port-events bitmap and the worker thread is notified. This timeout
777 * event will be used by the worker thread to invoke the actual timeout
778 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
779 * be performed in the timeout handler and the HBA timeout event bit shall
780 * be cleared by the worker thread after it has taken the event bitmap out.
781 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100782static void
James Smart858c9f62007-06-17 19:56:39 -0500783lpfc_hb_timeout(unsigned long ptr)
784{
785 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400786 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500787 unsigned long iflag;
788
789 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400790
791 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500792 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400793 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
794 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500795 phba->pport->work_port_events |= WORKER_HB_TMO;
796 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
797
James Smart93996272008-08-24 21:50:30 -0400798 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400799 if (!tmo_posted)
800 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500801 return;
802}
803
James Smarte59058c2008-08-24 21:49:00 -0400804/**
James Smart3621a712009-04-06 18:47:14 -0400805 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400806 * @phba: pointer to lpfc hba data structure.
807 * @pmboxq: pointer to the driver internal queue element for mailbox command.
808 *
809 * This is the callback function to the lpfc heart-beat mailbox command.
810 * If configured, the lpfc driver issues the heart-beat mailbox command to
811 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
812 * heart-beat mailbox command is issued, the driver shall set up heart-beat
813 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
814 * heart-beat outstanding state. Once the mailbox command comes back and
815 * no error conditions detected, the heart-beat mailbox command timer is
816 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
817 * state is cleared for the next heart-beat. If the timer expired with the
818 * heart-beat outstanding state set, the driver will put the HBA offline.
819 **/
James Smart858c9f62007-06-17 19:56:39 -0500820static void
821lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
822{
823 unsigned long drvr_flag;
824
825 spin_lock_irqsave(&phba->hbalock, drvr_flag);
826 phba->hb_outstanding = 0;
827 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
828
James Smart93996272008-08-24 21:50:30 -0400829 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500830 mempool_free(pmboxq, phba->mbox_mem_pool);
831 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
832 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400833 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500834 mod_timer(&phba->hb_tmofunc,
835 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
836 return;
837}
838
James Smarte59058c2008-08-24 21:49:00 -0400839/**
James Smart3621a712009-04-06 18:47:14 -0400840 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400841 * @phba: pointer to lpfc hba data structure.
842 *
843 * This is the actual HBA-timer timeout handler to be invoked by the worker
844 * thread whenever the HBA timer fired and HBA-timeout event posted. This
845 * handler performs any periodic operations needed for the device. If such
846 * periodic event has already been attended to either in the interrupt handler
847 * or by processing slow-ring or fast-ring events within the HBA-timer
848 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
849 * the timer for the next timeout period. If lpfc heart-beat mailbox command
850 * is configured and there is no heart-beat mailbox command outstanding, a
851 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
852 * has been a heart-beat mailbox command outstanding, the HBA shall be put
853 * to offline.
854 **/
James Smart858c9f62007-06-17 19:56:39 -0500855void
856lpfc_hb_timeout_handler(struct lpfc_hba *phba)
857{
858 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500859 struct lpfc_dmabuf *buf_ptr;
James Smart858c9f62007-06-17 19:56:39 -0500860 int retval;
861 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500862 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500863
864 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400865 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500866 (phba->pport->fc_flag & FC_OFFLINE_MODE))
867 return;
868
869 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -0500870
871 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
872 jiffies)) {
873 spin_unlock_irq(&phba->pport->work_port_lock);
874 if (!phba->hb_outstanding)
875 mod_timer(&phba->hb_tmofunc,
876 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
877 else
878 mod_timer(&phba->hb_tmofunc,
879 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
880 return;
881 }
882 spin_unlock_irq(&phba->pport->work_port_lock);
883
James Smart0ff10d42008-01-11 01:52:36 -0500884 if (phba->elsbuf_cnt &&
885 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
886 spin_lock_irq(&phba->hbalock);
887 list_splice_init(&phba->elsbuf, &completions);
888 phba->elsbuf_cnt = 0;
889 phba->elsbuf_prev_cnt = 0;
890 spin_unlock_irq(&phba->hbalock);
891
892 while (!list_empty(&completions)) {
893 list_remove_head(&completions, buf_ptr,
894 struct lpfc_dmabuf, list);
895 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
896 kfree(buf_ptr);
897 }
898 }
899 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
900
James Smart858c9f62007-06-17 19:56:39 -0500901 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -0500902 if (phba->cfg_enable_hba_heartbeat) {
903 if (!phba->hb_outstanding) {
904 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
905 if (!pmboxq) {
906 mod_timer(&phba->hb_tmofunc,
907 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
908 return;
909 }
910
911 lpfc_heart_beat(phba, pmboxq);
912 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
913 pmboxq->vport = phba->pport;
914 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
915
916 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
917 mempool_free(pmboxq, phba->mbox_mem_pool);
918 mod_timer(&phba->hb_tmofunc,
919 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
920 return;
921 }
James Smart858c9f62007-06-17 19:56:39 -0500922 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -0500923 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
924 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -0500925 return;
James Smart13815c82008-01-11 01:52:48 -0500926 } else {
927 /*
928 * If heart beat timeout called with hb_outstanding set
929 * we need to take the HBA offline.
930 */
931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
932 "0459 Adapter heartbeat failure, "
933 "taking this port offline.\n");
934
935 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400936 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart13815c82008-01-11 01:52:48 -0500937 spin_unlock_irq(&phba->hbalock);
938
939 lpfc_offline_prep(phba);
940 lpfc_offline(phba);
941 lpfc_unblock_mgmt_io(phba);
942 phba->link_state = LPFC_HBA_ERROR;
943 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -0500944 }
James Smart858c9f62007-06-17 19:56:39 -0500945 }
946}
947
James Smarte59058c2008-08-24 21:49:00 -0400948/**
James Smart3621a712009-04-06 18:47:14 -0400949 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -0400950 * @phba: pointer to lpfc hba data structure.
951 *
952 * This routine is called to bring the HBA offline when HBA hardware error
953 * other than Port Error 6 has been detected.
954 **/
James Smart09372822008-01-11 01:52:54 -0500955static void
956lpfc_offline_eratt(struct lpfc_hba *phba)
957{
958 struct lpfc_sli *psli = &phba->sli;
959
960 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400961 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -0500962 spin_unlock_irq(&phba->hbalock);
963 lpfc_offline_prep(phba);
964
965 lpfc_offline(phba);
966 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400967 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500968 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400969 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500970 lpfc_hba_down_post(phba);
971 lpfc_sli_brdready(phba, HS_MBRDY);
972 lpfc_unblock_mgmt_io(phba);
973 phba->link_state = LPFC_HBA_ERROR;
974 return;
975}
976
James Smarte59058c2008-08-24 21:49:00 -0400977/**
James Smartda0436e2009-05-22 14:51:39 -0400978 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
979 * @phba: pointer to lpfc hba data structure.
980 *
981 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
982 * other than Port Error 6 has been detected.
983 **/
984static void
985lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
986{
987 lpfc_offline_prep(phba);
988 lpfc_offline(phba);
989 lpfc_sli4_brdreset(phba);
990 lpfc_hba_down_post(phba);
991 lpfc_sli4_post_status_check(phba);
992 lpfc_unblock_mgmt_io(phba);
993 phba->link_state = LPFC_HBA_ERROR;
994}
995
996/**
James Smarta257bf92009-04-06 18:48:10 -0400997 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
998 * @phba: pointer to lpfc hba data structure.
999 *
1000 * This routine is invoked to handle the deferred HBA hardware error
1001 * conditions. This type of error is indicated by HBA by setting ER1
1002 * and another ER bit in the host status register. The driver will
1003 * wait until the ER1 bit clears before handling the error condition.
1004 **/
1005static void
1006lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1007{
1008 uint32_t old_host_status = phba->work_hs;
1009 struct lpfc_sli_ring *pring;
1010 struct lpfc_sli *psli = &phba->sli;
1011
James Smartf4b4c682009-05-22 14:53:12 -04001012 /* If the pci channel is offline, ignore possible errors,
1013 * since we cannot communicate with the pci card anyway.
1014 */
1015 if (pci_channel_offline(phba->pcidev)) {
1016 spin_lock_irq(&phba->hbalock);
1017 phba->hba_flag &= ~DEFER_ERATT;
1018 spin_unlock_irq(&phba->hbalock);
1019 return;
1020 }
1021
James Smarta257bf92009-04-06 18:48:10 -04001022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1023 "0479 Deferred Adapter Hardware Error "
1024 "Data: x%x x%x x%x\n",
1025 phba->work_hs,
1026 phba->work_status[0], phba->work_status[1]);
1027
1028 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001029 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001030 spin_unlock_irq(&phba->hbalock);
1031
1032
1033 /*
1034 * Firmware stops when it triggred erratt. That could cause the I/Os
1035 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1036 * SCSI layer retry it after re-establishing link.
1037 */
1038 pring = &psli->ring[psli->fcp_ring];
1039 lpfc_sli_abort_iocb_ring(phba, pring);
1040
1041 /*
1042 * There was a firmware error. Take the hba offline and then
1043 * attempt to restart it.
1044 */
1045 lpfc_offline_prep(phba);
1046 lpfc_offline(phba);
1047
1048 /* Wait for the ER1 bit to clear.*/
1049 while (phba->work_hs & HS_FFER1) {
1050 msleep(100);
1051 phba->work_hs = readl(phba->HSregaddr);
1052 /* If driver is unloading let the worker thread continue */
1053 if (phba->pport->load_flag & FC_UNLOADING) {
1054 phba->work_hs = 0;
1055 break;
1056 }
1057 }
1058
1059 /*
1060 * This is to ptrotect against a race condition in which
1061 * first write to the host attention register clear the
1062 * host status register.
1063 */
1064 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1065 phba->work_hs = old_host_status & ~HS_FFER1;
1066
James Smart3772a992009-05-22 14:50:54 -04001067 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001068 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001069 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001070 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1071 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1072}
1073
James Smart3772a992009-05-22 14:50:54 -04001074static void
1075lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1076{
1077 struct lpfc_board_event_header board_event;
1078 struct Scsi_Host *shost;
1079
1080 board_event.event_type = FC_REG_BOARD_EVENT;
1081 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1082 shost = lpfc_shost_from_vport(phba->pport);
1083 fc_host_post_vendor_event(shost, fc_get_event_number(),
1084 sizeof(board_event),
1085 (char *) &board_event,
1086 LPFC_NL_VENDOR_ID);
1087}
1088
James Smarta257bf92009-04-06 18:48:10 -04001089/**
James Smart3772a992009-05-22 14:50:54 -04001090 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001091 * @phba: pointer to lpfc hba data structure.
1092 *
1093 * This routine is invoked to handle the following HBA hardware error
1094 * conditions:
1095 * 1 - HBA error attention interrupt
1096 * 2 - DMA ring index out of range
1097 * 3 - Mailbox command came back as unknown
1098 **/
James Smart3772a992009-05-22 14:50:54 -04001099static void
1100lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001101{
James Smart2e0fef82007-06-17 19:56:36 -05001102 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001103 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001104 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001105 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001106 unsigned long temperature;
1107 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001108 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001109
Linas Vepstas8d63f372007-02-14 14:28:36 -06001110 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001111 * since we cannot communicate with the pci card anyway.
1112 */
1113 if (pci_channel_offline(phba->pcidev)) {
1114 spin_lock_irq(&phba->hbalock);
1115 phba->hba_flag &= ~DEFER_ERATT;
1116 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001117 return;
James Smart3772a992009-05-22 14:50:54 -04001118 }
1119
James Smart13815c82008-01-11 01:52:48 -05001120 /* If resets are disabled then leave the HBA alone and return */
1121 if (!phba->cfg_enable_hba_reset)
1122 return;
dea31012005-04-17 16:05:31 -05001123
James Smartea2151b2008-09-07 11:52:10 -04001124 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001125 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001126
James Smarta257bf92009-04-06 18:48:10 -04001127 if (phba->hba_flag & DEFER_ERATT)
1128 lpfc_handle_deferred_eratt(phba);
1129
James Smart97eab632008-04-07 10:16:05 -04001130 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -05001131 /* Re-establishing Link */
1132 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001133 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -05001134 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001135 phba->work_hs,
dea31012005-04-17 16:05:31 -05001136 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001137
James Smart92d7f7b2007-06-17 19:56:38 -05001138 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001139 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001140 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001141
1142 /*
1143 * Firmware stops when it triggled erratt with HS_FFER6.
1144 * That could cause the I/Os dropped by the firmware.
1145 * Error iocb (I/O) on txcmplq and let the SCSI layer
1146 * retry it after re-establishing link.
1147 */
1148 pring = &psli->ring[psli->fcp_ring];
1149 lpfc_sli_abort_iocb_ring(phba, pring);
1150
dea31012005-04-17 16:05:31 -05001151 /*
1152 * There was a firmware error. Take the hba offline and then
1153 * attempt to restart it.
1154 */
James Smart46fa3112007-04-25 09:51:45 -04001155 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001156 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001157 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001158 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001159 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001160 return;
1161 }
James Smart46fa3112007-04-25 09:51:45 -04001162 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001163 } else if (phba->work_hs & HS_CRIT_TEMP) {
1164 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1165 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1166 temp_event_data.event_code = LPFC_CRIT_TEMP;
1167 temp_event_data.data = (uint32_t)temperature;
1168
1169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001170 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001171 "(%ld), taking this port offline "
1172 "Data: x%x x%x x%x\n",
1173 temperature, phba->work_hs,
1174 phba->work_status[0], phba->work_status[1]);
1175
1176 shost = lpfc_shost_from_vport(phba->pport);
1177 fc_host_post_vendor_event(shost, fc_get_event_number(),
1178 sizeof(temp_event_data),
1179 (char *) &temp_event_data,
1180 SCSI_NL_VID_TYPE_PCI
1181 | PCI_VENDOR_ID_EMULEX);
1182
James Smart7af67052007-10-27 13:38:11 -04001183 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001184 phba->over_temp_state = HBA_OVER_TEMP;
1185 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001186 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001187
dea31012005-04-17 16:05:31 -05001188 } else {
1189 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001190 * failure is a value other than FFER6. Do not call the offline
1191 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001192 */
1193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001194 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001195 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001196 phba->work_hs,
dea31012005-04-17 16:05:31 -05001197 phba->work_status[0], phba->work_status[1]);
1198
James Smartd2873e42006-08-18 17:46:43 -04001199 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001200 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001201 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001202 sizeof(event_data), (char *) &event_data,
1203 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1204
James Smart09372822008-01-11 01:52:54 -05001205 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001206 }
James Smart93996272008-08-24 21:50:30 -04001207 return;
dea31012005-04-17 16:05:31 -05001208}
1209
James Smarte59058c2008-08-24 21:49:00 -04001210/**
James Smartda0436e2009-05-22 14:51:39 -04001211 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1212 * @phba: pointer to lpfc hba data structure.
1213 *
1214 * This routine is invoked to handle the SLI4 HBA hardware error attention
1215 * conditions.
1216 **/
1217static void
1218lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1219{
1220 struct lpfc_vport *vport = phba->pport;
1221 uint32_t event_data;
1222 struct Scsi_Host *shost;
1223
1224 /* If the pci channel is offline, ignore possible errors, since
1225 * we cannot communicate with the pci card anyway.
1226 */
1227 if (pci_channel_offline(phba->pcidev))
1228 return;
1229 /* If resets are disabled then leave the HBA alone and return */
1230 if (!phba->cfg_enable_hba_reset)
1231 return;
1232
1233 /* Send an internal error event to mgmt application */
1234 lpfc_board_errevt_to_mgmt(phba);
1235
1236 /* For now, the actual action for SLI4 device handling is not
1237 * specified yet, just treated it as adaptor hardware failure
1238 */
1239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1240 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1241 phba->work_status[0], phba->work_status[1]);
1242
1243 event_data = FC_REG_DUMP_EVENT;
1244 shost = lpfc_shost_from_vport(vport);
1245 fc_host_post_vendor_event(shost, fc_get_event_number(),
1246 sizeof(event_data), (char *) &event_data,
1247 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1248
1249 lpfc_sli4_offline_eratt(phba);
1250}
1251
1252/**
1253 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1254 * @phba: pointer to lpfc HBA data structure.
1255 *
1256 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1257 * routine from the API jump table function pointer from the lpfc_hba struct.
1258 *
1259 * Return codes
1260 * 0 - sucess.
1261 * Any other value - error.
1262 **/
1263void
1264lpfc_handle_eratt(struct lpfc_hba *phba)
1265{
1266 (*phba->lpfc_handle_eratt)(phba);
1267}
1268
1269/**
James Smart3621a712009-04-06 18:47:14 -04001270 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001271 * @phba: pointer to lpfc hba data structure.
1272 *
1273 * This routine is invoked from the worker thread to handle a HBA host
1274 * attention link event.
1275 **/
dea31012005-04-17 16:05:31 -05001276void
James Smart2e0fef82007-06-17 19:56:36 -05001277lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001278{
James Smart2e0fef82007-06-17 19:56:36 -05001279 struct lpfc_vport *vport = phba->pport;
1280 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001281 LPFC_MBOXQ_t *pmb;
1282 volatile uint32_t control;
1283 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001284 int rc = 0;
dea31012005-04-17 16:05:31 -05001285
1286 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001287 if (!pmb) {
1288 rc = 1;
dea31012005-04-17 16:05:31 -05001289 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001290 }
dea31012005-04-17 16:05:31 -05001291
1292 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001293 if (!mp) {
1294 rc = 2;
dea31012005-04-17 16:05:31 -05001295 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001296 }
dea31012005-04-17 16:05:31 -05001297
1298 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001299 if (!mp->virt) {
1300 rc = 3;
dea31012005-04-17 16:05:31 -05001301 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001302 }
dea31012005-04-17 16:05:31 -05001303
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001304 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001305 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001306
1307 psli->slistat.link_event++;
1308 lpfc_read_la(phba, pmb, mp);
1309 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001310 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001311 /* Block ELS IOCBs until we have processed this mbox command */
1312 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001313 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001314 if (rc == MBX_NOT_FINISHED) {
1315 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001316 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001317 }
dea31012005-04-17 16:05:31 -05001318
1319 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001320 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001321 writel(HA_LATT, phba->HAregaddr);
1322 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001323 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001324
1325 return;
1326
James Smart14691152006-12-02 13:34:28 -05001327lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001328 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001329 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001330lpfc_handle_latt_free_mp:
1331 kfree(mp);
1332lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001333 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001334lpfc_handle_latt_err_exit:
1335 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001336 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001337 psli->sli_flag |= LPFC_PROCESS_LA;
1338 control = readl(phba->HCregaddr);
1339 control |= HC_LAINT_ENA;
1340 writel(control, phba->HCregaddr);
1341 readl(phba->HCregaddr); /* flush */
1342
1343 /* Clear Link Attention in HA REG */
1344 writel(HA_LATT, phba->HAregaddr);
1345 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001346 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001347 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001348 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001349
James Smart09372822008-01-11 01:52:54 -05001350 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1351 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001352
1353 return;
1354}
1355
James Smarte59058c2008-08-24 21:49:00 -04001356/**
James Smart3621a712009-04-06 18:47:14 -04001357 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001358 * @phba: pointer to lpfc hba data structure.
1359 * @vpd: pointer to the vital product data.
1360 * @len: length of the vital product data in bytes.
1361 *
1362 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1363 * an array of characters. In this routine, the ModelName, ProgramType, and
1364 * ModelDesc, etc. fields of the phba data structure will be populated.
1365 *
1366 * Return codes
1367 * 0 - pointer to the VPD passed in is NULL
1368 * 1 - success
1369 **/
James Smart3772a992009-05-22 14:50:54 -04001370int
James Smart2e0fef82007-06-17 19:56:36 -05001371lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001372{
1373 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001374 int Length;
dea31012005-04-17 16:05:31 -05001375 int i, j;
1376 int finished = 0;
1377 int index = 0;
1378
1379 if (!vpd)
1380 return 0;
1381
1382 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001383 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001384 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001385 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1386 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001387 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001388 switch (vpd[index]) {
1389 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001390 case 0x91:
dea31012005-04-17 16:05:31 -05001391 index += 1;
1392 lenlo = vpd[index];
1393 index += 1;
1394 lenhi = vpd[index];
1395 index += 1;
1396 i = ((((unsigned short)lenhi) << 8) + lenlo);
1397 index += i;
1398 break;
1399 case 0x90:
1400 index += 1;
1401 lenlo = vpd[index];
1402 index += 1;
1403 lenhi = vpd[index];
1404 index += 1;
1405 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001406 if (Length > len - index)
1407 Length = len - index;
dea31012005-04-17 16:05:31 -05001408 while (Length > 0) {
1409 /* Look for Serial Number */
1410 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1411 index += 2;
1412 i = vpd[index];
1413 index += 1;
1414 j = 0;
1415 Length -= (3+i);
1416 while(i--) {
1417 phba->SerialNumber[j++] = vpd[index++];
1418 if (j == 31)
1419 break;
1420 }
1421 phba->SerialNumber[j] = 0;
1422 continue;
1423 }
1424 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1425 phba->vpd_flag |= VPD_MODEL_DESC;
1426 index += 2;
1427 i = vpd[index];
1428 index += 1;
1429 j = 0;
1430 Length -= (3+i);
1431 while(i--) {
1432 phba->ModelDesc[j++] = vpd[index++];
1433 if (j == 255)
1434 break;
1435 }
1436 phba->ModelDesc[j] = 0;
1437 continue;
1438 }
1439 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1440 phba->vpd_flag |= VPD_MODEL_NAME;
1441 index += 2;
1442 i = vpd[index];
1443 index += 1;
1444 j = 0;
1445 Length -= (3+i);
1446 while(i--) {
1447 phba->ModelName[j++] = vpd[index++];
1448 if (j == 79)
1449 break;
1450 }
1451 phba->ModelName[j] = 0;
1452 continue;
1453 }
1454 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1455 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1456 index += 2;
1457 i = vpd[index];
1458 index += 1;
1459 j = 0;
1460 Length -= (3+i);
1461 while(i--) {
1462 phba->ProgramType[j++] = vpd[index++];
1463 if (j == 255)
1464 break;
1465 }
1466 phba->ProgramType[j] = 0;
1467 continue;
1468 }
1469 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1470 phba->vpd_flag |= VPD_PORT;
1471 index += 2;
1472 i = vpd[index];
1473 index += 1;
1474 j = 0;
1475 Length -= (3+i);
1476 while(i--) {
1477 phba->Port[j++] = vpd[index++];
1478 if (j == 19)
1479 break;
1480 }
1481 phba->Port[j] = 0;
1482 continue;
1483 }
1484 else {
1485 index += 2;
1486 i = vpd[index];
1487 index += 1;
1488 index += i;
1489 Length -= (3 + i);
1490 }
1491 }
1492 finished = 0;
1493 break;
1494 case 0x78:
1495 finished = 1;
1496 break;
1497 default:
1498 index ++;
1499 break;
1500 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001501 }
dea31012005-04-17 16:05:31 -05001502
1503 return(1);
1504}
1505
James Smarte59058c2008-08-24 21:49:00 -04001506/**
James Smart3621a712009-04-06 18:47:14 -04001507 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001508 * @phba: pointer to lpfc hba data structure.
1509 * @mdp: pointer to the data structure to hold the derived model name.
1510 * @descp: pointer to the data structure to hold the derived description.
1511 *
1512 * This routine retrieves HBA's description based on its registered PCI device
1513 * ID. The @descp passed into this function points to an array of 256 chars. It
1514 * shall be returned with the model name, maximum speed, and the host bus type.
1515 * The @mdp passed into this function points to an array of 80 chars. When the
1516 * function returns, the @mdp will be filled with the model name.
1517 **/
dea31012005-04-17 16:05:31 -05001518static void
James Smart2e0fef82007-06-17 19:56:36 -05001519lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001520{
1521 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001522 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001523 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001524 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001525 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001526 struct {
1527 char * name;
1528 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001529 char * bus;
James Smart18a3b592006-12-02 13:35:08 -05001530 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001531
1532 if (mdp && mdp[0] != '\0'
1533 && descp && descp[0] != '\0')
1534 return;
1535
1536 if (phba->lmt & LMT_10Gb)
1537 max_speed = 10;
1538 else if (phba->lmt & LMT_8Gb)
1539 max_speed = 8;
1540 else if (phba->lmt & LMT_4Gb)
1541 max_speed = 4;
1542 else if (phba->lmt & LMT_2Gb)
1543 max_speed = 2;
1544 else
1545 max_speed = 1;
dea31012005-04-17 16:05:31 -05001546
1547 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001548
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001549 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001550 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -05001551 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001552 break;
dea31012005-04-17 16:05:31 -05001553 case PCI_DEVICE_ID_SUPERFLY:
1554 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -05001555 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001556 else
James Smart18a3b592006-12-02 13:35:08 -05001557 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001558 break;
1559 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -05001560 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001561 break;
1562 case PCI_DEVICE_ID_CENTAUR:
1563 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -05001564 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001565 else
James Smart18a3b592006-12-02 13:35:08 -05001566 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001567 break;
1568 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -05001569 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001570 break;
1571 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -05001572 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001573 break;
1574 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -05001575 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001576 break;
1577 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -05001578 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001579 break;
1580 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -05001581 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001582 break;
1583 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -05001584 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001585 break;
1586 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -05001587 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001588 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001589 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001590 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001591 break;
1592 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001593 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001594 break;
1595 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -05001596 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001597 break;
1598 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001599 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001600 break;
1601 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001602 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001603 break;
dea31012005-04-17 16:05:31 -05001604 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -05001605 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001606 break;
1607 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -05001608 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001609 break;
1610 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -05001611 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001612 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001613 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001614 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001615 break;
1616 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta257bf92009-04-06 18:48:10 -04001617 m = (typeof(m)){"LP2105", max_speed, "PCIe"};
1618 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001619 break;
dea31012005-04-17 16:05:31 -05001620 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -05001621 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001622 break;
1623 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -05001624 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001625 break;
1626 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -05001627 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001628 break;
1629 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -05001630 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001631 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001632 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -05001633 m = (typeof(m)){"LP11000-S", max_speed,
1634 "PCI-X2"};
1635 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001636 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -05001637 m = (typeof(m)){"LPe11000-S", max_speed,
1638 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001639 break;
James Smartb87eab32007-04-25 09:53:28 -04001640 case PCI_DEVICE_ID_SAT:
1641 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1642 break;
1643 case PCI_DEVICE_ID_SAT_MID:
1644 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1645 break;
1646 case PCI_DEVICE_ID_SAT_SMB:
1647 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1648 break;
1649 case PCI_DEVICE_ID_SAT_DCSP:
1650 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1651 break;
1652 case PCI_DEVICE_ID_SAT_SCSP:
1653 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1654 break;
1655 case PCI_DEVICE_ID_SAT_S:
1656 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1657 break;
James Smart84774a42008-08-24 21:50:06 -04001658 case PCI_DEVICE_ID_HORNET:
1659 m = (typeof(m)){"LP21000", max_speed, "PCIe"};
1660 GE = 1;
1661 break;
1662 case PCI_DEVICE_ID_PROTEUS_VF:
1663 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1664 break;
1665 case PCI_DEVICE_ID_PROTEUS_PF:
1666 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1667 break;
1668 case PCI_DEVICE_ID_PROTEUS_S:
1669 m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
1670 break;
James Smartda0436e2009-05-22 14:51:39 -04001671 case PCI_DEVICE_ID_TIGERSHARK:
1672 oneConnect = 1;
1673 m = (typeof(m)) {"OCe10100-F", max_speed, "PCIe"};
1674 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001675 default:
Randy Dunlap041976f2006-06-25 01:58:51 -07001676 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001677 break;
dea31012005-04-17 16:05:31 -05001678 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001679
1680 if (mdp && mdp[0] == '\0')
1681 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001682 /* oneConnect hba requires special processing, they are all initiators
1683 * and we put the port number on the end
1684 */
1685 if (descp && descp[0] == '\0') {
1686 if (oneConnect)
1687 snprintf(descp, 255,
1688 "Emulex OneConnect %s, FCoE Initiator, Port %s",
1689 m.name,
1690 phba->Port);
1691 else
1692 snprintf(descp, 255,
1693 "Emulex %s %d%s %s %s",
1694 m.name, m.max_speed,
1695 (GE) ? "GE" : "Gb",
1696 m.bus,
1697 (GE) ? "FCoE Adapter" :
1698 "Fibre Channel Adapter");
1699 }
dea31012005-04-17 16:05:31 -05001700}
1701
James Smarte59058c2008-08-24 21:49:00 -04001702/**
James Smart3621a712009-04-06 18:47:14 -04001703 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001704 * @phba: pointer to lpfc hba data structure.
1705 * @pring: pointer to a IOCB ring.
1706 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1707 *
1708 * This routine posts a given number of IOCBs with the associated DMA buffer
1709 * descriptors specified by the cnt argument to the given IOCB ring.
1710 *
1711 * Return codes
1712 * The number of IOCBs NOT able to be posted to the IOCB ring.
1713 **/
dea31012005-04-17 16:05:31 -05001714int
James Smart495a7142008-06-14 22:52:59 -04001715lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001716{
1717 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001718 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001719 struct lpfc_dmabuf *mp1, *mp2;
1720
1721 cnt += pring->missbufcnt;
1722
1723 /* While there are buffers to post */
1724 while (cnt > 0) {
1725 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001726 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001727 if (iocb == NULL) {
1728 pring->missbufcnt = cnt;
1729 return cnt;
1730 }
dea31012005-04-17 16:05:31 -05001731 icmd = &iocb->iocb;
1732
1733 /* 2 buffers can be posted per command */
1734 /* Allocate buffer to post */
1735 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1736 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001737 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1738 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001739 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001740 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001741 pring->missbufcnt = cnt;
1742 return cnt;
1743 }
1744
1745 INIT_LIST_HEAD(&mp1->list);
1746 /* Allocate buffer to post */
1747 if (cnt > 1) {
1748 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1749 if (mp2)
1750 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1751 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001752 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001753 kfree(mp2);
dea31012005-04-17 16:05:31 -05001754 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1755 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001756 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001757 pring->missbufcnt = cnt;
1758 return cnt;
1759 }
1760
1761 INIT_LIST_HEAD(&mp2->list);
1762 } else {
1763 mp2 = NULL;
1764 }
1765
1766 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1767 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1768 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1769 icmd->ulpBdeCount = 1;
1770 cnt--;
1771 if (mp2) {
1772 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1773 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1774 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1775 cnt--;
1776 icmd->ulpBdeCount = 2;
1777 }
1778
1779 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1780 icmd->ulpLe = 1;
1781
James Smart3772a992009-05-22 14:50:54 -04001782 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1783 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001784 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1785 kfree(mp1);
1786 cnt++;
1787 if (mp2) {
1788 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1789 kfree(mp2);
1790 cnt++;
1791 }
James Bottomley604a3e32005-10-29 10:28:33 -05001792 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001793 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001794 return cnt;
1795 }
dea31012005-04-17 16:05:31 -05001796 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001797 if (mp2)
dea31012005-04-17 16:05:31 -05001798 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001799 }
1800 pring->missbufcnt = 0;
1801 return 0;
1802}
1803
James Smarte59058c2008-08-24 21:49:00 -04001804/**
James Smart3621a712009-04-06 18:47:14 -04001805 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001806 * @phba: pointer to lpfc hba data structure.
1807 *
1808 * This routine posts initial receive IOCB buffers to the ELS ring. The
1809 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1810 * set to 64 IOCBs.
1811 *
1812 * Return codes
1813 * 0 - success (currently always success)
1814 **/
dea31012005-04-17 16:05:31 -05001815static int
James Smart2e0fef82007-06-17 19:56:36 -05001816lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001817{
1818 struct lpfc_sli *psli = &phba->sli;
1819
1820 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001821 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001822 /* Ring 2 - FCP no buffers needed */
1823
1824 return 0;
1825}
1826
1827#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1828
James Smarte59058c2008-08-24 21:49:00 -04001829/**
James Smart3621a712009-04-06 18:47:14 -04001830 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001831 * @HashResultPointer: pointer to an array as hash table.
1832 *
1833 * This routine sets up the initial values to the array of hash table entries
1834 * for the LC HBAs.
1835 **/
dea31012005-04-17 16:05:31 -05001836static void
1837lpfc_sha_init(uint32_t * HashResultPointer)
1838{
1839 HashResultPointer[0] = 0x67452301;
1840 HashResultPointer[1] = 0xEFCDAB89;
1841 HashResultPointer[2] = 0x98BADCFE;
1842 HashResultPointer[3] = 0x10325476;
1843 HashResultPointer[4] = 0xC3D2E1F0;
1844}
1845
James Smarte59058c2008-08-24 21:49:00 -04001846/**
James Smart3621a712009-04-06 18:47:14 -04001847 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001848 * @HashResultPointer: pointer to an initial/result hash table.
1849 * @HashWorkingPointer: pointer to an working hash table.
1850 *
1851 * This routine iterates an initial hash table pointed by @HashResultPointer
1852 * with the values from the working hash table pointeed by @HashWorkingPointer.
1853 * The results are putting back to the initial hash table, returned through
1854 * the @HashResultPointer as the result hash table.
1855 **/
dea31012005-04-17 16:05:31 -05001856static void
1857lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1858{
1859 int t;
1860 uint32_t TEMP;
1861 uint32_t A, B, C, D, E;
1862 t = 16;
1863 do {
1864 HashWorkingPointer[t] =
1865 S(1,
1866 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1867 8] ^
1868 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1869 } while (++t <= 79);
1870 t = 0;
1871 A = HashResultPointer[0];
1872 B = HashResultPointer[1];
1873 C = HashResultPointer[2];
1874 D = HashResultPointer[3];
1875 E = HashResultPointer[4];
1876
1877 do {
1878 if (t < 20) {
1879 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1880 } else if (t < 40) {
1881 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1882 } else if (t < 60) {
1883 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1884 } else {
1885 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1886 }
1887 TEMP += S(5, A) + E + HashWorkingPointer[t];
1888 E = D;
1889 D = C;
1890 C = S(30, B);
1891 B = A;
1892 A = TEMP;
1893 } while (++t <= 79);
1894
1895 HashResultPointer[0] += A;
1896 HashResultPointer[1] += B;
1897 HashResultPointer[2] += C;
1898 HashResultPointer[3] += D;
1899 HashResultPointer[4] += E;
1900
1901}
1902
James Smarte59058c2008-08-24 21:49:00 -04001903/**
James Smart3621a712009-04-06 18:47:14 -04001904 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04001905 * @RandomChallenge: pointer to the entry of host challenge random number array.
1906 * @HashWorking: pointer to the entry of the working hash array.
1907 *
1908 * This routine calculates the working hash array referred by @HashWorking
1909 * from the challenge random numbers associated with the host, referred by
1910 * @RandomChallenge. The result is put into the entry of the working hash
1911 * array and returned by reference through @HashWorking.
1912 **/
dea31012005-04-17 16:05:31 -05001913static void
1914lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1915{
1916 *HashWorking = (*RandomChallenge ^ *HashWorking);
1917}
1918
James Smarte59058c2008-08-24 21:49:00 -04001919/**
James Smart3621a712009-04-06 18:47:14 -04001920 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04001921 * @phba: pointer to lpfc hba data structure.
1922 * @hbainit: pointer to an array of unsigned 32-bit integers.
1923 *
1924 * This routine performs the special handling for LC HBA initialization.
1925 **/
dea31012005-04-17 16:05:31 -05001926void
1927lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1928{
1929 int t;
1930 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001931 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001932
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001933 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001934 if (!HashWorking)
1935 return;
1936
dea31012005-04-17 16:05:31 -05001937 HashWorking[0] = HashWorking[78] = *pwwnn++;
1938 HashWorking[1] = HashWorking[79] = *pwwnn;
1939
1940 for (t = 0; t < 7; t++)
1941 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1942
1943 lpfc_sha_init(hbainit);
1944 lpfc_sha_iterate(hbainit, HashWorking);
1945 kfree(HashWorking);
1946}
1947
James Smarte59058c2008-08-24 21:49:00 -04001948/**
James Smart3621a712009-04-06 18:47:14 -04001949 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04001950 * @vport: pointer to a virtual N_Port data structure.
1951 *
1952 * This routine performs the necessary cleanups before deleting the @vport.
1953 * It invokes the discovery state machine to perform necessary state
1954 * transitions and to release the ndlps associated with the @vport. Note,
1955 * the physical port is treated as @vport 0.
1956 **/
James Smart87af33f2007-10-27 13:37:43 -04001957void
James Smart2e0fef82007-06-17 19:56:36 -05001958lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001959{
James Smart87af33f2007-10-27 13:37:43 -04001960 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001961 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04001962 int i = 0;
dea31012005-04-17 16:05:31 -05001963
James Smart87af33f2007-10-27 13:37:43 -04001964 if (phba->link_state > LPFC_LINK_DOWN)
1965 lpfc_port_link_failure(vport);
1966
1967 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001968 if (!NLP_CHK_NODE_ACT(ndlp)) {
1969 ndlp = lpfc_enable_node(vport, ndlp,
1970 NLP_STE_UNUSED_NODE);
1971 if (!ndlp)
1972 continue;
1973 spin_lock_irq(&phba->ndlp_lock);
1974 NLP_SET_FREE_REQ(ndlp);
1975 spin_unlock_irq(&phba->ndlp_lock);
1976 /* Trigger the release of the ndlp memory */
1977 lpfc_nlp_put(ndlp);
1978 continue;
1979 }
1980 spin_lock_irq(&phba->ndlp_lock);
1981 if (NLP_CHK_FREE_REQ(ndlp)) {
1982 /* The ndlp should not be in memory free mode already */
1983 spin_unlock_irq(&phba->ndlp_lock);
1984 continue;
1985 } else
1986 /* Indicate request for freeing ndlp memory */
1987 NLP_SET_FREE_REQ(ndlp);
1988 spin_unlock_irq(&phba->ndlp_lock);
1989
James Smart58da1ff2008-04-07 10:15:56 -04001990 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1991 ndlp->nlp_DID == Fabric_DID) {
1992 /* Just free up ndlp with Fabric_DID for vports */
1993 lpfc_nlp_put(ndlp);
1994 continue;
1995 }
1996
James Smart87af33f2007-10-27 13:37:43 -04001997 if (ndlp->nlp_type & NLP_FABRIC)
1998 lpfc_disc_state_machine(vport, ndlp, NULL,
1999 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002000
James Smart87af33f2007-10-27 13:37:43 -04002001 lpfc_disc_state_machine(vport, ndlp, NULL,
2002 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002003
James Smart87af33f2007-10-27 13:37:43 -04002004 }
2005
James Smarta8adb832007-10-27 13:37:53 -04002006 /* At this point, ALL ndlp's should be gone
2007 * because of the previous NLP_EVT_DEVICE_RM.
2008 * Lets wait for this to happen, if needed.
2009 */
James Smart87af33f2007-10-27 13:37:43 -04002010 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002011 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002012 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002013 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002014 list_for_each_entry_safe(ndlp, next_ndlp,
2015 &vport->fc_nodes, nlp_listp) {
2016 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2017 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002018 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002019 "usgmap:x%x refcnt:%d\n",
2020 ndlp->nlp_DID, (void *)ndlp,
2021 ndlp->nlp_usg_map,
2022 atomic_read(
2023 &ndlp->kref.refcount));
2024 }
James Smarta8adb832007-10-27 13:37:53 -04002025 break;
James Smart87af33f2007-10-27 13:37:43 -04002026 }
James Smarta8adb832007-10-27 13:37:53 -04002027
2028 /* Wait for any activity on ndlps to settle */
2029 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002030 }
dea31012005-04-17 16:05:31 -05002031}
2032
James Smarte59058c2008-08-24 21:49:00 -04002033/**
James Smart3621a712009-04-06 18:47:14 -04002034 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002035 * @vport: pointer to a virtual N_Port data structure.
2036 *
2037 * This routine stops all the timers associated with a @vport. This function
2038 * is invoked before disabling or deleting a @vport. Note that the physical
2039 * port is treated as @vport 0.
2040 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002041void
2042lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002043{
James Smart92d7f7b2007-06-17 19:56:38 -05002044 del_timer_sync(&vport->els_tmofunc);
2045 del_timer_sync(&vport->fc_fdmitmo);
2046 lpfc_can_disctmo(vport);
2047 return;
dea31012005-04-17 16:05:31 -05002048}
2049
James Smarte59058c2008-08-24 21:49:00 -04002050/**
James Smart3772a992009-05-22 14:50:54 -04002051 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002052 * @phba: pointer to lpfc hba data structure.
2053 *
2054 * This routine stops all the timers associated with a HBA. This function is
2055 * invoked before either putting a HBA offline or unloading the driver.
2056 **/
James Smart3772a992009-05-22 14:50:54 -04002057void
2058lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002059{
James Smart51ef4c22007-08-02 11:10:31 -04002060 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002061 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002062 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002063 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002064 del_timer_sync(&phba->hb_tmofunc);
2065 phba->hb_outstanding = 0;
2066
2067 switch (phba->pci_dev_grp) {
2068 case LPFC_PCI_DEV_LP:
2069 /* Stop any LightPulse device specific driver timers */
2070 del_timer_sync(&phba->fcp_poll_timer);
2071 break;
2072 case LPFC_PCI_DEV_OC:
2073 /* Stop any OneConnect device sepcific driver timers */
2074 break;
2075 default:
2076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2077 "0297 Invalid device group (x%x)\n",
2078 phba->pci_dev_grp);
2079 break;
2080 }
James Smart2e0fef82007-06-17 19:56:36 -05002081 return;
dea31012005-04-17 16:05:31 -05002082}
2083
James Smarte59058c2008-08-24 21:49:00 -04002084/**
James Smart3621a712009-04-06 18:47:14 -04002085 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002086 * @phba: pointer to lpfc hba data structure.
2087 *
2088 * This routine marks a HBA's management interface as blocked. Once the HBA's
2089 * management interface is marked as blocked, all the user space access to
2090 * the HBA, whether they are from sysfs interface or libdfc interface will
2091 * all be blocked. The HBA is set to block the management interface when the
2092 * driver prepares the HBA interface for online or offline.
2093 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002094static void
2095lpfc_block_mgmt_io(struct lpfc_hba * phba)
2096{
2097 unsigned long iflag;
2098
2099 spin_lock_irqsave(&phba->hbalock, iflag);
2100 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2101 spin_unlock_irqrestore(&phba->hbalock, iflag);
2102}
2103
James Smarte59058c2008-08-24 21:49:00 -04002104/**
James Smart3621a712009-04-06 18:47:14 -04002105 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002106 * @phba: pointer to lpfc hba data structure.
2107 *
2108 * This routine initializes the HBA and brings a HBA online. During this
2109 * process, the management interface is blocked to prevent user space access
2110 * to the HBA interfering with the driver initialization.
2111 *
2112 * Return codes
2113 * 0 - successful
2114 * 1 - failed
2115 **/
dea31012005-04-17 16:05:31 -05002116int
James Smart2e0fef82007-06-17 19:56:36 -05002117lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002118{
Julia Lawall372bd282008-12-16 16:15:08 +01002119 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002120 struct lpfc_vport **vports;
2121 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002122
dea31012005-04-17 16:05:31 -05002123 if (!phba)
2124 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002125 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002126
James Smart2e0fef82007-06-17 19:56:36 -05002127 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002128 return 0;
2129
James Smarted957682007-06-17 19:56:37 -05002130 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002131 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002132
James Smart46fa3112007-04-25 09:51:45 -04002133 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002134
James Smart46fa3112007-04-25 09:51:45 -04002135 if (!lpfc_sli_queue_setup(phba)) {
2136 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002137 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002138 }
2139
James Smartda0436e2009-05-22 14:51:39 -04002140 if (phba->sli_rev == LPFC_SLI_REV4) {
2141 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2142 lpfc_unblock_mgmt_io(phba);
2143 return 1;
2144 }
2145 } else {
2146 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2147 lpfc_unblock_mgmt_io(phba);
2148 return 1;
2149 }
James Smart46fa3112007-04-25 09:51:45 -04002150 }
dea31012005-04-17 16:05:31 -05002151
James Smart549e55c2007-08-02 11:09:51 -04002152 vports = lpfc_create_vport_work_array(phba);
2153 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002154 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002155 struct Scsi_Host *shost;
2156 shost = lpfc_shost_from_vport(vports[i]);
2157 spin_lock_irq(shost->host_lock);
2158 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2159 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2160 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2161 spin_unlock_irq(shost->host_lock);
2162 }
James Smart09372822008-01-11 01:52:54 -05002163 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002164
James Smart46fa3112007-04-25 09:51:45 -04002165 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002166 return 0;
2167}
2168
James Smarte59058c2008-08-24 21:49:00 -04002169/**
James Smart3621a712009-04-06 18:47:14 -04002170 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002171 * @phba: pointer to lpfc hba data structure.
2172 *
2173 * This routine marks a HBA's management interface as not blocked. Once the
2174 * HBA's management interface is marked as not blocked, all the user space
2175 * access to the HBA, whether they are from sysfs interface or libdfc
2176 * interface will be allowed. The HBA is set to block the management interface
2177 * when the driver prepares the HBA interface for online or offline and then
2178 * set to unblock the management interface afterwards.
2179 **/
James Smart46fa3112007-04-25 09:51:45 -04002180void
James Smart46fa3112007-04-25 09:51:45 -04002181lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2182{
2183 unsigned long iflag;
2184
James Smart2e0fef82007-06-17 19:56:36 -05002185 spin_lock_irqsave(&phba->hbalock, iflag);
2186 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2187 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002188}
2189
James Smarte59058c2008-08-24 21:49:00 -04002190/**
James Smart3621a712009-04-06 18:47:14 -04002191 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002192 * @phba: pointer to lpfc hba data structure.
2193 *
2194 * This routine is invoked to prepare a HBA to be brought offline. It performs
2195 * unregistration login to all the nodes on all vports and flushes the mailbox
2196 * queue to make it ready to be brought offline.
2197 **/
James Smart46fa3112007-04-25 09:51:45 -04002198void
2199lpfc_offline_prep(struct lpfc_hba * phba)
2200{
James Smart2e0fef82007-06-17 19:56:36 -05002201 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002202 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002203 struct lpfc_vport **vports;
2204 int i;
dea31012005-04-17 16:05:31 -05002205
James Smart2e0fef82007-06-17 19:56:36 -05002206 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002207 return;
dea31012005-04-17 16:05:31 -05002208
James Smart46fa3112007-04-25 09:51:45 -04002209 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002210
2211 lpfc_linkdown(phba);
2212
James Smart87af33f2007-10-27 13:37:43 -04002213 /* Issue an unreg_login to all nodes on all vports */
2214 vports = lpfc_create_vport_work_array(phba);
2215 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002216 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart87af33f2007-10-27 13:37:43 -04002217 struct Scsi_Host *shost;
2218
James Smarta8adb832007-10-27 13:37:53 -04002219 if (vports[i]->load_flag & FC_UNLOADING)
2220 continue;
James Smartda0436e2009-05-22 14:51:39 -04002221 vports[i]->vfi_state &= ~LPFC_VFI_REGISTERED;
James Smart87af33f2007-10-27 13:37:43 -04002222 shost = lpfc_shost_from_vport(vports[i]);
2223 list_for_each_entry_safe(ndlp, next_ndlp,
2224 &vports[i]->fc_nodes,
2225 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002226 if (!NLP_CHK_NODE_ACT(ndlp))
2227 continue;
James Smart87af33f2007-10-27 13:37:43 -04002228 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2229 continue;
2230 if (ndlp->nlp_type & NLP_FABRIC) {
2231 lpfc_disc_state_machine(vports[i], ndlp,
2232 NULL, NLP_EVT_DEVICE_RECOVERY);
2233 lpfc_disc_state_machine(vports[i], ndlp,
2234 NULL, NLP_EVT_DEVICE_RM);
2235 }
2236 spin_lock_irq(shost->host_lock);
2237 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2238 spin_unlock_irq(shost->host_lock);
2239 lpfc_unreg_rpi(vports[i], ndlp);
2240 }
2241 }
2242 }
James Smart09372822008-01-11 01:52:54 -05002243 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002244
James Smart04c68492009-05-22 14:52:52 -04002245 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002246}
2247
James Smarte59058c2008-08-24 21:49:00 -04002248/**
James Smart3621a712009-04-06 18:47:14 -04002249 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002250 * @phba: pointer to lpfc hba data structure.
2251 *
2252 * This routine actually brings a HBA offline. It stops all the timers
2253 * associated with the HBA, brings down the SLI layer, and eventually
2254 * marks the HBA as in offline state for the upper layer protocol.
2255 **/
James Smart46fa3112007-04-25 09:51:45 -04002256void
James Smart2e0fef82007-06-17 19:56:36 -05002257lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002258{
James Smart549e55c2007-08-02 11:09:51 -04002259 struct Scsi_Host *shost;
2260 struct lpfc_vport **vports;
2261 int i;
James Smart46fa3112007-04-25 09:51:45 -04002262
James Smart549e55c2007-08-02 11:09:51 -04002263 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002264 return;
James Smart688a8862006-07-06 15:49:56 -04002265
James Smartda0436e2009-05-22 14:51:39 -04002266 /* stop port and all timers associated with this hba */
2267 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002268 vports = lpfc_create_vport_work_array(phba);
2269 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002270 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002271 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002272 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002273 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002274 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002275 /* Bring down the SLI Layer and cleanup. The HBA is offline
2276 now. */
2277 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002278 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002279 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002280 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002281 vports = lpfc_create_vport_work_array(phba);
2282 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002283 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002284 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002285 spin_lock_irq(shost->host_lock);
2286 vports[i]->work_port_events = 0;
2287 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2288 spin_unlock_irq(shost->host_lock);
2289 }
James Smart09372822008-01-11 01:52:54 -05002290 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002291}
2292
James Smarte59058c2008-08-24 21:49:00 -04002293/**
James Smart3621a712009-04-06 18:47:14 -04002294 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002295 * @phba: pointer to lpfc hba data structure.
2296 *
2297 * This routine is to free all the SCSI buffers and IOCBs from the driver
2298 * list back to kernel. It is called from lpfc_pci_remove_one to free
2299 * the internal resources before the device is removed from the system.
2300 *
2301 * Return codes
2302 * 0 - successful (for now, it always returns 0)
2303 **/
dea31012005-04-17 16:05:31 -05002304static int
James Smart2e0fef82007-06-17 19:56:36 -05002305lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002306{
2307 struct lpfc_scsi_buf *sb, *sb_next;
2308 struct lpfc_iocbq *io, *io_next;
2309
James Smart2e0fef82007-06-17 19:56:36 -05002310 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002311 /* Release all the lpfc_scsi_bufs maintained by this host. */
2312 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2313 list_del(&sb->list);
2314 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002315 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002316 kfree(sb);
2317 phba->total_scsi_bufs--;
2318 }
2319
2320 /* Release all the lpfc_iocbq entries maintained by this host. */
2321 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2322 list_del(&io->list);
2323 kfree(io);
2324 phba->total_iocbq_bufs--;
2325 }
2326
James Smart2e0fef82007-06-17 19:56:36 -05002327 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002328
2329 return 0;
2330}
2331
James Smarte59058c2008-08-24 21:49:00 -04002332/**
James Smart3621a712009-04-06 18:47:14 -04002333 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002334 * @phba: pointer to lpfc hba data structure.
2335 * @instance: a unique integer ID to this FC port.
2336 * @dev: pointer to the device data structure.
2337 *
2338 * This routine creates a FC port for the upper layer protocol. The FC port
2339 * can be created on top of either a physical port or a virtual port provided
2340 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2341 * and associates the FC port created before adding the shost into the SCSI
2342 * layer.
2343 *
2344 * Return codes
2345 * @vport - pointer to the virtual N_Port data structure.
2346 * NULL - port create failed.
2347 **/
James Smart2e0fef82007-06-17 19:56:36 -05002348struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002349lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002350{
James Smart2e0fef82007-06-17 19:56:36 -05002351 struct lpfc_vport *vport;
2352 struct Scsi_Host *shost;
2353 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002354
James Smart3de2a652007-08-02 11:09:59 -04002355 if (dev != &phba->pcidev->dev)
2356 shost = scsi_host_alloc(&lpfc_vport_template,
2357 sizeof(struct lpfc_vport));
2358 else
2359 shost = scsi_host_alloc(&lpfc_template,
2360 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002361 if (!shost)
2362 goto out;
James Smart47a86172007-04-25 09:53:22 -04002363
James Smart2e0fef82007-06-17 19:56:36 -05002364 vport = (struct lpfc_vport *) shost->hostdata;
2365 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002366 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002367 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002368 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002369
James Smart3de2a652007-08-02 11:09:59 -04002370 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002371 shost->unique_id = instance;
2372 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002373 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002374 shost->this_id = -1;
2375 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002376 if (phba->sli_rev == LPFC_SLI_REV4) {
2377 shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2378 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2379 }
James Smart81301a92008-12-04 22:39:46 -05002380
James Smart47a86172007-04-25 09:53:22 -04002381 /*
James Smart2e0fef82007-06-17 19:56:36 -05002382 * Set initial can_queue value since 0 is no longer supported and
2383 * scsi_add_host will fail. This will be adjusted later based on the
2384 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002385 */
James Smart2e0fef82007-06-17 19:56:36 -05002386 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002387 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002388 shost->transportt = lpfc_vport_transport_template;
2389 vport->port_type = LPFC_NPIV_PORT;
2390 } else {
2391 shost->transportt = lpfc_transport_template;
2392 vport->port_type = LPFC_PHYSICAL_PORT;
2393 }
James Smart47a86172007-04-25 09:53:22 -04002394
James Smart2e0fef82007-06-17 19:56:36 -05002395 /* Initialize all internally managed lists. */
2396 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002397 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002398 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002399
James Smart2e0fef82007-06-17 19:56:36 -05002400 init_timer(&vport->fc_disctmo);
2401 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002402 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002403
James Smart2e0fef82007-06-17 19:56:36 -05002404 init_timer(&vport->fc_fdmitmo);
2405 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002406 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002407
James Smart2e0fef82007-06-17 19:56:36 -05002408 init_timer(&vport->els_tmofunc);
2409 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002410 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002411
James Smart3de2a652007-08-02 11:09:59 -04002412 error = scsi_add_host(shost, dev);
James Smart2e0fef82007-06-17 19:56:36 -05002413 if (error)
2414 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002415
James Smart549e55c2007-08-02 11:09:51 -04002416 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002417 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002418 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002419 return vport;
James Smart47a86172007-04-25 09:53:22 -04002420
James Smart2e0fef82007-06-17 19:56:36 -05002421out_put_shost:
2422 scsi_host_put(shost);
2423out:
2424 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002425}
2426
James Smarte59058c2008-08-24 21:49:00 -04002427/**
James Smart3621a712009-04-06 18:47:14 -04002428 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002429 * @vport: pointer to an lpfc virtual N_Port data structure.
2430 *
2431 * This routine destroys a FC port from the upper layer protocol. All the
2432 * resources associated with the port are released.
2433 **/
James Smart2e0fef82007-06-17 19:56:36 -05002434void
2435destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002436{
James Smart92d7f7b2007-06-17 19:56:38 -05002437 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2438 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002439
James Smart858c9f62007-06-17 19:56:39 -05002440 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002441 fc_remove_host(shost);
2442 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002443
James Smart92d7f7b2007-06-17 19:56:38 -05002444 spin_lock_irq(&phba->hbalock);
2445 list_del_init(&vport->listentry);
2446 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002447
James Smart92d7f7b2007-06-17 19:56:38 -05002448 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002449 return;
James Smart47a86172007-04-25 09:53:22 -04002450}
2451
James Smarte59058c2008-08-24 21:49:00 -04002452/**
James Smart3621a712009-04-06 18:47:14 -04002453 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002454 *
2455 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2456 * uses the kernel idr facility to perform the task.
2457 *
2458 * Return codes:
2459 * instance - a unique integer ID allocated as the new instance.
2460 * -1 - lpfc get instance failed.
2461 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002462int
2463lpfc_get_instance(void)
2464{
2465 int instance = 0;
2466
2467 /* Assign an unused number */
2468 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2469 return -1;
2470 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2471 return -1;
2472 return instance;
2473}
2474
James Smarte59058c2008-08-24 21:49:00 -04002475/**
James Smart3621a712009-04-06 18:47:14 -04002476 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002477 * @shost: pointer to SCSI host data structure.
2478 * @time: elapsed time of the scan in jiffies.
2479 *
2480 * This routine is called by the SCSI layer with a SCSI host to determine
2481 * whether the scan host is finished.
2482 *
2483 * Note: there is no scan_start function as adapter initialization will have
2484 * asynchronously kicked off the link initialization.
2485 *
2486 * Return codes
2487 * 0 - SCSI host scan is not over yet.
2488 * 1 - SCSI host scan is over.
2489 **/
James Smart47a86172007-04-25 09:53:22 -04002490int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2491{
James Smart2e0fef82007-06-17 19:56:36 -05002492 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2493 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002494 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002495
James Smart858c9f62007-06-17 19:56:39 -05002496 spin_lock_irq(shost->host_lock);
2497
James Smart51ef4c22007-08-02 11:10:31 -04002498 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002499 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002500 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002501 }
James Smart2e0fef82007-06-17 19:56:36 -05002502 if (time >= 30 * HZ) {
2503 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002504 "0461 Scanning longer than 30 "
2505 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002506 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002507 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002508 }
2509 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2510 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002511 "0465 Link down longer than 15 "
2512 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002513 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002514 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002515 }
2516
James Smart2e0fef82007-06-17 19:56:36 -05002517 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002518 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002519 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002520 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002521 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002522 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002523 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002524 goto finished;
2525
2526 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002527
2528finished:
James Smart858c9f62007-06-17 19:56:39 -05002529 spin_unlock_irq(shost->host_lock);
2530 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002531}
2532
James Smarte59058c2008-08-24 21:49:00 -04002533/**
James Smart3621a712009-04-06 18:47:14 -04002534 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002535 * @shost: pointer to SCSI host data structure.
2536 *
2537 * This routine initializes a given SCSI host attributes on a FC port. The
2538 * SCSI host can be either on top of a physical port or a virtual port.
2539 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002540void lpfc_host_attrib_init(struct Scsi_Host *shost)
2541{
2542 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2543 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002544 /*
James Smart2e0fef82007-06-17 19:56:36 -05002545 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002546 */
2547
James Smart2e0fef82007-06-17 19:56:36 -05002548 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2549 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002550 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2551
2552 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002553 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002554 fc_host_supported_fc4s(shost)[2] = 1;
2555 fc_host_supported_fc4s(shost)[7] = 1;
2556
James Smart92d7f7b2007-06-17 19:56:38 -05002557 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2558 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002559
2560 fc_host_supported_speeds(shost) = 0;
2561 if (phba->lmt & LMT_10Gb)
2562 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002563 if (phba->lmt & LMT_8Gb)
2564 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002565 if (phba->lmt & LMT_4Gb)
2566 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2567 if (phba->lmt & LMT_2Gb)
2568 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2569 if (phba->lmt & LMT_1Gb)
2570 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2571
2572 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002573 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2574 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002575
2576 /* This value is also unchanging */
2577 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002578 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002579 fc_host_active_fc4s(shost)[2] = 1;
2580 fc_host_active_fc4s(shost)[7] = 1;
2581
James Smart92d7f7b2007-06-17 19:56:38 -05002582 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002583 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002584 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002585 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002586}
dea31012005-04-17 16:05:31 -05002587
James Smarte59058c2008-08-24 21:49:00 -04002588/**
James Smartda0436e2009-05-22 14:51:39 -04002589 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002590 * @phba: pointer to lpfc hba data structure.
2591 *
James Smartda0436e2009-05-22 14:51:39 -04002592 * This routine is invoked to stop an SLI3 device port, it stops the device
2593 * from generating interrupts and stops the device driver's timers for the
2594 * device.
James Smarte59058c2008-08-24 21:49:00 -04002595 **/
James Smartdb2378e2008-02-08 18:49:51 -05002596static void
James Smartda0436e2009-05-22 14:51:39 -04002597lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002598{
James Smartda0436e2009-05-22 14:51:39 -04002599 /* Clear all interrupt enable conditions */
2600 writel(0, phba->HCregaddr);
2601 readl(phba->HCregaddr); /* flush */
2602 /* Clear all pending interrupts */
2603 writel(0xffffffff, phba->HAregaddr);
2604 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002605
James Smartda0436e2009-05-22 14:51:39 -04002606 /* Reset some HBA SLI setup states */
2607 lpfc_stop_hba_timers(phba);
2608 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002609}
2610
James Smarte59058c2008-08-24 21:49:00 -04002611/**
James Smartda0436e2009-05-22 14:51:39 -04002612 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002613 * @phba: pointer to lpfc hba data structure.
2614 *
James Smartda0436e2009-05-22 14:51:39 -04002615 * This routine is invoked to stop an SLI4 device port, it stops the device
2616 * from generating interrupts and stops the device driver's timers for the
2617 * device.
2618 **/
2619static void
2620lpfc_stop_port_s4(struct lpfc_hba *phba)
2621{
2622 /* Reset some HBA SLI4 setup states */
2623 lpfc_stop_hba_timers(phba);
2624 phba->pport->work_port_events = 0;
2625 phba->sli4_hba.intr_enable = 0;
2626 /* Hard clear it for now, shall have more graceful way to wait later */
2627 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2628}
2629
2630/**
2631 * lpfc_stop_port - Wrapper function for stopping hba port
2632 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002633 *
James Smartda0436e2009-05-22 14:51:39 -04002634 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2635 * the API jump table function pointer from the lpfc_hba struct.
2636 **/
2637void
2638lpfc_stop_port(struct lpfc_hba *phba)
2639{
2640 phba->lpfc_stop_port(phba);
2641}
2642
2643/**
2644 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2645 * @phba: pointer to lpfc hba data structure.
2646 *
2647 * This routine is invoked to remove the driver default fcf record from
2648 * the port. This routine currently acts on FCF Index 0.
2649 *
2650 **/
2651void
2652lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2653{
2654 int rc = 0;
2655 LPFC_MBOXQ_t *mboxq;
2656 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2657 uint32_t mbox_tmo, req_len;
2658 uint32_t shdr_status, shdr_add_status;
2659
2660 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2661 if (!mboxq) {
2662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2663 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2664 return;
2665 }
2666
2667 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2668 sizeof(struct lpfc_sli4_cfg_mhdr);
2669 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2670 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2671 req_len, LPFC_SLI4_MBX_EMBED);
2672 /*
2673 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2674 * supports multiple FCF indices.
2675 */
2676 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2677 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2678 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2679 phba->fcf.fcf_indx);
2680
2681 if (!phba->sli4_hba.intr_enable)
2682 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2683 else {
2684 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2685 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2686 }
2687 /* The IOCTL status is embedded in the mailbox subheader. */
2688 shdr_status = bf_get(lpfc_mbox_hdr_status,
2689 &del_fcf_record->header.cfg_shdr.response);
2690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2691 &del_fcf_record->header.cfg_shdr.response);
2692 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2693 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2694 "2516 DEL FCF of default FCF Index failed "
2695 "mbx status x%x, status x%x add_status x%x\n",
2696 rc, shdr_status, shdr_add_status);
2697 }
2698 if (rc != MBX_TIMEOUT)
2699 mempool_free(mboxq, phba->mbox_mem_pool);
2700}
2701
2702/**
2703 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2704 * @phba: pointer to lpfc hba data structure.
2705 * @acqe_link: pointer to the async link completion queue entry.
2706 *
2707 * This routine is to parse the SLI4 link-attention link fault code and
2708 * translate it into the base driver's read link attention mailbox command
2709 * status.
2710 *
2711 * Return: Link-attention status in terms of base driver's coding.
2712 **/
2713static uint16_t
2714lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2715 struct lpfc_acqe_link *acqe_link)
2716{
2717 uint16_t latt_fault;
2718
2719 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2720 case LPFC_ASYNC_LINK_FAULT_NONE:
2721 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2722 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2723 latt_fault = 0;
2724 break;
2725 default:
2726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2727 "0398 Invalid link fault code: x%x\n",
2728 bf_get(lpfc_acqe_link_fault, acqe_link));
2729 latt_fault = MBXERR_ERROR;
2730 break;
2731 }
2732 return latt_fault;
2733}
2734
2735/**
2736 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2737 * @phba: pointer to lpfc hba data structure.
2738 * @acqe_link: pointer to the async link completion queue entry.
2739 *
2740 * This routine is to parse the SLI4 link attention type and translate it
2741 * into the base driver's link attention type coding.
2742 *
2743 * Return: Link attention type in terms of base driver's coding.
2744 **/
2745static uint8_t
2746lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2747 struct lpfc_acqe_link *acqe_link)
2748{
2749 uint8_t att_type;
2750
2751 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2752 case LPFC_ASYNC_LINK_STATUS_DOWN:
2753 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2754 att_type = AT_LINK_DOWN;
2755 break;
2756 case LPFC_ASYNC_LINK_STATUS_UP:
2757 /* Ignore physical link up events - wait for logical link up */
2758 att_type = AT_RESERVED;
2759 break;
2760 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2761 att_type = AT_LINK_UP;
2762 break;
2763 default:
2764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2765 "0399 Invalid link attention type: x%x\n",
2766 bf_get(lpfc_acqe_link_status, acqe_link));
2767 att_type = AT_RESERVED;
2768 break;
2769 }
2770 return att_type;
2771}
2772
2773/**
2774 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2775 * @phba: pointer to lpfc hba data structure.
2776 * @acqe_link: pointer to the async link completion queue entry.
2777 *
2778 * This routine is to parse the SLI4 link-attention link speed and translate
2779 * it into the base driver's link-attention link speed coding.
2780 *
2781 * Return: Link-attention link speed in terms of base driver's coding.
2782 **/
2783static uint8_t
2784lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2785 struct lpfc_acqe_link *acqe_link)
2786{
2787 uint8_t link_speed;
2788
2789 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2790 case LPFC_ASYNC_LINK_SPEED_ZERO:
2791 link_speed = LA_UNKNW_LINK;
2792 break;
2793 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2794 link_speed = LA_UNKNW_LINK;
2795 break;
2796 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2797 link_speed = LA_UNKNW_LINK;
2798 break;
2799 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2800 link_speed = LA_1GHZ_LINK;
2801 break;
2802 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2803 link_speed = LA_10GHZ_LINK;
2804 break;
2805 default:
2806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2807 "0483 Invalid link-attention link speed: x%x\n",
2808 bf_get(lpfc_acqe_link_speed, acqe_link));
2809 link_speed = LA_UNKNW_LINK;
2810 break;
2811 }
2812 return link_speed;
2813}
2814
2815/**
2816 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2817 * @phba: pointer to lpfc hba data structure.
2818 * @acqe_link: pointer to the async link completion queue entry.
2819 *
2820 * This routine is to handle the SLI4 asynchronous link event.
2821 **/
2822static void
2823lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
2824 struct lpfc_acqe_link *acqe_link)
2825{
2826 struct lpfc_dmabuf *mp;
2827 LPFC_MBOXQ_t *pmb;
2828 MAILBOX_t *mb;
2829 READ_LA_VAR *la;
2830 uint8_t att_type;
2831
2832 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
2833 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
2834 return;
2835 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
2922 switch (event_type) {
2923 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
2924 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2925 "2546 New FCF found index 0x%x tag 0x%x \n",
2926 acqe_fcoe->fcf_index,
2927 acqe_fcoe->event_tag);
2928 /*
2929 * If the current FCF is in discovered state,
2930 * do nothing.
2931 */
2932 spin_lock_irq(&phba->hbalock);
2933 if (phba->fcf.fcf_flag & FCF_DISCOVERED) {
2934 spin_unlock_irq(&phba->hbalock);
2935 break;
2936 }
2937 spin_unlock_irq(&phba->hbalock);
2938
2939 /* Read the FCF table and re-discover SAN. */
2940 rc = lpfc_sli4_read_fcf_record(phba,
2941 LPFC_FCOE_FCF_GET_FIRST);
2942 if (rc)
2943 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2944 "2547 Read FCF record failed 0x%x\n",
2945 rc);
2946 break;
2947
2948 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
2949 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2950 "2548 FCF Table full count 0x%x tag 0x%x \n",
2951 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
2952 acqe_fcoe->event_tag);
2953 break;
2954
2955 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
2956 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2957 "2549 FCF disconnected fron network index 0x%x"
2958 " tag 0x%x \n", acqe_fcoe->fcf_index,
2959 acqe_fcoe->event_tag);
2960 /* If the event is not for currently used fcf do nothing */
2961 if (phba->fcf.fcf_indx != acqe_fcoe->fcf_index)
2962 break;
2963 /*
2964 * Currently, driver support only one FCF - so treat this as
2965 * a link down.
2966 */
2967 lpfc_linkdown(phba);
2968 /* Unregister FCF if no devices connected to it */
2969 lpfc_unregister_unused_fcf(phba);
2970 break;
2971
2972 default:
2973 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2974 "0288 Unknown FCoE event type 0x%x event tag "
2975 "0x%x\n", event_type, acqe_fcoe->event_tag);
2976 break;
2977 }
2978}
2979
2980/**
2981 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
2982 * @phba: pointer to lpfc hba data structure.
2983 * @acqe_link: pointer to the async dcbx completion queue entry.
2984 *
2985 * This routine is to handle the SLI4 asynchronous dcbx event.
2986 **/
2987static void
2988lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
2989 struct lpfc_acqe_dcbx *acqe_dcbx)
2990{
2991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2992 "0290 The SLI4 DCBX asynchronous event is not "
2993 "handled yet\n");
2994}
2995
2996/**
2997 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
2998 * @phba: pointer to lpfc hba data structure.
2999 *
3000 * This routine is invoked by the worker thread to process all the pending
3001 * SLI4 asynchronous events.
3002 **/
3003void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3004{
3005 struct lpfc_cq_event *cq_event;
3006
3007 /* First, declare the async event has been handled */
3008 spin_lock_irq(&phba->hbalock);
3009 phba->hba_flag &= ~ASYNC_EVENT;
3010 spin_unlock_irq(&phba->hbalock);
3011 /* Now, handle all the async events */
3012 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3013 /* Get the first event from the head of the event queue */
3014 spin_lock_irq(&phba->hbalock);
3015 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3016 cq_event, struct lpfc_cq_event, list);
3017 spin_unlock_irq(&phba->hbalock);
3018 /* Process the asynchronous event */
3019 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3020 case LPFC_TRAILER_CODE_LINK:
3021 lpfc_sli4_async_link_evt(phba,
3022 &cq_event->cqe.acqe_link);
3023 break;
3024 case LPFC_TRAILER_CODE_FCOE:
3025 lpfc_sli4_async_fcoe_evt(phba,
3026 &cq_event->cqe.acqe_fcoe);
3027 break;
3028 case LPFC_TRAILER_CODE_DCBX:
3029 lpfc_sli4_async_dcbx_evt(phba,
3030 &cq_event->cqe.acqe_dcbx);
3031 break;
3032 default:
3033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3034 "1804 Invalid asynchrous event code: "
3035 "x%x\n", bf_get(lpfc_trailer_code,
3036 &cq_event->cqe.mcqe_cmpl));
3037 break;
3038 }
3039 /* Free the completion event processed to the free pool */
3040 lpfc_sli4_cq_event_release(phba, cq_event);
3041 }
3042}
3043
3044/**
3045 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3046 * @phba: pointer to lpfc hba data structure.
3047 * @dev_grp: The HBA PCI-Device group number.
3048 *
3049 * This routine is invoked to set up the per HBA PCI-Device group function
3050 * API jump table entries.
3051 *
3052 * Return: 0 if success, otherwise -ENODEV
3053 **/
3054int
3055lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003056{
3057 int rc;
3058
James Smartda0436e2009-05-22 14:51:39 -04003059 /* Set up lpfc PCI-device group */
3060 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003061
James Smartda0436e2009-05-22 14:51:39 -04003062 /* The LPFC_PCI_DEV_OC uses SLI4 */
3063 if (dev_grp == LPFC_PCI_DEV_OC)
3064 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003065
James Smartda0436e2009-05-22 14:51:39 -04003066 /* Set up device INIT API function jump table */
3067 rc = lpfc_init_api_table_setup(phba, dev_grp);
3068 if (rc)
3069 return -ENODEV;
3070 /* Set up SCSI API function jump table */
3071 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3072 if (rc)
3073 return -ENODEV;
3074 /* Set up SLI API function jump table */
3075 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3076 if (rc)
3077 return -ENODEV;
3078 /* Set up MBOX API function jump table */
3079 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3080 if (rc)
3081 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003082
James Smartda0436e2009-05-22 14:51:39 -04003083 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003084}
3085
3086/**
James Smart3621a712009-04-06 18:47:14 -04003087 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003088 * @phba: pointer to lpfc hba data structure.
3089 * @intr_mode: active interrupt mode adopted.
3090 *
3091 * This routine it invoked to log the currently used active interrupt mode
3092 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003093 **/
3094static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003095{
3096 switch (intr_mode) {
3097 case 0:
3098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3099 "0470 Enable INTx interrupt mode.\n");
3100 break;
3101 case 1:
3102 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3103 "0481 Enabled MSI interrupt mode.\n");
3104 break;
3105 case 2:
3106 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3107 "0480 Enabled MSI-X interrupt mode.\n");
3108 break;
3109 default:
3110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3111 "0482 Illegal interrupt mode.\n");
3112 break;
3113 }
3114 return;
3115}
3116
James Smart5b75da22008-12-04 22:39:35 -05003117/**
James Smart3772a992009-05-22 14:50:54 -04003118 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003119 * @phba: pointer to lpfc hba data structure.
3120 *
James Smart3772a992009-05-22 14:50:54 -04003121 * This routine is invoked to enable the PCI device that is common to all
3122 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003123 *
3124 * Return codes
James Smart3772a992009-05-22 14:50:54 -04003125 * 0 - sucessful
3126 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003127 **/
James Smart3772a992009-05-22 14:50:54 -04003128static int
3129lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003130{
James Smart3772a992009-05-22 14:50:54 -04003131 struct pci_dev *pdev;
3132 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003133
James Smart3772a992009-05-22 14:50:54 -04003134 /* Obtain PCI device reference */
3135 if (!phba->pcidev)
3136 goto out_error;
3137 else
3138 pdev = phba->pcidev;
3139 /* Select PCI BARs */
3140 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3141 /* Enable PCI device */
3142 if (pci_enable_device_mem(pdev))
3143 goto out_error;
3144 /* Request PCI resource for the device */
3145 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3146 goto out_disable_device;
3147 /* Set up device as PCI master and save state for EEH */
3148 pci_set_master(pdev);
3149 pci_try_set_mwi(pdev);
3150 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003151
James Smart3772a992009-05-22 14:50:54 -04003152 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003153
James Smart3772a992009-05-22 14:50:54 -04003154out_disable_device:
3155 pci_disable_device(pdev);
3156out_error:
3157 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003158}
3159
3160/**
James Smart3772a992009-05-22 14:50:54 -04003161 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003162 * @phba: pointer to lpfc hba data structure.
3163 *
James Smart3772a992009-05-22 14:50:54 -04003164 * This routine is invoked to disable the PCI device that is common to all
3165 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003166 **/
3167static void
James Smart3772a992009-05-22 14:50:54 -04003168lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003169{
James Smart3772a992009-05-22 14:50:54 -04003170 struct pci_dev *pdev;
3171 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003172
James Smart3772a992009-05-22 14:50:54 -04003173 /* Obtain PCI device reference */
3174 if (!phba->pcidev)
3175 return;
3176 else
3177 pdev = phba->pcidev;
3178 /* Select PCI BARs */
3179 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3180 /* Release PCI resource and disable PCI device */
3181 pci_release_selected_regions(pdev, bars);
3182 pci_disable_device(pdev);
3183 /* Null out PCI private reference to driver */
3184 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003185
3186 return;
3187}
3188
3189/**
James Smart3772a992009-05-22 14:50:54 -04003190 * lpfc_reset_hba - Reset a hba
3191 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003192 *
James Smart3772a992009-05-22 14:50:54 -04003193 * This routine is invoked to reset a hba device. It brings the HBA
3194 * offline, performs a board restart, and then brings the board back
3195 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3196 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003197 **/
James Smart3772a992009-05-22 14:50:54 -04003198void
3199lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003200{
James Smart3772a992009-05-22 14:50:54 -04003201 /* If resets are disabled then set error state and return. */
3202 if (!phba->cfg_enable_hba_reset) {
3203 phba->link_state = LPFC_HBA_ERROR;
3204 return;
3205 }
3206 lpfc_offline_prep(phba);
3207 lpfc_offline(phba);
3208 lpfc_sli_brdrestart(phba);
3209 lpfc_online(phba);
3210 lpfc_unblock_mgmt_io(phba);
3211}
dea31012005-04-17 16:05:31 -05003212
James Smart3772a992009-05-22 14:50:54 -04003213/**
3214 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3215 * @phba: pointer to lpfc hba data structure.
3216 *
3217 * This routine is invoked to set up the driver internal resources specific to
3218 * support the SLI-3 HBA device it attached to.
3219 *
3220 * Return codes
3221 * 0 - sucessful
3222 * other values - error
3223 **/
3224static int
3225lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3226{
3227 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003228
James Smart3772a992009-05-22 14:50:54 -04003229 /*
3230 * Initialize timers used by driver
3231 */
dea31012005-04-17 16:05:31 -05003232
James Smart3772a992009-05-22 14:50:54 -04003233 /* Heartbeat timer */
3234 init_timer(&phba->hb_tmofunc);
3235 phba->hb_tmofunc.function = lpfc_hb_timeout;
3236 phba->hb_tmofunc.data = (unsigned long)phba;
3237
3238 psli = &phba->sli;
3239 /* MBOX heartbeat timer */
3240 init_timer(&psli->mbox_tmo);
3241 psli->mbox_tmo.function = lpfc_mbox_timeout;
3242 psli->mbox_tmo.data = (unsigned long) phba;
3243 /* FCP polling mode timer */
3244 init_timer(&phba->fcp_poll_timer);
3245 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3246 phba->fcp_poll_timer.data = (unsigned long) phba;
3247 /* Fabric block timer */
3248 init_timer(&phba->fabric_block_timer);
3249 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3250 phba->fabric_block_timer.data = (unsigned long) phba;
3251 /* EA polling mode timer */
3252 init_timer(&phba->eratt_poll);
3253 phba->eratt_poll.function = lpfc_poll_eratt;
3254 phba->eratt_poll.data = (unsigned long) phba;
3255
3256 /* Host attention work mask setup */
3257 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3258 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3259
3260 /* Get all the module params for configuring this host */
3261 lpfc_get_cfgparam(phba);
3262 /*
3263 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3264 * used to create the sg_dma_buf_pool must be dynamically calculated.
3265 * 2 segments are added since the IOCB needs a command and response bde.
3266 */
3267 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3268 sizeof(struct fcp_rsp) +
3269 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3270
3271 if (phba->cfg_enable_bg) {
3272 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3273 phba->cfg_sg_dma_buf_size +=
3274 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3275 }
3276
3277 /* Also reinitialize the host templates with new values. */
3278 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3279 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3280
3281 phba->max_vpi = LPFC_MAX_VPI;
3282 /* This will be set to correct value after config_port mbox */
3283 phba->max_vports = 0;
3284
3285 /*
3286 * Initialize the SLI Layer to run with lpfc HBAs.
3287 */
3288 lpfc_sli_setup(phba);
3289 lpfc_sli_queue_setup(phba);
3290
3291 /* Allocate device driver memory */
3292 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3293 return -ENOMEM;
3294
3295 return 0;
3296}
3297
3298/**
3299 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3300 * @phba: pointer to lpfc hba data structure.
3301 *
3302 * This routine is invoked to unset the driver internal resources set up
3303 * specific for supporting the SLI-3 HBA device it attached to.
3304 **/
3305static void
3306lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3307{
3308 /* Free device driver memory allocated */
3309 lpfc_mem_free_all(phba);
3310
3311 return;
3312}
3313
3314/**
James Smartda0436e2009-05-22 14:51:39 -04003315 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3316 * @phba: pointer to lpfc hba data structure.
3317 *
3318 * This routine is invoked to set up the driver internal resources specific to
3319 * support the SLI-4 HBA device it attached to.
3320 *
3321 * Return codes
3322 * 0 - sucessful
3323 * other values - error
3324 **/
3325static int
3326lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3327{
3328 struct lpfc_sli *psli;
3329 int rc;
3330 int i, hbq_count;
3331
3332 /* Before proceed, wait for POST done and device ready */
3333 rc = lpfc_sli4_post_status_check(phba);
3334 if (rc)
3335 return -ENODEV;
3336
3337 /*
3338 * Initialize timers used by driver
3339 */
3340
3341 /* Heartbeat timer */
3342 init_timer(&phba->hb_tmofunc);
3343 phba->hb_tmofunc.function = lpfc_hb_timeout;
3344 phba->hb_tmofunc.data = (unsigned long)phba;
3345
3346 psli = &phba->sli;
3347 /* MBOX heartbeat timer */
3348 init_timer(&psli->mbox_tmo);
3349 psli->mbox_tmo.function = lpfc_mbox_timeout;
3350 psli->mbox_tmo.data = (unsigned long) phba;
3351 /* Fabric block timer */
3352 init_timer(&phba->fabric_block_timer);
3353 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3354 phba->fabric_block_timer.data = (unsigned long) phba;
3355 /* EA polling mode timer */
3356 init_timer(&phba->eratt_poll);
3357 phba->eratt_poll.function = lpfc_poll_eratt;
3358 phba->eratt_poll.data = (unsigned long) phba;
3359 /*
3360 * We need to do a READ_CONFIG mailbox command here before
3361 * calling lpfc_get_cfgparam. For VFs this will report the
3362 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3363 * All of the resources allocated
3364 * for this Port are tied to these values.
3365 */
3366 /* Get all the module params for configuring this host */
3367 lpfc_get_cfgparam(phba);
3368 phba->max_vpi = LPFC_MAX_VPI;
3369 /* This will be set to correct value after the read_config mbox */
3370 phba->max_vports = 0;
3371
3372 /* Program the default value of vlan_id and fc_map */
3373 phba->valid_vlan = 0;
3374 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3375 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3376 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3377
3378 /*
3379 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3380 * used to create the sg_dma_buf_pool must be dynamically calculated.
3381 * 2 segments are added since the IOCB needs a command and response bde.
3382 * To insure that the scsi sgl does not cross a 4k page boundary only
3383 * sgl sizes of 1k, 2k, 4k, and 8k are supported.
3384 * Table of sgl sizes and seg_cnt:
3385 * sgl size, sg_seg_cnt total seg
3386 * 1k 50 52
3387 * 2k 114 116
3388 * 4k 242 244
3389 * 8k 498 500
3390 * cmd(32) + rsp(160) + (52 * sizeof(sli4_sge)) = 1024
3391 * cmd(32) + rsp(160) + (116 * sizeof(sli4_sge)) = 2048
3392 * cmd(32) + rsp(160) + (244 * sizeof(sli4_sge)) = 4096
3393 * cmd(32) + rsp(160) + (500 * sizeof(sli4_sge)) = 8192
3394 */
3395 if (phba->cfg_sg_seg_cnt <= LPFC_DEFAULT_SG_SEG_CNT)
3396 phba->cfg_sg_seg_cnt = 50;
3397 else if (phba->cfg_sg_seg_cnt <= 114)
3398 phba->cfg_sg_seg_cnt = 114;
3399 else if (phba->cfg_sg_seg_cnt <= 242)
3400 phba->cfg_sg_seg_cnt = 242;
3401 else
3402 phba->cfg_sg_seg_cnt = 498;
3403
3404 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd)
3405 + sizeof(struct fcp_rsp);
3406 phba->cfg_sg_dma_buf_size +=
3407 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
3408
3409 /* Initialize buffer queue management fields */
3410 hbq_count = lpfc_sli_hbq_count();
3411 for (i = 0; i < hbq_count; ++i)
3412 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3413 INIT_LIST_HEAD(&phba->rb_pend_list);
3414 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3415 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3416
3417 /*
3418 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3419 */
3420 /* Initialize the Abort scsi buffer list used by driver */
3421 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3422 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3423 /* This abort list used by worker thread */
3424 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3425
3426 /*
3427 * Initialize dirver internal slow-path work queues
3428 */
3429
3430 /* Driver internel slow-path CQ Event pool */
3431 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3432 /* Response IOCB work queue list */
3433 INIT_LIST_HEAD(&phba->sli4_hba.sp_rspiocb_work_queue);
3434 /* Asynchronous event CQ Event work queue list */
3435 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3436 /* Fast-path XRI aborted CQ Event work queue list */
3437 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3438 /* Slow-path XRI aborted CQ Event work queue list */
3439 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3440 /* Receive queue CQ Event work queue list */
3441 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3442
3443 /* Initialize the driver internal SLI layer lists. */
3444 lpfc_sli_setup(phba);
3445 lpfc_sli_queue_setup(phba);
3446
3447 /* Allocate device driver memory */
3448 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3449 if (rc)
3450 return -ENOMEM;
3451
3452 /* Create the bootstrap mailbox command */
3453 rc = lpfc_create_bootstrap_mbox(phba);
3454 if (unlikely(rc))
3455 goto out_free_mem;
3456
3457 /* Set up the host's endian order with the device. */
3458 rc = lpfc_setup_endian_order(phba);
3459 if (unlikely(rc))
3460 goto out_free_bsmbx;
3461
3462 /* Set up the hba's configuration parameters. */
3463 rc = lpfc_sli4_read_config(phba);
3464 if (unlikely(rc))
3465 goto out_free_bsmbx;
3466
3467 /* Perform a function reset */
3468 rc = lpfc_pci_function_reset(phba);
3469 if (unlikely(rc))
3470 goto out_free_bsmbx;
3471
3472 /* Create all the SLI4 queues */
3473 rc = lpfc_sli4_queue_create(phba);
3474 if (rc)
3475 goto out_free_bsmbx;
3476
3477 /* Create driver internal CQE event pool */
3478 rc = lpfc_sli4_cq_event_pool_create(phba);
3479 if (rc)
3480 goto out_destroy_queue;
3481
3482 /* Initialize and populate the iocb list per host */
3483 rc = lpfc_init_sgl_list(phba);
3484 if (rc) {
3485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3486 "1400 Failed to initialize sgl list.\n");
3487 goto out_destroy_cq_event_pool;
3488 }
3489 rc = lpfc_init_active_sgl_array(phba);
3490 if (rc) {
3491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3492 "1430 Failed to initialize sgl list.\n");
3493 goto out_free_sgl_list;
3494 }
3495
3496 rc = lpfc_sli4_init_rpi_hdrs(phba);
3497 if (rc) {
3498 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3499 "1432 Failed to initialize rpi headers.\n");
3500 goto out_free_active_sgl;
3501 }
3502
3503 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3504 phba->cfg_fcp_eq_count), GFP_KERNEL);
3505 if (!phba->sli4_hba.fcp_eq_hdl) {
3506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3507 "2572 Failed allocate memory for fast-path "
3508 "per-EQ handle array\n");
3509 goto out_remove_rpi_hdrs;
3510 }
3511
3512 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3513 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3514 if (!phba->sli4_hba.msix_entries) {
3515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3516 "2573 Failed allocate memory for msi-x "
3517 "interrupt vector entries\n");
3518 goto out_free_fcp_eq_hdl;
3519 }
3520
3521 return rc;
3522
3523out_free_fcp_eq_hdl:
3524 kfree(phba->sli4_hba.fcp_eq_hdl);
3525out_remove_rpi_hdrs:
3526 lpfc_sli4_remove_rpi_hdrs(phba);
3527out_free_active_sgl:
3528 lpfc_free_active_sgl(phba);
3529out_free_sgl_list:
3530 lpfc_free_sgl_list(phba);
3531out_destroy_cq_event_pool:
3532 lpfc_sli4_cq_event_pool_destroy(phba);
3533out_destroy_queue:
3534 lpfc_sli4_queue_destroy(phba);
3535out_free_bsmbx:
3536 lpfc_destroy_bootstrap_mbox(phba);
3537out_free_mem:
3538 lpfc_mem_free(phba);
3539 return rc;
3540}
3541
3542/**
3543 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3544 * @phba: pointer to lpfc hba data structure.
3545 *
3546 * This routine is invoked to unset the driver internal resources set up
3547 * specific for supporting the SLI-4 HBA device it attached to.
3548 **/
3549static void
3550lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3551{
3552 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3553
3554 /* unregister default FCFI from the HBA */
3555 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3556
3557 /* Free the default FCR table */
3558 lpfc_sli_remove_dflt_fcf(phba);
3559
3560 /* Free memory allocated for msi-x interrupt vector entries */
3561 kfree(phba->sli4_hba.msix_entries);
3562
3563 /* Free memory allocated for fast-path work queue handles */
3564 kfree(phba->sli4_hba.fcp_eq_hdl);
3565
3566 /* Free the allocated rpi headers. */
3567 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04003568 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04003569
3570 /* Free the ELS sgl list */
3571 lpfc_free_active_sgl(phba);
3572 lpfc_free_sgl_list(phba);
3573
3574 /* Free the SCSI sgl management array */
3575 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3576
3577 /* Free the SLI4 queues */
3578 lpfc_sli4_queue_destroy(phba);
3579
3580 /* Free the completion queue EQ event pool */
3581 lpfc_sli4_cq_event_release_all(phba);
3582 lpfc_sli4_cq_event_pool_destroy(phba);
3583
3584 /* Reset SLI4 HBA FCoE function */
3585 lpfc_pci_function_reset(phba);
3586
3587 /* Free the bsmbx region. */
3588 lpfc_destroy_bootstrap_mbox(phba);
3589
3590 /* Free the SLI Layer memory with SLI4 HBAs */
3591 lpfc_mem_free_all(phba);
3592
3593 /* Free the current connect table */
3594 list_for_each_entry_safe(conn_entry, next_conn_entry,
3595 &phba->fcf_conn_rec_list, list)
3596 kfree(conn_entry);
3597
3598 return;
3599}
3600
3601/**
James Smart3772a992009-05-22 14:50:54 -04003602 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3603 * @phba: The hba struct for which this call is being executed.
3604 * @dev_grp: The HBA PCI-Device group number.
3605 *
3606 * This routine sets up the device INIT interface API function jump table
3607 * in @phba struct.
3608 *
3609 * Returns: 0 - success, -ENODEV - failure.
3610 **/
3611int
3612lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3613{
3614 switch (dev_grp) {
3615 case LPFC_PCI_DEV_LP:
3616 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3617 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3618 phba->lpfc_stop_port = lpfc_stop_port_s3;
3619 break;
James Smartda0436e2009-05-22 14:51:39 -04003620 case LPFC_PCI_DEV_OC:
3621 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3622 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3623 phba->lpfc_stop_port = lpfc_stop_port_s4;
3624 break;
James Smart3772a992009-05-22 14:50:54 -04003625 default:
3626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3627 "1431 Invalid HBA PCI-device group: 0x%x\n",
3628 dev_grp);
3629 return -ENODEV;
3630 break;
3631 }
3632 return 0;
3633}
3634
3635/**
3636 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3637 * @phba: pointer to lpfc hba data structure.
3638 *
3639 * This routine is invoked to set up the driver internal resources before the
3640 * device specific resource setup to support the HBA device it attached to.
3641 *
3642 * Return codes
3643 * 0 - sucessful
3644 * other values - error
3645 **/
3646static int
3647lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3648{
3649 /*
3650 * Driver resources common to all SLI revisions
3651 */
James Smartea2151b2008-09-07 11:52:10 -04003652 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003653 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003654
James Smarte47c9092008-02-08 18:49:26 -05003655 /* Initialize ndlp management spinlock */
3656 spin_lock_init(&phba->ndlp_lock);
3657
James Smart2e0fef82007-06-17 19:56:36 -05003658 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04003659 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04003660 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05003661
James Smart3772a992009-05-22 14:50:54 -04003662 /* Initialize the wait queue head for the kernel thread */
3663 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05003664
James Smart3772a992009-05-22 14:50:54 -04003665 /* Initialize the scsi buffer list used by driver for scsi IO */
3666 spin_lock_init(&phba->scsi_buf_list_lock);
3667 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05003668
James Smart3772a992009-05-22 14:50:54 -04003669 /* Initialize the fabric iocb list */
3670 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05003671
James Smart3772a992009-05-22 14:50:54 -04003672 /* Initialize list to save ELS buffers */
3673 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05003674
James Smart3772a992009-05-22 14:50:54 -04003675 /* Initialize FCF connection rec list */
3676 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05003677
James Smart3772a992009-05-22 14:50:54 -04003678 return 0;
3679}
dea31012005-04-17 16:05:31 -05003680
James Smart3772a992009-05-22 14:50:54 -04003681/**
3682 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
3683 * @phba: pointer to lpfc hba data structure.
3684 *
3685 * This routine is invoked to set up the driver internal resources after the
3686 * device specific resource setup to support the HBA device it attached to.
3687 *
3688 * Return codes
3689 * 0 - sucessful
3690 * other values - error
3691 **/
3692static int
3693lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
3694{
3695 int error;
3696
3697 /* Startup the kernel thread for this host adapter. */
3698 phba->worker_thread = kthread_run(lpfc_do_work, phba,
3699 "lpfc_worker_%d", phba->brd_no);
3700 if (IS_ERR(phba->worker_thread)) {
3701 error = PTR_ERR(phba->worker_thread);
3702 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003703 }
3704
James Smart3772a992009-05-22 14:50:54 -04003705 return 0;
3706}
3707
3708/**
3709 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
3710 * @phba: pointer to lpfc hba data structure.
3711 *
3712 * This routine is invoked to unset the driver internal resources set up after
3713 * the device specific resource setup for supporting the HBA device it
3714 * attached to.
3715 **/
3716static void
3717lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
3718{
3719 /* Stop kernel worker thread */
3720 kthread_stop(phba->worker_thread);
3721}
3722
3723/**
3724 * lpfc_free_iocb_list - Free iocb list.
3725 * @phba: pointer to lpfc hba data structure.
3726 *
3727 * This routine is invoked to free the driver's IOCB list and memory.
3728 **/
3729static void
3730lpfc_free_iocb_list(struct lpfc_hba *phba)
3731{
3732 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
3733
3734 spin_lock_irq(&phba->hbalock);
3735 list_for_each_entry_safe(iocbq_entry, iocbq_next,
3736 &phba->lpfc_iocb_list, list) {
3737 list_del(&iocbq_entry->list);
3738 kfree(iocbq_entry);
3739 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003740 }
James Smart3772a992009-05-22 14:50:54 -04003741 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003742
James Smart3772a992009-05-22 14:50:54 -04003743 return;
3744}
dea31012005-04-17 16:05:31 -05003745
James Smart3772a992009-05-22 14:50:54 -04003746/**
3747 * lpfc_init_iocb_list - Allocate and initialize iocb list.
3748 * @phba: pointer to lpfc hba data structure.
3749 *
3750 * This routine is invoked to allocate and initizlize the driver's IOCB
3751 * list and set up the IOCB tag array accordingly.
3752 *
3753 * Return codes
3754 * 0 - sucessful
3755 * other values - error
3756 **/
3757static int
3758lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
3759{
3760 struct lpfc_iocbq *iocbq_entry = NULL;
3761 uint16_t iotag;
3762 int i;
dea31012005-04-17 16:05:31 -05003763
3764 /* Initialize and populate the iocb list per host. */
3765 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04003766 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003767 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003768 if (iocbq_entry == NULL) {
3769 printk(KERN_ERR "%s: only allocated %d iocbs of "
3770 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003771 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05003772 goto out_free_iocbq;
3773 }
3774
James Bottomley604a3e32005-10-29 10:28:33 -05003775 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
3776 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04003777 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05003778 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04003779 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05003780 goto out_free_iocbq;
3781 }
James Smart3772a992009-05-22 14:50:54 -04003782 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05003783
3784 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003785 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
3786 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05003787 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003788 }
3789
James Smart3772a992009-05-22 14:50:54 -04003790 return 0;
3791
3792out_free_iocbq:
3793 lpfc_free_iocb_list(phba);
3794
3795 return -ENOMEM;
3796}
3797
3798/**
James Smartda0436e2009-05-22 14:51:39 -04003799 * lpfc_free_sgl_list - Free sgl list.
3800 * @phba: pointer to lpfc hba data structure.
3801 *
3802 * This routine is invoked to free the driver's sgl list and memory.
3803 **/
3804static void
3805lpfc_free_sgl_list(struct lpfc_hba *phba)
3806{
3807 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
3808 LIST_HEAD(sglq_list);
3809 int rc = 0;
3810
3811 spin_lock_irq(&phba->hbalock);
3812 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
3813 spin_unlock_irq(&phba->hbalock);
3814
3815 list_for_each_entry_safe(sglq_entry, sglq_next,
3816 &sglq_list, list) {
3817 list_del(&sglq_entry->list);
3818 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
3819 kfree(sglq_entry);
3820 phba->sli4_hba.total_sglq_bufs--;
3821 }
3822 rc = lpfc_sli4_remove_all_sgl_pages(phba);
3823 if (rc) {
3824 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3825 "2005 Unable to deregister pages from HBA: %x", rc);
3826 }
3827 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3828}
3829
3830/**
3831 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
3832 * @phba: pointer to lpfc hba data structure.
3833 *
3834 * This routine is invoked to allocate the driver's active sgl memory.
3835 * This array will hold the sglq_entry's for active IOs.
3836 **/
3837static int
3838lpfc_init_active_sgl_array(struct lpfc_hba *phba)
3839{
3840 int size;
3841 size = sizeof(struct lpfc_sglq *);
3842 size *= phba->sli4_hba.max_cfg_param.max_xri;
3843
3844 phba->sli4_hba.lpfc_sglq_active_list =
3845 kzalloc(size, GFP_KERNEL);
3846 if (!phba->sli4_hba.lpfc_sglq_active_list)
3847 return -ENOMEM;
3848 return 0;
3849}
3850
3851/**
3852 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3853 * @phba: pointer to lpfc hba data structure.
3854 *
3855 * This routine is invoked to walk through the array of active sglq entries
3856 * and free all of the resources.
3857 * This is just a place holder for now.
3858 **/
3859static void
3860lpfc_free_active_sgl(struct lpfc_hba *phba)
3861{
3862 kfree(phba->sli4_hba.lpfc_sglq_active_list);
3863}
3864
3865/**
3866 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3867 * @phba: pointer to lpfc hba data structure.
3868 *
3869 * This routine is invoked to allocate and initizlize the driver's sgl
3870 * list and set up the sgl xritag tag array accordingly.
3871 *
3872 * Return codes
3873 * 0 - sucessful
3874 * other values - error
3875 **/
3876static int
3877lpfc_init_sgl_list(struct lpfc_hba *phba)
3878{
3879 struct lpfc_sglq *sglq_entry = NULL;
3880 int i;
3881 int els_xri_cnt;
3882
3883 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3884 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3885 "2400 lpfc_init_sgl_list els %d.\n",
3886 els_xri_cnt);
3887 /* Initialize and populate the sglq list per host/VF. */
3888 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
3889 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
3890
3891 /* Sanity check on XRI management */
3892 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
3893 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3894 "2562 No room left for SCSI XRI allocation: "
3895 "max_xri=%d, els_xri=%d\n",
3896 phba->sli4_hba.max_cfg_param.max_xri,
3897 els_xri_cnt);
3898 return -ENOMEM;
3899 }
3900
3901 /* Allocate memory for the ELS XRI management array */
3902 phba->sli4_hba.lpfc_els_sgl_array =
3903 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
3904 GFP_KERNEL);
3905
3906 if (!phba->sli4_hba.lpfc_els_sgl_array) {
3907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3908 "2401 Failed to allocate memory for ELS "
3909 "XRI management array of size %d.\n",
3910 els_xri_cnt);
3911 return -ENOMEM;
3912 }
3913
3914 /* Keep the SCSI XRI into the XRI management array */
3915 phba->sli4_hba.scsi_xri_max =
3916 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3917 phba->sli4_hba.scsi_xri_cnt = 0;
3918
3919 phba->sli4_hba.lpfc_scsi_psb_array =
3920 kzalloc((sizeof(struct lpfc_scsi_buf *) *
3921 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
3922
3923 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
3924 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3925 "2563 Failed to allocate memory for SCSI "
3926 "XRI management array of size %d.\n",
3927 phba->sli4_hba.scsi_xri_max);
3928 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3929 return -ENOMEM;
3930 }
3931
3932 for (i = 0; i < els_xri_cnt; i++) {
3933 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
3934 if (sglq_entry == NULL) {
3935 printk(KERN_ERR "%s: only allocated %d sgls of "
3936 "expected %d count. Unloading driver.\n",
3937 __func__, i, els_xri_cnt);
3938 goto out_free_mem;
3939 }
3940
3941 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
3942 if (sglq_entry->sli4_xritag == NO_XRI) {
3943 kfree(sglq_entry);
3944 printk(KERN_ERR "%s: failed to allocate XRI.\n"
3945 "Unloading driver.\n", __func__);
3946 goto out_free_mem;
3947 }
3948 sglq_entry->buff_type = GEN_BUFF_TYPE;
3949 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
3950 if (sglq_entry->virt == NULL) {
3951 kfree(sglq_entry);
3952 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
3953 "Unloading driver.\n", __func__);
3954 goto out_free_mem;
3955 }
3956 sglq_entry->sgl = sglq_entry->virt;
3957 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3958
3959 /* The list order is used by later block SGL registraton */
3960 spin_lock_irq(&phba->hbalock);
3961 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
3962 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
3963 phba->sli4_hba.total_sglq_bufs++;
3964 spin_unlock_irq(&phba->hbalock);
3965 }
3966 return 0;
3967
3968out_free_mem:
3969 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3970 lpfc_free_sgl_list(phba);
3971 return -ENOMEM;
3972}
3973
3974/**
3975 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
3976 * @phba: pointer to lpfc hba data structure.
3977 *
3978 * This routine is invoked to post rpi header templates to the
3979 * HBA consistent with the SLI-4 interface spec. This routine
3980 * posts a PAGE_SIZE memory region to the port to hold up to
3981 * PAGE_SIZE modulo 64 rpi context headers.
3982 * No locks are held here because this is an initialization routine
3983 * called only from probe or lpfc_online when interrupts are not
3984 * enabled and the driver is reinitializing the device.
3985 *
3986 * Return codes
3987 * 0 - sucessful
3988 * ENOMEM - No availble memory
3989 * EIO - The mailbox failed to complete successfully.
3990 **/
3991int
3992lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
3993{
3994 int rc = 0;
3995 int longs;
3996 uint16_t rpi_count;
3997 struct lpfc_rpi_hdr *rpi_hdr;
3998
3999 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4000
4001 /*
4002 * Provision an rpi bitmask range for discovery. The total count
4003 * is the difference between max and base + 1.
4004 */
4005 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4006 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4007
4008 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4009 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4010 GFP_KERNEL);
4011 if (!phba->sli4_hba.rpi_bmask)
4012 return -ENOMEM;
4013
4014 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4015 if (!rpi_hdr) {
4016 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4017 "0391 Error during rpi post operation\n");
4018 lpfc_sli4_remove_rpis(phba);
4019 rc = -ENODEV;
4020 }
4021
4022 return rc;
4023}
4024
4025/**
4026 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4027 * @phba: pointer to lpfc hba data structure.
4028 *
4029 * This routine is invoked to allocate a single 4KB memory region to
4030 * support rpis and stores them in the phba. This single region
4031 * provides support for up to 64 rpis. The region is used globally
4032 * by the device.
4033 *
4034 * Returns:
4035 * A valid rpi hdr on success.
4036 * A NULL pointer on any failure.
4037 **/
4038struct lpfc_rpi_hdr *
4039lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4040{
4041 uint16_t rpi_limit, curr_rpi_range;
4042 struct lpfc_dmabuf *dmabuf;
4043 struct lpfc_rpi_hdr *rpi_hdr;
4044
4045 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4046 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4047
4048 spin_lock_irq(&phba->hbalock);
4049 curr_rpi_range = phba->sli4_hba.next_rpi;
4050 spin_unlock_irq(&phba->hbalock);
4051
4052 /*
4053 * The port has a limited number of rpis. The increment here
4054 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4055 * and to allow the full max_rpi range per port.
4056 */
4057 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4058 return NULL;
4059
4060 /*
4061 * First allocate the protocol header region for the port. The
4062 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4063 */
4064 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4065 if (!dmabuf)
4066 return NULL;
4067
4068 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4069 LPFC_HDR_TEMPLATE_SIZE,
4070 &dmabuf->phys,
4071 GFP_KERNEL);
4072 if (!dmabuf->virt) {
4073 rpi_hdr = NULL;
4074 goto err_free_dmabuf;
4075 }
4076
4077 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4078 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4079 rpi_hdr = NULL;
4080 goto err_free_coherent;
4081 }
4082
4083 /* Save the rpi header data for cleanup later. */
4084 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4085 if (!rpi_hdr)
4086 goto err_free_coherent;
4087
4088 rpi_hdr->dmabuf = dmabuf;
4089 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4090 rpi_hdr->page_count = 1;
4091 spin_lock_irq(&phba->hbalock);
4092 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4093 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4094
4095 /*
4096 * The next_rpi stores the next module-64 rpi value to post
4097 * in any subsequent rpi memory region postings.
4098 */
4099 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4100 spin_unlock_irq(&phba->hbalock);
4101 return rpi_hdr;
4102
4103 err_free_coherent:
4104 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4105 dmabuf->virt, dmabuf->phys);
4106 err_free_dmabuf:
4107 kfree(dmabuf);
4108 return NULL;
4109}
4110
4111/**
4112 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4113 * @phba: pointer to lpfc hba data structure.
4114 *
4115 * This routine is invoked to remove all memory resources allocated
4116 * to support rpis. This routine presumes the caller has released all
4117 * rpis consumed by fabric or port logins and is prepared to have
4118 * the header pages removed.
4119 **/
4120void
4121lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4122{
4123 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4124
4125 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4126 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4127 list_del(&rpi_hdr->list);
4128 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4129 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4130 kfree(rpi_hdr->dmabuf);
4131 kfree(rpi_hdr);
4132 }
4133
4134 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4135 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4136}
4137
4138/**
James Smart3772a992009-05-22 14:50:54 -04004139 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4140 * @pdev: pointer to pci device data structure.
4141 *
4142 * This routine is invoked to allocate the driver hba data structure for an
4143 * HBA device. If the allocation is successful, the phba reference to the
4144 * PCI device data structure is set.
4145 *
4146 * Return codes
4147 * pointer to @phba - sucessful
4148 * NULL - error
4149 **/
4150static struct lpfc_hba *
4151lpfc_hba_alloc(struct pci_dev *pdev)
4152{
4153 struct lpfc_hba *phba;
4154
4155 /* Allocate memory for HBA structure */
4156 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4157 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004158 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004159 return NULL;
4160 }
4161
4162 /* Set reference to PCI device in HBA structure */
4163 phba->pcidev = pdev;
4164
4165 /* Assign an unused board number */
4166 phba->brd_no = lpfc_get_instance();
4167 if (phba->brd_no < 0) {
4168 kfree(phba);
4169 return NULL;
4170 }
4171
4172 return phba;
4173}
4174
4175/**
4176 * lpfc_hba_free - Free driver hba data structure with a device.
4177 * @phba: pointer to lpfc hba data structure.
4178 *
4179 * This routine is invoked to free the driver hba data structure with an
4180 * HBA device.
4181 **/
4182static void
4183lpfc_hba_free(struct lpfc_hba *phba)
4184{
4185 /* Release the driver assigned board number */
4186 idr_remove(&lpfc_hba_index, phba->brd_no);
4187
4188 kfree(phba);
4189 return;
4190}
4191
4192/**
4193 * lpfc_create_shost - Create hba physical port with associated scsi host.
4194 * @phba: pointer to lpfc hba data structure.
4195 *
4196 * This routine is invoked to create HBA physical port and associate a SCSI
4197 * host with it.
4198 *
4199 * Return codes
4200 * 0 - sucessful
4201 * other values - error
4202 **/
4203static int
4204lpfc_create_shost(struct lpfc_hba *phba)
4205{
4206 struct lpfc_vport *vport;
4207 struct Scsi_Host *shost;
4208
4209 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004210 phba->fc_edtov = FF_DEF_EDTOV;
4211 phba->fc_ratov = FF_DEF_RATOV;
4212 phba->fc_altov = FF_DEF_ALTOV;
4213 phba->fc_arbtov = FF_DEF_ARBTOV;
4214
James Smart3de2a652007-08-02 11:09:59 -04004215 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004216 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004217 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004218
4219 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004220 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004221 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004222 /* Put reference to SCSI host to driver's device private data */
4223 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004224
James Smart3772a992009-05-22 14:50:54 -04004225 return 0;
4226}
dea31012005-04-17 16:05:31 -05004227
James Smart3772a992009-05-22 14:50:54 -04004228/**
4229 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4230 * @phba: pointer to lpfc hba data structure.
4231 *
4232 * This routine is invoked to destroy HBA physical port and the associated
4233 * SCSI host.
4234 **/
4235static void
4236lpfc_destroy_shost(struct lpfc_hba *phba)
4237{
4238 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004239
James Smart3772a992009-05-22 14:50:54 -04004240 /* Destroy physical port that associated with the SCSI host */
4241 destroy_port(vport);
4242
4243 return;
4244}
4245
4246/**
4247 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4248 * @phba: pointer to lpfc hba data structure.
4249 * @shost: the shost to be used to detect Block guard settings.
4250 *
4251 * This routine sets up the local Block guard protocol settings for @shost.
4252 * This routine also allocates memory for debugging bg buffers.
4253 **/
4254static void
4255lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4256{
4257 int pagecnt = 10;
4258 if (lpfc_prot_mask && lpfc_prot_guard) {
4259 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4260 "1478 Registering BlockGuard with the "
4261 "SCSI layer\n");
4262 scsi_host_set_prot(shost, lpfc_prot_mask);
4263 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004264 }
James Smart81301a92008-12-04 22:39:46 -05004265 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004266 while (pagecnt) {
4267 spin_lock_init(&_dump_buf_lock);
4268 _dump_buf_data =
4269 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4270 if (_dump_buf_data) {
4271 printk(KERN_ERR "BLKGRD allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004272 "_dump_buf_data at 0x%p\n",
4273 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004274 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004275 memset(_dump_buf_data, 0,
4276 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004277 break;
James Smart3772a992009-05-22 14:50:54 -04004278 } else
James Smart81301a92008-12-04 22:39:46 -05004279 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004280 }
James Smart81301a92008-12-04 22:39:46 -05004281 if (!_dump_buf_data_order)
4282 printk(KERN_ERR "BLKGRD ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004283 "memory for hexdump\n");
4284 } else
James Smart81301a92008-12-04 22:39:46 -05004285 printk(KERN_ERR "BLKGRD already allocated _dump_buf_data=0x%p"
4286 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004287 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004288 while (pagecnt) {
4289 _dump_buf_dif =
4290 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4291 if (_dump_buf_dif) {
4292 printk(KERN_ERR "BLKGRD allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004293 "_dump_buf_dif at 0x%p\n",
4294 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004295 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004296 memset(_dump_buf_dif, 0,
4297 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004298 break;
James Smart3772a992009-05-22 14:50:54 -04004299 } else
James Smart81301a92008-12-04 22:39:46 -05004300 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004301 }
James Smart81301a92008-12-04 22:39:46 -05004302 if (!_dump_buf_dif_order)
4303 printk(KERN_ERR "BLKGRD ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004304 "memory for hexdump\n");
4305 } else
James Smart81301a92008-12-04 22:39:46 -05004306 printk(KERN_ERR "BLKGRD already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004307 _dump_buf_dif);
4308}
4309
4310/**
4311 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4312 * @phba: pointer to lpfc hba data structure.
4313 *
4314 * This routine is invoked to perform all the necessary post initialization
4315 * setup for the device.
4316 **/
4317static void
4318lpfc_post_init_setup(struct lpfc_hba *phba)
4319{
4320 struct Scsi_Host *shost;
4321 struct lpfc_adapter_event_header adapter_event;
4322
4323 /* Get the default values for Model Name and Description */
4324 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4325
4326 /*
4327 * hba setup may have changed the hba_queue_depth so we need to
4328 * adjust the value of can_queue.
4329 */
4330 shost = pci_get_drvdata(phba->pcidev);
4331 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4332 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4333 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004334
4335 lpfc_host_attrib_init(shost);
4336
James Smart2e0fef82007-06-17 19:56:36 -05004337 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4338 spin_lock_irq(shost->host_lock);
4339 lpfc_poll_start_timer(phba);
4340 spin_unlock_irq(shost->host_lock);
4341 }
James Smart8f6d98d2006-08-01 07:34:00 -04004342
James Smart93996272008-08-24 21:50:30 -04004343 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4344 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004345 /* Send board arrival event to upper layer */
4346 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4347 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4348 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004349 sizeof(adapter_event),
4350 (char *) &adapter_event,
4351 LPFC_NL_VENDOR_ID);
4352 return;
4353}
4354
4355/**
4356 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4357 * @phba: pointer to lpfc hba data structure.
4358 *
4359 * This routine is invoked to set up the PCI device memory space for device
4360 * with SLI-3 interface spec.
4361 *
4362 * Return codes
4363 * 0 - sucessful
4364 * other values - error
4365 **/
4366static int
4367lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4368{
4369 struct pci_dev *pdev;
4370 unsigned long bar0map_len, bar2map_len;
4371 int i, hbq_count;
4372 void *ptr;
4373 int error = -ENODEV;
4374
4375 /* Obtain PCI device reference */
4376 if (!phba->pcidev)
4377 return error;
4378 else
4379 pdev = phba->pcidev;
4380
4381 /* Set the device DMA mask size */
4382 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
4383 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
4384 return error;
4385
4386 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4387 * required by each mapping.
4388 */
4389 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4390 bar0map_len = pci_resource_len(pdev, 0);
4391
4392 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4393 bar2map_len = pci_resource_len(pdev, 2);
4394
4395 /* Map HBA SLIM to a kernel virtual address. */
4396 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4397 if (!phba->slim_memmap_p) {
4398 dev_printk(KERN_ERR, &pdev->dev,
4399 "ioremap failed for SLIM memory.\n");
4400 goto out;
4401 }
4402
4403 /* Map HBA Control Registers to a kernel virtual address. */
4404 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4405 if (!phba->ctrl_regs_memmap_p) {
4406 dev_printk(KERN_ERR, &pdev->dev,
4407 "ioremap failed for HBA control registers.\n");
4408 goto out_iounmap_slim;
4409 }
4410
4411 /* Allocate memory for SLI-2 structures */
4412 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4413 SLI2_SLIM_SIZE,
4414 &phba->slim2p.phys,
4415 GFP_KERNEL);
4416 if (!phba->slim2p.virt)
4417 goto out_iounmap;
4418
4419 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4420 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4421 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4422 phba->IOCBs = (phba->slim2p.virt +
4423 offsetof(struct lpfc_sli2_slim, IOCBs));
4424
4425 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4426 lpfc_sli_hbq_size(),
4427 &phba->hbqslimp.phys,
4428 GFP_KERNEL);
4429 if (!phba->hbqslimp.virt)
4430 goto out_free_slim;
4431
4432 hbq_count = lpfc_sli_hbq_count();
4433 ptr = phba->hbqslimp.virt;
4434 for (i = 0; i < hbq_count; ++i) {
4435 phba->hbqs[i].hbq_virt = ptr;
4436 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4437 ptr += (lpfc_hbq_defs[i]->entry_count *
4438 sizeof(struct lpfc_hbq_entry));
4439 }
4440 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4441 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4442
4443 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4444
4445 INIT_LIST_HEAD(&phba->rb_pend_list);
4446
4447 phba->MBslimaddr = phba->slim_memmap_p;
4448 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4449 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4450 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4451 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004452
dea31012005-04-17 16:05:31 -05004453 return 0;
4454
dea31012005-04-17 16:05:31 -05004455out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004456 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4457 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004458out_iounmap:
4459 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004460out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004461 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004462out:
4463 return error;
4464}
4465
James Smarte59058c2008-08-24 21:49:00 -04004466/**
James Smart3772a992009-05-22 14:50:54 -04004467 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4468 * @phba: pointer to lpfc hba data structure.
4469 *
4470 * This routine is invoked to unset the PCI device memory space for device
4471 * with SLI-3 interface spec.
4472 **/
4473static void
4474lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4475{
4476 struct pci_dev *pdev;
4477
4478 /* Obtain PCI device reference */
4479 if (!phba->pcidev)
4480 return;
4481 else
4482 pdev = phba->pcidev;
4483
4484 /* Free coherent DMA memory allocated */
4485 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4486 phba->hbqslimp.virt, phba->hbqslimp.phys);
4487 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4488 phba->slim2p.virt, phba->slim2p.phys);
4489
4490 /* I/O memory unmap */
4491 iounmap(phba->ctrl_regs_memmap_p);
4492 iounmap(phba->slim_memmap_p);
4493
4494 return;
4495}
4496
4497/**
James Smartda0436e2009-05-22 14:51:39 -04004498 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4499 * @phba: pointer to lpfc hba data structure.
4500 *
4501 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4502 * done and check status.
4503 *
4504 * Return 0 if successful, otherwise -ENODEV.
4505 **/
4506int
4507lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4508{
4509 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg, scratchpad;
4510 uint32_t onlnreg0, onlnreg1;
4511 int i, port_error = -ENODEV;
4512
4513 if (!phba->sli4_hba.STAregaddr)
4514 return -ENODEV;
4515
James Smartda0436e2009-05-22 14:51:39 -04004516 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4517 for (i = 0; i < 3000; i++) {
4518 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4519 /* Encounter fatal POST error, break out */
4520 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4521 port_error = -ENODEV;
4522 break;
4523 }
4524 if (LPFC_POST_STAGE_ARMFW_READY ==
4525 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4526 port_error = 0;
4527 break;
4528 }
4529 msleep(10);
4530 }
4531
4532 if (port_error)
4533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4534 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4535 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4536 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4537 bf_get(lpfc_hst_state_perr, &sta_reg),
4538 bf_get(lpfc_hst_state_sfi, &sta_reg),
4539 bf_get(lpfc_hst_state_nip, &sta_reg),
4540 bf_get(lpfc_hst_state_ipc, &sta_reg),
4541 bf_get(lpfc_hst_state_xrom, &sta_reg),
4542 bf_get(lpfc_hst_state_dl, &sta_reg),
4543 bf_get(lpfc_hst_state_port_status, &sta_reg));
4544
4545 /* Log device information */
4546 scratchpad.word0 = readl(phba->sli4_hba.SCRATCHPADregaddr);
4547 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4548 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4549 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4550 bf_get(lpfc_scratchpad_chiptype, &scratchpad),
4551 bf_get(lpfc_scratchpad_slirev, &scratchpad),
4552 bf_get(lpfc_scratchpad_featurelevel1, &scratchpad),
4553 bf_get(lpfc_scratchpad_featurelevel2, &scratchpad));
4554
James Smart8fa38512009-07-19 10:01:03 -04004555 /* With uncoverable error, log the error message and return error */
4556 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
4557 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
4558 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
4559 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4560 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4561 if (uerrlo_reg.word0 || uerrhi_reg.word0) {
4562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4563 "1422 HBA Unrecoverable error: "
4564 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4565 "online0_reg=0x%x, online1_reg=0x%x\n",
4566 uerrlo_reg.word0, uerrhi_reg.word0,
4567 onlnreg0, onlnreg1);
4568 }
4569 return -ENODEV;
4570 }
4571
James Smartda0436e2009-05-22 14:51:39 -04004572 return port_error;
4573}
4574
4575/**
4576 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4577 * @phba: pointer to lpfc hba data structure.
4578 *
4579 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4580 * memory map.
4581 **/
4582static void
4583lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4584{
4585 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4586 LPFC_UERR_STATUS_LO;
4587 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4588 LPFC_UERR_STATUS_HI;
4589 phba->sli4_hba.ONLINE0regaddr = phba->sli4_hba.conf_regs_memmap_p +
4590 LPFC_ONLINE0;
4591 phba->sli4_hba.ONLINE1regaddr = phba->sli4_hba.conf_regs_memmap_p +
4592 LPFC_ONLINE1;
4593 phba->sli4_hba.SCRATCHPADregaddr = phba->sli4_hba.conf_regs_memmap_p +
4594 LPFC_SCRATCHPAD;
4595}
4596
4597/**
4598 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4599 * @phba: pointer to lpfc hba data structure.
4600 *
4601 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4602 * memory map.
4603 **/
4604static void
4605lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4606{
4607
4608 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4609 LPFC_HST_STATE;
4610 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4611 LPFC_HST_ISR0;
4612 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4613 LPFC_HST_IMR0;
4614 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4615 LPFC_HST_ISCR0;
4616 return;
4617}
4618
4619/**
4620 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
4621 * @phba: pointer to lpfc hba data structure.
4622 * @vf: virtual function number
4623 *
4624 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4625 * based on the given viftual function number, @vf.
4626 *
4627 * Return 0 if successful, otherwise -ENODEV.
4628 **/
4629static int
4630lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
4631{
4632 if (vf > LPFC_VIR_FUNC_MAX)
4633 return -ENODEV;
4634
4635 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4636 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4637 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4638 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4639 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4640 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
4641 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4642 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
4643 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4644 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
4645 return 0;
4646}
4647
4648/**
4649 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
4650 * @phba: pointer to lpfc hba data structure.
4651 *
4652 * This routine is invoked to create the bootstrap mailbox
4653 * region consistent with the SLI-4 interface spec. This
4654 * routine allocates all memory necessary to communicate
4655 * mailbox commands to the port and sets up all alignment
4656 * needs. No locks are expected to be held when calling
4657 * this routine.
4658 *
4659 * Return codes
4660 * 0 - sucessful
4661 * ENOMEM - could not allocated memory.
4662 **/
4663static int
4664lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
4665{
4666 uint32_t bmbx_size;
4667 struct lpfc_dmabuf *dmabuf;
4668 struct dma_address *dma_address;
4669 uint32_t pa_addr;
4670 uint64_t phys_addr;
4671
4672 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4673 if (!dmabuf)
4674 return -ENOMEM;
4675
4676 /*
4677 * The bootstrap mailbox region is comprised of 2 parts
4678 * plus an alignment restriction of 16 bytes.
4679 */
4680 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
4681 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4682 bmbx_size,
4683 &dmabuf->phys,
4684 GFP_KERNEL);
4685 if (!dmabuf->virt) {
4686 kfree(dmabuf);
4687 return -ENOMEM;
4688 }
4689 memset(dmabuf->virt, 0, bmbx_size);
4690
4691 /*
4692 * Initialize the bootstrap mailbox pointers now so that the register
4693 * operations are simple later. The mailbox dma address is required
4694 * to be 16-byte aligned. Also align the virtual memory as each
4695 * maibox is copied into the bmbx mailbox region before issuing the
4696 * command to the port.
4697 */
4698 phba->sli4_hba.bmbx.dmabuf = dmabuf;
4699 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
4700
4701 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
4702 LPFC_ALIGN_16_BYTE);
4703 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
4704 LPFC_ALIGN_16_BYTE);
4705
4706 /*
4707 * Set the high and low physical addresses now. The SLI4 alignment
4708 * requirement is 16 bytes and the mailbox is posted to the port
4709 * as two 30-bit addresses. The other data is a bit marking whether
4710 * the 30-bit address is the high or low address.
4711 * Upcast bmbx aphys to 64bits so shift instruction compiles
4712 * clean on 32 bit machines.
4713 */
4714 dma_address = &phba->sli4_hba.bmbx.dma_address;
4715 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
4716 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
4717 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
4718 LPFC_BMBX_BIT1_ADDR_HI);
4719
4720 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
4721 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
4722 LPFC_BMBX_BIT1_ADDR_LO);
4723 return 0;
4724}
4725
4726/**
4727 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
4728 * @phba: pointer to lpfc hba data structure.
4729 *
4730 * This routine is invoked to teardown the bootstrap mailbox
4731 * region and release all host resources. This routine requires
4732 * the caller to ensure all mailbox commands recovered, no
4733 * additional mailbox comands are sent, and interrupts are disabled
4734 * before calling this routine.
4735 *
4736 **/
4737static void
4738lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
4739{
4740 dma_free_coherent(&phba->pcidev->dev,
4741 phba->sli4_hba.bmbx.bmbx_size,
4742 phba->sli4_hba.bmbx.dmabuf->virt,
4743 phba->sli4_hba.bmbx.dmabuf->phys);
4744
4745 kfree(phba->sli4_hba.bmbx.dmabuf);
4746 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
4747}
4748
4749/**
4750 * lpfc_sli4_read_config - Get the config parameters.
4751 * @phba: pointer to lpfc hba data structure.
4752 *
4753 * This routine is invoked to read the configuration parameters from the HBA.
4754 * The configuration parameters are used to set the base and maximum values
4755 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
4756 * allocation for the port.
4757 *
4758 * Return codes
4759 * 0 - sucessful
4760 * ENOMEM - No availble memory
4761 * EIO - The mailbox failed to complete successfully.
4762 **/
4763static int
4764lpfc_sli4_read_config(struct lpfc_hba *phba)
4765{
4766 LPFC_MBOXQ_t *pmb;
4767 struct lpfc_mbx_read_config *rd_config;
4768 uint32_t rc = 0;
4769
4770 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4771 if (!pmb) {
4772 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4773 "2011 Unable to allocate memory for issuing "
4774 "SLI_CONFIG_SPECIAL mailbox command\n");
4775 return -ENOMEM;
4776 }
4777
4778 lpfc_read_config(phba, pmb);
4779
4780 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4781 if (rc != MBX_SUCCESS) {
4782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4783 "2012 Mailbox failed , mbxCmd x%x "
4784 "READ_CONFIG, mbxStatus x%x\n",
4785 bf_get(lpfc_mqe_command, &pmb->u.mqe),
4786 bf_get(lpfc_mqe_status, &pmb->u.mqe));
4787 rc = -EIO;
4788 } else {
4789 rd_config = &pmb->u.mqe.un.rd_config;
4790 phba->sli4_hba.max_cfg_param.max_xri =
4791 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
4792 phba->sli4_hba.max_cfg_param.xri_base =
4793 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
4794 phba->sli4_hba.max_cfg_param.max_vpi =
4795 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
4796 phba->sli4_hba.max_cfg_param.vpi_base =
4797 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
4798 phba->sli4_hba.max_cfg_param.max_rpi =
4799 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
4800 phba->sli4_hba.max_cfg_param.rpi_base =
4801 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
4802 phba->sli4_hba.max_cfg_param.max_vfi =
4803 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
4804 phba->sli4_hba.max_cfg_param.vfi_base =
4805 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
4806 phba->sli4_hba.max_cfg_param.max_fcfi =
4807 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
4808 phba->sli4_hba.max_cfg_param.fcfi_base =
4809 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
4810 phba->sli4_hba.max_cfg_param.max_eq =
4811 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
4812 phba->sli4_hba.max_cfg_param.max_rq =
4813 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
4814 phba->sli4_hba.max_cfg_param.max_wq =
4815 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
4816 phba->sli4_hba.max_cfg_param.max_cq =
4817 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
4818 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
4819 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
4820 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
4821 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
4822 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4823 phba->max_vpi = phba->sli4_hba.max_cfg_param.max_vpi;
4824 phba->max_vports = phba->max_vpi;
4825 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4826 "2003 cfg params XRI(B:%d M:%d), "
4827 "VPI(B:%d M:%d) "
4828 "VFI(B:%d M:%d) "
4829 "RPI(B:%d M:%d) "
4830 "FCFI(B:%d M:%d)\n",
4831 phba->sli4_hba.max_cfg_param.xri_base,
4832 phba->sli4_hba.max_cfg_param.max_xri,
4833 phba->sli4_hba.max_cfg_param.vpi_base,
4834 phba->sli4_hba.max_cfg_param.max_vpi,
4835 phba->sli4_hba.max_cfg_param.vfi_base,
4836 phba->sli4_hba.max_cfg_param.max_vfi,
4837 phba->sli4_hba.max_cfg_param.rpi_base,
4838 phba->sli4_hba.max_cfg_param.max_rpi,
4839 phba->sli4_hba.max_cfg_param.fcfi_base,
4840 phba->sli4_hba.max_cfg_param.max_fcfi);
4841 }
4842 mempool_free(pmb, phba->mbox_mem_pool);
4843
4844 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
4845 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
4846 phba->cfg_hba_queue_depth =
4847 phba->sli4_hba.max_cfg_param.max_xri;
4848 return rc;
4849}
4850
4851/**
4852 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
4853 * @phba: pointer to lpfc hba data structure.
4854 *
4855 * This routine is invoked to setup the host-side endian order to the
4856 * HBA consistent with the SLI-4 interface spec.
4857 *
4858 * Return codes
4859 * 0 - sucessful
4860 * ENOMEM - No availble memory
4861 * EIO - The mailbox failed to complete successfully.
4862 **/
4863static int
4864lpfc_setup_endian_order(struct lpfc_hba *phba)
4865{
4866 LPFC_MBOXQ_t *mboxq;
4867 uint32_t rc = 0;
4868 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
4869 HOST_ENDIAN_HIGH_WORD1};
4870
4871 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4872 if (!mboxq) {
4873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4874 "0492 Unable to allocate memory for issuing "
4875 "SLI_CONFIG_SPECIAL mailbox command\n");
4876 return -ENOMEM;
4877 }
4878
4879 /*
4880 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
4881 * words to contain special data values and no other data.
4882 */
4883 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
4884 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
4885 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4886 if (rc != MBX_SUCCESS) {
4887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4888 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
4889 "status x%x\n",
4890 rc);
4891 rc = -EIO;
4892 }
4893
4894 mempool_free(mboxq, phba->mbox_mem_pool);
4895 return rc;
4896}
4897
4898/**
4899 * lpfc_sli4_queue_create - Create all the SLI4 queues
4900 * @phba: pointer to lpfc hba data structure.
4901 *
4902 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
4903 * operation. For each SLI4 queue type, the parameters such as queue entry
4904 * count (queue depth) shall be taken from the module parameter. For now,
4905 * we just use some constant number as place holder.
4906 *
4907 * Return codes
4908 * 0 - sucessful
4909 * ENOMEM - No availble memory
4910 * EIO - The mailbox failed to complete successfully.
4911 **/
4912static int
4913lpfc_sli4_queue_create(struct lpfc_hba *phba)
4914{
4915 struct lpfc_queue *qdesc;
4916 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
4917 int cfg_fcp_wq_count;
4918 int cfg_fcp_eq_count;
4919
4920 /*
4921 * Sanity check for confiugred queue parameters against the run-time
4922 * device parameters
4923 */
4924
4925 /* Sanity check on FCP fast-path WQ parameters */
4926 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
4927 if (cfg_fcp_wq_count >
4928 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
4929 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
4930 LPFC_SP_WQN_DEF;
4931 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
4932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4933 "2581 Not enough WQs (%d) from "
4934 "the pci function for supporting "
4935 "FCP WQs (%d)\n",
4936 phba->sli4_hba.max_cfg_param.max_wq,
4937 phba->cfg_fcp_wq_count);
4938 goto out_error;
4939 }
4940 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4941 "2582 Not enough WQs (%d) from the pci "
4942 "function for supporting the requested "
4943 "FCP WQs (%d), the actual FCP WQs can "
4944 "be supported: %d\n",
4945 phba->sli4_hba.max_cfg_param.max_wq,
4946 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
4947 }
4948 /* The actual number of FCP work queues adopted */
4949 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
4950
4951 /* Sanity check on FCP fast-path EQ parameters */
4952 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
4953 if (cfg_fcp_eq_count >
4954 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
4955 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
4956 LPFC_SP_EQN_DEF;
4957 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
4958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4959 "2574 Not enough EQs (%d) from the "
4960 "pci function for supporting FCP "
4961 "EQs (%d)\n",
4962 phba->sli4_hba.max_cfg_param.max_eq,
4963 phba->cfg_fcp_eq_count);
4964 goto out_error;
4965 }
4966 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4967 "2575 Not enough EQs (%d) from the pci "
4968 "function for supporting the requested "
4969 "FCP EQs (%d), the actual FCP EQs can "
4970 "be supported: %d\n",
4971 phba->sli4_hba.max_cfg_param.max_eq,
4972 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
4973 }
4974 /* It does not make sense to have more EQs than WQs */
4975 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
4976 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4977 "2593 The number of FCP EQs (%d) is more "
4978 "than the number of FCP WQs (%d), take "
4979 "the number of FCP EQs same as than of "
4980 "WQs (%d)\n", cfg_fcp_eq_count,
4981 phba->cfg_fcp_wq_count,
4982 phba->cfg_fcp_wq_count);
4983 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
4984 }
4985 /* The actual number of FCP event queues adopted */
4986 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
4987 /* The overall number of event queues used */
4988 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
4989
4990 /*
4991 * Create Event Queues (EQs)
4992 */
4993
4994 /* Get EQ depth from module parameter, fake the default for now */
4995 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
4996 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
4997
4998 /* Create slow path event queue */
4999 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5000 phba->sli4_hba.eq_ecount);
5001 if (!qdesc) {
5002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5003 "0496 Failed allocate slow-path EQ\n");
5004 goto out_error;
5005 }
5006 phba->sli4_hba.sp_eq = qdesc;
5007
5008 /* Create fast-path FCP Event Queue(s) */
5009 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5010 phba->cfg_fcp_eq_count), GFP_KERNEL);
5011 if (!phba->sli4_hba.fp_eq) {
5012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5013 "2576 Failed allocate memory for fast-path "
5014 "EQ record array\n");
5015 goto out_free_sp_eq;
5016 }
5017 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5018 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5019 phba->sli4_hba.eq_ecount);
5020 if (!qdesc) {
5021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5022 "0497 Failed allocate fast-path EQ\n");
5023 goto out_free_fp_eq;
5024 }
5025 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5026 }
5027
5028 /*
5029 * Create Complete Queues (CQs)
5030 */
5031
5032 /* Get CQ depth from module parameter, fake the default for now */
5033 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5034 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5035
5036 /* Create slow-path Mailbox Command Complete Queue */
5037 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5038 phba->sli4_hba.cq_ecount);
5039 if (!qdesc) {
5040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5041 "0500 Failed allocate slow-path mailbox CQ\n");
5042 goto out_free_fp_eq;
5043 }
5044 phba->sli4_hba.mbx_cq = qdesc;
5045
5046 /* Create slow-path ELS Complete Queue */
5047 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5048 phba->sli4_hba.cq_ecount);
5049 if (!qdesc) {
5050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5051 "0501 Failed allocate slow-path ELS CQ\n");
5052 goto out_free_mbx_cq;
5053 }
5054 phba->sli4_hba.els_cq = qdesc;
5055
5056 /* Create slow-path Unsolicited Receive Complete Queue */
5057 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5058 phba->sli4_hba.cq_ecount);
5059 if (!qdesc) {
5060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5061 "0502 Failed allocate slow-path USOL RX CQ\n");
5062 goto out_free_els_cq;
5063 }
5064 phba->sli4_hba.rxq_cq = qdesc;
5065
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");
5073 goto out_free_rxq_cq;
5074 }
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);
5186out_free_rxq_cq:
5187 lpfc_sli4_queue_free(phba->sli4_hba.rxq_cq);
5188 phba->sli4_hba.rxq_cq = NULL;
5189out_free_els_cq:
5190 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5191 phba->sli4_hba.els_cq = NULL;
5192out_free_mbx_cq:
5193 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5194 phba->sli4_hba.mbx_cq = NULL;
5195out_free_fp_eq:
5196 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5197 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5198 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5199 }
5200 kfree(phba->sli4_hba.fp_eq);
5201out_free_sp_eq:
5202 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5203 phba->sli4_hba.sp_eq = NULL;
5204out_error:
5205 return -ENOMEM;
5206}
5207
5208/**
5209 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5210 * @phba: pointer to lpfc hba data structure.
5211 *
5212 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5213 * operation.
5214 *
5215 * Return codes
5216 * 0 - sucessful
5217 * ENOMEM - No availble memory
5218 * EIO - The mailbox failed to complete successfully.
5219 **/
5220static void
5221lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5222{
5223 int fcp_qidx;
5224
5225 /* Release mailbox command work queue */
5226 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5227 phba->sli4_hba.mbx_wq = NULL;
5228
5229 /* Release ELS work queue */
5230 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5231 phba->sli4_hba.els_wq = NULL;
5232
5233 /* Release FCP work queue */
5234 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5235 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5236 kfree(phba->sli4_hba.fcp_wq);
5237 phba->sli4_hba.fcp_wq = NULL;
5238
5239 /* Release unsolicited receive queue */
5240 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5241 phba->sli4_hba.hdr_rq = NULL;
5242 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5243 phba->sli4_hba.dat_rq = NULL;
5244
5245 /* Release unsolicited receive complete queue */
5246 lpfc_sli4_queue_free(phba->sli4_hba.rxq_cq);
5247 phba->sli4_hba.rxq_cq = NULL;
5248
5249 /* Release ELS complete queue */
5250 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5251 phba->sli4_hba.els_cq = NULL;
5252
5253 /* Release mailbox command complete queue */
5254 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5255 phba->sli4_hba.mbx_cq = NULL;
5256
5257 /* Release FCP response complete queue */
5258 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5259 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5260 kfree(phba->sli4_hba.fcp_cq);
5261 phba->sli4_hba.fcp_cq = NULL;
5262
5263 /* Release fast-path event queue */
5264 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5265 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5266 kfree(phba->sli4_hba.fp_eq);
5267 phba->sli4_hba.fp_eq = NULL;
5268
5269 /* Release slow-path event queue */
5270 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5271 phba->sli4_hba.sp_eq = NULL;
5272
5273 return;
5274}
5275
5276/**
5277 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5278 * @phba: pointer to lpfc hba data structure.
5279 *
5280 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5281 * operation.
5282 *
5283 * Return codes
5284 * 0 - sucessful
5285 * ENOMEM - No availble memory
5286 * EIO - The mailbox failed to complete successfully.
5287 **/
5288int
5289lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5290{
5291 int rc = -ENOMEM;
5292 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5293 int fcp_cq_index = 0;
5294
5295 /*
5296 * Set up Event Queues (EQs)
5297 */
5298
5299 /* Set up slow-path event queue */
5300 if (!phba->sli4_hba.sp_eq) {
5301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5302 "0520 Slow-path EQ not allocated\n");
5303 goto out_error;
5304 }
5305 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5306 LPFC_SP_DEF_IMAX);
5307 if (rc) {
5308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5309 "0521 Failed setup of slow-path EQ: "
5310 "rc = 0x%x\n", rc);
5311 goto out_error;
5312 }
5313 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5314 "2583 Slow-path EQ setup: queue-id=%d\n",
5315 phba->sli4_hba.sp_eq->queue_id);
5316
5317 /* Set up fast-path event queue */
5318 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5319 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5321 "0522 Fast-path EQ (%d) not "
5322 "allocated\n", fcp_eqidx);
5323 goto out_destroy_fp_eq;
5324 }
5325 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5326 phba->cfg_fcp_imax);
5327 if (rc) {
5328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5329 "0523 Failed setup of fast-path EQ "
5330 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5331 goto out_destroy_fp_eq;
5332 }
5333 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5334 "2584 Fast-path EQ setup: "
5335 "queue[%d]-id=%d\n", fcp_eqidx,
5336 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5337 }
5338
5339 /*
5340 * Set up Complete Queues (CQs)
5341 */
5342
5343 /* Set up slow-path MBOX Complete Queue as the first CQ */
5344 if (!phba->sli4_hba.mbx_cq) {
5345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5346 "0528 Mailbox CQ not allocated\n");
5347 goto out_destroy_fp_eq;
5348 }
5349 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5350 LPFC_MCQ, LPFC_MBOX);
5351 if (rc) {
5352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5353 "0529 Failed setup of slow-path mailbox CQ: "
5354 "rc = 0x%x\n", rc);
5355 goto out_destroy_fp_eq;
5356 }
5357 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5358 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5359 phba->sli4_hba.mbx_cq->queue_id,
5360 phba->sli4_hba.sp_eq->queue_id);
5361
5362 /* Set up slow-path ELS Complete Queue */
5363 if (!phba->sli4_hba.els_cq) {
5364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5365 "0530 ELS CQ not allocated\n");
5366 goto out_destroy_mbx_cq;
5367 }
5368 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5369 LPFC_WCQ, LPFC_ELS);
5370 if (rc) {
5371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5372 "0531 Failed setup of slow-path ELS CQ: "
5373 "rc = 0x%x\n", rc);
5374 goto out_destroy_mbx_cq;
5375 }
5376 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5377 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5378 phba->sli4_hba.els_cq->queue_id,
5379 phba->sli4_hba.sp_eq->queue_id);
5380
5381 /* Set up slow-path Unsolicited Receive Complete Queue */
5382 if (!phba->sli4_hba.rxq_cq) {
5383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5384 "0532 USOL RX CQ not allocated\n");
5385 goto out_destroy_els_cq;
5386 }
5387 rc = lpfc_cq_create(phba, phba->sli4_hba.rxq_cq, phba->sli4_hba.sp_eq,
5388 LPFC_RCQ, LPFC_USOL);
5389 if (rc) {
5390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5391 "0533 Failed setup of slow-path USOL RX CQ: "
5392 "rc = 0x%x\n", rc);
5393 goto out_destroy_els_cq;
5394 }
5395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5396 "2587 USL CQ setup: cq-id=%d, parent eq-id=%d\n",
5397 phba->sli4_hba.rxq_cq->queue_id,
5398 phba->sli4_hba.sp_eq->queue_id);
5399
5400 /* Set up fast-path FCP Response Complete Queue */
5401 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5402 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5404 "0526 Fast-path FCP CQ (%d) not "
5405 "allocated\n", fcp_cqidx);
5406 goto out_destroy_fcp_cq;
5407 }
5408 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5409 phba->sli4_hba.fp_eq[fcp_cqidx],
5410 LPFC_WCQ, LPFC_FCP);
5411 if (rc) {
5412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5413 "0527 Failed setup of fast-path FCP "
5414 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5415 goto out_destroy_fcp_cq;
5416 }
5417 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5418 "2588 FCP CQ setup: cq[%d]-id=%d, "
5419 "parent eq[%d]-id=%d\n",
5420 fcp_cqidx,
5421 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5422 fcp_cqidx,
5423 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5424 }
5425
5426 /*
5427 * Set up all the Work Queues (WQs)
5428 */
5429
5430 /* Set up Mailbox Command Queue */
5431 if (!phba->sli4_hba.mbx_wq) {
5432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5433 "0538 Slow-path MQ not allocated\n");
5434 goto out_destroy_fcp_cq;
5435 }
5436 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5437 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5438 if (rc) {
5439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5440 "0539 Failed setup of slow-path MQ: "
5441 "rc = 0x%x\n", rc);
5442 goto out_destroy_fcp_cq;
5443 }
5444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5445 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5446 phba->sli4_hba.mbx_wq->queue_id,
5447 phba->sli4_hba.mbx_cq->queue_id);
5448
5449 /* Set up slow-path ELS Work Queue */
5450 if (!phba->sli4_hba.els_wq) {
5451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5452 "0536 Slow-path ELS WQ not allocated\n");
5453 goto out_destroy_mbx_wq;
5454 }
5455 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5456 phba->sli4_hba.els_cq, LPFC_ELS);
5457 if (rc) {
5458 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5459 "0537 Failed setup of slow-path ELS WQ: "
5460 "rc = 0x%x\n", rc);
5461 goto out_destroy_mbx_wq;
5462 }
5463 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5464 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5465 phba->sli4_hba.els_wq->queue_id,
5466 phba->sli4_hba.els_cq->queue_id);
5467
5468 /* Set up fast-path FCP Work Queue */
5469 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5470 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5472 "0534 Fast-path FCP WQ (%d) not "
5473 "allocated\n", fcp_wqidx);
5474 goto out_destroy_fcp_wq;
5475 }
5476 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5477 phba->sli4_hba.fcp_cq[fcp_cq_index],
5478 LPFC_FCP);
5479 if (rc) {
5480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5481 "0535 Failed setup of fast-path FCP "
5482 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5483 goto out_destroy_fcp_wq;
5484 }
5485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5486 "2591 FCP WQ setup: wq[%d]-id=%d, "
5487 "parent cq[%d]-id=%d\n",
5488 fcp_wqidx,
5489 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5490 fcp_cq_index,
5491 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5492 /* Round robin FCP Work Queue's Completion Queue assignment */
5493 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5494 }
5495
5496 /*
5497 * Create Receive Queue (RQ)
5498 */
5499 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5501 "0540 Receive Queue not allocated\n");
5502 goto out_destroy_fcp_wq;
5503 }
5504 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
5505 phba->sli4_hba.rxq_cq, LPFC_USOL);
5506 if (rc) {
5507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5508 "0541 Failed setup of Receive Queue: "
5509 "rc = 0x%x\n", rc);
5510 goto out_destroy_fcp_wq;
5511 }
5512 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5513 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5514 "parent cq-id=%d\n",
5515 phba->sli4_hba.hdr_rq->queue_id,
5516 phba->sli4_hba.dat_rq->queue_id,
5517 phba->sli4_hba.rxq_cq->queue_id);
5518 return 0;
5519
5520out_destroy_fcp_wq:
5521 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5522 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5523 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5524out_destroy_mbx_wq:
5525 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5526out_destroy_fcp_cq:
5527 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5528 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
5529 lpfc_cq_destroy(phba, phba->sli4_hba.rxq_cq);
5530out_destroy_els_cq:
5531 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5532out_destroy_mbx_cq:
5533 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5534out_destroy_fp_eq:
5535 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5536 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5537 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5538out_error:
5539 return rc;
5540}
5541
5542/**
5543 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5544 * @phba: pointer to lpfc hba data structure.
5545 *
5546 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5547 * operation.
5548 *
5549 * Return codes
5550 * 0 - sucessful
5551 * ENOMEM - No availble memory
5552 * EIO - The mailbox failed to complete successfully.
5553 **/
5554void
5555lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5556{
5557 int fcp_qidx;
5558
5559 /* Unset mailbox command work queue */
5560 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5561 /* Unset ELS work queue */
5562 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5563 /* Unset unsolicited receive queue */
5564 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5565 /* Unset FCP work queue */
5566 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5567 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5568 /* Unset mailbox command complete queue */
5569 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5570 /* Unset ELS complete queue */
5571 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5572 /* Unset unsolicited receive complete queue */
5573 lpfc_cq_destroy(phba, phba->sli4_hba.rxq_cq);
5574 /* Unset FCP response complete queue */
5575 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5576 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5577 /* Unset fast-path event queue */
5578 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5579 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5580 /* Unset slow-path event queue */
5581 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5582}
5583
5584/**
5585 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5586 * @phba: pointer to lpfc hba data structure.
5587 *
5588 * This routine is invoked to allocate and set up a pool of completion queue
5589 * events. The body of the completion queue event is a completion queue entry
5590 * CQE. For now, this pool is used for the interrupt service routine to queue
5591 * the following HBA completion queue events for the worker thread to process:
5592 * - Mailbox asynchronous events
5593 * - Receive queue completion unsolicited events
5594 * Later, this can be used for all the slow-path events.
5595 *
5596 * Return codes
5597 * 0 - sucessful
5598 * -ENOMEM - No availble memory
5599 **/
5600static int
5601lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5602{
5603 struct lpfc_cq_event *cq_event;
5604 int i;
5605
5606 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5607 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5608 if (!cq_event)
5609 goto out_pool_create_fail;
5610 list_add_tail(&cq_event->list,
5611 &phba->sli4_hba.sp_cqe_event_pool);
5612 }
5613 return 0;
5614
5615out_pool_create_fail:
5616 lpfc_sli4_cq_event_pool_destroy(phba);
5617 return -ENOMEM;
5618}
5619
5620/**
5621 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5622 * @phba: pointer to lpfc hba data structure.
5623 *
5624 * This routine is invoked to free the pool of completion queue events at
5625 * driver unload time. Note that, it is the responsibility of the driver
5626 * cleanup routine to free all the outstanding completion-queue events
5627 * allocated from this pool back into the pool before invoking this routine
5628 * to destroy the pool.
5629 **/
5630static void
5631lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5632{
5633 struct lpfc_cq_event *cq_event, *next_cq_event;
5634
5635 list_for_each_entry_safe(cq_event, next_cq_event,
5636 &phba->sli4_hba.sp_cqe_event_pool, list) {
5637 list_del(&cq_event->list);
5638 kfree(cq_event);
5639 }
5640}
5641
5642/**
5643 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5644 * @phba: pointer to lpfc hba data structure.
5645 *
5646 * This routine is the lock free version of the API invoked to allocate a
5647 * completion-queue event from the free pool.
5648 *
5649 * Return: Pointer to the newly allocated completion-queue event if successful
5650 * NULL otherwise.
5651 **/
5652struct lpfc_cq_event *
5653__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5654{
5655 struct lpfc_cq_event *cq_event = NULL;
5656
5657 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5658 struct lpfc_cq_event, list);
5659 return cq_event;
5660}
5661
5662/**
5663 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5664 * @phba: pointer to lpfc hba data structure.
5665 *
5666 * This routine is the lock version of the API invoked to allocate a
5667 * completion-queue event from the free pool.
5668 *
5669 * Return: Pointer to the newly allocated completion-queue event if successful
5670 * NULL otherwise.
5671 **/
5672struct lpfc_cq_event *
5673lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5674{
5675 struct lpfc_cq_event *cq_event;
5676 unsigned long iflags;
5677
5678 spin_lock_irqsave(&phba->hbalock, iflags);
5679 cq_event = __lpfc_sli4_cq_event_alloc(phba);
5680 spin_unlock_irqrestore(&phba->hbalock, iflags);
5681 return cq_event;
5682}
5683
5684/**
5685 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5686 * @phba: pointer to lpfc hba data structure.
5687 * @cq_event: pointer to the completion queue event to be freed.
5688 *
5689 * This routine is the lock free version of the API invoked to release a
5690 * completion-queue event back into the free pool.
5691 **/
5692void
5693__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5694 struct lpfc_cq_event *cq_event)
5695{
5696 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
5697}
5698
5699/**
5700 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5701 * @phba: pointer to lpfc hba data structure.
5702 * @cq_event: pointer to the completion queue event to be freed.
5703 *
5704 * This routine is the lock version of the API invoked to release a
5705 * completion-queue event back into the free pool.
5706 **/
5707void
5708lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5709 struct lpfc_cq_event *cq_event)
5710{
5711 unsigned long iflags;
5712 spin_lock_irqsave(&phba->hbalock, iflags);
5713 __lpfc_sli4_cq_event_release(phba, cq_event);
5714 spin_unlock_irqrestore(&phba->hbalock, iflags);
5715}
5716
5717/**
5718 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
5719 * @phba: pointer to lpfc hba data structure.
5720 *
5721 * This routine is to free all the pending completion-queue events to the
5722 * back into the free pool for device reset.
5723 **/
5724static void
5725lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
5726{
5727 LIST_HEAD(cqelist);
5728 struct lpfc_cq_event *cqe;
5729 unsigned long iflags;
5730
5731 /* Retrieve all the pending WCQEs from pending WCQE lists */
5732 spin_lock_irqsave(&phba->hbalock, iflags);
5733 /* Pending FCP XRI abort events */
5734 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
5735 &cqelist);
5736 /* Pending ELS XRI abort events */
5737 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
5738 &cqelist);
5739 /* Pending asynnc events */
5740 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
5741 &cqelist);
5742 spin_unlock_irqrestore(&phba->hbalock, iflags);
5743
5744 while (!list_empty(&cqelist)) {
5745 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
5746 lpfc_sli4_cq_event_release(phba, cqe);
5747 }
5748}
5749
5750/**
5751 * lpfc_pci_function_reset - Reset pci function.
5752 * @phba: pointer to lpfc hba data structure.
5753 *
5754 * This routine is invoked to request a PCI function reset. It will destroys
5755 * all resources assigned to the PCI function which originates this request.
5756 *
5757 * Return codes
5758 * 0 - sucessful
5759 * ENOMEM - No availble memory
5760 * EIO - The mailbox failed to complete successfully.
5761 **/
5762int
5763lpfc_pci_function_reset(struct lpfc_hba *phba)
5764{
5765 LPFC_MBOXQ_t *mboxq;
5766 uint32_t rc = 0;
5767 uint32_t shdr_status, shdr_add_status;
5768 union lpfc_sli4_cfg_shdr *shdr;
5769
5770 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5771 if (!mboxq) {
5772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5773 "0494 Unable to allocate memory for issuing "
5774 "SLI_FUNCTION_RESET mailbox command\n");
5775 return -ENOMEM;
5776 }
5777
5778 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
5779 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5780 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
5781 LPFC_SLI4_MBX_EMBED);
5782 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5783 shdr = (union lpfc_sli4_cfg_shdr *)
5784 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5785 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5786 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5787 if (rc != MBX_TIMEOUT)
5788 mempool_free(mboxq, phba->mbox_mem_pool);
5789 if (shdr_status || shdr_add_status || rc) {
5790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5791 "0495 SLI_FUNCTION_RESET mailbox failed with "
5792 "status x%x add_status x%x, mbx status x%x\n",
5793 shdr_status, shdr_add_status, rc);
5794 rc = -ENXIO;
5795 }
5796 return rc;
5797}
5798
5799/**
5800 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
5801 * @phba: pointer to lpfc hba data structure.
5802 * @cnt: number of nop mailbox commands to send.
5803 *
5804 * This routine is invoked to send a number @cnt of NOP mailbox command and
5805 * wait for each command to complete.
5806 *
5807 * Return: the number of NOP mailbox command completed.
5808 **/
5809static int
5810lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
5811{
5812 LPFC_MBOXQ_t *mboxq;
5813 int length, cmdsent;
5814 uint32_t mbox_tmo;
5815 uint32_t rc = 0;
5816 uint32_t shdr_status, shdr_add_status;
5817 union lpfc_sli4_cfg_shdr *shdr;
5818
5819 if (cnt == 0) {
5820 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5821 "2518 Requested to send 0 NOP mailbox cmd\n");
5822 return cnt;
5823 }
5824
5825 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5826 if (!mboxq) {
5827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5828 "2519 Unable to allocate memory for issuing "
5829 "NOP mailbox command\n");
5830 return 0;
5831 }
5832
5833 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
5834 length = (sizeof(struct lpfc_mbx_nop) -
5835 sizeof(struct lpfc_sli4_cfg_mhdr));
5836 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5837 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
5838
5839 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5840 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
5841 if (!phba->sli4_hba.intr_enable)
5842 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5843 else
5844 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
5845 if (rc == MBX_TIMEOUT)
5846 break;
5847 /* Check return status */
5848 shdr = (union lpfc_sli4_cfg_shdr *)
5849 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5850 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5851 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
5852 &shdr->response);
5853 if (shdr_status || shdr_add_status || rc) {
5854 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5855 "2520 NOP mailbox command failed "
5856 "status x%x add_status x%x mbx "
5857 "status x%x\n", shdr_status,
5858 shdr_add_status, rc);
5859 break;
5860 }
5861 }
5862
5863 if (rc != MBX_TIMEOUT)
5864 mempool_free(mboxq, phba->mbox_mem_pool);
5865
5866 return cmdsent;
5867}
5868
5869/**
5870 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
5871 * @phba: pointer to lpfc hba data structure.
5872 * @fcfi: fcf index.
5873 *
5874 * This routine is invoked to unregister a FCFI from device.
5875 **/
5876void
5877lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
5878{
5879 LPFC_MBOXQ_t *mbox;
5880 uint32_t mbox_tmo;
5881 int rc;
5882 unsigned long flags;
5883
5884 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5885
5886 if (!mbox)
5887 return;
5888
5889 lpfc_unreg_fcfi(mbox, fcfi);
5890
5891 if (!phba->sli4_hba.intr_enable)
5892 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5893 else {
5894 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5895 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5896 }
5897 if (rc != MBX_TIMEOUT)
5898 mempool_free(mbox, phba->mbox_mem_pool);
5899 if (rc != MBX_SUCCESS)
5900 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5901 "2517 Unregister FCFI command failed "
5902 "status %d, mbxStatus x%x\n", rc,
5903 bf_get(lpfc_mqe_status, &mbox->u.mqe));
5904 else {
5905 spin_lock_irqsave(&phba->hbalock, flags);
5906 /* Mark the FCFI is no longer registered */
5907 phba->fcf.fcf_flag &=
5908 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_DISCOVERED);
5909 spin_unlock_irqrestore(&phba->hbalock, flags);
5910 }
5911}
5912
5913/**
5914 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
5915 * @phba: pointer to lpfc hba data structure.
5916 *
5917 * This routine is invoked to set up the PCI device memory space for device
5918 * with SLI-4 interface spec.
5919 *
5920 * Return codes
5921 * 0 - sucessful
5922 * other values - error
5923 **/
5924static int
5925lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
5926{
5927 struct pci_dev *pdev;
5928 unsigned long bar0map_len, bar1map_len, bar2map_len;
5929 int error = -ENODEV;
5930
5931 /* Obtain PCI device reference */
5932 if (!phba->pcidev)
5933 return error;
5934 else
5935 pdev = phba->pcidev;
5936
5937 /* Set the device DMA mask size */
5938 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
5939 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5940 return error;
5941
5942 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
5943 * number of bytes required by each mapping. They are actually
5944 * mapping to the PCI BAR regions 1, 2, and 4 by the SLI4 device.
5945 */
5946 phba->pci_bar0_map = pci_resource_start(pdev, LPFC_SLI4_BAR0);
5947 bar0map_len = pci_resource_len(pdev, LPFC_SLI4_BAR0);
5948
5949 phba->pci_bar1_map = pci_resource_start(pdev, LPFC_SLI4_BAR1);
5950 bar1map_len = pci_resource_len(pdev, LPFC_SLI4_BAR1);
5951
5952 phba->pci_bar2_map = pci_resource_start(pdev, LPFC_SLI4_BAR2);
5953 bar2map_len = pci_resource_len(pdev, LPFC_SLI4_BAR2);
5954
5955 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
5956 phba->sli4_hba.conf_regs_memmap_p =
5957 ioremap(phba->pci_bar0_map, bar0map_len);
5958 if (!phba->sli4_hba.conf_regs_memmap_p) {
5959 dev_printk(KERN_ERR, &pdev->dev,
5960 "ioremap failed for SLI4 PCI config registers.\n");
5961 goto out;
5962 }
5963
5964 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
5965 phba->sli4_hba.ctrl_regs_memmap_p =
5966 ioremap(phba->pci_bar1_map, bar1map_len);
5967 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
5968 dev_printk(KERN_ERR, &pdev->dev,
5969 "ioremap failed for SLI4 HBA control registers.\n");
5970 goto out_iounmap_conf;
5971 }
5972
5973 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
5974 phba->sli4_hba.drbl_regs_memmap_p =
5975 ioremap(phba->pci_bar2_map, bar2map_len);
5976 if (!phba->sli4_hba.drbl_regs_memmap_p) {
5977 dev_printk(KERN_ERR, &pdev->dev,
5978 "ioremap failed for SLI4 HBA doorbell registers.\n");
5979 goto out_iounmap_ctrl;
5980 }
5981
5982 /* Set up BAR0 PCI config space register memory map */
5983 lpfc_sli4_bar0_register_memmap(phba);
5984
5985 /* Set up BAR1 register memory map */
5986 lpfc_sli4_bar1_register_memmap(phba);
5987
5988 /* Set up BAR2 register memory map */
5989 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
5990 if (error)
5991 goto out_iounmap_all;
5992
5993 return 0;
5994
5995out_iounmap_all:
5996 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
5997out_iounmap_ctrl:
5998 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
5999out_iounmap_conf:
6000 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6001out:
6002 return error;
6003}
6004
6005/**
6006 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6007 * @phba: pointer to lpfc hba data structure.
6008 *
6009 * This routine is invoked to unset the PCI device memory space for device
6010 * with SLI-4 interface spec.
6011 **/
6012static void
6013lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6014{
6015 struct pci_dev *pdev;
6016
6017 /* Obtain PCI device reference */
6018 if (!phba->pcidev)
6019 return;
6020 else
6021 pdev = phba->pcidev;
6022
6023 /* Free coherent DMA memory allocated */
6024
6025 /* Unmap I/O memory space */
6026 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6027 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6028 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6029
6030 return;
6031}
6032
6033/**
James Smart3772a992009-05-22 14:50:54 -04006034 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6035 * @phba: pointer to lpfc hba data structure.
6036 *
6037 * This routine is invoked to enable the MSI-X interrupt vectors to device
6038 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6039 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6040 * invoked, enables either all or nothing, depending on the current
6041 * availability of PCI vector resources. The device driver is responsible
6042 * for calling the individual request_irq() to register each MSI-X vector
6043 * with a interrupt handler, which is done in this function. Note that
6044 * later when device is unloading, the driver should always call free_irq()
6045 * on all MSI-X vectors it has done request_irq() on before calling
6046 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6047 * will be left with MSI-X enabled and leaks its vectors.
6048 *
6049 * Return codes
6050 * 0 - sucessful
6051 * other values - error
6052 **/
6053static int
6054lpfc_sli_enable_msix(struct lpfc_hba *phba)
6055{
6056 int rc, i;
6057 LPFC_MBOXQ_t *pmb;
6058
6059 /* Set up MSI-X multi-message vectors */
6060 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6061 phba->msix_entries[i].entry = i;
6062
6063 /* Configure MSI-X capability structure */
6064 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6065 ARRAY_SIZE(phba->msix_entries));
6066 if (rc) {
6067 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6068 "0420 PCI enable MSI-X failed (%d)\n", rc);
6069 goto msi_fail_out;
6070 }
6071 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6072 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6073 "0477 MSI-X entry[%d]: vector=x%x "
6074 "message=%d\n", i,
6075 phba->msix_entries[i].vector,
6076 phba->msix_entries[i].entry);
6077 /*
6078 * Assign MSI-X vectors to interrupt handlers
6079 */
6080
6081 /* vector-0 is associated to slow-path handler */
6082 rc = request_irq(phba->msix_entries[0].vector,
6083 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6084 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6085 if (rc) {
6086 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6087 "0421 MSI-X slow-path request_irq failed "
6088 "(%d)\n", rc);
6089 goto msi_fail_out;
6090 }
6091
6092 /* vector-1 is associated to fast-path handler */
6093 rc = request_irq(phba->msix_entries[1].vector,
6094 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6095 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6096
6097 if (rc) {
6098 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6099 "0429 MSI-X fast-path request_irq failed "
6100 "(%d)\n", rc);
6101 goto irq_fail_out;
6102 }
6103
6104 /*
6105 * Configure HBA MSI-X attention conditions to messages
6106 */
6107 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6108
6109 if (!pmb) {
6110 rc = -ENOMEM;
6111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6112 "0474 Unable to allocate memory for issuing "
6113 "MBOX_CONFIG_MSI command\n");
6114 goto mem_fail_out;
6115 }
6116 rc = lpfc_config_msi(phba, pmb);
6117 if (rc)
6118 goto mbx_fail_out;
6119 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6120 if (rc != MBX_SUCCESS) {
6121 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6122 "0351 Config MSI mailbox command failed, "
6123 "mbxCmd x%x, mbxStatus x%x\n",
6124 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6125 goto mbx_fail_out;
6126 }
6127
6128 /* Free memory allocated for mailbox command */
6129 mempool_free(pmb, phba->mbox_mem_pool);
6130 return rc;
6131
6132mbx_fail_out:
6133 /* Free memory allocated for mailbox command */
6134 mempool_free(pmb, phba->mbox_mem_pool);
6135
6136mem_fail_out:
6137 /* free the irq already requested */
6138 free_irq(phba->msix_entries[1].vector, phba);
6139
6140irq_fail_out:
6141 /* free the irq already requested */
6142 free_irq(phba->msix_entries[0].vector, phba);
6143
6144msi_fail_out:
6145 /* Unconfigure MSI-X capability structure */
6146 pci_disable_msix(phba->pcidev);
6147 return rc;
6148}
6149
6150/**
6151 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6152 * @phba: pointer to lpfc hba data structure.
6153 *
6154 * This routine is invoked to release the MSI-X vectors and then disable the
6155 * MSI-X interrupt mode to device with SLI-3 interface spec.
6156 **/
6157static void
6158lpfc_sli_disable_msix(struct lpfc_hba *phba)
6159{
6160 int i;
6161
6162 /* Free up MSI-X multi-message vectors */
6163 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6164 free_irq(phba->msix_entries[i].vector, phba);
6165 /* Disable MSI-X */
6166 pci_disable_msix(phba->pcidev);
6167
6168 return;
6169}
6170
6171/**
6172 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6173 * @phba: pointer to lpfc hba data structure.
6174 *
6175 * This routine is invoked to enable the MSI interrupt mode to device with
6176 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6177 * enable the MSI vector. The device driver is responsible for calling the
6178 * request_irq() to register MSI vector with a interrupt the handler, which
6179 * is done in this function.
6180 *
6181 * Return codes
6182 * 0 - sucessful
6183 * other values - error
6184 */
6185static int
6186lpfc_sli_enable_msi(struct lpfc_hba *phba)
6187{
6188 int rc;
6189
6190 rc = pci_enable_msi(phba->pcidev);
6191 if (!rc)
6192 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6193 "0462 PCI enable MSI mode success.\n");
6194 else {
6195 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6196 "0471 PCI enable MSI mode failed (%d)\n", rc);
6197 return rc;
6198 }
6199
6200 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6201 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6202 if (rc) {
6203 pci_disable_msi(phba->pcidev);
6204 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6205 "0478 MSI request_irq failed (%d)\n", rc);
6206 }
6207 return rc;
6208}
6209
6210/**
6211 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6212 * @phba: pointer to lpfc hba data structure.
6213 *
6214 * This routine is invoked to disable the MSI interrupt mode to device with
6215 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6216 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6217 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6218 * its vector.
6219 */
6220static void
6221lpfc_sli_disable_msi(struct lpfc_hba *phba)
6222{
6223 free_irq(phba->pcidev->irq, phba);
6224 pci_disable_msi(phba->pcidev);
6225 return;
6226}
6227
6228/**
6229 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6230 * @phba: pointer to lpfc hba data structure.
6231 *
6232 * This routine is invoked to enable device interrupt and associate driver's
6233 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6234 * spec. Depends on the interrupt mode configured to the driver, the driver
6235 * will try to fallback from the configured interrupt mode to an interrupt
6236 * mode which is supported by the platform, kernel, and device in the order
6237 * of:
6238 * MSI-X -> MSI -> IRQ.
6239 *
6240 * Return codes
6241 * 0 - sucessful
6242 * other values - error
6243 **/
6244static uint32_t
6245lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6246{
6247 uint32_t intr_mode = LPFC_INTR_ERROR;
6248 int retval;
6249
6250 if (cfg_mode == 2) {
6251 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6252 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6253 if (!retval) {
6254 /* Now, try to enable MSI-X interrupt mode */
6255 retval = lpfc_sli_enable_msix(phba);
6256 if (!retval) {
6257 /* Indicate initialization to MSI-X mode */
6258 phba->intr_type = MSIX;
6259 intr_mode = 2;
6260 }
6261 }
6262 }
6263
6264 /* Fallback to MSI if MSI-X initialization failed */
6265 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6266 retval = lpfc_sli_enable_msi(phba);
6267 if (!retval) {
6268 /* Indicate initialization to MSI mode */
6269 phba->intr_type = MSI;
6270 intr_mode = 1;
6271 }
6272 }
6273
6274 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6275 if (phba->intr_type == NONE) {
6276 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6277 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6278 if (!retval) {
6279 /* Indicate initialization to INTx mode */
6280 phba->intr_type = INTx;
6281 intr_mode = 0;
6282 }
6283 }
6284 return intr_mode;
6285}
6286
6287/**
6288 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6289 * @phba: pointer to lpfc hba data structure.
6290 *
6291 * This routine is invoked to disable device interrupt and disassociate the
6292 * driver's interrupt handler(s) from interrupt vector(s) to device with
6293 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6294 * release the interrupt vector(s) for the message signaled interrupt.
6295 **/
6296static void
6297lpfc_sli_disable_intr(struct lpfc_hba *phba)
6298{
6299 /* Disable the currently initialized interrupt mode */
6300 if (phba->intr_type == MSIX)
6301 lpfc_sli_disable_msix(phba);
6302 else if (phba->intr_type == MSI)
6303 lpfc_sli_disable_msi(phba);
6304 else if (phba->intr_type == INTx)
6305 free_irq(phba->pcidev->irq, phba);
6306
6307 /* Reset interrupt management states */
6308 phba->intr_type = NONE;
6309 phba->sli.slistat.sli_intr = 0;
6310
6311 return;
6312}
6313
6314/**
James Smartda0436e2009-05-22 14:51:39 -04006315 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6316 * @phba: pointer to lpfc hba data structure.
6317 *
6318 * This routine is invoked to enable the MSI-X interrupt vectors to device
6319 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6320 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6321 * enables either all or nothing, depending on the current availability of
6322 * PCI vector resources. The device driver is responsible for calling the
6323 * individual request_irq() to register each MSI-X vector with a interrupt
6324 * handler, which is done in this function. Note that later when device is
6325 * unloading, the driver should always call free_irq() on all MSI-X vectors
6326 * it has done request_irq() on before calling pci_disable_msix(). Failure
6327 * to do so results in a BUG_ON() and a device will be left with MSI-X
6328 * enabled and leaks its vectors.
6329 *
6330 * Return codes
6331 * 0 - sucessful
6332 * other values - error
6333 **/
6334static int
6335lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6336{
6337 int rc, index;
6338
6339 /* Set up MSI-X multi-message vectors */
6340 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6341 phba->sli4_hba.msix_entries[index].entry = index;
6342
6343 /* Configure MSI-X capability structure */
6344 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6345 phba->sli4_hba.cfg_eqn);
6346 if (rc) {
6347 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6348 "0484 PCI enable MSI-X failed (%d)\n", rc);
6349 goto msi_fail_out;
6350 }
6351 /* Log MSI-X vector assignment */
6352 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6353 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6354 "0489 MSI-X entry[%d]: vector=x%x "
6355 "message=%d\n", index,
6356 phba->sli4_hba.msix_entries[index].vector,
6357 phba->sli4_hba.msix_entries[index].entry);
6358 /*
6359 * Assign MSI-X vectors to interrupt handlers
6360 */
6361
6362 /* The first vector must associated to slow-path handler for MQ */
6363 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6364 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6365 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6366 if (rc) {
6367 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6368 "0485 MSI-X slow-path request_irq failed "
6369 "(%d)\n", rc);
6370 goto msi_fail_out;
6371 }
6372
6373 /* The rest of the vector(s) are associated to fast-path handler(s) */
6374 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6375 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6376 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6377 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6378 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6379 LPFC_FP_DRIVER_HANDLER_NAME,
6380 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6381 if (rc) {
6382 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6383 "0486 MSI-X fast-path (%d) "
6384 "request_irq failed (%d)\n", index, rc);
6385 goto cfg_fail_out;
6386 }
6387 }
6388
6389 return rc;
6390
6391cfg_fail_out:
6392 /* free the irq already requested */
6393 for (--index; index >= 1; index--)
6394 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6395 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6396
6397 /* free the irq already requested */
6398 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6399
6400msi_fail_out:
6401 /* Unconfigure MSI-X capability structure */
6402 pci_disable_msix(phba->pcidev);
6403 return rc;
6404}
6405
6406/**
6407 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6408 * @phba: pointer to lpfc hba data structure.
6409 *
6410 * This routine is invoked to release the MSI-X vectors and then disable the
6411 * MSI-X interrupt mode to device with SLI-4 interface spec.
6412 **/
6413static void
6414lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6415{
6416 int index;
6417
6418 /* Free up MSI-X multi-message vectors */
6419 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6420
6421 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6422 free_irq(phba->sli4_hba.msix_entries[index].vector,
6423 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6424 /* Disable MSI-X */
6425 pci_disable_msix(phba->pcidev);
6426
6427 return;
6428}
6429
6430/**
6431 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6432 * @phba: pointer to lpfc hba data structure.
6433 *
6434 * This routine is invoked to enable the MSI interrupt mode to device with
6435 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6436 * to enable the MSI vector. The device driver is responsible for calling
6437 * the request_irq() to register MSI vector with a interrupt the handler,
6438 * which is done in this function.
6439 *
6440 * Return codes
6441 * 0 - sucessful
6442 * other values - error
6443 **/
6444static int
6445lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6446{
6447 int rc, index;
6448
6449 rc = pci_enable_msi(phba->pcidev);
6450 if (!rc)
6451 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6452 "0487 PCI enable MSI mode success.\n");
6453 else {
6454 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6455 "0488 PCI enable MSI mode failed (%d)\n", rc);
6456 return rc;
6457 }
6458
6459 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6460 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6461 if (rc) {
6462 pci_disable_msi(phba->pcidev);
6463 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6464 "0490 MSI request_irq failed (%d)\n", rc);
6465 }
6466
6467 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6468 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6469 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6470 }
6471
6472 return rc;
6473}
6474
6475/**
6476 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6477 * @phba: pointer to lpfc hba data structure.
6478 *
6479 * This routine is invoked to disable the MSI interrupt mode to device with
6480 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6481 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6482 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6483 * its vector.
6484 **/
6485static void
6486lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6487{
6488 free_irq(phba->pcidev->irq, phba);
6489 pci_disable_msi(phba->pcidev);
6490 return;
6491}
6492
6493/**
6494 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6495 * @phba: pointer to lpfc hba data structure.
6496 *
6497 * This routine is invoked to enable device interrupt and associate driver's
6498 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6499 * interface spec. Depends on the interrupt mode configured to the driver,
6500 * the driver will try to fallback from the configured interrupt mode to an
6501 * interrupt mode which is supported by the platform, kernel, and device in
6502 * the order of:
6503 * MSI-X -> MSI -> IRQ.
6504 *
6505 * Return codes
6506 * 0 - sucessful
6507 * other values - error
6508 **/
6509static uint32_t
6510lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6511{
6512 uint32_t intr_mode = LPFC_INTR_ERROR;
6513 int retval, index;
6514
6515 if (cfg_mode == 2) {
6516 /* Preparation before conf_msi mbox cmd */
6517 retval = 0;
6518 if (!retval) {
6519 /* Now, try to enable MSI-X interrupt mode */
6520 retval = lpfc_sli4_enable_msix(phba);
6521 if (!retval) {
6522 /* Indicate initialization to MSI-X mode */
6523 phba->intr_type = MSIX;
6524 intr_mode = 2;
6525 }
6526 }
6527 }
6528
6529 /* Fallback to MSI if MSI-X initialization failed */
6530 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6531 retval = lpfc_sli4_enable_msi(phba);
6532 if (!retval) {
6533 /* Indicate initialization to MSI mode */
6534 phba->intr_type = MSI;
6535 intr_mode = 1;
6536 }
6537 }
6538
6539 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6540 if (phba->intr_type == NONE) {
6541 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6542 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6543 if (!retval) {
6544 /* Indicate initialization to INTx mode */
6545 phba->intr_type = INTx;
6546 intr_mode = 0;
6547 for (index = 0; index < phba->cfg_fcp_eq_count;
6548 index++) {
6549 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6550 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6551 }
6552 }
6553 }
6554 return intr_mode;
6555}
6556
6557/**
6558 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6559 * @phba: pointer to lpfc hba data structure.
6560 *
6561 * This routine is invoked to disable device interrupt and disassociate
6562 * the driver's interrupt handler(s) from interrupt vector(s) to device
6563 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6564 * will release the interrupt vector(s) for the message signaled interrupt.
6565 **/
6566static void
6567lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6568{
6569 /* Disable the currently initialized interrupt mode */
6570 if (phba->intr_type == MSIX)
6571 lpfc_sli4_disable_msix(phba);
6572 else if (phba->intr_type == MSI)
6573 lpfc_sli4_disable_msi(phba);
6574 else if (phba->intr_type == INTx)
6575 free_irq(phba->pcidev->irq, phba);
6576
6577 /* Reset interrupt management states */
6578 phba->intr_type = NONE;
6579 phba->sli.slistat.sli_intr = 0;
6580
6581 return;
6582}
6583
6584/**
James Smart3772a992009-05-22 14:50:54 -04006585 * lpfc_unset_hba - Unset SLI3 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-3 interface spec.
6590 **/
6591static void
6592lpfc_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 lpfc_stop_hba_timers(phba);
6602
6603 phba->pport->work_port_events = 0;
6604
6605 lpfc_sli_hba_down(phba);
6606
6607 lpfc_sli_brdrestart(phba);
6608
6609 lpfc_sli_disable_intr(phba);
6610
6611 return;
6612}
6613
6614/**
James Smartda0436e2009-05-22 14:51:39 -04006615 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6616 * @phba: pointer to lpfc hba data structure.
6617 *
6618 * This routine is invoked to unset the HBA device initialization steps to
6619 * a device with SLI-4 interface spec.
6620 **/
6621static void
6622lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6623{
6624 struct lpfc_vport *vport = phba->pport;
6625 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6626
6627 spin_lock_irq(shost->host_lock);
6628 vport->load_flag |= FC_UNLOADING;
6629 spin_unlock_irq(shost->host_lock);
6630
6631 phba->pport->work_port_events = 0;
6632
6633 lpfc_sli4_hba_down(phba);
6634
6635 lpfc_sli4_disable_intr(phba);
6636
6637 return;
6638}
6639
6640/**
6641 * lpfc_sli4_hba_unset - Unset the fcoe hba
6642 * @phba: Pointer to HBA context object.
6643 *
6644 * This function is called in the SLI4 code path to reset the HBA's FCoE
6645 * function. The caller is not required to hold any lock. This routine
6646 * issues PCI function reset mailbox command to reset the FCoE function.
6647 * At the end of the function, it calls lpfc_hba_down_post function to
6648 * free any pending commands.
6649 **/
6650static void
6651lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6652{
6653 int wait_cnt = 0;
6654 LPFC_MBOXQ_t *mboxq;
6655
6656 lpfc_stop_hba_timers(phba);
6657 phba->sli4_hba.intr_enable = 0;
6658
6659 /*
6660 * Gracefully wait out the potential current outstanding asynchronous
6661 * mailbox command.
6662 */
6663
6664 /* First, block any pending async mailbox command from posted */
6665 spin_lock_irq(&phba->hbalock);
6666 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6667 spin_unlock_irq(&phba->hbalock);
6668 /* Now, trying to wait it out if we can */
6669 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6670 msleep(10);
6671 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
6672 break;
6673 }
6674 /* Forcefully release the outstanding mailbox command if timed out */
6675 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6676 spin_lock_irq(&phba->hbalock);
6677 mboxq = phba->sli.mbox_active;
6678 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6679 __lpfc_mbox_cmpl_put(phba, mboxq);
6680 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6681 phba->sli.mbox_active = NULL;
6682 spin_unlock_irq(&phba->hbalock);
6683 }
6684
6685 /* Tear down the queues in the HBA */
6686 lpfc_sli4_queue_unset(phba);
6687
6688 /* Disable PCI subsystem interrupt */
6689 lpfc_sli4_disable_intr(phba);
6690
6691 /* Stop kthread signal shall trigger work_done one more time */
6692 kthread_stop(phba->worker_thread);
6693
6694 /* Stop the SLI4 device port */
6695 phba->pport->work_port_events = 0;
6696}
6697
6698/**
James Smart3772a992009-05-22 14:50:54 -04006699 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
6700 * @pdev: pointer to PCI device
6701 * @pid: pointer to PCI device identifier
6702 *
6703 * This routine is to be called to attach a device with SLI-3 interface spec
6704 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6705 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
6706 * information of the device and driver to see if the driver state that it can
6707 * support this kind of device. If the match is successful, the driver core
6708 * invokes this routine. If this routine determines it can claim the HBA, it
6709 * does all the initialization that it needs to do to handle the HBA properly.
6710 *
6711 * Return code
6712 * 0 - driver can claim the device
6713 * negative value - driver can not claim the device
6714 **/
6715static int __devinit
6716lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
6717{
6718 struct lpfc_hba *phba;
6719 struct lpfc_vport *vport = NULL;
6720 int error;
6721 uint32_t cfg_mode, intr_mode;
6722
6723 /* Allocate memory for HBA structure */
6724 phba = lpfc_hba_alloc(pdev);
6725 if (!phba)
6726 return -ENOMEM;
6727
6728 /* Perform generic PCI device enabling operation */
6729 error = lpfc_enable_pci_dev(phba);
6730 if (error) {
6731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6732 "1401 Failed to enable pci device.\n");
6733 goto out_free_phba;
6734 }
6735
6736 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
6737 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
6738 if (error)
6739 goto out_disable_pci_dev;
6740
6741 /* Set up SLI-3 specific device PCI memory space */
6742 error = lpfc_sli_pci_mem_setup(phba);
6743 if (error) {
6744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6745 "1402 Failed to set up pci memory space.\n");
6746 goto out_disable_pci_dev;
6747 }
6748
6749 /* Set up phase-1 common device driver resources */
6750 error = lpfc_setup_driver_resource_phase1(phba);
6751 if (error) {
6752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6753 "1403 Failed to set up driver resource.\n");
6754 goto out_unset_pci_mem_s3;
6755 }
6756
6757 /* Set up SLI-3 specific device driver resources */
6758 error = lpfc_sli_driver_resource_setup(phba);
6759 if (error) {
6760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6761 "1404 Failed to set up driver resource.\n");
6762 goto out_unset_pci_mem_s3;
6763 }
6764
6765 /* Initialize and populate the iocb list per host */
6766 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
6767 if (error) {
6768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6769 "1405 Failed to initialize iocb list.\n");
6770 goto out_unset_driver_resource_s3;
6771 }
6772
6773 /* Set up common device driver resources */
6774 error = lpfc_setup_driver_resource_phase2(phba);
6775 if (error) {
6776 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6777 "1406 Failed to set up driver resource.\n");
6778 goto out_free_iocb_list;
6779 }
6780
6781 /* Create SCSI host to the physical port */
6782 error = lpfc_create_shost(phba);
6783 if (error) {
6784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6785 "1407 Failed to create scsi host.\n");
6786 goto out_unset_driver_resource;
6787 }
6788
6789 /* Configure sysfs attributes */
6790 vport = phba->pport;
6791 error = lpfc_alloc_sysfs_attr(vport);
6792 if (error) {
6793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6794 "1476 Failed to allocate sysfs attr\n");
6795 goto out_destroy_shost;
6796 }
6797
6798 /* Now, trying to enable interrupt and bring up the device */
6799 cfg_mode = phba->cfg_use_msi;
6800 while (true) {
6801 /* Put device to a known state before enabling interrupt */
6802 lpfc_stop_port(phba);
6803 /* Configure and enable interrupt */
6804 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
6805 if (intr_mode == LPFC_INTR_ERROR) {
6806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6807 "0431 Failed to enable interrupt.\n");
6808 error = -ENODEV;
6809 goto out_free_sysfs_attr;
6810 }
6811 /* SLI-3 HBA setup */
6812 if (lpfc_sli_hba_setup(phba)) {
6813 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6814 "1477 Failed to set up hba\n");
6815 error = -ENODEV;
6816 goto out_remove_device;
6817 }
6818
6819 /* Wait 50ms for the interrupts of previous mailbox commands */
6820 msleep(50);
6821 /* Check active interrupts on message signaled interrupts */
6822 if (intr_mode == 0 ||
6823 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
6824 /* Log the current active interrupt mode */
6825 phba->intr_mode = intr_mode;
6826 lpfc_log_intr_mode(phba, intr_mode);
6827 break;
6828 } else {
6829 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6830 "0447 Configure interrupt mode (%d) "
6831 "failed active interrupt test.\n",
6832 intr_mode);
6833 /* Disable the current interrupt mode */
6834 lpfc_sli_disable_intr(phba);
6835 /* Try next level of interrupt mode */
6836 cfg_mode = --intr_mode;
6837 }
6838 }
6839
6840 /* Perform post initialization setup */
6841 lpfc_post_init_setup(phba);
6842
6843 /* Check if there are static vports to be created. */
6844 lpfc_create_static_vport(phba);
6845
6846 return 0;
6847
6848out_remove_device:
6849 lpfc_unset_hba(phba);
6850out_free_sysfs_attr:
6851 lpfc_free_sysfs_attr(vport);
6852out_destroy_shost:
6853 lpfc_destroy_shost(phba);
6854out_unset_driver_resource:
6855 lpfc_unset_driver_resource_phase2(phba);
6856out_free_iocb_list:
6857 lpfc_free_iocb_list(phba);
6858out_unset_driver_resource_s3:
6859 lpfc_sli_driver_resource_unset(phba);
6860out_unset_pci_mem_s3:
6861 lpfc_sli_pci_mem_unset(phba);
6862out_disable_pci_dev:
6863 lpfc_disable_pci_dev(phba);
6864out_free_phba:
6865 lpfc_hba_free(phba);
6866 return error;
6867}
6868
6869/**
6870 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04006871 * @pdev: pointer to PCI device
6872 *
James Smart3772a992009-05-22 14:50:54 -04006873 * This routine is to be called to disattach a device with SLI-3 interface
6874 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6875 * removed from PCI bus, it performs all the necessary cleanup for the HBA
6876 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04006877 **/
dea31012005-04-17 16:05:31 -05006878static void __devexit
James Smart3772a992009-05-22 14:50:54 -04006879lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05006880{
James Smart2e0fef82007-06-17 19:56:36 -05006881 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6882 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05006883 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05006884 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05006885 int i;
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05006886 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
6887
James Smart549e55c2007-08-02 11:09:51 -04006888 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04006889 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04006890 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006891
James Smart858c9f62007-06-17 19:56:39 -05006892 lpfc_free_sysfs_attr(vport);
6893
James Smarteada2722008-12-04 22:39:13 -05006894 /* Release all the vports against this physical port */
6895 vports = lpfc_create_vport_work_array(phba);
6896 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04006897 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05006898 fc_vport_terminate(vports[i]->fc_vport);
6899 lpfc_destroy_vport_work_array(phba, vports);
6900
6901 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05006902 fc_remove_host(shost);
6903 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04006904 lpfc_cleanup(vport);
6905
James Smart2e0fef82007-06-17 19:56:36 -05006906 /*
6907 * Bring down the SLI Layer. This step disable all interrupts,
6908 * clears the rings, discards all mailbox commands, and resets
6909 * the HBA.
6910 */
James Smarta257bf92009-04-06 18:48:10 -04006911
6912 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05006913 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04006914 /* Stop kthread signal shall trigger work_done one more time */
6915 kthread_stop(phba->worker_thread);
6916 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05006917 lpfc_sli_brdrestart(phba);
6918
James Smart3772a992009-05-22 14:50:54 -04006919 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05006920 spin_lock_irq(&phba->hbalock);
6921 list_del_init(&vport->listentry);
6922 spin_unlock_irq(&phba->hbalock);
6923
James Smart858c9f62007-06-17 19:56:39 -05006924 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006925
James Smart5b75da22008-12-04 22:39:35 -05006926 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04006927 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05006928
6929 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05006930 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05006931
6932 /*
6933 * Call scsi_free before mem_free since scsi bufs are released to their
6934 * corresponding pools here.
6935 */
6936 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04006937 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05006938
James Smart34b02dc2008-08-24 21:49:55 -04006939 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6940 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05006941
James Smart2e0fef82007-06-17 19:56:36 -05006942 /* Free resources associated with SLI2 interface */
6943 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04006944 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05006945
6946 /* unmap adapter SLIM and Control Registers */
6947 iounmap(phba->ctrl_regs_memmap_p);
6948 iounmap(phba->slim_memmap_p);
6949
James Smart3772a992009-05-22 14:50:54 -04006950 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05006951
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05006952 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05006953 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05006954}
6955
Linas Vepstas8d63f372007-02-14 14:28:36 -06006956/**
James Smart3772a992009-05-22 14:50:54 -04006957 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05006958 * @pdev: pointer to PCI device
6959 * @msg: power management message
6960 *
James Smart3772a992009-05-22 14:50:54 -04006961 * This routine is to be called from the kernel's PCI subsystem to support
6962 * system Power Management (PM) to device with SLI-3 interface spec. When
6963 * PM invokes this method, it quiesces the device by stopping the driver's
6964 * worker thread for the device, turning off device's interrupt and DMA,
6965 * and bring the device offline. Note that as the driver implements the
6966 * minimum PM requirements to a power-aware driver's PM support for the
6967 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
6968 * to the suspend() method call will be treated as SUSPEND and the driver will
6969 * fully reinitialize its device during resume() method call, the driver will
6970 * set device to PCI_D3hot state in PCI config space instead of setting it
6971 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05006972 *
6973 * Return code
James Smart3772a992009-05-22 14:50:54 -04006974 * 0 - driver suspended the device
6975 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05006976 **/
6977static int
James Smart3772a992009-05-22 14:50:54 -04006978lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05006979{
6980 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6981 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
6982
6983 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6984 "0473 PCI device Power Management suspend.\n");
6985
6986 /* Bring down the device */
6987 lpfc_offline_prep(phba);
6988 lpfc_offline(phba);
6989 kthread_stop(phba->worker_thread);
6990
6991 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04006992 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05006993
6994 /* Save device state to PCI config space */
6995 pci_save_state(pdev);
6996 pci_set_power_state(pdev, PCI_D3hot);
6997
6998 return 0;
6999}
7000
7001/**
James Smart3772a992009-05-22 14:50:54 -04007002 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007003 * @pdev: pointer to PCI device
7004 *
James Smart3772a992009-05-22 14:50:54 -04007005 * This routine is to be called from the kernel's PCI subsystem to support
7006 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7007 * invokes this method, it restores the device's PCI config space state and
7008 * fully reinitializes the device and brings it online. Note that as the
7009 * driver implements the minimum PM requirements to a power-aware driver's
7010 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7011 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7012 * driver will fully reinitialize its device during resume() method call,
7013 * the device will be set to PCI_D0 directly in PCI config space before
7014 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007015 *
7016 * Return code
James Smart3772a992009-05-22 14:50:54 -04007017 * 0 - driver suspended the device
7018 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007019 **/
7020static int
James Smart3772a992009-05-22 14:50:54 -04007021lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007022{
7023 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7024 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007025 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007026 int error;
7027
7028 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7029 "0452 PCI device Power Management resume.\n");
7030
7031 /* Restore device state from PCI config space */
7032 pci_set_power_state(pdev, PCI_D0);
7033 pci_restore_state(pdev);
7034 if (pdev->is_busmaster)
7035 pci_set_master(pdev);
7036
7037 /* Startup the kernel thread for this host adapter. */
7038 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7039 "lpfc_worker_%d", phba->brd_no);
7040 if (IS_ERR(phba->worker_thread)) {
7041 error = PTR_ERR(phba->worker_thread);
7042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7043 "0434 PM resume failed to start worker "
7044 "thread: error=x%x.\n", error);
7045 return error;
7046 }
7047
James Smart5b75da22008-12-04 22:39:35 -05007048 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007049 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007050 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007052 "0430 PM resume Failed to enable interrupt\n");
7053 return -EIO;
7054 } else
7055 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007056
7057 /* Restart HBA and bring it online */
7058 lpfc_sli_brdrestart(phba);
7059 lpfc_online(phba);
7060
James Smart5b75da22008-12-04 22:39:35 -05007061 /* Log the current active interrupt mode */
7062 lpfc_log_intr_mode(phba, phba->intr_mode);
7063
James Smart3a55b532008-12-04 22:38:54 -05007064 return 0;
7065}
7066
7067/**
James Smart3772a992009-05-22 14:50:54 -04007068 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007069 * @pdev: pointer to PCI device.
7070 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007071 *
James Smart3772a992009-05-22 14:50:54 -04007072 * This routine is called from the PCI subsystem for I/O error handling to
7073 * device with SLI-3 interface spec. This function is called by the PCI
7074 * subsystem after a PCI bus error affecting this device has been detected.
7075 * When this function is invoked, it will need to stop all the I/Os and
7076 * interrupt(s) to the device. Once that is done, it will return
7077 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7078 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007079 *
7080 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007081 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7082 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007083 **/
James Smart3772a992009-05-22 14:50:54 -04007084static pci_ers_result_t
7085lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007086{
James Smart51ef4c22007-08-02 11:10:31 -04007087 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7088 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007089 struct lpfc_sli *psli = &phba->sli;
7090 struct lpfc_sli_ring *pring;
7091
James Smarta8e497d2008-08-24 21:50:11 -04007092 if (state == pci_channel_io_perm_failure) {
7093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7094 "0472 PCI channel I/O permanent failure\n");
7095 /* Block all SCSI devices' I/Os on the host */
7096 lpfc_scsi_dev_block(phba);
7097 /* Clean up all driver's outstanding SCSI I/Os */
7098 lpfc_sli_flush_fcp_rings(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007099 return PCI_ERS_RESULT_DISCONNECT;
James Smarta8e497d2008-08-24 21:50:11 -04007100 }
Linas Vepstas5daa49e2007-03-08 16:19:11 -06007101
Linas Vepstas8d63f372007-02-14 14:28:36 -06007102 pci_disable_device(pdev);
7103 /*
7104 * There may be I/Os dropped by the firmware.
7105 * Error iocb (I/O) on txcmplq and let the SCSI layer
7106 * retry it after re-establishing link.
7107 */
7108 pring = &psli->ring[psli->fcp_ring];
7109 lpfc_sli_abort_iocb_ring(phba, pring);
7110
James Smart5b75da22008-12-04 22:39:35 -05007111 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007112 lpfc_sli_disable_intr(phba);
James Smart51ef4c22007-08-02 11:10:31 -04007113
Linas Vepstas8d63f372007-02-14 14:28:36 -06007114 /* Request a slot reset. */
7115 return PCI_ERS_RESULT_NEED_RESET;
7116}
7117
7118/**
James Smart3772a992009-05-22 14:50:54 -04007119 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007120 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007121 *
James Smart3772a992009-05-22 14:50:54 -04007122 * This routine is called from the PCI subsystem for error handling to
7123 * device with SLI-3 interface spec. This is called after PCI bus has been
7124 * reset to restart the PCI card from scratch, as if from a cold-boot.
7125 * During the PCI subsystem error recovery, after driver returns
7126 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7127 * recovery and then call this routine before calling the .resume method
7128 * to recover the device. This function will initialize the HBA device,
7129 * enable the interrupt, but it will just put the HBA to offline state
7130 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007131 *
7132 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007133 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7134 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007135 */
James Smart3772a992009-05-22 14:50:54 -04007136static pci_ers_result_t
7137lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007138{
James Smart51ef4c22007-08-02 11:10:31 -04007139 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7140 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007141 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007142 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007143
7144 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007145 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007146 printk(KERN_ERR "lpfc: Cannot re-enable "
7147 "PCI device after reset.\n");
7148 return PCI_ERS_RESULT_DISCONNECT;
7149 }
7150
James Smart97207482008-12-04 22:39:19 -05007151 pci_restore_state(pdev);
7152 if (pdev->is_busmaster)
7153 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007154
James Smart92d7f7b2007-06-17 19:56:38 -05007155 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007156 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007157 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007158
James Smart5b75da22008-12-04 22:39:35 -05007159 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007160 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007161 if (intr_mode == LPFC_INTR_ERROR) {
7162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7163 "0427 Cannot re-enable interrupt after "
7164 "slot reset.\n");
7165 return PCI_ERS_RESULT_DISCONNECT;
7166 } else
7167 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007168
7169 /* Take device offline; this will perform cleanup */
7170 lpfc_offline(phba);
7171 lpfc_sli_brdrestart(phba);
7172
James Smart5b75da22008-12-04 22:39:35 -05007173 /* Log the current active interrupt mode */
7174 lpfc_log_intr_mode(phba, phba->intr_mode);
7175
Linas Vepstas8d63f372007-02-14 14:28:36 -06007176 return PCI_ERS_RESULT_RECOVERED;
7177}
7178
7179/**
James Smart3772a992009-05-22 14:50:54 -04007180 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007181 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007182 *
James Smart3772a992009-05-22 14:50:54 -04007183 * This routine is called from the PCI subsystem for error handling to device
7184 * with SLI-3 interface spec. It is called when kernel error recovery tells
7185 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7186 * error recovery. After this call, traffic can start to flow from this device
7187 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007188 */
James Smart3772a992009-05-22 14:50:54 -04007189static void
7190lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007191{
James Smart51ef4c22007-08-02 11:10:31 -04007192 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7193 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007194
James Smart58da1ff2008-04-07 10:15:56 -04007195 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007196}
7197
James Smart3772a992009-05-22 14:50:54 -04007198/**
James Smartda0436e2009-05-22 14:51:39 -04007199 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7200 * @phba: pointer to lpfc hba data structure.
7201 *
7202 * returns the number of ELS/CT IOCBs to reserve
7203 **/
7204int
7205lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7206{
7207 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7208
James Smartf1126682009-06-10 17:22:44 -04007209 if (phba->sli_rev == LPFC_SLI_REV4) {
7210 if (max_xri <= 100)
7211 return 4;
7212 else if (max_xri <= 256)
7213 return 8;
7214 else if (max_xri <= 512)
7215 return 16;
7216 else if (max_xri <= 1024)
7217 return 32;
7218 else
7219 return 48;
7220 } else
7221 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007222}
7223
7224/**
7225 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7226 * @pdev: pointer to PCI device
7227 * @pid: pointer to PCI device identifier
7228 *
7229 * This routine is called from the kernel's PCI subsystem to device with
7230 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7231 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7232 * information of the device and driver to see if the driver state that it
7233 * can support this kind of device. If the match is successful, the driver
7234 * core invokes this routine. If this routine determines it can claim the HBA,
7235 * it does all the initialization that it needs to do to handle the HBA
7236 * properly.
7237 *
7238 * Return code
7239 * 0 - driver can claim the device
7240 * negative value - driver can not claim the device
7241 **/
7242static int __devinit
7243lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7244{
7245 struct lpfc_hba *phba;
7246 struct lpfc_vport *vport = NULL;
7247 int error;
7248 uint32_t cfg_mode, intr_mode;
7249 int mcnt;
7250
7251 /* Allocate memory for HBA structure */
7252 phba = lpfc_hba_alloc(pdev);
7253 if (!phba)
7254 return -ENOMEM;
7255
7256 /* Perform generic PCI device enabling operation */
7257 error = lpfc_enable_pci_dev(phba);
7258 if (error) {
7259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7260 "1409 Failed to enable pci device.\n");
7261 goto out_free_phba;
7262 }
7263
7264 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7265 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7266 if (error)
7267 goto out_disable_pci_dev;
7268
7269 /* Set up SLI-4 specific device PCI memory space */
7270 error = lpfc_sli4_pci_mem_setup(phba);
7271 if (error) {
7272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7273 "1410 Failed to set up pci memory space.\n");
7274 goto out_disable_pci_dev;
7275 }
7276
7277 /* Set up phase-1 common device driver resources */
7278 error = lpfc_setup_driver_resource_phase1(phba);
7279 if (error) {
7280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7281 "1411 Failed to set up driver resource.\n");
7282 goto out_unset_pci_mem_s4;
7283 }
7284
7285 /* Set up SLI-4 Specific device driver resources */
7286 error = lpfc_sli4_driver_resource_setup(phba);
7287 if (error) {
7288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7289 "1412 Failed to set up driver resource.\n");
7290 goto out_unset_pci_mem_s4;
7291 }
7292
7293 /* Initialize and populate the iocb list per host */
7294 error = lpfc_init_iocb_list(phba,
7295 phba->sli4_hba.max_cfg_param.max_xri);
7296 if (error) {
7297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7298 "1413 Failed to initialize iocb list.\n");
7299 goto out_unset_driver_resource_s4;
7300 }
7301
7302 /* Set up common device driver resources */
7303 error = lpfc_setup_driver_resource_phase2(phba);
7304 if (error) {
7305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7306 "1414 Failed to set up driver resource.\n");
7307 goto out_free_iocb_list;
7308 }
7309
7310 /* Create SCSI host to the physical port */
7311 error = lpfc_create_shost(phba);
7312 if (error) {
7313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7314 "1415 Failed to create scsi host.\n");
7315 goto out_unset_driver_resource;
7316 }
7317
7318 /* Configure sysfs attributes */
7319 vport = phba->pport;
7320 error = lpfc_alloc_sysfs_attr(vport);
7321 if (error) {
7322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7323 "1416 Failed to allocate sysfs attr\n");
7324 goto out_destroy_shost;
7325 }
7326
7327 /* Now, trying to enable interrupt and bring up the device */
7328 cfg_mode = phba->cfg_use_msi;
7329 while (true) {
7330 /* Put device to a known state before enabling interrupt */
7331 lpfc_stop_port(phba);
7332 /* Configure and enable interrupt */
7333 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7334 if (intr_mode == LPFC_INTR_ERROR) {
7335 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7336 "0426 Failed to enable interrupt.\n");
7337 error = -ENODEV;
7338 goto out_free_sysfs_attr;
7339 }
7340 /* Set up SLI-4 HBA */
7341 if (lpfc_sli4_hba_setup(phba)) {
7342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7343 "1421 Failed to set up hba\n");
7344 error = -ENODEV;
7345 goto out_disable_intr;
7346 }
7347
7348 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7349 if (intr_mode != 0)
7350 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7351 LPFC_ACT_INTR_CNT);
7352
7353 /* Check active interrupts received only for MSI/MSI-X */
7354 if (intr_mode == 0 ||
7355 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7356 /* Log the current active interrupt mode */
7357 phba->intr_mode = intr_mode;
7358 lpfc_log_intr_mode(phba, intr_mode);
7359 break;
7360 }
7361 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7362 "0451 Configure interrupt mode (%d) "
7363 "failed active interrupt test.\n",
7364 intr_mode);
7365 /* Unset the preivous SLI-4 HBA setup */
7366 lpfc_sli4_unset_hba(phba);
7367 /* Try next level of interrupt mode */
7368 cfg_mode = --intr_mode;
7369 }
7370
7371 /* Perform post initialization setup */
7372 lpfc_post_init_setup(phba);
7373
7374 return 0;
7375
7376out_disable_intr:
7377 lpfc_sli4_disable_intr(phba);
7378out_free_sysfs_attr:
7379 lpfc_free_sysfs_attr(vport);
7380out_destroy_shost:
7381 lpfc_destroy_shost(phba);
7382out_unset_driver_resource:
7383 lpfc_unset_driver_resource_phase2(phba);
7384out_free_iocb_list:
7385 lpfc_free_iocb_list(phba);
7386out_unset_driver_resource_s4:
7387 lpfc_sli4_driver_resource_unset(phba);
7388out_unset_pci_mem_s4:
7389 lpfc_sli4_pci_mem_unset(phba);
7390out_disable_pci_dev:
7391 lpfc_disable_pci_dev(phba);
7392out_free_phba:
7393 lpfc_hba_free(phba);
7394 return error;
7395}
7396
7397/**
7398 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
7399 * @pdev: pointer to PCI device
7400 *
7401 * This routine is called from the kernel's PCI subsystem to device with
7402 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7403 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7404 * device to be removed from the PCI subsystem properly.
7405 **/
7406static void __devexit
7407lpfc_pci_remove_one_s4(struct pci_dev *pdev)
7408{
7409 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7410 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7411 struct lpfc_vport **vports;
7412 struct lpfc_hba *phba = vport->phba;
7413 int i;
7414
7415 /* Mark the device unloading flag */
7416 spin_lock_irq(&phba->hbalock);
7417 vport->load_flag |= FC_UNLOADING;
7418 spin_unlock_irq(&phba->hbalock);
7419
7420 /* Free the HBA sysfs attributes */
7421 lpfc_free_sysfs_attr(vport);
7422
7423 /* Release all the vports against this physical port */
7424 vports = lpfc_create_vport_work_array(phba);
7425 if (vports != NULL)
7426 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7427 fc_vport_terminate(vports[i]->fc_vport);
7428 lpfc_destroy_vport_work_array(phba, vports);
7429
7430 /* Remove FC host and then SCSI host with the physical port */
7431 fc_remove_host(shost);
7432 scsi_remove_host(shost);
7433
7434 /* Perform cleanup on the physical port */
7435 lpfc_cleanup(vport);
7436
7437 /*
7438 * Bring down the SLI Layer. This step disables all interrupts,
7439 * clears the rings, discards all mailbox commands, and resets
7440 * the HBA FCoE function.
7441 */
7442 lpfc_debugfs_terminate(vport);
7443 lpfc_sli4_hba_unset(phba);
7444
7445 spin_lock_irq(&phba->hbalock);
7446 list_del_init(&vport->listentry);
7447 spin_unlock_irq(&phba->hbalock);
7448
7449 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7450 * buffers are released to their corresponding pools here.
7451 */
7452 lpfc_scsi_free(phba);
7453 lpfc_sli4_driver_resource_unset(phba);
7454
7455 /* Unmap adapter Control and Doorbell registers */
7456 lpfc_sli4_pci_mem_unset(phba);
7457
7458 /* Release PCI resources and disable device's PCI function */
7459 scsi_host_put(shost);
7460 lpfc_disable_pci_dev(phba);
7461
7462 /* Finally, free the driver's device data structure */
7463 lpfc_hba_free(phba);
7464
7465 return;
7466}
7467
7468/**
7469 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
7470 * @pdev: pointer to PCI device
7471 * @msg: power management message
7472 *
7473 * This routine is called from the kernel's PCI subsystem to support system
7474 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7475 * this method, it quiesces the device by stopping the driver's worker
7476 * thread for the device, turning off device's interrupt and DMA, and bring
7477 * the device offline. Note that as the driver implements the minimum PM
7478 * requirements to a power-aware driver's PM support for suspend/resume -- all
7479 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7480 * method call will be treated as SUSPEND and the driver will fully
7481 * reinitialize its device during resume() method call, the driver will set
7482 * device to PCI_D3hot state in PCI config space instead of setting it
7483 * according to the @msg provided by the PM.
7484 *
7485 * Return code
7486 * 0 - driver suspended the device
7487 * Error otherwise
7488 **/
7489static int
7490lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
7491{
7492 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7493 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7494
7495 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7496 "0298 PCI device Power Management suspend.\n");
7497
7498 /* Bring down the device */
7499 lpfc_offline_prep(phba);
7500 lpfc_offline(phba);
7501 kthread_stop(phba->worker_thread);
7502
7503 /* Disable interrupt from device */
7504 lpfc_sli4_disable_intr(phba);
7505
7506 /* Save device state to PCI config space */
7507 pci_save_state(pdev);
7508 pci_set_power_state(pdev, PCI_D3hot);
7509
7510 return 0;
7511}
7512
7513/**
7514 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
7515 * @pdev: pointer to PCI device
7516 *
7517 * This routine is called from the kernel's PCI subsystem to support system
7518 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
7519 * this method, it restores the device's PCI config space state and fully
7520 * reinitializes the device and brings it online. Note that as the driver
7521 * implements the minimum PM requirements to a power-aware driver's PM for
7522 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7523 * to the suspend() method call will be treated as SUSPEND and the driver
7524 * will fully reinitialize its device during resume() method call, the device
7525 * will be set to PCI_D0 directly in PCI config space before restoring the
7526 * state.
7527 *
7528 * Return code
7529 * 0 - driver suspended the device
7530 * Error otherwise
7531 **/
7532static int
7533lpfc_pci_resume_one_s4(struct pci_dev *pdev)
7534{
7535 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7536 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7537 uint32_t intr_mode;
7538 int error;
7539
7540 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7541 "0292 PCI device Power Management resume.\n");
7542
7543 /* Restore device state from PCI config space */
7544 pci_set_power_state(pdev, PCI_D0);
7545 pci_restore_state(pdev);
7546 if (pdev->is_busmaster)
7547 pci_set_master(pdev);
7548
7549 /* Startup the kernel thread for this host adapter. */
7550 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7551 "lpfc_worker_%d", phba->brd_no);
7552 if (IS_ERR(phba->worker_thread)) {
7553 error = PTR_ERR(phba->worker_thread);
7554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7555 "0293 PM resume failed to start worker "
7556 "thread: error=x%x.\n", error);
7557 return error;
7558 }
7559
7560 /* Configure and enable interrupt */
7561 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
7562 if (intr_mode == LPFC_INTR_ERROR) {
7563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7564 "0294 PM resume Failed to enable interrupt\n");
7565 return -EIO;
7566 } else
7567 phba->intr_mode = intr_mode;
7568
7569 /* Restart HBA and bring it online */
7570 lpfc_sli_brdrestart(phba);
7571 lpfc_online(phba);
7572
7573 /* Log the current active interrupt mode */
7574 lpfc_log_intr_mode(phba, phba->intr_mode);
7575
7576 return 0;
7577}
7578
7579/**
7580 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
7581 * @pdev: pointer to PCI device.
7582 * @state: the current PCI connection state.
7583 *
7584 * This routine is called from the PCI subsystem for error handling to device
7585 * with SLI-4 interface spec. This function is called by the PCI subsystem
7586 * after a PCI bus error affecting this device has been detected. When this
7587 * function is invoked, it will need to stop all the I/Os and interrupt(s)
7588 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
7589 * for the PCI subsystem to perform proper recovery as desired.
7590 *
7591 * Return codes
7592 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7593 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7594 **/
7595static pci_ers_result_t
7596lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
7597{
7598 return PCI_ERS_RESULT_NEED_RESET;
7599}
7600
7601/**
7602 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
7603 * @pdev: pointer to PCI device.
7604 *
7605 * This routine is called from the PCI subsystem for error handling to device
7606 * with SLI-4 interface spec. It is called after PCI bus has been reset to
7607 * restart the PCI card from scratch, as if from a cold-boot. During the
7608 * PCI subsystem error recovery, after the driver returns
7609 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7610 * recovery and then call this routine before calling the .resume method to
7611 * recover the device. This function will initialize the HBA device, enable
7612 * the interrupt, but it will just put the HBA to offline state without
7613 * passing any I/O traffic.
7614 *
7615 * Return codes
7616 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7617 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7618 */
7619static pci_ers_result_t
7620lpfc_io_slot_reset_s4(struct pci_dev *pdev)
7621{
7622 return PCI_ERS_RESULT_RECOVERED;
7623}
7624
7625/**
7626 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
7627 * @pdev: pointer to PCI device
7628 *
7629 * This routine is called from the PCI subsystem for error handling to device
7630 * with SLI-4 interface spec. It is called when kernel error recovery tells
7631 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7632 * error recovery. After this call, traffic can start to flow from this device
7633 * again.
7634 **/
7635static void
7636lpfc_io_resume_s4(struct pci_dev *pdev)
7637{
7638 return;
7639}
7640
7641/**
James Smart3772a992009-05-22 14:50:54 -04007642 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
7643 * @pdev: pointer to PCI device
7644 * @pid: pointer to PCI device identifier
7645 *
7646 * This routine is to be registered to the kernel's PCI subsystem. When an
7647 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
7648 * at PCI device-specific information of the device and driver to see if the
7649 * driver state that it can support this kind of device. If the match is
7650 * successful, the driver core invokes this routine. This routine dispatches
7651 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
7652 * do all the initialization that it needs to do to handle the HBA device
7653 * properly.
7654 *
7655 * Return code
7656 * 0 - driver can claim the device
7657 * negative value - driver can not claim the device
7658 **/
7659static int __devinit
7660lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
7661{
7662 int rc;
James Smart8fa38512009-07-19 10:01:03 -04007663 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04007664
James Smart8fa38512009-07-19 10:01:03 -04007665 if (pci_read_config_dword(pdev, LPFC_SLIREV_CONF_WORD, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04007666 return -ENODEV;
7667
James Smart8fa38512009-07-19 10:01:03 -04007668 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
7669 (bf_get(lpfc_sli_intf_rev, &intf) == LPFC_SLIREV_CONF_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04007670 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007671 else
James Smart3772a992009-05-22 14:50:54 -04007672 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007673
James Smart3772a992009-05-22 14:50:54 -04007674 return rc;
7675}
7676
7677/**
7678 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
7679 * @pdev: pointer to PCI device
7680 *
7681 * This routine is to be registered to the kernel's PCI subsystem. When an
7682 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
7683 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
7684 * remove routine, which will perform all the necessary cleanup for the
7685 * device to be removed from the PCI subsystem properly.
7686 **/
7687static void __devexit
7688lpfc_pci_remove_one(struct pci_dev *pdev)
7689{
7690 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7691 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7692
7693 switch (phba->pci_dev_grp) {
7694 case LPFC_PCI_DEV_LP:
7695 lpfc_pci_remove_one_s3(pdev);
7696 break;
James Smartda0436e2009-05-22 14:51:39 -04007697 case LPFC_PCI_DEV_OC:
7698 lpfc_pci_remove_one_s4(pdev);
7699 break;
James Smart3772a992009-05-22 14:50:54 -04007700 default:
7701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7702 "1424 Invalid PCI device group: 0x%x\n",
7703 phba->pci_dev_grp);
7704 break;
7705 }
7706 return;
7707}
7708
7709/**
7710 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
7711 * @pdev: pointer to PCI device
7712 * @msg: power management message
7713 *
7714 * This routine is to be registered to the kernel's PCI subsystem to support
7715 * system Power Management (PM). When PM invokes this method, it dispatches
7716 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
7717 * suspend the device.
7718 *
7719 * Return code
7720 * 0 - driver suspended the device
7721 * Error otherwise
7722 **/
7723static int
7724lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
7725{
7726 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7727 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7728 int rc = -ENODEV;
7729
7730 switch (phba->pci_dev_grp) {
7731 case LPFC_PCI_DEV_LP:
7732 rc = lpfc_pci_suspend_one_s3(pdev, msg);
7733 break;
James Smartda0436e2009-05-22 14:51:39 -04007734 case LPFC_PCI_DEV_OC:
7735 rc = lpfc_pci_suspend_one_s4(pdev, msg);
7736 break;
James Smart3772a992009-05-22 14:50:54 -04007737 default:
7738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7739 "1425 Invalid PCI device group: 0x%x\n",
7740 phba->pci_dev_grp);
7741 break;
7742 }
7743 return rc;
7744}
7745
7746/**
7747 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
7748 * @pdev: pointer to PCI device
7749 *
7750 * This routine is to be registered to the kernel's PCI subsystem to support
7751 * system Power Management (PM). When PM invokes this method, it dispatches
7752 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
7753 * resume the device.
7754 *
7755 * Return code
7756 * 0 - driver suspended the device
7757 * Error otherwise
7758 **/
7759static int
7760lpfc_pci_resume_one(struct pci_dev *pdev)
7761{
7762 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7763 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7764 int rc = -ENODEV;
7765
7766 switch (phba->pci_dev_grp) {
7767 case LPFC_PCI_DEV_LP:
7768 rc = lpfc_pci_resume_one_s3(pdev);
7769 break;
James Smartda0436e2009-05-22 14:51:39 -04007770 case LPFC_PCI_DEV_OC:
7771 rc = lpfc_pci_resume_one_s4(pdev);
7772 break;
James Smart3772a992009-05-22 14:50:54 -04007773 default:
7774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7775 "1426 Invalid PCI device group: 0x%x\n",
7776 phba->pci_dev_grp);
7777 break;
7778 }
7779 return rc;
7780}
7781
7782/**
7783 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
7784 * @pdev: pointer to PCI device.
7785 * @state: the current PCI connection state.
7786 *
7787 * This routine is registered to the PCI subsystem for error handling. This
7788 * function is called by the PCI subsystem after a PCI bus error affecting
7789 * this device has been detected. When this routine is invoked, it dispatches
7790 * the action to the proper SLI-3 or SLI-4 device error detected handling
7791 * routine, which will perform the proper error detected operation.
7792 *
7793 * Return codes
7794 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7795 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7796 **/
7797static pci_ers_result_t
7798lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7799{
7800 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7801 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7802 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7803
7804 switch (phba->pci_dev_grp) {
7805 case LPFC_PCI_DEV_LP:
7806 rc = lpfc_io_error_detected_s3(pdev, state);
7807 break;
James Smartda0436e2009-05-22 14:51:39 -04007808 case LPFC_PCI_DEV_OC:
7809 rc = lpfc_io_error_detected_s4(pdev, state);
7810 break;
James Smart3772a992009-05-22 14:50:54 -04007811 default:
7812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7813 "1427 Invalid PCI device group: 0x%x\n",
7814 phba->pci_dev_grp);
7815 break;
7816 }
7817 return rc;
7818}
7819
7820/**
7821 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
7822 * @pdev: pointer to PCI device.
7823 *
7824 * This routine is registered to the PCI subsystem for error handling. This
7825 * function is called after PCI bus has been reset to restart the PCI card
7826 * from scratch, as if from a cold-boot. When this routine is invoked, it
7827 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
7828 * routine, which will perform the proper device reset.
7829 *
7830 * Return codes
7831 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7832 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7833 **/
7834static pci_ers_result_t
7835lpfc_io_slot_reset(struct pci_dev *pdev)
7836{
7837 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7838 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7839 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7840
7841 switch (phba->pci_dev_grp) {
7842 case LPFC_PCI_DEV_LP:
7843 rc = lpfc_io_slot_reset_s3(pdev);
7844 break;
James Smartda0436e2009-05-22 14:51:39 -04007845 case LPFC_PCI_DEV_OC:
7846 rc = lpfc_io_slot_reset_s4(pdev);
7847 break;
James Smart3772a992009-05-22 14:50:54 -04007848 default:
7849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7850 "1428 Invalid PCI device group: 0x%x\n",
7851 phba->pci_dev_grp);
7852 break;
7853 }
7854 return rc;
7855}
7856
7857/**
7858 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
7859 * @pdev: pointer to PCI device
7860 *
7861 * This routine is registered to the PCI subsystem for error handling. It
7862 * is called when kernel error recovery tells the lpfc driver that it is
7863 * OK to resume normal PCI operation after PCI bus error recovery. When
7864 * this routine is invoked, it dispatches the action to the proper SLI-3
7865 * or SLI-4 device io_resume routine, which will resume the device operation.
7866 **/
7867static void
7868lpfc_io_resume(struct pci_dev *pdev)
7869{
7870 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7871 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7872
7873 switch (phba->pci_dev_grp) {
7874 case LPFC_PCI_DEV_LP:
7875 lpfc_io_resume_s3(pdev);
7876 break;
James Smartda0436e2009-05-22 14:51:39 -04007877 case LPFC_PCI_DEV_OC:
7878 lpfc_io_resume_s4(pdev);
7879 break;
James Smart3772a992009-05-22 14:50:54 -04007880 default:
7881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7882 "1429 Invalid PCI device group: 0x%x\n",
7883 phba->pci_dev_grp);
7884 break;
7885 }
7886 return;
7887}
7888
dea31012005-04-17 16:05:31 -05007889static struct pci_device_id lpfc_id_table[] = {
7890 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
7891 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04007892 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
7893 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007894 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
7895 PCI_ANY_ID, PCI_ANY_ID, },
7896 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
7897 PCI_ANY_ID, PCI_ANY_ID, },
7898 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
7899 PCI_ANY_ID, PCI_ANY_ID, },
7900 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
7901 PCI_ANY_ID, PCI_ANY_ID, },
7902 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
7903 PCI_ANY_ID, PCI_ANY_ID, },
7904 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
7905 PCI_ANY_ID, PCI_ANY_ID, },
7906 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
7907 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007908 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
7909 PCI_ANY_ID, PCI_ANY_ID, },
7910 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
7911 PCI_ANY_ID, PCI_ANY_ID, },
7912 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
7913 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007914 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
7915 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007916 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
7917 PCI_ANY_ID, PCI_ANY_ID, },
7918 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
7919 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007920 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
7921 PCI_ANY_ID, PCI_ANY_ID, },
7922 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
7923 PCI_ANY_ID, PCI_ANY_ID, },
7924 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
7925 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04007926 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
7927 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007928 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
7929 PCI_ANY_ID, PCI_ANY_ID, },
7930 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
7931 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007932 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
7933 PCI_ANY_ID, PCI_ANY_ID, },
7934 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
7935 PCI_ANY_ID, PCI_ANY_ID, },
7936 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
7937 PCI_ANY_ID, PCI_ANY_ID, },
7938 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
7939 PCI_ANY_ID, PCI_ANY_ID, },
7940 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
7941 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007942 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
7943 PCI_ANY_ID, PCI_ANY_ID, },
7944 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
7945 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04007946 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
7947 PCI_ANY_ID, PCI_ANY_ID, },
7948 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
7949 PCI_ANY_ID, PCI_ANY_ID, },
7950 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
7951 PCI_ANY_ID, PCI_ANY_ID, },
7952 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
7953 PCI_ANY_ID, PCI_ANY_ID, },
7954 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
7955 PCI_ANY_ID, PCI_ANY_ID, },
7956 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
7957 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04007958 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
7959 PCI_ANY_ID, PCI_ANY_ID, },
7960 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
7961 PCI_ANY_ID, PCI_ANY_ID, },
7962 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
7963 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04007964 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
7965 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007966 { 0 }
7967};
7968
7969MODULE_DEVICE_TABLE(pci, lpfc_id_table);
7970
Linas Vepstas8d63f372007-02-14 14:28:36 -06007971static struct pci_error_handlers lpfc_err_handler = {
7972 .error_detected = lpfc_io_error_detected,
7973 .slot_reset = lpfc_io_slot_reset,
7974 .resume = lpfc_io_resume,
7975};
7976
dea31012005-04-17 16:05:31 -05007977static struct pci_driver lpfc_driver = {
7978 .name = LPFC_DRIVER_NAME,
7979 .id_table = lpfc_id_table,
7980 .probe = lpfc_pci_probe_one,
7981 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05007982 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04007983 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05007984 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05007985};
7986
James Smarte59058c2008-08-24 21:49:00 -04007987/**
James Smart3621a712009-04-06 18:47:14 -04007988 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04007989 *
7990 * This routine is to be invoked when the lpfc module is loaded into the
7991 * kernel. The special kernel macro module_init() is used to indicate the
7992 * role of this routine to the kernel as lpfc module entry point.
7993 *
7994 * Return codes
7995 * 0 - successful
7996 * -ENOMEM - FC attach transport failed
7997 * all others - failed
7998 */
dea31012005-04-17 16:05:31 -05007999static int __init
8000lpfc_init(void)
8001{
8002 int error = 0;
8003
8004 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008005 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008006
James Smart7ee5d432007-10-27 13:37:17 -04008007 if (lpfc_enable_npiv) {
8008 lpfc_transport_functions.vport_create = lpfc_vport_create;
8009 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8010 }
dea31012005-04-17 16:05:31 -05008011 lpfc_transport_template =
8012 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008013 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008014 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008015 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008016 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008017 fc_attach_transport(&lpfc_vport_transport_functions);
8018 if (lpfc_vport_transport_template == NULL) {
8019 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008020 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008021 }
James Smart7ee5d432007-10-27 13:37:17 -04008022 }
dea31012005-04-17 16:05:31 -05008023 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008024 if (error) {
dea31012005-04-17 16:05:31 -05008025 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008026 if (lpfc_enable_npiv)
8027 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008028 }
dea31012005-04-17 16:05:31 -05008029
8030 return error;
8031}
8032
James Smarte59058c2008-08-24 21:49:00 -04008033/**
James Smart3621a712009-04-06 18:47:14 -04008034 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008035 *
8036 * This routine is invoked when the lpfc module is removed from the kernel.
8037 * The special kernel macro module_exit() is used to indicate the role of
8038 * this routine to the kernel as lpfc module exit point.
8039 */
dea31012005-04-17 16:05:31 -05008040static void __exit
8041lpfc_exit(void)
8042{
8043 pci_unregister_driver(&lpfc_driver);
8044 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008045 if (lpfc_enable_npiv)
8046 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008047 if (_dump_buf_data) {
8048 printk(KERN_ERR "BLKGRD freeing %lu pages for _dump_buf_data "
8049 "at 0x%p\n",
8050 (1L << _dump_buf_data_order), _dump_buf_data);
8051 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8052 }
8053
8054 if (_dump_buf_dif) {
8055 printk(KERN_ERR "BLKGRD freeing %lu pages for _dump_buf_dif "
8056 "at 0x%p\n",
8057 (1L << _dump_buf_dif_order), _dump_buf_dif);
8058 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8059 }
dea31012005-04-17 16:05:31 -05008060}
8061
8062module_init(lpfc_init);
8063module_exit(lpfc_exit);
8064MODULE_LICENSE("GPL");
8065MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8066MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8067MODULE_VERSION("0:" LPFC_DRIVER_VERSION);