blob: 12ab1eae47f9020670fd6e7c1f74ede3adbfc267 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
dea31012005-04-17 16:05:31 -050031
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040032#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport_fc.h>
36
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_hw.h"
39#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040040#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040041#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_disc.h"
43#include "lpfc_scsi.h"
44#include "lpfc.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050047#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_version.h"
49
James Smart81301a92008-12-04 22:39:46 -050050char *_dump_buf_data;
51unsigned long _dump_buf_data_order;
52char *_dump_buf_dif;
53unsigned long _dump_buf_dif_order;
54spinlock_t _dump_buf_lock;
55
dea31012005-04-17 16:05:31 -050056static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
57static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040058static int lpfc_sli4_queue_create(struct lpfc_hba *);
59static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
60static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
61static int lpfc_setup_endian_order(struct lpfc_hba *);
62static int lpfc_sli4_read_config(struct lpfc_hba *);
63static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
64static void lpfc_free_sgl_list(struct lpfc_hba *);
65static int lpfc_init_sgl_list(struct lpfc_hba *);
66static int lpfc_init_active_sgl_array(struct lpfc_hba *);
67static void lpfc_free_active_sgl(struct lpfc_hba *);
68static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
69static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
70static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
71static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
72static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050073
74static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050075static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050076static DEFINE_IDR(lpfc_hba_index);
77
James Smarte59058c2008-08-24 21:49:00 -040078/**
James Smart3621a712009-04-06 18:47:14 -040079 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040080 * @phba: pointer to lpfc hba data structure.
81 *
82 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
83 * mailbox command. It retrieves the revision information from the HBA and
84 * collects the Vital Product Data (VPD) about the HBA for preparing the
85 * configuration of the HBA.
86 *
87 * Return codes:
88 * 0 - success.
89 * -ERESTART - requests the SLI layer to reset the HBA and try again.
90 * Any other value - indicates an error.
91 **/
dea31012005-04-17 16:05:31 -050092int
James Smart2e0fef82007-06-17 19:56:36 -050093lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050094{
95 lpfc_vpd_t *vp = &phba->vpd;
96 int i = 0, rc;
97 LPFC_MBOXQ_t *pmb;
98 MAILBOX_t *mb;
99 char *lpfc_vpd_data = NULL;
100 uint16_t offset = 0;
101 static char licensed[56] =
102 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400103 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500104
105 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
106 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500107 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500108 return -ENOMEM;
109 }
110
James Smart04c68492009-05-22 14:52:52 -0400111 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500112 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500113
114 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400115 if (init_key) {
116 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500117
James Smart65a29c12006-07-06 15:50:50 -0400118 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
119 *ptext = cpu_to_be32(*ptext);
120 init_key = 0;
121 }
dea31012005-04-17 16:05:31 -0500122
123 lpfc_read_nv(phba, pmb);
124 memset((char*)mb->un.varRDnvp.rsvd3, 0,
125 sizeof (mb->un.varRDnvp.rsvd3));
126 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
127 sizeof (licensed));
128
129 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
130
131 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400133 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500134 "error, mbxCmd x%x READ_NVPARM, "
135 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500136 mb->mbxCommand, mb->mbxStatus);
137 mempool_free(pmb, phba->mbox_mem_pool);
138 return -ERESTART;
139 }
140 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500141 sizeof(phba->wwnn));
142 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
143 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500144 }
145
James Smart92d7f7b2007-06-17 19:56:38 -0500146 phba->sli3_options = 0x0;
147
dea31012005-04-17 16:05:31 -0500148 /* Setup and issue mailbox READ REV command */
149 lpfc_read_rev(phba, pmb);
150 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
151 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400153 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500154 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500155 mb->mbxCommand, mb->mbxStatus);
156 mempool_free( pmb, phba->mbox_mem_pool);
157 return -ERESTART;
158 }
159
James Smart92d7f7b2007-06-17 19:56:38 -0500160
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500161 /*
162 * The value of rr must be 1 since the driver set the cv field to 1.
163 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500164 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500165 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500166 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400168 "0440 Adapter failed to init, READ_REV has "
169 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500170 mempool_free(pmb, phba->mbox_mem_pool);
171 return -ERESTART;
dea31012005-04-17 16:05:31 -0500172 }
173
James Smart495a7142008-06-14 22:52:59 -0400174 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
175 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500176 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400177 }
James Smarted957682007-06-17 19:56:37 -0500178
dea31012005-04-17 16:05:31 -0500179 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500180 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500181 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500182 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
183 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
184 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
185 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500186 vp->rev.biuRev = mb->un.varRdRev.biuRev;
187 vp->rev.smRev = mb->un.varRdRev.smRev;
188 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
189 vp->rev.endecRev = mb->un.varRdRev.endecRev;
190 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
191 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
192 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
193 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
194 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
195 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
196
James Smart92d7f7b2007-06-17 19:56:38 -0500197 /* If the sli feature level is less then 9, we must
198 * tear down all RPIs and VPIs on link down if NPIV
199 * is enabled.
200 */
201 if (vp->rev.feaLevelHigh < 9)
202 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
203
dea31012005-04-17 16:05:31 -0500204 if (lpfc_is_LC_HBA(phba->pcidev->device))
205 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
206 sizeof (phba->RandomData));
207
dea31012005-04-17 16:05:31 -0500208 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500209 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
210 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400211 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500212
213 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400214 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500215 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
216
217 if (rc != MBX_SUCCESS) {
218 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400219 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500220 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500221 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500222 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500223 }
James Smart04c68492009-05-22 14:52:52 -0400224 /* dump mem may return a zero when finished or we got a
225 * mailbox error, either way we are done.
226 */
227 if (mb->un.varDmp.word_cnt == 0)
228 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500229 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
230 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400231 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
232 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500233 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500234 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500235 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
236 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500237
238 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500239out_free_mbox:
240 mempool_free(pmb, phba->mbox_mem_pool);
241 return 0;
242}
243
James Smarte59058c2008-08-24 21:49:00 -0400244/**
James Smart3621a712009-04-06 18:47:14 -0400245 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400246 * @phba: pointer to lpfc hba data structure.
247 * @pmboxq: pointer to the driver internal queue element for mailbox command.
248 *
249 * This is the completion handler for driver's configuring asynchronous event
250 * mailbox command to the device. If the mailbox command returns successfully,
251 * it will set internal async event support flag to 1; otherwise, it will
252 * set internal async event support flag to 0.
253 **/
James Smart57127f12007-10-27 13:37:05 -0400254static void
255lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
256{
James Smart04c68492009-05-22 14:52:52 -0400257 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400258 phba->temp_sensor_support = 1;
259 else
260 phba->temp_sensor_support = 0;
261 mempool_free(pmboxq, phba->mbox_mem_pool);
262 return;
263}
264
James Smarte59058c2008-08-24 21:49:00 -0400265/**
James Smart3621a712009-04-06 18:47:14 -0400266 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500267 * @phba: pointer to lpfc hba data structure.
268 * @pmboxq: pointer to the driver internal queue element for mailbox command.
269 *
270 * This is the completion handler for dump mailbox command for getting
271 * wake up parameters. When this command complete, the response contain
272 * Option rom version of the HBA. This function translate the version number
273 * into a human readable string and store it in OptionROMVersion.
274 **/
275static void
276lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
277{
278 struct prog_id *prg;
279 uint32_t prog_id_word;
280 char dist = ' ';
281 /* character array used for decoding dist type. */
282 char dist_char[] = "nabx";
283
James Smart04c68492009-05-22 14:52:52 -0400284 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500285 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500286 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500287 }
James Smart97207482008-12-04 22:39:19 -0500288
289 prg = (struct prog_id *) &prog_id_word;
290
291 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400292 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500293
294 /* Decode the Option rom version word to a readable string */
295 if (prg->dist < 4)
296 dist = dist_char[prg->dist];
297
298 if ((prg->dist == 3) && (prg->num == 0))
299 sprintf(phba->OptionROMVersion, "%d.%d%d",
300 prg->ver, prg->rev, prg->lev);
301 else
302 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
303 prg->ver, prg->rev, prg->lev,
304 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500305 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500306 return;
307}
308
309/**
James Smart3621a712009-04-06 18:47:14 -0400310 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400311 * @phba: pointer to lpfc hba data structure.
312 *
313 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
314 * command call. It performs all internal resource and state setups on the
315 * port: post IOCB buffers, enable appropriate host interrupt attentions,
316 * ELS ring timers, etc.
317 *
318 * Return codes
319 * 0 - success.
320 * Any other value - error.
321 **/
dea31012005-04-17 16:05:31 -0500322int
James Smart2e0fef82007-06-17 19:56:36 -0500323lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500324{
James Smart2e0fef82007-06-17 19:56:36 -0500325 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400326 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500327 LPFC_MBOXQ_t *pmb;
328 MAILBOX_t *mb;
329 struct lpfc_dmabuf *mp;
330 struct lpfc_sli *psli = &phba->sli;
331 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500332 int i, j;
333 int rc;
dea31012005-04-17 16:05:31 -0500334
James Smart7af67052007-10-27 13:38:11 -0400335 spin_lock_irq(&phba->hbalock);
336 /*
337 * If the Config port completed correctly the HBA is not
338 * over heated any more.
339 */
340 if (phba->over_temp_state == HBA_OVER_TEMP)
341 phba->over_temp_state = HBA_NORMAL_TEMP;
342 spin_unlock_irq(&phba->hbalock);
343
dea31012005-04-17 16:05:31 -0500344 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
345 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500346 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500347 return -ENOMEM;
348 }
James Smart04c68492009-05-22 14:52:52 -0400349 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500350
dea31012005-04-17 16:05:31 -0500351 /* Get login parameters for NID. */
James Smart92d7f7b2007-06-17 19:56:38 -0500352 lpfc_read_sparam(phba, pmb, 0);
James Smarted957682007-06-17 19:56:37 -0500353 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500354 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400356 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500357 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500358 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500359 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500360 mp = (struct lpfc_dmabuf *) pmb->context1;
361 mempool_free( pmb, phba->mbox_mem_pool);
362 lpfc_mbuf_free(phba, mp->virt, mp->phys);
363 kfree(mp);
364 return -EIO;
365 }
366
367 mp = (struct lpfc_dmabuf *) pmb->context1;
368
James Smart2e0fef82007-06-17 19:56:36 -0500369 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500370 lpfc_mbuf_free(phba, mp->virt, mp->phys);
371 kfree(mp);
372 pmb->context1 = NULL;
373
James Smarta12e07b2006-12-02 13:35:30 -0500374 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500375 u64_to_wwn(phba->cfg_soft_wwnn,
376 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400377 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500378 u64_to_wwn(phba->cfg_soft_wwpn,
379 vport->fc_sparam.portName.u.wwn);
380 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500381 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500382 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500383 sizeof (struct lpfc_name));
James Smarta257bf92009-04-06 18:48:10 -0400384
385 /* Update the fc_host data structures with new wwn. */
386 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
387 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400388 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400389
dea31012005-04-17 16:05:31 -0500390 /* If no serial number in VPD data, use low 6 bytes of WWNN */
391 /* This should be consolidated into parse_vpd ? - mr */
392 if (phba->SerialNumber[0] == 0) {
393 uint8_t *outptr;
394
James Smart2e0fef82007-06-17 19:56:36 -0500395 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500396 for (i = 0; i < 12; i++) {
397 status = *outptr++;
398 j = ((status & 0xf0) >> 4);
399 if (j <= 9)
400 phba->SerialNumber[i] =
401 (char)((uint8_t) 0x30 + (uint8_t) j);
402 else
403 phba->SerialNumber[i] =
404 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
405 i++;
406 j = (status & 0xf);
407 if (j <= 9)
408 phba->SerialNumber[i] =
409 (char)((uint8_t) 0x30 + (uint8_t) j);
410 else
411 phba->SerialNumber[i] =
412 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
413 }
414 }
415
dea31012005-04-17 16:05:31 -0500416 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500417 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500418 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400420 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500421 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500422 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500423 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500424 mempool_free( pmb, phba->mbox_mem_pool);
425 return -EIO;
426 }
427
James Smarta0c87cb2009-07-19 10:01:10 -0400428 /* Check if the port is disabled */
429 lpfc_sli_read_link_ste(phba);
430
dea31012005-04-17 16:05:31 -0500431 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
432 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
433 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400434 (mb->un.varRdConfig.max_xri + 1) -
435 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500436
437 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500438
439 /* Get the default values for Model Name and Description */
440 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
441
442 if ((phba->cfg_link_speed > LINK_SPEED_10G)
443 || ((phba->cfg_link_speed == LINK_SPEED_1G)
444 && !(phba->lmt & LMT_1Gb))
445 || ((phba->cfg_link_speed == LINK_SPEED_2G)
446 && !(phba->lmt & LMT_2Gb))
447 || ((phba->cfg_link_speed == LINK_SPEED_4G)
448 && !(phba->lmt & LMT_4Gb))
449 || ((phba->cfg_link_speed == LINK_SPEED_8G)
450 && !(phba->lmt & LMT_8Gb))
451 || ((phba->cfg_link_speed == LINK_SPEED_10G)
452 && !(phba->lmt & LMT_10Gb))) {
453 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500454 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400455 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500456 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500457 phba->cfg_link_speed);
458 phba->cfg_link_speed = LINK_SPEED_AUTO;
459 }
460
James Smart2e0fef82007-06-17 19:56:36 -0500461 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500462
James Smart0b727fe2007-10-27 13:37:25 -0400463 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500464 if (psli->ring[psli->extra_ring].cmdringaddr)
465 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500466 if (psli->ring[psli->fcp_ring].cmdringaddr)
467 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
468 if (psli->ring[psli->next_ring].cmdringaddr)
469 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
470
471 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500472 if (phba->sli_rev != 3)
473 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500474
James Smart93996272008-08-24 21:50:30 -0400475 /*
476 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
477 */
478 if (phba->intr_type == MSIX) {
479 rc = lpfc_config_msi(phba, pmb);
480 if (rc) {
481 mempool_free(pmb, phba->mbox_mem_pool);
482 return -EIO;
483 }
484 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
485 if (rc != MBX_SUCCESS) {
486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
487 "0352 Config MSI mailbox command "
488 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400489 pmb->u.mb.mbxCommand,
490 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400491 mempool_free(pmb, phba->mbox_mem_pool);
492 return -EIO;
493 }
494 }
495
James Smart04c68492009-05-22 14:52:52 -0400496 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400497 /* Initialize ERATT handling flag */
498 phba->hba_flag &= ~HBA_ERATT_HANDLED;
499
dea31012005-04-17 16:05:31 -0500500 /* Enable appropriate host interrupts */
dea31012005-04-17 16:05:31 -0500501 status = readl(phba->HCregaddr);
502 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
503 if (psli->num_rings > 0)
504 status |= HC_R0INT_ENA;
505 if (psli->num_rings > 1)
506 status |= HC_R1INT_ENA;
507 if (psli->num_rings > 2)
508 status |= HC_R2INT_ENA;
509 if (psli->num_rings > 3)
510 status |= HC_R3INT_ENA;
511
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500512 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
513 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400514 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500515
dea31012005-04-17 16:05:31 -0500516 writel(status, phba->HCregaddr);
517 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500518 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500519
James Smart93996272008-08-24 21:50:30 -0400520 /* Set up ring-0 (ELS) timer */
521 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500522 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400523 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500524 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
525 phba->hb_outstanding = 0;
526 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400527 /* Set up error attention (ERATT) polling timer */
528 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500529
James Smarta0c87cb2009-07-19 10:01:10 -0400530 if (phba->hba_flag & LINK_DISABLED) {
531 lpfc_printf_log(phba,
532 KERN_ERR, LOG_INIT,
533 "2598 Adapter Link is disabled.\n");
534 lpfc_down_link(phba, pmb);
535 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
536 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
537 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
538 lpfc_printf_log(phba,
539 KERN_ERR, LOG_INIT,
540 "2599 Adapter failed to issue DOWN_LINK"
541 " mbox command rc 0x%x\n", rc);
542
543 mempool_free(pmb, phba->mbox_mem_pool);
544 return -EIO;
545 }
546 } else {
547 lpfc_init_link(phba, pmb, phba->cfg_topology,
548 phba->cfg_link_speed);
549 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
550 lpfc_set_loopback_flag(phba);
551 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
552 if (rc != MBX_SUCCESS) {
553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400554 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500555 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500556 mb->mbxCommand, mb->mbxStatus);
557
James Smarta0c87cb2009-07-19 10:01:10 -0400558 /* Clear all interrupt enable conditions */
559 writel(0, phba->HCregaddr);
560 readl(phba->HCregaddr); /* flush */
561 /* Clear all pending interrupts */
562 writel(0xffffffff, phba->HAregaddr);
563 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500564
James Smarta0c87cb2009-07-19 10:01:10 -0400565 phba->link_state = LPFC_HBA_ERROR;
566 if (rc != MBX_BUSY)
567 mempool_free(pmb, phba->mbox_mem_pool);
568 return -EIO;
569 }
dea31012005-04-17 16:05:31 -0500570 }
571 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400572 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
573 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
574 pmb->mbox_cmpl = lpfc_config_async_cmpl;
575 pmb->vport = phba->pport;
576 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500577
James Smart57127f12007-10-27 13:37:05 -0400578 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
579 lpfc_printf_log(phba,
580 KERN_ERR,
581 LOG_INIT,
582 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400583 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400584 rc);
585 mempool_free(pmb, phba->mbox_mem_pool);
586 }
James Smart97207482008-12-04 22:39:19 -0500587
588 /* Get Option rom version */
589 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
590 lpfc_dump_wakeup_param(phba, pmb);
591 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
592 pmb->vport = phba->pport;
593 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
594
595 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400597 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500598 mempool_free(pmb, phba->mbox_mem_pool);
599 }
600
James Smartd7c255b2008-08-24 21:50:00 -0400601 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400602}
603
James Smarte59058c2008-08-24 21:49:00 -0400604/**
James Smart3621a712009-04-06 18:47:14 -0400605 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400606 * @phba: pointer to lpfc HBA data structure.
607 *
608 * This routine will do LPFC uninitialization before the HBA is reset when
609 * bringing down the SLI Layer.
610 *
611 * Return codes
612 * 0 - success.
613 * Any other value - error.
614 **/
dea31012005-04-17 16:05:31 -0500615int
James Smart2e0fef82007-06-17 19:56:36 -0500616lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500617{
James Smart1b32f6a2008-02-08 18:49:39 -0500618 struct lpfc_vport **vports;
619 int i;
James Smart3772a992009-05-22 14:50:54 -0400620
621 if (phba->sli_rev <= LPFC_SLI_REV3) {
622 /* Disable interrupts */
623 writel(0, phba->HCregaddr);
624 readl(phba->HCregaddr); /* flush */
625 }
dea31012005-04-17 16:05:31 -0500626
James Smart1b32f6a2008-02-08 18:49:39 -0500627 if (phba->pport->load_flag & FC_UNLOADING)
628 lpfc_cleanup_discovery_resources(phba->pport);
629 else {
630 vports = lpfc_create_vport_work_array(phba);
631 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400632 for (i = 0; i <= phba->max_vports &&
633 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500634 lpfc_cleanup_discovery_resources(vports[i]);
635 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500636 }
637 return 0;
dea31012005-04-17 16:05:31 -0500638}
639
James Smarte59058c2008-08-24 21:49:00 -0400640/**
James Smart3772a992009-05-22 14:50:54 -0400641 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400642 * @phba: pointer to lpfc HBA data structure.
643 *
644 * This routine will do uninitialization after the HBA is reset when bring
645 * down the SLI Layer.
646 *
647 * Return codes
648 * 0 - sucess.
649 * Any other value - error.
650 **/
James Smart3772a992009-05-22 14:50:54 -0400651static int
652lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500653{
654 struct lpfc_sli *psli = &phba->sli;
655 struct lpfc_sli_ring *pring;
656 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500657 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500658 int i;
659
James Smart92d7f7b2007-06-17 19:56:38 -0500660 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
661 lpfc_sli_hbqbuf_free_all(phba);
662 else {
663 /* Cleanup preposted buffers on the ELS ring */
664 pring = &psli->ring[LPFC_ELS_RING];
665 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
666 list_del(&mp->list);
667 pring->postbufq_cnt--;
668 lpfc_mbuf_free(phba, mp->virt, mp->phys);
669 kfree(mp);
670 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500671 }
672
James Smart09372822008-01-11 01:52:54 -0500673 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500674 for (i = 0; i < psli->num_rings; i++) {
675 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500676
677 /* At this point in time the HBA is either reset or DOA. Either
678 * way, nothing should be on txcmplq as it will NEVER complete.
679 */
680 list_splice_init(&pring->txcmplq, &completions);
681 pring->txcmplq_cnt = 0;
682 spin_unlock_irq(&phba->hbalock);
683
James Smarta257bf92009-04-06 18:48:10 -0400684 /* Cancel all the IOCBs from the completions list */
685 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
686 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500687
Jamie Wellnitz41415862006-02-28 19:25:27 -0500688 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500689 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500690 }
James Smart09372822008-01-11 01:52:54 -0500691 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500692
693 return 0;
694}
James Smartda0436e2009-05-22 14:51:39 -0400695/**
696 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
697 * @phba: pointer to lpfc HBA data structure.
698 *
699 * This routine will do uninitialization after the HBA is reset when bring
700 * down the SLI Layer.
701 *
702 * Return codes
703 * 0 - sucess.
704 * Any other value - error.
705 **/
706static int
707lpfc_hba_down_post_s4(struct lpfc_hba *phba)
708{
709 struct lpfc_scsi_buf *psb, *psb_next;
710 LIST_HEAD(aborts);
711 int ret;
712 unsigned long iflag = 0;
713 ret = lpfc_hba_down_post_s3(phba);
714 if (ret)
715 return ret;
716 /* At this point in time the HBA is either reset or DOA. Either
717 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
718 * on the lpfc_sgl_list so that it can either be freed if the
719 * driver is unloading or reposted if the driver is restarting
720 * the port.
721 */
722 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
723 /* scsl_buf_list */
724 /* abts_sgl_list_lock required because worker thread uses this
725 * list.
726 */
727 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
728 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
729 &phba->sli4_hba.lpfc_sgl_list);
730 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
731 /* abts_scsi_buf_list_lock required because worker thread uses this
732 * list.
733 */
734 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
735 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
736 &aborts);
737 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
738 spin_unlock_irq(&phba->hbalock);
739
740 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
741 psb->pCmd = NULL;
742 psb->status = IOSTAT_SUCCESS;
743 }
744 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
745 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
746 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
747 return 0;
748}
749
750/**
751 * lpfc_hba_down_post - Wrapper func for hba down post routine
752 * @phba: pointer to lpfc HBA data structure.
753 *
754 * This routine wraps the actual SLI3 or SLI4 routine for performing
755 * uninitialization after the HBA is reset when bring down the SLI Layer.
756 *
757 * Return codes
758 * 0 - sucess.
759 * Any other value - error.
760 **/
761int
762lpfc_hba_down_post(struct lpfc_hba *phba)
763{
764 return (*phba->lpfc_hba_down_post)(phba);
765}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500766
James Smarte59058c2008-08-24 21:49:00 -0400767/**
James Smart3621a712009-04-06 18:47:14 -0400768 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400769 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
770 *
771 * This is the HBA-timer timeout handler registered to the lpfc driver. When
772 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
773 * work-port-events bitmap and the worker thread is notified. This timeout
774 * event will be used by the worker thread to invoke the actual timeout
775 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
776 * be performed in the timeout handler and the HBA timeout event bit shall
777 * be cleared by the worker thread after it has taken the event bitmap out.
778 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100779static void
James Smart858c9f62007-06-17 19:56:39 -0500780lpfc_hb_timeout(unsigned long ptr)
781{
782 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400783 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500784 unsigned long iflag;
785
786 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400787
788 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500789 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400790 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
791 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500792 phba->pport->work_port_events |= WORKER_HB_TMO;
793 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
794
James Smart93996272008-08-24 21:50:30 -0400795 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400796 if (!tmo_posted)
797 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500798 return;
799}
800
James Smarte59058c2008-08-24 21:49:00 -0400801/**
James Smart3621a712009-04-06 18:47:14 -0400802 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400803 * @phba: pointer to lpfc hba data structure.
804 * @pmboxq: pointer to the driver internal queue element for mailbox command.
805 *
806 * This is the callback function to the lpfc heart-beat mailbox command.
807 * If configured, the lpfc driver issues the heart-beat mailbox command to
808 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
809 * heart-beat mailbox command is issued, the driver shall set up heart-beat
810 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
811 * heart-beat outstanding state. Once the mailbox command comes back and
812 * no error conditions detected, the heart-beat mailbox command timer is
813 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
814 * state is cleared for the next heart-beat. If the timer expired with the
815 * heart-beat outstanding state set, the driver will put the HBA offline.
816 **/
James Smart858c9f62007-06-17 19:56:39 -0500817static void
818lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
819{
820 unsigned long drvr_flag;
821
822 spin_lock_irqsave(&phba->hbalock, drvr_flag);
823 phba->hb_outstanding = 0;
824 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
825
James Smart93996272008-08-24 21:50:30 -0400826 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500827 mempool_free(pmboxq, phba->mbox_mem_pool);
828 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
829 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400830 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500831 mod_timer(&phba->hb_tmofunc,
832 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
833 return;
834}
835
James Smarte59058c2008-08-24 21:49:00 -0400836/**
James Smart3621a712009-04-06 18:47:14 -0400837 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400838 * @phba: pointer to lpfc hba data structure.
839 *
840 * This is the actual HBA-timer timeout handler to be invoked by the worker
841 * thread whenever the HBA timer fired and HBA-timeout event posted. This
842 * handler performs any periodic operations needed for the device. If such
843 * periodic event has already been attended to either in the interrupt handler
844 * or by processing slow-ring or fast-ring events within the HBA-timer
845 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
846 * the timer for the next timeout period. If lpfc heart-beat mailbox command
847 * is configured and there is no heart-beat mailbox command outstanding, a
848 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
849 * has been a heart-beat mailbox command outstanding, the HBA shall be put
850 * to offline.
851 **/
James Smart858c9f62007-06-17 19:56:39 -0500852void
853lpfc_hb_timeout_handler(struct lpfc_hba *phba)
854{
855 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500856 struct lpfc_dmabuf *buf_ptr;
James Smart858c9f62007-06-17 19:56:39 -0500857 int retval;
858 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500859 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500860
861 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400862 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500863 (phba->pport->fc_flag & FC_OFFLINE_MODE))
864 return;
865
866 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -0500867
868 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
869 jiffies)) {
870 spin_unlock_irq(&phba->pport->work_port_lock);
871 if (!phba->hb_outstanding)
872 mod_timer(&phba->hb_tmofunc,
873 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
874 else
875 mod_timer(&phba->hb_tmofunc,
876 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
877 return;
878 }
879 spin_unlock_irq(&phba->pport->work_port_lock);
880
James Smart0ff10d42008-01-11 01:52:36 -0500881 if (phba->elsbuf_cnt &&
882 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
883 spin_lock_irq(&phba->hbalock);
884 list_splice_init(&phba->elsbuf, &completions);
885 phba->elsbuf_cnt = 0;
886 phba->elsbuf_prev_cnt = 0;
887 spin_unlock_irq(&phba->hbalock);
888
889 while (!list_empty(&completions)) {
890 list_remove_head(&completions, buf_ptr,
891 struct lpfc_dmabuf, list);
892 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
893 kfree(buf_ptr);
894 }
895 }
896 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
897
James Smart858c9f62007-06-17 19:56:39 -0500898 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -0500899 if (phba->cfg_enable_hba_heartbeat) {
900 if (!phba->hb_outstanding) {
901 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
902 if (!pmboxq) {
903 mod_timer(&phba->hb_tmofunc,
904 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
905 return;
906 }
907
908 lpfc_heart_beat(phba, pmboxq);
909 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
910 pmboxq->vport = phba->pport;
911 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
912
913 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
914 mempool_free(pmboxq, phba->mbox_mem_pool);
915 mod_timer(&phba->hb_tmofunc,
916 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
917 return;
918 }
James Smart858c9f62007-06-17 19:56:39 -0500919 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -0500920 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
921 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -0500922 return;
James Smart13815c82008-01-11 01:52:48 -0500923 } else {
924 /*
925 * If heart beat timeout called with hb_outstanding set
926 * we need to take the HBA offline.
927 */
928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
929 "0459 Adapter heartbeat failure, "
930 "taking this port offline.\n");
931
932 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400933 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart13815c82008-01-11 01:52:48 -0500934 spin_unlock_irq(&phba->hbalock);
935
936 lpfc_offline_prep(phba);
937 lpfc_offline(phba);
938 lpfc_unblock_mgmt_io(phba);
939 phba->link_state = LPFC_HBA_ERROR;
940 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -0500941 }
James Smart858c9f62007-06-17 19:56:39 -0500942 }
943}
944
James Smarte59058c2008-08-24 21:49:00 -0400945/**
James Smart3621a712009-04-06 18:47:14 -0400946 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -0400947 * @phba: pointer to lpfc hba data structure.
948 *
949 * This routine is called to bring the HBA offline when HBA hardware error
950 * other than Port Error 6 has been detected.
951 **/
James Smart09372822008-01-11 01:52:54 -0500952static void
953lpfc_offline_eratt(struct lpfc_hba *phba)
954{
955 struct lpfc_sli *psli = &phba->sli;
956
957 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400958 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -0500959 spin_unlock_irq(&phba->hbalock);
960 lpfc_offline_prep(phba);
961
962 lpfc_offline(phba);
963 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400964 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500965 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400966 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500967 lpfc_hba_down_post(phba);
968 lpfc_sli_brdready(phba, HS_MBRDY);
969 lpfc_unblock_mgmt_io(phba);
970 phba->link_state = LPFC_HBA_ERROR;
971 return;
972}
973
James Smarte59058c2008-08-24 21:49:00 -0400974/**
James Smartda0436e2009-05-22 14:51:39 -0400975 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
976 * @phba: pointer to lpfc hba data structure.
977 *
978 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
979 * other than Port Error 6 has been detected.
980 **/
981static void
982lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
983{
984 lpfc_offline_prep(phba);
985 lpfc_offline(phba);
986 lpfc_sli4_brdreset(phba);
987 lpfc_hba_down_post(phba);
988 lpfc_sli4_post_status_check(phba);
989 lpfc_unblock_mgmt_io(phba);
990 phba->link_state = LPFC_HBA_ERROR;
991}
992
993/**
James Smarta257bf92009-04-06 18:48:10 -0400994 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
995 * @phba: pointer to lpfc hba data structure.
996 *
997 * This routine is invoked to handle the deferred HBA hardware error
998 * conditions. This type of error is indicated by HBA by setting ER1
999 * and another ER bit in the host status register. The driver will
1000 * wait until the ER1 bit clears before handling the error condition.
1001 **/
1002static void
1003lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1004{
1005 uint32_t old_host_status = phba->work_hs;
1006 struct lpfc_sli_ring *pring;
1007 struct lpfc_sli *psli = &phba->sli;
1008
James Smartf4b4c682009-05-22 14:53:12 -04001009 /* If the pci channel is offline, ignore possible errors,
1010 * since we cannot communicate with the pci card anyway.
1011 */
1012 if (pci_channel_offline(phba->pcidev)) {
1013 spin_lock_irq(&phba->hbalock);
1014 phba->hba_flag &= ~DEFER_ERATT;
1015 spin_unlock_irq(&phba->hbalock);
1016 return;
1017 }
1018
James Smarta257bf92009-04-06 18:48:10 -04001019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1020 "0479 Deferred Adapter Hardware Error "
1021 "Data: x%x x%x x%x\n",
1022 phba->work_hs,
1023 phba->work_status[0], phba->work_status[1]);
1024
1025 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001026 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001027 spin_unlock_irq(&phba->hbalock);
1028
1029
1030 /*
1031 * Firmware stops when it triggred erratt. That could cause the I/Os
1032 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1033 * SCSI layer retry it after re-establishing link.
1034 */
1035 pring = &psli->ring[psli->fcp_ring];
1036 lpfc_sli_abort_iocb_ring(phba, pring);
1037
1038 /*
1039 * There was a firmware error. Take the hba offline and then
1040 * attempt to restart it.
1041 */
1042 lpfc_offline_prep(phba);
1043 lpfc_offline(phba);
1044
1045 /* Wait for the ER1 bit to clear.*/
1046 while (phba->work_hs & HS_FFER1) {
1047 msleep(100);
1048 phba->work_hs = readl(phba->HSregaddr);
1049 /* If driver is unloading let the worker thread continue */
1050 if (phba->pport->load_flag & FC_UNLOADING) {
1051 phba->work_hs = 0;
1052 break;
1053 }
1054 }
1055
1056 /*
1057 * This is to ptrotect against a race condition in which
1058 * first write to the host attention register clear the
1059 * host status register.
1060 */
1061 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1062 phba->work_hs = old_host_status & ~HS_FFER1;
1063
James Smart3772a992009-05-22 14:50:54 -04001064 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001065 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001066 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001067 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1068 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1069}
1070
James Smart3772a992009-05-22 14:50:54 -04001071static void
1072lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1073{
1074 struct lpfc_board_event_header board_event;
1075 struct Scsi_Host *shost;
1076
1077 board_event.event_type = FC_REG_BOARD_EVENT;
1078 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1079 shost = lpfc_shost_from_vport(phba->pport);
1080 fc_host_post_vendor_event(shost, fc_get_event_number(),
1081 sizeof(board_event),
1082 (char *) &board_event,
1083 LPFC_NL_VENDOR_ID);
1084}
1085
James Smarta257bf92009-04-06 18:48:10 -04001086/**
James Smart3772a992009-05-22 14:50:54 -04001087 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001088 * @phba: pointer to lpfc hba data structure.
1089 *
1090 * This routine is invoked to handle the following HBA hardware error
1091 * conditions:
1092 * 1 - HBA error attention interrupt
1093 * 2 - DMA ring index out of range
1094 * 3 - Mailbox command came back as unknown
1095 **/
James Smart3772a992009-05-22 14:50:54 -04001096static void
1097lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001098{
James Smart2e0fef82007-06-17 19:56:36 -05001099 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001100 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001101 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001102 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001103 unsigned long temperature;
1104 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001105 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001106
Linas Vepstas8d63f372007-02-14 14:28:36 -06001107 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001108 * since we cannot communicate with the pci card anyway.
1109 */
1110 if (pci_channel_offline(phba->pcidev)) {
1111 spin_lock_irq(&phba->hbalock);
1112 phba->hba_flag &= ~DEFER_ERATT;
1113 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001114 return;
James Smart3772a992009-05-22 14:50:54 -04001115 }
1116
James Smart13815c82008-01-11 01:52:48 -05001117 /* If resets are disabled then leave the HBA alone and return */
1118 if (!phba->cfg_enable_hba_reset)
1119 return;
dea31012005-04-17 16:05:31 -05001120
James Smartea2151b2008-09-07 11:52:10 -04001121 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001122 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001123
James Smarta257bf92009-04-06 18:48:10 -04001124 if (phba->hba_flag & DEFER_ERATT)
1125 lpfc_handle_deferred_eratt(phba);
1126
James Smart97eab632008-04-07 10:16:05 -04001127 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -05001128 /* Re-establishing Link */
1129 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001130 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -05001131 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001132 phba->work_hs,
dea31012005-04-17 16:05:31 -05001133 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001134
James Smart92d7f7b2007-06-17 19:56:38 -05001135 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001136 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001137 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001138
1139 /*
1140 * Firmware stops when it triggled erratt with HS_FFER6.
1141 * That could cause the I/Os dropped by the firmware.
1142 * Error iocb (I/O) on txcmplq and let the SCSI layer
1143 * retry it after re-establishing link.
1144 */
1145 pring = &psli->ring[psli->fcp_ring];
1146 lpfc_sli_abort_iocb_ring(phba, pring);
1147
dea31012005-04-17 16:05:31 -05001148 /*
1149 * There was a firmware error. Take the hba offline and then
1150 * attempt to restart it.
1151 */
James Smart46fa3112007-04-25 09:51:45 -04001152 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001153 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001154 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001155 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001156 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001157 return;
1158 }
James Smart46fa3112007-04-25 09:51:45 -04001159 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001160 } else if (phba->work_hs & HS_CRIT_TEMP) {
1161 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1162 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1163 temp_event_data.event_code = LPFC_CRIT_TEMP;
1164 temp_event_data.data = (uint32_t)temperature;
1165
1166 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001167 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001168 "(%ld), taking this port offline "
1169 "Data: x%x x%x x%x\n",
1170 temperature, phba->work_hs,
1171 phba->work_status[0], phba->work_status[1]);
1172
1173 shost = lpfc_shost_from_vport(phba->pport);
1174 fc_host_post_vendor_event(shost, fc_get_event_number(),
1175 sizeof(temp_event_data),
1176 (char *) &temp_event_data,
1177 SCSI_NL_VID_TYPE_PCI
1178 | PCI_VENDOR_ID_EMULEX);
1179
James Smart7af67052007-10-27 13:38:11 -04001180 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001181 phba->over_temp_state = HBA_OVER_TEMP;
1182 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001183 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001184
dea31012005-04-17 16:05:31 -05001185 } else {
1186 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001187 * failure is a value other than FFER6. Do not call the offline
1188 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001189 */
1190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001191 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001192 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001193 phba->work_hs,
dea31012005-04-17 16:05:31 -05001194 phba->work_status[0], phba->work_status[1]);
1195
James Smartd2873e42006-08-18 17:46:43 -04001196 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001197 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001198 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001199 sizeof(event_data), (char *) &event_data,
1200 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1201
James Smart09372822008-01-11 01:52:54 -05001202 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001203 }
James Smart93996272008-08-24 21:50:30 -04001204 return;
dea31012005-04-17 16:05:31 -05001205}
1206
James Smarte59058c2008-08-24 21:49:00 -04001207/**
James Smartda0436e2009-05-22 14:51:39 -04001208 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1209 * @phba: pointer to lpfc hba data structure.
1210 *
1211 * This routine is invoked to handle the SLI4 HBA hardware error attention
1212 * conditions.
1213 **/
1214static void
1215lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1216{
1217 struct lpfc_vport *vport = phba->pport;
1218 uint32_t event_data;
1219 struct Scsi_Host *shost;
1220
1221 /* If the pci channel is offline, ignore possible errors, since
1222 * we cannot communicate with the pci card anyway.
1223 */
1224 if (pci_channel_offline(phba->pcidev))
1225 return;
1226 /* If resets are disabled then leave the HBA alone and return */
1227 if (!phba->cfg_enable_hba_reset)
1228 return;
1229
1230 /* Send an internal error event to mgmt application */
1231 lpfc_board_errevt_to_mgmt(phba);
1232
1233 /* For now, the actual action for SLI4 device handling is not
1234 * specified yet, just treated it as adaptor hardware failure
1235 */
1236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1237 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1238 phba->work_status[0], phba->work_status[1]);
1239
1240 event_data = FC_REG_DUMP_EVENT;
1241 shost = lpfc_shost_from_vport(vport);
1242 fc_host_post_vendor_event(shost, fc_get_event_number(),
1243 sizeof(event_data), (char *) &event_data,
1244 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1245
1246 lpfc_sli4_offline_eratt(phba);
1247}
1248
1249/**
1250 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1251 * @phba: pointer to lpfc HBA data structure.
1252 *
1253 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1254 * routine from the API jump table function pointer from the lpfc_hba struct.
1255 *
1256 * Return codes
1257 * 0 - sucess.
1258 * Any other value - error.
1259 **/
1260void
1261lpfc_handle_eratt(struct lpfc_hba *phba)
1262{
1263 (*phba->lpfc_handle_eratt)(phba);
1264}
1265
1266/**
James Smart3621a712009-04-06 18:47:14 -04001267 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001268 * @phba: pointer to lpfc hba data structure.
1269 *
1270 * This routine is invoked from the worker thread to handle a HBA host
1271 * attention link event.
1272 **/
dea31012005-04-17 16:05:31 -05001273void
James Smart2e0fef82007-06-17 19:56:36 -05001274lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001275{
James Smart2e0fef82007-06-17 19:56:36 -05001276 struct lpfc_vport *vport = phba->pport;
1277 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001278 LPFC_MBOXQ_t *pmb;
1279 volatile uint32_t control;
1280 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001281 int rc = 0;
dea31012005-04-17 16:05:31 -05001282
1283 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001284 if (!pmb) {
1285 rc = 1;
dea31012005-04-17 16:05:31 -05001286 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001287 }
dea31012005-04-17 16:05:31 -05001288
1289 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001290 if (!mp) {
1291 rc = 2;
dea31012005-04-17 16:05:31 -05001292 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001293 }
dea31012005-04-17 16:05:31 -05001294
1295 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001296 if (!mp->virt) {
1297 rc = 3;
dea31012005-04-17 16:05:31 -05001298 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001299 }
dea31012005-04-17 16:05:31 -05001300
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001301 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001302 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001303
1304 psli->slistat.link_event++;
1305 lpfc_read_la(phba, pmb, mp);
1306 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001307 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001308 /* Block ELS IOCBs until we have processed this mbox command */
1309 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001310 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001311 if (rc == MBX_NOT_FINISHED) {
1312 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001313 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001314 }
dea31012005-04-17 16:05:31 -05001315
1316 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001317 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001318 writel(HA_LATT, phba->HAregaddr);
1319 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001320 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001321
1322 return;
1323
James Smart14691152006-12-02 13:34:28 -05001324lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001325 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001326 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001327lpfc_handle_latt_free_mp:
1328 kfree(mp);
1329lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001330 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001331lpfc_handle_latt_err_exit:
1332 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001333 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001334 psli->sli_flag |= LPFC_PROCESS_LA;
1335 control = readl(phba->HCregaddr);
1336 control |= HC_LAINT_ENA;
1337 writel(control, phba->HCregaddr);
1338 readl(phba->HCregaddr); /* flush */
1339
1340 /* Clear Link Attention in HA REG */
1341 writel(HA_LATT, phba->HAregaddr);
1342 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001343 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001344 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001345 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001346
James Smart09372822008-01-11 01:52:54 -05001347 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1348 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001349
1350 return;
1351}
1352
James Smarte59058c2008-08-24 21:49:00 -04001353/**
James Smart3621a712009-04-06 18:47:14 -04001354 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001355 * @phba: pointer to lpfc hba data structure.
1356 * @vpd: pointer to the vital product data.
1357 * @len: length of the vital product data in bytes.
1358 *
1359 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1360 * an array of characters. In this routine, the ModelName, ProgramType, and
1361 * ModelDesc, etc. fields of the phba data structure will be populated.
1362 *
1363 * Return codes
1364 * 0 - pointer to the VPD passed in is NULL
1365 * 1 - success
1366 **/
James Smart3772a992009-05-22 14:50:54 -04001367int
James Smart2e0fef82007-06-17 19:56:36 -05001368lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001369{
1370 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001371 int Length;
dea31012005-04-17 16:05:31 -05001372 int i, j;
1373 int finished = 0;
1374 int index = 0;
1375
1376 if (!vpd)
1377 return 0;
1378
1379 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001380 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001381 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001382 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1383 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001384 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001385 switch (vpd[index]) {
1386 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001387 case 0x91:
dea31012005-04-17 16:05:31 -05001388 index += 1;
1389 lenlo = vpd[index];
1390 index += 1;
1391 lenhi = vpd[index];
1392 index += 1;
1393 i = ((((unsigned short)lenhi) << 8) + lenlo);
1394 index += i;
1395 break;
1396 case 0x90:
1397 index += 1;
1398 lenlo = vpd[index];
1399 index += 1;
1400 lenhi = vpd[index];
1401 index += 1;
1402 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001403 if (Length > len - index)
1404 Length = len - index;
dea31012005-04-17 16:05:31 -05001405 while (Length > 0) {
1406 /* Look for Serial Number */
1407 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1408 index += 2;
1409 i = vpd[index];
1410 index += 1;
1411 j = 0;
1412 Length -= (3+i);
1413 while(i--) {
1414 phba->SerialNumber[j++] = vpd[index++];
1415 if (j == 31)
1416 break;
1417 }
1418 phba->SerialNumber[j] = 0;
1419 continue;
1420 }
1421 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1422 phba->vpd_flag |= VPD_MODEL_DESC;
1423 index += 2;
1424 i = vpd[index];
1425 index += 1;
1426 j = 0;
1427 Length -= (3+i);
1428 while(i--) {
1429 phba->ModelDesc[j++] = vpd[index++];
1430 if (j == 255)
1431 break;
1432 }
1433 phba->ModelDesc[j] = 0;
1434 continue;
1435 }
1436 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1437 phba->vpd_flag |= VPD_MODEL_NAME;
1438 index += 2;
1439 i = vpd[index];
1440 index += 1;
1441 j = 0;
1442 Length -= (3+i);
1443 while(i--) {
1444 phba->ModelName[j++] = vpd[index++];
1445 if (j == 79)
1446 break;
1447 }
1448 phba->ModelName[j] = 0;
1449 continue;
1450 }
1451 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1452 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1453 index += 2;
1454 i = vpd[index];
1455 index += 1;
1456 j = 0;
1457 Length -= (3+i);
1458 while(i--) {
1459 phba->ProgramType[j++] = vpd[index++];
1460 if (j == 255)
1461 break;
1462 }
1463 phba->ProgramType[j] = 0;
1464 continue;
1465 }
1466 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1467 phba->vpd_flag |= VPD_PORT;
1468 index += 2;
1469 i = vpd[index];
1470 index += 1;
1471 j = 0;
1472 Length -= (3+i);
1473 while(i--) {
1474 phba->Port[j++] = vpd[index++];
1475 if (j == 19)
1476 break;
1477 }
1478 phba->Port[j] = 0;
1479 continue;
1480 }
1481 else {
1482 index += 2;
1483 i = vpd[index];
1484 index += 1;
1485 index += i;
1486 Length -= (3 + i);
1487 }
1488 }
1489 finished = 0;
1490 break;
1491 case 0x78:
1492 finished = 1;
1493 break;
1494 default:
1495 index ++;
1496 break;
1497 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001498 }
dea31012005-04-17 16:05:31 -05001499
1500 return(1);
1501}
1502
James Smarte59058c2008-08-24 21:49:00 -04001503/**
James Smart3621a712009-04-06 18:47:14 -04001504 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001505 * @phba: pointer to lpfc hba data structure.
1506 * @mdp: pointer to the data structure to hold the derived model name.
1507 * @descp: pointer to the data structure to hold the derived description.
1508 *
1509 * This routine retrieves HBA's description based on its registered PCI device
1510 * ID. The @descp passed into this function points to an array of 256 chars. It
1511 * shall be returned with the model name, maximum speed, and the host bus type.
1512 * The @mdp passed into this function points to an array of 80 chars. When the
1513 * function returns, the @mdp will be filled with the model name.
1514 **/
dea31012005-04-17 16:05:31 -05001515static void
James Smart2e0fef82007-06-17 19:56:36 -05001516lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001517{
1518 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001519 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001520 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001521 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001522 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001523 struct {
1524 char * name;
1525 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001526 char * bus;
James Smart18a3b592006-12-02 13:35:08 -05001527 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001528
1529 if (mdp && mdp[0] != '\0'
1530 && descp && descp[0] != '\0')
1531 return;
1532
1533 if (phba->lmt & LMT_10Gb)
1534 max_speed = 10;
1535 else if (phba->lmt & LMT_8Gb)
1536 max_speed = 8;
1537 else if (phba->lmt & LMT_4Gb)
1538 max_speed = 4;
1539 else if (phba->lmt & LMT_2Gb)
1540 max_speed = 2;
1541 else
1542 max_speed = 1;
dea31012005-04-17 16:05:31 -05001543
1544 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001545
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001546 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001547 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -05001548 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001549 break;
dea31012005-04-17 16:05:31 -05001550 case PCI_DEVICE_ID_SUPERFLY:
1551 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -05001552 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001553 else
James Smart18a3b592006-12-02 13:35:08 -05001554 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001555 break;
1556 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -05001557 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001558 break;
1559 case PCI_DEVICE_ID_CENTAUR:
1560 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -05001561 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001562 else
James Smart18a3b592006-12-02 13:35:08 -05001563 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001564 break;
1565 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -05001566 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001567 break;
1568 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -05001569 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001570 break;
1571 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -05001572 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001573 break;
1574 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -05001575 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001576 break;
1577 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -05001578 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001579 break;
1580 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -05001581 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001582 break;
1583 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -05001584 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001585 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001586 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001587 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001588 break;
1589 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001590 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001591 break;
1592 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -05001593 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001594 break;
1595 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001596 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001597 break;
1598 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001599 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001600 break;
dea31012005-04-17 16:05:31 -05001601 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -05001602 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001603 break;
1604 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -05001605 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001606 break;
1607 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -05001608 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001609 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001610 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001611 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001612 break;
1613 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta257bf92009-04-06 18:48:10 -04001614 m = (typeof(m)){"LP2105", max_speed, "PCIe"};
1615 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001616 break;
dea31012005-04-17 16:05:31 -05001617 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -05001618 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001619 break;
1620 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -05001621 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001622 break;
1623 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -05001624 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001625 break;
1626 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -05001627 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001628 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001629 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -05001630 m = (typeof(m)){"LP11000-S", max_speed,
1631 "PCI-X2"};
1632 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001633 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -05001634 m = (typeof(m)){"LPe11000-S", max_speed,
1635 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001636 break;
James Smartb87eab32007-04-25 09:53:28 -04001637 case PCI_DEVICE_ID_SAT:
1638 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1639 break;
1640 case PCI_DEVICE_ID_SAT_MID:
1641 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1642 break;
1643 case PCI_DEVICE_ID_SAT_SMB:
1644 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1645 break;
1646 case PCI_DEVICE_ID_SAT_DCSP:
1647 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1648 break;
1649 case PCI_DEVICE_ID_SAT_SCSP:
1650 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1651 break;
1652 case PCI_DEVICE_ID_SAT_S:
1653 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1654 break;
James Smart84774a42008-08-24 21:50:06 -04001655 case PCI_DEVICE_ID_HORNET:
1656 m = (typeof(m)){"LP21000", max_speed, "PCIe"};
1657 GE = 1;
1658 break;
1659 case PCI_DEVICE_ID_PROTEUS_VF:
1660 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1661 break;
1662 case PCI_DEVICE_ID_PROTEUS_PF:
1663 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1664 break;
1665 case PCI_DEVICE_ID_PROTEUS_S:
1666 m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
1667 break;
James Smartda0436e2009-05-22 14:51:39 -04001668 case PCI_DEVICE_ID_TIGERSHARK:
1669 oneConnect = 1;
1670 m = (typeof(m)) {"OCe10100-F", max_speed, "PCIe"};
1671 break;
James Smart6669f9b2009-10-02 15:16:45 -04001672 case PCI_DEVICE_ID_TS_BE3:
1673 oneConnect = 1;
1674 m = (typeof(m)) {"OCeXXXXX-F", max_speed, "PCIe"};
1675 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001676 default:
Randy Dunlap041976f2006-06-25 01:58:51 -07001677 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001678 break;
dea31012005-04-17 16:05:31 -05001679 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001680
1681 if (mdp && mdp[0] == '\0')
1682 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001683 /* oneConnect hba requires special processing, they are all initiators
1684 * and we put the port number on the end
1685 */
1686 if (descp && descp[0] == '\0') {
1687 if (oneConnect)
1688 snprintf(descp, 255,
1689 "Emulex OneConnect %s, FCoE Initiator, Port %s",
1690 m.name,
1691 phba->Port);
1692 else
1693 snprintf(descp, 255,
1694 "Emulex %s %d%s %s %s",
1695 m.name, m.max_speed,
1696 (GE) ? "GE" : "Gb",
1697 m.bus,
1698 (GE) ? "FCoE Adapter" :
1699 "Fibre Channel Adapter");
1700 }
dea31012005-04-17 16:05:31 -05001701}
1702
James Smarte59058c2008-08-24 21:49:00 -04001703/**
James Smart3621a712009-04-06 18:47:14 -04001704 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001705 * @phba: pointer to lpfc hba data structure.
1706 * @pring: pointer to a IOCB ring.
1707 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1708 *
1709 * This routine posts a given number of IOCBs with the associated DMA buffer
1710 * descriptors specified by the cnt argument to the given IOCB ring.
1711 *
1712 * Return codes
1713 * The number of IOCBs NOT able to be posted to the IOCB ring.
1714 **/
dea31012005-04-17 16:05:31 -05001715int
James Smart495a7142008-06-14 22:52:59 -04001716lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001717{
1718 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001719 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001720 struct lpfc_dmabuf *mp1, *mp2;
1721
1722 cnt += pring->missbufcnt;
1723
1724 /* While there are buffers to post */
1725 while (cnt > 0) {
1726 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001727 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001728 if (iocb == NULL) {
1729 pring->missbufcnt = cnt;
1730 return cnt;
1731 }
dea31012005-04-17 16:05:31 -05001732 icmd = &iocb->iocb;
1733
1734 /* 2 buffers can be posted per command */
1735 /* Allocate buffer to post */
1736 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1737 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001738 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1739 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001740 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001741 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001742 pring->missbufcnt = cnt;
1743 return cnt;
1744 }
1745
1746 INIT_LIST_HEAD(&mp1->list);
1747 /* Allocate buffer to post */
1748 if (cnt > 1) {
1749 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1750 if (mp2)
1751 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1752 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001753 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001754 kfree(mp2);
dea31012005-04-17 16:05:31 -05001755 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1756 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001757 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001758 pring->missbufcnt = cnt;
1759 return cnt;
1760 }
1761
1762 INIT_LIST_HEAD(&mp2->list);
1763 } else {
1764 mp2 = NULL;
1765 }
1766
1767 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1768 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1769 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1770 icmd->ulpBdeCount = 1;
1771 cnt--;
1772 if (mp2) {
1773 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1774 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1775 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1776 cnt--;
1777 icmd->ulpBdeCount = 2;
1778 }
1779
1780 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1781 icmd->ulpLe = 1;
1782
James Smart3772a992009-05-22 14:50:54 -04001783 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1784 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001785 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1786 kfree(mp1);
1787 cnt++;
1788 if (mp2) {
1789 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1790 kfree(mp2);
1791 cnt++;
1792 }
James Bottomley604a3e32005-10-29 10:28:33 -05001793 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001794 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001795 return cnt;
1796 }
dea31012005-04-17 16:05:31 -05001797 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001798 if (mp2)
dea31012005-04-17 16:05:31 -05001799 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001800 }
1801 pring->missbufcnt = 0;
1802 return 0;
1803}
1804
James Smarte59058c2008-08-24 21:49:00 -04001805/**
James Smart3621a712009-04-06 18:47:14 -04001806 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001807 * @phba: pointer to lpfc hba data structure.
1808 *
1809 * This routine posts initial receive IOCB buffers to the ELS ring. The
1810 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1811 * set to 64 IOCBs.
1812 *
1813 * Return codes
1814 * 0 - success (currently always success)
1815 **/
dea31012005-04-17 16:05:31 -05001816static int
James Smart2e0fef82007-06-17 19:56:36 -05001817lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001818{
1819 struct lpfc_sli *psli = &phba->sli;
1820
1821 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001822 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001823 /* Ring 2 - FCP no buffers needed */
1824
1825 return 0;
1826}
1827
1828#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1829
James Smarte59058c2008-08-24 21:49:00 -04001830/**
James Smart3621a712009-04-06 18:47:14 -04001831 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001832 * @HashResultPointer: pointer to an array as hash table.
1833 *
1834 * This routine sets up the initial values to the array of hash table entries
1835 * for the LC HBAs.
1836 **/
dea31012005-04-17 16:05:31 -05001837static void
1838lpfc_sha_init(uint32_t * HashResultPointer)
1839{
1840 HashResultPointer[0] = 0x67452301;
1841 HashResultPointer[1] = 0xEFCDAB89;
1842 HashResultPointer[2] = 0x98BADCFE;
1843 HashResultPointer[3] = 0x10325476;
1844 HashResultPointer[4] = 0xC3D2E1F0;
1845}
1846
James Smarte59058c2008-08-24 21:49:00 -04001847/**
James Smart3621a712009-04-06 18:47:14 -04001848 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001849 * @HashResultPointer: pointer to an initial/result hash table.
1850 * @HashWorkingPointer: pointer to an working hash table.
1851 *
1852 * This routine iterates an initial hash table pointed by @HashResultPointer
1853 * with the values from the working hash table pointeed by @HashWorkingPointer.
1854 * The results are putting back to the initial hash table, returned through
1855 * the @HashResultPointer as the result hash table.
1856 **/
dea31012005-04-17 16:05:31 -05001857static void
1858lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1859{
1860 int t;
1861 uint32_t TEMP;
1862 uint32_t A, B, C, D, E;
1863 t = 16;
1864 do {
1865 HashWorkingPointer[t] =
1866 S(1,
1867 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1868 8] ^
1869 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1870 } while (++t <= 79);
1871 t = 0;
1872 A = HashResultPointer[0];
1873 B = HashResultPointer[1];
1874 C = HashResultPointer[2];
1875 D = HashResultPointer[3];
1876 E = HashResultPointer[4];
1877
1878 do {
1879 if (t < 20) {
1880 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1881 } else if (t < 40) {
1882 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1883 } else if (t < 60) {
1884 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1885 } else {
1886 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1887 }
1888 TEMP += S(5, A) + E + HashWorkingPointer[t];
1889 E = D;
1890 D = C;
1891 C = S(30, B);
1892 B = A;
1893 A = TEMP;
1894 } while (++t <= 79);
1895
1896 HashResultPointer[0] += A;
1897 HashResultPointer[1] += B;
1898 HashResultPointer[2] += C;
1899 HashResultPointer[3] += D;
1900 HashResultPointer[4] += E;
1901
1902}
1903
James Smarte59058c2008-08-24 21:49:00 -04001904/**
James Smart3621a712009-04-06 18:47:14 -04001905 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04001906 * @RandomChallenge: pointer to the entry of host challenge random number array.
1907 * @HashWorking: pointer to the entry of the working hash array.
1908 *
1909 * This routine calculates the working hash array referred by @HashWorking
1910 * from the challenge random numbers associated with the host, referred by
1911 * @RandomChallenge. The result is put into the entry of the working hash
1912 * array and returned by reference through @HashWorking.
1913 **/
dea31012005-04-17 16:05:31 -05001914static void
1915lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1916{
1917 *HashWorking = (*RandomChallenge ^ *HashWorking);
1918}
1919
James Smarte59058c2008-08-24 21:49:00 -04001920/**
James Smart3621a712009-04-06 18:47:14 -04001921 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04001922 * @phba: pointer to lpfc hba data structure.
1923 * @hbainit: pointer to an array of unsigned 32-bit integers.
1924 *
1925 * This routine performs the special handling for LC HBA initialization.
1926 **/
dea31012005-04-17 16:05:31 -05001927void
1928lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1929{
1930 int t;
1931 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001932 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001933
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001934 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001935 if (!HashWorking)
1936 return;
1937
dea31012005-04-17 16:05:31 -05001938 HashWorking[0] = HashWorking[78] = *pwwnn++;
1939 HashWorking[1] = HashWorking[79] = *pwwnn;
1940
1941 for (t = 0; t < 7; t++)
1942 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1943
1944 lpfc_sha_init(hbainit);
1945 lpfc_sha_iterate(hbainit, HashWorking);
1946 kfree(HashWorking);
1947}
1948
James Smarte59058c2008-08-24 21:49:00 -04001949/**
James Smart3621a712009-04-06 18:47:14 -04001950 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04001951 * @vport: pointer to a virtual N_Port data structure.
1952 *
1953 * This routine performs the necessary cleanups before deleting the @vport.
1954 * It invokes the discovery state machine to perform necessary state
1955 * transitions and to release the ndlps associated with the @vport. Note,
1956 * the physical port is treated as @vport 0.
1957 **/
James Smart87af33f2007-10-27 13:37:43 -04001958void
James Smart2e0fef82007-06-17 19:56:36 -05001959lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001960{
James Smart87af33f2007-10-27 13:37:43 -04001961 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001962 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04001963 int i = 0;
dea31012005-04-17 16:05:31 -05001964
James Smart87af33f2007-10-27 13:37:43 -04001965 if (phba->link_state > LPFC_LINK_DOWN)
1966 lpfc_port_link_failure(vport);
1967
1968 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001969 if (!NLP_CHK_NODE_ACT(ndlp)) {
1970 ndlp = lpfc_enable_node(vport, ndlp,
1971 NLP_STE_UNUSED_NODE);
1972 if (!ndlp)
1973 continue;
1974 spin_lock_irq(&phba->ndlp_lock);
1975 NLP_SET_FREE_REQ(ndlp);
1976 spin_unlock_irq(&phba->ndlp_lock);
1977 /* Trigger the release of the ndlp memory */
1978 lpfc_nlp_put(ndlp);
1979 continue;
1980 }
1981 spin_lock_irq(&phba->ndlp_lock);
1982 if (NLP_CHK_FREE_REQ(ndlp)) {
1983 /* The ndlp should not be in memory free mode already */
1984 spin_unlock_irq(&phba->ndlp_lock);
1985 continue;
1986 } else
1987 /* Indicate request for freeing ndlp memory */
1988 NLP_SET_FREE_REQ(ndlp);
1989 spin_unlock_irq(&phba->ndlp_lock);
1990
James Smart58da1ff2008-04-07 10:15:56 -04001991 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1992 ndlp->nlp_DID == Fabric_DID) {
1993 /* Just free up ndlp with Fabric_DID for vports */
1994 lpfc_nlp_put(ndlp);
1995 continue;
1996 }
1997
James Smart87af33f2007-10-27 13:37:43 -04001998 if (ndlp->nlp_type & NLP_FABRIC)
1999 lpfc_disc_state_machine(vport, ndlp, NULL,
2000 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002001
James Smart87af33f2007-10-27 13:37:43 -04002002 lpfc_disc_state_machine(vport, ndlp, NULL,
2003 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002004
James Smart87af33f2007-10-27 13:37:43 -04002005 }
2006
James Smarta8adb832007-10-27 13:37:53 -04002007 /* At this point, ALL ndlp's should be gone
2008 * because of the previous NLP_EVT_DEVICE_RM.
2009 * Lets wait for this to happen, if needed.
2010 */
James Smart87af33f2007-10-27 13:37:43 -04002011 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002012 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002013 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002014 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002015 list_for_each_entry_safe(ndlp, next_ndlp,
2016 &vport->fc_nodes, nlp_listp) {
2017 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2018 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002019 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002020 "usgmap:x%x refcnt:%d\n",
2021 ndlp->nlp_DID, (void *)ndlp,
2022 ndlp->nlp_usg_map,
2023 atomic_read(
2024 &ndlp->kref.refcount));
2025 }
James Smarta8adb832007-10-27 13:37:53 -04002026 break;
James Smart87af33f2007-10-27 13:37:43 -04002027 }
James Smarta8adb832007-10-27 13:37:53 -04002028
2029 /* Wait for any activity on ndlps to settle */
2030 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002031 }
dea31012005-04-17 16:05:31 -05002032}
2033
James Smarte59058c2008-08-24 21:49:00 -04002034/**
James Smart3621a712009-04-06 18:47:14 -04002035 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002036 * @vport: pointer to a virtual N_Port data structure.
2037 *
2038 * This routine stops all the timers associated with a @vport. This function
2039 * is invoked before disabling or deleting a @vport. Note that the physical
2040 * port is treated as @vport 0.
2041 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002042void
2043lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002044{
James Smart92d7f7b2007-06-17 19:56:38 -05002045 del_timer_sync(&vport->els_tmofunc);
2046 del_timer_sync(&vport->fc_fdmitmo);
2047 lpfc_can_disctmo(vport);
2048 return;
dea31012005-04-17 16:05:31 -05002049}
2050
James Smarte59058c2008-08-24 21:49:00 -04002051/**
James Smart3772a992009-05-22 14:50:54 -04002052 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002053 * @phba: pointer to lpfc hba data structure.
2054 *
2055 * This routine stops all the timers associated with a HBA. This function is
2056 * invoked before either putting a HBA offline or unloading the driver.
2057 **/
James Smart3772a992009-05-22 14:50:54 -04002058void
2059lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002060{
James Smart51ef4c22007-08-02 11:10:31 -04002061 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002062 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002063 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002064 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002065 del_timer_sync(&phba->hb_tmofunc);
2066 phba->hb_outstanding = 0;
2067
2068 switch (phba->pci_dev_grp) {
2069 case LPFC_PCI_DEV_LP:
2070 /* Stop any LightPulse device specific driver timers */
2071 del_timer_sync(&phba->fcp_poll_timer);
2072 break;
2073 case LPFC_PCI_DEV_OC:
2074 /* Stop any OneConnect device sepcific driver timers */
2075 break;
2076 default:
2077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2078 "0297 Invalid device group (x%x)\n",
2079 phba->pci_dev_grp);
2080 break;
2081 }
James Smart2e0fef82007-06-17 19:56:36 -05002082 return;
dea31012005-04-17 16:05:31 -05002083}
2084
James Smarte59058c2008-08-24 21:49:00 -04002085/**
James Smart3621a712009-04-06 18:47:14 -04002086 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002087 * @phba: pointer to lpfc hba data structure.
2088 *
2089 * This routine marks a HBA's management interface as blocked. Once the HBA's
2090 * management interface is marked as blocked, all the user space access to
2091 * the HBA, whether they are from sysfs interface or libdfc interface will
2092 * all be blocked. The HBA is set to block the management interface when the
2093 * driver prepares the HBA interface for online or offline.
2094 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002095static void
2096lpfc_block_mgmt_io(struct lpfc_hba * phba)
2097{
2098 unsigned long iflag;
2099
2100 spin_lock_irqsave(&phba->hbalock, iflag);
2101 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2102 spin_unlock_irqrestore(&phba->hbalock, iflag);
2103}
2104
James Smarte59058c2008-08-24 21:49:00 -04002105/**
James Smart3621a712009-04-06 18:47:14 -04002106 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002107 * @phba: pointer to lpfc hba data structure.
2108 *
2109 * This routine initializes the HBA and brings a HBA online. During this
2110 * process, the management interface is blocked to prevent user space access
2111 * to the HBA interfering with the driver initialization.
2112 *
2113 * Return codes
2114 * 0 - successful
2115 * 1 - failed
2116 **/
dea31012005-04-17 16:05:31 -05002117int
James Smart2e0fef82007-06-17 19:56:36 -05002118lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002119{
Julia Lawall372bd282008-12-16 16:15:08 +01002120 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002121 struct lpfc_vport **vports;
2122 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002123
dea31012005-04-17 16:05:31 -05002124 if (!phba)
2125 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002126 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002127
James Smart2e0fef82007-06-17 19:56:36 -05002128 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002129 return 0;
2130
James Smarted957682007-06-17 19:56:37 -05002131 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002132 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002133
James Smart46fa3112007-04-25 09:51:45 -04002134 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002135
James Smart46fa3112007-04-25 09:51:45 -04002136 if (!lpfc_sli_queue_setup(phba)) {
2137 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002138 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002139 }
2140
James Smartda0436e2009-05-22 14:51:39 -04002141 if (phba->sli_rev == LPFC_SLI_REV4) {
2142 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2143 lpfc_unblock_mgmt_io(phba);
2144 return 1;
2145 }
2146 } else {
2147 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2148 lpfc_unblock_mgmt_io(phba);
2149 return 1;
2150 }
James Smart46fa3112007-04-25 09:51:45 -04002151 }
dea31012005-04-17 16:05:31 -05002152
James Smart549e55c2007-08-02 11:09:51 -04002153 vports = lpfc_create_vport_work_array(phba);
2154 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002155 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002156 struct Scsi_Host *shost;
2157 shost = lpfc_shost_from_vport(vports[i]);
2158 spin_lock_irq(shost->host_lock);
2159 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2160 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2161 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002162 if (phba->sli_rev == LPFC_SLI_REV4)
2163 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002164 spin_unlock_irq(shost->host_lock);
2165 }
James Smart09372822008-01-11 01:52:54 -05002166 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002167
James Smart46fa3112007-04-25 09:51:45 -04002168 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002169 return 0;
2170}
2171
James Smarte59058c2008-08-24 21:49:00 -04002172/**
James Smart3621a712009-04-06 18:47:14 -04002173 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002174 * @phba: pointer to lpfc hba data structure.
2175 *
2176 * This routine marks a HBA's management interface as not blocked. Once the
2177 * HBA's management interface is marked as not blocked, all the user space
2178 * access to the HBA, whether they are from sysfs interface or libdfc
2179 * interface will be allowed. The HBA is set to block the management interface
2180 * when the driver prepares the HBA interface for online or offline and then
2181 * set to unblock the management interface afterwards.
2182 **/
James Smart46fa3112007-04-25 09:51:45 -04002183void
James Smart46fa3112007-04-25 09:51:45 -04002184lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2185{
2186 unsigned long iflag;
2187
James Smart2e0fef82007-06-17 19:56:36 -05002188 spin_lock_irqsave(&phba->hbalock, iflag);
2189 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2190 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002191}
2192
James Smarte59058c2008-08-24 21:49:00 -04002193/**
James Smart3621a712009-04-06 18:47:14 -04002194 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002195 * @phba: pointer to lpfc hba data structure.
2196 *
2197 * This routine is invoked to prepare a HBA to be brought offline. It performs
2198 * unregistration login to all the nodes on all vports and flushes the mailbox
2199 * queue to make it ready to be brought offline.
2200 **/
James Smart46fa3112007-04-25 09:51:45 -04002201void
2202lpfc_offline_prep(struct lpfc_hba * phba)
2203{
James Smart2e0fef82007-06-17 19:56:36 -05002204 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002205 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002206 struct lpfc_vport **vports;
2207 int i;
dea31012005-04-17 16:05:31 -05002208
James Smart2e0fef82007-06-17 19:56:36 -05002209 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002210 return;
dea31012005-04-17 16:05:31 -05002211
James Smart46fa3112007-04-25 09:51:45 -04002212 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002213
2214 lpfc_linkdown(phba);
2215
James Smart87af33f2007-10-27 13:37:43 -04002216 /* Issue an unreg_login to all nodes on all vports */
2217 vports = lpfc_create_vport_work_array(phba);
2218 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002219 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart87af33f2007-10-27 13:37:43 -04002220 struct Scsi_Host *shost;
2221
James Smarta8adb832007-10-27 13:37:53 -04002222 if (vports[i]->load_flag & FC_UNLOADING)
2223 continue;
James Smartda0436e2009-05-22 14:51:39 -04002224 vports[i]->vfi_state &= ~LPFC_VFI_REGISTERED;
James Smart87af33f2007-10-27 13:37:43 -04002225 shost = lpfc_shost_from_vport(vports[i]);
2226 list_for_each_entry_safe(ndlp, next_ndlp,
2227 &vports[i]->fc_nodes,
2228 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002229 if (!NLP_CHK_NODE_ACT(ndlp))
2230 continue;
James Smart87af33f2007-10-27 13:37:43 -04002231 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2232 continue;
2233 if (ndlp->nlp_type & NLP_FABRIC) {
2234 lpfc_disc_state_machine(vports[i], ndlp,
2235 NULL, NLP_EVT_DEVICE_RECOVERY);
2236 lpfc_disc_state_machine(vports[i], ndlp,
2237 NULL, NLP_EVT_DEVICE_RM);
2238 }
2239 spin_lock_irq(shost->host_lock);
2240 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2241 spin_unlock_irq(shost->host_lock);
2242 lpfc_unreg_rpi(vports[i], ndlp);
2243 }
2244 }
2245 }
James Smart09372822008-01-11 01:52:54 -05002246 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002247
James Smart04c68492009-05-22 14:52:52 -04002248 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002249}
2250
James Smarte59058c2008-08-24 21:49:00 -04002251/**
James Smart3621a712009-04-06 18:47:14 -04002252 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002253 * @phba: pointer to lpfc hba data structure.
2254 *
2255 * This routine actually brings a HBA offline. It stops all the timers
2256 * associated with the HBA, brings down the SLI layer, and eventually
2257 * marks the HBA as in offline state for the upper layer protocol.
2258 **/
James Smart46fa3112007-04-25 09:51:45 -04002259void
James Smart2e0fef82007-06-17 19:56:36 -05002260lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002261{
James Smart549e55c2007-08-02 11:09:51 -04002262 struct Scsi_Host *shost;
2263 struct lpfc_vport **vports;
2264 int i;
James Smart46fa3112007-04-25 09:51:45 -04002265
James Smart549e55c2007-08-02 11:09:51 -04002266 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002267 return;
James Smart688a8862006-07-06 15:49:56 -04002268
James Smartda0436e2009-05-22 14:51:39 -04002269 /* stop port and all timers associated with this hba */
2270 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002271 vports = lpfc_create_vport_work_array(phba);
2272 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002273 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002274 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002275 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002276 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002277 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002278 /* Bring down the SLI Layer and cleanup. The HBA is offline
2279 now. */
2280 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002281 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002282 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002283 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002284 vports = lpfc_create_vport_work_array(phba);
2285 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002286 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002287 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002288 spin_lock_irq(shost->host_lock);
2289 vports[i]->work_port_events = 0;
2290 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2291 spin_unlock_irq(shost->host_lock);
2292 }
James Smart09372822008-01-11 01:52:54 -05002293 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002294}
2295
James Smarte59058c2008-08-24 21:49:00 -04002296/**
James Smart3621a712009-04-06 18:47:14 -04002297 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002298 * @phba: pointer to lpfc hba data structure.
2299 *
2300 * This routine is to free all the SCSI buffers and IOCBs from the driver
2301 * list back to kernel. It is called from lpfc_pci_remove_one to free
2302 * the internal resources before the device is removed from the system.
2303 *
2304 * Return codes
2305 * 0 - successful (for now, it always returns 0)
2306 **/
dea31012005-04-17 16:05:31 -05002307static int
James Smart2e0fef82007-06-17 19:56:36 -05002308lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002309{
2310 struct lpfc_scsi_buf *sb, *sb_next;
2311 struct lpfc_iocbq *io, *io_next;
2312
James Smart2e0fef82007-06-17 19:56:36 -05002313 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002314 /* Release all the lpfc_scsi_bufs maintained by this host. */
2315 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2316 list_del(&sb->list);
2317 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002318 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002319 kfree(sb);
2320 phba->total_scsi_bufs--;
2321 }
2322
2323 /* Release all the lpfc_iocbq entries maintained by this host. */
2324 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2325 list_del(&io->list);
2326 kfree(io);
2327 phba->total_iocbq_bufs--;
2328 }
2329
James Smart2e0fef82007-06-17 19:56:36 -05002330 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002331
2332 return 0;
2333}
2334
James Smarte59058c2008-08-24 21:49:00 -04002335/**
James Smart3621a712009-04-06 18:47:14 -04002336 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002337 * @phba: pointer to lpfc hba data structure.
2338 * @instance: a unique integer ID to this FC port.
2339 * @dev: pointer to the device data structure.
2340 *
2341 * This routine creates a FC port for the upper layer protocol. The FC port
2342 * can be created on top of either a physical port or a virtual port provided
2343 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2344 * and associates the FC port created before adding the shost into the SCSI
2345 * layer.
2346 *
2347 * Return codes
2348 * @vport - pointer to the virtual N_Port data structure.
2349 * NULL - port create failed.
2350 **/
James Smart2e0fef82007-06-17 19:56:36 -05002351struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002352lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002353{
James Smart2e0fef82007-06-17 19:56:36 -05002354 struct lpfc_vport *vport;
2355 struct Scsi_Host *shost;
2356 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002357
James Smart3de2a652007-08-02 11:09:59 -04002358 if (dev != &phba->pcidev->dev)
2359 shost = scsi_host_alloc(&lpfc_vport_template,
2360 sizeof(struct lpfc_vport));
2361 else
2362 shost = scsi_host_alloc(&lpfc_template,
2363 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002364 if (!shost)
2365 goto out;
James Smart47a86172007-04-25 09:53:22 -04002366
James Smart2e0fef82007-06-17 19:56:36 -05002367 vport = (struct lpfc_vport *) shost->hostdata;
2368 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002369 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002370 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002371 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002372
James Smart3de2a652007-08-02 11:09:59 -04002373 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002374 shost->unique_id = instance;
2375 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002376 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002377 shost->this_id = -1;
2378 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002379 if (phba->sli_rev == LPFC_SLI_REV4) {
2380 shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2381 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2382 }
James Smart81301a92008-12-04 22:39:46 -05002383
James Smart47a86172007-04-25 09:53:22 -04002384 /*
James Smart2e0fef82007-06-17 19:56:36 -05002385 * Set initial can_queue value since 0 is no longer supported and
2386 * scsi_add_host will fail. This will be adjusted later based on the
2387 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002388 */
James Smart2e0fef82007-06-17 19:56:36 -05002389 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002390 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002391 shost->transportt = lpfc_vport_transport_template;
2392 vport->port_type = LPFC_NPIV_PORT;
2393 } else {
2394 shost->transportt = lpfc_transport_template;
2395 vport->port_type = LPFC_PHYSICAL_PORT;
2396 }
James Smart47a86172007-04-25 09:53:22 -04002397
James Smart2e0fef82007-06-17 19:56:36 -05002398 /* Initialize all internally managed lists. */
2399 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002400 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002401 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002402
James Smart2e0fef82007-06-17 19:56:36 -05002403 init_timer(&vport->fc_disctmo);
2404 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002405 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002406
James Smart2e0fef82007-06-17 19:56:36 -05002407 init_timer(&vport->fc_fdmitmo);
2408 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002409 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002410
James Smart2e0fef82007-06-17 19:56:36 -05002411 init_timer(&vport->els_tmofunc);
2412 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002413 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002414
James Bottomleyd139b9b2009-11-05 13:33:12 -06002415 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002416 if (error)
2417 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002418
James Smart549e55c2007-08-02 11:09:51 -04002419 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002420 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002421 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002422 return vport;
James Smart47a86172007-04-25 09:53:22 -04002423
James Smart2e0fef82007-06-17 19:56:36 -05002424out_put_shost:
2425 scsi_host_put(shost);
2426out:
2427 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002428}
2429
James Smarte59058c2008-08-24 21:49:00 -04002430/**
James Smart3621a712009-04-06 18:47:14 -04002431 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002432 * @vport: pointer to an lpfc virtual N_Port data structure.
2433 *
2434 * This routine destroys a FC port from the upper layer protocol. All the
2435 * resources associated with the port are released.
2436 **/
James Smart2e0fef82007-06-17 19:56:36 -05002437void
2438destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002439{
James Smart92d7f7b2007-06-17 19:56:38 -05002440 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2441 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002442
James Smart858c9f62007-06-17 19:56:39 -05002443 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002444 fc_remove_host(shost);
2445 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002446
James Smart92d7f7b2007-06-17 19:56:38 -05002447 spin_lock_irq(&phba->hbalock);
2448 list_del_init(&vport->listentry);
2449 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002450
James Smart92d7f7b2007-06-17 19:56:38 -05002451 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002452 return;
James Smart47a86172007-04-25 09:53:22 -04002453}
2454
James Smarte59058c2008-08-24 21:49:00 -04002455/**
James Smart3621a712009-04-06 18:47:14 -04002456 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002457 *
2458 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2459 * uses the kernel idr facility to perform the task.
2460 *
2461 * Return codes:
2462 * instance - a unique integer ID allocated as the new instance.
2463 * -1 - lpfc get instance failed.
2464 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002465int
2466lpfc_get_instance(void)
2467{
2468 int instance = 0;
2469
2470 /* Assign an unused number */
2471 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2472 return -1;
2473 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2474 return -1;
2475 return instance;
2476}
2477
James Smarte59058c2008-08-24 21:49:00 -04002478/**
James Smart3621a712009-04-06 18:47:14 -04002479 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002480 * @shost: pointer to SCSI host data structure.
2481 * @time: elapsed time of the scan in jiffies.
2482 *
2483 * This routine is called by the SCSI layer with a SCSI host to determine
2484 * whether the scan host is finished.
2485 *
2486 * Note: there is no scan_start function as adapter initialization will have
2487 * asynchronously kicked off the link initialization.
2488 *
2489 * Return codes
2490 * 0 - SCSI host scan is not over yet.
2491 * 1 - SCSI host scan is over.
2492 **/
James Smart47a86172007-04-25 09:53:22 -04002493int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2494{
James Smart2e0fef82007-06-17 19:56:36 -05002495 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2496 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002497 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002498
James Smart858c9f62007-06-17 19:56:39 -05002499 spin_lock_irq(shost->host_lock);
2500
James Smart51ef4c22007-08-02 11:10:31 -04002501 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002502 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002503 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002504 }
James Smart2e0fef82007-06-17 19:56:36 -05002505 if (time >= 30 * HZ) {
2506 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002507 "0461 Scanning longer than 30 "
2508 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002509 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002510 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002511 }
2512 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2513 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002514 "0465 Link down longer than 15 "
2515 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002516 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002517 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002518 }
2519
James Smart2e0fef82007-06-17 19:56:36 -05002520 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002521 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002522 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002523 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002524 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002525 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002526 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002527 goto finished;
2528
2529 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002530
2531finished:
James Smart858c9f62007-06-17 19:56:39 -05002532 spin_unlock_irq(shost->host_lock);
2533 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002534}
2535
James Smarte59058c2008-08-24 21:49:00 -04002536/**
James Smart3621a712009-04-06 18:47:14 -04002537 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002538 * @shost: pointer to SCSI host data structure.
2539 *
2540 * This routine initializes a given SCSI host attributes on a FC port. The
2541 * SCSI host can be either on top of a physical port or a virtual port.
2542 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002543void lpfc_host_attrib_init(struct Scsi_Host *shost)
2544{
2545 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2546 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002547 /*
James Smart2e0fef82007-06-17 19:56:36 -05002548 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002549 */
2550
James Smart2e0fef82007-06-17 19:56:36 -05002551 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2552 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002553 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2554
2555 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002556 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002557 fc_host_supported_fc4s(shost)[2] = 1;
2558 fc_host_supported_fc4s(shost)[7] = 1;
2559
James Smart92d7f7b2007-06-17 19:56:38 -05002560 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2561 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002562
2563 fc_host_supported_speeds(shost) = 0;
2564 if (phba->lmt & LMT_10Gb)
2565 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002566 if (phba->lmt & LMT_8Gb)
2567 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002568 if (phba->lmt & LMT_4Gb)
2569 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2570 if (phba->lmt & LMT_2Gb)
2571 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2572 if (phba->lmt & LMT_1Gb)
2573 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2574
2575 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002576 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2577 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002578
2579 /* This value is also unchanging */
2580 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002581 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002582 fc_host_active_fc4s(shost)[2] = 1;
2583 fc_host_active_fc4s(shost)[7] = 1;
2584
James Smart92d7f7b2007-06-17 19:56:38 -05002585 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002586 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002587 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002588 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002589}
dea31012005-04-17 16:05:31 -05002590
James Smarte59058c2008-08-24 21:49:00 -04002591/**
James Smartda0436e2009-05-22 14:51:39 -04002592 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002593 * @phba: pointer to lpfc hba data structure.
2594 *
James Smartda0436e2009-05-22 14:51:39 -04002595 * This routine is invoked to stop an SLI3 device port, it stops the device
2596 * from generating interrupts and stops the device driver's timers for the
2597 * device.
James Smarte59058c2008-08-24 21:49:00 -04002598 **/
James Smartdb2378e2008-02-08 18:49:51 -05002599static void
James Smartda0436e2009-05-22 14:51:39 -04002600lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002601{
James Smartda0436e2009-05-22 14:51:39 -04002602 /* Clear all interrupt enable conditions */
2603 writel(0, phba->HCregaddr);
2604 readl(phba->HCregaddr); /* flush */
2605 /* Clear all pending interrupts */
2606 writel(0xffffffff, phba->HAregaddr);
2607 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002608
James Smartda0436e2009-05-22 14:51:39 -04002609 /* Reset some HBA SLI setup states */
2610 lpfc_stop_hba_timers(phba);
2611 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002612}
2613
James Smarte59058c2008-08-24 21:49:00 -04002614/**
James Smartda0436e2009-05-22 14:51:39 -04002615 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002616 * @phba: pointer to lpfc hba data structure.
2617 *
James Smartda0436e2009-05-22 14:51:39 -04002618 * This routine is invoked to stop an SLI4 device port, it stops the device
2619 * from generating interrupts and stops the device driver's timers for the
2620 * device.
2621 **/
2622static void
2623lpfc_stop_port_s4(struct lpfc_hba *phba)
2624{
2625 /* Reset some HBA SLI4 setup states */
2626 lpfc_stop_hba_timers(phba);
2627 phba->pport->work_port_events = 0;
2628 phba->sli4_hba.intr_enable = 0;
2629 /* Hard clear it for now, shall have more graceful way to wait later */
2630 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2631}
2632
2633/**
2634 * lpfc_stop_port - Wrapper function for stopping hba port
2635 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002636 *
James Smartda0436e2009-05-22 14:51:39 -04002637 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2638 * the API jump table function pointer from the lpfc_hba struct.
2639 **/
2640void
2641lpfc_stop_port(struct lpfc_hba *phba)
2642{
2643 phba->lpfc_stop_port(phba);
2644}
2645
2646/**
2647 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2648 * @phba: pointer to lpfc hba data structure.
2649 *
2650 * This routine is invoked to remove the driver default fcf record from
2651 * the port. This routine currently acts on FCF Index 0.
2652 *
2653 **/
2654void
2655lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2656{
2657 int rc = 0;
2658 LPFC_MBOXQ_t *mboxq;
2659 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2660 uint32_t mbox_tmo, req_len;
2661 uint32_t shdr_status, shdr_add_status;
2662
2663 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2664 if (!mboxq) {
2665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2666 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2667 return;
2668 }
2669
2670 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2671 sizeof(struct lpfc_sli4_cfg_mhdr);
2672 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2673 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2674 req_len, LPFC_SLI4_MBX_EMBED);
2675 /*
2676 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2677 * supports multiple FCF indices.
2678 */
2679 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2680 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2681 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2682 phba->fcf.fcf_indx);
2683
2684 if (!phba->sli4_hba.intr_enable)
2685 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2686 else {
2687 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2688 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2689 }
2690 /* The IOCTL status is embedded in the mailbox subheader. */
2691 shdr_status = bf_get(lpfc_mbox_hdr_status,
2692 &del_fcf_record->header.cfg_shdr.response);
2693 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2694 &del_fcf_record->header.cfg_shdr.response);
2695 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2696 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2697 "2516 DEL FCF of default FCF Index failed "
2698 "mbx status x%x, status x%x add_status x%x\n",
2699 rc, shdr_status, shdr_add_status);
2700 }
2701 if (rc != MBX_TIMEOUT)
2702 mempool_free(mboxq, phba->mbox_mem_pool);
2703}
2704
2705/**
James Smart6669f9b2009-10-02 15:16:45 -04002706 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2707 * @phba: pointer to lpfc hba data structure.
2708 *
2709 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2710 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2711 * was successful and the firmware supports FCoE. Any other return indicates
2712 * a error. It is assumed that this function will be called before interrupts
2713 * are enabled.
2714 **/
2715static int
2716lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2717{
2718 int rc = 0;
2719 LPFC_MBOXQ_t *mboxq;
2720 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2721 uint32_t length;
2722 uint32_t shdr_status, shdr_add_status;
2723
2724 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2725 if (!mboxq) {
2726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2727 "2621 Failed to allocate mbox for "
2728 "query firmware config cmd\n");
2729 return -ENOMEM;
2730 }
2731 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2732 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2733 sizeof(struct lpfc_sli4_cfg_mhdr));
2734 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2735 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2736 length, LPFC_SLI4_MBX_EMBED);
2737 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2738 /* The IOCTL status is embedded in the mailbox subheader. */
2739 shdr_status = bf_get(lpfc_mbox_hdr_status,
2740 &query_fw_cfg->header.cfg_shdr.response);
2741 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2742 &query_fw_cfg->header.cfg_shdr.response);
2743 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2745 "2622 Query Firmware Config failed "
2746 "mbx status x%x, status x%x add_status x%x\n",
2747 rc, shdr_status, shdr_add_status);
2748 return -EINVAL;
2749 }
2750 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2751 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2752 "2623 FCoE Function not supported by firmware. "
2753 "Function mode = %08x\n",
2754 query_fw_cfg->function_mode);
2755 return -EINVAL;
2756 }
2757 if (rc != MBX_TIMEOUT)
2758 mempool_free(mboxq, phba->mbox_mem_pool);
2759 return 0;
2760}
2761
2762/**
James Smartda0436e2009-05-22 14:51:39 -04002763 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2764 * @phba: pointer to lpfc hba data structure.
2765 * @acqe_link: pointer to the async link completion queue entry.
2766 *
2767 * This routine is to parse the SLI4 link-attention link fault code and
2768 * translate it into the base driver's read link attention mailbox command
2769 * status.
2770 *
2771 * Return: Link-attention status in terms of base driver's coding.
2772 **/
2773static uint16_t
2774lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2775 struct lpfc_acqe_link *acqe_link)
2776{
2777 uint16_t latt_fault;
2778
2779 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2780 case LPFC_ASYNC_LINK_FAULT_NONE:
2781 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2782 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2783 latt_fault = 0;
2784 break;
2785 default:
2786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2787 "0398 Invalid link fault code: x%x\n",
2788 bf_get(lpfc_acqe_link_fault, acqe_link));
2789 latt_fault = MBXERR_ERROR;
2790 break;
2791 }
2792 return latt_fault;
2793}
2794
2795/**
2796 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2797 * @phba: pointer to lpfc hba data structure.
2798 * @acqe_link: pointer to the async link completion queue entry.
2799 *
2800 * This routine is to parse the SLI4 link attention type and translate it
2801 * into the base driver's link attention type coding.
2802 *
2803 * Return: Link attention type in terms of base driver's coding.
2804 **/
2805static uint8_t
2806lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2807 struct lpfc_acqe_link *acqe_link)
2808{
2809 uint8_t att_type;
2810
2811 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2812 case LPFC_ASYNC_LINK_STATUS_DOWN:
2813 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2814 att_type = AT_LINK_DOWN;
2815 break;
2816 case LPFC_ASYNC_LINK_STATUS_UP:
2817 /* Ignore physical link up events - wait for logical link up */
2818 att_type = AT_RESERVED;
2819 break;
2820 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2821 att_type = AT_LINK_UP;
2822 break;
2823 default:
2824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2825 "0399 Invalid link attention type: x%x\n",
2826 bf_get(lpfc_acqe_link_status, acqe_link));
2827 att_type = AT_RESERVED;
2828 break;
2829 }
2830 return att_type;
2831}
2832
2833/**
2834 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2835 * @phba: pointer to lpfc hba data structure.
2836 * @acqe_link: pointer to the async link completion queue entry.
2837 *
2838 * This routine is to parse the SLI4 link-attention link speed and translate
2839 * it into the base driver's link-attention link speed coding.
2840 *
2841 * Return: Link-attention link speed in terms of base driver's coding.
2842 **/
2843static uint8_t
2844lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2845 struct lpfc_acqe_link *acqe_link)
2846{
2847 uint8_t link_speed;
2848
2849 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2850 case LPFC_ASYNC_LINK_SPEED_ZERO:
2851 link_speed = LA_UNKNW_LINK;
2852 break;
2853 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2854 link_speed = LA_UNKNW_LINK;
2855 break;
2856 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2857 link_speed = LA_UNKNW_LINK;
2858 break;
2859 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2860 link_speed = LA_1GHZ_LINK;
2861 break;
2862 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2863 link_speed = LA_10GHZ_LINK;
2864 break;
2865 default:
2866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2867 "0483 Invalid link-attention link speed: x%x\n",
2868 bf_get(lpfc_acqe_link_speed, acqe_link));
2869 link_speed = LA_UNKNW_LINK;
2870 break;
2871 }
2872 return link_speed;
2873}
2874
2875/**
2876 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2877 * @phba: pointer to lpfc hba data structure.
2878 * @acqe_link: pointer to the async link completion queue entry.
2879 *
2880 * This routine is to handle the SLI4 asynchronous link event.
2881 **/
2882static void
2883lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
2884 struct lpfc_acqe_link *acqe_link)
2885{
2886 struct lpfc_dmabuf *mp;
2887 LPFC_MBOXQ_t *pmb;
2888 MAILBOX_t *mb;
2889 READ_LA_VAR *la;
2890 uint8_t att_type;
2891
2892 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
2893 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
2894 return;
James Smart32b97932009-07-19 10:01:21 -04002895 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04002896 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2897 if (!pmb) {
2898 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2899 "0395 The mboxq allocation failed\n");
2900 return;
2901 }
2902 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2903 if (!mp) {
2904 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2905 "0396 The lpfc_dmabuf allocation failed\n");
2906 goto out_free_pmb;
2907 }
2908 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2909 if (!mp->virt) {
2910 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2911 "0397 The mbuf allocation failed\n");
2912 goto out_free_dmabuf;
2913 }
2914
2915 /* Cleanup any outstanding ELS commands */
2916 lpfc_els_flush_all_cmd(phba);
2917
2918 /* Block ELS IOCBs until we have done process link event */
2919 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2920
2921 /* Update link event statistics */
2922 phba->sli.slistat.link_event++;
2923
2924 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
2925 lpfc_read_la(phba, pmb, mp);
2926 pmb->vport = phba->pport;
2927
2928 /* Parse and translate status field */
2929 mb = &pmb->u.mb;
2930 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
2931
2932 /* Parse and translate link attention fields */
2933 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2934 la->eventTag = acqe_link->event_tag;
2935 la->attType = att_type;
2936 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
2937
2938 /* Fake the the following irrelvant fields */
2939 la->topology = TOPOLOGY_PT_PT;
2940 la->granted_AL_PA = 0;
2941 la->il = 0;
2942 la->pb = 0;
2943 la->fa = 0;
2944 la->mm = 0;
2945
2946 /* Keep the link status for extra SLI4 state machine reference */
2947 phba->sli4_hba.link_state.speed =
2948 bf_get(lpfc_acqe_link_speed, acqe_link);
2949 phba->sli4_hba.link_state.duplex =
2950 bf_get(lpfc_acqe_link_duplex, acqe_link);
2951 phba->sli4_hba.link_state.status =
2952 bf_get(lpfc_acqe_link_status, acqe_link);
2953 phba->sli4_hba.link_state.physical =
2954 bf_get(lpfc_acqe_link_physical, acqe_link);
2955 phba->sli4_hba.link_state.fault =
2956 bf_get(lpfc_acqe_link_fault, acqe_link);
2957
2958 /* Invoke the lpfc_handle_latt mailbox command callback function */
2959 lpfc_mbx_cmpl_read_la(phba, pmb);
2960
2961 return;
2962
2963out_free_dmabuf:
2964 kfree(mp);
2965out_free_pmb:
2966 mempool_free(pmb, phba->mbox_mem_pool);
2967}
2968
2969/**
2970 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
2971 * @phba: pointer to lpfc hba data structure.
2972 * @acqe_link: pointer to the async fcoe completion queue entry.
2973 *
2974 * This routine is to handle the SLI4 asynchronous fcoe event.
2975 **/
2976static void
2977lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
2978 struct lpfc_acqe_fcoe *acqe_fcoe)
2979{
2980 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
2981 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04002982 struct lpfc_vport *vport;
2983 struct lpfc_nodelist *ndlp;
2984 struct Scsi_Host *shost;
James Smartda0436e2009-05-22 14:51:39 -04002985
James Smart4d9ab992009-10-02 15:16:39 -04002986 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04002987 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04002988 switch (event_type) {
2989 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
2990 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smarte4e74272009-07-19 10:01:38 -04002991 "2546 New FCF found index 0x%x tag 0x%x\n",
James Smart6669f9b2009-10-02 15:16:45 -04002992 acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04002993 acqe_fcoe->event_tag);
2994 /*
James Smart32b97932009-07-19 10:01:21 -04002995 * If the current FCF is in discovered state, or
2996 * FCF discovery is in progress do nothing.
James Smartda0436e2009-05-22 14:51:39 -04002997 */
2998 spin_lock_irq(&phba->hbalock);
James Smart32b97932009-07-19 10:01:21 -04002999 if ((phba->fcf.fcf_flag & FCF_DISCOVERED) ||
3000 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
James Smartda0436e2009-05-22 14:51:39 -04003001 spin_unlock_irq(&phba->hbalock);
3002 break;
3003 }
3004 spin_unlock_irq(&phba->hbalock);
3005
3006 /* Read the FCF table and re-discover SAN. */
James Smart6a9c52c2009-10-02 15:16:51 -04003007 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003008 if (rc)
3009 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003010 "2547 Read FCF record failed 0x%x\n",
3011 rc);
James Smartda0436e2009-05-22 14:51:39 -04003012 break;
3013
3014 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003016 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003017 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3018 acqe_fcoe->event_tag);
3019 break;
3020
3021 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3022 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003023 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003024 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003025 acqe_fcoe->event_tag);
3026 /* If the event is not for currently used fcf do nothing */
James Smart6669f9b2009-10-02 15:16:45 -04003027 if (phba->fcf.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003028 break;
3029 /*
3030 * Currently, driver support only one FCF - so treat this as
3031 * a link down.
3032 */
3033 lpfc_linkdown(phba);
3034 /* Unregister FCF if no devices connected to it */
3035 lpfc_unregister_unused_fcf(phba);
3036 break;
James Smart6669f9b2009-10-02 15:16:45 -04003037 case LPFC_FCOE_EVENT_TYPE_CVL:
3038 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3039 "2718 Clear Virtual Link Received for VPI 0x%x"
3040 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3041 vport = lpfc_find_vport_by_vpid(phba,
3042 acqe_fcoe->index /*- phba->vpi_base*/);
3043 if (!vport)
3044 break;
3045 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3046 if (!ndlp)
3047 break;
3048 shost = lpfc_shost_from_vport(vport);
3049 lpfc_linkdown_port(vport);
3050 if (vport->port_type != LPFC_NPIV_PORT) {
3051 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3052 spin_lock_irq(shost->host_lock);
3053 ndlp->nlp_flag |= NLP_DELAY_TMO;
3054 spin_unlock_irq(shost->host_lock);
3055 ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
3056 vport->port_state = LPFC_FLOGI;
3057 }
3058 break;
James Smartda0436e2009-05-22 14:51:39 -04003059 default:
3060 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3061 "0288 Unknown FCoE event type 0x%x event tag "
3062 "0x%x\n", event_type, acqe_fcoe->event_tag);
3063 break;
3064 }
3065}
3066
3067/**
3068 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3069 * @phba: pointer to lpfc hba data structure.
3070 * @acqe_link: pointer to the async dcbx completion queue entry.
3071 *
3072 * This routine is to handle the SLI4 asynchronous dcbx event.
3073 **/
3074static void
3075lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3076 struct lpfc_acqe_dcbx *acqe_dcbx)
3077{
James Smart4d9ab992009-10-02 15:16:39 -04003078 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003079 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3080 "0290 The SLI4 DCBX asynchronous event is not "
3081 "handled yet\n");
3082}
3083
3084/**
3085 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3086 * @phba: pointer to lpfc hba data structure.
3087 *
3088 * This routine is invoked by the worker thread to process all the pending
3089 * SLI4 asynchronous events.
3090 **/
3091void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3092{
3093 struct lpfc_cq_event *cq_event;
3094
3095 /* First, declare the async event has been handled */
3096 spin_lock_irq(&phba->hbalock);
3097 phba->hba_flag &= ~ASYNC_EVENT;
3098 spin_unlock_irq(&phba->hbalock);
3099 /* Now, handle all the async events */
3100 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3101 /* Get the first event from the head of the event queue */
3102 spin_lock_irq(&phba->hbalock);
3103 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3104 cq_event, struct lpfc_cq_event, list);
3105 spin_unlock_irq(&phba->hbalock);
3106 /* Process the asynchronous event */
3107 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3108 case LPFC_TRAILER_CODE_LINK:
3109 lpfc_sli4_async_link_evt(phba,
3110 &cq_event->cqe.acqe_link);
3111 break;
3112 case LPFC_TRAILER_CODE_FCOE:
3113 lpfc_sli4_async_fcoe_evt(phba,
3114 &cq_event->cqe.acqe_fcoe);
3115 break;
3116 case LPFC_TRAILER_CODE_DCBX:
3117 lpfc_sli4_async_dcbx_evt(phba,
3118 &cq_event->cqe.acqe_dcbx);
3119 break;
3120 default:
3121 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3122 "1804 Invalid asynchrous event code: "
3123 "x%x\n", bf_get(lpfc_trailer_code,
3124 &cq_event->cqe.mcqe_cmpl));
3125 break;
3126 }
3127 /* Free the completion event processed to the free pool */
3128 lpfc_sli4_cq_event_release(phba, cq_event);
3129 }
3130}
3131
3132/**
3133 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3134 * @phba: pointer to lpfc hba data structure.
3135 * @dev_grp: The HBA PCI-Device group number.
3136 *
3137 * This routine is invoked to set up the per HBA PCI-Device group function
3138 * API jump table entries.
3139 *
3140 * Return: 0 if success, otherwise -ENODEV
3141 **/
3142int
3143lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003144{
3145 int rc;
3146
James Smartda0436e2009-05-22 14:51:39 -04003147 /* Set up lpfc PCI-device group */
3148 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003149
James Smartda0436e2009-05-22 14:51:39 -04003150 /* The LPFC_PCI_DEV_OC uses SLI4 */
3151 if (dev_grp == LPFC_PCI_DEV_OC)
3152 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003153
James Smartda0436e2009-05-22 14:51:39 -04003154 /* Set up device INIT API function jump table */
3155 rc = lpfc_init_api_table_setup(phba, dev_grp);
3156 if (rc)
3157 return -ENODEV;
3158 /* Set up SCSI API function jump table */
3159 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3160 if (rc)
3161 return -ENODEV;
3162 /* Set up SLI API function jump table */
3163 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3164 if (rc)
3165 return -ENODEV;
3166 /* Set up MBOX API function jump table */
3167 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3168 if (rc)
3169 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003170
James Smartda0436e2009-05-22 14:51:39 -04003171 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003172}
3173
3174/**
James Smart3621a712009-04-06 18:47:14 -04003175 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003176 * @phba: pointer to lpfc hba data structure.
3177 * @intr_mode: active interrupt mode adopted.
3178 *
3179 * This routine it invoked to log the currently used active interrupt mode
3180 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003181 **/
3182static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003183{
3184 switch (intr_mode) {
3185 case 0:
3186 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3187 "0470 Enable INTx interrupt mode.\n");
3188 break;
3189 case 1:
3190 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3191 "0481 Enabled MSI interrupt mode.\n");
3192 break;
3193 case 2:
3194 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3195 "0480 Enabled MSI-X interrupt mode.\n");
3196 break;
3197 default:
3198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3199 "0482 Illegal interrupt mode.\n");
3200 break;
3201 }
3202 return;
3203}
3204
James Smart5b75da22008-12-04 22:39:35 -05003205/**
James Smart3772a992009-05-22 14:50:54 -04003206 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003207 * @phba: pointer to lpfc hba data structure.
3208 *
James Smart3772a992009-05-22 14:50:54 -04003209 * This routine is invoked to enable the PCI device that is common to all
3210 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003211 *
3212 * Return codes
James Smart3772a992009-05-22 14:50:54 -04003213 * 0 - sucessful
3214 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003215 **/
James Smart3772a992009-05-22 14:50:54 -04003216static int
3217lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003218{
James Smart3772a992009-05-22 14:50:54 -04003219 struct pci_dev *pdev;
3220 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003221
James Smart3772a992009-05-22 14:50:54 -04003222 /* Obtain PCI device reference */
3223 if (!phba->pcidev)
3224 goto out_error;
3225 else
3226 pdev = phba->pcidev;
3227 /* Select PCI BARs */
3228 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3229 /* Enable PCI device */
3230 if (pci_enable_device_mem(pdev))
3231 goto out_error;
3232 /* Request PCI resource for the device */
3233 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3234 goto out_disable_device;
3235 /* Set up device as PCI master and save state for EEH */
3236 pci_set_master(pdev);
3237 pci_try_set_mwi(pdev);
3238 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003239
James Smart3772a992009-05-22 14:50:54 -04003240 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003241
James Smart3772a992009-05-22 14:50:54 -04003242out_disable_device:
3243 pci_disable_device(pdev);
3244out_error:
3245 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003246}
3247
3248/**
James Smart3772a992009-05-22 14:50:54 -04003249 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003250 * @phba: pointer to lpfc hba data structure.
3251 *
James Smart3772a992009-05-22 14:50:54 -04003252 * This routine is invoked to disable the PCI device that is common to all
3253 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003254 **/
3255static void
James Smart3772a992009-05-22 14:50:54 -04003256lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003257{
James Smart3772a992009-05-22 14:50:54 -04003258 struct pci_dev *pdev;
3259 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003260
James Smart3772a992009-05-22 14:50:54 -04003261 /* Obtain PCI device reference */
3262 if (!phba->pcidev)
3263 return;
3264 else
3265 pdev = phba->pcidev;
3266 /* Select PCI BARs */
3267 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3268 /* Release PCI resource and disable PCI device */
3269 pci_release_selected_regions(pdev, bars);
3270 pci_disable_device(pdev);
3271 /* Null out PCI private reference to driver */
3272 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003273
3274 return;
3275}
3276
3277/**
James Smart3772a992009-05-22 14:50:54 -04003278 * lpfc_reset_hba - Reset a hba
3279 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003280 *
James Smart3772a992009-05-22 14:50:54 -04003281 * This routine is invoked to reset a hba device. It brings the HBA
3282 * offline, performs a board restart, and then brings the board back
3283 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3284 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003285 **/
James Smart3772a992009-05-22 14:50:54 -04003286void
3287lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003288{
James Smart3772a992009-05-22 14:50:54 -04003289 /* If resets are disabled then set error state and return. */
3290 if (!phba->cfg_enable_hba_reset) {
3291 phba->link_state = LPFC_HBA_ERROR;
3292 return;
3293 }
3294 lpfc_offline_prep(phba);
3295 lpfc_offline(phba);
3296 lpfc_sli_brdrestart(phba);
3297 lpfc_online(phba);
3298 lpfc_unblock_mgmt_io(phba);
3299}
dea31012005-04-17 16:05:31 -05003300
James Smart3772a992009-05-22 14:50:54 -04003301/**
3302 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3303 * @phba: pointer to lpfc hba data structure.
3304 *
3305 * This routine is invoked to set up the driver internal resources specific to
3306 * support the SLI-3 HBA device it attached to.
3307 *
3308 * Return codes
3309 * 0 - sucessful
3310 * other values - error
3311 **/
3312static int
3313lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3314{
3315 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003316
James Smart3772a992009-05-22 14:50:54 -04003317 /*
3318 * Initialize timers used by driver
3319 */
dea31012005-04-17 16:05:31 -05003320
James Smart3772a992009-05-22 14:50:54 -04003321 /* Heartbeat timer */
3322 init_timer(&phba->hb_tmofunc);
3323 phba->hb_tmofunc.function = lpfc_hb_timeout;
3324 phba->hb_tmofunc.data = (unsigned long)phba;
3325
3326 psli = &phba->sli;
3327 /* MBOX heartbeat timer */
3328 init_timer(&psli->mbox_tmo);
3329 psli->mbox_tmo.function = lpfc_mbox_timeout;
3330 psli->mbox_tmo.data = (unsigned long) phba;
3331 /* FCP polling mode timer */
3332 init_timer(&phba->fcp_poll_timer);
3333 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3334 phba->fcp_poll_timer.data = (unsigned long) phba;
3335 /* Fabric block timer */
3336 init_timer(&phba->fabric_block_timer);
3337 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3338 phba->fabric_block_timer.data = (unsigned long) phba;
3339 /* EA polling mode timer */
3340 init_timer(&phba->eratt_poll);
3341 phba->eratt_poll.function = lpfc_poll_eratt;
3342 phba->eratt_poll.data = (unsigned long) phba;
3343
3344 /* Host attention work mask setup */
3345 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3346 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3347
3348 /* Get all the module params for configuring this host */
3349 lpfc_get_cfgparam(phba);
3350 /*
3351 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3352 * used to create the sg_dma_buf_pool must be dynamically calculated.
3353 * 2 segments are added since the IOCB needs a command and response bde.
3354 */
3355 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3356 sizeof(struct fcp_rsp) +
3357 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3358
3359 if (phba->cfg_enable_bg) {
3360 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3361 phba->cfg_sg_dma_buf_size +=
3362 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3363 }
3364
3365 /* Also reinitialize the host templates with new values. */
3366 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3367 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3368
3369 phba->max_vpi = LPFC_MAX_VPI;
3370 /* This will be set to correct value after config_port mbox */
3371 phba->max_vports = 0;
3372
3373 /*
3374 * Initialize the SLI Layer to run with lpfc HBAs.
3375 */
3376 lpfc_sli_setup(phba);
3377 lpfc_sli_queue_setup(phba);
3378
3379 /* Allocate device driver memory */
3380 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3381 return -ENOMEM;
3382
3383 return 0;
3384}
3385
3386/**
3387 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3388 * @phba: pointer to lpfc hba data structure.
3389 *
3390 * This routine is invoked to unset the driver internal resources set up
3391 * specific for supporting the SLI-3 HBA device it attached to.
3392 **/
3393static void
3394lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3395{
3396 /* Free device driver memory allocated */
3397 lpfc_mem_free_all(phba);
3398
3399 return;
3400}
3401
3402/**
James Smartda0436e2009-05-22 14:51:39 -04003403 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3404 * @phba: pointer to lpfc hba data structure.
3405 *
3406 * This routine is invoked to set up the driver internal resources specific to
3407 * support the SLI-4 HBA device it attached to.
3408 *
3409 * Return codes
3410 * 0 - sucessful
3411 * other values - error
3412 **/
3413static int
3414lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3415{
3416 struct lpfc_sli *psli;
3417 int rc;
3418 int i, hbq_count;
3419
3420 /* Before proceed, wait for POST done and device ready */
3421 rc = lpfc_sli4_post_status_check(phba);
3422 if (rc)
3423 return -ENODEV;
3424
3425 /*
3426 * Initialize timers used by driver
3427 */
3428
3429 /* Heartbeat timer */
3430 init_timer(&phba->hb_tmofunc);
3431 phba->hb_tmofunc.function = lpfc_hb_timeout;
3432 phba->hb_tmofunc.data = (unsigned long)phba;
3433
3434 psli = &phba->sli;
3435 /* MBOX heartbeat timer */
3436 init_timer(&psli->mbox_tmo);
3437 psli->mbox_tmo.function = lpfc_mbox_timeout;
3438 psli->mbox_tmo.data = (unsigned long) phba;
3439 /* Fabric block timer */
3440 init_timer(&phba->fabric_block_timer);
3441 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3442 phba->fabric_block_timer.data = (unsigned long) phba;
3443 /* EA polling mode timer */
3444 init_timer(&phba->eratt_poll);
3445 phba->eratt_poll.function = lpfc_poll_eratt;
3446 phba->eratt_poll.data = (unsigned long) phba;
3447 /*
3448 * We need to do a READ_CONFIG mailbox command here before
3449 * calling lpfc_get_cfgparam. For VFs this will report the
3450 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3451 * All of the resources allocated
3452 * for this Port are tied to these values.
3453 */
3454 /* Get all the module params for configuring this host */
3455 lpfc_get_cfgparam(phba);
3456 phba->max_vpi = LPFC_MAX_VPI;
3457 /* This will be set to correct value after the read_config mbox */
3458 phba->max_vports = 0;
3459
3460 /* Program the default value of vlan_id and fc_map */
3461 phba->valid_vlan = 0;
3462 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3463 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3464 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3465
3466 /*
3467 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3468 * used to create the sg_dma_buf_pool must be dynamically calculated.
3469 * 2 segments are added since the IOCB needs a command and response bde.
3470 * To insure that the scsi sgl does not cross a 4k page boundary only
3471 * sgl sizes of 1k, 2k, 4k, and 8k are supported.
3472 * Table of sgl sizes and seg_cnt:
3473 * sgl size, sg_seg_cnt total seg
3474 * 1k 50 52
3475 * 2k 114 116
3476 * 4k 242 244
3477 * 8k 498 500
3478 * cmd(32) + rsp(160) + (52 * sizeof(sli4_sge)) = 1024
3479 * cmd(32) + rsp(160) + (116 * sizeof(sli4_sge)) = 2048
3480 * cmd(32) + rsp(160) + (244 * sizeof(sli4_sge)) = 4096
3481 * cmd(32) + rsp(160) + (500 * sizeof(sli4_sge)) = 8192
3482 */
3483 if (phba->cfg_sg_seg_cnt <= LPFC_DEFAULT_SG_SEG_CNT)
3484 phba->cfg_sg_seg_cnt = 50;
3485 else if (phba->cfg_sg_seg_cnt <= 114)
3486 phba->cfg_sg_seg_cnt = 114;
3487 else if (phba->cfg_sg_seg_cnt <= 242)
3488 phba->cfg_sg_seg_cnt = 242;
3489 else
3490 phba->cfg_sg_seg_cnt = 498;
3491
3492 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd)
3493 + sizeof(struct fcp_rsp);
3494 phba->cfg_sg_dma_buf_size +=
3495 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
3496
3497 /* Initialize buffer queue management fields */
3498 hbq_count = lpfc_sli_hbq_count();
3499 for (i = 0; i < hbq_count; ++i)
3500 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3501 INIT_LIST_HEAD(&phba->rb_pend_list);
3502 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3503 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3504
3505 /*
3506 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3507 */
3508 /* Initialize the Abort scsi buffer list used by driver */
3509 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3510 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3511 /* This abort list used by worker thread */
3512 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3513
3514 /*
3515 * Initialize dirver internal slow-path work queues
3516 */
3517
3518 /* Driver internel slow-path CQ Event pool */
3519 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3520 /* Response IOCB work queue list */
3521 INIT_LIST_HEAD(&phba->sli4_hba.sp_rspiocb_work_queue);
3522 /* Asynchronous event CQ Event work queue list */
3523 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3524 /* Fast-path XRI aborted CQ Event work queue list */
3525 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3526 /* Slow-path XRI aborted CQ Event work queue list */
3527 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3528 /* Receive queue CQ Event work queue list */
3529 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3530
3531 /* Initialize the driver internal SLI layer lists. */
3532 lpfc_sli_setup(phba);
3533 lpfc_sli_queue_setup(phba);
3534
3535 /* Allocate device driver memory */
3536 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3537 if (rc)
3538 return -ENOMEM;
3539
3540 /* Create the bootstrap mailbox command */
3541 rc = lpfc_create_bootstrap_mbox(phba);
3542 if (unlikely(rc))
3543 goto out_free_mem;
3544
3545 /* Set up the host's endian order with the device. */
3546 rc = lpfc_setup_endian_order(phba);
3547 if (unlikely(rc))
3548 goto out_free_bsmbx;
3549
James Smart6669f9b2009-10-02 15:16:45 -04003550 rc = lpfc_sli4_fw_cfg_check(phba);
3551 if (unlikely(rc))
3552 goto out_free_bsmbx;
3553
James Smartda0436e2009-05-22 14:51:39 -04003554 /* Set up the hba's configuration parameters. */
3555 rc = lpfc_sli4_read_config(phba);
3556 if (unlikely(rc))
3557 goto out_free_bsmbx;
3558
3559 /* Perform a function reset */
3560 rc = lpfc_pci_function_reset(phba);
3561 if (unlikely(rc))
3562 goto out_free_bsmbx;
3563
3564 /* Create all the SLI4 queues */
3565 rc = lpfc_sli4_queue_create(phba);
3566 if (rc)
3567 goto out_free_bsmbx;
3568
3569 /* Create driver internal CQE event pool */
3570 rc = lpfc_sli4_cq_event_pool_create(phba);
3571 if (rc)
3572 goto out_destroy_queue;
3573
3574 /* Initialize and populate the iocb list per host */
3575 rc = lpfc_init_sgl_list(phba);
3576 if (rc) {
3577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3578 "1400 Failed to initialize sgl list.\n");
3579 goto out_destroy_cq_event_pool;
3580 }
3581 rc = lpfc_init_active_sgl_array(phba);
3582 if (rc) {
3583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3584 "1430 Failed to initialize sgl list.\n");
3585 goto out_free_sgl_list;
3586 }
3587
3588 rc = lpfc_sli4_init_rpi_hdrs(phba);
3589 if (rc) {
3590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3591 "1432 Failed to initialize rpi headers.\n");
3592 goto out_free_active_sgl;
3593 }
3594
3595 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3596 phba->cfg_fcp_eq_count), GFP_KERNEL);
3597 if (!phba->sli4_hba.fcp_eq_hdl) {
3598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3599 "2572 Failed allocate memory for fast-path "
3600 "per-EQ handle array\n");
3601 goto out_remove_rpi_hdrs;
3602 }
3603
3604 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3605 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3606 if (!phba->sli4_hba.msix_entries) {
3607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3608 "2573 Failed allocate memory for msi-x "
3609 "interrupt vector entries\n");
3610 goto out_free_fcp_eq_hdl;
3611 }
3612
3613 return rc;
3614
3615out_free_fcp_eq_hdl:
3616 kfree(phba->sli4_hba.fcp_eq_hdl);
3617out_remove_rpi_hdrs:
3618 lpfc_sli4_remove_rpi_hdrs(phba);
3619out_free_active_sgl:
3620 lpfc_free_active_sgl(phba);
3621out_free_sgl_list:
3622 lpfc_free_sgl_list(phba);
3623out_destroy_cq_event_pool:
3624 lpfc_sli4_cq_event_pool_destroy(phba);
3625out_destroy_queue:
3626 lpfc_sli4_queue_destroy(phba);
3627out_free_bsmbx:
3628 lpfc_destroy_bootstrap_mbox(phba);
3629out_free_mem:
3630 lpfc_mem_free(phba);
3631 return rc;
3632}
3633
3634/**
3635 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3636 * @phba: pointer to lpfc hba data structure.
3637 *
3638 * This routine is invoked to unset the driver internal resources set up
3639 * specific for supporting the SLI-4 HBA device it attached to.
3640 **/
3641static void
3642lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3643{
3644 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3645
3646 /* unregister default FCFI from the HBA */
3647 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3648
3649 /* Free the default FCR table */
3650 lpfc_sli_remove_dflt_fcf(phba);
3651
3652 /* Free memory allocated for msi-x interrupt vector entries */
3653 kfree(phba->sli4_hba.msix_entries);
3654
3655 /* Free memory allocated for fast-path work queue handles */
3656 kfree(phba->sli4_hba.fcp_eq_hdl);
3657
3658 /* Free the allocated rpi headers. */
3659 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04003660 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04003661
3662 /* Free the ELS sgl list */
3663 lpfc_free_active_sgl(phba);
3664 lpfc_free_sgl_list(phba);
3665
3666 /* Free the SCSI sgl management array */
3667 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3668
3669 /* Free the SLI4 queues */
3670 lpfc_sli4_queue_destroy(phba);
3671
3672 /* Free the completion queue EQ event pool */
3673 lpfc_sli4_cq_event_release_all(phba);
3674 lpfc_sli4_cq_event_pool_destroy(phba);
3675
3676 /* Reset SLI4 HBA FCoE function */
3677 lpfc_pci_function_reset(phba);
3678
3679 /* Free the bsmbx region. */
3680 lpfc_destroy_bootstrap_mbox(phba);
3681
3682 /* Free the SLI Layer memory with SLI4 HBAs */
3683 lpfc_mem_free_all(phba);
3684
3685 /* Free the current connect table */
3686 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04003687 &phba->fcf_conn_rec_list, list) {
3688 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04003689 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04003690 }
James Smartda0436e2009-05-22 14:51:39 -04003691
3692 return;
3693}
3694
3695/**
James Smart3772a992009-05-22 14:50:54 -04003696 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3697 * @phba: The hba struct for which this call is being executed.
3698 * @dev_grp: The HBA PCI-Device group number.
3699 *
3700 * This routine sets up the device INIT interface API function jump table
3701 * in @phba struct.
3702 *
3703 * Returns: 0 - success, -ENODEV - failure.
3704 **/
3705int
3706lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3707{
3708 switch (dev_grp) {
3709 case LPFC_PCI_DEV_LP:
3710 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3711 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3712 phba->lpfc_stop_port = lpfc_stop_port_s3;
3713 break;
James Smartda0436e2009-05-22 14:51:39 -04003714 case LPFC_PCI_DEV_OC:
3715 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3716 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3717 phba->lpfc_stop_port = lpfc_stop_port_s4;
3718 break;
James Smart3772a992009-05-22 14:50:54 -04003719 default:
3720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3721 "1431 Invalid HBA PCI-device group: 0x%x\n",
3722 dev_grp);
3723 return -ENODEV;
3724 break;
3725 }
3726 return 0;
3727}
3728
3729/**
3730 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3731 * @phba: pointer to lpfc hba data structure.
3732 *
3733 * This routine is invoked to set up the driver internal resources before the
3734 * device specific resource setup to support the HBA device it attached to.
3735 *
3736 * Return codes
3737 * 0 - sucessful
3738 * other values - error
3739 **/
3740static int
3741lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3742{
3743 /*
3744 * Driver resources common to all SLI revisions
3745 */
James Smartea2151b2008-09-07 11:52:10 -04003746 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003747 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003748
James Smarte47c9092008-02-08 18:49:26 -05003749 /* Initialize ndlp management spinlock */
3750 spin_lock_init(&phba->ndlp_lock);
3751
James Smart2e0fef82007-06-17 19:56:36 -05003752 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04003753 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04003754 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05003755
James Smart3772a992009-05-22 14:50:54 -04003756 /* Initialize the wait queue head for the kernel thread */
3757 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05003758
James Smart3772a992009-05-22 14:50:54 -04003759 /* Initialize the scsi buffer list used by driver for scsi IO */
3760 spin_lock_init(&phba->scsi_buf_list_lock);
3761 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05003762
James Smart3772a992009-05-22 14:50:54 -04003763 /* Initialize the fabric iocb list */
3764 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05003765
James Smart3772a992009-05-22 14:50:54 -04003766 /* Initialize list to save ELS buffers */
3767 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05003768
James Smart3772a992009-05-22 14:50:54 -04003769 /* Initialize FCF connection rec list */
3770 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05003771
James Smart3772a992009-05-22 14:50:54 -04003772 return 0;
3773}
dea31012005-04-17 16:05:31 -05003774
James Smart3772a992009-05-22 14:50:54 -04003775/**
3776 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
3777 * @phba: pointer to lpfc hba data structure.
3778 *
3779 * This routine is invoked to set up the driver internal resources after the
3780 * device specific resource setup to support the HBA device it attached to.
3781 *
3782 * Return codes
3783 * 0 - sucessful
3784 * other values - error
3785 **/
3786static int
3787lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
3788{
3789 int error;
3790
3791 /* Startup the kernel thread for this host adapter. */
3792 phba->worker_thread = kthread_run(lpfc_do_work, phba,
3793 "lpfc_worker_%d", phba->brd_no);
3794 if (IS_ERR(phba->worker_thread)) {
3795 error = PTR_ERR(phba->worker_thread);
3796 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003797 }
3798
James Smart3772a992009-05-22 14:50:54 -04003799 return 0;
3800}
3801
3802/**
3803 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
3804 * @phba: pointer to lpfc hba data structure.
3805 *
3806 * This routine is invoked to unset the driver internal resources set up after
3807 * the device specific resource setup for supporting the HBA device it
3808 * attached to.
3809 **/
3810static void
3811lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
3812{
3813 /* Stop kernel worker thread */
3814 kthread_stop(phba->worker_thread);
3815}
3816
3817/**
3818 * lpfc_free_iocb_list - Free iocb list.
3819 * @phba: pointer to lpfc hba data structure.
3820 *
3821 * This routine is invoked to free the driver's IOCB list and memory.
3822 **/
3823static void
3824lpfc_free_iocb_list(struct lpfc_hba *phba)
3825{
3826 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
3827
3828 spin_lock_irq(&phba->hbalock);
3829 list_for_each_entry_safe(iocbq_entry, iocbq_next,
3830 &phba->lpfc_iocb_list, list) {
3831 list_del(&iocbq_entry->list);
3832 kfree(iocbq_entry);
3833 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003834 }
James Smart3772a992009-05-22 14:50:54 -04003835 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003836
James Smart3772a992009-05-22 14:50:54 -04003837 return;
3838}
dea31012005-04-17 16:05:31 -05003839
James Smart3772a992009-05-22 14:50:54 -04003840/**
3841 * lpfc_init_iocb_list - Allocate and initialize iocb list.
3842 * @phba: pointer to lpfc hba data structure.
3843 *
3844 * This routine is invoked to allocate and initizlize the driver's IOCB
3845 * list and set up the IOCB tag array accordingly.
3846 *
3847 * Return codes
3848 * 0 - sucessful
3849 * other values - error
3850 **/
3851static int
3852lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
3853{
3854 struct lpfc_iocbq *iocbq_entry = NULL;
3855 uint16_t iotag;
3856 int i;
dea31012005-04-17 16:05:31 -05003857
3858 /* Initialize and populate the iocb list per host. */
3859 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04003860 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003861 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003862 if (iocbq_entry == NULL) {
3863 printk(KERN_ERR "%s: only allocated %d iocbs of "
3864 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003865 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05003866 goto out_free_iocbq;
3867 }
3868
James Bottomley604a3e32005-10-29 10:28:33 -05003869 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
3870 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04003871 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05003872 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04003873 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05003874 goto out_free_iocbq;
3875 }
James Smart3772a992009-05-22 14:50:54 -04003876 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05003877
3878 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003879 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
3880 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05003881 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003882 }
3883
James Smart3772a992009-05-22 14:50:54 -04003884 return 0;
3885
3886out_free_iocbq:
3887 lpfc_free_iocb_list(phba);
3888
3889 return -ENOMEM;
3890}
3891
3892/**
James Smartda0436e2009-05-22 14:51:39 -04003893 * lpfc_free_sgl_list - Free sgl list.
3894 * @phba: pointer to lpfc hba data structure.
3895 *
3896 * This routine is invoked to free the driver's sgl list and memory.
3897 **/
3898static void
3899lpfc_free_sgl_list(struct lpfc_hba *phba)
3900{
3901 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
3902 LIST_HEAD(sglq_list);
3903 int rc = 0;
3904
3905 spin_lock_irq(&phba->hbalock);
3906 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
3907 spin_unlock_irq(&phba->hbalock);
3908
3909 list_for_each_entry_safe(sglq_entry, sglq_next,
3910 &sglq_list, list) {
3911 list_del(&sglq_entry->list);
3912 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
3913 kfree(sglq_entry);
3914 phba->sli4_hba.total_sglq_bufs--;
3915 }
3916 rc = lpfc_sli4_remove_all_sgl_pages(phba);
3917 if (rc) {
3918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04003919 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003920 }
3921 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3922}
3923
3924/**
3925 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
3926 * @phba: pointer to lpfc hba data structure.
3927 *
3928 * This routine is invoked to allocate the driver's active sgl memory.
3929 * This array will hold the sglq_entry's for active IOs.
3930 **/
3931static int
3932lpfc_init_active_sgl_array(struct lpfc_hba *phba)
3933{
3934 int size;
3935 size = sizeof(struct lpfc_sglq *);
3936 size *= phba->sli4_hba.max_cfg_param.max_xri;
3937
3938 phba->sli4_hba.lpfc_sglq_active_list =
3939 kzalloc(size, GFP_KERNEL);
3940 if (!phba->sli4_hba.lpfc_sglq_active_list)
3941 return -ENOMEM;
3942 return 0;
3943}
3944
3945/**
3946 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3947 * @phba: pointer to lpfc hba data structure.
3948 *
3949 * This routine is invoked to walk through the array of active sglq entries
3950 * and free all of the resources.
3951 * This is just a place holder for now.
3952 **/
3953static void
3954lpfc_free_active_sgl(struct lpfc_hba *phba)
3955{
3956 kfree(phba->sli4_hba.lpfc_sglq_active_list);
3957}
3958
3959/**
3960 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3961 * @phba: pointer to lpfc hba data structure.
3962 *
3963 * This routine is invoked to allocate and initizlize the driver's sgl
3964 * list and set up the sgl xritag tag array accordingly.
3965 *
3966 * Return codes
3967 * 0 - sucessful
3968 * other values - error
3969 **/
3970static int
3971lpfc_init_sgl_list(struct lpfc_hba *phba)
3972{
3973 struct lpfc_sglq *sglq_entry = NULL;
3974 int i;
3975 int els_xri_cnt;
3976
3977 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3978 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3979 "2400 lpfc_init_sgl_list els %d.\n",
3980 els_xri_cnt);
3981 /* Initialize and populate the sglq list per host/VF. */
3982 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
3983 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
3984
3985 /* Sanity check on XRI management */
3986 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
3987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3988 "2562 No room left for SCSI XRI allocation: "
3989 "max_xri=%d, els_xri=%d\n",
3990 phba->sli4_hba.max_cfg_param.max_xri,
3991 els_xri_cnt);
3992 return -ENOMEM;
3993 }
3994
3995 /* Allocate memory for the ELS XRI management array */
3996 phba->sli4_hba.lpfc_els_sgl_array =
3997 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
3998 GFP_KERNEL);
3999
4000 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4001 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4002 "2401 Failed to allocate memory for ELS "
4003 "XRI management array of size %d.\n",
4004 els_xri_cnt);
4005 return -ENOMEM;
4006 }
4007
4008 /* Keep the SCSI XRI into the XRI management array */
4009 phba->sli4_hba.scsi_xri_max =
4010 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4011 phba->sli4_hba.scsi_xri_cnt = 0;
4012
4013 phba->sli4_hba.lpfc_scsi_psb_array =
4014 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4015 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4016
4017 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4019 "2563 Failed to allocate memory for SCSI "
4020 "XRI management array of size %d.\n",
4021 phba->sli4_hba.scsi_xri_max);
4022 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4023 return -ENOMEM;
4024 }
4025
4026 for (i = 0; i < els_xri_cnt; i++) {
4027 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4028 if (sglq_entry == NULL) {
4029 printk(KERN_ERR "%s: only allocated %d sgls of "
4030 "expected %d count. Unloading driver.\n",
4031 __func__, i, els_xri_cnt);
4032 goto out_free_mem;
4033 }
4034
4035 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4036 if (sglq_entry->sli4_xritag == NO_XRI) {
4037 kfree(sglq_entry);
4038 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4039 "Unloading driver.\n", __func__);
4040 goto out_free_mem;
4041 }
4042 sglq_entry->buff_type = GEN_BUFF_TYPE;
4043 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4044 if (sglq_entry->virt == NULL) {
4045 kfree(sglq_entry);
4046 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4047 "Unloading driver.\n", __func__);
4048 goto out_free_mem;
4049 }
4050 sglq_entry->sgl = sglq_entry->virt;
4051 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4052
4053 /* The list order is used by later block SGL registraton */
4054 spin_lock_irq(&phba->hbalock);
4055 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4056 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4057 phba->sli4_hba.total_sglq_bufs++;
4058 spin_unlock_irq(&phba->hbalock);
4059 }
4060 return 0;
4061
4062out_free_mem:
4063 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4064 lpfc_free_sgl_list(phba);
4065 return -ENOMEM;
4066}
4067
4068/**
4069 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4070 * @phba: pointer to lpfc hba data structure.
4071 *
4072 * This routine is invoked to post rpi header templates to the
4073 * HBA consistent with the SLI-4 interface spec. This routine
4074 * posts a PAGE_SIZE memory region to the port to hold up to
4075 * PAGE_SIZE modulo 64 rpi context headers.
4076 * No locks are held here because this is an initialization routine
4077 * called only from probe or lpfc_online when interrupts are not
4078 * enabled and the driver is reinitializing the device.
4079 *
4080 * Return codes
4081 * 0 - sucessful
4082 * ENOMEM - No availble memory
4083 * EIO - The mailbox failed to complete successfully.
4084 **/
4085int
4086lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4087{
4088 int rc = 0;
4089 int longs;
4090 uint16_t rpi_count;
4091 struct lpfc_rpi_hdr *rpi_hdr;
4092
4093 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4094
4095 /*
4096 * Provision an rpi bitmask range for discovery. The total count
4097 * is the difference between max and base + 1.
4098 */
4099 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4100 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4101
4102 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4103 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4104 GFP_KERNEL);
4105 if (!phba->sli4_hba.rpi_bmask)
4106 return -ENOMEM;
4107
4108 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4109 if (!rpi_hdr) {
4110 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4111 "0391 Error during rpi post operation\n");
4112 lpfc_sli4_remove_rpis(phba);
4113 rc = -ENODEV;
4114 }
4115
4116 return rc;
4117}
4118
4119/**
4120 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4121 * @phba: pointer to lpfc hba data structure.
4122 *
4123 * This routine is invoked to allocate a single 4KB memory region to
4124 * support rpis and stores them in the phba. This single region
4125 * provides support for up to 64 rpis. The region is used globally
4126 * by the device.
4127 *
4128 * Returns:
4129 * A valid rpi hdr on success.
4130 * A NULL pointer on any failure.
4131 **/
4132struct lpfc_rpi_hdr *
4133lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4134{
4135 uint16_t rpi_limit, curr_rpi_range;
4136 struct lpfc_dmabuf *dmabuf;
4137 struct lpfc_rpi_hdr *rpi_hdr;
4138
4139 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4140 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4141
4142 spin_lock_irq(&phba->hbalock);
4143 curr_rpi_range = phba->sli4_hba.next_rpi;
4144 spin_unlock_irq(&phba->hbalock);
4145
4146 /*
4147 * The port has a limited number of rpis. The increment here
4148 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4149 * and to allow the full max_rpi range per port.
4150 */
4151 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4152 return NULL;
4153
4154 /*
4155 * First allocate the protocol header region for the port. The
4156 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4157 */
4158 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4159 if (!dmabuf)
4160 return NULL;
4161
4162 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4163 LPFC_HDR_TEMPLATE_SIZE,
4164 &dmabuf->phys,
4165 GFP_KERNEL);
4166 if (!dmabuf->virt) {
4167 rpi_hdr = NULL;
4168 goto err_free_dmabuf;
4169 }
4170
4171 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4172 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4173 rpi_hdr = NULL;
4174 goto err_free_coherent;
4175 }
4176
4177 /* Save the rpi header data for cleanup later. */
4178 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4179 if (!rpi_hdr)
4180 goto err_free_coherent;
4181
4182 rpi_hdr->dmabuf = dmabuf;
4183 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4184 rpi_hdr->page_count = 1;
4185 spin_lock_irq(&phba->hbalock);
4186 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4187 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4188
4189 /*
4190 * The next_rpi stores the next module-64 rpi value to post
4191 * in any subsequent rpi memory region postings.
4192 */
4193 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4194 spin_unlock_irq(&phba->hbalock);
4195 return rpi_hdr;
4196
4197 err_free_coherent:
4198 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4199 dmabuf->virt, dmabuf->phys);
4200 err_free_dmabuf:
4201 kfree(dmabuf);
4202 return NULL;
4203}
4204
4205/**
4206 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4207 * @phba: pointer to lpfc hba data structure.
4208 *
4209 * This routine is invoked to remove all memory resources allocated
4210 * to support rpis. This routine presumes the caller has released all
4211 * rpis consumed by fabric or port logins and is prepared to have
4212 * the header pages removed.
4213 **/
4214void
4215lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4216{
4217 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4218
4219 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4220 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4221 list_del(&rpi_hdr->list);
4222 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4223 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4224 kfree(rpi_hdr->dmabuf);
4225 kfree(rpi_hdr);
4226 }
4227
4228 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4229 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4230}
4231
4232/**
James Smart3772a992009-05-22 14:50:54 -04004233 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4234 * @pdev: pointer to pci device data structure.
4235 *
4236 * This routine is invoked to allocate the driver hba data structure for an
4237 * HBA device. If the allocation is successful, the phba reference to the
4238 * PCI device data structure is set.
4239 *
4240 * Return codes
4241 * pointer to @phba - sucessful
4242 * NULL - error
4243 **/
4244static struct lpfc_hba *
4245lpfc_hba_alloc(struct pci_dev *pdev)
4246{
4247 struct lpfc_hba *phba;
4248
4249 /* Allocate memory for HBA structure */
4250 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4251 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004252 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004253 return NULL;
4254 }
4255
4256 /* Set reference to PCI device in HBA structure */
4257 phba->pcidev = pdev;
4258
4259 /* Assign an unused board number */
4260 phba->brd_no = lpfc_get_instance();
4261 if (phba->brd_no < 0) {
4262 kfree(phba);
4263 return NULL;
4264 }
4265
James Smartf1c3b0f2009-07-19 10:01:32 -04004266 mutex_init(&phba->ct_event_mutex);
4267 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4268
James Smart3772a992009-05-22 14:50:54 -04004269 return phba;
4270}
4271
4272/**
4273 * lpfc_hba_free - Free driver hba data structure with a device.
4274 * @phba: pointer to lpfc hba data structure.
4275 *
4276 * This routine is invoked to free the driver hba data structure with an
4277 * HBA device.
4278 **/
4279static void
4280lpfc_hba_free(struct lpfc_hba *phba)
4281{
4282 /* Release the driver assigned board number */
4283 idr_remove(&lpfc_hba_index, phba->brd_no);
4284
4285 kfree(phba);
4286 return;
4287}
4288
4289/**
4290 * lpfc_create_shost - Create hba physical port with associated scsi host.
4291 * @phba: pointer to lpfc hba data structure.
4292 *
4293 * This routine is invoked to create HBA physical port and associate a SCSI
4294 * host with it.
4295 *
4296 * Return codes
4297 * 0 - sucessful
4298 * other values - error
4299 **/
4300static int
4301lpfc_create_shost(struct lpfc_hba *phba)
4302{
4303 struct lpfc_vport *vport;
4304 struct Scsi_Host *shost;
4305
4306 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004307 phba->fc_edtov = FF_DEF_EDTOV;
4308 phba->fc_ratov = FF_DEF_RATOV;
4309 phba->fc_altov = FF_DEF_ALTOV;
4310 phba->fc_arbtov = FF_DEF_ARBTOV;
4311
James Smart3de2a652007-08-02 11:09:59 -04004312 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004313 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004314 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004315
4316 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004317 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004318 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004319 /* Put reference to SCSI host to driver's device private data */
4320 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004321
James Smart3772a992009-05-22 14:50:54 -04004322 return 0;
4323}
dea31012005-04-17 16:05:31 -05004324
James Smart3772a992009-05-22 14:50:54 -04004325/**
4326 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4327 * @phba: pointer to lpfc hba data structure.
4328 *
4329 * This routine is invoked to destroy HBA physical port and the associated
4330 * SCSI host.
4331 **/
4332static void
4333lpfc_destroy_shost(struct lpfc_hba *phba)
4334{
4335 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004336
James Smart3772a992009-05-22 14:50:54 -04004337 /* Destroy physical port that associated with the SCSI host */
4338 destroy_port(vport);
4339
4340 return;
4341}
4342
4343/**
4344 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4345 * @phba: pointer to lpfc hba data structure.
4346 * @shost: the shost to be used to detect Block guard settings.
4347 *
4348 * This routine sets up the local Block guard protocol settings for @shost.
4349 * This routine also allocates memory for debugging bg buffers.
4350 **/
4351static void
4352lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4353{
4354 int pagecnt = 10;
4355 if (lpfc_prot_mask && lpfc_prot_guard) {
4356 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4357 "1478 Registering BlockGuard with the "
4358 "SCSI layer\n");
4359 scsi_host_set_prot(shost, lpfc_prot_mask);
4360 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004361 }
James Smart81301a92008-12-04 22:39:46 -05004362 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004363 while (pagecnt) {
4364 spin_lock_init(&_dump_buf_lock);
4365 _dump_buf_data =
4366 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4367 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004368 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4369 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004370 "_dump_buf_data at 0x%p\n",
4371 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004372 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004373 memset(_dump_buf_data, 0,
4374 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004375 break;
James Smart3772a992009-05-22 14:50:54 -04004376 } else
James Smart81301a92008-12-04 22:39:46 -05004377 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004378 }
James Smart81301a92008-12-04 22:39:46 -05004379 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004380 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4381 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004382 "memory for hexdump\n");
4383 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004384 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4385 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004386 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004387 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004388 while (pagecnt) {
4389 _dump_buf_dif =
4390 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4391 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004392 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4393 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004394 "_dump_buf_dif at 0x%p\n",
4395 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004396 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004397 memset(_dump_buf_dif, 0,
4398 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004399 break;
James Smart3772a992009-05-22 14:50:54 -04004400 } else
James Smart81301a92008-12-04 22:39:46 -05004401 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004402 }
James Smart81301a92008-12-04 22:39:46 -05004403 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004404 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4405 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004406 "memory for hexdump\n");
4407 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004408 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4409 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004410 _dump_buf_dif);
4411}
4412
4413/**
4414 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4415 * @phba: pointer to lpfc hba data structure.
4416 *
4417 * This routine is invoked to perform all the necessary post initialization
4418 * setup for the device.
4419 **/
4420static void
4421lpfc_post_init_setup(struct lpfc_hba *phba)
4422{
4423 struct Scsi_Host *shost;
4424 struct lpfc_adapter_event_header adapter_event;
4425
4426 /* Get the default values for Model Name and Description */
4427 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4428
4429 /*
4430 * hba setup may have changed the hba_queue_depth so we need to
4431 * adjust the value of can_queue.
4432 */
4433 shost = pci_get_drvdata(phba->pcidev);
4434 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4435 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4436 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004437
4438 lpfc_host_attrib_init(shost);
4439
James Smart2e0fef82007-06-17 19:56:36 -05004440 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4441 spin_lock_irq(shost->host_lock);
4442 lpfc_poll_start_timer(phba);
4443 spin_unlock_irq(shost->host_lock);
4444 }
James Smart8f6d98d2006-08-01 07:34:00 -04004445
James Smart93996272008-08-24 21:50:30 -04004446 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4447 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004448 /* Send board arrival event to upper layer */
4449 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4450 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4451 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004452 sizeof(adapter_event),
4453 (char *) &adapter_event,
4454 LPFC_NL_VENDOR_ID);
4455 return;
4456}
4457
4458/**
4459 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4460 * @phba: pointer to lpfc hba data structure.
4461 *
4462 * This routine is invoked to set up the PCI device memory space for device
4463 * with SLI-3 interface spec.
4464 *
4465 * Return codes
4466 * 0 - sucessful
4467 * other values - error
4468 **/
4469static int
4470lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4471{
4472 struct pci_dev *pdev;
4473 unsigned long bar0map_len, bar2map_len;
4474 int i, hbq_count;
4475 void *ptr;
4476 int error = -ENODEV;
4477
4478 /* Obtain PCI device reference */
4479 if (!phba->pcidev)
4480 return error;
4481 else
4482 pdev = phba->pcidev;
4483
4484 /* Set the device DMA mask size */
4485 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
4486 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
4487 return error;
4488
4489 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4490 * required by each mapping.
4491 */
4492 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4493 bar0map_len = pci_resource_len(pdev, 0);
4494
4495 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4496 bar2map_len = pci_resource_len(pdev, 2);
4497
4498 /* Map HBA SLIM to a kernel virtual address. */
4499 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4500 if (!phba->slim_memmap_p) {
4501 dev_printk(KERN_ERR, &pdev->dev,
4502 "ioremap failed for SLIM memory.\n");
4503 goto out;
4504 }
4505
4506 /* Map HBA Control Registers to a kernel virtual address. */
4507 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4508 if (!phba->ctrl_regs_memmap_p) {
4509 dev_printk(KERN_ERR, &pdev->dev,
4510 "ioremap failed for HBA control registers.\n");
4511 goto out_iounmap_slim;
4512 }
4513
4514 /* Allocate memory for SLI-2 structures */
4515 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4516 SLI2_SLIM_SIZE,
4517 &phba->slim2p.phys,
4518 GFP_KERNEL);
4519 if (!phba->slim2p.virt)
4520 goto out_iounmap;
4521
4522 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4523 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4524 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4525 phba->IOCBs = (phba->slim2p.virt +
4526 offsetof(struct lpfc_sli2_slim, IOCBs));
4527
4528 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4529 lpfc_sli_hbq_size(),
4530 &phba->hbqslimp.phys,
4531 GFP_KERNEL);
4532 if (!phba->hbqslimp.virt)
4533 goto out_free_slim;
4534
4535 hbq_count = lpfc_sli_hbq_count();
4536 ptr = phba->hbqslimp.virt;
4537 for (i = 0; i < hbq_count; ++i) {
4538 phba->hbqs[i].hbq_virt = ptr;
4539 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4540 ptr += (lpfc_hbq_defs[i]->entry_count *
4541 sizeof(struct lpfc_hbq_entry));
4542 }
4543 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4544 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4545
4546 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4547
4548 INIT_LIST_HEAD(&phba->rb_pend_list);
4549
4550 phba->MBslimaddr = phba->slim_memmap_p;
4551 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4552 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4553 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4554 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004555
dea31012005-04-17 16:05:31 -05004556 return 0;
4557
dea31012005-04-17 16:05:31 -05004558out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004559 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4560 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004561out_iounmap:
4562 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004563out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004564 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004565out:
4566 return error;
4567}
4568
James Smarte59058c2008-08-24 21:49:00 -04004569/**
James Smart3772a992009-05-22 14:50:54 -04004570 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4571 * @phba: pointer to lpfc hba data structure.
4572 *
4573 * This routine is invoked to unset the PCI device memory space for device
4574 * with SLI-3 interface spec.
4575 **/
4576static void
4577lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4578{
4579 struct pci_dev *pdev;
4580
4581 /* Obtain PCI device reference */
4582 if (!phba->pcidev)
4583 return;
4584 else
4585 pdev = phba->pcidev;
4586
4587 /* Free coherent DMA memory allocated */
4588 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4589 phba->hbqslimp.virt, phba->hbqslimp.phys);
4590 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4591 phba->slim2p.virt, phba->slim2p.phys);
4592
4593 /* I/O memory unmap */
4594 iounmap(phba->ctrl_regs_memmap_p);
4595 iounmap(phba->slim_memmap_p);
4596
4597 return;
4598}
4599
4600/**
James Smartda0436e2009-05-22 14:51:39 -04004601 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4602 * @phba: pointer to lpfc hba data structure.
4603 *
4604 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4605 * done and check status.
4606 *
4607 * Return 0 if successful, otherwise -ENODEV.
4608 **/
4609int
4610lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4611{
4612 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg, scratchpad;
4613 uint32_t onlnreg0, onlnreg1;
4614 int i, port_error = -ENODEV;
4615
4616 if (!phba->sli4_hba.STAregaddr)
4617 return -ENODEV;
4618
James Smartda0436e2009-05-22 14:51:39 -04004619 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4620 for (i = 0; i < 3000; i++) {
4621 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4622 /* Encounter fatal POST error, break out */
4623 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4624 port_error = -ENODEV;
4625 break;
4626 }
4627 if (LPFC_POST_STAGE_ARMFW_READY ==
4628 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4629 port_error = 0;
4630 break;
4631 }
4632 msleep(10);
4633 }
4634
4635 if (port_error)
4636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4637 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4638 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4639 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4640 bf_get(lpfc_hst_state_perr, &sta_reg),
4641 bf_get(lpfc_hst_state_sfi, &sta_reg),
4642 bf_get(lpfc_hst_state_nip, &sta_reg),
4643 bf_get(lpfc_hst_state_ipc, &sta_reg),
4644 bf_get(lpfc_hst_state_xrom, &sta_reg),
4645 bf_get(lpfc_hst_state_dl, &sta_reg),
4646 bf_get(lpfc_hst_state_port_status, &sta_reg));
4647
4648 /* Log device information */
4649 scratchpad.word0 = readl(phba->sli4_hba.SCRATCHPADregaddr);
4650 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4651 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4652 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4653 bf_get(lpfc_scratchpad_chiptype, &scratchpad),
4654 bf_get(lpfc_scratchpad_slirev, &scratchpad),
4655 bf_get(lpfc_scratchpad_featurelevel1, &scratchpad),
4656 bf_get(lpfc_scratchpad_featurelevel2, &scratchpad));
4657
James Smart8fa38512009-07-19 10:01:03 -04004658 /* With uncoverable error, log the error message and return error */
4659 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
4660 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
4661 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
4662 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4663 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4664 if (uerrlo_reg.word0 || uerrhi_reg.word0) {
4665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4666 "1422 HBA Unrecoverable error: "
4667 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4668 "online0_reg=0x%x, online1_reg=0x%x\n",
4669 uerrlo_reg.word0, uerrhi_reg.word0,
4670 onlnreg0, onlnreg1);
4671 }
4672 return -ENODEV;
4673 }
4674
James Smartda0436e2009-05-22 14:51:39 -04004675 return port_error;
4676}
4677
4678/**
4679 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4680 * @phba: pointer to lpfc hba data structure.
4681 *
4682 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4683 * memory map.
4684 **/
4685static void
4686lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4687{
4688 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4689 LPFC_UERR_STATUS_LO;
4690 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4691 LPFC_UERR_STATUS_HI;
4692 phba->sli4_hba.ONLINE0regaddr = phba->sli4_hba.conf_regs_memmap_p +
4693 LPFC_ONLINE0;
4694 phba->sli4_hba.ONLINE1regaddr = phba->sli4_hba.conf_regs_memmap_p +
4695 LPFC_ONLINE1;
4696 phba->sli4_hba.SCRATCHPADregaddr = phba->sli4_hba.conf_regs_memmap_p +
4697 LPFC_SCRATCHPAD;
4698}
4699
4700/**
4701 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4702 * @phba: pointer to lpfc hba data structure.
4703 *
4704 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4705 * memory map.
4706 **/
4707static void
4708lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4709{
4710
4711 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4712 LPFC_HST_STATE;
4713 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4714 LPFC_HST_ISR0;
4715 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4716 LPFC_HST_IMR0;
4717 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4718 LPFC_HST_ISCR0;
4719 return;
4720}
4721
4722/**
4723 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
4724 * @phba: pointer to lpfc hba data structure.
4725 * @vf: virtual function number
4726 *
4727 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4728 * based on the given viftual function number, @vf.
4729 *
4730 * Return 0 if successful, otherwise -ENODEV.
4731 **/
4732static int
4733lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
4734{
4735 if (vf > LPFC_VIR_FUNC_MAX)
4736 return -ENODEV;
4737
4738 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4739 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4740 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4741 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4742 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4743 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
4744 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4745 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
4746 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4747 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
4748 return 0;
4749}
4750
4751/**
4752 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
4753 * @phba: pointer to lpfc hba data structure.
4754 *
4755 * This routine is invoked to create the bootstrap mailbox
4756 * region consistent with the SLI-4 interface spec. This
4757 * routine allocates all memory necessary to communicate
4758 * mailbox commands to the port and sets up all alignment
4759 * needs. No locks are expected to be held when calling
4760 * this routine.
4761 *
4762 * Return codes
4763 * 0 - sucessful
4764 * ENOMEM - could not allocated memory.
4765 **/
4766static int
4767lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
4768{
4769 uint32_t bmbx_size;
4770 struct lpfc_dmabuf *dmabuf;
4771 struct dma_address *dma_address;
4772 uint32_t pa_addr;
4773 uint64_t phys_addr;
4774
4775 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4776 if (!dmabuf)
4777 return -ENOMEM;
4778
4779 /*
4780 * The bootstrap mailbox region is comprised of 2 parts
4781 * plus an alignment restriction of 16 bytes.
4782 */
4783 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
4784 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4785 bmbx_size,
4786 &dmabuf->phys,
4787 GFP_KERNEL);
4788 if (!dmabuf->virt) {
4789 kfree(dmabuf);
4790 return -ENOMEM;
4791 }
4792 memset(dmabuf->virt, 0, bmbx_size);
4793
4794 /*
4795 * Initialize the bootstrap mailbox pointers now so that the register
4796 * operations are simple later. The mailbox dma address is required
4797 * to be 16-byte aligned. Also align the virtual memory as each
4798 * maibox is copied into the bmbx mailbox region before issuing the
4799 * command to the port.
4800 */
4801 phba->sli4_hba.bmbx.dmabuf = dmabuf;
4802 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
4803
4804 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
4805 LPFC_ALIGN_16_BYTE);
4806 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
4807 LPFC_ALIGN_16_BYTE);
4808
4809 /*
4810 * Set the high and low physical addresses now. The SLI4 alignment
4811 * requirement is 16 bytes and the mailbox is posted to the port
4812 * as two 30-bit addresses. The other data is a bit marking whether
4813 * the 30-bit address is the high or low address.
4814 * Upcast bmbx aphys to 64bits so shift instruction compiles
4815 * clean on 32 bit machines.
4816 */
4817 dma_address = &phba->sli4_hba.bmbx.dma_address;
4818 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
4819 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
4820 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
4821 LPFC_BMBX_BIT1_ADDR_HI);
4822
4823 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
4824 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
4825 LPFC_BMBX_BIT1_ADDR_LO);
4826 return 0;
4827}
4828
4829/**
4830 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
4831 * @phba: pointer to lpfc hba data structure.
4832 *
4833 * This routine is invoked to teardown the bootstrap mailbox
4834 * region and release all host resources. This routine requires
4835 * the caller to ensure all mailbox commands recovered, no
4836 * additional mailbox comands are sent, and interrupts are disabled
4837 * before calling this routine.
4838 *
4839 **/
4840static void
4841lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
4842{
4843 dma_free_coherent(&phba->pcidev->dev,
4844 phba->sli4_hba.bmbx.bmbx_size,
4845 phba->sli4_hba.bmbx.dmabuf->virt,
4846 phba->sli4_hba.bmbx.dmabuf->phys);
4847
4848 kfree(phba->sli4_hba.bmbx.dmabuf);
4849 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
4850}
4851
4852/**
4853 * lpfc_sli4_read_config - Get the config parameters.
4854 * @phba: pointer to lpfc hba data structure.
4855 *
4856 * This routine is invoked to read the configuration parameters from the HBA.
4857 * The configuration parameters are used to set the base and maximum values
4858 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
4859 * allocation for the port.
4860 *
4861 * Return codes
4862 * 0 - sucessful
4863 * ENOMEM - No availble memory
4864 * EIO - The mailbox failed to complete successfully.
4865 **/
4866static int
4867lpfc_sli4_read_config(struct lpfc_hba *phba)
4868{
4869 LPFC_MBOXQ_t *pmb;
4870 struct lpfc_mbx_read_config *rd_config;
4871 uint32_t rc = 0;
4872
4873 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4874 if (!pmb) {
4875 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4876 "2011 Unable to allocate memory for issuing "
4877 "SLI_CONFIG_SPECIAL mailbox command\n");
4878 return -ENOMEM;
4879 }
4880
4881 lpfc_read_config(phba, pmb);
4882
4883 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4884 if (rc != MBX_SUCCESS) {
4885 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4886 "2012 Mailbox failed , mbxCmd x%x "
4887 "READ_CONFIG, mbxStatus x%x\n",
4888 bf_get(lpfc_mqe_command, &pmb->u.mqe),
4889 bf_get(lpfc_mqe_status, &pmb->u.mqe));
4890 rc = -EIO;
4891 } else {
4892 rd_config = &pmb->u.mqe.un.rd_config;
4893 phba->sli4_hba.max_cfg_param.max_xri =
4894 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
4895 phba->sli4_hba.max_cfg_param.xri_base =
4896 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
4897 phba->sli4_hba.max_cfg_param.max_vpi =
4898 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
4899 phba->sli4_hba.max_cfg_param.vpi_base =
4900 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
4901 phba->sli4_hba.max_cfg_param.max_rpi =
4902 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
4903 phba->sli4_hba.max_cfg_param.rpi_base =
4904 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
4905 phba->sli4_hba.max_cfg_param.max_vfi =
4906 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
4907 phba->sli4_hba.max_cfg_param.vfi_base =
4908 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
4909 phba->sli4_hba.max_cfg_param.max_fcfi =
4910 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
4911 phba->sli4_hba.max_cfg_param.fcfi_base =
4912 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
4913 phba->sli4_hba.max_cfg_param.max_eq =
4914 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
4915 phba->sli4_hba.max_cfg_param.max_rq =
4916 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
4917 phba->sli4_hba.max_cfg_param.max_wq =
4918 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
4919 phba->sli4_hba.max_cfg_param.max_cq =
4920 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
4921 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
4922 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
4923 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
4924 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
4925 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4926 phba->max_vpi = phba->sli4_hba.max_cfg_param.max_vpi;
4927 phba->max_vports = phba->max_vpi;
4928 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4929 "2003 cfg params XRI(B:%d M:%d), "
4930 "VPI(B:%d M:%d) "
4931 "VFI(B:%d M:%d) "
4932 "RPI(B:%d M:%d) "
4933 "FCFI(B:%d M:%d)\n",
4934 phba->sli4_hba.max_cfg_param.xri_base,
4935 phba->sli4_hba.max_cfg_param.max_xri,
4936 phba->sli4_hba.max_cfg_param.vpi_base,
4937 phba->sli4_hba.max_cfg_param.max_vpi,
4938 phba->sli4_hba.max_cfg_param.vfi_base,
4939 phba->sli4_hba.max_cfg_param.max_vfi,
4940 phba->sli4_hba.max_cfg_param.rpi_base,
4941 phba->sli4_hba.max_cfg_param.max_rpi,
4942 phba->sli4_hba.max_cfg_param.fcfi_base,
4943 phba->sli4_hba.max_cfg_param.max_fcfi);
4944 }
4945 mempool_free(pmb, phba->mbox_mem_pool);
4946
4947 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
4948 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
4949 phba->cfg_hba_queue_depth =
4950 phba->sli4_hba.max_cfg_param.max_xri;
4951 return rc;
4952}
4953
4954/**
4955 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
4956 * @phba: pointer to lpfc hba data structure.
4957 *
4958 * This routine is invoked to setup the host-side endian order to the
4959 * HBA consistent with the SLI-4 interface spec.
4960 *
4961 * Return codes
4962 * 0 - sucessful
4963 * ENOMEM - No availble memory
4964 * EIO - The mailbox failed to complete successfully.
4965 **/
4966static int
4967lpfc_setup_endian_order(struct lpfc_hba *phba)
4968{
4969 LPFC_MBOXQ_t *mboxq;
4970 uint32_t rc = 0;
4971 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
4972 HOST_ENDIAN_HIGH_WORD1};
4973
4974 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4975 if (!mboxq) {
4976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4977 "0492 Unable to allocate memory for issuing "
4978 "SLI_CONFIG_SPECIAL mailbox command\n");
4979 return -ENOMEM;
4980 }
4981
4982 /*
4983 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
4984 * words to contain special data values and no other data.
4985 */
4986 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
4987 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
4988 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4989 if (rc != MBX_SUCCESS) {
4990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4991 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
4992 "status x%x\n",
4993 rc);
4994 rc = -EIO;
4995 }
4996
4997 mempool_free(mboxq, phba->mbox_mem_pool);
4998 return rc;
4999}
5000
5001/**
5002 * lpfc_sli4_queue_create - Create all the SLI4 queues
5003 * @phba: pointer to lpfc hba data structure.
5004 *
5005 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5006 * operation. For each SLI4 queue type, the parameters such as queue entry
5007 * count (queue depth) shall be taken from the module parameter. For now,
5008 * we just use some constant number as place holder.
5009 *
5010 * Return codes
5011 * 0 - sucessful
5012 * ENOMEM - No availble memory
5013 * EIO - The mailbox failed to complete successfully.
5014 **/
5015static int
5016lpfc_sli4_queue_create(struct lpfc_hba *phba)
5017{
5018 struct lpfc_queue *qdesc;
5019 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5020 int cfg_fcp_wq_count;
5021 int cfg_fcp_eq_count;
5022
5023 /*
5024 * Sanity check for confiugred queue parameters against the run-time
5025 * device parameters
5026 */
5027
5028 /* Sanity check on FCP fast-path WQ parameters */
5029 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5030 if (cfg_fcp_wq_count >
5031 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5032 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5033 LPFC_SP_WQN_DEF;
5034 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5036 "2581 Not enough WQs (%d) from "
5037 "the pci function for supporting "
5038 "FCP WQs (%d)\n",
5039 phba->sli4_hba.max_cfg_param.max_wq,
5040 phba->cfg_fcp_wq_count);
5041 goto out_error;
5042 }
5043 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5044 "2582 Not enough WQs (%d) from the pci "
5045 "function for supporting the requested "
5046 "FCP WQs (%d), the actual FCP WQs can "
5047 "be supported: %d\n",
5048 phba->sli4_hba.max_cfg_param.max_wq,
5049 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5050 }
5051 /* The actual number of FCP work queues adopted */
5052 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5053
5054 /* Sanity check on FCP fast-path EQ parameters */
5055 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5056 if (cfg_fcp_eq_count >
5057 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5058 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5059 LPFC_SP_EQN_DEF;
5060 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5062 "2574 Not enough EQs (%d) from the "
5063 "pci function for supporting FCP "
5064 "EQs (%d)\n",
5065 phba->sli4_hba.max_cfg_param.max_eq,
5066 phba->cfg_fcp_eq_count);
5067 goto out_error;
5068 }
5069 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5070 "2575 Not enough EQs (%d) from the pci "
5071 "function for supporting the requested "
5072 "FCP EQs (%d), the actual FCP EQs can "
5073 "be supported: %d\n",
5074 phba->sli4_hba.max_cfg_param.max_eq,
5075 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5076 }
5077 /* It does not make sense to have more EQs than WQs */
5078 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5079 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005080 "2593 The FCP EQ count(%d) cannot be greater "
5081 "than the FCP WQ count(%d), limiting the "
5082 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005083 phba->cfg_fcp_wq_count,
5084 phba->cfg_fcp_wq_count);
5085 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5086 }
5087 /* The actual number of FCP event queues adopted */
5088 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5089 /* The overall number of event queues used */
5090 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5091
5092 /*
5093 * Create Event Queues (EQs)
5094 */
5095
5096 /* Get EQ depth from module parameter, fake the default for now */
5097 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5098 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5099
5100 /* Create slow path event queue */
5101 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5102 phba->sli4_hba.eq_ecount);
5103 if (!qdesc) {
5104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5105 "0496 Failed allocate slow-path EQ\n");
5106 goto out_error;
5107 }
5108 phba->sli4_hba.sp_eq = qdesc;
5109
5110 /* Create fast-path FCP Event Queue(s) */
5111 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5112 phba->cfg_fcp_eq_count), GFP_KERNEL);
5113 if (!phba->sli4_hba.fp_eq) {
5114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5115 "2576 Failed allocate memory for fast-path "
5116 "EQ record array\n");
5117 goto out_free_sp_eq;
5118 }
5119 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5120 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5121 phba->sli4_hba.eq_ecount);
5122 if (!qdesc) {
5123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5124 "0497 Failed allocate fast-path EQ\n");
5125 goto out_free_fp_eq;
5126 }
5127 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5128 }
5129
5130 /*
5131 * Create Complete Queues (CQs)
5132 */
5133
5134 /* Get CQ depth from module parameter, fake the default for now */
5135 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5136 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5137
5138 /* Create slow-path Mailbox Command Complete Queue */
5139 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5140 phba->sli4_hba.cq_ecount);
5141 if (!qdesc) {
5142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5143 "0500 Failed allocate slow-path mailbox CQ\n");
5144 goto out_free_fp_eq;
5145 }
5146 phba->sli4_hba.mbx_cq = qdesc;
5147
5148 /* Create slow-path ELS Complete Queue */
5149 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5150 phba->sli4_hba.cq_ecount);
5151 if (!qdesc) {
5152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5153 "0501 Failed allocate slow-path ELS CQ\n");
5154 goto out_free_mbx_cq;
5155 }
5156 phba->sli4_hba.els_cq = qdesc;
5157
James Smartda0436e2009-05-22 14:51:39 -04005158
5159 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5160 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5161 phba->cfg_fcp_eq_count), GFP_KERNEL);
5162 if (!phba->sli4_hba.fcp_cq) {
5163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5164 "2577 Failed allocate memory for fast-path "
5165 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005166 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005167 }
5168 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5169 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5170 phba->sli4_hba.cq_ecount);
5171 if (!qdesc) {
5172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5173 "0499 Failed allocate fast-path FCP "
5174 "CQ (%d)\n", fcp_cqidx);
5175 goto out_free_fcp_cq;
5176 }
5177 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5178 }
5179
5180 /* Create Mailbox Command Queue */
5181 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5182 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5183
5184 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5185 phba->sli4_hba.mq_ecount);
5186 if (!qdesc) {
5187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5188 "0505 Failed allocate slow-path MQ\n");
5189 goto out_free_fcp_cq;
5190 }
5191 phba->sli4_hba.mbx_wq = qdesc;
5192
5193 /*
5194 * Create all the Work Queues (WQs)
5195 */
5196 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5197 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5198
5199 /* Create slow-path ELS Work Queue */
5200 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5201 phba->sli4_hba.wq_ecount);
5202 if (!qdesc) {
5203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5204 "0504 Failed allocate slow-path ELS WQ\n");
5205 goto out_free_mbx_wq;
5206 }
5207 phba->sli4_hba.els_wq = qdesc;
5208
5209 /* Create fast-path FCP Work Queue(s) */
5210 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5211 phba->cfg_fcp_wq_count), GFP_KERNEL);
5212 if (!phba->sli4_hba.fcp_wq) {
5213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5214 "2578 Failed allocate memory for fast-path "
5215 "WQ record array\n");
5216 goto out_free_els_wq;
5217 }
5218 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5219 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5220 phba->sli4_hba.wq_ecount);
5221 if (!qdesc) {
5222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5223 "0503 Failed allocate fast-path FCP "
5224 "WQ (%d)\n", fcp_wqidx);
5225 goto out_free_fcp_wq;
5226 }
5227 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5228 }
5229
5230 /*
5231 * Create Receive Queue (RQ)
5232 */
5233 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5234 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5235
5236 /* Create Receive Queue for header */
5237 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5238 phba->sli4_hba.rq_ecount);
5239 if (!qdesc) {
5240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5241 "0506 Failed allocate receive HRQ\n");
5242 goto out_free_fcp_wq;
5243 }
5244 phba->sli4_hba.hdr_rq = qdesc;
5245
5246 /* Create Receive Queue for data */
5247 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5248 phba->sli4_hba.rq_ecount);
5249 if (!qdesc) {
5250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5251 "0507 Failed allocate receive DRQ\n");
5252 goto out_free_hdr_rq;
5253 }
5254 phba->sli4_hba.dat_rq = qdesc;
5255
5256 return 0;
5257
5258out_free_hdr_rq:
5259 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5260 phba->sli4_hba.hdr_rq = NULL;
5261out_free_fcp_wq:
5262 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5263 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5264 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5265 }
5266 kfree(phba->sli4_hba.fcp_wq);
5267out_free_els_wq:
5268 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5269 phba->sli4_hba.els_wq = NULL;
5270out_free_mbx_wq:
5271 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5272 phba->sli4_hba.mbx_wq = NULL;
5273out_free_fcp_cq:
5274 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5275 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5276 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5277 }
5278 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005279out_free_els_cq:
5280 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5281 phba->sli4_hba.els_cq = NULL;
5282out_free_mbx_cq:
5283 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5284 phba->sli4_hba.mbx_cq = NULL;
5285out_free_fp_eq:
5286 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5287 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5288 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5289 }
5290 kfree(phba->sli4_hba.fp_eq);
5291out_free_sp_eq:
5292 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5293 phba->sli4_hba.sp_eq = NULL;
5294out_error:
5295 return -ENOMEM;
5296}
5297
5298/**
5299 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5300 * @phba: pointer to lpfc hba data structure.
5301 *
5302 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5303 * operation.
5304 *
5305 * Return codes
5306 * 0 - sucessful
5307 * ENOMEM - No availble memory
5308 * EIO - The mailbox failed to complete successfully.
5309 **/
5310static void
5311lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5312{
5313 int fcp_qidx;
5314
5315 /* Release mailbox command work queue */
5316 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5317 phba->sli4_hba.mbx_wq = NULL;
5318
5319 /* Release ELS work queue */
5320 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5321 phba->sli4_hba.els_wq = NULL;
5322
5323 /* Release FCP work queue */
5324 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5325 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5326 kfree(phba->sli4_hba.fcp_wq);
5327 phba->sli4_hba.fcp_wq = NULL;
5328
5329 /* Release unsolicited receive queue */
5330 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5331 phba->sli4_hba.hdr_rq = NULL;
5332 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5333 phba->sli4_hba.dat_rq = NULL;
5334
James Smartda0436e2009-05-22 14:51:39 -04005335 /* Release ELS complete queue */
5336 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5337 phba->sli4_hba.els_cq = NULL;
5338
5339 /* Release mailbox command complete queue */
5340 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5341 phba->sli4_hba.mbx_cq = NULL;
5342
5343 /* Release FCP response complete queue */
5344 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5345 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5346 kfree(phba->sli4_hba.fcp_cq);
5347 phba->sli4_hba.fcp_cq = NULL;
5348
5349 /* Release fast-path event queue */
5350 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5351 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5352 kfree(phba->sli4_hba.fp_eq);
5353 phba->sli4_hba.fp_eq = NULL;
5354
5355 /* Release slow-path event queue */
5356 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5357 phba->sli4_hba.sp_eq = NULL;
5358
5359 return;
5360}
5361
5362/**
5363 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5364 * @phba: pointer to lpfc hba data structure.
5365 *
5366 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5367 * operation.
5368 *
5369 * Return codes
5370 * 0 - sucessful
5371 * ENOMEM - No availble memory
5372 * EIO - The mailbox failed to complete successfully.
5373 **/
5374int
5375lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5376{
5377 int rc = -ENOMEM;
5378 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5379 int fcp_cq_index = 0;
5380
5381 /*
5382 * Set up Event Queues (EQs)
5383 */
5384
5385 /* Set up slow-path event queue */
5386 if (!phba->sli4_hba.sp_eq) {
5387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5388 "0520 Slow-path EQ not allocated\n");
5389 goto out_error;
5390 }
5391 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5392 LPFC_SP_DEF_IMAX);
5393 if (rc) {
5394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5395 "0521 Failed setup of slow-path EQ: "
5396 "rc = 0x%x\n", rc);
5397 goto out_error;
5398 }
5399 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5400 "2583 Slow-path EQ setup: queue-id=%d\n",
5401 phba->sli4_hba.sp_eq->queue_id);
5402
5403 /* Set up fast-path event queue */
5404 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5405 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5407 "0522 Fast-path EQ (%d) not "
5408 "allocated\n", fcp_eqidx);
5409 goto out_destroy_fp_eq;
5410 }
5411 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5412 phba->cfg_fcp_imax);
5413 if (rc) {
5414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5415 "0523 Failed setup of fast-path EQ "
5416 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5417 goto out_destroy_fp_eq;
5418 }
5419 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5420 "2584 Fast-path EQ setup: "
5421 "queue[%d]-id=%d\n", fcp_eqidx,
5422 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5423 }
5424
5425 /*
5426 * Set up Complete Queues (CQs)
5427 */
5428
5429 /* Set up slow-path MBOX Complete Queue as the first CQ */
5430 if (!phba->sli4_hba.mbx_cq) {
5431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5432 "0528 Mailbox CQ not allocated\n");
5433 goto out_destroy_fp_eq;
5434 }
5435 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5436 LPFC_MCQ, LPFC_MBOX);
5437 if (rc) {
5438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5439 "0529 Failed setup of slow-path mailbox CQ: "
5440 "rc = 0x%x\n", rc);
5441 goto out_destroy_fp_eq;
5442 }
5443 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5444 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5445 phba->sli4_hba.mbx_cq->queue_id,
5446 phba->sli4_hba.sp_eq->queue_id);
5447
5448 /* Set up slow-path ELS Complete Queue */
5449 if (!phba->sli4_hba.els_cq) {
5450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5451 "0530 ELS CQ not allocated\n");
5452 goto out_destroy_mbx_cq;
5453 }
5454 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5455 LPFC_WCQ, LPFC_ELS);
5456 if (rc) {
5457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5458 "0531 Failed setup of slow-path ELS CQ: "
5459 "rc = 0x%x\n", rc);
5460 goto out_destroy_mbx_cq;
5461 }
5462 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5463 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5464 phba->sli4_hba.els_cq->queue_id,
5465 phba->sli4_hba.sp_eq->queue_id);
5466
James Smartda0436e2009-05-22 14:51:39 -04005467 /* Set up fast-path FCP Response Complete Queue */
5468 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5469 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5471 "0526 Fast-path FCP CQ (%d) not "
5472 "allocated\n", fcp_cqidx);
5473 goto out_destroy_fcp_cq;
5474 }
5475 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5476 phba->sli4_hba.fp_eq[fcp_cqidx],
5477 LPFC_WCQ, LPFC_FCP);
5478 if (rc) {
5479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5480 "0527 Failed setup of fast-path FCP "
5481 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5482 goto out_destroy_fcp_cq;
5483 }
5484 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5485 "2588 FCP CQ setup: cq[%d]-id=%d, "
5486 "parent eq[%d]-id=%d\n",
5487 fcp_cqidx,
5488 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5489 fcp_cqidx,
5490 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5491 }
5492
5493 /*
5494 * Set up all the Work Queues (WQs)
5495 */
5496
5497 /* Set up Mailbox Command Queue */
5498 if (!phba->sli4_hba.mbx_wq) {
5499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5500 "0538 Slow-path MQ not allocated\n");
5501 goto out_destroy_fcp_cq;
5502 }
5503 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5504 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5505 if (rc) {
5506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5507 "0539 Failed setup of slow-path MQ: "
5508 "rc = 0x%x\n", rc);
5509 goto out_destroy_fcp_cq;
5510 }
5511 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5512 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5513 phba->sli4_hba.mbx_wq->queue_id,
5514 phba->sli4_hba.mbx_cq->queue_id);
5515
5516 /* Set up slow-path ELS Work Queue */
5517 if (!phba->sli4_hba.els_wq) {
5518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5519 "0536 Slow-path ELS WQ not allocated\n");
5520 goto out_destroy_mbx_wq;
5521 }
5522 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5523 phba->sli4_hba.els_cq, LPFC_ELS);
5524 if (rc) {
5525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5526 "0537 Failed setup of slow-path ELS WQ: "
5527 "rc = 0x%x\n", rc);
5528 goto out_destroy_mbx_wq;
5529 }
5530 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5531 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5532 phba->sli4_hba.els_wq->queue_id,
5533 phba->sli4_hba.els_cq->queue_id);
5534
5535 /* Set up fast-path FCP Work Queue */
5536 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5537 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5539 "0534 Fast-path FCP WQ (%d) not "
5540 "allocated\n", fcp_wqidx);
5541 goto out_destroy_fcp_wq;
5542 }
5543 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5544 phba->sli4_hba.fcp_cq[fcp_cq_index],
5545 LPFC_FCP);
5546 if (rc) {
5547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5548 "0535 Failed setup of fast-path FCP "
5549 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5550 goto out_destroy_fcp_wq;
5551 }
5552 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5553 "2591 FCP WQ setup: wq[%d]-id=%d, "
5554 "parent cq[%d]-id=%d\n",
5555 fcp_wqidx,
5556 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5557 fcp_cq_index,
5558 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5559 /* Round robin FCP Work Queue's Completion Queue assignment */
5560 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5561 }
5562
5563 /*
5564 * Create Receive Queue (RQ)
5565 */
5566 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5568 "0540 Receive Queue not allocated\n");
5569 goto out_destroy_fcp_wq;
5570 }
5571 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04005572 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04005573 if (rc) {
5574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5575 "0541 Failed setup of Receive Queue: "
5576 "rc = 0x%x\n", rc);
5577 goto out_destroy_fcp_wq;
5578 }
5579 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5580 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5581 "parent cq-id=%d\n",
5582 phba->sli4_hba.hdr_rq->queue_id,
5583 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04005584 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04005585 return 0;
5586
5587out_destroy_fcp_wq:
5588 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5589 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5590 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5591out_destroy_mbx_wq:
5592 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5593out_destroy_fcp_cq:
5594 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5595 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04005596 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5597out_destroy_mbx_cq:
5598 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5599out_destroy_fp_eq:
5600 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5601 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5602 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5603out_error:
5604 return rc;
5605}
5606
5607/**
5608 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5609 * @phba: pointer to lpfc hba data structure.
5610 *
5611 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5612 * operation.
5613 *
5614 * Return codes
5615 * 0 - sucessful
5616 * ENOMEM - No availble memory
5617 * EIO - The mailbox failed to complete successfully.
5618 **/
5619void
5620lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5621{
5622 int fcp_qidx;
5623
5624 /* Unset mailbox command work queue */
5625 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5626 /* Unset ELS work queue */
5627 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5628 /* Unset unsolicited receive queue */
5629 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5630 /* Unset FCP work queue */
5631 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5632 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5633 /* Unset mailbox command complete queue */
5634 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5635 /* Unset ELS complete queue */
5636 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04005637 /* Unset FCP response complete queue */
5638 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5639 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5640 /* Unset fast-path event queue */
5641 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5642 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5643 /* Unset slow-path event queue */
5644 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5645}
5646
5647/**
5648 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5649 * @phba: pointer to lpfc hba data structure.
5650 *
5651 * This routine is invoked to allocate and set up a pool of completion queue
5652 * events. The body of the completion queue event is a completion queue entry
5653 * CQE. For now, this pool is used for the interrupt service routine to queue
5654 * the following HBA completion queue events for the worker thread to process:
5655 * - Mailbox asynchronous events
5656 * - Receive queue completion unsolicited events
5657 * Later, this can be used for all the slow-path events.
5658 *
5659 * Return codes
5660 * 0 - sucessful
5661 * -ENOMEM - No availble memory
5662 **/
5663static int
5664lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5665{
5666 struct lpfc_cq_event *cq_event;
5667 int i;
5668
5669 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5670 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5671 if (!cq_event)
5672 goto out_pool_create_fail;
5673 list_add_tail(&cq_event->list,
5674 &phba->sli4_hba.sp_cqe_event_pool);
5675 }
5676 return 0;
5677
5678out_pool_create_fail:
5679 lpfc_sli4_cq_event_pool_destroy(phba);
5680 return -ENOMEM;
5681}
5682
5683/**
5684 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5685 * @phba: pointer to lpfc hba data structure.
5686 *
5687 * This routine is invoked to free the pool of completion queue events at
5688 * driver unload time. Note that, it is the responsibility of the driver
5689 * cleanup routine to free all the outstanding completion-queue events
5690 * allocated from this pool back into the pool before invoking this routine
5691 * to destroy the pool.
5692 **/
5693static void
5694lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5695{
5696 struct lpfc_cq_event *cq_event, *next_cq_event;
5697
5698 list_for_each_entry_safe(cq_event, next_cq_event,
5699 &phba->sli4_hba.sp_cqe_event_pool, list) {
5700 list_del(&cq_event->list);
5701 kfree(cq_event);
5702 }
5703}
5704
5705/**
5706 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5707 * @phba: pointer to lpfc hba data structure.
5708 *
5709 * This routine is the lock free version of the API invoked to allocate a
5710 * completion-queue event from the free pool.
5711 *
5712 * Return: Pointer to the newly allocated completion-queue event if successful
5713 * NULL otherwise.
5714 **/
5715struct lpfc_cq_event *
5716__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5717{
5718 struct lpfc_cq_event *cq_event = NULL;
5719
5720 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5721 struct lpfc_cq_event, list);
5722 return cq_event;
5723}
5724
5725/**
5726 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5727 * @phba: pointer to lpfc hba data structure.
5728 *
5729 * This routine is the lock version of the API invoked to allocate a
5730 * completion-queue event from the free pool.
5731 *
5732 * Return: Pointer to the newly allocated completion-queue event if successful
5733 * NULL otherwise.
5734 **/
5735struct lpfc_cq_event *
5736lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5737{
5738 struct lpfc_cq_event *cq_event;
5739 unsigned long iflags;
5740
5741 spin_lock_irqsave(&phba->hbalock, iflags);
5742 cq_event = __lpfc_sli4_cq_event_alloc(phba);
5743 spin_unlock_irqrestore(&phba->hbalock, iflags);
5744 return cq_event;
5745}
5746
5747/**
5748 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5749 * @phba: pointer to lpfc hba data structure.
5750 * @cq_event: pointer to the completion queue event to be freed.
5751 *
5752 * This routine is the lock free version of the API invoked to release a
5753 * completion-queue event back into the free pool.
5754 **/
5755void
5756__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5757 struct lpfc_cq_event *cq_event)
5758{
5759 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
5760}
5761
5762/**
5763 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5764 * @phba: pointer to lpfc hba data structure.
5765 * @cq_event: pointer to the completion queue event to be freed.
5766 *
5767 * This routine is the lock version of the API invoked to release a
5768 * completion-queue event back into the free pool.
5769 **/
5770void
5771lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5772 struct lpfc_cq_event *cq_event)
5773{
5774 unsigned long iflags;
5775 spin_lock_irqsave(&phba->hbalock, iflags);
5776 __lpfc_sli4_cq_event_release(phba, cq_event);
5777 spin_unlock_irqrestore(&phba->hbalock, iflags);
5778}
5779
5780/**
5781 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
5782 * @phba: pointer to lpfc hba data structure.
5783 *
5784 * This routine is to free all the pending completion-queue events to the
5785 * back into the free pool for device reset.
5786 **/
5787static void
5788lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
5789{
5790 LIST_HEAD(cqelist);
5791 struct lpfc_cq_event *cqe;
5792 unsigned long iflags;
5793
5794 /* Retrieve all the pending WCQEs from pending WCQE lists */
5795 spin_lock_irqsave(&phba->hbalock, iflags);
5796 /* Pending FCP XRI abort events */
5797 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
5798 &cqelist);
5799 /* Pending ELS XRI abort events */
5800 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
5801 &cqelist);
5802 /* Pending asynnc events */
5803 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
5804 &cqelist);
5805 spin_unlock_irqrestore(&phba->hbalock, iflags);
5806
5807 while (!list_empty(&cqelist)) {
5808 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
5809 lpfc_sli4_cq_event_release(phba, cqe);
5810 }
5811}
5812
5813/**
5814 * lpfc_pci_function_reset - Reset pci function.
5815 * @phba: pointer to lpfc hba data structure.
5816 *
5817 * This routine is invoked to request a PCI function reset. It will destroys
5818 * all resources assigned to the PCI function which originates this request.
5819 *
5820 * Return codes
5821 * 0 - sucessful
5822 * ENOMEM - No availble memory
5823 * EIO - The mailbox failed to complete successfully.
5824 **/
5825int
5826lpfc_pci_function_reset(struct lpfc_hba *phba)
5827{
5828 LPFC_MBOXQ_t *mboxq;
5829 uint32_t rc = 0;
5830 uint32_t shdr_status, shdr_add_status;
5831 union lpfc_sli4_cfg_shdr *shdr;
5832
5833 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5834 if (!mboxq) {
5835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5836 "0494 Unable to allocate memory for issuing "
5837 "SLI_FUNCTION_RESET mailbox command\n");
5838 return -ENOMEM;
5839 }
5840
5841 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
5842 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5843 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
5844 LPFC_SLI4_MBX_EMBED);
5845 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5846 shdr = (union lpfc_sli4_cfg_shdr *)
5847 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5848 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5849 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5850 if (rc != MBX_TIMEOUT)
5851 mempool_free(mboxq, phba->mbox_mem_pool);
5852 if (shdr_status || shdr_add_status || rc) {
5853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5854 "0495 SLI_FUNCTION_RESET mailbox failed with "
5855 "status x%x add_status x%x, mbx status x%x\n",
5856 shdr_status, shdr_add_status, rc);
5857 rc = -ENXIO;
5858 }
5859 return rc;
5860}
5861
5862/**
5863 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
5864 * @phba: pointer to lpfc hba data structure.
5865 * @cnt: number of nop mailbox commands to send.
5866 *
5867 * This routine is invoked to send a number @cnt of NOP mailbox command and
5868 * wait for each command to complete.
5869 *
5870 * Return: the number of NOP mailbox command completed.
5871 **/
5872static int
5873lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
5874{
5875 LPFC_MBOXQ_t *mboxq;
5876 int length, cmdsent;
5877 uint32_t mbox_tmo;
5878 uint32_t rc = 0;
5879 uint32_t shdr_status, shdr_add_status;
5880 union lpfc_sli4_cfg_shdr *shdr;
5881
5882 if (cnt == 0) {
5883 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5884 "2518 Requested to send 0 NOP mailbox cmd\n");
5885 return cnt;
5886 }
5887
5888 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5889 if (!mboxq) {
5890 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5891 "2519 Unable to allocate memory for issuing "
5892 "NOP mailbox command\n");
5893 return 0;
5894 }
5895
5896 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
5897 length = (sizeof(struct lpfc_mbx_nop) -
5898 sizeof(struct lpfc_sli4_cfg_mhdr));
5899 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5900 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
5901
5902 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5903 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
5904 if (!phba->sli4_hba.intr_enable)
5905 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5906 else
5907 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
5908 if (rc == MBX_TIMEOUT)
5909 break;
5910 /* Check return status */
5911 shdr = (union lpfc_sli4_cfg_shdr *)
5912 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5913 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5914 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
5915 &shdr->response);
5916 if (shdr_status || shdr_add_status || rc) {
5917 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5918 "2520 NOP mailbox command failed "
5919 "status x%x add_status x%x mbx "
5920 "status x%x\n", shdr_status,
5921 shdr_add_status, rc);
5922 break;
5923 }
5924 }
5925
5926 if (rc != MBX_TIMEOUT)
5927 mempool_free(mboxq, phba->mbox_mem_pool);
5928
5929 return cmdsent;
5930}
5931
5932/**
5933 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
5934 * @phba: pointer to lpfc hba data structure.
5935 * @fcfi: fcf index.
5936 *
5937 * This routine is invoked to unregister a FCFI from device.
5938 **/
5939void
5940lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
5941{
5942 LPFC_MBOXQ_t *mbox;
5943 uint32_t mbox_tmo;
5944 int rc;
5945 unsigned long flags;
5946
5947 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5948
5949 if (!mbox)
5950 return;
5951
5952 lpfc_unreg_fcfi(mbox, fcfi);
5953
5954 if (!phba->sli4_hba.intr_enable)
5955 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5956 else {
5957 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5958 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5959 }
5960 if (rc != MBX_TIMEOUT)
5961 mempool_free(mbox, phba->mbox_mem_pool);
5962 if (rc != MBX_SUCCESS)
5963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5964 "2517 Unregister FCFI command failed "
5965 "status %d, mbxStatus x%x\n", rc,
5966 bf_get(lpfc_mqe_status, &mbox->u.mqe));
5967 else {
5968 spin_lock_irqsave(&phba->hbalock, flags);
5969 /* Mark the FCFI is no longer registered */
5970 phba->fcf.fcf_flag &=
5971 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_DISCOVERED);
5972 spin_unlock_irqrestore(&phba->hbalock, flags);
5973 }
5974}
5975
5976/**
5977 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
5978 * @phba: pointer to lpfc hba data structure.
5979 *
5980 * This routine is invoked to set up the PCI device memory space for device
5981 * with SLI-4 interface spec.
5982 *
5983 * Return codes
5984 * 0 - sucessful
5985 * other values - error
5986 **/
5987static int
5988lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
5989{
5990 struct pci_dev *pdev;
5991 unsigned long bar0map_len, bar1map_len, bar2map_len;
5992 int error = -ENODEV;
5993
5994 /* Obtain PCI device reference */
5995 if (!phba->pcidev)
5996 return error;
5997 else
5998 pdev = phba->pcidev;
5999
6000 /* Set the device DMA mask size */
6001 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
6002 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
6003 return error;
6004
6005 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6006 * number of bytes required by each mapping. They are actually
6007 * mapping to the PCI BAR regions 1, 2, and 4 by the SLI4 device.
6008 */
6009 phba->pci_bar0_map = pci_resource_start(pdev, LPFC_SLI4_BAR0);
6010 bar0map_len = pci_resource_len(pdev, LPFC_SLI4_BAR0);
6011
6012 phba->pci_bar1_map = pci_resource_start(pdev, LPFC_SLI4_BAR1);
6013 bar1map_len = pci_resource_len(pdev, LPFC_SLI4_BAR1);
6014
6015 phba->pci_bar2_map = pci_resource_start(pdev, LPFC_SLI4_BAR2);
6016 bar2map_len = pci_resource_len(pdev, LPFC_SLI4_BAR2);
6017
6018 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6019 phba->sli4_hba.conf_regs_memmap_p =
6020 ioremap(phba->pci_bar0_map, bar0map_len);
6021 if (!phba->sli4_hba.conf_regs_memmap_p) {
6022 dev_printk(KERN_ERR, &pdev->dev,
6023 "ioremap failed for SLI4 PCI config registers.\n");
6024 goto out;
6025 }
6026
6027 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6028 phba->sli4_hba.ctrl_regs_memmap_p =
6029 ioremap(phba->pci_bar1_map, bar1map_len);
6030 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6031 dev_printk(KERN_ERR, &pdev->dev,
6032 "ioremap failed for SLI4 HBA control registers.\n");
6033 goto out_iounmap_conf;
6034 }
6035
6036 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6037 phba->sli4_hba.drbl_regs_memmap_p =
6038 ioremap(phba->pci_bar2_map, bar2map_len);
6039 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6040 dev_printk(KERN_ERR, &pdev->dev,
6041 "ioremap failed for SLI4 HBA doorbell registers.\n");
6042 goto out_iounmap_ctrl;
6043 }
6044
6045 /* Set up BAR0 PCI config space register memory map */
6046 lpfc_sli4_bar0_register_memmap(phba);
6047
6048 /* Set up BAR1 register memory map */
6049 lpfc_sli4_bar1_register_memmap(phba);
6050
6051 /* Set up BAR2 register memory map */
6052 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6053 if (error)
6054 goto out_iounmap_all;
6055
6056 return 0;
6057
6058out_iounmap_all:
6059 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6060out_iounmap_ctrl:
6061 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6062out_iounmap_conf:
6063 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6064out:
6065 return error;
6066}
6067
6068/**
6069 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6070 * @phba: pointer to lpfc hba data structure.
6071 *
6072 * This routine is invoked to unset the PCI device memory space for device
6073 * with SLI-4 interface spec.
6074 **/
6075static void
6076lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6077{
6078 struct pci_dev *pdev;
6079
6080 /* Obtain PCI device reference */
6081 if (!phba->pcidev)
6082 return;
6083 else
6084 pdev = phba->pcidev;
6085
6086 /* Free coherent DMA memory allocated */
6087
6088 /* Unmap I/O memory space */
6089 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6090 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6091 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6092
6093 return;
6094}
6095
6096/**
James Smart3772a992009-05-22 14:50:54 -04006097 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6098 * @phba: pointer to lpfc hba data structure.
6099 *
6100 * This routine is invoked to enable the MSI-X interrupt vectors to device
6101 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6102 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6103 * invoked, enables either all or nothing, depending on the current
6104 * availability of PCI vector resources. The device driver is responsible
6105 * for calling the individual request_irq() to register each MSI-X vector
6106 * with a interrupt handler, which is done in this function. Note that
6107 * later when device is unloading, the driver should always call free_irq()
6108 * on all MSI-X vectors it has done request_irq() on before calling
6109 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6110 * will be left with MSI-X enabled and leaks its vectors.
6111 *
6112 * Return codes
6113 * 0 - sucessful
6114 * other values - error
6115 **/
6116static int
6117lpfc_sli_enable_msix(struct lpfc_hba *phba)
6118{
6119 int rc, i;
6120 LPFC_MBOXQ_t *pmb;
6121
6122 /* Set up MSI-X multi-message vectors */
6123 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6124 phba->msix_entries[i].entry = i;
6125
6126 /* Configure MSI-X capability structure */
6127 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6128 ARRAY_SIZE(phba->msix_entries));
6129 if (rc) {
6130 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6131 "0420 PCI enable MSI-X failed (%d)\n", rc);
6132 goto msi_fail_out;
6133 }
6134 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6135 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6136 "0477 MSI-X entry[%d]: vector=x%x "
6137 "message=%d\n", i,
6138 phba->msix_entries[i].vector,
6139 phba->msix_entries[i].entry);
6140 /*
6141 * Assign MSI-X vectors to interrupt handlers
6142 */
6143
6144 /* vector-0 is associated to slow-path handler */
6145 rc = request_irq(phba->msix_entries[0].vector,
6146 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6147 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6148 if (rc) {
6149 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6150 "0421 MSI-X slow-path request_irq failed "
6151 "(%d)\n", rc);
6152 goto msi_fail_out;
6153 }
6154
6155 /* vector-1 is associated to fast-path handler */
6156 rc = request_irq(phba->msix_entries[1].vector,
6157 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6158 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6159
6160 if (rc) {
6161 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6162 "0429 MSI-X fast-path request_irq failed "
6163 "(%d)\n", rc);
6164 goto irq_fail_out;
6165 }
6166
6167 /*
6168 * Configure HBA MSI-X attention conditions to messages
6169 */
6170 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6171
6172 if (!pmb) {
6173 rc = -ENOMEM;
6174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6175 "0474 Unable to allocate memory for issuing "
6176 "MBOX_CONFIG_MSI command\n");
6177 goto mem_fail_out;
6178 }
6179 rc = lpfc_config_msi(phba, pmb);
6180 if (rc)
6181 goto mbx_fail_out;
6182 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6183 if (rc != MBX_SUCCESS) {
6184 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6185 "0351 Config MSI mailbox command failed, "
6186 "mbxCmd x%x, mbxStatus x%x\n",
6187 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6188 goto mbx_fail_out;
6189 }
6190
6191 /* Free memory allocated for mailbox command */
6192 mempool_free(pmb, phba->mbox_mem_pool);
6193 return rc;
6194
6195mbx_fail_out:
6196 /* Free memory allocated for mailbox command */
6197 mempool_free(pmb, phba->mbox_mem_pool);
6198
6199mem_fail_out:
6200 /* free the irq already requested */
6201 free_irq(phba->msix_entries[1].vector, phba);
6202
6203irq_fail_out:
6204 /* free the irq already requested */
6205 free_irq(phba->msix_entries[0].vector, phba);
6206
6207msi_fail_out:
6208 /* Unconfigure MSI-X capability structure */
6209 pci_disable_msix(phba->pcidev);
6210 return rc;
6211}
6212
6213/**
6214 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6215 * @phba: pointer to lpfc hba data structure.
6216 *
6217 * This routine is invoked to release the MSI-X vectors and then disable the
6218 * MSI-X interrupt mode to device with SLI-3 interface spec.
6219 **/
6220static void
6221lpfc_sli_disable_msix(struct lpfc_hba *phba)
6222{
6223 int i;
6224
6225 /* Free up MSI-X multi-message vectors */
6226 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6227 free_irq(phba->msix_entries[i].vector, phba);
6228 /* Disable MSI-X */
6229 pci_disable_msix(phba->pcidev);
6230
6231 return;
6232}
6233
6234/**
6235 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6236 * @phba: pointer to lpfc hba data structure.
6237 *
6238 * This routine is invoked to enable the MSI interrupt mode to device with
6239 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6240 * enable the MSI vector. The device driver is responsible for calling the
6241 * request_irq() to register MSI vector with a interrupt the handler, which
6242 * is done in this function.
6243 *
6244 * Return codes
6245 * 0 - sucessful
6246 * other values - error
6247 */
6248static int
6249lpfc_sli_enable_msi(struct lpfc_hba *phba)
6250{
6251 int rc;
6252
6253 rc = pci_enable_msi(phba->pcidev);
6254 if (!rc)
6255 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6256 "0462 PCI enable MSI mode success.\n");
6257 else {
6258 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6259 "0471 PCI enable MSI mode failed (%d)\n", rc);
6260 return rc;
6261 }
6262
6263 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6264 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6265 if (rc) {
6266 pci_disable_msi(phba->pcidev);
6267 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6268 "0478 MSI request_irq failed (%d)\n", rc);
6269 }
6270 return rc;
6271}
6272
6273/**
6274 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6275 * @phba: pointer to lpfc hba data structure.
6276 *
6277 * This routine is invoked to disable the MSI interrupt mode to device with
6278 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6279 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6280 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6281 * its vector.
6282 */
6283static void
6284lpfc_sli_disable_msi(struct lpfc_hba *phba)
6285{
6286 free_irq(phba->pcidev->irq, phba);
6287 pci_disable_msi(phba->pcidev);
6288 return;
6289}
6290
6291/**
6292 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6293 * @phba: pointer to lpfc hba data structure.
6294 *
6295 * This routine is invoked to enable device interrupt and associate driver's
6296 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6297 * spec. Depends on the interrupt mode configured to the driver, the driver
6298 * will try to fallback from the configured interrupt mode to an interrupt
6299 * mode which is supported by the platform, kernel, and device in the order
6300 * of:
6301 * MSI-X -> MSI -> IRQ.
6302 *
6303 * Return codes
6304 * 0 - sucessful
6305 * other values - error
6306 **/
6307static uint32_t
6308lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6309{
6310 uint32_t intr_mode = LPFC_INTR_ERROR;
6311 int retval;
6312
6313 if (cfg_mode == 2) {
6314 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6315 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6316 if (!retval) {
6317 /* Now, try to enable MSI-X interrupt mode */
6318 retval = lpfc_sli_enable_msix(phba);
6319 if (!retval) {
6320 /* Indicate initialization to MSI-X mode */
6321 phba->intr_type = MSIX;
6322 intr_mode = 2;
6323 }
6324 }
6325 }
6326
6327 /* Fallback to MSI if MSI-X initialization failed */
6328 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6329 retval = lpfc_sli_enable_msi(phba);
6330 if (!retval) {
6331 /* Indicate initialization to MSI mode */
6332 phba->intr_type = MSI;
6333 intr_mode = 1;
6334 }
6335 }
6336
6337 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6338 if (phba->intr_type == NONE) {
6339 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6340 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6341 if (!retval) {
6342 /* Indicate initialization to INTx mode */
6343 phba->intr_type = INTx;
6344 intr_mode = 0;
6345 }
6346 }
6347 return intr_mode;
6348}
6349
6350/**
6351 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6352 * @phba: pointer to lpfc hba data structure.
6353 *
6354 * This routine is invoked to disable device interrupt and disassociate the
6355 * driver's interrupt handler(s) from interrupt vector(s) to device with
6356 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6357 * release the interrupt vector(s) for the message signaled interrupt.
6358 **/
6359static void
6360lpfc_sli_disable_intr(struct lpfc_hba *phba)
6361{
6362 /* Disable the currently initialized interrupt mode */
6363 if (phba->intr_type == MSIX)
6364 lpfc_sli_disable_msix(phba);
6365 else if (phba->intr_type == MSI)
6366 lpfc_sli_disable_msi(phba);
6367 else if (phba->intr_type == INTx)
6368 free_irq(phba->pcidev->irq, phba);
6369
6370 /* Reset interrupt management states */
6371 phba->intr_type = NONE;
6372 phba->sli.slistat.sli_intr = 0;
6373
6374 return;
6375}
6376
6377/**
James Smartda0436e2009-05-22 14:51:39 -04006378 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6379 * @phba: pointer to lpfc hba data structure.
6380 *
6381 * This routine is invoked to enable the MSI-X interrupt vectors to device
6382 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6383 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6384 * enables either all or nothing, depending on the current availability of
6385 * PCI vector resources. The device driver is responsible for calling the
6386 * individual request_irq() to register each MSI-X vector with a interrupt
6387 * handler, which is done in this function. Note that later when device is
6388 * unloading, the driver should always call free_irq() on all MSI-X vectors
6389 * it has done request_irq() on before calling pci_disable_msix(). Failure
6390 * to do so results in a BUG_ON() and a device will be left with MSI-X
6391 * enabled and leaks its vectors.
6392 *
6393 * Return codes
6394 * 0 - sucessful
6395 * other values - error
6396 **/
6397static int
6398lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6399{
6400 int rc, index;
6401
6402 /* Set up MSI-X multi-message vectors */
6403 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6404 phba->sli4_hba.msix_entries[index].entry = index;
6405
6406 /* Configure MSI-X capability structure */
6407 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6408 phba->sli4_hba.cfg_eqn);
6409 if (rc) {
6410 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6411 "0484 PCI enable MSI-X failed (%d)\n", rc);
6412 goto msi_fail_out;
6413 }
6414 /* Log MSI-X vector assignment */
6415 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6416 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6417 "0489 MSI-X entry[%d]: vector=x%x "
6418 "message=%d\n", index,
6419 phba->sli4_hba.msix_entries[index].vector,
6420 phba->sli4_hba.msix_entries[index].entry);
6421 /*
6422 * Assign MSI-X vectors to interrupt handlers
6423 */
6424
6425 /* The first vector must associated to slow-path handler for MQ */
6426 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6427 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6428 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6429 if (rc) {
6430 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6431 "0485 MSI-X slow-path request_irq failed "
6432 "(%d)\n", rc);
6433 goto msi_fail_out;
6434 }
6435
6436 /* The rest of the vector(s) are associated to fast-path handler(s) */
6437 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6438 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6439 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6440 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6441 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6442 LPFC_FP_DRIVER_HANDLER_NAME,
6443 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6444 if (rc) {
6445 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6446 "0486 MSI-X fast-path (%d) "
6447 "request_irq failed (%d)\n", index, rc);
6448 goto cfg_fail_out;
6449 }
6450 }
6451
6452 return rc;
6453
6454cfg_fail_out:
6455 /* free the irq already requested */
6456 for (--index; index >= 1; index--)
6457 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6458 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6459
6460 /* free the irq already requested */
6461 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6462
6463msi_fail_out:
6464 /* Unconfigure MSI-X capability structure */
6465 pci_disable_msix(phba->pcidev);
6466 return rc;
6467}
6468
6469/**
6470 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6471 * @phba: pointer to lpfc hba data structure.
6472 *
6473 * This routine is invoked to release the MSI-X vectors and then disable the
6474 * MSI-X interrupt mode to device with SLI-4 interface spec.
6475 **/
6476static void
6477lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6478{
6479 int index;
6480
6481 /* Free up MSI-X multi-message vectors */
6482 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6483
6484 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6485 free_irq(phba->sli4_hba.msix_entries[index].vector,
6486 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6487 /* Disable MSI-X */
6488 pci_disable_msix(phba->pcidev);
6489
6490 return;
6491}
6492
6493/**
6494 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6495 * @phba: pointer to lpfc hba data structure.
6496 *
6497 * This routine is invoked to enable the MSI interrupt mode to device with
6498 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6499 * to enable the MSI vector. The device driver is responsible for calling
6500 * the request_irq() to register MSI vector with a interrupt the handler,
6501 * which is done in this function.
6502 *
6503 * Return codes
6504 * 0 - sucessful
6505 * other values - error
6506 **/
6507static int
6508lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6509{
6510 int rc, index;
6511
6512 rc = pci_enable_msi(phba->pcidev);
6513 if (!rc)
6514 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6515 "0487 PCI enable MSI mode success.\n");
6516 else {
6517 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6518 "0488 PCI enable MSI mode failed (%d)\n", rc);
6519 return rc;
6520 }
6521
6522 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6523 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6524 if (rc) {
6525 pci_disable_msi(phba->pcidev);
6526 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6527 "0490 MSI request_irq failed (%d)\n", rc);
6528 }
6529
6530 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6531 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6532 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6533 }
6534
6535 return rc;
6536}
6537
6538/**
6539 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6540 * @phba: pointer to lpfc hba data structure.
6541 *
6542 * This routine is invoked to disable the MSI interrupt mode to device with
6543 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6544 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6545 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6546 * its vector.
6547 **/
6548static void
6549lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6550{
6551 free_irq(phba->pcidev->irq, phba);
6552 pci_disable_msi(phba->pcidev);
6553 return;
6554}
6555
6556/**
6557 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6558 * @phba: pointer to lpfc hba data structure.
6559 *
6560 * This routine is invoked to enable device interrupt and associate driver's
6561 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6562 * interface spec. Depends on the interrupt mode configured to the driver,
6563 * the driver will try to fallback from the configured interrupt mode to an
6564 * interrupt mode which is supported by the platform, kernel, and device in
6565 * the order of:
6566 * MSI-X -> MSI -> IRQ.
6567 *
6568 * Return codes
6569 * 0 - sucessful
6570 * other values - error
6571 **/
6572static uint32_t
6573lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6574{
6575 uint32_t intr_mode = LPFC_INTR_ERROR;
6576 int retval, index;
6577
6578 if (cfg_mode == 2) {
6579 /* Preparation before conf_msi mbox cmd */
6580 retval = 0;
6581 if (!retval) {
6582 /* Now, try to enable MSI-X interrupt mode */
6583 retval = lpfc_sli4_enable_msix(phba);
6584 if (!retval) {
6585 /* Indicate initialization to MSI-X mode */
6586 phba->intr_type = MSIX;
6587 intr_mode = 2;
6588 }
6589 }
6590 }
6591
6592 /* Fallback to MSI if MSI-X initialization failed */
6593 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6594 retval = lpfc_sli4_enable_msi(phba);
6595 if (!retval) {
6596 /* Indicate initialization to MSI mode */
6597 phba->intr_type = MSI;
6598 intr_mode = 1;
6599 }
6600 }
6601
6602 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6603 if (phba->intr_type == NONE) {
6604 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6605 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6606 if (!retval) {
6607 /* Indicate initialization to INTx mode */
6608 phba->intr_type = INTx;
6609 intr_mode = 0;
6610 for (index = 0; index < phba->cfg_fcp_eq_count;
6611 index++) {
6612 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6613 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6614 }
6615 }
6616 }
6617 return intr_mode;
6618}
6619
6620/**
6621 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6622 * @phba: pointer to lpfc hba data structure.
6623 *
6624 * This routine is invoked to disable device interrupt and disassociate
6625 * the driver's interrupt handler(s) from interrupt vector(s) to device
6626 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6627 * will release the interrupt vector(s) for the message signaled interrupt.
6628 **/
6629static void
6630lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6631{
6632 /* Disable the currently initialized interrupt mode */
6633 if (phba->intr_type == MSIX)
6634 lpfc_sli4_disable_msix(phba);
6635 else if (phba->intr_type == MSI)
6636 lpfc_sli4_disable_msi(phba);
6637 else if (phba->intr_type == INTx)
6638 free_irq(phba->pcidev->irq, phba);
6639
6640 /* Reset interrupt management states */
6641 phba->intr_type = NONE;
6642 phba->sli.slistat.sli_intr = 0;
6643
6644 return;
6645}
6646
6647/**
James Smart3772a992009-05-22 14:50:54 -04006648 * lpfc_unset_hba - Unset SLI3 hba device initialization
6649 * @phba: pointer to lpfc hba data structure.
6650 *
6651 * This routine is invoked to unset the HBA device initialization steps to
6652 * a device with SLI-3 interface spec.
6653 **/
6654static void
6655lpfc_unset_hba(struct lpfc_hba *phba)
6656{
6657 struct lpfc_vport *vport = phba->pport;
6658 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6659
6660 spin_lock_irq(shost->host_lock);
6661 vport->load_flag |= FC_UNLOADING;
6662 spin_unlock_irq(shost->host_lock);
6663
6664 lpfc_stop_hba_timers(phba);
6665
6666 phba->pport->work_port_events = 0;
6667
6668 lpfc_sli_hba_down(phba);
6669
6670 lpfc_sli_brdrestart(phba);
6671
6672 lpfc_sli_disable_intr(phba);
6673
6674 return;
6675}
6676
6677/**
James Smartda0436e2009-05-22 14:51:39 -04006678 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6679 * @phba: pointer to lpfc hba data structure.
6680 *
6681 * This routine is invoked to unset the HBA device initialization steps to
6682 * a device with SLI-4 interface spec.
6683 **/
6684static void
6685lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6686{
6687 struct lpfc_vport *vport = phba->pport;
6688 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6689
6690 spin_lock_irq(shost->host_lock);
6691 vport->load_flag |= FC_UNLOADING;
6692 spin_unlock_irq(shost->host_lock);
6693
6694 phba->pport->work_port_events = 0;
6695
6696 lpfc_sli4_hba_down(phba);
6697
6698 lpfc_sli4_disable_intr(phba);
6699
6700 return;
6701}
6702
6703/**
6704 * lpfc_sli4_hba_unset - Unset the fcoe hba
6705 * @phba: Pointer to HBA context object.
6706 *
6707 * This function is called in the SLI4 code path to reset the HBA's FCoE
6708 * function. The caller is not required to hold any lock. This routine
6709 * issues PCI function reset mailbox command to reset the FCoE function.
6710 * At the end of the function, it calls lpfc_hba_down_post function to
6711 * free any pending commands.
6712 **/
6713static void
6714lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6715{
6716 int wait_cnt = 0;
6717 LPFC_MBOXQ_t *mboxq;
6718
6719 lpfc_stop_hba_timers(phba);
6720 phba->sli4_hba.intr_enable = 0;
6721
6722 /*
6723 * Gracefully wait out the potential current outstanding asynchronous
6724 * mailbox command.
6725 */
6726
6727 /* First, block any pending async mailbox command from posted */
6728 spin_lock_irq(&phba->hbalock);
6729 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6730 spin_unlock_irq(&phba->hbalock);
6731 /* Now, trying to wait it out if we can */
6732 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6733 msleep(10);
6734 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
6735 break;
6736 }
6737 /* Forcefully release the outstanding mailbox command if timed out */
6738 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6739 spin_lock_irq(&phba->hbalock);
6740 mboxq = phba->sli.mbox_active;
6741 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6742 __lpfc_mbox_cmpl_put(phba, mboxq);
6743 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6744 phba->sli.mbox_active = NULL;
6745 spin_unlock_irq(&phba->hbalock);
6746 }
6747
6748 /* Tear down the queues in the HBA */
6749 lpfc_sli4_queue_unset(phba);
6750
6751 /* Disable PCI subsystem interrupt */
6752 lpfc_sli4_disable_intr(phba);
6753
6754 /* Stop kthread signal shall trigger work_done one more time */
6755 kthread_stop(phba->worker_thread);
6756
6757 /* Stop the SLI4 device port */
6758 phba->pport->work_port_events = 0;
6759}
6760
6761/**
James Smart3772a992009-05-22 14:50:54 -04006762 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
6763 * @pdev: pointer to PCI device
6764 * @pid: pointer to PCI device identifier
6765 *
6766 * This routine is to be called to attach a device with SLI-3 interface spec
6767 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6768 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
6769 * information of the device and driver to see if the driver state that it can
6770 * support this kind of device. If the match is successful, the driver core
6771 * invokes this routine. If this routine determines it can claim the HBA, it
6772 * does all the initialization that it needs to do to handle the HBA properly.
6773 *
6774 * Return code
6775 * 0 - driver can claim the device
6776 * negative value - driver can not claim the device
6777 **/
6778static int __devinit
6779lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
6780{
6781 struct lpfc_hba *phba;
6782 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04006783 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04006784 int error;
6785 uint32_t cfg_mode, intr_mode;
6786
6787 /* Allocate memory for HBA structure */
6788 phba = lpfc_hba_alloc(pdev);
6789 if (!phba)
6790 return -ENOMEM;
6791
6792 /* Perform generic PCI device enabling operation */
6793 error = lpfc_enable_pci_dev(phba);
6794 if (error) {
6795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6796 "1401 Failed to enable pci device.\n");
6797 goto out_free_phba;
6798 }
6799
6800 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
6801 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
6802 if (error)
6803 goto out_disable_pci_dev;
6804
6805 /* Set up SLI-3 specific device PCI memory space */
6806 error = lpfc_sli_pci_mem_setup(phba);
6807 if (error) {
6808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6809 "1402 Failed to set up pci memory space.\n");
6810 goto out_disable_pci_dev;
6811 }
6812
6813 /* Set up phase-1 common device driver resources */
6814 error = lpfc_setup_driver_resource_phase1(phba);
6815 if (error) {
6816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6817 "1403 Failed to set up driver resource.\n");
6818 goto out_unset_pci_mem_s3;
6819 }
6820
6821 /* Set up SLI-3 specific device driver resources */
6822 error = lpfc_sli_driver_resource_setup(phba);
6823 if (error) {
6824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6825 "1404 Failed to set up driver resource.\n");
6826 goto out_unset_pci_mem_s3;
6827 }
6828
6829 /* Initialize and populate the iocb list per host */
6830 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
6831 if (error) {
6832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6833 "1405 Failed to initialize iocb list.\n");
6834 goto out_unset_driver_resource_s3;
6835 }
6836
6837 /* Set up common device driver resources */
6838 error = lpfc_setup_driver_resource_phase2(phba);
6839 if (error) {
6840 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6841 "1406 Failed to set up driver resource.\n");
6842 goto out_free_iocb_list;
6843 }
6844
6845 /* Create SCSI host to the physical port */
6846 error = lpfc_create_shost(phba);
6847 if (error) {
6848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6849 "1407 Failed to create scsi host.\n");
6850 goto out_unset_driver_resource;
6851 }
6852
6853 /* Configure sysfs attributes */
6854 vport = phba->pport;
6855 error = lpfc_alloc_sysfs_attr(vport);
6856 if (error) {
6857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6858 "1476 Failed to allocate sysfs attr\n");
6859 goto out_destroy_shost;
6860 }
6861
James Smart6669f9b2009-10-02 15:16:45 -04006862 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04006863 /* Now, trying to enable interrupt and bring up the device */
6864 cfg_mode = phba->cfg_use_msi;
6865 while (true) {
6866 /* Put device to a known state before enabling interrupt */
6867 lpfc_stop_port(phba);
6868 /* Configure and enable interrupt */
6869 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
6870 if (intr_mode == LPFC_INTR_ERROR) {
6871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6872 "0431 Failed to enable interrupt.\n");
6873 error = -ENODEV;
6874 goto out_free_sysfs_attr;
6875 }
6876 /* SLI-3 HBA setup */
6877 if (lpfc_sli_hba_setup(phba)) {
6878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6879 "1477 Failed to set up hba\n");
6880 error = -ENODEV;
6881 goto out_remove_device;
6882 }
6883
6884 /* Wait 50ms for the interrupts of previous mailbox commands */
6885 msleep(50);
6886 /* Check active interrupts on message signaled interrupts */
6887 if (intr_mode == 0 ||
6888 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
6889 /* Log the current active interrupt mode */
6890 phba->intr_mode = intr_mode;
6891 lpfc_log_intr_mode(phba, intr_mode);
6892 break;
6893 } else {
6894 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6895 "0447 Configure interrupt mode (%d) "
6896 "failed active interrupt test.\n",
6897 intr_mode);
6898 /* Disable the current interrupt mode */
6899 lpfc_sli_disable_intr(phba);
6900 /* Try next level of interrupt mode */
6901 cfg_mode = --intr_mode;
6902 }
6903 }
6904
6905 /* Perform post initialization setup */
6906 lpfc_post_init_setup(phba);
6907
6908 /* Check if there are static vports to be created. */
6909 lpfc_create_static_vport(phba);
6910
6911 return 0;
6912
6913out_remove_device:
6914 lpfc_unset_hba(phba);
6915out_free_sysfs_attr:
6916 lpfc_free_sysfs_attr(vport);
6917out_destroy_shost:
6918 lpfc_destroy_shost(phba);
6919out_unset_driver_resource:
6920 lpfc_unset_driver_resource_phase2(phba);
6921out_free_iocb_list:
6922 lpfc_free_iocb_list(phba);
6923out_unset_driver_resource_s3:
6924 lpfc_sli_driver_resource_unset(phba);
6925out_unset_pci_mem_s3:
6926 lpfc_sli_pci_mem_unset(phba);
6927out_disable_pci_dev:
6928 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04006929 if (shost)
6930 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04006931out_free_phba:
6932 lpfc_hba_free(phba);
6933 return error;
6934}
6935
6936/**
6937 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04006938 * @pdev: pointer to PCI device
6939 *
James Smart3772a992009-05-22 14:50:54 -04006940 * This routine is to be called to disattach a device with SLI-3 interface
6941 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6942 * removed from PCI bus, it performs all the necessary cleanup for the HBA
6943 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04006944 **/
dea31012005-04-17 16:05:31 -05006945static void __devexit
James Smart3772a992009-05-22 14:50:54 -04006946lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05006947{
James Smart2e0fef82007-06-17 19:56:36 -05006948 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6949 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05006950 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05006951 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05006952 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05006953 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
6954
James Smart549e55c2007-08-02 11:09:51 -04006955 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04006956 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04006957 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006958
James Smart858c9f62007-06-17 19:56:39 -05006959 lpfc_free_sysfs_attr(vport);
6960
James Smarteada2722008-12-04 22:39:13 -05006961 /* Release all the vports against this physical port */
6962 vports = lpfc_create_vport_work_array(phba);
6963 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04006964 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05006965 fc_vport_terminate(vports[i]->fc_vport);
6966 lpfc_destroy_vport_work_array(phba, vports);
6967
6968 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05006969 fc_remove_host(shost);
6970 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04006971 lpfc_cleanup(vport);
6972
James Smart2e0fef82007-06-17 19:56:36 -05006973 /*
6974 * Bring down the SLI Layer. This step disable all interrupts,
6975 * clears the rings, discards all mailbox commands, and resets
6976 * the HBA.
6977 */
James Smarta257bf92009-04-06 18:48:10 -04006978
6979 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05006980 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04006981 /* Stop kthread signal shall trigger work_done one more time */
6982 kthread_stop(phba->worker_thread);
6983 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05006984 lpfc_sli_brdrestart(phba);
6985
James Smart3772a992009-05-22 14:50:54 -04006986 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05006987 spin_lock_irq(&phba->hbalock);
6988 list_del_init(&vport->listentry);
6989 spin_unlock_irq(&phba->hbalock);
6990
James Smart858c9f62007-06-17 19:56:39 -05006991 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006992
James Smart5b75da22008-12-04 22:39:35 -05006993 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04006994 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05006995
6996 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05006997 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05006998
6999 /*
7000 * Call scsi_free before mem_free since scsi bufs are released to their
7001 * corresponding pools here.
7002 */
7003 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007004 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007005
James Smart34b02dc2008-08-24 21:49:55 -04007006 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7007 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007008
James Smart2e0fef82007-06-17 19:56:36 -05007009 /* Free resources associated with SLI2 interface */
7010 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007011 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007012
7013 /* unmap adapter SLIM and Control Registers */
7014 iounmap(phba->ctrl_regs_memmap_p);
7015 iounmap(phba->slim_memmap_p);
7016
James Smart3772a992009-05-22 14:50:54 -04007017 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007018
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007019 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007020 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007021}
7022
Linas Vepstas8d63f372007-02-14 14:28:36 -06007023/**
James Smart3772a992009-05-22 14:50:54 -04007024 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007025 * @pdev: pointer to PCI device
7026 * @msg: power management message
7027 *
James Smart3772a992009-05-22 14:50:54 -04007028 * This routine is to be called from the kernel's PCI subsystem to support
7029 * system Power Management (PM) to device with SLI-3 interface spec. When
7030 * PM invokes this method, it quiesces the device by stopping the driver's
7031 * worker thread for the device, turning off device's interrupt and DMA,
7032 * and bring the device offline. Note that as the driver implements the
7033 * minimum PM requirements to a power-aware driver's PM support for the
7034 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7035 * to the suspend() method call will be treated as SUSPEND and the driver will
7036 * fully reinitialize its device during resume() method call, the driver will
7037 * set device to PCI_D3hot state in PCI config space instead of setting it
7038 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007039 *
7040 * Return code
James Smart3772a992009-05-22 14:50:54 -04007041 * 0 - driver suspended the device
7042 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007043 **/
7044static int
James Smart3772a992009-05-22 14:50:54 -04007045lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007046{
7047 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7048 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7049
7050 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7051 "0473 PCI device Power Management suspend.\n");
7052
7053 /* Bring down the device */
7054 lpfc_offline_prep(phba);
7055 lpfc_offline(phba);
7056 kthread_stop(phba->worker_thread);
7057
7058 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007059 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007060
7061 /* Save device state to PCI config space */
7062 pci_save_state(pdev);
7063 pci_set_power_state(pdev, PCI_D3hot);
7064
7065 return 0;
7066}
7067
7068/**
James Smart3772a992009-05-22 14:50:54 -04007069 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007070 * @pdev: pointer to PCI device
7071 *
James Smart3772a992009-05-22 14:50:54 -04007072 * This routine is to be called from the kernel's PCI subsystem to support
7073 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7074 * invokes this method, it restores the device's PCI config space state and
7075 * fully reinitializes the device and brings it online. Note that as the
7076 * driver implements the minimum PM requirements to a power-aware driver's
7077 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7078 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7079 * driver will fully reinitialize its device during resume() method call,
7080 * the device will be set to PCI_D0 directly in PCI config space before
7081 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007082 *
7083 * Return code
James Smart3772a992009-05-22 14:50:54 -04007084 * 0 - driver suspended the device
7085 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007086 **/
7087static int
James Smart3772a992009-05-22 14:50:54 -04007088lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007089{
7090 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7091 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007092 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007093 int error;
7094
7095 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7096 "0452 PCI device Power Management resume.\n");
7097
7098 /* Restore device state from PCI config space */
7099 pci_set_power_state(pdev, PCI_D0);
7100 pci_restore_state(pdev);
7101 if (pdev->is_busmaster)
7102 pci_set_master(pdev);
7103
7104 /* Startup the kernel thread for this host adapter. */
7105 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7106 "lpfc_worker_%d", phba->brd_no);
7107 if (IS_ERR(phba->worker_thread)) {
7108 error = PTR_ERR(phba->worker_thread);
7109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7110 "0434 PM resume failed to start worker "
7111 "thread: error=x%x.\n", error);
7112 return error;
7113 }
7114
James Smart5b75da22008-12-04 22:39:35 -05007115 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007116 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007117 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007119 "0430 PM resume Failed to enable interrupt\n");
7120 return -EIO;
7121 } else
7122 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007123
7124 /* Restart HBA and bring it online */
7125 lpfc_sli_brdrestart(phba);
7126 lpfc_online(phba);
7127
James Smart5b75da22008-12-04 22:39:35 -05007128 /* Log the current active interrupt mode */
7129 lpfc_log_intr_mode(phba, phba->intr_mode);
7130
James Smart3a55b532008-12-04 22:38:54 -05007131 return 0;
7132}
7133
7134/**
James Smart3772a992009-05-22 14:50:54 -04007135 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007136 * @pdev: pointer to PCI device.
7137 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007138 *
James Smart3772a992009-05-22 14:50:54 -04007139 * This routine is called from the PCI subsystem for I/O error handling to
7140 * device with SLI-3 interface spec. This function is called by the PCI
7141 * subsystem after a PCI bus error affecting this device has been detected.
7142 * When this function is invoked, it will need to stop all the I/Os and
7143 * interrupt(s) to the device. Once that is done, it will return
7144 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7145 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007146 *
7147 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007148 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7149 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007150 **/
James Smart3772a992009-05-22 14:50:54 -04007151static pci_ers_result_t
7152lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007153{
James Smart51ef4c22007-08-02 11:10:31 -04007154 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7155 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007156 struct lpfc_sli *psli = &phba->sli;
7157 struct lpfc_sli_ring *pring;
7158
James Smarta8e497d2008-08-24 21:50:11 -04007159 if (state == pci_channel_io_perm_failure) {
7160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7161 "0472 PCI channel I/O permanent failure\n");
7162 /* Block all SCSI devices' I/Os on the host */
7163 lpfc_scsi_dev_block(phba);
7164 /* Clean up all driver's outstanding SCSI I/Os */
7165 lpfc_sli_flush_fcp_rings(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007166 return PCI_ERS_RESULT_DISCONNECT;
James Smarta8e497d2008-08-24 21:50:11 -04007167 }
Linas Vepstas5daa49e2007-03-08 16:19:11 -06007168
Linas Vepstas8d63f372007-02-14 14:28:36 -06007169 pci_disable_device(pdev);
7170 /*
7171 * There may be I/Os dropped by the firmware.
7172 * Error iocb (I/O) on txcmplq and let the SCSI layer
7173 * retry it after re-establishing link.
7174 */
7175 pring = &psli->ring[psli->fcp_ring];
7176 lpfc_sli_abort_iocb_ring(phba, pring);
7177
James Smart5b75da22008-12-04 22:39:35 -05007178 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007179 lpfc_sli_disable_intr(phba);
James Smart51ef4c22007-08-02 11:10:31 -04007180
Linas Vepstas8d63f372007-02-14 14:28:36 -06007181 /* Request a slot reset. */
7182 return PCI_ERS_RESULT_NEED_RESET;
7183}
7184
7185/**
James Smart3772a992009-05-22 14:50:54 -04007186 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007187 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007188 *
James Smart3772a992009-05-22 14:50:54 -04007189 * This routine is called from the PCI subsystem for error handling to
7190 * device with SLI-3 interface spec. This is called after PCI bus has been
7191 * reset to restart the PCI card from scratch, as if from a cold-boot.
7192 * During the PCI subsystem error recovery, after driver returns
7193 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7194 * recovery and then call this routine before calling the .resume method
7195 * to recover the device. This function will initialize the HBA device,
7196 * enable the interrupt, but it will just put the HBA to offline state
7197 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007198 *
7199 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007200 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7201 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007202 */
James Smart3772a992009-05-22 14:50:54 -04007203static pci_ers_result_t
7204lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007205{
James Smart51ef4c22007-08-02 11:10:31 -04007206 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7207 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007208 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007209 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007210
7211 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007212 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007213 printk(KERN_ERR "lpfc: Cannot re-enable "
7214 "PCI device after reset.\n");
7215 return PCI_ERS_RESULT_DISCONNECT;
7216 }
7217
James Smart97207482008-12-04 22:39:19 -05007218 pci_restore_state(pdev);
7219 if (pdev->is_busmaster)
7220 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007221
James Smart92d7f7b2007-06-17 19:56:38 -05007222 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007223 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007224 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007225
James Smart5b75da22008-12-04 22:39:35 -05007226 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007227 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007228 if (intr_mode == LPFC_INTR_ERROR) {
7229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7230 "0427 Cannot re-enable interrupt after "
7231 "slot reset.\n");
7232 return PCI_ERS_RESULT_DISCONNECT;
7233 } else
7234 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007235
7236 /* Take device offline; this will perform cleanup */
7237 lpfc_offline(phba);
7238 lpfc_sli_brdrestart(phba);
7239
James Smart5b75da22008-12-04 22:39:35 -05007240 /* Log the current active interrupt mode */
7241 lpfc_log_intr_mode(phba, phba->intr_mode);
7242
Linas Vepstas8d63f372007-02-14 14:28:36 -06007243 return PCI_ERS_RESULT_RECOVERED;
7244}
7245
7246/**
James Smart3772a992009-05-22 14:50:54 -04007247 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007248 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007249 *
James Smart3772a992009-05-22 14:50:54 -04007250 * This routine is called from the PCI subsystem for error handling to device
7251 * with SLI-3 interface spec. It is called when kernel error recovery tells
7252 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7253 * error recovery. After this call, traffic can start to flow from this device
7254 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007255 */
James Smart3772a992009-05-22 14:50:54 -04007256static void
7257lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007258{
James Smart51ef4c22007-08-02 11:10:31 -04007259 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7260 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007261
James Smart58da1ff2008-04-07 10:15:56 -04007262 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007263}
7264
James Smart3772a992009-05-22 14:50:54 -04007265/**
James Smartda0436e2009-05-22 14:51:39 -04007266 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7267 * @phba: pointer to lpfc hba data structure.
7268 *
7269 * returns the number of ELS/CT IOCBs to reserve
7270 **/
7271int
7272lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7273{
7274 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7275
James Smartf1126682009-06-10 17:22:44 -04007276 if (phba->sli_rev == LPFC_SLI_REV4) {
7277 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04007278 return 10;
James Smartf1126682009-06-10 17:22:44 -04007279 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04007280 return 25;
James Smartf1126682009-06-10 17:22:44 -04007281 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04007282 return 50;
James Smartf1126682009-06-10 17:22:44 -04007283 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04007284 return 100;
James Smartf1126682009-06-10 17:22:44 -04007285 else
James Smart6a9c52c2009-10-02 15:16:51 -04007286 return 150;
James Smartf1126682009-06-10 17:22:44 -04007287 } else
7288 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007289}
7290
7291/**
7292 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7293 * @pdev: pointer to PCI device
7294 * @pid: pointer to PCI device identifier
7295 *
7296 * This routine is called from the kernel's PCI subsystem to device with
7297 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7298 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7299 * information of the device and driver to see if the driver state that it
7300 * can support this kind of device. If the match is successful, the driver
7301 * core invokes this routine. If this routine determines it can claim the HBA,
7302 * it does all the initialization that it needs to do to handle the HBA
7303 * properly.
7304 *
7305 * Return code
7306 * 0 - driver can claim the device
7307 * negative value - driver can not claim the device
7308 **/
7309static int __devinit
7310lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7311{
7312 struct lpfc_hba *phba;
7313 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007314 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007315 int error;
7316 uint32_t cfg_mode, intr_mode;
7317 int mcnt;
7318
7319 /* Allocate memory for HBA structure */
7320 phba = lpfc_hba_alloc(pdev);
7321 if (!phba)
7322 return -ENOMEM;
7323
7324 /* Perform generic PCI device enabling operation */
7325 error = lpfc_enable_pci_dev(phba);
7326 if (error) {
7327 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7328 "1409 Failed to enable pci device.\n");
7329 goto out_free_phba;
7330 }
7331
7332 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7333 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7334 if (error)
7335 goto out_disable_pci_dev;
7336
7337 /* Set up SLI-4 specific device PCI memory space */
7338 error = lpfc_sli4_pci_mem_setup(phba);
7339 if (error) {
7340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7341 "1410 Failed to set up pci memory space.\n");
7342 goto out_disable_pci_dev;
7343 }
7344
7345 /* Set up phase-1 common device driver resources */
7346 error = lpfc_setup_driver_resource_phase1(phba);
7347 if (error) {
7348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7349 "1411 Failed to set up driver resource.\n");
7350 goto out_unset_pci_mem_s4;
7351 }
7352
7353 /* Set up SLI-4 Specific device driver resources */
7354 error = lpfc_sli4_driver_resource_setup(phba);
7355 if (error) {
7356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7357 "1412 Failed to set up driver resource.\n");
7358 goto out_unset_pci_mem_s4;
7359 }
7360
7361 /* Initialize and populate the iocb list per host */
7362 error = lpfc_init_iocb_list(phba,
7363 phba->sli4_hba.max_cfg_param.max_xri);
7364 if (error) {
7365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7366 "1413 Failed to initialize iocb list.\n");
7367 goto out_unset_driver_resource_s4;
7368 }
7369
7370 /* Set up common device driver resources */
7371 error = lpfc_setup_driver_resource_phase2(phba);
7372 if (error) {
7373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7374 "1414 Failed to set up driver resource.\n");
7375 goto out_free_iocb_list;
7376 }
7377
7378 /* Create SCSI host to the physical port */
7379 error = lpfc_create_shost(phba);
7380 if (error) {
7381 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7382 "1415 Failed to create scsi host.\n");
7383 goto out_unset_driver_resource;
7384 }
7385
7386 /* Configure sysfs attributes */
7387 vport = phba->pport;
7388 error = lpfc_alloc_sysfs_attr(vport);
7389 if (error) {
7390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7391 "1416 Failed to allocate sysfs attr\n");
7392 goto out_destroy_shost;
7393 }
7394
James Smart6669f9b2009-10-02 15:16:45 -04007395 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04007396 /* Now, trying to enable interrupt and bring up the device */
7397 cfg_mode = phba->cfg_use_msi;
7398 while (true) {
7399 /* Put device to a known state before enabling interrupt */
7400 lpfc_stop_port(phba);
7401 /* Configure and enable interrupt */
7402 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7403 if (intr_mode == LPFC_INTR_ERROR) {
7404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7405 "0426 Failed to enable interrupt.\n");
7406 error = -ENODEV;
7407 goto out_free_sysfs_attr;
7408 }
7409 /* Set up SLI-4 HBA */
7410 if (lpfc_sli4_hba_setup(phba)) {
7411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7412 "1421 Failed to set up hba\n");
7413 error = -ENODEV;
7414 goto out_disable_intr;
7415 }
7416
7417 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7418 if (intr_mode != 0)
7419 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7420 LPFC_ACT_INTR_CNT);
7421
7422 /* Check active interrupts received only for MSI/MSI-X */
7423 if (intr_mode == 0 ||
7424 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7425 /* Log the current active interrupt mode */
7426 phba->intr_mode = intr_mode;
7427 lpfc_log_intr_mode(phba, intr_mode);
7428 break;
7429 }
7430 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7431 "0451 Configure interrupt mode (%d) "
7432 "failed active interrupt test.\n",
7433 intr_mode);
7434 /* Unset the preivous SLI-4 HBA setup */
7435 lpfc_sli4_unset_hba(phba);
7436 /* Try next level of interrupt mode */
7437 cfg_mode = --intr_mode;
7438 }
7439
7440 /* Perform post initialization setup */
7441 lpfc_post_init_setup(phba);
7442
James Smart1c6834a2009-07-19 10:01:26 -04007443 /* Check if there are static vports to be created. */
7444 lpfc_create_static_vport(phba);
7445
James Smartda0436e2009-05-22 14:51:39 -04007446 return 0;
7447
7448out_disable_intr:
7449 lpfc_sli4_disable_intr(phba);
7450out_free_sysfs_attr:
7451 lpfc_free_sysfs_attr(vport);
7452out_destroy_shost:
7453 lpfc_destroy_shost(phba);
7454out_unset_driver_resource:
7455 lpfc_unset_driver_resource_phase2(phba);
7456out_free_iocb_list:
7457 lpfc_free_iocb_list(phba);
7458out_unset_driver_resource_s4:
7459 lpfc_sli4_driver_resource_unset(phba);
7460out_unset_pci_mem_s4:
7461 lpfc_sli4_pci_mem_unset(phba);
7462out_disable_pci_dev:
7463 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007464 if (shost)
7465 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04007466out_free_phba:
7467 lpfc_hba_free(phba);
7468 return error;
7469}
7470
7471/**
7472 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
7473 * @pdev: pointer to PCI device
7474 *
7475 * This routine is called from the kernel's PCI subsystem to device with
7476 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7477 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7478 * device to be removed from the PCI subsystem properly.
7479 **/
7480static void __devexit
7481lpfc_pci_remove_one_s4(struct pci_dev *pdev)
7482{
7483 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7484 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7485 struct lpfc_vport **vports;
7486 struct lpfc_hba *phba = vport->phba;
7487 int i;
7488
7489 /* Mark the device unloading flag */
7490 spin_lock_irq(&phba->hbalock);
7491 vport->load_flag |= FC_UNLOADING;
7492 spin_unlock_irq(&phba->hbalock);
7493
7494 /* Free the HBA sysfs attributes */
7495 lpfc_free_sysfs_attr(vport);
7496
7497 /* Release all the vports against this physical port */
7498 vports = lpfc_create_vport_work_array(phba);
7499 if (vports != NULL)
7500 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7501 fc_vport_terminate(vports[i]->fc_vport);
7502 lpfc_destroy_vport_work_array(phba, vports);
7503
7504 /* Remove FC host and then SCSI host with the physical port */
7505 fc_remove_host(shost);
7506 scsi_remove_host(shost);
7507
7508 /* Perform cleanup on the physical port */
7509 lpfc_cleanup(vport);
7510
7511 /*
7512 * Bring down the SLI Layer. This step disables all interrupts,
7513 * clears the rings, discards all mailbox commands, and resets
7514 * the HBA FCoE function.
7515 */
7516 lpfc_debugfs_terminate(vport);
7517 lpfc_sli4_hba_unset(phba);
7518
7519 spin_lock_irq(&phba->hbalock);
7520 list_del_init(&vport->listentry);
7521 spin_unlock_irq(&phba->hbalock);
7522
7523 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7524 * buffers are released to their corresponding pools here.
7525 */
7526 lpfc_scsi_free(phba);
7527 lpfc_sli4_driver_resource_unset(phba);
7528
7529 /* Unmap adapter Control and Doorbell registers */
7530 lpfc_sli4_pci_mem_unset(phba);
7531
7532 /* Release PCI resources and disable device's PCI function */
7533 scsi_host_put(shost);
7534 lpfc_disable_pci_dev(phba);
7535
7536 /* Finally, free the driver's device data structure */
7537 lpfc_hba_free(phba);
7538
7539 return;
7540}
7541
7542/**
7543 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
7544 * @pdev: pointer to PCI device
7545 * @msg: power management message
7546 *
7547 * This routine is called from the kernel's PCI subsystem to support system
7548 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7549 * this method, it quiesces the device by stopping the driver's worker
7550 * thread for the device, turning off device's interrupt and DMA, and bring
7551 * the device offline. Note that as the driver implements the minimum PM
7552 * requirements to a power-aware driver's PM support for suspend/resume -- all
7553 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7554 * method call will be treated as SUSPEND and the driver will fully
7555 * reinitialize its device during resume() method call, the driver will set
7556 * device to PCI_D3hot state in PCI config space instead of setting it
7557 * according to the @msg provided by the PM.
7558 *
7559 * Return code
7560 * 0 - driver suspended the device
7561 * Error otherwise
7562 **/
7563static int
7564lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
7565{
7566 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7567 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7568
7569 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7570 "0298 PCI device Power Management suspend.\n");
7571
7572 /* Bring down the device */
7573 lpfc_offline_prep(phba);
7574 lpfc_offline(phba);
7575 kthread_stop(phba->worker_thread);
7576
7577 /* Disable interrupt from device */
7578 lpfc_sli4_disable_intr(phba);
7579
7580 /* Save device state to PCI config space */
7581 pci_save_state(pdev);
7582 pci_set_power_state(pdev, PCI_D3hot);
7583
7584 return 0;
7585}
7586
7587/**
7588 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
7589 * @pdev: pointer to PCI device
7590 *
7591 * This routine is called from the kernel's PCI subsystem to support system
7592 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
7593 * this method, it restores the device's PCI config space state and fully
7594 * reinitializes the device and brings it online. Note that as the driver
7595 * implements the minimum PM requirements to a power-aware driver's PM for
7596 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7597 * to the suspend() method call will be treated as SUSPEND and the driver
7598 * will fully reinitialize its device during resume() method call, the device
7599 * will be set to PCI_D0 directly in PCI config space before restoring the
7600 * state.
7601 *
7602 * Return code
7603 * 0 - driver suspended the device
7604 * Error otherwise
7605 **/
7606static int
7607lpfc_pci_resume_one_s4(struct pci_dev *pdev)
7608{
7609 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7610 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7611 uint32_t intr_mode;
7612 int error;
7613
7614 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7615 "0292 PCI device Power Management resume.\n");
7616
7617 /* Restore device state from PCI config space */
7618 pci_set_power_state(pdev, PCI_D0);
7619 pci_restore_state(pdev);
7620 if (pdev->is_busmaster)
7621 pci_set_master(pdev);
7622
7623 /* Startup the kernel thread for this host adapter. */
7624 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7625 "lpfc_worker_%d", phba->brd_no);
7626 if (IS_ERR(phba->worker_thread)) {
7627 error = PTR_ERR(phba->worker_thread);
7628 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7629 "0293 PM resume failed to start worker "
7630 "thread: error=x%x.\n", error);
7631 return error;
7632 }
7633
7634 /* Configure and enable interrupt */
7635 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
7636 if (intr_mode == LPFC_INTR_ERROR) {
7637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7638 "0294 PM resume Failed to enable interrupt\n");
7639 return -EIO;
7640 } else
7641 phba->intr_mode = intr_mode;
7642
7643 /* Restart HBA and bring it online */
7644 lpfc_sli_brdrestart(phba);
7645 lpfc_online(phba);
7646
7647 /* Log the current active interrupt mode */
7648 lpfc_log_intr_mode(phba, phba->intr_mode);
7649
7650 return 0;
7651}
7652
7653/**
7654 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
7655 * @pdev: pointer to PCI device.
7656 * @state: the current PCI connection state.
7657 *
7658 * This routine is called from the PCI subsystem for error handling to device
7659 * with SLI-4 interface spec. This function is called by the PCI subsystem
7660 * after a PCI bus error affecting this device has been detected. When this
7661 * function is invoked, it will need to stop all the I/Os and interrupt(s)
7662 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
7663 * for the PCI subsystem to perform proper recovery as desired.
7664 *
7665 * Return codes
7666 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7667 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7668 **/
7669static pci_ers_result_t
7670lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
7671{
7672 return PCI_ERS_RESULT_NEED_RESET;
7673}
7674
7675/**
7676 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
7677 * @pdev: pointer to PCI device.
7678 *
7679 * This routine is called from the PCI subsystem for error handling to device
7680 * with SLI-4 interface spec. It is called after PCI bus has been reset to
7681 * restart the PCI card from scratch, as if from a cold-boot. During the
7682 * PCI subsystem error recovery, after the driver returns
7683 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7684 * recovery and then call this routine before calling the .resume method to
7685 * recover the device. This function will initialize the HBA device, enable
7686 * the interrupt, but it will just put the HBA to offline state without
7687 * passing any I/O traffic.
7688 *
7689 * Return codes
7690 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7691 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7692 */
7693static pci_ers_result_t
7694lpfc_io_slot_reset_s4(struct pci_dev *pdev)
7695{
7696 return PCI_ERS_RESULT_RECOVERED;
7697}
7698
7699/**
7700 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
7701 * @pdev: pointer to PCI device
7702 *
7703 * This routine is called from the PCI subsystem for error handling to device
7704 * with SLI-4 interface spec. It is called when kernel error recovery tells
7705 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7706 * error recovery. After this call, traffic can start to flow from this device
7707 * again.
7708 **/
7709static void
7710lpfc_io_resume_s4(struct pci_dev *pdev)
7711{
7712 return;
7713}
7714
7715/**
James Smart3772a992009-05-22 14:50:54 -04007716 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
7717 * @pdev: pointer to PCI device
7718 * @pid: pointer to PCI device identifier
7719 *
7720 * This routine is to be registered to the kernel's PCI subsystem. When an
7721 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
7722 * at PCI device-specific information of the device and driver to see if the
7723 * driver state that it can support this kind of device. If the match is
7724 * successful, the driver core invokes this routine. This routine dispatches
7725 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
7726 * do all the initialization that it needs to do to handle the HBA device
7727 * properly.
7728 *
7729 * Return code
7730 * 0 - driver can claim the device
7731 * negative value - driver can not claim the device
7732 **/
7733static int __devinit
7734lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
7735{
7736 int rc;
James Smart8fa38512009-07-19 10:01:03 -04007737 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04007738
James Smart8fa38512009-07-19 10:01:03 -04007739 if (pci_read_config_dword(pdev, LPFC_SLIREV_CONF_WORD, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04007740 return -ENODEV;
7741
James Smart8fa38512009-07-19 10:01:03 -04007742 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
7743 (bf_get(lpfc_sli_intf_rev, &intf) == LPFC_SLIREV_CONF_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04007744 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007745 else
James Smart3772a992009-05-22 14:50:54 -04007746 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007747
James Smart3772a992009-05-22 14:50:54 -04007748 return rc;
7749}
7750
7751/**
7752 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
7753 * @pdev: pointer to PCI device
7754 *
7755 * This routine is to be registered to the kernel's PCI subsystem. When an
7756 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
7757 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
7758 * remove routine, which will perform all the necessary cleanup for the
7759 * device to be removed from the PCI subsystem properly.
7760 **/
7761static void __devexit
7762lpfc_pci_remove_one(struct pci_dev *pdev)
7763{
7764 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7765 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7766
7767 switch (phba->pci_dev_grp) {
7768 case LPFC_PCI_DEV_LP:
7769 lpfc_pci_remove_one_s3(pdev);
7770 break;
James Smartda0436e2009-05-22 14:51:39 -04007771 case LPFC_PCI_DEV_OC:
7772 lpfc_pci_remove_one_s4(pdev);
7773 break;
James Smart3772a992009-05-22 14:50:54 -04007774 default:
7775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7776 "1424 Invalid PCI device group: 0x%x\n",
7777 phba->pci_dev_grp);
7778 break;
7779 }
7780 return;
7781}
7782
7783/**
7784 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
7785 * @pdev: pointer to PCI device
7786 * @msg: power management message
7787 *
7788 * This routine is to be registered to the kernel's PCI subsystem to support
7789 * system Power Management (PM). When PM invokes this method, it dispatches
7790 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
7791 * suspend the device.
7792 *
7793 * Return code
7794 * 0 - driver suspended the device
7795 * Error otherwise
7796 **/
7797static int
7798lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
7799{
7800 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7801 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7802 int rc = -ENODEV;
7803
7804 switch (phba->pci_dev_grp) {
7805 case LPFC_PCI_DEV_LP:
7806 rc = lpfc_pci_suspend_one_s3(pdev, msg);
7807 break;
James Smartda0436e2009-05-22 14:51:39 -04007808 case LPFC_PCI_DEV_OC:
7809 rc = lpfc_pci_suspend_one_s4(pdev, msg);
7810 break;
James Smart3772a992009-05-22 14:50:54 -04007811 default:
7812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7813 "1425 Invalid PCI device group: 0x%x\n",
7814 phba->pci_dev_grp);
7815 break;
7816 }
7817 return rc;
7818}
7819
7820/**
7821 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
7822 * @pdev: pointer to PCI device
7823 *
7824 * This routine is to be registered to the kernel's PCI subsystem to support
7825 * system Power Management (PM). When PM invokes this method, it dispatches
7826 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
7827 * resume the device.
7828 *
7829 * Return code
7830 * 0 - driver suspended the device
7831 * Error otherwise
7832 **/
7833static int
7834lpfc_pci_resume_one(struct pci_dev *pdev)
7835{
7836 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7837 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7838 int rc = -ENODEV;
7839
7840 switch (phba->pci_dev_grp) {
7841 case LPFC_PCI_DEV_LP:
7842 rc = lpfc_pci_resume_one_s3(pdev);
7843 break;
James Smartda0436e2009-05-22 14:51:39 -04007844 case LPFC_PCI_DEV_OC:
7845 rc = lpfc_pci_resume_one_s4(pdev);
7846 break;
James Smart3772a992009-05-22 14:50:54 -04007847 default:
7848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7849 "1426 Invalid PCI device group: 0x%x\n",
7850 phba->pci_dev_grp);
7851 break;
7852 }
7853 return rc;
7854}
7855
7856/**
7857 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
7858 * @pdev: pointer to PCI device.
7859 * @state: the current PCI connection state.
7860 *
7861 * This routine is registered to the PCI subsystem for error handling. This
7862 * function is called by the PCI subsystem after a PCI bus error affecting
7863 * this device has been detected. When this routine is invoked, it dispatches
7864 * the action to the proper SLI-3 or SLI-4 device error detected handling
7865 * routine, which will perform the proper error detected operation.
7866 *
7867 * Return codes
7868 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7869 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7870 **/
7871static pci_ers_result_t
7872lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7873{
7874 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7875 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7876 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7877
7878 switch (phba->pci_dev_grp) {
7879 case LPFC_PCI_DEV_LP:
7880 rc = lpfc_io_error_detected_s3(pdev, state);
7881 break;
James Smartda0436e2009-05-22 14:51:39 -04007882 case LPFC_PCI_DEV_OC:
7883 rc = lpfc_io_error_detected_s4(pdev, state);
7884 break;
James Smart3772a992009-05-22 14:50:54 -04007885 default:
7886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7887 "1427 Invalid PCI device group: 0x%x\n",
7888 phba->pci_dev_grp);
7889 break;
7890 }
7891 return rc;
7892}
7893
7894/**
7895 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
7896 * @pdev: pointer to PCI device.
7897 *
7898 * This routine is registered to the PCI subsystem for error handling. This
7899 * function is called after PCI bus has been reset to restart the PCI card
7900 * from scratch, as if from a cold-boot. When this routine is invoked, it
7901 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
7902 * routine, which will perform the proper device reset.
7903 *
7904 * Return codes
7905 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7906 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7907 **/
7908static pci_ers_result_t
7909lpfc_io_slot_reset(struct pci_dev *pdev)
7910{
7911 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7912 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7913 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7914
7915 switch (phba->pci_dev_grp) {
7916 case LPFC_PCI_DEV_LP:
7917 rc = lpfc_io_slot_reset_s3(pdev);
7918 break;
James Smartda0436e2009-05-22 14:51:39 -04007919 case LPFC_PCI_DEV_OC:
7920 rc = lpfc_io_slot_reset_s4(pdev);
7921 break;
James Smart3772a992009-05-22 14:50:54 -04007922 default:
7923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7924 "1428 Invalid PCI device group: 0x%x\n",
7925 phba->pci_dev_grp);
7926 break;
7927 }
7928 return rc;
7929}
7930
7931/**
7932 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
7933 * @pdev: pointer to PCI device
7934 *
7935 * This routine is registered to the PCI subsystem for error handling. It
7936 * is called when kernel error recovery tells the lpfc driver that it is
7937 * OK to resume normal PCI operation after PCI bus error recovery. When
7938 * this routine is invoked, it dispatches the action to the proper SLI-3
7939 * or SLI-4 device io_resume routine, which will resume the device operation.
7940 **/
7941static void
7942lpfc_io_resume(struct pci_dev *pdev)
7943{
7944 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7945 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7946
7947 switch (phba->pci_dev_grp) {
7948 case LPFC_PCI_DEV_LP:
7949 lpfc_io_resume_s3(pdev);
7950 break;
James Smartda0436e2009-05-22 14:51:39 -04007951 case LPFC_PCI_DEV_OC:
7952 lpfc_io_resume_s4(pdev);
7953 break;
James Smart3772a992009-05-22 14:50:54 -04007954 default:
7955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7956 "1429 Invalid PCI device group: 0x%x\n",
7957 phba->pci_dev_grp);
7958 break;
7959 }
7960 return;
7961}
7962
dea31012005-04-17 16:05:31 -05007963static struct pci_device_id lpfc_id_table[] = {
7964 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
7965 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04007966 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
7967 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007968 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
7969 PCI_ANY_ID, PCI_ANY_ID, },
7970 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
7971 PCI_ANY_ID, PCI_ANY_ID, },
7972 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
7973 PCI_ANY_ID, PCI_ANY_ID, },
7974 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
7975 PCI_ANY_ID, PCI_ANY_ID, },
7976 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
7977 PCI_ANY_ID, PCI_ANY_ID, },
7978 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
7979 PCI_ANY_ID, PCI_ANY_ID, },
7980 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
7981 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007982 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
7983 PCI_ANY_ID, PCI_ANY_ID, },
7984 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
7985 PCI_ANY_ID, PCI_ANY_ID, },
7986 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
7987 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007988 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
7989 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05007990 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
7991 PCI_ANY_ID, PCI_ANY_ID, },
7992 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
7993 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05007994 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
7995 PCI_ANY_ID, PCI_ANY_ID, },
7996 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
7997 PCI_ANY_ID, PCI_ANY_ID, },
7998 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
7999 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008000 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8001 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008002 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8003 PCI_ANY_ID, PCI_ANY_ID, },
8004 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8005 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008006 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8007 PCI_ANY_ID, PCI_ANY_ID, },
8008 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8009 PCI_ANY_ID, PCI_ANY_ID, },
8010 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8011 PCI_ANY_ID, PCI_ANY_ID, },
8012 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8013 PCI_ANY_ID, PCI_ANY_ID, },
8014 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8015 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008016 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8017 PCI_ANY_ID, PCI_ANY_ID, },
8018 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8019 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008020 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8021 PCI_ANY_ID, PCI_ANY_ID, },
8022 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8023 PCI_ANY_ID, PCI_ANY_ID, },
8024 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8025 PCI_ANY_ID, PCI_ANY_ID, },
8026 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8027 PCI_ANY_ID, PCI_ANY_ID, },
8028 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8029 PCI_ANY_ID, PCI_ANY_ID, },
8030 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8031 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008032 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8033 PCI_ANY_ID, PCI_ANY_ID, },
8034 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8035 PCI_ANY_ID, PCI_ANY_ID, },
8036 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8037 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008038 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8039 PCI_ANY_ID, PCI_ANY_ID, },
James Smart6669f9b2009-10-02 15:16:45 -04008040 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TS_BE3,
8041 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008042 { 0 }
8043};
8044
8045MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8046
Linas Vepstas8d63f372007-02-14 14:28:36 -06008047static struct pci_error_handlers lpfc_err_handler = {
8048 .error_detected = lpfc_io_error_detected,
8049 .slot_reset = lpfc_io_slot_reset,
8050 .resume = lpfc_io_resume,
8051};
8052
dea31012005-04-17 16:05:31 -05008053static struct pci_driver lpfc_driver = {
8054 .name = LPFC_DRIVER_NAME,
8055 .id_table = lpfc_id_table,
8056 .probe = lpfc_pci_probe_one,
8057 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008058 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008059 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008060 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008061};
8062
James Smarte59058c2008-08-24 21:49:00 -04008063/**
James Smart3621a712009-04-06 18:47:14 -04008064 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008065 *
8066 * This routine is to be invoked when the lpfc module is loaded into the
8067 * kernel. The special kernel macro module_init() is used to indicate the
8068 * role of this routine to the kernel as lpfc module entry point.
8069 *
8070 * Return codes
8071 * 0 - successful
8072 * -ENOMEM - FC attach transport failed
8073 * all others - failed
8074 */
dea31012005-04-17 16:05:31 -05008075static int __init
8076lpfc_init(void)
8077{
8078 int error = 0;
8079
8080 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008081 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008082
James Smart7ee5d432007-10-27 13:37:17 -04008083 if (lpfc_enable_npiv) {
8084 lpfc_transport_functions.vport_create = lpfc_vport_create;
8085 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8086 }
dea31012005-04-17 16:05:31 -05008087 lpfc_transport_template =
8088 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008089 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008090 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008091 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008092 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008093 fc_attach_transport(&lpfc_vport_transport_functions);
8094 if (lpfc_vport_transport_template == NULL) {
8095 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008096 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008097 }
James Smart7ee5d432007-10-27 13:37:17 -04008098 }
dea31012005-04-17 16:05:31 -05008099 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008100 if (error) {
dea31012005-04-17 16:05:31 -05008101 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008102 if (lpfc_enable_npiv)
8103 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008104 }
dea31012005-04-17 16:05:31 -05008105
8106 return error;
8107}
8108
James Smarte59058c2008-08-24 21:49:00 -04008109/**
James Smart3621a712009-04-06 18:47:14 -04008110 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008111 *
8112 * This routine is invoked when the lpfc module is removed from the kernel.
8113 * The special kernel macro module_exit() is used to indicate the role of
8114 * this routine to the kernel as lpfc module exit point.
8115 */
dea31012005-04-17 16:05:31 -05008116static void __exit
8117lpfc_exit(void)
8118{
8119 pci_unregister_driver(&lpfc_driver);
8120 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008121 if (lpfc_enable_npiv)
8122 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008123 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008124 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8125 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008126 (1L << _dump_buf_data_order), _dump_buf_data);
8127 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8128 }
8129
8130 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008131 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8132 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008133 (1L << _dump_buf_dif_order), _dump_buf_dif);
8134 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8135 }
dea31012005-04-17 16:05:31 -05008136}
8137
8138module_init(lpfc_init);
8139module_exit(lpfc_exit);
8140MODULE_LICENSE("GPL");
8141MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8142MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8143MODULE_VERSION("0:" LPFC_DRIVER_VERSION);