blob: 6e04679167e3e04c5171a7a4da2f6922ac346300 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart4fede782010-01-26 23:08:55 -05004 * Copyright (C) 2004-2010 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040031#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050032
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040033#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050034#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_transport_fc.h>
37
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
40#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
44#include "lpfc_scsi.h"
45#include "lpfc.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_version.h"
50
James Smart81301a92008-12-04 22:39:46 -050051char *_dump_buf_data;
52unsigned long _dump_buf_data_order;
53char *_dump_buf_dif;
54unsigned long _dump_buf_dif_order;
55spinlock_t _dump_buf_lock;
56
dea31012005-04-17 16:05:31 -050057static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
58static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040059static int lpfc_sli4_queue_create(struct lpfc_hba *);
60static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
61static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
62static int lpfc_setup_endian_order(struct lpfc_hba *);
63static int lpfc_sli4_read_config(struct lpfc_hba *);
64static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
65static void lpfc_free_sgl_list(struct lpfc_hba *);
66static int lpfc_init_sgl_list(struct lpfc_hba *);
67static int lpfc_init_active_sgl_array(struct lpfc_hba *);
68static void lpfc_free_active_sgl(struct lpfc_hba *);
69static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
70static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
71static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
72static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
73static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050074
75static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050076static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050077static DEFINE_IDR(lpfc_hba_index);
78
James Smarte59058c2008-08-24 21:49:00 -040079/**
James Smart3621a712009-04-06 18:47:14 -040080 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040081 * @phba: pointer to lpfc hba data structure.
82 *
83 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
84 * mailbox command. It retrieves the revision information from the HBA and
85 * collects the Vital Product Data (VPD) about the HBA for preparing the
86 * configuration of the HBA.
87 *
88 * Return codes:
89 * 0 - success.
90 * -ERESTART - requests the SLI layer to reset the HBA and try again.
91 * Any other value - indicates an error.
92 **/
dea31012005-04-17 16:05:31 -050093int
James Smart2e0fef82007-06-17 19:56:36 -050094lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050095{
96 lpfc_vpd_t *vp = &phba->vpd;
97 int i = 0, rc;
98 LPFC_MBOXQ_t *pmb;
99 MAILBOX_t *mb;
100 char *lpfc_vpd_data = NULL;
101 uint16_t offset = 0;
102 static char licensed[56] =
103 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400104 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500105
106 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
107 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500108 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500109 return -ENOMEM;
110 }
111
James Smart04c68492009-05-22 14:52:52 -0400112 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500113 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500114
115 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400116 if (init_key) {
117 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500118
James Smart65a29c12006-07-06 15:50:50 -0400119 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
120 *ptext = cpu_to_be32(*ptext);
121 init_key = 0;
122 }
dea31012005-04-17 16:05:31 -0500123
124 lpfc_read_nv(phba, pmb);
125 memset((char*)mb->un.varRDnvp.rsvd3, 0,
126 sizeof (mb->un.varRDnvp.rsvd3));
127 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
128 sizeof (licensed));
129
130 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
131
132 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500133 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400134 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500135 "error, mbxCmd x%x READ_NVPARM, "
136 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500137 mb->mbxCommand, mb->mbxStatus);
138 mempool_free(pmb, phba->mbox_mem_pool);
139 return -ERESTART;
140 }
141 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500142 sizeof(phba->wwnn));
143 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
144 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500145 }
146
James Smart92d7f7b2007-06-17 19:56:38 -0500147 phba->sli3_options = 0x0;
148
dea31012005-04-17 16:05:31 -0500149 /* Setup and issue mailbox READ REV command */
150 lpfc_read_rev(phba, pmb);
151 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
152 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400154 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500155 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500156 mb->mbxCommand, mb->mbxStatus);
157 mempool_free( pmb, phba->mbox_mem_pool);
158 return -ERESTART;
159 }
160
James Smart92d7f7b2007-06-17 19:56:38 -0500161
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500162 /*
163 * The value of rr must be 1 since the driver set the cv field to 1.
164 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500165 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500166 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500167 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400169 "0440 Adapter failed to init, READ_REV has "
170 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500171 mempool_free(pmb, phba->mbox_mem_pool);
172 return -ERESTART;
dea31012005-04-17 16:05:31 -0500173 }
174
James Smart495a7142008-06-14 22:52:59 -0400175 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
176 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500177 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400178 }
James Smarted957682007-06-17 19:56:37 -0500179
dea31012005-04-17 16:05:31 -0500180 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500181 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500182 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500183 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
184 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
185 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
186 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500187 vp->rev.biuRev = mb->un.varRdRev.biuRev;
188 vp->rev.smRev = mb->un.varRdRev.smRev;
189 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
190 vp->rev.endecRev = mb->un.varRdRev.endecRev;
191 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
192 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
193 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
194 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
195 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
196 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
197
James Smart92d7f7b2007-06-17 19:56:38 -0500198 /* If the sli feature level is less then 9, we must
199 * tear down all RPIs and VPIs on link down if NPIV
200 * is enabled.
201 */
202 if (vp->rev.feaLevelHigh < 9)
203 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
204
dea31012005-04-17 16:05:31 -0500205 if (lpfc_is_LC_HBA(phba->pcidev->device))
206 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
207 sizeof (phba->RandomData));
208
dea31012005-04-17 16:05:31 -0500209 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500210 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
211 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400212 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500213
214 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400215 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500216 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
217
218 if (rc != MBX_SUCCESS) {
219 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400220 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500221 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500222 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500223 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500224 }
James Smart04c68492009-05-22 14:52:52 -0400225 /* dump mem may return a zero when finished or we got a
226 * mailbox error, either way we are done.
227 */
228 if (mb->un.varDmp.word_cnt == 0)
229 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500230 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
231 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400232 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
233 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500234 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500235 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500236 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
237 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500238
239 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500240out_free_mbox:
241 mempool_free(pmb, phba->mbox_mem_pool);
242 return 0;
243}
244
James Smarte59058c2008-08-24 21:49:00 -0400245/**
James Smart3621a712009-04-06 18:47:14 -0400246 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400247 * @phba: pointer to lpfc hba data structure.
248 * @pmboxq: pointer to the driver internal queue element for mailbox command.
249 *
250 * This is the completion handler for driver's configuring asynchronous event
251 * mailbox command to the device. If the mailbox command returns successfully,
252 * it will set internal async event support flag to 1; otherwise, it will
253 * set internal async event support flag to 0.
254 **/
James Smart57127f12007-10-27 13:37:05 -0400255static void
256lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
257{
James Smart04c68492009-05-22 14:52:52 -0400258 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400259 phba->temp_sensor_support = 1;
260 else
261 phba->temp_sensor_support = 0;
262 mempool_free(pmboxq, phba->mbox_mem_pool);
263 return;
264}
265
James Smarte59058c2008-08-24 21:49:00 -0400266/**
James Smart3621a712009-04-06 18:47:14 -0400267 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500268 * @phba: pointer to lpfc hba data structure.
269 * @pmboxq: pointer to the driver internal queue element for mailbox command.
270 *
271 * This is the completion handler for dump mailbox command for getting
272 * wake up parameters. When this command complete, the response contain
273 * Option rom version of the HBA. This function translate the version number
274 * into a human readable string and store it in OptionROMVersion.
275 **/
276static void
277lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
278{
279 struct prog_id *prg;
280 uint32_t prog_id_word;
281 char dist = ' ';
282 /* character array used for decoding dist type. */
283 char dist_char[] = "nabx";
284
James Smart04c68492009-05-22 14:52:52 -0400285 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500286 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500287 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500288 }
James Smart97207482008-12-04 22:39:19 -0500289
290 prg = (struct prog_id *) &prog_id_word;
291
292 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400293 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500294
295 /* Decode the Option rom version word to a readable string */
296 if (prg->dist < 4)
297 dist = dist_char[prg->dist];
298
299 if ((prg->dist == 3) && (prg->num == 0))
300 sprintf(phba->OptionROMVersion, "%d.%d%d",
301 prg->ver, prg->rev, prg->lev);
302 else
303 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
304 prg->ver, prg->rev, prg->lev,
305 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500306 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500307 return;
308}
309
310/**
James Smart3621a712009-04-06 18:47:14 -0400311 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400312 * @phba: pointer to lpfc hba data structure.
313 *
314 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
315 * command call. It performs all internal resource and state setups on the
316 * port: post IOCB buffers, enable appropriate host interrupt attentions,
317 * ELS ring timers, etc.
318 *
319 * Return codes
320 * 0 - success.
321 * Any other value - error.
322 **/
dea31012005-04-17 16:05:31 -0500323int
James Smart2e0fef82007-06-17 19:56:36 -0500324lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500325{
James Smart2e0fef82007-06-17 19:56:36 -0500326 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400327 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500328 LPFC_MBOXQ_t *pmb;
329 MAILBOX_t *mb;
330 struct lpfc_dmabuf *mp;
331 struct lpfc_sli *psli = &phba->sli;
332 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500333 int i, j;
334 int rc;
dea31012005-04-17 16:05:31 -0500335
James Smart7af67052007-10-27 13:38:11 -0400336 spin_lock_irq(&phba->hbalock);
337 /*
338 * If the Config port completed correctly the HBA is not
339 * over heated any more.
340 */
341 if (phba->over_temp_state == HBA_OVER_TEMP)
342 phba->over_temp_state = HBA_NORMAL_TEMP;
343 spin_unlock_irq(&phba->hbalock);
344
dea31012005-04-17 16:05:31 -0500345 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
346 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500347 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500348 return -ENOMEM;
349 }
James Smart04c68492009-05-22 14:52:52 -0400350 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500351
dea31012005-04-17 16:05:31 -0500352 /* Get login parameters for NID. */
James Smart92d7f7b2007-06-17 19:56:38 -0500353 lpfc_read_sparam(phba, pmb, 0);
James Smarted957682007-06-17 19:56:37 -0500354 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500355 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400357 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500358 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500359 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500360 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500361 mp = (struct lpfc_dmabuf *) pmb->context1;
362 mempool_free( pmb, phba->mbox_mem_pool);
363 lpfc_mbuf_free(phba, mp->virt, mp->phys);
364 kfree(mp);
365 return -EIO;
366 }
367
368 mp = (struct lpfc_dmabuf *) pmb->context1;
369
James Smart2e0fef82007-06-17 19:56:36 -0500370 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500371 lpfc_mbuf_free(phba, mp->virt, mp->phys);
372 kfree(mp);
373 pmb->context1 = NULL;
374
James Smarta12e07b2006-12-02 13:35:30 -0500375 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500376 u64_to_wwn(phba->cfg_soft_wwnn,
377 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400378 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500379 u64_to_wwn(phba->cfg_soft_wwpn,
380 vport->fc_sparam.portName.u.wwn);
381 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500382 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500383 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500384 sizeof (struct lpfc_name));
James Smarta257bf92009-04-06 18:48:10 -0400385
386 /* Update the fc_host data structures with new wwn. */
387 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
388 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400389 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400390
dea31012005-04-17 16:05:31 -0500391 /* If no serial number in VPD data, use low 6 bytes of WWNN */
392 /* This should be consolidated into parse_vpd ? - mr */
393 if (phba->SerialNumber[0] == 0) {
394 uint8_t *outptr;
395
James Smart2e0fef82007-06-17 19:56:36 -0500396 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500397 for (i = 0; i < 12; i++) {
398 status = *outptr++;
399 j = ((status & 0xf0) >> 4);
400 if (j <= 9)
401 phba->SerialNumber[i] =
402 (char)((uint8_t) 0x30 + (uint8_t) j);
403 else
404 phba->SerialNumber[i] =
405 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
406 i++;
407 j = (status & 0xf);
408 if (j <= 9)
409 phba->SerialNumber[i] =
410 (char)((uint8_t) 0x30 + (uint8_t) j);
411 else
412 phba->SerialNumber[i] =
413 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
414 }
415 }
416
dea31012005-04-17 16:05:31 -0500417 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500418 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500419 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400421 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500422 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500423 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500424 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500425 mempool_free( pmb, phba->mbox_mem_pool);
426 return -EIO;
427 }
428
James Smarta0c87cb2009-07-19 10:01:10 -0400429 /* Check if the port is disabled */
430 lpfc_sli_read_link_ste(phba);
431
dea31012005-04-17 16:05:31 -0500432 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
433 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
434 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400435 (mb->un.varRdConfig.max_xri + 1) -
436 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500437
438 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500439
440 /* Get the default values for Model Name and Description */
441 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
442
443 if ((phba->cfg_link_speed > LINK_SPEED_10G)
444 || ((phba->cfg_link_speed == LINK_SPEED_1G)
445 && !(phba->lmt & LMT_1Gb))
446 || ((phba->cfg_link_speed == LINK_SPEED_2G)
447 && !(phba->lmt & LMT_2Gb))
448 || ((phba->cfg_link_speed == LINK_SPEED_4G)
449 && !(phba->lmt & LMT_4Gb))
450 || ((phba->cfg_link_speed == LINK_SPEED_8G)
451 && !(phba->lmt & LMT_8Gb))
452 || ((phba->cfg_link_speed == LINK_SPEED_10G)
453 && !(phba->lmt & LMT_10Gb))) {
454 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500455 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400456 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500457 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500458 phba->cfg_link_speed);
459 phba->cfg_link_speed = LINK_SPEED_AUTO;
460 }
461
James Smart2e0fef82007-06-17 19:56:36 -0500462 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500463
James Smart0b727fe2007-10-27 13:37:25 -0400464 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500465 if (psli->ring[psli->extra_ring].cmdringaddr)
466 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500467 if (psli->ring[psli->fcp_ring].cmdringaddr)
468 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
469 if (psli->ring[psli->next_ring].cmdringaddr)
470 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
471
472 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500473 if (phba->sli_rev != 3)
474 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500475
James Smart93996272008-08-24 21:50:30 -0400476 /*
477 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
478 */
479 if (phba->intr_type == MSIX) {
480 rc = lpfc_config_msi(phba, pmb);
481 if (rc) {
482 mempool_free(pmb, phba->mbox_mem_pool);
483 return -EIO;
484 }
485 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
486 if (rc != MBX_SUCCESS) {
487 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
488 "0352 Config MSI mailbox command "
489 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400490 pmb->u.mb.mbxCommand,
491 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400492 mempool_free(pmb, phba->mbox_mem_pool);
493 return -EIO;
494 }
495 }
496
James Smart04c68492009-05-22 14:52:52 -0400497 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400498 /* Initialize ERATT handling flag */
499 phba->hba_flag &= ~HBA_ERATT_HANDLED;
500
dea31012005-04-17 16:05:31 -0500501 /* Enable appropriate host interrupts */
dea31012005-04-17 16:05:31 -0500502 status = readl(phba->HCregaddr);
503 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
504 if (psli->num_rings > 0)
505 status |= HC_R0INT_ENA;
506 if (psli->num_rings > 1)
507 status |= HC_R1INT_ENA;
508 if (psli->num_rings > 2)
509 status |= HC_R2INT_ENA;
510 if (psli->num_rings > 3)
511 status |= HC_R3INT_ENA;
512
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500513 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
514 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400515 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500516
dea31012005-04-17 16:05:31 -0500517 writel(status, phba->HCregaddr);
518 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500519 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500520
James Smart93996272008-08-24 21:50:30 -0400521 /* Set up ring-0 (ELS) timer */
522 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500523 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400524 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500525 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
526 phba->hb_outstanding = 0;
527 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400528 /* Set up error attention (ERATT) polling timer */
529 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500530
James Smarta0c87cb2009-07-19 10:01:10 -0400531 if (phba->hba_flag & LINK_DISABLED) {
532 lpfc_printf_log(phba,
533 KERN_ERR, LOG_INIT,
534 "2598 Adapter Link is disabled.\n");
535 lpfc_down_link(phba, pmb);
536 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
537 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
538 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
539 lpfc_printf_log(phba,
540 KERN_ERR, LOG_INIT,
541 "2599 Adapter failed to issue DOWN_LINK"
542 " mbox command rc 0x%x\n", rc);
543
544 mempool_free(pmb, phba->mbox_mem_pool);
545 return -EIO;
546 }
547 } else {
548 lpfc_init_link(phba, pmb, phba->cfg_topology,
549 phba->cfg_link_speed);
550 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
551 lpfc_set_loopback_flag(phba);
552 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
553 if (rc != MBX_SUCCESS) {
554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400555 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500556 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500557 mb->mbxCommand, mb->mbxStatus);
558
James Smarta0c87cb2009-07-19 10:01:10 -0400559 /* Clear all interrupt enable conditions */
560 writel(0, phba->HCregaddr);
561 readl(phba->HCregaddr); /* flush */
562 /* Clear all pending interrupts */
563 writel(0xffffffff, phba->HAregaddr);
564 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500565
James Smarta0c87cb2009-07-19 10:01:10 -0400566 phba->link_state = LPFC_HBA_ERROR;
567 if (rc != MBX_BUSY)
568 mempool_free(pmb, phba->mbox_mem_pool);
569 return -EIO;
570 }
dea31012005-04-17 16:05:31 -0500571 }
572 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400573 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
574 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
575 pmb->mbox_cmpl = lpfc_config_async_cmpl;
576 pmb->vport = phba->pport;
577 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500578
James Smart57127f12007-10-27 13:37:05 -0400579 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
580 lpfc_printf_log(phba,
581 KERN_ERR,
582 LOG_INIT,
583 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400584 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400585 rc);
586 mempool_free(pmb, phba->mbox_mem_pool);
587 }
James Smart97207482008-12-04 22:39:19 -0500588
589 /* Get Option rom version */
590 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
591 lpfc_dump_wakeup_param(phba, pmb);
592 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
593 pmb->vport = phba->pport;
594 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
595
596 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400598 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500599 mempool_free(pmb, phba->mbox_mem_pool);
600 }
601
James Smartd7c255b2008-08-24 21:50:00 -0400602 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400603}
604
James Smarte59058c2008-08-24 21:49:00 -0400605/**
James Smart3621a712009-04-06 18:47:14 -0400606 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400607 * @phba: pointer to lpfc HBA data structure.
608 *
609 * This routine will do LPFC uninitialization before the HBA is reset when
610 * bringing down the SLI Layer.
611 *
612 * Return codes
613 * 0 - success.
614 * Any other value - error.
615 **/
dea31012005-04-17 16:05:31 -0500616int
James Smart2e0fef82007-06-17 19:56:36 -0500617lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500618{
James Smart1b32f6a2008-02-08 18:49:39 -0500619 struct lpfc_vport **vports;
620 int i;
James Smart3772a992009-05-22 14:50:54 -0400621
622 if (phba->sli_rev <= LPFC_SLI_REV3) {
623 /* Disable interrupts */
624 writel(0, phba->HCregaddr);
625 readl(phba->HCregaddr); /* flush */
626 }
dea31012005-04-17 16:05:31 -0500627
James Smart1b32f6a2008-02-08 18:49:39 -0500628 if (phba->pport->load_flag & FC_UNLOADING)
629 lpfc_cleanup_discovery_resources(phba->pport);
630 else {
631 vports = lpfc_create_vport_work_array(phba);
632 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400633 for (i = 0; i <= phba->max_vports &&
634 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500635 lpfc_cleanup_discovery_resources(vports[i]);
636 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500637 }
638 return 0;
dea31012005-04-17 16:05:31 -0500639}
640
James Smarte59058c2008-08-24 21:49:00 -0400641/**
James Smart3772a992009-05-22 14:50:54 -0400642 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400643 * @phba: pointer to lpfc HBA data structure.
644 *
645 * This routine will do uninitialization after the HBA is reset when bring
646 * down the SLI Layer.
647 *
648 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200649 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400650 * Any other value - error.
651 **/
James Smart3772a992009-05-22 14:50:54 -0400652static int
653lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500654{
655 struct lpfc_sli *psli = &phba->sli;
656 struct lpfc_sli_ring *pring;
657 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500658 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500659 int i;
660
James Smart92d7f7b2007-06-17 19:56:38 -0500661 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
662 lpfc_sli_hbqbuf_free_all(phba);
663 else {
664 /* Cleanup preposted buffers on the ELS ring */
665 pring = &psli->ring[LPFC_ELS_RING];
666 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
667 list_del(&mp->list);
668 pring->postbufq_cnt--;
669 lpfc_mbuf_free(phba, mp->virt, mp->phys);
670 kfree(mp);
671 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500672 }
673
James Smart09372822008-01-11 01:52:54 -0500674 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500675 for (i = 0; i < psli->num_rings; i++) {
676 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500677
678 /* At this point in time the HBA is either reset or DOA. Either
679 * way, nothing should be on txcmplq as it will NEVER complete.
680 */
681 list_splice_init(&pring->txcmplq, &completions);
682 pring->txcmplq_cnt = 0;
683 spin_unlock_irq(&phba->hbalock);
684
James Smarta257bf92009-04-06 18:48:10 -0400685 /* Cancel all the IOCBs from the completions list */
686 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
687 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500688
Jamie Wellnitz41415862006-02-28 19:25:27 -0500689 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500690 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500691 }
James Smart09372822008-01-11 01:52:54 -0500692 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500693
694 return 0;
695}
James Smartda0436e2009-05-22 14:51:39 -0400696/**
697 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
698 * @phba: pointer to lpfc HBA data structure.
699 *
700 * This routine will do uninitialization after the HBA is reset when bring
701 * down the SLI Layer.
702 *
703 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200704 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400705 * Any other value - error.
706 **/
707static int
708lpfc_hba_down_post_s4(struct lpfc_hba *phba)
709{
710 struct lpfc_scsi_buf *psb, *psb_next;
711 LIST_HEAD(aborts);
712 int ret;
713 unsigned long iflag = 0;
714 ret = lpfc_hba_down_post_s3(phba);
715 if (ret)
716 return ret;
717 /* At this point in time the HBA is either reset or DOA. Either
718 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
719 * on the lpfc_sgl_list so that it can either be freed if the
720 * driver is unloading or reposted if the driver is restarting
721 * the port.
722 */
723 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
724 /* scsl_buf_list */
725 /* abts_sgl_list_lock required because worker thread uses this
726 * list.
727 */
728 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
729 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
730 &phba->sli4_hba.lpfc_sgl_list);
731 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
732 /* abts_scsi_buf_list_lock required because worker thread uses this
733 * list.
734 */
735 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
736 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
737 &aborts);
738 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
739 spin_unlock_irq(&phba->hbalock);
740
741 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
742 psb->pCmd = NULL;
743 psb->status = IOSTAT_SUCCESS;
744 }
745 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
746 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
747 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
748 return 0;
749}
750
751/**
752 * lpfc_hba_down_post - Wrapper func for hba down post routine
753 * @phba: pointer to lpfc HBA data structure.
754 *
755 * This routine wraps the actual SLI3 or SLI4 routine for performing
756 * uninitialization after the HBA is reset when bring down the SLI Layer.
757 *
758 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200759 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400760 * Any other value - error.
761 **/
762int
763lpfc_hba_down_post(struct lpfc_hba *phba)
764{
765 return (*phba->lpfc_hba_down_post)(phba);
766}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500767
James Smarte59058c2008-08-24 21:49:00 -0400768/**
James Smart3621a712009-04-06 18:47:14 -0400769 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400770 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
771 *
772 * This is the HBA-timer timeout handler registered to the lpfc driver. When
773 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
774 * work-port-events bitmap and the worker thread is notified. This timeout
775 * event will be used by the worker thread to invoke the actual timeout
776 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
777 * be performed in the timeout handler and the HBA timeout event bit shall
778 * be cleared by the worker thread after it has taken the event bitmap out.
779 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100780static void
James Smart858c9f62007-06-17 19:56:39 -0500781lpfc_hb_timeout(unsigned long ptr)
782{
783 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400784 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500785 unsigned long iflag;
786
787 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400788
789 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500790 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400791 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
792 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500793 phba->pport->work_port_events |= WORKER_HB_TMO;
794 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
795
James Smart93996272008-08-24 21:50:30 -0400796 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400797 if (!tmo_posted)
798 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500799 return;
800}
801
James Smarte59058c2008-08-24 21:49:00 -0400802/**
James Smart3621a712009-04-06 18:47:14 -0400803 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400804 * @phba: pointer to lpfc hba data structure.
805 * @pmboxq: pointer to the driver internal queue element for mailbox command.
806 *
807 * This is the callback function to the lpfc heart-beat mailbox command.
808 * If configured, the lpfc driver issues the heart-beat mailbox command to
809 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
810 * heart-beat mailbox command is issued, the driver shall set up heart-beat
811 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
812 * heart-beat outstanding state. Once the mailbox command comes back and
813 * no error conditions detected, the heart-beat mailbox command timer is
814 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
815 * state is cleared for the next heart-beat. If the timer expired with the
816 * heart-beat outstanding state set, the driver will put the HBA offline.
817 **/
James Smart858c9f62007-06-17 19:56:39 -0500818static void
819lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
820{
821 unsigned long drvr_flag;
822
823 spin_lock_irqsave(&phba->hbalock, drvr_flag);
824 phba->hb_outstanding = 0;
825 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
826
James Smart93996272008-08-24 21:50:30 -0400827 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500828 mempool_free(pmboxq, phba->mbox_mem_pool);
829 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
830 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400831 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500832 mod_timer(&phba->hb_tmofunc,
833 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
834 return;
835}
836
James Smarte59058c2008-08-24 21:49:00 -0400837/**
James Smart3621a712009-04-06 18:47:14 -0400838 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400839 * @phba: pointer to lpfc hba data structure.
840 *
841 * This is the actual HBA-timer timeout handler to be invoked by the worker
842 * thread whenever the HBA timer fired and HBA-timeout event posted. This
843 * handler performs any periodic operations needed for the device. If such
844 * periodic event has already been attended to either in the interrupt handler
845 * or by processing slow-ring or fast-ring events within the HBA-timer
846 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
847 * the timer for the next timeout period. If lpfc heart-beat mailbox command
848 * is configured and there is no heart-beat mailbox command outstanding, a
849 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
850 * has been a heart-beat mailbox command outstanding, the HBA shall be put
851 * to offline.
852 **/
James Smart858c9f62007-06-17 19:56:39 -0500853void
854lpfc_hb_timeout_handler(struct lpfc_hba *phba)
855{
James Smart45ed1192009-10-02 15:17:02 -0400856 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -0500857 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500858 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -0400859 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -0500860 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500861 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500862
James Smart45ed1192009-10-02 15:17:02 -0400863 vports = lpfc_create_vport_work_array(phba);
864 if (vports != NULL)
865 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
866 lpfc_rcv_seq_check_edtov(vports[i]);
867 lpfc_destroy_vport_work_array(phba, vports);
868
James Smart858c9f62007-06-17 19:56:39 -0500869 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400870 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500871 (phba->pport->fc_flag & FC_OFFLINE_MODE))
872 return;
873
874 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -0500875
876 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
877 jiffies)) {
878 spin_unlock_irq(&phba->pport->work_port_lock);
879 if (!phba->hb_outstanding)
880 mod_timer(&phba->hb_tmofunc,
881 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
882 else
883 mod_timer(&phba->hb_tmofunc,
884 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
885 return;
886 }
887 spin_unlock_irq(&phba->pport->work_port_lock);
888
James Smart0ff10d42008-01-11 01:52:36 -0500889 if (phba->elsbuf_cnt &&
890 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
891 spin_lock_irq(&phba->hbalock);
892 list_splice_init(&phba->elsbuf, &completions);
893 phba->elsbuf_cnt = 0;
894 phba->elsbuf_prev_cnt = 0;
895 spin_unlock_irq(&phba->hbalock);
896
897 while (!list_empty(&completions)) {
898 list_remove_head(&completions, buf_ptr,
899 struct lpfc_dmabuf, list);
900 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
901 kfree(buf_ptr);
902 }
903 }
904 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
905
James Smart858c9f62007-06-17 19:56:39 -0500906 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -0500907 if (phba->cfg_enable_hba_heartbeat) {
908 if (!phba->hb_outstanding) {
909 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
910 if (!pmboxq) {
911 mod_timer(&phba->hb_tmofunc,
912 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
913 return;
914 }
915
916 lpfc_heart_beat(phba, pmboxq);
917 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
918 pmboxq->vport = phba->pport;
919 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
920
921 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
922 mempool_free(pmboxq, phba->mbox_mem_pool);
923 mod_timer(&phba->hb_tmofunc,
924 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
925 return;
926 }
James Smart858c9f62007-06-17 19:56:39 -0500927 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -0500928 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
929 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -0500930 return;
James Smart13815c82008-01-11 01:52:48 -0500931 } else {
932 /*
933 * If heart beat timeout called with hb_outstanding set
934 * we need to take the HBA offline.
935 */
936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
937 "0459 Adapter heartbeat failure, "
938 "taking this port offline.\n");
939
940 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400941 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart13815c82008-01-11 01:52:48 -0500942 spin_unlock_irq(&phba->hbalock);
943
944 lpfc_offline_prep(phba);
945 lpfc_offline(phba);
946 lpfc_unblock_mgmt_io(phba);
947 phba->link_state = LPFC_HBA_ERROR;
948 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -0500949 }
James Smart858c9f62007-06-17 19:56:39 -0500950 }
951}
952
James Smarte59058c2008-08-24 21:49:00 -0400953/**
James Smart3621a712009-04-06 18:47:14 -0400954 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -0400955 * @phba: pointer to lpfc hba data structure.
956 *
957 * This routine is called to bring the HBA offline when HBA hardware error
958 * other than Port Error 6 has been detected.
959 **/
James Smart09372822008-01-11 01:52:54 -0500960static void
961lpfc_offline_eratt(struct lpfc_hba *phba)
962{
963 struct lpfc_sli *psli = &phba->sli;
964
965 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400966 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -0500967 spin_unlock_irq(&phba->hbalock);
968 lpfc_offline_prep(phba);
969
970 lpfc_offline(phba);
971 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400972 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500973 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400974 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500975 lpfc_hba_down_post(phba);
976 lpfc_sli_brdready(phba, HS_MBRDY);
977 lpfc_unblock_mgmt_io(phba);
978 phba->link_state = LPFC_HBA_ERROR;
979 return;
980}
981
James Smarte59058c2008-08-24 21:49:00 -0400982/**
James Smartda0436e2009-05-22 14:51:39 -0400983 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
984 * @phba: pointer to lpfc hba data structure.
985 *
986 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
987 * other than Port Error 6 has been detected.
988 **/
989static void
990lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
991{
992 lpfc_offline_prep(phba);
993 lpfc_offline(phba);
994 lpfc_sli4_brdreset(phba);
995 lpfc_hba_down_post(phba);
996 lpfc_sli4_post_status_check(phba);
997 lpfc_unblock_mgmt_io(phba);
998 phba->link_state = LPFC_HBA_ERROR;
999}
1000
1001/**
James Smarta257bf92009-04-06 18:48:10 -04001002 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1003 * @phba: pointer to lpfc hba data structure.
1004 *
1005 * This routine is invoked to handle the deferred HBA hardware error
1006 * conditions. This type of error is indicated by HBA by setting ER1
1007 * and another ER bit in the host status register. The driver will
1008 * wait until the ER1 bit clears before handling the error condition.
1009 **/
1010static void
1011lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1012{
1013 uint32_t old_host_status = phba->work_hs;
1014 struct lpfc_sli_ring *pring;
1015 struct lpfc_sli *psli = &phba->sli;
1016
James Smartf4b4c682009-05-22 14:53:12 -04001017 /* If the pci channel is offline, ignore possible errors,
1018 * since we cannot communicate with the pci card anyway.
1019 */
1020 if (pci_channel_offline(phba->pcidev)) {
1021 spin_lock_irq(&phba->hbalock);
1022 phba->hba_flag &= ~DEFER_ERATT;
1023 spin_unlock_irq(&phba->hbalock);
1024 return;
1025 }
1026
James Smarta257bf92009-04-06 18:48:10 -04001027 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1028 "0479 Deferred Adapter Hardware Error "
1029 "Data: x%x x%x x%x\n",
1030 phba->work_hs,
1031 phba->work_status[0], phba->work_status[1]);
1032
1033 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001034 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001035 spin_unlock_irq(&phba->hbalock);
1036
1037
1038 /*
1039 * Firmware stops when it triggred erratt. That could cause the I/Os
1040 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1041 * SCSI layer retry it after re-establishing link.
1042 */
1043 pring = &psli->ring[psli->fcp_ring];
1044 lpfc_sli_abort_iocb_ring(phba, pring);
1045
1046 /*
1047 * There was a firmware error. Take the hba offline and then
1048 * attempt to restart it.
1049 */
1050 lpfc_offline_prep(phba);
1051 lpfc_offline(phba);
1052
1053 /* Wait for the ER1 bit to clear.*/
1054 while (phba->work_hs & HS_FFER1) {
1055 msleep(100);
1056 phba->work_hs = readl(phba->HSregaddr);
1057 /* If driver is unloading let the worker thread continue */
1058 if (phba->pport->load_flag & FC_UNLOADING) {
1059 phba->work_hs = 0;
1060 break;
1061 }
1062 }
1063
1064 /*
1065 * This is to ptrotect against a race condition in which
1066 * first write to the host attention register clear the
1067 * host status register.
1068 */
1069 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1070 phba->work_hs = old_host_status & ~HS_FFER1;
1071
James Smart3772a992009-05-22 14:50:54 -04001072 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001073 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001074 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001075 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1076 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1077}
1078
James Smart3772a992009-05-22 14:50:54 -04001079static void
1080lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1081{
1082 struct lpfc_board_event_header board_event;
1083 struct Scsi_Host *shost;
1084
1085 board_event.event_type = FC_REG_BOARD_EVENT;
1086 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1087 shost = lpfc_shost_from_vport(phba->pport);
1088 fc_host_post_vendor_event(shost, fc_get_event_number(),
1089 sizeof(board_event),
1090 (char *) &board_event,
1091 LPFC_NL_VENDOR_ID);
1092}
1093
James Smarta257bf92009-04-06 18:48:10 -04001094/**
James Smart3772a992009-05-22 14:50:54 -04001095 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001096 * @phba: pointer to lpfc hba data structure.
1097 *
1098 * This routine is invoked to handle the following HBA hardware error
1099 * conditions:
1100 * 1 - HBA error attention interrupt
1101 * 2 - DMA ring index out of range
1102 * 3 - Mailbox command came back as unknown
1103 **/
James Smart3772a992009-05-22 14:50:54 -04001104static void
1105lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001106{
James Smart2e0fef82007-06-17 19:56:36 -05001107 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001108 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001109 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001110 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001111 unsigned long temperature;
1112 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001113 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001114
Linas Vepstas8d63f372007-02-14 14:28:36 -06001115 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001116 * since we cannot communicate with the pci card anyway.
1117 */
1118 if (pci_channel_offline(phba->pcidev)) {
1119 spin_lock_irq(&phba->hbalock);
1120 phba->hba_flag &= ~DEFER_ERATT;
1121 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001122 return;
James Smart3772a992009-05-22 14:50:54 -04001123 }
1124
James Smart13815c82008-01-11 01:52:48 -05001125 /* If resets are disabled then leave the HBA alone and return */
1126 if (!phba->cfg_enable_hba_reset)
1127 return;
dea31012005-04-17 16:05:31 -05001128
James Smartea2151b2008-09-07 11:52:10 -04001129 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001130 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001131
James Smarta257bf92009-04-06 18:48:10 -04001132 if (phba->hba_flag & DEFER_ERATT)
1133 lpfc_handle_deferred_eratt(phba);
1134
James Smart97eab632008-04-07 10:16:05 -04001135 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -05001136 /* Re-establishing Link */
1137 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001138 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -05001139 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001140 phba->work_hs,
dea31012005-04-17 16:05:31 -05001141 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001142
James Smart92d7f7b2007-06-17 19:56:38 -05001143 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001144 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001145 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001146
1147 /*
1148 * Firmware stops when it triggled erratt with HS_FFER6.
1149 * That could cause the I/Os dropped by the firmware.
1150 * Error iocb (I/O) on txcmplq and let the SCSI layer
1151 * retry it after re-establishing link.
1152 */
1153 pring = &psli->ring[psli->fcp_ring];
1154 lpfc_sli_abort_iocb_ring(phba, pring);
1155
dea31012005-04-17 16:05:31 -05001156 /*
1157 * There was a firmware error. Take the hba offline and then
1158 * attempt to restart it.
1159 */
James Smart46fa3112007-04-25 09:51:45 -04001160 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001161 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001162 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001163 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001164 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001165 return;
1166 }
James Smart46fa3112007-04-25 09:51:45 -04001167 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001168 } else if (phba->work_hs & HS_CRIT_TEMP) {
1169 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1170 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1171 temp_event_data.event_code = LPFC_CRIT_TEMP;
1172 temp_event_data.data = (uint32_t)temperature;
1173
1174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001175 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001176 "(%ld), taking this port offline "
1177 "Data: x%x x%x x%x\n",
1178 temperature, phba->work_hs,
1179 phba->work_status[0], phba->work_status[1]);
1180
1181 shost = lpfc_shost_from_vport(phba->pport);
1182 fc_host_post_vendor_event(shost, fc_get_event_number(),
1183 sizeof(temp_event_data),
1184 (char *) &temp_event_data,
1185 SCSI_NL_VID_TYPE_PCI
1186 | PCI_VENDOR_ID_EMULEX);
1187
James Smart7af67052007-10-27 13:38:11 -04001188 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001189 phba->over_temp_state = HBA_OVER_TEMP;
1190 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001191 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001192
dea31012005-04-17 16:05:31 -05001193 } else {
1194 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001195 * failure is a value other than FFER6. Do not call the offline
1196 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001197 */
1198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001199 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001200 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001201 phba->work_hs,
dea31012005-04-17 16:05:31 -05001202 phba->work_status[0], phba->work_status[1]);
1203
James Smartd2873e42006-08-18 17:46:43 -04001204 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001205 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001206 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001207 sizeof(event_data), (char *) &event_data,
1208 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1209
James Smart09372822008-01-11 01:52:54 -05001210 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001211 }
James Smart93996272008-08-24 21:50:30 -04001212 return;
dea31012005-04-17 16:05:31 -05001213}
1214
James Smarte59058c2008-08-24 21:49:00 -04001215/**
James Smartda0436e2009-05-22 14:51:39 -04001216 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1217 * @phba: pointer to lpfc hba data structure.
1218 *
1219 * This routine is invoked to handle the SLI4 HBA hardware error attention
1220 * conditions.
1221 **/
1222static void
1223lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1224{
1225 struct lpfc_vport *vport = phba->pport;
1226 uint32_t event_data;
1227 struct Scsi_Host *shost;
1228
1229 /* If the pci channel is offline, ignore possible errors, since
1230 * we cannot communicate with the pci card anyway.
1231 */
1232 if (pci_channel_offline(phba->pcidev))
1233 return;
1234 /* If resets are disabled then leave the HBA alone and return */
1235 if (!phba->cfg_enable_hba_reset)
1236 return;
1237
1238 /* Send an internal error event to mgmt application */
1239 lpfc_board_errevt_to_mgmt(phba);
1240
1241 /* For now, the actual action for SLI4 device handling is not
1242 * specified yet, just treated it as adaptor hardware failure
1243 */
1244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1245 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1246 phba->work_status[0], phba->work_status[1]);
1247
1248 event_data = FC_REG_DUMP_EVENT;
1249 shost = lpfc_shost_from_vport(vport);
1250 fc_host_post_vendor_event(shost, fc_get_event_number(),
1251 sizeof(event_data), (char *) &event_data,
1252 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1253
1254 lpfc_sli4_offline_eratt(phba);
1255}
1256
1257/**
1258 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1259 * @phba: pointer to lpfc HBA data structure.
1260 *
1261 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1262 * routine from the API jump table function pointer from the lpfc_hba struct.
1263 *
1264 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001265 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001266 * Any other value - error.
1267 **/
1268void
1269lpfc_handle_eratt(struct lpfc_hba *phba)
1270{
1271 (*phba->lpfc_handle_eratt)(phba);
1272}
1273
1274/**
James Smart3621a712009-04-06 18:47:14 -04001275 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001276 * @phba: pointer to lpfc hba data structure.
1277 *
1278 * This routine is invoked from the worker thread to handle a HBA host
1279 * attention link event.
1280 **/
dea31012005-04-17 16:05:31 -05001281void
James Smart2e0fef82007-06-17 19:56:36 -05001282lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001283{
James Smart2e0fef82007-06-17 19:56:36 -05001284 struct lpfc_vport *vport = phba->pport;
1285 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001286 LPFC_MBOXQ_t *pmb;
1287 volatile uint32_t control;
1288 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001289 int rc = 0;
dea31012005-04-17 16:05:31 -05001290
1291 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001292 if (!pmb) {
1293 rc = 1;
dea31012005-04-17 16:05:31 -05001294 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001295 }
dea31012005-04-17 16:05:31 -05001296
1297 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001298 if (!mp) {
1299 rc = 2;
dea31012005-04-17 16:05:31 -05001300 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001301 }
dea31012005-04-17 16:05:31 -05001302
1303 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001304 if (!mp->virt) {
1305 rc = 3;
dea31012005-04-17 16:05:31 -05001306 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001307 }
dea31012005-04-17 16:05:31 -05001308
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001309 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001310 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001311
1312 psli->slistat.link_event++;
1313 lpfc_read_la(phba, pmb, mp);
1314 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001315 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001316 /* Block ELS IOCBs until we have processed this mbox command */
1317 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001318 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001319 if (rc == MBX_NOT_FINISHED) {
1320 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001321 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001322 }
dea31012005-04-17 16:05:31 -05001323
1324 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001325 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001326 writel(HA_LATT, phba->HAregaddr);
1327 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001328 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001329
1330 return;
1331
James Smart14691152006-12-02 13:34:28 -05001332lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001333 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001334 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001335lpfc_handle_latt_free_mp:
1336 kfree(mp);
1337lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001338 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001339lpfc_handle_latt_err_exit:
1340 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001341 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001342 psli->sli_flag |= LPFC_PROCESS_LA;
1343 control = readl(phba->HCregaddr);
1344 control |= HC_LAINT_ENA;
1345 writel(control, phba->HCregaddr);
1346 readl(phba->HCregaddr); /* flush */
1347
1348 /* Clear Link Attention in HA REG */
1349 writel(HA_LATT, phba->HAregaddr);
1350 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001351 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001352 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001353 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001354
James Smart09372822008-01-11 01:52:54 -05001355 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1356 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001357
1358 return;
1359}
1360
James Smarte59058c2008-08-24 21:49:00 -04001361/**
James Smart3621a712009-04-06 18:47:14 -04001362 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001363 * @phba: pointer to lpfc hba data structure.
1364 * @vpd: pointer to the vital product data.
1365 * @len: length of the vital product data in bytes.
1366 *
1367 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1368 * an array of characters. In this routine, the ModelName, ProgramType, and
1369 * ModelDesc, etc. fields of the phba data structure will be populated.
1370 *
1371 * Return codes
1372 * 0 - pointer to the VPD passed in is NULL
1373 * 1 - success
1374 **/
James Smart3772a992009-05-22 14:50:54 -04001375int
James Smart2e0fef82007-06-17 19:56:36 -05001376lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001377{
1378 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001379 int Length;
dea31012005-04-17 16:05:31 -05001380 int i, j;
1381 int finished = 0;
1382 int index = 0;
1383
1384 if (!vpd)
1385 return 0;
1386
1387 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001388 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001389 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001390 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1391 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001392 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001393 switch (vpd[index]) {
1394 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001395 case 0x91:
dea31012005-04-17 16:05:31 -05001396 index += 1;
1397 lenlo = vpd[index];
1398 index += 1;
1399 lenhi = vpd[index];
1400 index += 1;
1401 i = ((((unsigned short)lenhi) << 8) + lenlo);
1402 index += i;
1403 break;
1404 case 0x90:
1405 index += 1;
1406 lenlo = vpd[index];
1407 index += 1;
1408 lenhi = vpd[index];
1409 index += 1;
1410 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001411 if (Length > len - index)
1412 Length = len - index;
dea31012005-04-17 16:05:31 -05001413 while (Length > 0) {
1414 /* Look for Serial Number */
1415 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1416 index += 2;
1417 i = vpd[index];
1418 index += 1;
1419 j = 0;
1420 Length -= (3+i);
1421 while(i--) {
1422 phba->SerialNumber[j++] = vpd[index++];
1423 if (j == 31)
1424 break;
1425 }
1426 phba->SerialNumber[j] = 0;
1427 continue;
1428 }
1429 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1430 phba->vpd_flag |= VPD_MODEL_DESC;
1431 index += 2;
1432 i = vpd[index];
1433 index += 1;
1434 j = 0;
1435 Length -= (3+i);
1436 while(i--) {
1437 phba->ModelDesc[j++] = vpd[index++];
1438 if (j == 255)
1439 break;
1440 }
1441 phba->ModelDesc[j] = 0;
1442 continue;
1443 }
1444 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1445 phba->vpd_flag |= VPD_MODEL_NAME;
1446 index += 2;
1447 i = vpd[index];
1448 index += 1;
1449 j = 0;
1450 Length -= (3+i);
1451 while(i--) {
1452 phba->ModelName[j++] = vpd[index++];
1453 if (j == 79)
1454 break;
1455 }
1456 phba->ModelName[j] = 0;
1457 continue;
1458 }
1459 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1460 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1461 index += 2;
1462 i = vpd[index];
1463 index += 1;
1464 j = 0;
1465 Length -= (3+i);
1466 while(i--) {
1467 phba->ProgramType[j++] = vpd[index++];
1468 if (j == 255)
1469 break;
1470 }
1471 phba->ProgramType[j] = 0;
1472 continue;
1473 }
1474 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1475 phba->vpd_flag |= VPD_PORT;
1476 index += 2;
1477 i = vpd[index];
1478 index += 1;
1479 j = 0;
1480 Length -= (3+i);
1481 while(i--) {
1482 phba->Port[j++] = vpd[index++];
1483 if (j == 19)
1484 break;
1485 }
1486 phba->Port[j] = 0;
1487 continue;
1488 }
1489 else {
1490 index += 2;
1491 i = vpd[index];
1492 index += 1;
1493 index += i;
1494 Length -= (3 + i);
1495 }
1496 }
1497 finished = 0;
1498 break;
1499 case 0x78:
1500 finished = 1;
1501 break;
1502 default:
1503 index ++;
1504 break;
1505 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001506 }
dea31012005-04-17 16:05:31 -05001507
1508 return(1);
1509}
1510
James Smarte59058c2008-08-24 21:49:00 -04001511/**
James Smart3621a712009-04-06 18:47:14 -04001512 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001513 * @phba: pointer to lpfc hba data structure.
1514 * @mdp: pointer to the data structure to hold the derived model name.
1515 * @descp: pointer to the data structure to hold the derived description.
1516 *
1517 * This routine retrieves HBA's description based on its registered PCI device
1518 * ID. The @descp passed into this function points to an array of 256 chars. It
1519 * shall be returned with the model name, maximum speed, and the host bus type.
1520 * The @mdp passed into this function points to an array of 80 chars. When the
1521 * function returns, the @mdp will be filled with the model name.
1522 **/
dea31012005-04-17 16:05:31 -05001523static void
James Smart2e0fef82007-06-17 19:56:36 -05001524lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001525{
1526 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001527 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001528 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001529 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001530 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001531 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001532 char *name;
1533 char *bus;
1534 char *function;
1535 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001536
1537 if (mdp && mdp[0] != '\0'
1538 && descp && descp[0] != '\0')
1539 return;
1540
1541 if (phba->lmt & LMT_10Gb)
1542 max_speed = 10;
1543 else if (phba->lmt & LMT_8Gb)
1544 max_speed = 8;
1545 else if (phba->lmt & LMT_4Gb)
1546 max_speed = 4;
1547 else if (phba->lmt & LMT_2Gb)
1548 max_speed = 2;
1549 else
1550 max_speed = 1;
dea31012005-04-17 16:05:31 -05001551
1552 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001553
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001554 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001555 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001556 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001557 break;
dea31012005-04-17 16:05:31 -05001558 case PCI_DEVICE_ID_SUPERFLY:
1559 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001560 m = (typeof(m)){"LP7000", "PCI",
1561 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001562 else
James Smarta747c9c2009-11-18 15:41:10 -05001563 m = (typeof(m)){"LP7000E", "PCI",
1564 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001565 break;
1566 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001567 m = (typeof(m)){"LP8000", "PCI",
1568 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001569 break;
1570 case PCI_DEVICE_ID_CENTAUR:
1571 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001572 m = (typeof(m)){"LP9002", "PCI",
1573 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001574 else
James Smarta747c9c2009-11-18 15:41:10 -05001575 m = (typeof(m)){"LP9000", "PCI",
1576 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001577 break;
1578 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001579 m = (typeof(m)){"LP952", "PCI",
1580 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001581 break;
1582 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001583 m = (typeof(m)){"LP9802", "PCI-X",
1584 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001585 break;
1586 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001587 m = (typeof(m)){"LP10000", "PCI-X",
1588 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001589 break;
1590 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001591 m = (typeof(m)){"LPX1000", "PCI-X",
1592 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001593 break;
1594 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001595 m = (typeof(m)){"LP982", "PCI-X",
1596 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001597 break;
1598 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001599 m = (typeof(m)){"LP1050", "PCI-X",
1600 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001601 break;
1602 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001603 m = (typeof(m)){"LP11000", "PCI-X2",
1604 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001605 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001606 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001607 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1608 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001609 break;
1610 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001611 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1612 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001613 break;
1614 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001615 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001616 break;
1617 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001618 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001619 break;
1620 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001621 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001622 break;
dea31012005-04-17 16:05:31 -05001623 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001624 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001625 break;
1626 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001627 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001628 break;
1629 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001630 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001631 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001632 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001633 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001634 break;
1635 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001636 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001637 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001638 break;
dea31012005-04-17 16:05:31 -05001639 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001640 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001641 break;
1642 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001643 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001644 break;
1645 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001646 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001647 break;
1648 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001649 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001650 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001651 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001652 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001653 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001654 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001655 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001656 break;
James Smartb87eab32007-04-25 09:53:28 -04001657 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001658 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001659 break;
1660 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001661 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001662 break;
1663 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001664 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001665 break;
1666 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001667 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001668 break;
1669 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001670 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001671 break;
1672 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001673 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001674 break;
James Smart84774a42008-08-24 21:50:06 -04001675 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001676 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001677 GE = 1;
1678 break;
1679 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001680 m = (typeof(m)){"LPev12000", "PCIe IOV",
1681 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001682 break;
1683 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001684 m = (typeof(m)){"LPev12000", "PCIe IOV",
1685 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001686 break;
1687 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001688 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1689 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001690 break;
James Smartda0436e2009-05-22 14:51:39 -04001691 case PCI_DEVICE_ID_TIGERSHARK:
1692 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001693 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04001694 break;
James Smarta747c9c2009-11-18 15:41:10 -05001695 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04001696 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001697 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1698 break;
1699 case PCI_DEVICE_ID_FALCON:
1700 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1701 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04001702 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001703 default:
James Smarta747c9c2009-11-18 15:41:10 -05001704 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001705 break;
dea31012005-04-17 16:05:31 -05001706 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001707
1708 if (mdp && mdp[0] == '\0')
1709 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001710 /* oneConnect hba requires special processing, they are all initiators
1711 * and we put the port number on the end
1712 */
1713 if (descp && descp[0] == '\0') {
1714 if (oneConnect)
1715 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05001716 "Emulex OneConnect %s, %s Initiator, Port %s",
1717 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04001718 phba->Port);
1719 else
1720 snprintf(descp, 255,
1721 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05001722 m.name, max_speed, (GE) ? "GE" : "Gb",
1723 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04001724 }
dea31012005-04-17 16:05:31 -05001725}
1726
James Smarte59058c2008-08-24 21:49:00 -04001727/**
James Smart3621a712009-04-06 18:47:14 -04001728 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001729 * @phba: pointer to lpfc hba data structure.
1730 * @pring: pointer to a IOCB ring.
1731 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1732 *
1733 * This routine posts a given number of IOCBs with the associated DMA buffer
1734 * descriptors specified by the cnt argument to the given IOCB ring.
1735 *
1736 * Return codes
1737 * The number of IOCBs NOT able to be posted to the IOCB ring.
1738 **/
dea31012005-04-17 16:05:31 -05001739int
James Smart495a7142008-06-14 22:52:59 -04001740lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001741{
1742 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001743 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001744 struct lpfc_dmabuf *mp1, *mp2;
1745
1746 cnt += pring->missbufcnt;
1747
1748 /* While there are buffers to post */
1749 while (cnt > 0) {
1750 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001751 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001752 if (iocb == NULL) {
1753 pring->missbufcnt = cnt;
1754 return cnt;
1755 }
dea31012005-04-17 16:05:31 -05001756 icmd = &iocb->iocb;
1757
1758 /* 2 buffers can be posted per command */
1759 /* Allocate buffer to post */
1760 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1761 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001762 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1763 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001764 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001765 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001766 pring->missbufcnt = cnt;
1767 return cnt;
1768 }
1769
1770 INIT_LIST_HEAD(&mp1->list);
1771 /* Allocate buffer to post */
1772 if (cnt > 1) {
1773 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1774 if (mp2)
1775 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1776 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001777 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001778 kfree(mp2);
dea31012005-04-17 16:05:31 -05001779 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1780 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001781 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001782 pring->missbufcnt = cnt;
1783 return cnt;
1784 }
1785
1786 INIT_LIST_HEAD(&mp2->list);
1787 } else {
1788 mp2 = NULL;
1789 }
1790
1791 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1792 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1793 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1794 icmd->ulpBdeCount = 1;
1795 cnt--;
1796 if (mp2) {
1797 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1798 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1799 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1800 cnt--;
1801 icmd->ulpBdeCount = 2;
1802 }
1803
1804 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1805 icmd->ulpLe = 1;
1806
James Smart3772a992009-05-22 14:50:54 -04001807 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1808 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001809 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1810 kfree(mp1);
1811 cnt++;
1812 if (mp2) {
1813 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1814 kfree(mp2);
1815 cnt++;
1816 }
James Bottomley604a3e32005-10-29 10:28:33 -05001817 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001818 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001819 return cnt;
1820 }
dea31012005-04-17 16:05:31 -05001821 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001822 if (mp2)
dea31012005-04-17 16:05:31 -05001823 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001824 }
1825 pring->missbufcnt = 0;
1826 return 0;
1827}
1828
James Smarte59058c2008-08-24 21:49:00 -04001829/**
James Smart3621a712009-04-06 18:47:14 -04001830 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001831 * @phba: pointer to lpfc hba data structure.
1832 *
1833 * This routine posts initial receive IOCB buffers to the ELS ring. The
1834 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1835 * set to 64 IOCBs.
1836 *
1837 * Return codes
1838 * 0 - success (currently always success)
1839 **/
dea31012005-04-17 16:05:31 -05001840static int
James Smart2e0fef82007-06-17 19:56:36 -05001841lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001842{
1843 struct lpfc_sli *psli = &phba->sli;
1844
1845 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001846 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001847 /* Ring 2 - FCP no buffers needed */
1848
1849 return 0;
1850}
1851
1852#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1853
James Smarte59058c2008-08-24 21:49:00 -04001854/**
James Smart3621a712009-04-06 18:47:14 -04001855 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001856 * @HashResultPointer: pointer to an array as hash table.
1857 *
1858 * This routine sets up the initial values to the array of hash table entries
1859 * for the LC HBAs.
1860 **/
dea31012005-04-17 16:05:31 -05001861static void
1862lpfc_sha_init(uint32_t * HashResultPointer)
1863{
1864 HashResultPointer[0] = 0x67452301;
1865 HashResultPointer[1] = 0xEFCDAB89;
1866 HashResultPointer[2] = 0x98BADCFE;
1867 HashResultPointer[3] = 0x10325476;
1868 HashResultPointer[4] = 0xC3D2E1F0;
1869}
1870
James Smarte59058c2008-08-24 21:49:00 -04001871/**
James Smart3621a712009-04-06 18:47:14 -04001872 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001873 * @HashResultPointer: pointer to an initial/result hash table.
1874 * @HashWorkingPointer: pointer to an working hash table.
1875 *
1876 * This routine iterates an initial hash table pointed by @HashResultPointer
1877 * with the values from the working hash table pointeed by @HashWorkingPointer.
1878 * The results are putting back to the initial hash table, returned through
1879 * the @HashResultPointer as the result hash table.
1880 **/
dea31012005-04-17 16:05:31 -05001881static void
1882lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1883{
1884 int t;
1885 uint32_t TEMP;
1886 uint32_t A, B, C, D, E;
1887 t = 16;
1888 do {
1889 HashWorkingPointer[t] =
1890 S(1,
1891 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1892 8] ^
1893 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1894 } while (++t <= 79);
1895 t = 0;
1896 A = HashResultPointer[0];
1897 B = HashResultPointer[1];
1898 C = HashResultPointer[2];
1899 D = HashResultPointer[3];
1900 E = HashResultPointer[4];
1901
1902 do {
1903 if (t < 20) {
1904 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1905 } else if (t < 40) {
1906 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1907 } else if (t < 60) {
1908 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1909 } else {
1910 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1911 }
1912 TEMP += S(5, A) + E + HashWorkingPointer[t];
1913 E = D;
1914 D = C;
1915 C = S(30, B);
1916 B = A;
1917 A = TEMP;
1918 } while (++t <= 79);
1919
1920 HashResultPointer[0] += A;
1921 HashResultPointer[1] += B;
1922 HashResultPointer[2] += C;
1923 HashResultPointer[3] += D;
1924 HashResultPointer[4] += E;
1925
1926}
1927
James Smarte59058c2008-08-24 21:49:00 -04001928/**
James Smart3621a712009-04-06 18:47:14 -04001929 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04001930 * @RandomChallenge: pointer to the entry of host challenge random number array.
1931 * @HashWorking: pointer to the entry of the working hash array.
1932 *
1933 * This routine calculates the working hash array referred by @HashWorking
1934 * from the challenge random numbers associated with the host, referred by
1935 * @RandomChallenge. The result is put into the entry of the working hash
1936 * array and returned by reference through @HashWorking.
1937 **/
dea31012005-04-17 16:05:31 -05001938static void
1939lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1940{
1941 *HashWorking = (*RandomChallenge ^ *HashWorking);
1942}
1943
James Smarte59058c2008-08-24 21:49:00 -04001944/**
James Smart3621a712009-04-06 18:47:14 -04001945 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04001946 * @phba: pointer to lpfc hba data structure.
1947 * @hbainit: pointer to an array of unsigned 32-bit integers.
1948 *
1949 * This routine performs the special handling for LC HBA initialization.
1950 **/
dea31012005-04-17 16:05:31 -05001951void
1952lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1953{
1954 int t;
1955 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001956 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001957
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001958 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001959 if (!HashWorking)
1960 return;
1961
dea31012005-04-17 16:05:31 -05001962 HashWorking[0] = HashWorking[78] = *pwwnn++;
1963 HashWorking[1] = HashWorking[79] = *pwwnn;
1964
1965 for (t = 0; t < 7; t++)
1966 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1967
1968 lpfc_sha_init(hbainit);
1969 lpfc_sha_iterate(hbainit, HashWorking);
1970 kfree(HashWorking);
1971}
1972
James Smarte59058c2008-08-24 21:49:00 -04001973/**
James Smart3621a712009-04-06 18:47:14 -04001974 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04001975 * @vport: pointer to a virtual N_Port data structure.
1976 *
1977 * This routine performs the necessary cleanups before deleting the @vport.
1978 * It invokes the discovery state machine to perform necessary state
1979 * transitions and to release the ndlps associated with the @vport. Note,
1980 * the physical port is treated as @vport 0.
1981 **/
James Smart87af33f2007-10-27 13:37:43 -04001982void
James Smart2e0fef82007-06-17 19:56:36 -05001983lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001984{
James Smart87af33f2007-10-27 13:37:43 -04001985 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001986 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04001987 int i = 0;
dea31012005-04-17 16:05:31 -05001988
James Smart87af33f2007-10-27 13:37:43 -04001989 if (phba->link_state > LPFC_LINK_DOWN)
1990 lpfc_port_link_failure(vport);
1991
1992 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001993 if (!NLP_CHK_NODE_ACT(ndlp)) {
1994 ndlp = lpfc_enable_node(vport, ndlp,
1995 NLP_STE_UNUSED_NODE);
1996 if (!ndlp)
1997 continue;
1998 spin_lock_irq(&phba->ndlp_lock);
1999 NLP_SET_FREE_REQ(ndlp);
2000 spin_unlock_irq(&phba->ndlp_lock);
2001 /* Trigger the release of the ndlp memory */
2002 lpfc_nlp_put(ndlp);
2003 continue;
2004 }
2005 spin_lock_irq(&phba->ndlp_lock);
2006 if (NLP_CHK_FREE_REQ(ndlp)) {
2007 /* The ndlp should not be in memory free mode already */
2008 spin_unlock_irq(&phba->ndlp_lock);
2009 continue;
2010 } else
2011 /* Indicate request for freeing ndlp memory */
2012 NLP_SET_FREE_REQ(ndlp);
2013 spin_unlock_irq(&phba->ndlp_lock);
2014
James Smart58da1ff2008-04-07 10:15:56 -04002015 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2016 ndlp->nlp_DID == Fabric_DID) {
2017 /* Just free up ndlp with Fabric_DID for vports */
2018 lpfc_nlp_put(ndlp);
2019 continue;
2020 }
2021
James Smart87af33f2007-10-27 13:37:43 -04002022 if (ndlp->nlp_type & NLP_FABRIC)
2023 lpfc_disc_state_machine(vport, ndlp, NULL,
2024 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002025
James Smart87af33f2007-10-27 13:37:43 -04002026 lpfc_disc_state_machine(vport, ndlp, NULL,
2027 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002028
James Smart87af33f2007-10-27 13:37:43 -04002029 }
2030
James Smarta8adb832007-10-27 13:37:53 -04002031 /* At this point, ALL ndlp's should be gone
2032 * because of the previous NLP_EVT_DEVICE_RM.
2033 * Lets wait for this to happen, if needed.
2034 */
James Smart87af33f2007-10-27 13:37:43 -04002035 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002036 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002037 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002038 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002039 list_for_each_entry_safe(ndlp, next_ndlp,
2040 &vport->fc_nodes, nlp_listp) {
2041 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2042 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002043 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002044 "usgmap:x%x refcnt:%d\n",
2045 ndlp->nlp_DID, (void *)ndlp,
2046 ndlp->nlp_usg_map,
2047 atomic_read(
2048 &ndlp->kref.refcount));
2049 }
James Smarta8adb832007-10-27 13:37:53 -04002050 break;
James Smart87af33f2007-10-27 13:37:43 -04002051 }
James Smarta8adb832007-10-27 13:37:53 -04002052
2053 /* Wait for any activity on ndlps to settle */
2054 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002055 }
dea31012005-04-17 16:05:31 -05002056}
2057
James Smarte59058c2008-08-24 21:49:00 -04002058/**
James Smart3621a712009-04-06 18:47:14 -04002059 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002060 * @vport: pointer to a virtual N_Port data structure.
2061 *
2062 * This routine stops all the timers associated with a @vport. This function
2063 * is invoked before disabling or deleting a @vport. Note that the physical
2064 * port is treated as @vport 0.
2065 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002066void
2067lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002068{
James Smart92d7f7b2007-06-17 19:56:38 -05002069 del_timer_sync(&vport->els_tmofunc);
2070 del_timer_sync(&vport->fc_fdmitmo);
2071 lpfc_can_disctmo(vport);
2072 return;
dea31012005-04-17 16:05:31 -05002073}
2074
James Smarte59058c2008-08-24 21:49:00 -04002075/**
James Smartecfd03c2010-02-12 14:41:27 -05002076 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2077 * @phba: pointer to lpfc hba data structure.
2078 *
2079 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2080 * caller of this routine should already hold the host lock.
2081 **/
2082void
2083__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2084{
2085 /* Clear pending FCF rediscovery wait timer */
2086 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2087 /* Now, try to stop the timer */
2088 del_timer(&phba->fcf.redisc_wait);
2089}
2090
2091/**
2092 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2093 * @phba: pointer to lpfc hba data structure.
2094 *
2095 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2096 * checks whether the FCF rediscovery wait timer is pending with the host
2097 * lock held before proceeding with disabling the timer and clearing the
2098 * wait timer pendig flag.
2099 **/
2100void
2101lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2102{
2103 spin_lock_irq(&phba->hbalock);
2104 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2105 /* FCF rediscovery timer already fired or stopped */
2106 spin_unlock_irq(&phba->hbalock);
2107 return;
2108 }
2109 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2110 spin_unlock_irq(&phba->hbalock);
2111}
2112
2113/**
James Smart3772a992009-05-22 14:50:54 -04002114 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002115 * @phba: pointer to lpfc hba data structure.
2116 *
2117 * This routine stops all the timers associated with a HBA. This function is
2118 * invoked before either putting a HBA offline or unloading the driver.
2119 **/
James Smart3772a992009-05-22 14:50:54 -04002120void
2121lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002122{
James Smart51ef4c22007-08-02 11:10:31 -04002123 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002124 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002125 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002126 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002127 del_timer_sync(&phba->hb_tmofunc);
2128 phba->hb_outstanding = 0;
2129
2130 switch (phba->pci_dev_grp) {
2131 case LPFC_PCI_DEV_LP:
2132 /* Stop any LightPulse device specific driver timers */
2133 del_timer_sync(&phba->fcp_poll_timer);
2134 break;
2135 case LPFC_PCI_DEV_OC:
2136 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002137 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002138 break;
2139 default:
2140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2141 "0297 Invalid device group (x%x)\n",
2142 phba->pci_dev_grp);
2143 break;
2144 }
James Smart2e0fef82007-06-17 19:56:36 -05002145 return;
dea31012005-04-17 16:05:31 -05002146}
2147
James Smarte59058c2008-08-24 21:49:00 -04002148/**
James Smart3621a712009-04-06 18:47:14 -04002149 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002150 * @phba: pointer to lpfc hba data structure.
2151 *
2152 * This routine marks a HBA's management interface as blocked. Once the HBA's
2153 * management interface is marked as blocked, all the user space access to
2154 * the HBA, whether they are from sysfs interface or libdfc interface will
2155 * all be blocked. The HBA is set to block the management interface when the
2156 * driver prepares the HBA interface for online or offline.
2157 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002158static void
2159lpfc_block_mgmt_io(struct lpfc_hba * phba)
2160{
2161 unsigned long iflag;
2162
2163 spin_lock_irqsave(&phba->hbalock, iflag);
2164 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2165 spin_unlock_irqrestore(&phba->hbalock, iflag);
2166}
2167
James Smarte59058c2008-08-24 21:49:00 -04002168/**
James Smart3621a712009-04-06 18:47:14 -04002169 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002170 * @phba: pointer to lpfc hba data structure.
2171 *
2172 * This routine initializes the HBA and brings a HBA online. During this
2173 * process, the management interface is blocked to prevent user space access
2174 * to the HBA interfering with the driver initialization.
2175 *
2176 * Return codes
2177 * 0 - successful
2178 * 1 - failed
2179 **/
dea31012005-04-17 16:05:31 -05002180int
James Smart2e0fef82007-06-17 19:56:36 -05002181lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002182{
Julia Lawall372bd282008-12-16 16:15:08 +01002183 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002184 struct lpfc_vport **vports;
2185 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002186
dea31012005-04-17 16:05:31 -05002187 if (!phba)
2188 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002189 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002190
James Smart2e0fef82007-06-17 19:56:36 -05002191 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002192 return 0;
2193
James Smarted957682007-06-17 19:56:37 -05002194 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002195 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002196
James Smart46fa3112007-04-25 09:51:45 -04002197 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002198
James Smart46fa3112007-04-25 09:51:45 -04002199 if (!lpfc_sli_queue_setup(phba)) {
2200 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002201 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002202 }
2203
James Smartda0436e2009-05-22 14:51:39 -04002204 if (phba->sli_rev == LPFC_SLI_REV4) {
2205 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2206 lpfc_unblock_mgmt_io(phba);
2207 return 1;
2208 }
2209 } else {
2210 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2211 lpfc_unblock_mgmt_io(phba);
2212 return 1;
2213 }
James Smart46fa3112007-04-25 09:51:45 -04002214 }
dea31012005-04-17 16:05:31 -05002215
James Smart549e55c2007-08-02 11:09:51 -04002216 vports = lpfc_create_vport_work_array(phba);
2217 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002218 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002219 struct Scsi_Host *shost;
2220 shost = lpfc_shost_from_vport(vports[i]);
2221 spin_lock_irq(shost->host_lock);
2222 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2223 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2224 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002225 if (phba->sli_rev == LPFC_SLI_REV4)
2226 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002227 spin_unlock_irq(shost->host_lock);
2228 }
James Smart09372822008-01-11 01:52:54 -05002229 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002230
James Smart46fa3112007-04-25 09:51:45 -04002231 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002232 return 0;
2233}
2234
James Smarte59058c2008-08-24 21:49:00 -04002235/**
James Smart3621a712009-04-06 18:47:14 -04002236 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002237 * @phba: pointer to lpfc hba data structure.
2238 *
2239 * This routine marks a HBA's management interface as not blocked. Once the
2240 * HBA's management interface is marked as not blocked, all the user space
2241 * access to the HBA, whether they are from sysfs interface or libdfc
2242 * interface will be allowed. The HBA is set to block the management interface
2243 * when the driver prepares the HBA interface for online or offline and then
2244 * set to unblock the management interface afterwards.
2245 **/
James Smart46fa3112007-04-25 09:51:45 -04002246void
James Smart46fa3112007-04-25 09:51:45 -04002247lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2248{
2249 unsigned long iflag;
2250
James Smart2e0fef82007-06-17 19:56:36 -05002251 spin_lock_irqsave(&phba->hbalock, iflag);
2252 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2253 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002254}
2255
James Smarte59058c2008-08-24 21:49:00 -04002256/**
James Smart3621a712009-04-06 18:47:14 -04002257 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002258 * @phba: pointer to lpfc hba data structure.
2259 *
2260 * This routine is invoked to prepare a HBA to be brought offline. It performs
2261 * unregistration login to all the nodes on all vports and flushes the mailbox
2262 * queue to make it ready to be brought offline.
2263 **/
James Smart46fa3112007-04-25 09:51:45 -04002264void
2265lpfc_offline_prep(struct lpfc_hba * phba)
2266{
James Smart2e0fef82007-06-17 19:56:36 -05002267 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002268 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002269 struct lpfc_vport **vports;
2270 int i;
dea31012005-04-17 16:05:31 -05002271
James Smart2e0fef82007-06-17 19:56:36 -05002272 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002273 return;
dea31012005-04-17 16:05:31 -05002274
James Smart46fa3112007-04-25 09:51:45 -04002275 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002276
2277 lpfc_linkdown(phba);
2278
James Smart87af33f2007-10-27 13:37:43 -04002279 /* Issue an unreg_login to all nodes on all vports */
2280 vports = lpfc_create_vport_work_array(phba);
2281 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002282 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart87af33f2007-10-27 13:37:43 -04002283 struct Scsi_Host *shost;
2284
James Smarta8adb832007-10-27 13:37:53 -04002285 if (vports[i]->load_flag & FC_UNLOADING)
2286 continue;
James Smartc8685952009-11-18 15:39:16 -05002287 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002288 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2289 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2290
James Smart87af33f2007-10-27 13:37:43 -04002291 shost = lpfc_shost_from_vport(vports[i]);
2292 list_for_each_entry_safe(ndlp, next_ndlp,
2293 &vports[i]->fc_nodes,
2294 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002295 if (!NLP_CHK_NODE_ACT(ndlp))
2296 continue;
James Smart87af33f2007-10-27 13:37:43 -04002297 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2298 continue;
2299 if (ndlp->nlp_type & NLP_FABRIC) {
2300 lpfc_disc_state_machine(vports[i], ndlp,
2301 NULL, NLP_EVT_DEVICE_RECOVERY);
2302 lpfc_disc_state_machine(vports[i], ndlp,
2303 NULL, NLP_EVT_DEVICE_RM);
2304 }
2305 spin_lock_irq(shost->host_lock);
2306 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2307 spin_unlock_irq(shost->host_lock);
2308 lpfc_unreg_rpi(vports[i], ndlp);
2309 }
2310 }
2311 }
James Smart09372822008-01-11 01:52:54 -05002312 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002313
James Smart04c68492009-05-22 14:52:52 -04002314 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002315}
2316
James Smarte59058c2008-08-24 21:49:00 -04002317/**
James Smart3621a712009-04-06 18:47:14 -04002318 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002319 * @phba: pointer to lpfc hba data structure.
2320 *
2321 * This routine actually brings a HBA offline. It stops all the timers
2322 * associated with the HBA, brings down the SLI layer, and eventually
2323 * marks the HBA as in offline state for the upper layer protocol.
2324 **/
James Smart46fa3112007-04-25 09:51:45 -04002325void
James Smart2e0fef82007-06-17 19:56:36 -05002326lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002327{
James Smart549e55c2007-08-02 11:09:51 -04002328 struct Scsi_Host *shost;
2329 struct lpfc_vport **vports;
2330 int i;
James Smart46fa3112007-04-25 09:51:45 -04002331
James Smart549e55c2007-08-02 11:09:51 -04002332 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002333 return;
James Smart688a8862006-07-06 15:49:56 -04002334
James Smartda0436e2009-05-22 14:51:39 -04002335 /* stop port and all timers associated with this hba */
2336 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002337 vports = lpfc_create_vport_work_array(phba);
2338 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002339 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002340 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002341 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002342 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002343 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002344 /* Bring down the SLI Layer and cleanup. The HBA is offline
2345 now. */
2346 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002347 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002348 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002349 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002350 vports = lpfc_create_vport_work_array(phba);
2351 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002352 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002353 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002354 spin_lock_irq(shost->host_lock);
2355 vports[i]->work_port_events = 0;
2356 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2357 spin_unlock_irq(shost->host_lock);
2358 }
James Smart09372822008-01-11 01:52:54 -05002359 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002360}
2361
James Smarte59058c2008-08-24 21:49:00 -04002362/**
James Smart3621a712009-04-06 18:47:14 -04002363 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002364 * @phba: pointer to lpfc hba data structure.
2365 *
2366 * This routine is to free all the SCSI buffers and IOCBs from the driver
2367 * list back to kernel. It is called from lpfc_pci_remove_one to free
2368 * the internal resources before the device is removed from the system.
2369 *
2370 * Return codes
2371 * 0 - successful (for now, it always returns 0)
2372 **/
dea31012005-04-17 16:05:31 -05002373static int
James Smart2e0fef82007-06-17 19:56:36 -05002374lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002375{
2376 struct lpfc_scsi_buf *sb, *sb_next;
2377 struct lpfc_iocbq *io, *io_next;
2378
James Smart2e0fef82007-06-17 19:56:36 -05002379 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002380 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002381 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002382 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2383 list_del(&sb->list);
2384 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002385 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002386 kfree(sb);
2387 phba->total_scsi_bufs--;
2388 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002389 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002390
2391 /* Release all the lpfc_iocbq entries maintained by this host. */
2392 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2393 list_del(&io->list);
2394 kfree(io);
2395 phba->total_iocbq_bufs--;
2396 }
James Smart2e0fef82007-06-17 19:56:36 -05002397 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002398 return 0;
2399}
2400
James Smarte59058c2008-08-24 21:49:00 -04002401/**
James Smart3621a712009-04-06 18:47:14 -04002402 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002403 * @phba: pointer to lpfc hba data structure.
2404 * @instance: a unique integer ID to this FC port.
2405 * @dev: pointer to the device data structure.
2406 *
2407 * This routine creates a FC port for the upper layer protocol. The FC port
2408 * can be created on top of either a physical port or a virtual port provided
2409 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2410 * and associates the FC port created before adding the shost into the SCSI
2411 * layer.
2412 *
2413 * Return codes
2414 * @vport - pointer to the virtual N_Port data structure.
2415 * NULL - port create failed.
2416 **/
James Smart2e0fef82007-06-17 19:56:36 -05002417struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002418lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002419{
James Smart2e0fef82007-06-17 19:56:36 -05002420 struct lpfc_vport *vport;
2421 struct Scsi_Host *shost;
2422 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002423
James Smart3de2a652007-08-02 11:09:59 -04002424 if (dev != &phba->pcidev->dev)
2425 shost = scsi_host_alloc(&lpfc_vport_template,
2426 sizeof(struct lpfc_vport));
2427 else
2428 shost = scsi_host_alloc(&lpfc_template,
2429 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002430 if (!shost)
2431 goto out;
James Smart47a86172007-04-25 09:53:22 -04002432
James Smart2e0fef82007-06-17 19:56:36 -05002433 vport = (struct lpfc_vport *) shost->hostdata;
2434 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002435 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002436 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002437 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002438
James Smart3de2a652007-08-02 11:09:59 -04002439 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002440 shost->unique_id = instance;
2441 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002442 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002443 shost->this_id = -1;
2444 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002445 if (phba->sli_rev == LPFC_SLI_REV4) {
2446 shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2447 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2448 }
James Smart81301a92008-12-04 22:39:46 -05002449
James Smart47a86172007-04-25 09:53:22 -04002450 /*
James Smart2e0fef82007-06-17 19:56:36 -05002451 * Set initial can_queue value since 0 is no longer supported and
2452 * scsi_add_host will fail. This will be adjusted later based on the
2453 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002454 */
James Smart2e0fef82007-06-17 19:56:36 -05002455 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002456 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002457 shost->transportt = lpfc_vport_transport_template;
2458 vport->port_type = LPFC_NPIV_PORT;
2459 } else {
2460 shost->transportt = lpfc_transport_template;
2461 vport->port_type = LPFC_PHYSICAL_PORT;
2462 }
James Smart47a86172007-04-25 09:53:22 -04002463
James Smart2e0fef82007-06-17 19:56:36 -05002464 /* Initialize all internally managed lists. */
2465 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002466 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002467 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002468
James Smart2e0fef82007-06-17 19:56:36 -05002469 init_timer(&vport->fc_disctmo);
2470 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002471 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002472
James Smart2e0fef82007-06-17 19:56:36 -05002473 init_timer(&vport->fc_fdmitmo);
2474 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002475 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002476
James Smart2e0fef82007-06-17 19:56:36 -05002477 init_timer(&vport->els_tmofunc);
2478 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002479 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002480
James Bottomleyd139b9b2009-11-05 13:33:12 -06002481 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002482 if (error)
2483 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002484
James Smart549e55c2007-08-02 11:09:51 -04002485 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002486 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002487 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002488 return vport;
James Smart47a86172007-04-25 09:53:22 -04002489
James Smart2e0fef82007-06-17 19:56:36 -05002490out_put_shost:
2491 scsi_host_put(shost);
2492out:
2493 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002494}
2495
James Smarte59058c2008-08-24 21:49:00 -04002496/**
James Smart3621a712009-04-06 18:47:14 -04002497 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002498 * @vport: pointer to an lpfc virtual N_Port data structure.
2499 *
2500 * This routine destroys a FC port from the upper layer protocol. All the
2501 * resources associated with the port are released.
2502 **/
James Smart2e0fef82007-06-17 19:56:36 -05002503void
2504destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002505{
James Smart92d7f7b2007-06-17 19:56:38 -05002506 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2507 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002508
James Smart858c9f62007-06-17 19:56:39 -05002509 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002510 fc_remove_host(shost);
2511 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002512
James Smart92d7f7b2007-06-17 19:56:38 -05002513 spin_lock_irq(&phba->hbalock);
2514 list_del_init(&vport->listentry);
2515 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002516
James Smart92d7f7b2007-06-17 19:56:38 -05002517 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002518 return;
James Smart47a86172007-04-25 09:53:22 -04002519}
2520
James Smarte59058c2008-08-24 21:49:00 -04002521/**
James Smart3621a712009-04-06 18:47:14 -04002522 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002523 *
2524 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2525 * uses the kernel idr facility to perform the task.
2526 *
2527 * Return codes:
2528 * instance - a unique integer ID allocated as the new instance.
2529 * -1 - lpfc get instance failed.
2530 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002531int
2532lpfc_get_instance(void)
2533{
2534 int instance = 0;
2535
2536 /* Assign an unused number */
2537 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2538 return -1;
2539 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2540 return -1;
2541 return instance;
2542}
2543
James Smarte59058c2008-08-24 21:49:00 -04002544/**
James Smart3621a712009-04-06 18:47:14 -04002545 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002546 * @shost: pointer to SCSI host data structure.
2547 * @time: elapsed time of the scan in jiffies.
2548 *
2549 * This routine is called by the SCSI layer with a SCSI host to determine
2550 * whether the scan host is finished.
2551 *
2552 * Note: there is no scan_start function as adapter initialization will have
2553 * asynchronously kicked off the link initialization.
2554 *
2555 * Return codes
2556 * 0 - SCSI host scan is not over yet.
2557 * 1 - SCSI host scan is over.
2558 **/
James Smart47a86172007-04-25 09:53:22 -04002559int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2560{
James Smart2e0fef82007-06-17 19:56:36 -05002561 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2562 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002563 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002564
James Smart858c9f62007-06-17 19:56:39 -05002565 spin_lock_irq(shost->host_lock);
2566
James Smart51ef4c22007-08-02 11:10:31 -04002567 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002568 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002569 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002570 }
James Smart2e0fef82007-06-17 19:56:36 -05002571 if (time >= 30 * HZ) {
2572 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002573 "0461 Scanning longer than 30 "
2574 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002575 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002576 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002577 }
2578 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2579 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002580 "0465 Link down longer than 15 "
2581 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002582 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002583 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002584 }
2585
James Smart2e0fef82007-06-17 19:56:36 -05002586 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002587 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002588 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002589 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002590 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002591 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002592 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002593 goto finished;
2594
2595 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002596
2597finished:
James Smart858c9f62007-06-17 19:56:39 -05002598 spin_unlock_irq(shost->host_lock);
2599 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002600}
2601
James Smarte59058c2008-08-24 21:49:00 -04002602/**
James Smart3621a712009-04-06 18:47:14 -04002603 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002604 * @shost: pointer to SCSI host data structure.
2605 *
2606 * This routine initializes a given SCSI host attributes on a FC port. The
2607 * SCSI host can be either on top of a physical port or a virtual port.
2608 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002609void lpfc_host_attrib_init(struct Scsi_Host *shost)
2610{
2611 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2612 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002613 /*
James Smart2e0fef82007-06-17 19:56:36 -05002614 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002615 */
2616
James Smart2e0fef82007-06-17 19:56:36 -05002617 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2618 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002619 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2620
2621 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002622 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002623 fc_host_supported_fc4s(shost)[2] = 1;
2624 fc_host_supported_fc4s(shost)[7] = 1;
2625
James Smart92d7f7b2007-06-17 19:56:38 -05002626 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2627 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002628
2629 fc_host_supported_speeds(shost) = 0;
2630 if (phba->lmt & LMT_10Gb)
2631 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002632 if (phba->lmt & LMT_8Gb)
2633 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002634 if (phba->lmt & LMT_4Gb)
2635 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2636 if (phba->lmt & LMT_2Gb)
2637 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2638 if (phba->lmt & LMT_1Gb)
2639 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2640
2641 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002642 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2643 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002644
2645 /* This value is also unchanging */
2646 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002647 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002648 fc_host_active_fc4s(shost)[2] = 1;
2649 fc_host_active_fc4s(shost)[7] = 1;
2650
James Smart92d7f7b2007-06-17 19:56:38 -05002651 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002652 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002653 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002654 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002655}
dea31012005-04-17 16:05:31 -05002656
James Smarte59058c2008-08-24 21:49:00 -04002657/**
James Smartda0436e2009-05-22 14:51:39 -04002658 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002659 * @phba: pointer to lpfc hba data structure.
2660 *
James Smartda0436e2009-05-22 14:51:39 -04002661 * This routine is invoked to stop an SLI3 device port, it stops the device
2662 * from generating interrupts and stops the device driver's timers for the
2663 * device.
James Smarte59058c2008-08-24 21:49:00 -04002664 **/
James Smartdb2378e2008-02-08 18:49:51 -05002665static void
James Smartda0436e2009-05-22 14:51:39 -04002666lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002667{
James Smartda0436e2009-05-22 14:51:39 -04002668 /* Clear all interrupt enable conditions */
2669 writel(0, phba->HCregaddr);
2670 readl(phba->HCregaddr); /* flush */
2671 /* Clear all pending interrupts */
2672 writel(0xffffffff, phba->HAregaddr);
2673 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002674
James Smartda0436e2009-05-22 14:51:39 -04002675 /* Reset some HBA SLI setup states */
2676 lpfc_stop_hba_timers(phba);
2677 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002678}
2679
James Smarte59058c2008-08-24 21:49:00 -04002680/**
James Smartda0436e2009-05-22 14:51:39 -04002681 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002682 * @phba: pointer to lpfc hba data structure.
2683 *
James Smartda0436e2009-05-22 14:51:39 -04002684 * This routine is invoked to stop an SLI4 device port, it stops the device
2685 * from generating interrupts and stops the device driver's timers for the
2686 * device.
2687 **/
2688static void
2689lpfc_stop_port_s4(struct lpfc_hba *phba)
2690{
2691 /* Reset some HBA SLI4 setup states */
2692 lpfc_stop_hba_timers(phba);
2693 phba->pport->work_port_events = 0;
2694 phba->sli4_hba.intr_enable = 0;
2695 /* Hard clear it for now, shall have more graceful way to wait later */
2696 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2697}
2698
2699/**
2700 * lpfc_stop_port - Wrapper function for stopping hba port
2701 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002702 *
James Smartda0436e2009-05-22 14:51:39 -04002703 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2704 * the API jump table function pointer from the lpfc_hba struct.
2705 **/
2706void
2707lpfc_stop_port(struct lpfc_hba *phba)
2708{
2709 phba->lpfc_stop_port(phba);
2710}
2711
2712/**
2713 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2714 * @phba: pointer to lpfc hba data structure.
2715 *
2716 * This routine is invoked to remove the driver default fcf record from
2717 * the port. This routine currently acts on FCF Index 0.
2718 *
2719 **/
2720void
2721lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2722{
2723 int rc = 0;
2724 LPFC_MBOXQ_t *mboxq;
2725 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2726 uint32_t mbox_tmo, req_len;
2727 uint32_t shdr_status, shdr_add_status;
2728
2729 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2730 if (!mboxq) {
2731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2732 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2733 return;
2734 }
2735
2736 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2737 sizeof(struct lpfc_sli4_cfg_mhdr);
2738 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2739 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2740 req_len, LPFC_SLI4_MBX_EMBED);
2741 /*
2742 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2743 * supports multiple FCF indices.
2744 */
2745 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2746 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2747 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
James Smartecfd03c2010-02-12 14:41:27 -05002748 phba->fcf.current_rec.fcf_indx);
James Smartda0436e2009-05-22 14:51:39 -04002749
2750 if (!phba->sli4_hba.intr_enable)
2751 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2752 else {
2753 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2754 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2755 }
2756 /* The IOCTL status is embedded in the mailbox subheader. */
2757 shdr_status = bf_get(lpfc_mbox_hdr_status,
2758 &del_fcf_record->header.cfg_shdr.response);
2759 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2760 &del_fcf_record->header.cfg_shdr.response);
2761 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2762 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2763 "2516 DEL FCF of default FCF Index failed "
2764 "mbx status x%x, status x%x add_status x%x\n",
2765 rc, shdr_status, shdr_add_status);
2766 }
2767 if (rc != MBX_TIMEOUT)
2768 mempool_free(mboxq, phba->mbox_mem_pool);
2769}
2770
2771/**
James Smartecfd03c2010-02-12 14:41:27 -05002772 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2773 * @phba: Pointer to hba for which this call is being executed.
2774 *
2775 * This routine starts the timer waiting for the FCF rediscovery to complete.
2776 **/
2777void
2778lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2779{
2780 unsigned long fcf_redisc_wait_tmo =
2781 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2782 /* Start fcf rediscovery wait period timer */
2783 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2784 spin_lock_irq(&phba->hbalock);
2785 /* Allow action to new fcf asynchronous event */
2786 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2787 /* Mark the FCF rediscovery pending state */
2788 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2789 spin_unlock_irq(&phba->hbalock);
2790}
2791
2792/**
2793 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2794 * @ptr: Map to lpfc_hba data structure pointer.
2795 *
2796 * This routine is invoked when waiting for FCF table rediscover has been
2797 * timed out. If new FCF record(s) has (have) been discovered during the
2798 * wait period, a new FCF event shall be added to the FCOE async event
2799 * list, and then worker thread shall be waked up for processing from the
2800 * worker thread context.
2801 **/
2802void
2803lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2804{
2805 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2806
2807 /* Don't send FCF rediscovery event if timer cancelled */
2808 spin_lock_irq(&phba->hbalock);
2809 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2810 spin_unlock_irq(&phba->hbalock);
2811 return;
2812 }
2813 /* Clear FCF rediscovery timer pending flag */
2814 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2815 /* FCF rediscovery event to worker thread */
2816 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2817 spin_unlock_irq(&phba->hbalock);
2818 /* wake up worker thread */
2819 lpfc_worker_wake_up(phba);
2820}
2821
2822/**
James Smart6669f9b2009-10-02 15:16:45 -04002823 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2824 * @phba: pointer to lpfc hba data structure.
2825 *
2826 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2827 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2828 * was successful and the firmware supports FCoE. Any other return indicates
2829 * a error. It is assumed that this function will be called before interrupts
2830 * are enabled.
2831 **/
2832static int
2833lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2834{
2835 int rc = 0;
2836 LPFC_MBOXQ_t *mboxq;
2837 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2838 uint32_t length;
2839 uint32_t shdr_status, shdr_add_status;
2840
2841 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2842 if (!mboxq) {
2843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2844 "2621 Failed to allocate mbox for "
2845 "query firmware config cmd\n");
2846 return -ENOMEM;
2847 }
2848 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2849 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2850 sizeof(struct lpfc_sli4_cfg_mhdr));
2851 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2852 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2853 length, LPFC_SLI4_MBX_EMBED);
2854 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2855 /* The IOCTL status is embedded in the mailbox subheader. */
2856 shdr_status = bf_get(lpfc_mbox_hdr_status,
2857 &query_fw_cfg->header.cfg_shdr.response);
2858 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2859 &query_fw_cfg->header.cfg_shdr.response);
2860 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2862 "2622 Query Firmware Config failed "
2863 "mbx status x%x, status x%x add_status x%x\n",
2864 rc, shdr_status, shdr_add_status);
2865 return -EINVAL;
2866 }
2867 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2868 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2869 "2623 FCoE Function not supported by firmware. "
2870 "Function mode = %08x\n",
2871 query_fw_cfg->function_mode);
2872 return -EINVAL;
2873 }
2874 if (rc != MBX_TIMEOUT)
2875 mempool_free(mboxq, phba->mbox_mem_pool);
2876 return 0;
2877}
2878
2879/**
James Smartda0436e2009-05-22 14:51:39 -04002880 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2881 * @phba: pointer to lpfc hba data structure.
2882 * @acqe_link: pointer to the async link completion queue entry.
2883 *
2884 * This routine is to parse the SLI4 link-attention link fault code and
2885 * translate it into the base driver's read link attention mailbox command
2886 * status.
2887 *
2888 * Return: Link-attention status in terms of base driver's coding.
2889 **/
2890static uint16_t
2891lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2892 struct lpfc_acqe_link *acqe_link)
2893{
2894 uint16_t latt_fault;
2895
2896 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2897 case LPFC_ASYNC_LINK_FAULT_NONE:
2898 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2899 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2900 latt_fault = 0;
2901 break;
2902 default:
2903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2904 "0398 Invalid link fault code: x%x\n",
2905 bf_get(lpfc_acqe_link_fault, acqe_link));
2906 latt_fault = MBXERR_ERROR;
2907 break;
2908 }
2909 return latt_fault;
2910}
2911
2912/**
2913 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2914 * @phba: pointer to lpfc hba data structure.
2915 * @acqe_link: pointer to the async link completion queue entry.
2916 *
2917 * This routine is to parse the SLI4 link attention type and translate it
2918 * into the base driver's link attention type coding.
2919 *
2920 * Return: Link attention type in terms of base driver's coding.
2921 **/
2922static uint8_t
2923lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2924 struct lpfc_acqe_link *acqe_link)
2925{
2926 uint8_t att_type;
2927
2928 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2929 case LPFC_ASYNC_LINK_STATUS_DOWN:
2930 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2931 att_type = AT_LINK_DOWN;
2932 break;
2933 case LPFC_ASYNC_LINK_STATUS_UP:
2934 /* Ignore physical link up events - wait for logical link up */
2935 att_type = AT_RESERVED;
2936 break;
2937 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2938 att_type = AT_LINK_UP;
2939 break;
2940 default:
2941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2942 "0399 Invalid link attention type: x%x\n",
2943 bf_get(lpfc_acqe_link_status, acqe_link));
2944 att_type = AT_RESERVED;
2945 break;
2946 }
2947 return att_type;
2948}
2949
2950/**
2951 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2952 * @phba: pointer to lpfc hba data structure.
2953 * @acqe_link: pointer to the async link completion queue entry.
2954 *
2955 * This routine is to parse the SLI4 link-attention link speed and translate
2956 * it into the base driver's link-attention link speed coding.
2957 *
2958 * Return: Link-attention link speed in terms of base driver's coding.
2959 **/
2960static uint8_t
2961lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2962 struct lpfc_acqe_link *acqe_link)
2963{
2964 uint8_t link_speed;
2965
2966 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2967 case LPFC_ASYNC_LINK_SPEED_ZERO:
2968 link_speed = LA_UNKNW_LINK;
2969 break;
2970 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2971 link_speed = LA_UNKNW_LINK;
2972 break;
2973 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2974 link_speed = LA_UNKNW_LINK;
2975 break;
2976 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2977 link_speed = LA_1GHZ_LINK;
2978 break;
2979 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2980 link_speed = LA_10GHZ_LINK;
2981 break;
2982 default:
2983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2984 "0483 Invalid link-attention link speed: x%x\n",
2985 bf_get(lpfc_acqe_link_speed, acqe_link));
2986 link_speed = LA_UNKNW_LINK;
2987 break;
2988 }
2989 return link_speed;
2990}
2991
2992/**
2993 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2994 * @phba: pointer to lpfc hba data structure.
2995 * @acqe_link: pointer to the async link completion queue entry.
2996 *
2997 * This routine is to handle the SLI4 asynchronous link event.
2998 **/
2999static void
3000lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3001 struct lpfc_acqe_link *acqe_link)
3002{
3003 struct lpfc_dmabuf *mp;
3004 LPFC_MBOXQ_t *pmb;
3005 MAILBOX_t *mb;
3006 READ_LA_VAR *la;
3007 uint8_t att_type;
3008
3009 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3010 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3011 return;
James Smart32b97932009-07-19 10:01:21 -04003012 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003013 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3014 if (!pmb) {
3015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3016 "0395 The mboxq allocation failed\n");
3017 return;
3018 }
3019 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3020 if (!mp) {
3021 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3022 "0396 The lpfc_dmabuf allocation failed\n");
3023 goto out_free_pmb;
3024 }
3025 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3026 if (!mp->virt) {
3027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3028 "0397 The mbuf allocation failed\n");
3029 goto out_free_dmabuf;
3030 }
3031
3032 /* Cleanup any outstanding ELS commands */
3033 lpfc_els_flush_all_cmd(phba);
3034
3035 /* Block ELS IOCBs until we have done process link event */
3036 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3037
3038 /* Update link event statistics */
3039 phba->sli.slistat.link_event++;
3040
3041 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3042 lpfc_read_la(phba, pmb, mp);
3043 pmb->vport = phba->pport;
3044
3045 /* Parse and translate status field */
3046 mb = &pmb->u.mb;
3047 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3048
3049 /* Parse and translate link attention fields */
3050 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3051 la->eventTag = acqe_link->event_tag;
3052 la->attType = att_type;
3053 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3054
3055 /* Fake the the following irrelvant fields */
3056 la->topology = TOPOLOGY_PT_PT;
3057 la->granted_AL_PA = 0;
3058 la->il = 0;
3059 la->pb = 0;
3060 la->fa = 0;
3061 la->mm = 0;
3062
3063 /* Keep the link status for extra SLI4 state machine reference */
3064 phba->sli4_hba.link_state.speed =
3065 bf_get(lpfc_acqe_link_speed, acqe_link);
3066 phba->sli4_hba.link_state.duplex =
3067 bf_get(lpfc_acqe_link_duplex, acqe_link);
3068 phba->sli4_hba.link_state.status =
3069 bf_get(lpfc_acqe_link_status, acqe_link);
3070 phba->sli4_hba.link_state.physical =
3071 bf_get(lpfc_acqe_link_physical, acqe_link);
3072 phba->sli4_hba.link_state.fault =
3073 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003074 phba->sli4_hba.link_state.logical_speed =
3075 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003076
3077 /* Invoke the lpfc_handle_latt mailbox command callback function */
3078 lpfc_mbx_cmpl_read_la(phba, pmb);
3079
3080 return;
3081
3082out_free_dmabuf:
3083 kfree(mp);
3084out_free_pmb:
3085 mempool_free(pmb, phba->mbox_mem_pool);
3086}
3087
3088/**
3089 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3090 * @phba: pointer to lpfc hba data structure.
3091 * @acqe_link: pointer to the async fcoe completion queue entry.
3092 *
3093 * This routine is to handle the SLI4 asynchronous fcoe event.
3094 **/
3095static void
3096lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3097 struct lpfc_acqe_fcoe *acqe_fcoe)
3098{
3099 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3100 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003101 struct lpfc_vport *vport;
3102 struct lpfc_nodelist *ndlp;
3103 struct Scsi_Host *shost;
James Smart97957242009-12-21 17:03:15 -05003104 uint32_t link_state;
James Smart695a8142010-01-26 23:08:03 -05003105 int active_vlink_present;
3106 struct lpfc_vport **vports;
3107 int i;
James Smartda0436e2009-05-22 14:51:39 -04003108
James Smart4d9ab992009-10-02 15:16:39 -04003109 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003110 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003111 switch (event_type) {
3112 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
James Smartecfd03c2010-02-12 14:41:27 -05003113 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
James Smartda0436e2009-05-22 14:51:39 -04003114 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smarte4e74272009-07-19 10:01:38 -04003115 "2546 New FCF found index 0x%x tag 0x%x\n",
James Smart6669f9b2009-10-02 15:16:45 -04003116 acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003117 acqe_fcoe->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003118 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003119 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3120 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3121 /*
3122 * If the current FCF is in discovered state or
3123 * FCF discovery is in progress, do nothing.
3124 */
3125 spin_unlock_irq(&phba->hbalock);
3126 break;
3127 }
3128 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3129 /*
3130 * If fast FCF failover rescan event is pending,
3131 * do nothing.
3132 */
James Smartda0436e2009-05-22 14:51:39 -04003133 spin_unlock_irq(&phba->hbalock);
3134 break;
3135 }
3136 spin_unlock_irq(&phba->hbalock);
3137
3138 /* Read the FCF table and re-discover SAN. */
James Smart6a9c52c2009-10-02 15:16:51 -04003139 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003140 if (rc)
3141 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003142 "2547 Read FCF record failed 0x%x\n",
3143 rc);
James Smartda0436e2009-05-22 14:51:39 -04003144 break;
3145
3146 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3147 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003148 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003149 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3150 acqe_fcoe->event_tag);
3151 break;
3152
3153 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3154 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003155 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003156 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003157 acqe_fcoe->event_tag);
3158 /* If the event is not for currently used fcf do nothing */
James Smartecfd03c2010-02-12 14:41:27 -05003159 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003160 break;
3161 /*
3162 * Currently, driver support only one FCF - so treat this as
James Smart97957242009-12-21 17:03:15 -05003163 * a link down, but save the link state because we don't want
3164 * it to be changed to Link Down unless it is already down.
James Smartda0436e2009-05-22 14:51:39 -04003165 */
James Smart97957242009-12-21 17:03:15 -05003166 link_state = phba->link_state;
James Smartda0436e2009-05-22 14:51:39 -04003167 lpfc_linkdown(phba);
James Smart97957242009-12-21 17:03:15 -05003168 phba->link_state = link_state;
James Smartda0436e2009-05-22 14:51:39 -04003169 /* Unregister FCF if no devices connected to it */
3170 lpfc_unregister_unused_fcf(phba);
3171 break;
James Smart6669f9b2009-10-02 15:16:45 -04003172 case LPFC_FCOE_EVENT_TYPE_CVL:
3173 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3174 "2718 Clear Virtual Link Received for VPI 0x%x"
3175 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3176 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003177 acqe_fcoe->index - phba->vpi_base);
James Smart6669f9b2009-10-02 15:16:45 -04003178 if (!vport)
3179 break;
3180 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3181 if (!ndlp)
3182 break;
3183 shost = lpfc_shost_from_vport(vport);
James Smart695a8142010-01-26 23:08:03 -05003184 if (phba->pport->port_state <= LPFC_FLOGI)
3185 break;
3186 /* If virtual link is not yet instantiated ignore CVL */
3187 if (vport->port_state <= LPFC_FDISC)
3188 break;
3189
James Smart6669f9b2009-10-02 15:16:45 -04003190 lpfc_linkdown_port(vport);
James Smart695a8142010-01-26 23:08:03 -05003191 lpfc_cleanup_pending_mbox(vport);
3192 spin_lock_irq(shost->host_lock);
3193 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3194 spin_unlock_irq(shost->host_lock);
3195 active_vlink_present = 0;
3196
3197 vports = lpfc_create_vport_work_array(phba);
3198 if (vports) {
3199 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3200 i++) {
3201 if ((!(vports[i]->fc_flag &
3202 FC_VPORT_CVL_RCVD)) &&
3203 (vports[i]->port_state > LPFC_FDISC)) {
3204 active_vlink_present = 1;
3205 break;
3206 }
3207 }
3208 lpfc_destroy_vport_work_array(phba, vports);
3209 }
3210
3211 if (active_vlink_present) {
3212 /*
3213 * If there are other active VLinks present,
3214 * re-instantiate the Vlink using FDISC.
3215 */
James Smart6669f9b2009-10-02 15:16:45 -04003216 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3217 spin_lock_irq(shost->host_lock);
3218 ndlp->nlp_flag |= NLP_DELAY_TMO;
3219 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003220 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3221 vport->port_state = LPFC_FDISC;
3222 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003223 /*
3224 * Otherwise, we request port to rediscover
3225 * the entire FCF table for a fast recovery
3226 * from possible case that the current FCF
3227 * is no longer valid.
3228 */
3229 rc = lpfc_sli4_redisc_fcf_table(phba);
3230 if (rc)
3231 /*
3232 * Last resort will be re-try on the
3233 * the current registered FCF entry.
3234 */
3235 lpfc_retry_pport_discovery(phba);
James Smart6669f9b2009-10-02 15:16:45 -04003236 }
3237 break;
James Smartda0436e2009-05-22 14:51:39 -04003238 default:
3239 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3240 "0288 Unknown FCoE event type 0x%x event tag "
3241 "0x%x\n", event_type, acqe_fcoe->event_tag);
3242 break;
3243 }
3244}
3245
3246/**
3247 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3248 * @phba: pointer to lpfc hba data structure.
3249 * @acqe_link: pointer to the async dcbx completion queue entry.
3250 *
3251 * This routine is to handle the SLI4 asynchronous dcbx event.
3252 **/
3253static void
3254lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3255 struct lpfc_acqe_dcbx *acqe_dcbx)
3256{
James Smart4d9ab992009-10-02 15:16:39 -04003257 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003258 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3259 "0290 The SLI4 DCBX asynchronous event is not "
3260 "handled yet\n");
3261}
3262
3263/**
3264 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3265 * @phba: pointer to lpfc hba data structure.
3266 *
3267 * This routine is invoked by the worker thread to process all the pending
3268 * SLI4 asynchronous events.
3269 **/
3270void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3271{
3272 struct lpfc_cq_event *cq_event;
3273
3274 /* First, declare the async event has been handled */
3275 spin_lock_irq(&phba->hbalock);
3276 phba->hba_flag &= ~ASYNC_EVENT;
3277 spin_unlock_irq(&phba->hbalock);
3278 /* Now, handle all the async events */
3279 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3280 /* Get the first event from the head of the event queue */
3281 spin_lock_irq(&phba->hbalock);
3282 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3283 cq_event, struct lpfc_cq_event, list);
3284 spin_unlock_irq(&phba->hbalock);
3285 /* Process the asynchronous event */
3286 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3287 case LPFC_TRAILER_CODE_LINK:
3288 lpfc_sli4_async_link_evt(phba,
3289 &cq_event->cqe.acqe_link);
3290 break;
3291 case LPFC_TRAILER_CODE_FCOE:
3292 lpfc_sli4_async_fcoe_evt(phba,
3293 &cq_event->cqe.acqe_fcoe);
3294 break;
3295 case LPFC_TRAILER_CODE_DCBX:
3296 lpfc_sli4_async_dcbx_evt(phba,
3297 &cq_event->cqe.acqe_dcbx);
3298 break;
3299 default:
3300 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3301 "1804 Invalid asynchrous event code: "
3302 "x%x\n", bf_get(lpfc_trailer_code,
3303 &cq_event->cqe.mcqe_cmpl));
3304 break;
3305 }
3306 /* Free the completion event processed to the free pool */
3307 lpfc_sli4_cq_event_release(phba, cq_event);
3308 }
3309}
3310
3311/**
James Smartecfd03c2010-02-12 14:41:27 -05003312 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3313 * @phba: pointer to lpfc hba data structure.
3314 *
3315 * This routine is invoked by the worker thread to process FCF table
3316 * rediscovery pending completion event.
3317 **/
3318void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3319{
3320 int rc;
3321
3322 spin_lock_irq(&phba->hbalock);
3323 /* Clear FCF rediscovery timeout event */
3324 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3325 /* Clear driver fast failover FCF record flag */
3326 phba->fcf.failover_rec.flag = 0;
3327 /* Set state for FCF fast failover */
3328 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3329 spin_unlock_irq(&phba->hbalock);
3330
3331 /* Scan FCF table from the first entry to re-discover SAN */
3332 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
3333 if (rc)
3334 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3335 "2747 Post FCF rediscovery read FCF record "
3336 "failed 0x%x\n", rc);
3337}
3338
3339/**
James Smartda0436e2009-05-22 14:51:39 -04003340 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3341 * @phba: pointer to lpfc hba data structure.
3342 * @dev_grp: The HBA PCI-Device group number.
3343 *
3344 * This routine is invoked to set up the per HBA PCI-Device group function
3345 * API jump table entries.
3346 *
3347 * Return: 0 if success, otherwise -ENODEV
3348 **/
3349int
3350lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003351{
3352 int rc;
3353
James Smartda0436e2009-05-22 14:51:39 -04003354 /* Set up lpfc PCI-device group */
3355 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003356
James Smartda0436e2009-05-22 14:51:39 -04003357 /* The LPFC_PCI_DEV_OC uses SLI4 */
3358 if (dev_grp == LPFC_PCI_DEV_OC)
3359 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003360
James Smartda0436e2009-05-22 14:51:39 -04003361 /* Set up device INIT API function jump table */
3362 rc = lpfc_init_api_table_setup(phba, dev_grp);
3363 if (rc)
3364 return -ENODEV;
3365 /* Set up SCSI API function jump table */
3366 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3367 if (rc)
3368 return -ENODEV;
3369 /* Set up SLI API function jump table */
3370 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3371 if (rc)
3372 return -ENODEV;
3373 /* Set up MBOX API function jump table */
3374 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3375 if (rc)
3376 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003377
James Smartda0436e2009-05-22 14:51:39 -04003378 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003379}
3380
3381/**
James Smart3621a712009-04-06 18:47:14 -04003382 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003383 * @phba: pointer to lpfc hba data structure.
3384 * @intr_mode: active interrupt mode adopted.
3385 *
3386 * This routine it invoked to log the currently used active interrupt mode
3387 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003388 **/
3389static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003390{
3391 switch (intr_mode) {
3392 case 0:
3393 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3394 "0470 Enable INTx interrupt mode.\n");
3395 break;
3396 case 1:
3397 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3398 "0481 Enabled MSI interrupt mode.\n");
3399 break;
3400 case 2:
3401 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3402 "0480 Enabled MSI-X interrupt mode.\n");
3403 break;
3404 default:
3405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3406 "0482 Illegal interrupt mode.\n");
3407 break;
3408 }
3409 return;
3410}
3411
James Smart5b75da22008-12-04 22:39:35 -05003412/**
James Smart3772a992009-05-22 14:50:54 -04003413 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003414 * @phba: pointer to lpfc hba data structure.
3415 *
James Smart3772a992009-05-22 14:50:54 -04003416 * This routine is invoked to enable the PCI device that is common to all
3417 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003418 *
3419 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003420 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003421 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003422 **/
James Smart3772a992009-05-22 14:50:54 -04003423static int
3424lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003425{
James Smart3772a992009-05-22 14:50:54 -04003426 struct pci_dev *pdev;
3427 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003428
James Smart3772a992009-05-22 14:50:54 -04003429 /* Obtain PCI device reference */
3430 if (!phba->pcidev)
3431 goto out_error;
3432 else
3433 pdev = phba->pcidev;
3434 /* Select PCI BARs */
3435 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3436 /* Enable PCI device */
3437 if (pci_enable_device_mem(pdev))
3438 goto out_error;
3439 /* Request PCI resource for the device */
3440 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3441 goto out_disable_device;
3442 /* Set up device as PCI master and save state for EEH */
3443 pci_set_master(pdev);
3444 pci_try_set_mwi(pdev);
3445 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003446
James Smart3772a992009-05-22 14:50:54 -04003447 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003448
James Smart3772a992009-05-22 14:50:54 -04003449out_disable_device:
3450 pci_disable_device(pdev);
3451out_error:
3452 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003453}
3454
3455/**
James Smart3772a992009-05-22 14:50:54 -04003456 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003457 * @phba: pointer to lpfc hba data structure.
3458 *
James Smart3772a992009-05-22 14:50:54 -04003459 * This routine is invoked to disable the PCI device that is common to all
3460 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003461 **/
3462static void
James Smart3772a992009-05-22 14:50:54 -04003463lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003464{
James Smart3772a992009-05-22 14:50:54 -04003465 struct pci_dev *pdev;
3466 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003467
James Smart3772a992009-05-22 14:50:54 -04003468 /* Obtain PCI device reference */
3469 if (!phba->pcidev)
3470 return;
3471 else
3472 pdev = phba->pcidev;
3473 /* Select PCI BARs */
3474 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3475 /* Release PCI resource and disable PCI device */
3476 pci_release_selected_regions(pdev, bars);
3477 pci_disable_device(pdev);
3478 /* Null out PCI private reference to driver */
3479 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003480
3481 return;
3482}
3483
3484/**
James Smart3772a992009-05-22 14:50:54 -04003485 * lpfc_reset_hba - Reset a hba
3486 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003487 *
James Smart3772a992009-05-22 14:50:54 -04003488 * This routine is invoked to reset a hba device. It brings the HBA
3489 * offline, performs a board restart, and then brings the board back
3490 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3491 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003492 **/
James Smart3772a992009-05-22 14:50:54 -04003493void
3494lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003495{
James Smart3772a992009-05-22 14:50:54 -04003496 /* If resets are disabled then set error state and return. */
3497 if (!phba->cfg_enable_hba_reset) {
3498 phba->link_state = LPFC_HBA_ERROR;
3499 return;
3500 }
3501 lpfc_offline_prep(phba);
3502 lpfc_offline(phba);
3503 lpfc_sli_brdrestart(phba);
3504 lpfc_online(phba);
3505 lpfc_unblock_mgmt_io(phba);
3506}
dea31012005-04-17 16:05:31 -05003507
James Smart3772a992009-05-22 14:50:54 -04003508/**
3509 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3510 * @phba: pointer to lpfc hba data structure.
3511 *
3512 * This routine is invoked to set up the driver internal resources specific to
3513 * support the SLI-3 HBA device it attached to.
3514 *
3515 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003516 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003517 * other values - error
3518 **/
3519static int
3520lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3521{
3522 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003523
James Smart3772a992009-05-22 14:50:54 -04003524 /*
3525 * Initialize timers used by driver
3526 */
dea31012005-04-17 16:05:31 -05003527
James Smart3772a992009-05-22 14:50:54 -04003528 /* Heartbeat timer */
3529 init_timer(&phba->hb_tmofunc);
3530 phba->hb_tmofunc.function = lpfc_hb_timeout;
3531 phba->hb_tmofunc.data = (unsigned long)phba;
3532
3533 psli = &phba->sli;
3534 /* MBOX heartbeat timer */
3535 init_timer(&psli->mbox_tmo);
3536 psli->mbox_tmo.function = lpfc_mbox_timeout;
3537 psli->mbox_tmo.data = (unsigned long) phba;
3538 /* FCP polling mode timer */
3539 init_timer(&phba->fcp_poll_timer);
3540 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3541 phba->fcp_poll_timer.data = (unsigned long) phba;
3542 /* Fabric block timer */
3543 init_timer(&phba->fabric_block_timer);
3544 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3545 phba->fabric_block_timer.data = (unsigned long) phba;
3546 /* EA polling mode timer */
3547 init_timer(&phba->eratt_poll);
3548 phba->eratt_poll.function = lpfc_poll_eratt;
3549 phba->eratt_poll.data = (unsigned long) phba;
3550
3551 /* Host attention work mask setup */
3552 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3553 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3554
3555 /* Get all the module params for configuring this host */
3556 lpfc_get_cfgparam(phba);
3557 /*
3558 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3559 * used to create the sg_dma_buf_pool must be dynamically calculated.
3560 * 2 segments are added since the IOCB needs a command and response bde.
3561 */
3562 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3563 sizeof(struct fcp_rsp) +
3564 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3565
3566 if (phba->cfg_enable_bg) {
3567 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3568 phba->cfg_sg_dma_buf_size +=
3569 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3570 }
3571
3572 /* Also reinitialize the host templates with new values. */
3573 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3574 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3575
3576 phba->max_vpi = LPFC_MAX_VPI;
3577 /* This will be set to correct value after config_port mbox */
3578 phba->max_vports = 0;
3579
3580 /*
3581 * Initialize the SLI Layer to run with lpfc HBAs.
3582 */
3583 lpfc_sli_setup(phba);
3584 lpfc_sli_queue_setup(phba);
3585
3586 /* Allocate device driver memory */
3587 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3588 return -ENOMEM;
3589
3590 return 0;
3591}
3592
3593/**
3594 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3595 * @phba: pointer to lpfc hba data structure.
3596 *
3597 * This routine is invoked to unset the driver internal resources set up
3598 * specific for supporting the SLI-3 HBA device it attached to.
3599 **/
3600static void
3601lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3602{
3603 /* Free device driver memory allocated */
3604 lpfc_mem_free_all(phba);
3605
3606 return;
3607}
3608
3609/**
James Smartda0436e2009-05-22 14:51:39 -04003610 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3611 * @phba: pointer to lpfc hba data structure.
3612 *
3613 * This routine is invoked to set up the driver internal resources specific to
3614 * support the SLI-4 HBA device it attached to.
3615 *
3616 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003617 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04003618 * other values - error
3619 **/
3620static int
3621lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3622{
3623 struct lpfc_sli *psli;
3624 int rc;
3625 int i, hbq_count;
3626
3627 /* Before proceed, wait for POST done and device ready */
3628 rc = lpfc_sli4_post_status_check(phba);
3629 if (rc)
3630 return -ENODEV;
3631
3632 /*
3633 * Initialize timers used by driver
3634 */
3635
3636 /* Heartbeat timer */
3637 init_timer(&phba->hb_tmofunc);
3638 phba->hb_tmofunc.function = lpfc_hb_timeout;
3639 phba->hb_tmofunc.data = (unsigned long)phba;
3640
3641 psli = &phba->sli;
3642 /* MBOX heartbeat timer */
3643 init_timer(&psli->mbox_tmo);
3644 psli->mbox_tmo.function = lpfc_mbox_timeout;
3645 psli->mbox_tmo.data = (unsigned long) phba;
3646 /* Fabric block timer */
3647 init_timer(&phba->fabric_block_timer);
3648 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3649 phba->fabric_block_timer.data = (unsigned long) phba;
3650 /* EA polling mode timer */
3651 init_timer(&phba->eratt_poll);
3652 phba->eratt_poll.function = lpfc_poll_eratt;
3653 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05003654 /* FCF rediscover timer */
3655 init_timer(&phba->fcf.redisc_wait);
3656 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3657 phba->fcf.redisc_wait.data = (unsigned long)phba;
3658
James Smartda0436e2009-05-22 14:51:39 -04003659 /*
3660 * We need to do a READ_CONFIG mailbox command here before
3661 * calling lpfc_get_cfgparam. For VFs this will report the
3662 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3663 * All of the resources allocated
3664 * for this Port are tied to these values.
3665 */
3666 /* Get all the module params for configuring this host */
3667 lpfc_get_cfgparam(phba);
3668 phba->max_vpi = LPFC_MAX_VPI;
3669 /* This will be set to correct value after the read_config mbox */
3670 phba->max_vports = 0;
3671
3672 /* Program the default value of vlan_id and fc_map */
3673 phba->valid_vlan = 0;
3674 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3675 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3676 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3677
3678 /*
3679 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3680 * used to create the sg_dma_buf_pool must be dynamically calculated.
3681 * 2 segments are added since the IOCB needs a command and response bde.
3682 * To insure that the scsi sgl does not cross a 4k page boundary only
3683 * sgl sizes of 1k, 2k, 4k, and 8k are supported.
3684 * Table of sgl sizes and seg_cnt:
3685 * sgl size, sg_seg_cnt total seg
3686 * 1k 50 52
3687 * 2k 114 116
3688 * 4k 242 244
3689 * 8k 498 500
3690 * cmd(32) + rsp(160) + (52 * sizeof(sli4_sge)) = 1024
3691 * cmd(32) + rsp(160) + (116 * sizeof(sli4_sge)) = 2048
3692 * cmd(32) + rsp(160) + (244 * sizeof(sli4_sge)) = 4096
3693 * cmd(32) + rsp(160) + (500 * sizeof(sli4_sge)) = 8192
3694 */
3695 if (phba->cfg_sg_seg_cnt <= LPFC_DEFAULT_SG_SEG_CNT)
3696 phba->cfg_sg_seg_cnt = 50;
3697 else if (phba->cfg_sg_seg_cnt <= 114)
3698 phba->cfg_sg_seg_cnt = 114;
3699 else if (phba->cfg_sg_seg_cnt <= 242)
3700 phba->cfg_sg_seg_cnt = 242;
3701 else
3702 phba->cfg_sg_seg_cnt = 498;
3703
3704 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd)
3705 + sizeof(struct fcp_rsp);
3706 phba->cfg_sg_dma_buf_size +=
3707 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
3708
3709 /* Initialize buffer queue management fields */
3710 hbq_count = lpfc_sli_hbq_count();
3711 for (i = 0; i < hbq_count; ++i)
3712 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3713 INIT_LIST_HEAD(&phba->rb_pend_list);
3714 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3715 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3716
3717 /*
3718 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3719 */
3720 /* Initialize the Abort scsi buffer list used by driver */
3721 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3722 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3723 /* This abort list used by worker thread */
3724 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3725
3726 /*
3727 * Initialize dirver internal slow-path work queues
3728 */
3729
3730 /* Driver internel slow-path CQ Event pool */
3731 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3732 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04003733 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04003734 /* Asynchronous event CQ Event work queue list */
3735 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3736 /* Fast-path XRI aborted CQ Event work queue list */
3737 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3738 /* Slow-path XRI aborted CQ Event work queue list */
3739 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3740 /* Receive queue CQ Event work queue list */
3741 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3742
3743 /* Initialize the driver internal SLI layer lists. */
3744 lpfc_sli_setup(phba);
3745 lpfc_sli_queue_setup(phba);
3746
3747 /* Allocate device driver memory */
3748 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3749 if (rc)
3750 return -ENOMEM;
3751
3752 /* Create the bootstrap mailbox command */
3753 rc = lpfc_create_bootstrap_mbox(phba);
3754 if (unlikely(rc))
3755 goto out_free_mem;
3756
3757 /* Set up the host's endian order with the device. */
3758 rc = lpfc_setup_endian_order(phba);
3759 if (unlikely(rc))
3760 goto out_free_bsmbx;
3761
James Smart6669f9b2009-10-02 15:16:45 -04003762 rc = lpfc_sli4_fw_cfg_check(phba);
3763 if (unlikely(rc))
3764 goto out_free_bsmbx;
3765
James Smartda0436e2009-05-22 14:51:39 -04003766 /* Set up the hba's configuration parameters. */
3767 rc = lpfc_sli4_read_config(phba);
3768 if (unlikely(rc))
3769 goto out_free_bsmbx;
3770
3771 /* Perform a function reset */
3772 rc = lpfc_pci_function_reset(phba);
3773 if (unlikely(rc))
3774 goto out_free_bsmbx;
3775
3776 /* Create all the SLI4 queues */
3777 rc = lpfc_sli4_queue_create(phba);
3778 if (rc)
3779 goto out_free_bsmbx;
3780
3781 /* Create driver internal CQE event pool */
3782 rc = lpfc_sli4_cq_event_pool_create(phba);
3783 if (rc)
3784 goto out_destroy_queue;
3785
3786 /* Initialize and populate the iocb list per host */
3787 rc = lpfc_init_sgl_list(phba);
3788 if (rc) {
3789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3790 "1400 Failed to initialize sgl list.\n");
3791 goto out_destroy_cq_event_pool;
3792 }
3793 rc = lpfc_init_active_sgl_array(phba);
3794 if (rc) {
3795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3796 "1430 Failed to initialize sgl list.\n");
3797 goto out_free_sgl_list;
3798 }
3799
3800 rc = lpfc_sli4_init_rpi_hdrs(phba);
3801 if (rc) {
3802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3803 "1432 Failed to initialize rpi headers.\n");
3804 goto out_free_active_sgl;
3805 }
3806
3807 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3808 phba->cfg_fcp_eq_count), GFP_KERNEL);
3809 if (!phba->sli4_hba.fcp_eq_hdl) {
3810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3811 "2572 Failed allocate memory for fast-path "
3812 "per-EQ handle array\n");
3813 goto out_remove_rpi_hdrs;
3814 }
3815
3816 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3817 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3818 if (!phba->sli4_hba.msix_entries) {
3819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3820 "2573 Failed allocate memory for msi-x "
3821 "interrupt vector entries\n");
3822 goto out_free_fcp_eq_hdl;
3823 }
3824
3825 return rc;
3826
3827out_free_fcp_eq_hdl:
3828 kfree(phba->sli4_hba.fcp_eq_hdl);
3829out_remove_rpi_hdrs:
3830 lpfc_sli4_remove_rpi_hdrs(phba);
3831out_free_active_sgl:
3832 lpfc_free_active_sgl(phba);
3833out_free_sgl_list:
3834 lpfc_free_sgl_list(phba);
3835out_destroy_cq_event_pool:
3836 lpfc_sli4_cq_event_pool_destroy(phba);
3837out_destroy_queue:
3838 lpfc_sli4_queue_destroy(phba);
3839out_free_bsmbx:
3840 lpfc_destroy_bootstrap_mbox(phba);
3841out_free_mem:
3842 lpfc_mem_free(phba);
3843 return rc;
3844}
3845
3846/**
3847 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3848 * @phba: pointer to lpfc hba data structure.
3849 *
3850 * This routine is invoked to unset the driver internal resources set up
3851 * specific for supporting the SLI-4 HBA device it attached to.
3852 **/
3853static void
3854lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3855{
3856 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3857
3858 /* unregister default FCFI from the HBA */
3859 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3860
3861 /* Free the default FCR table */
3862 lpfc_sli_remove_dflt_fcf(phba);
3863
3864 /* Free memory allocated for msi-x interrupt vector entries */
3865 kfree(phba->sli4_hba.msix_entries);
3866
3867 /* Free memory allocated for fast-path work queue handles */
3868 kfree(phba->sli4_hba.fcp_eq_hdl);
3869
3870 /* Free the allocated rpi headers. */
3871 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04003872 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04003873
3874 /* Free the ELS sgl list */
3875 lpfc_free_active_sgl(phba);
3876 lpfc_free_sgl_list(phba);
3877
3878 /* Free the SCSI sgl management array */
3879 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3880
3881 /* Free the SLI4 queues */
3882 lpfc_sli4_queue_destroy(phba);
3883
3884 /* Free the completion queue EQ event pool */
3885 lpfc_sli4_cq_event_release_all(phba);
3886 lpfc_sli4_cq_event_pool_destroy(phba);
3887
3888 /* Reset SLI4 HBA FCoE function */
3889 lpfc_pci_function_reset(phba);
3890
3891 /* Free the bsmbx region. */
3892 lpfc_destroy_bootstrap_mbox(phba);
3893
3894 /* Free the SLI Layer memory with SLI4 HBAs */
3895 lpfc_mem_free_all(phba);
3896
3897 /* Free the current connect table */
3898 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04003899 &phba->fcf_conn_rec_list, list) {
3900 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04003901 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04003902 }
James Smartda0436e2009-05-22 14:51:39 -04003903
3904 return;
3905}
3906
3907/**
James Smart3772a992009-05-22 14:50:54 -04003908 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3909 * @phba: The hba struct for which this call is being executed.
3910 * @dev_grp: The HBA PCI-Device group number.
3911 *
3912 * This routine sets up the device INIT interface API function jump table
3913 * in @phba struct.
3914 *
3915 * Returns: 0 - success, -ENODEV - failure.
3916 **/
3917int
3918lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3919{
3920 switch (dev_grp) {
3921 case LPFC_PCI_DEV_LP:
3922 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3923 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3924 phba->lpfc_stop_port = lpfc_stop_port_s3;
3925 break;
James Smartda0436e2009-05-22 14:51:39 -04003926 case LPFC_PCI_DEV_OC:
3927 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3928 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3929 phba->lpfc_stop_port = lpfc_stop_port_s4;
3930 break;
James Smart3772a992009-05-22 14:50:54 -04003931 default:
3932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3933 "1431 Invalid HBA PCI-device group: 0x%x\n",
3934 dev_grp);
3935 return -ENODEV;
3936 break;
3937 }
3938 return 0;
3939}
3940
3941/**
3942 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3943 * @phba: pointer to lpfc hba data structure.
3944 *
3945 * This routine is invoked to set up the driver internal resources before the
3946 * device specific resource setup to support the HBA device it attached to.
3947 *
3948 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003949 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003950 * other values - error
3951 **/
3952static int
3953lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3954{
3955 /*
3956 * Driver resources common to all SLI revisions
3957 */
James Smartea2151b2008-09-07 11:52:10 -04003958 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003959 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003960
James Smarte47c9092008-02-08 18:49:26 -05003961 /* Initialize ndlp management spinlock */
3962 spin_lock_init(&phba->ndlp_lock);
3963
James Smart2e0fef82007-06-17 19:56:36 -05003964 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04003965 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04003966 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05003967
James Smart3772a992009-05-22 14:50:54 -04003968 /* Initialize the wait queue head for the kernel thread */
3969 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05003970
James Smart3772a992009-05-22 14:50:54 -04003971 /* Initialize the scsi buffer list used by driver for scsi IO */
3972 spin_lock_init(&phba->scsi_buf_list_lock);
3973 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05003974
James Smart3772a992009-05-22 14:50:54 -04003975 /* Initialize the fabric iocb list */
3976 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05003977
James Smart3772a992009-05-22 14:50:54 -04003978 /* Initialize list to save ELS buffers */
3979 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05003980
James Smart3772a992009-05-22 14:50:54 -04003981 /* Initialize FCF connection rec list */
3982 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05003983
James Smart3772a992009-05-22 14:50:54 -04003984 return 0;
3985}
dea31012005-04-17 16:05:31 -05003986
James Smart3772a992009-05-22 14:50:54 -04003987/**
3988 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
3989 * @phba: pointer to lpfc hba data structure.
3990 *
3991 * This routine is invoked to set up the driver internal resources after the
3992 * device specific resource setup to support the HBA device it attached to.
3993 *
3994 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003995 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003996 * other values - error
3997 **/
3998static int
3999lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4000{
4001 int error;
4002
4003 /* Startup the kernel thread for this host adapter. */
4004 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4005 "lpfc_worker_%d", phba->brd_no);
4006 if (IS_ERR(phba->worker_thread)) {
4007 error = PTR_ERR(phba->worker_thread);
4008 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004009 }
4010
James Smart3772a992009-05-22 14:50:54 -04004011 return 0;
4012}
4013
4014/**
4015 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4016 * @phba: pointer to lpfc hba data structure.
4017 *
4018 * This routine is invoked to unset the driver internal resources set up after
4019 * the device specific resource setup for supporting the HBA device it
4020 * attached to.
4021 **/
4022static void
4023lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4024{
4025 /* Stop kernel worker thread */
4026 kthread_stop(phba->worker_thread);
4027}
4028
4029/**
4030 * lpfc_free_iocb_list - Free iocb list.
4031 * @phba: pointer to lpfc hba data structure.
4032 *
4033 * This routine is invoked to free the driver's IOCB list and memory.
4034 **/
4035static void
4036lpfc_free_iocb_list(struct lpfc_hba *phba)
4037{
4038 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4039
4040 spin_lock_irq(&phba->hbalock);
4041 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4042 &phba->lpfc_iocb_list, list) {
4043 list_del(&iocbq_entry->list);
4044 kfree(iocbq_entry);
4045 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004046 }
James Smart3772a992009-05-22 14:50:54 -04004047 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004048
James Smart3772a992009-05-22 14:50:54 -04004049 return;
4050}
dea31012005-04-17 16:05:31 -05004051
James Smart3772a992009-05-22 14:50:54 -04004052/**
4053 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4054 * @phba: pointer to lpfc hba data structure.
4055 *
4056 * This routine is invoked to allocate and initizlize the driver's IOCB
4057 * list and set up the IOCB tag array accordingly.
4058 *
4059 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004060 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004061 * other values - error
4062 **/
4063static int
4064lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4065{
4066 struct lpfc_iocbq *iocbq_entry = NULL;
4067 uint16_t iotag;
4068 int i;
dea31012005-04-17 16:05:31 -05004069
4070 /* Initialize and populate the iocb list per host. */
4071 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004072 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004073 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004074 if (iocbq_entry == NULL) {
4075 printk(KERN_ERR "%s: only allocated %d iocbs of "
4076 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004077 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004078 goto out_free_iocbq;
4079 }
4080
James Bottomley604a3e32005-10-29 10:28:33 -05004081 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4082 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004083 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004084 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004085 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004086 goto out_free_iocbq;
4087 }
James Smart3772a992009-05-22 14:50:54 -04004088 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004089
4090 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004091 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4092 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004093 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004094 }
4095
James Smart3772a992009-05-22 14:50:54 -04004096 return 0;
4097
4098out_free_iocbq:
4099 lpfc_free_iocb_list(phba);
4100
4101 return -ENOMEM;
4102}
4103
4104/**
James Smartda0436e2009-05-22 14:51:39 -04004105 * lpfc_free_sgl_list - Free sgl list.
4106 * @phba: pointer to lpfc hba data structure.
4107 *
4108 * This routine is invoked to free the driver's sgl list and memory.
4109 **/
4110static void
4111lpfc_free_sgl_list(struct lpfc_hba *phba)
4112{
4113 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4114 LIST_HEAD(sglq_list);
4115 int rc = 0;
4116
4117 spin_lock_irq(&phba->hbalock);
4118 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4119 spin_unlock_irq(&phba->hbalock);
4120
4121 list_for_each_entry_safe(sglq_entry, sglq_next,
4122 &sglq_list, list) {
4123 list_del(&sglq_entry->list);
4124 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4125 kfree(sglq_entry);
4126 phba->sli4_hba.total_sglq_bufs--;
4127 }
4128 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4129 if (rc) {
4130 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004131 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004132 }
4133 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4134}
4135
4136/**
4137 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4138 * @phba: pointer to lpfc hba data structure.
4139 *
4140 * This routine is invoked to allocate the driver's active sgl memory.
4141 * This array will hold the sglq_entry's for active IOs.
4142 **/
4143static int
4144lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4145{
4146 int size;
4147 size = sizeof(struct lpfc_sglq *);
4148 size *= phba->sli4_hba.max_cfg_param.max_xri;
4149
4150 phba->sli4_hba.lpfc_sglq_active_list =
4151 kzalloc(size, GFP_KERNEL);
4152 if (!phba->sli4_hba.lpfc_sglq_active_list)
4153 return -ENOMEM;
4154 return 0;
4155}
4156
4157/**
4158 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4159 * @phba: pointer to lpfc hba data structure.
4160 *
4161 * This routine is invoked to walk through the array of active sglq entries
4162 * and free all of the resources.
4163 * This is just a place holder for now.
4164 **/
4165static void
4166lpfc_free_active_sgl(struct lpfc_hba *phba)
4167{
4168 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4169}
4170
4171/**
4172 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4173 * @phba: pointer to lpfc hba data structure.
4174 *
4175 * This routine is invoked to allocate and initizlize the driver's sgl
4176 * list and set up the sgl xritag tag array accordingly.
4177 *
4178 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004179 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004180 * other values - error
4181 **/
4182static int
4183lpfc_init_sgl_list(struct lpfc_hba *phba)
4184{
4185 struct lpfc_sglq *sglq_entry = NULL;
4186 int i;
4187 int els_xri_cnt;
4188
4189 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4190 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4191 "2400 lpfc_init_sgl_list els %d.\n",
4192 els_xri_cnt);
4193 /* Initialize and populate the sglq list per host/VF. */
4194 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4195 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4196
4197 /* Sanity check on XRI management */
4198 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4199 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4200 "2562 No room left for SCSI XRI allocation: "
4201 "max_xri=%d, els_xri=%d\n",
4202 phba->sli4_hba.max_cfg_param.max_xri,
4203 els_xri_cnt);
4204 return -ENOMEM;
4205 }
4206
4207 /* Allocate memory for the ELS XRI management array */
4208 phba->sli4_hba.lpfc_els_sgl_array =
4209 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4210 GFP_KERNEL);
4211
4212 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4213 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4214 "2401 Failed to allocate memory for ELS "
4215 "XRI management array of size %d.\n",
4216 els_xri_cnt);
4217 return -ENOMEM;
4218 }
4219
4220 /* Keep the SCSI XRI into the XRI management array */
4221 phba->sli4_hba.scsi_xri_max =
4222 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4223 phba->sli4_hba.scsi_xri_cnt = 0;
4224
4225 phba->sli4_hba.lpfc_scsi_psb_array =
4226 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4227 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4228
4229 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4230 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4231 "2563 Failed to allocate memory for SCSI "
4232 "XRI management array of size %d.\n",
4233 phba->sli4_hba.scsi_xri_max);
4234 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4235 return -ENOMEM;
4236 }
4237
4238 for (i = 0; i < els_xri_cnt; i++) {
4239 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4240 if (sglq_entry == NULL) {
4241 printk(KERN_ERR "%s: only allocated %d sgls of "
4242 "expected %d count. Unloading driver.\n",
4243 __func__, i, els_xri_cnt);
4244 goto out_free_mem;
4245 }
4246
4247 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4248 if (sglq_entry->sli4_xritag == NO_XRI) {
4249 kfree(sglq_entry);
4250 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4251 "Unloading driver.\n", __func__);
4252 goto out_free_mem;
4253 }
4254 sglq_entry->buff_type = GEN_BUFF_TYPE;
4255 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4256 if (sglq_entry->virt == NULL) {
4257 kfree(sglq_entry);
4258 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4259 "Unloading driver.\n", __func__);
4260 goto out_free_mem;
4261 }
4262 sglq_entry->sgl = sglq_entry->virt;
4263 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4264
4265 /* The list order is used by later block SGL registraton */
4266 spin_lock_irq(&phba->hbalock);
4267 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4268 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4269 phba->sli4_hba.total_sglq_bufs++;
4270 spin_unlock_irq(&phba->hbalock);
4271 }
4272 return 0;
4273
4274out_free_mem:
4275 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4276 lpfc_free_sgl_list(phba);
4277 return -ENOMEM;
4278}
4279
4280/**
4281 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4282 * @phba: pointer to lpfc hba data structure.
4283 *
4284 * This routine is invoked to post rpi header templates to the
4285 * HBA consistent with the SLI-4 interface spec. This routine
4286 * posts a PAGE_SIZE memory region to the port to hold up to
4287 * PAGE_SIZE modulo 64 rpi context headers.
4288 * No locks are held here because this is an initialization routine
4289 * called only from probe or lpfc_online when interrupts are not
4290 * enabled and the driver is reinitializing the device.
4291 *
4292 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004293 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004294 * ENOMEM - No availble memory
4295 * EIO - The mailbox failed to complete successfully.
4296 **/
4297int
4298lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4299{
4300 int rc = 0;
4301 int longs;
4302 uint16_t rpi_count;
4303 struct lpfc_rpi_hdr *rpi_hdr;
4304
4305 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4306
4307 /*
4308 * Provision an rpi bitmask range for discovery. The total count
4309 * is the difference between max and base + 1.
4310 */
4311 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4312 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4313
4314 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4315 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4316 GFP_KERNEL);
4317 if (!phba->sli4_hba.rpi_bmask)
4318 return -ENOMEM;
4319
4320 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4321 if (!rpi_hdr) {
4322 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4323 "0391 Error during rpi post operation\n");
4324 lpfc_sli4_remove_rpis(phba);
4325 rc = -ENODEV;
4326 }
4327
4328 return rc;
4329}
4330
4331/**
4332 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4333 * @phba: pointer to lpfc hba data structure.
4334 *
4335 * This routine is invoked to allocate a single 4KB memory region to
4336 * support rpis and stores them in the phba. This single region
4337 * provides support for up to 64 rpis. The region is used globally
4338 * by the device.
4339 *
4340 * Returns:
4341 * A valid rpi hdr on success.
4342 * A NULL pointer on any failure.
4343 **/
4344struct lpfc_rpi_hdr *
4345lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4346{
4347 uint16_t rpi_limit, curr_rpi_range;
4348 struct lpfc_dmabuf *dmabuf;
4349 struct lpfc_rpi_hdr *rpi_hdr;
4350
4351 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4352 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4353
4354 spin_lock_irq(&phba->hbalock);
4355 curr_rpi_range = phba->sli4_hba.next_rpi;
4356 spin_unlock_irq(&phba->hbalock);
4357
4358 /*
4359 * The port has a limited number of rpis. The increment here
4360 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4361 * and to allow the full max_rpi range per port.
4362 */
4363 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4364 return NULL;
4365
4366 /*
4367 * First allocate the protocol header region for the port. The
4368 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4369 */
4370 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4371 if (!dmabuf)
4372 return NULL;
4373
4374 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4375 LPFC_HDR_TEMPLATE_SIZE,
4376 &dmabuf->phys,
4377 GFP_KERNEL);
4378 if (!dmabuf->virt) {
4379 rpi_hdr = NULL;
4380 goto err_free_dmabuf;
4381 }
4382
4383 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4384 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4385 rpi_hdr = NULL;
4386 goto err_free_coherent;
4387 }
4388
4389 /* Save the rpi header data for cleanup later. */
4390 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4391 if (!rpi_hdr)
4392 goto err_free_coherent;
4393
4394 rpi_hdr->dmabuf = dmabuf;
4395 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4396 rpi_hdr->page_count = 1;
4397 spin_lock_irq(&phba->hbalock);
4398 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4399 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4400
4401 /*
4402 * The next_rpi stores the next module-64 rpi value to post
4403 * in any subsequent rpi memory region postings.
4404 */
4405 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4406 spin_unlock_irq(&phba->hbalock);
4407 return rpi_hdr;
4408
4409 err_free_coherent:
4410 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4411 dmabuf->virt, dmabuf->phys);
4412 err_free_dmabuf:
4413 kfree(dmabuf);
4414 return NULL;
4415}
4416
4417/**
4418 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4419 * @phba: pointer to lpfc hba data structure.
4420 *
4421 * This routine is invoked to remove all memory resources allocated
4422 * to support rpis. This routine presumes the caller has released all
4423 * rpis consumed by fabric or port logins and is prepared to have
4424 * the header pages removed.
4425 **/
4426void
4427lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4428{
4429 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4430
4431 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4432 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4433 list_del(&rpi_hdr->list);
4434 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4435 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4436 kfree(rpi_hdr->dmabuf);
4437 kfree(rpi_hdr);
4438 }
4439
4440 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4441 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4442}
4443
4444/**
James Smart3772a992009-05-22 14:50:54 -04004445 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4446 * @pdev: pointer to pci device data structure.
4447 *
4448 * This routine is invoked to allocate the driver hba data structure for an
4449 * HBA device. If the allocation is successful, the phba reference to the
4450 * PCI device data structure is set.
4451 *
4452 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004453 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004454 * NULL - error
4455 **/
4456static struct lpfc_hba *
4457lpfc_hba_alloc(struct pci_dev *pdev)
4458{
4459 struct lpfc_hba *phba;
4460
4461 /* Allocate memory for HBA structure */
4462 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4463 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004464 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004465 return NULL;
4466 }
4467
4468 /* Set reference to PCI device in HBA structure */
4469 phba->pcidev = pdev;
4470
4471 /* Assign an unused board number */
4472 phba->brd_no = lpfc_get_instance();
4473 if (phba->brd_no < 0) {
4474 kfree(phba);
4475 return NULL;
4476 }
4477
James Smart4fede782010-01-26 23:08:55 -05004478 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04004479 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4480
James Smart3772a992009-05-22 14:50:54 -04004481 return phba;
4482}
4483
4484/**
4485 * lpfc_hba_free - Free driver hba data structure with a device.
4486 * @phba: pointer to lpfc hba data structure.
4487 *
4488 * This routine is invoked to free the driver hba data structure with an
4489 * HBA device.
4490 **/
4491static void
4492lpfc_hba_free(struct lpfc_hba *phba)
4493{
4494 /* Release the driver assigned board number */
4495 idr_remove(&lpfc_hba_index, phba->brd_no);
4496
4497 kfree(phba);
4498 return;
4499}
4500
4501/**
4502 * lpfc_create_shost - Create hba physical port with associated scsi host.
4503 * @phba: pointer to lpfc hba data structure.
4504 *
4505 * This routine is invoked to create HBA physical port and associate a SCSI
4506 * host with it.
4507 *
4508 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004509 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004510 * other values - error
4511 **/
4512static int
4513lpfc_create_shost(struct lpfc_hba *phba)
4514{
4515 struct lpfc_vport *vport;
4516 struct Scsi_Host *shost;
4517
4518 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004519 phba->fc_edtov = FF_DEF_EDTOV;
4520 phba->fc_ratov = FF_DEF_RATOV;
4521 phba->fc_altov = FF_DEF_ALTOV;
4522 phba->fc_arbtov = FF_DEF_ARBTOV;
4523
James Smart3de2a652007-08-02 11:09:59 -04004524 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004525 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004526 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004527
4528 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004529 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004530 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004531 /* Put reference to SCSI host to driver's device private data */
4532 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004533
James Smart3772a992009-05-22 14:50:54 -04004534 return 0;
4535}
dea31012005-04-17 16:05:31 -05004536
James Smart3772a992009-05-22 14:50:54 -04004537/**
4538 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4539 * @phba: pointer to lpfc hba data structure.
4540 *
4541 * This routine is invoked to destroy HBA physical port and the associated
4542 * SCSI host.
4543 **/
4544static void
4545lpfc_destroy_shost(struct lpfc_hba *phba)
4546{
4547 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004548
James Smart3772a992009-05-22 14:50:54 -04004549 /* Destroy physical port that associated with the SCSI host */
4550 destroy_port(vport);
4551
4552 return;
4553}
4554
4555/**
4556 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4557 * @phba: pointer to lpfc hba data structure.
4558 * @shost: the shost to be used to detect Block guard settings.
4559 *
4560 * This routine sets up the local Block guard protocol settings for @shost.
4561 * This routine also allocates memory for debugging bg buffers.
4562 **/
4563static void
4564lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4565{
4566 int pagecnt = 10;
4567 if (lpfc_prot_mask && lpfc_prot_guard) {
4568 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4569 "1478 Registering BlockGuard with the "
4570 "SCSI layer\n");
4571 scsi_host_set_prot(shost, lpfc_prot_mask);
4572 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004573 }
James Smart81301a92008-12-04 22:39:46 -05004574 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004575 while (pagecnt) {
4576 spin_lock_init(&_dump_buf_lock);
4577 _dump_buf_data =
4578 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4579 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004580 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4581 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004582 "_dump_buf_data at 0x%p\n",
4583 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004584 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004585 memset(_dump_buf_data, 0,
4586 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004587 break;
James Smart3772a992009-05-22 14:50:54 -04004588 } else
James Smart81301a92008-12-04 22:39:46 -05004589 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004590 }
James Smart81301a92008-12-04 22:39:46 -05004591 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004592 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4593 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004594 "memory for hexdump\n");
4595 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004596 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4597 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004598 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004599 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004600 while (pagecnt) {
4601 _dump_buf_dif =
4602 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4603 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004604 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4605 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004606 "_dump_buf_dif at 0x%p\n",
4607 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004608 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004609 memset(_dump_buf_dif, 0,
4610 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004611 break;
James Smart3772a992009-05-22 14:50:54 -04004612 } else
James Smart81301a92008-12-04 22:39:46 -05004613 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004614 }
James Smart81301a92008-12-04 22:39:46 -05004615 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004616 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4617 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004618 "memory for hexdump\n");
4619 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004620 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4621 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004622 _dump_buf_dif);
4623}
4624
4625/**
4626 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4627 * @phba: pointer to lpfc hba data structure.
4628 *
4629 * This routine is invoked to perform all the necessary post initialization
4630 * setup for the device.
4631 **/
4632static void
4633lpfc_post_init_setup(struct lpfc_hba *phba)
4634{
4635 struct Scsi_Host *shost;
4636 struct lpfc_adapter_event_header adapter_event;
4637
4638 /* Get the default values for Model Name and Description */
4639 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4640
4641 /*
4642 * hba setup may have changed the hba_queue_depth so we need to
4643 * adjust the value of can_queue.
4644 */
4645 shost = pci_get_drvdata(phba->pcidev);
4646 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4647 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4648 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004649
4650 lpfc_host_attrib_init(shost);
4651
James Smart2e0fef82007-06-17 19:56:36 -05004652 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4653 spin_lock_irq(shost->host_lock);
4654 lpfc_poll_start_timer(phba);
4655 spin_unlock_irq(shost->host_lock);
4656 }
James Smart8f6d98d2006-08-01 07:34:00 -04004657
James Smart93996272008-08-24 21:50:30 -04004658 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4659 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004660 /* Send board arrival event to upper layer */
4661 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4662 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4663 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004664 sizeof(adapter_event),
4665 (char *) &adapter_event,
4666 LPFC_NL_VENDOR_ID);
4667 return;
4668}
4669
4670/**
4671 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4672 * @phba: pointer to lpfc hba data structure.
4673 *
4674 * This routine is invoked to set up the PCI device memory space for device
4675 * with SLI-3 interface spec.
4676 *
4677 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004678 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004679 * other values - error
4680 **/
4681static int
4682lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4683{
4684 struct pci_dev *pdev;
4685 unsigned long bar0map_len, bar2map_len;
4686 int i, hbq_count;
4687 void *ptr;
4688 int error = -ENODEV;
4689
4690 /* Obtain PCI device reference */
4691 if (!phba->pcidev)
4692 return error;
4693 else
4694 pdev = phba->pcidev;
4695
4696 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05004697 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
4698 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
4699 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
4700 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04004701 return error;
Michael Reed8e685972009-09-18 12:02:05 -05004702 }
4703 }
James Smart3772a992009-05-22 14:50:54 -04004704
4705 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4706 * required by each mapping.
4707 */
4708 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4709 bar0map_len = pci_resource_len(pdev, 0);
4710
4711 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4712 bar2map_len = pci_resource_len(pdev, 2);
4713
4714 /* Map HBA SLIM to a kernel virtual address. */
4715 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4716 if (!phba->slim_memmap_p) {
4717 dev_printk(KERN_ERR, &pdev->dev,
4718 "ioremap failed for SLIM memory.\n");
4719 goto out;
4720 }
4721
4722 /* Map HBA Control Registers to a kernel virtual address. */
4723 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4724 if (!phba->ctrl_regs_memmap_p) {
4725 dev_printk(KERN_ERR, &pdev->dev,
4726 "ioremap failed for HBA control registers.\n");
4727 goto out_iounmap_slim;
4728 }
4729
4730 /* Allocate memory for SLI-2 structures */
4731 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4732 SLI2_SLIM_SIZE,
4733 &phba->slim2p.phys,
4734 GFP_KERNEL);
4735 if (!phba->slim2p.virt)
4736 goto out_iounmap;
4737
4738 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4739 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4740 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4741 phba->IOCBs = (phba->slim2p.virt +
4742 offsetof(struct lpfc_sli2_slim, IOCBs));
4743
4744 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4745 lpfc_sli_hbq_size(),
4746 &phba->hbqslimp.phys,
4747 GFP_KERNEL);
4748 if (!phba->hbqslimp.virt)
4749 goto out_free_slim;
4750
4751 hbq_count = lpfc_sli_hbq_count();
4752 ptr = phba->hbqslimp.virt;
4753 for (i = 0; i < hbq_count; ++i) {
4754 phba->hbqs[i].hbq_virt = ptr;
4755 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4756 ptr += (lpfc_hbq_defs[i]->entry_count *
4757 sizeof(struct lpfc_hbq_entry));
4758 }
4759 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4760 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4761
4762 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4763
4764 INIT_LIST_HEAD(&phba->rb_pend_list);
4765
4766 phba->MBslimaddr = phba->slim_memmap_p;
4767 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4768 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4769 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4770 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004771
dea31012005-04-17 16:05:31 -05004772 return 0;
4773
dea31012005-04-17 16:05:31 -05004774out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004775 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4776 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004777out_iounmap:
4778 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004779out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004780 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004781out:
4782 return error;
4783}
4784
James Smarte59058c2008-08-24 21:49:00 -04004785/**
James Smart3772a992009-05-22 14:50:54 -04004786 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4787 * @phba: pointer to lpfc hba data structure.
4788 *
4789 * This routine is invoked to unset the PCI device memory space for device
4790 * with SLI-3 interface spec.
4791 **/
4792static void
4793lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4794{
4795 struct pci_dev *pdev;
4796
4797 /* Obtain PCI device reference */
4798 if (!phba->pcidev)
4799 return;
4800 else
4801 pdev = phba->pcidev;
4802
4803 /* Free coherent DMA memory allocated */
4804 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4805 phba->hbqslimp.virt, phba->hbqslimp.phys);
4806 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4807 phba->slim2p.virt, phba->slim2p.phys);
4808
4809 /* I/O memory unmap */
4810 iounmap(phba->ctrl_regs_memmap_p);
4811 iounmap(phba->slim_memmap_p);
4812
4813 return;
4814}
4815
4816/**
James Smartda0436e2009-05-22 14:51:39 -04004817 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4818 * @phba: pointer to lpfc hba data structure.
4819 *
4820 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4821 * done and check status.
4822 *
4823 * Return 0 if successful, otherwise -ENODEV.
4824 **/
4825int
4826lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4827{
4828 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg, scratchpad;
James Smartda0436e2009-05-22 14:51:39 -04004829 int i, port_error = -ENODEV;
4830
4831 if (!phba->sli4_hba.STAregaddr)
4832 return -ENODEV;
4833
James Smartda0436e2009-05-22 14:51:39 -04004834 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4835 for (i = 0; i < 3000; i++) {
4836 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4837 /* Encounter fatal POST error, break out */
4838 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4839 port_error = -ENODEV;
4840 break;
4841 }
4842 if (LPFC_POST_STAGE_ARMFW_READY ==
4843 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4844 port_error = 0;
4845 break;
4846 }
4847 msleep(10);
4848 }
4849
4850 if (port_error)
4851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4852 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4853 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4854 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4855 bf_get(lpfc_hst_state_perr, &sta_reg),
4856 bf_get(lpfc_hst_state_sfi, &sta_reg),
4857 bf_get(lpfc_hst_state_nip, &sta_reg),
4858 bf_get(lpfc_hst_state_ipc, &sta_reg),
4859 bf_get(lpfc_hst_state_xrom, &sta_reg),
4860 bf_get(lpfc_hst_state_dl, &sta_reg),
4861 bf_get(lpfc_hst_state_port_status, &sta_reg));
4862
4863 /* Log device information */
4864 scratchpad.word0 = readl(phba->sli4_hba.SCRATCHPADregaddr);
4865 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4866 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4867 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4868 bf_get(lpfc_scratchpad_chiptype, &scratchpad),
4869 bf_get(lpfc_scratchpad_slirev, &scratchpad),
4870 bf_get(lpfc_scratchpad_featurelevel1, &scratchpad),
4871 bf_get(lpfc_scratchpad_featurelevel2, &scratchpad));
James Smarta747c9c2009-11-18 15:41:10 -05004872 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
4873 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04004874 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05004875 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4876 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4877 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
4878 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
4879 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4880 "1422 HBA Unrecoverable error: "
4881 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4882 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
4883 uerrlo_reg.word0, uerrhi_reg.word0,
4884 phba->sli4_hba.ue_mask_lo,
4885 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04004886 return -ENODEV;
4887 }
4888
James Smartda0436e2009-05-22 14:51:39 -04004889 return port_error;
4890}
4891
4892/**
4893 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4894 * @phba: pointer to lpfc hba data structure.
4895 *
4896 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4897 * memory map.
4898 **/
4899static void
4900lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4901{
4902 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4903 LPFC_UERR_STATUS_LO;
4904 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4905 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05004906 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4907 LPFC_UE_MASK_LO;
4908 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4909 LPFC_UE_MASK_HI;
James Smartda0436e2009-05-22 14:51:39 -04004910 phba->sli4_hba.SCRATCHPADregaddr = phba->sli4_hba.conf_regs_memmap_p +
4911 LPFC_SCRATCHPAD;
4912}
4913
4914/**
4915 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4916 * @phba: pointer to lpfc hba data structure.
4917 *
4918 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4919 * memory map.
4920 **/
4921static void
4922lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4923{
4924
4925 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4926 LPFC_HST_STATE;
4927 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4928 LPFC_HST_ISR0;
4929 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4930 LPFC_HST_IMR0;
4931 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4932 LPFC_HST_ISCR0;
4933 return;
4934}
4935
4936/**
4937 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
4938 * @phba: pointer to lpfc hba data structure.
4939 * @vf: virtual function number
4940 *
4941 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4942 * based on the given viftual function number, @vf.
4943 *
4944 * Return 0 if successful, otherwise -ENODEV.
4945 **/
4946static int
4947lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
4948{
4949 if (vf > LPFC_VIR_FUNC_MAX)
4950 return -ENODEV;
4951
4952 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4953 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4954 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4955 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4956 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4957 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
4958 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4959 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
4960 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4961 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
4962 return 0;
4963}
4964
4965/**
4966 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
4967 * @phba: pointer to lpfc hba data structure.
4968 *
4969 * This routine is invoked to create the bootstrap mailbox
4970 * region consistent with the SLI-4 interface spec. This
4971 * routine allocates all memory necessary to communicate
4972 * mailbox commands to the port and sets up all alignment
4973 * needs. No locks are expected to be held when calling
4974 * this routine.
4975 *
4976 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004977 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004978 * ENOMEM - could not allocated memory.
4979 **/
4980static int
4981lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
4982{
4983 uint32_t bmbx_size;
4984 struct lpfc_dmabuf *dmabuf;
4985 struct dma_address *dma_address;
4986 uint32_t pa_addr;
4987 uint64_t phys_addr;
4988
4989 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4990 if (!dmabuf)
4991 return -ENOMEM;
4992
4993 /*
4994 * The bootstrap mailbox region is comprised of 2 parts
4995 * plus an alignment restriction of 16 bytes.
4996 */
4997 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
4998 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4999 bmbx_size,
5000 &dmabuf->phys,
5001 GFP_KERNEL);
5002 if (!dmabuf->virt) {
5003 kfree(dmabuf);
5004 return -ENOMEM;
5005 }
5006 memset(dmabuf->virt, 0, bmbx_size);
5007
5008 /*
5009 * Initialize the bootstrap mailbox pointers now so that the register
5010 * operations are simple later. The mailbox dma address is required
5011 * to be 16-byte aligned. Also align the virtual memory as each
5012 * maibox is copied into the bmbx mailbox region before issuing the
5013 * command to the port.
5014 */
5015 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5016 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5017
5018 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5019 LPFC_ALIGN_16_BYTE);
5020 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5021 LPFC_ALIGN_16_BYTE);
5022
5023 /*
5024 * Set the high and low physical addresses now. The SLI4 alignment
5025 * requirement is 16 bytes and the mailbox is posted to the port
5026 * as two 30-bit addresses. The other data is a bit marking whether
5027 * the 30-bit address is the high or low address.
5028 * Upcast bmbx aphys to 64bits so shift instruction compiles
5029 * clean on 32 bit machines.
5030 */
5031 dma_address = &phba->sli4_hba.bmbx.dma_address;
5032 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5033 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5034 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5035 LPFC_BMBX_BIT1_ADDR_HI);
5036
5037 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5038 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5039 LPFC_BMBX_BIT1_ADDR_LO);
5040 return 0;
5041}
5042
5043/**
5044 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5045 * @phba: pointer to lpfc hba data structure.
5046 *
5047 * This routine is invoked to teardown the bootstrap mailbox
5048 * region and release all host resources. This routine requires
5049 * the caller to ensure all mailbox commands recovered, no
5050 * additional mailbox comands are sent, and interrupts are disabled
5051 * before calling this routine.
5052 *
5053 **/
5054static void
5055lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5056{
5057 dma_free_coherent(&phba->pcidev->dev,
5058 phba->sli4_hba.bmbx.bmbx_size,
5059 phba->sli4_hba.bmbx.dmabuf->virt,
5060 phba->sli4_hba.bmbx.dmabuf->phys);
5061
5062 kfree(phba->sli4_hba.bmbx.dmabuf);
5063 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5064}
5065
5066/**
5067 * lpfc_sli4_read_config - Get the config parameters.
5068 * @phba: pointer to lpfc hba data structure.
5069 *
5070 * This routine is invoked to read the configuration parameters from the HBA.
5071 * The configuration parameters are used to set the base and maximum values
5072 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5073 * allocation for the port.
5074 *
5075 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005076 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005077 * ENOMEM - No availble memory
5078 * EIO - The mailbox failed to complete successfully.
5079 **/
5080static int
5081lpfc_sli4_read_config(struct lpfc_hba *phba)
5082{
5083 LPFC_MBOXQ_t *pmb;
5084 struct lpfc_mbx_read_config *rd_config;
5085 uint32_t rc = 0;
5086
5087 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5088 if (!pmb) {
5089 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5090 "2011 Unable to allocate memory for issuing "
5091 "SLI_CONFIG_SPECIAL mailbox command\n");
5092 return -ENOMEM;
5093 }
5094
5095 lpfc_read_config(phba, pmb);
5096
5097 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5098 if (rc != MBX_SUCCESS) {
5099 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5100 "2012 Mailbox failed , mbxCmd x%x "
5101 "READ_CONFIG, mbxStatus x%x\n",
5102 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5103 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5104 rc = -EIO;
5105 } else {
5106 rd_config = &pmb->u.mqe.un.rd_config;
5107 phba->sli4_hba.max_cfg_param.max_xri =
5108 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5109 phba->sli4_hba.max_cfg_param.xri_base =
5110 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5111 phba->sli4_hba.max_cfg_param.max_vpi =
5112 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5113 phba->sli4_hba.max_cfg_param.vpi_base =
5114 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5115 phba->sli4_hba.max_cfg_param.max_rpi =
5116 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5117 phba->sli4_hba.max_cfg_param.rpi_base =
5118 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5119 phba->sli4_hba.max_cfg_param.max_vfi =
5120 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5121 phba->sli4_hba.max_cfg_param.vfi_base =
5122 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5123 phba->sli4_hba.max_cfg_param.max_fcfi =
5124 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5125 phba->sli4_hba.max_cfg_param.fcfi_base =
5126 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5127 phba->sli4_hba.max_cfg_param.max_eq =
5128 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5129 phba->sli4_hba.max_cfg_param.max_rq =
5130 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5131 phba->sli4_hba.max_cfg_param.max_wq =
5132 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5133 phba->sli4_hba.max_cfg_param.max_cq =
5134 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5135 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5136 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5137 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5138 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5139 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005140 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5141 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005142 phba->max_vports = phba->max_vpi;
5143 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5144 "2003 cfg params XRI(B:%d M:%d), "
5145 "VPI(B:%d M:%d) "
5146 "VFI(B:%d M:%d) "
5147 "RPI(B:%d M:%d) "
5148 "FCFI(B:%d M:%d)\n",
5149 phba->sli4_hba.max_cfg_param.xri_base,
5150 phba->sli4_hba.max_cfg_param.max_xri,
5151 phba->sli4_hba.max_cfg_param.vpi_base,
5152 phba->sli4_hba.max_cfg_param.max_vpi,
5153 phba->sli4_hba.max_cfg_param.vfi_base,
5154 phba->sli4_hba.max_cfg_param.max_vfi,
5155 phba->sli4_hba.max_cfg_param.rpi_base,
5156 phba->sli4_hba.max_cfg_param.max_rpi,
5157 phba->sli4_hba.max_cfg_param.fcfi_base,
5158 phba->sli4_hba.max_cfg_param.max_fcfi);
5159 }
5160 mempool_free(pmb, phba->mbox_mem_pool);
5161
5162 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5163 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5164 phba->cfg_hba_queue_depth =
5165 phba->sli4_hba.max_cfg_param.max_xri;
5166 return rc;
5167}
5168
5169/**
5170 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5171 * @phba: pointer to lpfc hba data structure.
5172 *
5173 * This routine is invoked to setup the host-side endian order to the
5174 * HBA consistent with the SLI-4 interface spec.
5175 *
5176 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005177 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005178 * ENOMEM - No availble memory
5179 * EIO - The mailbox failed to complete successfully.
5180 **/
5181static int
5182lpfc_setup_endian_order(struct lpfc_hba *phba)
5183{
5184 LPFC_MBOXQ_t *mboxq;
5185 uint32_t rc = 0;
5186 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5187 HOST_ENDIAN_HIGH_WORD1};
5188
5189 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5190 if (!mboxq) {
5191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5192 "0492 Unable to allocate memory for issuing "
5193 "SLI_CONFIG_SPECIAL mailbox command\n");
5194 return -ENOMEM;
5195 }
5196
5197 /*
5198 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5199 * words to contain special data values and no other data.
5200 */
5201 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5202 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5203 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5204 if (rc != MBX_SUCCESS) {
5205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5206 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5207 "status x%x\n",
5208 rc);
5209 rc = -EIO;
5210 }
5211
5212 mempool_free(mboxq, phba->mbox_mem_pool);
5213 return rc;
5214}
5215
5216/**
5217 * lpfc_sli4_queue_create - Create all the SLI4 queues
5218 * @phba: pointer to lpfc hba data structure.
5219 *
5220 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5221 * operation. For each SLI4 queue type, the parameters such as queue entry
5222 * count (queue depth) shall be taken from the module parameter. For now,
5223 * we just use some constant number as place holder.
5224 *
5225 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005226 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005227 * ENOMEM - No availble memory
5228 * EIO - The mailbox failed to complete successfully.
5229 **/
5230static int
5231lpfc_sli4_queue_create(struct lpfc_hba *phba)
5232{
5233 struct lpfc_queue *qdesc;
5234 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5235 int cfg_fcp_wq_count;
5236 int cfg_fcp_eq_count;
5237
5238 /*
5239 * Sanity check for confiugred queue parameters against the run-time
5240 * device parameters
5241 */
5242
5243 /* Sanity check on FCP fast-path WQ parameters */
5244 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5245 if (cfg_fcp_wq_count >
5246 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5247 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5248 LPFC_SP_WQN_DEF;
5249 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5251 "2581 Not enough WQs (%d) from "
5252 "the pci function for supporting "
5253 "FCP WQs (%d)\n",
5254 phba->sli4_hba.max_cfg_param.max_wq,
5255 phba->cfg_fcp_wq_count);
5256 goto out_error;
5257 }
5258 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5259 "2582 Not enough WQs (%d) from the pci "
5260 "function for supporting the requested "
5261 "FCP WQs (%d), the actual FCP WQs can "
5262 "be supported: %d\n",
5263 phba->sli4_hba.max_cfg_param.max_wq,
5264 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5265 }
5266 /* The actual number of FCP work queues adopted */
5267 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5268
5269 /* Sanity check on FCP fast-path EQ parameters */
5270 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5271 if (cfg_fcp_eq_count >
5272 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5273 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5274 LPFC_SP_EQN_DEF;
5275 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5277 "2574 Not enough EQs (%d) from the "
5278 "pci function for supporting FCP "
5279 "EQs (%d)\n",
5280 phba->sli4_hba.max_cfg_param.max_eq,
5281 phba->cfg_fcp_eq_count);
5282 goto out_error;
5283 }
5284 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5285 "2575 Not enough EQs (%d) from the pci "
5286 "function for supporting the requested "
5287 "FCP EQs (%d), the actual FCP EQs can "
5288 "be supported: %d\n",
5289 phba->sli4_hba.max_cfg_param.max_eq,
5290 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5291 }
5292 /* It does not make sense to have more EQs than WQs */
5293 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5294 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005295 "2593 The FCP EQ count(%d) cannot be greater "
5296 "than the FCP WQ count(%d), limiting the "
5297 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005298 phba->cfg_fcp_wq_count,
5299 phba->cfg_fcp_wq_count);
5300 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5301 }
5302 /* The actual number of FCP event queues adopted */
5303 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5304 /* The overall number of event queues used */
5305 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5306
5307 /*
5308 * Create Event Queues (EQs)
5309 */
5310
5311 /* Get EQ depth from module parameter, fake the default for now */
5312 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5313 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5314
5315 /* Create slow path event queue */
5316 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5317 phba->sli4_hba.eq_ecount);
5318 if (!qdesc) {
5319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5320 "0496 Failed allocate slow-path EQ\n");
5321 goto out_error;
5322 }
5323 phba->sli4_hba.sp_eq = qdesc;
5324
5325 /* Create fast-path FCP Event Queue(s) */
5326 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5327 phba->cfg_fcp_eq_count), GFP_KERNEL);
5328 if (!phba->sli4_hba.fp_eq) {
5329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5330 "2576 Failed allocate memory for fast-path "
5331 "EQ record array\n");
5332 goto out_free_sp_eq;
5333 }
5334 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5335 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5336 phba->sli4_hba.eq_ecount);
5337 if (!qdesc) {
5338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5339 "0497 Failed allocate fast-path EQ\n");
5340 goto out_free_fp_eq;
5341 }
5342 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5343 }
5344
5345 /*
5346 * Create Complete Queues (CQs)
5347 */
5348
5349 /* Get CQ depth from module parameter, fake the default for now */
5350 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5351 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5352
5353 /* Create slow-path Mailbox Command Complete Queue */
5354 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5355 phba->sli4_hba.cq_ecount);
5356 if (!qdesc) {
5357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5358 "0500 Failed allocate slow-path mailbox CQ\n");
5359 goto out_free_fp_eq;
5360 }
5361 phba->sli4_hba.mbx_cq = qdesc;
5362
5363 /* Create slow-path ELS Complete Queue */
5364 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5365 phba->sli4_hba.cq_ecount);
5366 if (!qdesc) {
5367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5368 "0501 Failed allocate slow-path ELS CQ\n");
5369 goto out_free_mbx_cq;
5370 }
5371 phba->sli4_hba.els_cq = qdesc;
5372
James Smartda0436e2009-05-22 14:51:39 -04005373
5374 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5375 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5376 phba->cfg_fcp_eq_count), GFP_KERNEL);
5377 if (!phba->sli4_hba.fcp_cq) {
5378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5379 "2577 Failed allocate memory for fast-path "
5380 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005381 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005382 }
5383 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5384 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5385 phba->sli4_hba.cq_ecount);
5386 if (!qdesc) {
5387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5388 "0499 Failed allocate fast-path FCP "
5389 "CQ (%d)\n", fcp_cqidx);
5390 goto out_free_fcp_cq;
5391 }
5392 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5393 }
5394
5395 /* Create Mailbox Command Queue */
5396 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5397 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5398
5399 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5400 phba->sli4_hba.mq_ecount);
5401 if (!qdesc) {
5402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5403 "0505 Failed allocate slow-path MQ\n");
5404 goto out_free_fcp_cq;
5405 }
5406 phba->sli4_hba.mbx_wq = qdesc;
5407
5408 /*
5409 * Create all the Work Queues (WQs)
5410 */
5411 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5412 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5413
5414 /* Create slow-path ELS Work Queue */
5415 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5416 phba->sli4_hba.wq_ecount);
5417 if (!qdesc) {
5418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5419 "0504 Failed allocate slow-path ELS WQ\n");
5420 goto out_free_mbx_wq;
5421 }
5422 phba->sli4_hba.els_wq = qdesc;
5423
5424 /* Create fast-path FCP Work Queue(s) */
5425 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5426 phba->cfg_fcp_wq_count), GFP_KERNEL);
5427 if (!phba->sli4_hba.fcp_wq) {
5428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5429 "2578 Failed allocate memory for fast-path "
5430 "WQ record array\n");
5431 goto out_free_els_wq;
5432 }
5433 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5434 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5435 phba->sli4_hba.wq_ecount);
5436 if (!qdesc) {
5437 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5438 "0503 Failed allocate fast-path FCP "
5439 "WQ (%d)\n", fcp_wqidx);
5440 goto out_free_fcp_wq;
5441 }
5442 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5443 }
5444
5445 /*
5446 * Create Receive Queue (RQ)
5447 */
5448 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5449 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5450
5451 /* Create Receive Queue for header */
5452 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5453 phba->sli4_hba.rq_ecount);
5454 if (!qdesc) {
5455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5456 "0506 Failed allocate receive HRQ\n");
5457 goto out_free_fcp_wq;
5458 }
5459 phba->sli4_hba.hdr_rq = qdesc;
5460
5461 /* Create Receive Queue for data */
5462 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5463 phba->sli4_hba.rq_ecount);
5464 if (!qdesc) {
5465 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5466 "0507 Failed allocate receive DRQ\n");
5467 goto out_free_hdr_rq;
5468 }
5469 phba->sli4_hba.dat_rq = qdesc;
5470
5471 return 0;
5472
5473out_free_hdr_rq:
5474 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5475 phba->sli4_hba.hdr_rq = NULL;
5476out_free_fcp_wq:
5477 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5478 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5479 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5480 }
5481 kfree(phba->sli4_hba.fcp_wq);
5482out_free_els_wq:
5483 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5484 phba->sli4_hba.els_wq = NULL;
5485out_free_mbx_wq:
5486 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5487 phba->sli4_hba.mbx_wq = NULL;
5488out_free_fcp_cq:
5489 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5490 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5491 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5492 }
5493 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005494out_free_els_cq:
5495 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5496 phba->sli4_hba.els_cq = NULL;
5497out_free_mbx_cq:
5498 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5499 phba->sli4_hba.mbx_cq = NULL;
5500out_free_fp_eq:
5501 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5502 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5503 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5504 }
5505 kfree(phba->sli4_hba.fp_eq);
5506out_free_sp_eq:
5507 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5508 phba->sli4_hba.sp_eq = NULL;
5509out_error:
5510 return -ENOMEM;
5511}
5512
5513/**
5514 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5515 * @phba: pointer to lpfc hba data structure.
5516 *
5517 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5518 * operation.
5519 *
5520 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005521 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005522 * ENOMEM - No availble memory
5523 * EIO - The mailbox failed to complete successfully.
5524 **/
5525static void
5526lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5527{
5528 int fcp_qidx;
5529
5530 /* Release mailbox command work queue */
5531 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5532 phba->sli4_hba.mbx_wq = NULL;
5533
5534 /* Release ELS work queue */
5535 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5536 phba->sli4_hba.els_wq = NULL;
5537
5538 /* Release FCP work queue */
5539 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5540 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5541 kfree(phba->sli4_hba.fcp_wq);
5542 phba->sli4_hba.fcp_wq = NULL;
5543
5544 /* Release unsolicited receive queue */
5545 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5546 phba->sli4_hba.hdr_rq = NULL;
5547 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5548 phba->sli4_hba.dat_rq = NULL;
5549
James Smartda0436e2009-05-22 14:51:39 -04005550 /* Release ELS complete queue */
5551 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5552 phba->sli4_hba.els_cq = NULL;
5553
5554 /* Release mailbox command complete queue */
5555 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5556 phba->sli4_hba.mbx_cq = NULL;
5557
5558 /* Release FCP response complete queue */
5559 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5560 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5561 kfree(phba->sli4_hba.fcp_cq);
5562 phba->sli4_hba.fcp_cq = NULL;
5563
5564 /* Release fast-path event queue */
5565 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5566 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5567 kfree(phba->sli4_hba.fp_eq);
5568 phba->sli4_hba.fp_eq = NULL;
5569
5570 /* Release slow-path event queue */
5571 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5572 phba->sli4_hba.sp_eq = NULL;
5573
5574 return;
5575}
5576
5577/**
5578 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5579 * @phba: pointer to lpfc hba data structure.
5580 *
5581 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5582 * operation.
5583 *
5584 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005585 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005586 * ENOMEM - No availble memory
5587 * EIO - The mailbox failed to complete successfully.
5588 **/
5589int
5590lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5591{
5592 int rc = -ENOMEM;
5593 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5594 int fcp_cq_index = 0;
5595
5596 /*
5597 * Set up Event Queues (EQs)
5598 */
5599
5600 /* Set up slow-path event queue */
5601 if (!phba->sli4_hba.sp_eq) {
5602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5603 "0520 Slow-path EQ not allocated\n");
5604 goto out_error;
5605 }
5606 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5607 LPFC_SP_DEF_IMAX);
5608 if (rc) {
5609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5610 "0521 Failed setup of slow-path EQ: "
5611 "rc = 0x%x\n", rc);
5612 goto out_error;
5613 }
5614 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5615 "2583 Slow-path EQ setup: queue-id=%d\n",
5616 phba->sli4_hba.sp_eq->queue_id);
5617
5618 /* Set up fast-path event queue */
5619 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5620 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5621 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5622 "0522 Fast-path EQ (%d) not "
5623 "allocated\n", fcp_eqidx);
5624 goto out_destroy_fp_eq;
5625 }
5626 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5627 phba->cfg_fcp_imax);
5628 if (rc) {
5629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5630 "0523 Failed setup of fast-path EQ "
5631 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5632 goto out_destroy_fp_eq;
5633 }
5634 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5635 "2584 Fast-path EQ setup: "
5636 "queue[%d]-id=%d\n", fcp_eqidx,
5637 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5638 }
5639
5640 /*
5641 * Set up Complete Queues (CQs)
5642 */
5643
5644 /* Set up slow-path MBOX Complete Queue as the first CQ */
5645 if (!phba->sli4_hba.mbx_cq) {
5646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5647 "0528 Mailbox CQ not allocated\n");
5648 goto out_destroy_fp_eq;
5649 }
5650 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5651 LPFC_MCQ, LPFC_MBOX);
5652 if (rc) {
5653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5654 "0529 Failed setup of slow-path mailbox CQ: "
5655 "rc = 0x%x\n", rc);
5656 goto out_destroy_fp_eq;
5657 }
5658 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5659 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5660 phba->sli4_hba.mbx_cq->queue_id,
5661 phba->sli4_hba.sp_eq->queue_id);
5662
5663 /* Set up slow-path ELS Complete Queue */
5664 if (!phba->sli4_hba.els_cq) {
5665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5666 "0530 ELS CQ not allocated\n");
5667 goto out_destroy_mbx_cq;
5668 }
5669 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5670 LPFC_WCQ, LPFC_ELS);
5671 if (rc) {
5672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5673 "0531 Failed setup of slow-path ELS CQ: "
5674 "rc = 0x%x\n", rc);
5675 goto out_destroy_mbx_cq;
5676 }
5677 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5678 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5679 phba->sli4_hba.els_cq->queue_id,
5680 phba->sli4_hba.sp_eq->queue_id);
5681
James Smartda0436e2009-05-22 14:51:39 -04005682 /* Set up fast-path FCP Response Complete Queue */
5683 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5684 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5685 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5686 "0526 Fast-path FCP CQ (%d) not "
5687 "allocated\n", fcp_cqidx);
5688 goto out_destroy_fcp_cq;
5689 }
5690 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5691 phba->sli4_hba.fp_eq[fcp_cqidx],
5692 LPFC_WCQ, LPFC_FCP);
5693 if (rc) {
5694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5695 "0527 Failed setup of fast-path FCP "
5696 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5697 goto out_destroy_fcp_cq;
5698 }
5699 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5700 "2588 FCP CQ setup: cq[%d]-id=%d, "
5701 "parent eq[%d]-id=%d\n",
5702 fcp_cqidx,
5703 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5704 fcp_cqidx,
5705 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5706 }
5707
5708 /*
5709 * Set up all the Work Queues (WQs)
5710 */
5711
5712 /* Set up Mailbox Command Queue */
5713 if (!phba->sli4_hba.mbx_wq) {
5714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5715 "0538 Slow-path MQ not allocated\n");
5716 goto out_destroy_fcp_cq;
5717 }
5718 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5719 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5720 if (rc) {
5721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5722 "0539 Failed setup of slow-path MQ: "
5723 "rc = 0x%x\n", rc);
5724 goto out_destroy_fcp_cq;
5725 }
5726 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5727 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5728 phba->sli4_hba.mbx_wq->queue_id,
5729 phba->sli4_hba.mbx_cq->queue_id);
5730
5731 /* Set up slow-path ELS Work Queue */
5732 if (!phba->sli4_hba.els_wq) {
5733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5734 "0536 Slow-path ELS WQ not allocated\n");
5735 goto out_destroy_mbx_wq;
5736 }
5737 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5738 phba->sli4_hba.els_cq, LPFC_ELS);
5739 if (rc) {
5740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5741 "0537 Failed setup of slow-path ELS WQ: "
5742 "rc = 0x%x\n", rc);
5743 goto out_destroy_mbx_wq;
5744 }
5745 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5746 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5747 phba->sli4_hba.els_wq->queue_id,
5748 phba->sli4_hba.els_cq->queue_id);
5749
5750 /* Set up fast-path FCP Work Queue */
5751 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5752 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5754 "0534 Fast-path FCP WQ (%d) not "
5755 "allocated\n", fcp_wqidx);
5756 goto out_destroy_fcp_wq;
5757 }
5758 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5759 phba->sli4_hba.fcp_cq[fcp_cq_index],
5760 LPFC_FCP);
5761 if (rc) {
5762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5763 "0535 Failed setup of fast-path FCP "
5764 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5765 goto out_destroy_fcp_wq;
5766 }
5767 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5768 "2591 FCP WQ setup: wq[%d]-id=%d, "
5769 "parent cq[%d]-id=%d\n",
5770 fcp_wqidx,
5771 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5772 fcp_cq_index,
5773 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5774 /* Round robin FCP Work Queue's Completion Queue assignment */
5775 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5776 }
5777
5778 /*
5779 * Create Receive Queue (RQ)
5780 */
5781 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5783 "0540 Receive Queue not allocated\n");
5784 goto out_destroy_fcp_wq;
5785 }
5786 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04005787 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04005788 if (rc) {
5789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5790 "0541 Failed setup of Receive Queue: "
5791 "rc = 0x%x\n", rc);
5792 goto out_destroy_fcp_wq;
5793 }
5794 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5795 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5796 "parent cq-id=%d\n",
5797 phba->sli4_hba.hdr_rq->queue_id,
5798 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04005799 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04005800 return 0;
5801
5802out_destroy_fcp_wq:
5803 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5804 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5805 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5806out_destroy_mbx_wq:
5807 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5808out_destroy_fcp_cq:
5809 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5810 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04005811 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5812out_destroy_mbx_cq:
5813 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5814out_destroy_fp_eq:
5815 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5816 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5817 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5818out_error:
5819 return rc;
5820}
5821
5822/**
5823 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5824 * @phba: pointer to lpfc hba data structure.
5825 *
5826 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5827 * operation.
5828 *
5829 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005830 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005831 * ENOMEM - No availble memory
5832 * EIO - The mailbox failed to complete successfully.
5833 **/
5834void
5835lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5836{
5837 int fcp_qidx;
5838
5839 /* Unset mailbox command work queue */
5840 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5841 /* Unset ELS work queue */
5842 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5843 /* Unset unsolicited receive queue */
5844 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5845 /* Unset FCP work queue */
5846 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5847 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5848 /* Unset mailbox command complete queue */
5849 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5850 /* Unset ELS complete queue */
5851 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04005852 /* Unset FCP response complete queue */
5853 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5854 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5855 /* Unset fast-path event queue */
5856 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5857 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5858 /* Unset slow-path event queue */
5859 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5860}
5861
5862/**
5863 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5864 * @phba: pointer to lpfc hba data structure.
5865 *
5866 * This routine is invoked to allocate and set up a pool of completion queue
5867 * events. The body of the completion queue event is a completion queue entry
5868 * CQE. For now, this pool is used for the interrupt service routine to queue
5869 * the following HBA completion queue events for the worker thread to process:
5870 * - Mailbox asynchronous events
5871 * - Receive queue completion unsolicited events
5872 * Later, this can be used for all the slow-path events.
5873 *
5874 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005875 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005876 * -ENOMEM - No availble memory
5877 **/
5878static int
5879lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5880{
5881 struct lpfc_cq_event *cq_event;
5882 int i;
5883
5884 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5885 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5886 if (!cq_event)
5887 goto out_pool_create_fail;
5888 list_add_tail(&cq_event->list,
5889 &phba->sli4_hba.sp_cqe_event_pool);
5890 }
5891 return 0;
5892
5893out_pool_create_fail:
5894 lpfc_sli4_cq_event_pool_destroy(phba);
5895 return -ENOMEM;
5896}
5897
5898/**
5899 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5900 * @phba: pointer to lpfc hba data structure.
5901 *
5902 * This routine is invoked to free the pool of completion queue events at
5903 * driver unload time. Note that, it is the responsibility of the driver
5904 * cleanup routine to free all the outstanding completion-queue events
5905 * allocated from this pool back into the pool before invoking this routine
5906 * to destroy the pool.
5907 **/
5908static void
5909lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5910{
5911 struct lpfc_cq_event *cq_event, *next_cq_event;
5912
5913 list_for_each_entry_safe(cq_event, next_cq_event,
5914 &phba->sli4_hba.sp_cqe_event_pool, list) {
5915 list_del(&cq_event->list);
5916 kfree(cq_event);
5917 }
5918}
5919
5920/**
5921 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5922 * @phba: pointer to lpfc hba data structure.
5923 *
5924 * This routine is the lock free version of the API invoked to allocate a
5925 * completion-queue event from the free pool.
5926 *
5927 * Return: Pointer to the newly allocated completion-queue event if successful
5928 * NULL otherwise.
5929 **/
5930struct lpfc_cq_event *
5931__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5932{
5933 struct lpfc_cq_event *cq_event = NULL;
5934
5935 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5936 struct lpfc_cq_event, list);
5937 return cq_event;
5938}
5939
5940/**
5941 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5942 * @phba: pointer to lpfc hba data structure.
5943 *
5944 * This routine is the lock version of the API invoked to allocate a
5945 * completion-queue event from the free pool.
5946 *
5947 * Return: Pointer to the newly allocated completion-queue event if successful
5948 * NULL otherwise.
5949 **/
5950struct lpfc_cq_event *
5951lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5952{
5953 struct lpfc_cq_event *cq_event;
5954 unsigned long iflags;
5955
5956 spin_lock_irqsave(&phba->hbalock, iflags);
5957 cq_event = __lpfc_sli4_cq_event_alloc(phba);
5958 spin_unlock_irqrestore(&phba->hbalock, iflags);
5959 return cq_event;
5960}
5961
5962/**
5963 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5964 * @phba: pointer to lpfc hba data structure.
5965 * @cq_event: pointer to the completion queue event to be freed.
5966 *
5967 * This routine is the lock free version of the API invoked to release a
5968 * completion-queue event back into the free pool.
5969 **/
5970void
5971__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5972 struct lpfc_cq_event *cq_event)
5973{
5974 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
5975}
5976
5977/**
5978 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5979 * @phba: pointer to lpfc hba data structure.
5980 * @cq_event: pointer to the completion queue event to be freed.
5981 *
5982 * This routine is the lock version of the API invoked to release a
5983 * completion-queue event back into the free pool.
5984 **/
5985void
5986lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5987 struct lpfc_cq_event *cq_event)
5988{
5989 unsigned long iflags;
5990 spin_lock_irqsave(&phba->hbalock, iflags);
5991 __lpfc_sli4_cq_event_release(phba, cq_event);
5992 spin_unlock_irqrestore(&phba->hbalock, iflags);
5993}
5994
5995/**
5996 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
5997 * @phba: pointer to lpfc hba data structure.
5998 *
5999 * This routine is to free all the pending completion-queue events to the
6000 * back into the free pool for device reset.
6001 **/
6002static void
6003lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6004{
6005 LIST_HEAD(cqelist);
6006 struct lpfc_cq_event *cqe;
6007 unsigned long iflags;
6008
6009 /* Retrieve all the pending WCQEs from pending WCQE lists */
6010 spin_lock_irqsave(&phba->hbalock, iflags);
6011 /* Pending FCP XRI abort events */
6012 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6013 &cqelist);
6014 /* Pending ELS XRI abort events */
6015 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6016 &cqelist);
6017 /* Pending asynnc events */
6018 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6019 &cqelist);
6020 spin_unlock_irqrestore(&phba->hbalock, iflags);
6021
6022 while (!list_empty(&cqelist)) {
6023 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6024 lpfc_sli4_cq_event_release(phba, cqe);
6025 }
6026}
6027
6028/**
6029 * lpfc_pci_function_reset - Reset pci function.
6030 * @phba: pointer to lpfc hba data structure.
6031 *
6032 * This routine is invoked to request a PCI function reset. It will destroys
6033 * all resources assigned to the PCI function which originates this request.
6034 *
6035 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006036 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006037 * ENOMEM - No availble memory
6038 * EIO - The mailbox failed to complete successfully.
6039 **/
6040int
6041lpfc_pci_function_reset(struct lpfc_hba *phba)
6042{
6043 LPFC_MBOXQ_t *mboxq;
6044 uint32_t rc = 0;
6045 uint32_t shdr_status, shdr_add_status;
6046 union lpfc_sli4_cfg_shdr *shdr;
6047
6048 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6049 if (!mboxq) {
6050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6051 "0494 Unable to allocate memory for issuing "
6052 "SLI_FUNCTION_RESET mailbox command\n");
6053 return -ENOMEM;
6054 }
6055
6056 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6057 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6058 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6059 LPFC_SLI4_MBX_EMBED);
6060 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6061 shdr = (union lpfc_sli4_cfg_shdr *)
6062 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6063 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6064 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6065 if (rc != MBX_TIMEOUT)
6066 mempool_free(mboxq, phba->mbox_mem_pool);
6067 if (shdr_status || shdr_add_status || rc) {
6068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6069 "0495 SLI_FUNCTION_RESET mailbox failed with "
6070 "status x%x add_status x%x, mbx status x%x\n",
6071 shdr_status, shdr_add_status, rc);
6072 rc = -ENXIO;
6073 }
6074 return rc;
6075}
6076
6077/**
6078 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6079 * @phba: pointer to lpfc hba data structure.
6080 * @cnt: number of nop mailbox commands to send.
6081 *
6082 * This routine is invoked to send a number @cnt of NOP mailbox command and
6083 * wait for each command to complete.
6084 *
6085 * Return: the number of NOP mailbox command completed.
6086 **/
6087static int
6088lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6089{
6090 LPFC_MBOXQ_t *mboxq;
6091 int length, cmdsent;
6092 uint32_t mbox_tmo;
6093 uint32_t rc = 0;
6094 uint32_t shdr_status, shdr_add_status;
6095 union lpfc_sli4_cfg_shdr *shdr;
6096
6097 if (cnt == 0) {
6098 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6099 "2518 Requested to send 0 NOP mailbox cmd\n");
6100 return cnt;
6101 }
6102
6103 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6104 if (!mboxq) {
6105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6106 "2519 Unable to allocate memory for issuing "
6107 "NOP mailbox command\n");
6108 return 0;
6109 }
6110
6111 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6112 length = (sizeof(struct lpfc_mbx_nop) -
6113 sizeof(struct lpfc_sli4_cfg_mhdr));
6114 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6115 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6116
6117 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6118 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6119 if (!phba->sli4_hba.intr_enable)
6120 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6121 else
6122 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6123 if (rc == MBX_TIMEOUT)
6124 break;
6125 /* Check return status */
6126 shdr = (union lpfc_sli4_cfg_shdr *)
6127 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6128 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6129 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6130 &shdr->response);
6131 if (shdr_status || shdr_add_status || rc) {
6132 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6133 "2520 NOP mailbox command failed "
6134 "status x%x add_status x%x mbx "
6135 "status x%x\n", shdr_status,
6136 shdr_add_status, rc);
6137 break;
6138 }
6139 }
6140
6141 if (rc != MBX_TIMEOUT)
6142 mempool_free(mboxq, phba->mbox_mem_pool);
6143
6144 return cmdsent;
6145}
6146
6147/**
6148 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6149 * @phba: pointer to lpfc hba data structure.
6150 * @fcfi: fcf index.
6151 *
6152 * This routine is invoked to unregister a FCFI from device.
6153 **/
6154void
6155lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6156{
6157 LPFC_MBOXQ_t *mbox;
6158 uint32_t mbox_tmo;
6159 int rc;
6160 unsigned long flags;
6161
6162 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6163
6164 if (!mbox)
6165 return;
6166
6167 lpfc_unreg_fcfi(mbox, fcfi);
6168
6169 if (!phba->sli4_hba.intr_enable)
6170 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6171 else {
6172 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6173 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6174 }
6175 if (rc != MBX_TIMEOUT)
6176 mempool_free(mbox, phba->mbox_mem_pool);
6177 if (rc != MBX_SUCCESS)
6178 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6179 "2517 Unregister FCFI command failed "
6180 "status %d, mbxStatus x%x\n", rc,
6181 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6182 else {
6183 spin_lock_irqsave(&phba->hbalock, flags);
6184 /* Mark the FCFI is no longer registered */
6185 phba->fcf.fcf_flag &=
James Smartecfd03c2010-02-12 14:41:27 -05006186 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
James Smartda0436e2009-05-22 14:51:39 -04006187 spin_unlock_irqrestore(&phba->hbalock, flags);
6188 }
6189}
6190
6191/**
6192 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6193 * @phba: pointer to lpfc hba data structure.
6194 *
6195 * This routine is invoked to set up the PCI device memory space for device
6196 * with SLI-4 interface spec.
6197 *
6198 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006199 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006200 * other values - error
6201 **/
6202static int
6203lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6204{
6205 struct pci_dev *pdev;
6206 unsigned long bar0map_len, bar1map_len, bar2map_len;
6207 int error = -ENODEV;
6208
6209 /* Obtain PCI device reference */
6210 if (!phba->pcidev)
6211 return error;
6212 else
6213 pdev = phba->pcidev;
6214
6215 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006216 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6217 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6218 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6219 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006220 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006221 }
6222 }
James Smartda0436e2009-05-22 14:51:39 -04006223
6224 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6225 * number of bytes required by each mapping. They are actually
James Smart1dfb5a42010-02-12 14:40:50 -05006226 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006227 */
James Smart1dfb5a42010-02-12 14:40:50 -05006228 if (pci_resource_start(pdev, 0)) {
6229 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6230 bar0map_len = pci_resource_len(pdev, 0);
6231 } else {
6232 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6233 bar0map_len = pci_resource_len(pdev, 1);
6234 }
6235 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6236 bar1map_len = pci_resource_len(pdev, 2);
James Smartda0436e2009-05-22 14:51:39 -04006237
James Smart1dfb5a42010-02-12 14:40:50 -05006238 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6239 bar2map_len = pci_resource_len(pdev, 4);
James Smartda0436e2009-05-22 14:51:39 -04006240
6241 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6242 phba->sli4_hba.conf_regs_memmap_p =
6243 ioremap(phba->pci_bar0_map, bar0map_len);
6244 if (!phba->sli4_hba.conf_regs_memmap_p) {
6245 dev_printk(KERN_ERR, &pdev->dev,
6246 "ioremap failed for SLI4 PCI config registers.\n");
6247 goto out;
6248 }
6249
6250 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6251 phba->sli4_hba.ctrl_regs_memmap_p =
6252 ioremap(phba->pci_bar1_map, bar1map_len);
6253 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6254 dev_printk(KERN_ERR, &pdev->dev,
6255 "ioremap failed for SLI4 HBA control registers.\n");
6256 goto out_iounmap_conf;
6257 }
6258
6259 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6260 phba->sli4_hba.drbl_regs_memmap_p =
6261 ioremap(phba->pci_bar2_map, bar2map_len);
6262 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6263 dev_printk(KERN_ERR, &pdev->dev,
6264 "ioremap failed for SLI4 HBA doorbell registers.\n");
6265 goto out_iounmap_ctrl;
6266 }
6267
6268 /* Set up BAR0 PCI config space register memory map */
6269 lpfc_sli4_bar0_register_memmap(phba);
6270
6271 /* Set up BAR1 register memory map */
6272 lpfc_sli4_bar1_register_memmap(phba);
6273
6274 /* Set up BAR2 register memory map */
6275 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6276 if (error)
6277 goto out_iounmap_all;
6278
6279 return 0;
6280
6281out_iounmap_all:
6282 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6283out_iounmap_ctrl:
6284 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6285out_iounmap_conf:
6286 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6287out:
6288 return error;
6289}
6290
6291/**
6292 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6293 * @phba: pointer to lpfc hba data structure.
6294 *
6295 * This routine is invoked to unset the PCI device memory space for device
6296 * with SLI-4 interface spec.
6297 **/
6298static void
6299lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6300{
6301 struct pci_dev *pdev;
6302
6303 /* Obtain PCI device reference */
6304 if (!phba->pcidev)
6305 return;
6306 else
6307 pdev = phba->pcidev;
6308
6309 /* Free coherent DMA memory allocated */
6310
6311 /* Unmap I/O memory space */
6312 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6313 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6314 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6315
6316 return;
6317}
6318
6319/**
James Smart3772a992009-05-22 14:50:54 -04006320 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6321 * @phba: pointer to lpfc hba data structure.
6322 *
6323 * This routine is invoked to enable the MSI-X interrupt vectors to device
6324 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6325 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6326 * invoked, enables either all or nothing, depending on the current
6327 * availability of PCI vector resources. The device driver is responsible
6328 * for calling the individual request_irq() to register each MSI-X vector
6329 * with a interrupt handler, which is done in this function. Note that
6330 * later when device is unloading, the driver should always call free_irq()
6331 * on all MSI-X vectors it has done request_irq() on before calling
6332 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6333 * will be left with MSI-X enabled and leaks its vectors.
6334 *
6335 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006336 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006337 * other values - error
6338 **/
6339static int
6340lpfc_sli_enable_msix(struct lpfc_hba *phba)
6341{
6342 int rc, i;
6343 LPFC_MBOXQ_t *pmb;
6344
6345 /* Set up MSI-X multi-message vectors */
6346 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6347 phba->msix_entries[i].entry = i;
6348
6349 /* Configure MSI-X capability structure */
6350 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6351 ARRAY_SIZE(phba->msix_entries));
6352 if (rc) {
6353 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6354 "0420 PCI enable MSI-X failed (%d)\n", rc);
6355 goto msi_fail_out;
6356 }
6357 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6358 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6359 "0477 MSI-X entry[%d]: vector=x%x "
6360 "message=%d\n", i,
6361 phba->msix_entries[i].vector,
6362 phba->msix_entries[i].entry);
6363 /*
6364 * Assign MSI-X vectors to interrupt handlers
6365 */
6366
6367 /* vector-0 is associated to slow-path handler */
6368 rc = request_irq(phba->msix_entries[0].vector,
6369 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6370 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6371 if (rc) {
6372 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6373 "0421 MSI-X slow-path request_irq failed "
6374 "(%d)\n", rc);
6375 goto msi_fail_out;
6376 }
6377
6378 /* vector-1 is associated to fast-path handler */
6379 rc = request_irq(phba->msix_entries[1].vector,
6380 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6381 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6382
6383 if (rc) {
6384 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6385 "0429 MSI-X fast-path request_irq failed "
6386 "(%d)\n", rc);
6387 goto irq_fail_out;
6388 }
6389
6390 /*
6391 * Configure HBA MSI-X attention conditions to messages
6392 */
6393 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6394
6395 if (!pmb) {
6396 rc = -ENOMEM;
6397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6398 "0474 Unable to allocate memory for issuing "
6399 "MBOX_CONFIG_MSI command\n");
6400 goto mem_fail_out;
6401 }
6402 rc = lpfc_config_msi(phba, pmb);
6403 if (rc)
6404 goto mbx_fail_out;
6405 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6406 if (rc != MBX_SUCCESS) {
6407 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6408 "0351 Config MSI mailbox command failed, "
6409 "mbxCmd x%x, mbxStatus x%x\n",
6410 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6411 goto mbx_fail_out;
6412 }
6413
6414 /* Free memory allocated for mailbox command */
6415 mempool_free(pmb, phba->mbox_mem_pool);
6416 return rc;
6417
6418mbx_fail_out:
6419 /* Free memory allocated for mailbox command */
6420 mempool_free(pmb, phba->mbox_mem_pool);
6421
6422mem_fail_out:
6423 /* free the irq already requested */
6424 free_irq(phba->msix_entries[1].vector, phba);
6425
6426irq_fail_out:
6427 /* free the irq already requested */
6428 free_irq(phba->msix_entries[0].vector, phba);
6429
6430msi_fail_out:
6431 /* Unconfigure MSI-X capability structure */
6432 pci_disable_msix(phba->pcidev);
6433 return rc;
6434}
6435
6436/**
6437 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6438 * @phba: pointer to lpfc hba data structure.
6439 *
6440 * This routine is invoked to release the MSI-X vectors and then disable the
6441 * MSI-X interrupt mode to device with SLI-3 interface spec.
6442 **/
6443static void
6444lpfc_sli_disable_msix(struct lpfc_hba *phba)
6445{
6446 int i;
6447
6448 /* Free up MSI-X multi-message vectors */
6449 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6450 free_irq(phba->msix_entries[i].vector, phba);
6451 /* Disable MSI-X */
6452 pci_disable_msix(phba->pcidev);
6453
6454 return;
6455}
6456
6457/**
6458 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6459 * @phba: pointer to lpfc hba data structure.
6460 *
6461 * This routine is invoked to enable the MSI interrupt mode to device with
6462 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6463 * enable the MSI vector. The device driver is responsible for calling the
6464 * request_irq() to register MSI vector with a interrupt the handler, which
6465 * is done in this function.
6466 *
6467 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006468 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006469 * other values - error
6470 */
6471static int
6472lpfc_sli_enable_msi(struct lpfc_hba *phba)
6473{
6474 int rc;
6475
6476 rc = pci_enable_msi(phba->pcidev);
6477 if (!rc)
6478 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6479 "0462 PCI enable MSI mode success.\n");
6480 else {
6481 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6482 "0471 PCI enable MSI mode failed (%d)\n", rc);
6483 return rc;
6484 }
6485
6486 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6487 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6488 if (rc) {
6489 pci_disable_msi(phba->pcidev);
6490 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6491 "0478 MSI request_irq failed (%d)\n", rc);
6492 }
6493 return rc;
6494}
6495
6496/**
6497 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6498 * @phba: pointer to lpfc hba data structure.
6499 *
6500 * This routine is invoked to disable the MSI interrupt mode to device with
6501 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6502 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6503 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6504 * its vector.
6505 */
6506static void
6507lpfc_sli_disable_msi(struct lpfc_hba *phba)
6508{
6509 free_irq(phba->pcidev->irq, phba);
6510 pci_disable_msi(phba->pcidev);
6511 return;
6512}
6513
6514/**
6515 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6516 * @phba: pointer to lpfc hba data structure.
6517 *
6518 * This routine is invoked to enable device interrupt and associate driver's
6519 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6520 * spec. Depends on the interrupt mode configured to the driver, the driver
6521 * will try to fallback from the configured interrupt mode to an interrupt
6522 * mode which is supported by the platform, kernel, and device in the order
6523 * of:
6524 * MSI-X -> MSI -> IRQ.
6525 *
6526 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006527 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006528 * other values - error
6529 **/
6530static uint32_t
6531lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6532{
6533 uint32_t intr_mode = LPFC_INTR_ERROR;
6534 int retval;
6535
6536 if (cfg_mode == 2) {
6537 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6538 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6539 if (!retval) {
6540 /* Now, try to enable MSI-X interrupt mode */
6541 retval = lpfc_sli_enable_msix(phba);
6542 if (!retval) {
6543 /* Indicate initialization to MSI-X mode */
6544 phba->intr_type = MSIX;
6545 intr_mode = 2;
6546 }
6547 }
6548 }
6549
6550 /* Fallback to MSI if MSI-X initialization failed */
6551 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6552 retval = lpfc_sli_enable_msi(phba);
6553 if (!retval) {
6554 /* Indicate initialization to MSI mode */
6555 phba->intr_type = MSI;
6556 intr_mode = 1;
6557 }
6558 }
6559
6560 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6561 if (phba->intr_type == NONE) {
6562 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6563 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6564 if (!retval) {
6565 /* Indicate initialization to INTx mode */
6566 phba->intr_type = INTx;
6567 intr_mode = 0;
6568 }
6569 }
6570 return intr_mode;
6571}
6572
6573/**
6574 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6575 * @phba: pointer to lpfc hba data structure.
6576 *
6577 * This routine is invoked to disable device interrupt and disassociate the
6578 * driver's interrupt handler(s) from interrupt vector(s) to device with
6579 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6580 * release the interrupt vector(s) for the message signaled interrupt.
6581 **/
6582static void
6583lpfc_sli_disable_intr(struct lpfc_hba *phba)
6584{
6585 /* Disable the currently initialized interrupt mode */
6586 if (phba->intr_type == MSIX)
6587 lpfc_sli_disable_msix(phba);
6588 else if (phba->intr_type == MSI)
6589 lpfc_sli_disable_msi(phba);
6590 else if (phba->intr_type == INTx)
6591 free_irq(phba->pcidev->irq, phba);
6592
6593 /* Reset interrupt management states */
6594 phba->intr_type = NONE;
6595 phba->sli.slistat.sli_intr = 0;
6596
6597 return;
6598}
6599
6600/**
James Smartda0436e2009-05-22 14:51:39 -04006601 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6602 * @phba: pointer to lpfc hba data structure.
6603 *
6604 * This routine is invoked to enable the MSI-X interrupt vectors to device
6605 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6606 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6607 * enables either all or nothing, depending on the current availability of
6608 * PCI vector resources. The device driver is responsible for calling the
6609 * individual request_irq() to register each MSI-X vector with a interrupt
6610 * handler, which is done in this function. Note that later when device is
6611 * unloading, the driver should always call free_irq() on all MSI-X vectors
6612 * it has done request_irq() on before calling pci_disable_msix(). Failure
6613 * to do so results in a BUG_ON() and a device will be left with MSI-X
6614 * enabled and leaks its vectors.
6615 *
6616 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006617 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006618 * other values - error
6619 **/
6620static int
6621lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6622{
6623 int rc, index;
6624
6625 /* Set up MSI-X multi-message vectors */
6626 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6627 phba->sli4_hba.msix_entries[index].entry = index;
6628
6629 /* Configure MSI-X capability structure */
6630 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6631 phba->sli4_hba.cfg_eqn);
6632 if (rc) {
6633 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6634 "0484 PCI enable MSI-X failed (%d)\n", rc);
6635 goto msi_fail_out;
6636 }
6637 /* Log MSI-X vector assignment */
6638 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6639 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6640 "0489 MSI-X entry[%d]: vector=x%x "
6641 "message=%d\n", index,
6642 phba->sli4_hba.msix_entries[index].vector,
6643 phba->sli4_hba.msix_entries[index].entry);
6644 /*
6645 * Assign MSI-X vectors to interrupt handlers
6646 */
6647
6648 /* The first vector must associated to slow-path handler for MQ */
6649 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6650 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6651 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6652 if (rc) {
6653 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6654 "0485 MSI-X slow-path request_irq failed "
6655 "(%d)\n", rc);
6656 goto msi_fail_out;
6657 }
6658
6659 /* The rest of the vector(s) are associated to fast-path handler(s) */
6660 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6661 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6662 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6663 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6664 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6665 LPFC_FP_DRIVER_HANDLER_NAME,
6666 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6667 if (rc) {
6668 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6669 "0486 MSI-X fast-path (%d) "
6670 "request_irq failed (%d)\n", index, rc);
6671 goto cfg_fail_out;
6672 }
6673 }
6674
6675 return rc;
6676
6677cfg_fail_out:
6678 /* free the irq already requested */
6679 for (--index; index >= 1; index--)
6680 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6681 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6682
6683 /* free the irq already requested */
6684 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6685
6686msi_fail_out:
6687 /* Unconfigure MSI-X capability structure */
6688 pci_disable_msix(phba->pcidev);
6689 return rc;
6690}
6691
6692/**
6693 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6694 * @phba: pointer to lpfc hba data structure.
6695 *
6696 * This routine is invoked to release the MSI-X vectors and then disable the
6697 * MSI-X interrupt mode to device with SLI-4 interface spec.
6698 **/
6699static void
6700lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6701{
6702 int index;
6703
6704 /* Free up MSI-X multi-message vectors */
6705 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6706
6707 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6708 free_irq(phba->sli4_hba.msix_entries[index].vector,
6709 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6710 /* Disable MSI-X */
6711 pci_disable_msix(phba->pcidev);
6712
6713 return;
6714}
6715
6716/**
6717 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6718 * @phba: pointer to lpfc hba data structure.
6719 *
6720 * This routine is invoked to enable the MSI interrupt mode to device with
6721 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6722 * to enable the MSI vector. The device driver is responsible for calling
6723 * the request_irq() to register MSI vector with a interrupt the handler,
6724 * which is done in this function.
6725 *
6726 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006727 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006728 * other values - error
6729 **/
6730static int
6731lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6732{
6733 int rc, index;
6734
6735 rc = pci_enable_msi(phba->pcidev);
6736 if (!rc)
6737 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6738 "0487 PCI enable MSI mode success.\n");
6739 else {
6740 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6741 "0488 PCI enable MSI mode failed (%d)\n", rc);
6742 return rc;
6743 }
6744
6745 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6746 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6747 if (rc) {
6748 pci_disable_msi(phba->pcidev);
6749 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6750 "0490 MSI request_irq failed (%d)\n", rc);
6751 }
6752
6753 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6754 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6755 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6756 }
6757
6758 return rc;
6759}
6760
6761/**
6762 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6763 * @phba: pointer to lpfc hba data structure.
6764 *
6765 * This routine is invoked to disable the MSI interrupt mode to device with
6766 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6767 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6768 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6769 * its vector.
6770 **/
6771static void
6772lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6773{
6774 free_irq(phba->pcidev->irq, phba);
6775 pci_disable_msi(phba->pcidev);
6776 return;
6777}
6778
6779/**
6780 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6781 * @phba: pointer to lpfc hba data structure.
6782 *
6783 * This routine is invoked to enable device interrupt and associate driver's
6784 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6785 * interface spec. Depends on the interrupt mode configured to the driver,
6786 * the driver will try to fallback from the configured interrupt mode to an
6787 * interrupt mode which is supported by the platform, kernel, and device in
6788 * the order of:
6789 * MSI-X -> MSI -> IRQ.
6790 *
6791 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006792 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006793 * other values - error
6794 **/
6795static uint32_t
6796lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6797{
6798 uint32_t intr_mode = LPFC_INTR_ERROR;
6799 int retval, index;
6800
6801 if (cfg_mode == 2) {
6802 /* Preparation before conf_msi mbox cmd */
6803 retval = 0;
6804 if (!retval) {
6805 /* Now, try to enable MSI-X interrupt mode */
6806 retval = lpfc_sli4_enable_msix(phba);
6807 if (!retval) {
6808 /* Indicate initialization to MSI-X mode */
6809 phba->intr_type = MSIX;
6810 intr_mode = 2;
6811 }
6812 }
6813 }
6814
6815 /* Fallback to MSI if MSI-X initialization failed */
6816 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6817 retval = lpfc_sli4_enable_msi(phba);
6818 if (!retval) {
6819 /* Indicate initialization to MSI mode */
6820 phba->intr_type = MSI;
6821 intr_mode = 1;
6822 }
6823 }
6824
6825 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6826 if (phba->intr_type == NONE) {
6827 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6828 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6829 if (!retval) {
6830 /* Indicate initialization to INTx mode */
6831 phba->intr_type = INTx;
6832 intr_mode = 0;
6833 for (index = 0; index < phba->cfg_fcp_eq_count;
6834 index++) {
6835 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6836 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6837 }
6838 }
6839 }
6840 return intr_mode;
6841}
6842
6843/**
6844 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6845 * @phba: pointer to lpfc hba data structure.
6846 *
6847 * This routine is invoked to disable device interrupt and disassociate
6848 * the driver's interrupt handler(s) from interrupt vector(s) to device
6849 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6850 * will release the interrupt vector(s) for the message signaled interrupt.
6851 **/
6852static void
6853lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6854{
6855 /* Disable the currently initialized interrupt mode */
6856 if (phba->intr_type == MSIX)
6857 lpfc_sli4_disable_msix(phba);
6858 else if (phba->intr_type == MSI)
6859 lpfc_sli4_disable_msi(phba);
6860 else if (phba->intr_type == INTx)
6861 free_irq(phba->pcidev->irq, phba);
6862
6863 /* Reset interrupt management states */
6864 phba->intr_type = NONE;
6865 phba->sli.slistat.sli_intr = 0;
6866
6867 return;
6868}
6869
6870/**
James Smart3772a992009-05-22 14:50:54 -04006871 * lpfc_unset_hba - Unset SLI3 hba device initialization
6872 * @phba: pointer to lpfc hba data structure.
6873 *
6874 * This routine is invoked to unset the HBA device initialization steps to
6875 * a device with SLI-3 interface spec.
6876 **/
6877static void
6878lpfc_unset_hba(struct lpfc_hba *phba)
6879{
6880 struct lpfc_vport *vport = phba->pport;
6881 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6882
6883 spin_lock_irq(shost->host_lock);
6884 vport->load_flag |= FC_UNLOADING;
6885 spin_unlock_irq(shost->host_lock);
6886
6887 lpfc_stop_hba_timers(phba);
6888
6889 phba->pport->work_port_events = 0;
6890
6891 lpfc_sli_hba_down(phba);
6892
6893 lpfc_sli_brdrestart(phba);
6894
6895 lpfc_sli_disable_intr(phba);
6896
6897 return;
6898}
6899
6900/**
James Smartda0436e2009-05-22 14:51:39 -04006901 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6902 * @phba: pointer to lpfc hba data structure.
6903 *
6904 * This routine is invoked to unset the HBA device initialization steps to
6905 * a device with SLI-4 interface spec.
6906 **/
6907static void
6908lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6909{
6910 struct lpfc_vport *vport = phba->pport;
6911 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6912
6913 spin_lock_irq(shost->host_lock);
6914 vport->load_flag |= FC_UNLOADING;
6915 spin_unlock_irq(shost->host_lock);
6916
6917 phba->pport->work_port_events = 0;
6918
6919 lpfc_sli4_hba_down(phba);
6920
6921 lpfc_sli4_disable_intr(phba);
6922
6923 return;
6924}
6925
6926/**
6927 * lpfc_sli4_hba_unset - Unset the fcoe hba
6928 * @phba: Pointer to HBA context object.
6929 *
6930 * This function is called in the SLI4 code path to reset the HBA's FCoE
6931 * function. The caller is not required to hold any lock. This routine
6932 * issues PCI function reset mailbox command to reset the FCoE function.
6933 * At the end of the function, it calls lpfc_hba_down_post function to
6934 * free any pending commands.
6935 **/
6936static void
6937lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6938{
6939 int wait_cnt = 0;
6940 LPFC_MBOXQ_t *mboxq;
6941
6942 lpfc_stop_hba_timers(phba);
6943 phba->sli4_hba.intr_enable = 0;
6944
6945 /*
6946 * Gracefully wait out the potential current outstanding asynchronous
6947 * mailbox command.
6948 */
6949
6950 /* First, block any pending async mailbox command from posted */
6951 spin_lock_irq(&phba->hbalock);
6952 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6953 spin_unlock_irq(&phba->hbalock);
6954 /* Now, trying to wait it out if we can */
6955 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6956 msleep(10);
6957 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
6958 break;
6959 }
6960 /* Forcefully release the outstanding mailbox command if timed out */
6961 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6962 spin_lock_irq(&phba->hbalock);
6963 mboxq = phba->sli.mbox_active;
6964 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6965 __lpfc_mbox_cmpl_put(phba, mboxq);
6966 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6967 phba->sli.mbox_active = NULL;
6968 spin_unlock_irq(&phba->hbalock);
6969 }
6970
6971 /* Tear down the queues in the HBA */
6972 lpfc_sli4_queue_unset(phba);
6973
6974 /* Disable PCI subsystem interrupt */
6975 lpfc_sli4_disable_intr(phba);
6976
6977 /* Stop kthread signal shall trigger work_done one more time */
6978 kthread_stop(phba->worker_thread);
6979
6980 /* Stop the SLI4 device port */
6981 phba->pport->work_port_events = 0;
6982}
6983
6984/**
James Smart3772a992009-05-22 14:50:54 -04006985 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
6986 * @pdev: pointer to PCI device
6987 * @pid: pointer to PCI device identifier
6988 *
6989 * This routine is to be called to attach a device with SLI-3 interface spec
6990 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6991 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
6992 * information of the device and driver to see if the driver state that it can
6993 * support this kind of device. If the match is successful, the driver core
6994 * invokes this routine. If this routine determines it can claim the HBA, it
6995 * does all the initialization that it needs to do to handle the HBA properly.
6996 *
6997 * Return code
6998 * 0 - driver can claim the device
6999 * negative value - driver can not claim the device
7000 **/
7001static int __devinit
7002lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7003{
7004 struct lpfc_hba *phba;
7005 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007006 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007007 int error;
7008 uint32_t cfg_mode, intr_mode;
7009
7010 /* Allocate memory for HBA structure */
7011 phba = lpfc_hba_alloc(pdev);
7012 if (!phba)
7013 return -ENOMEM;
7014
7015 /* Perform generic PCI device enabling operation */
7016 error = lpfc_enable_pci_dev(phba);
7017 if (error) {
7018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7019 "1401 Failed to enable pci device.\n");
7020 goto out_free_phba;
7021 }
7022
7023 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7024 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7025 if (error)
7026 goto out_disable_pci_dev;
7027
7028 /* Set up SLI-3 specific device PCI memory space */
7029 error = lpfc_sli_pci_mem_setup(phba);
7030 if (error) {
7031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7032 "1402 Failed to set up pci memory space.\n");
7033 goto out_disable_pci_dev;
7034 }
7035
7036 /* Set up phase-1 common device driver resources */
7037 error = lpfc_setup_driver_resource_phase1(phba);
7038 if (error) {
7039 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7040 "1403 Failed to set up driver resource.\n");
7041 goto out_unset_pci_mem_s3;
7042 }
7043
7044 /* Set up SLI-3 specific device driver resources */
7045 error = lpfc_sli_driver_resource_setup(phba);
7046 if (error) {
7047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7048 "1404 Failed to set up driver resource.\n");
7049 goto out_unset_pci_mem_s3;
7050 }
7051
7052 /* Initialize and populate the iocb list per host */
7053 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7054 if (error) {
7055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7056 "1405 Failed to initialize iocb list.\n");
7057 goto out_unset_driver_resource_s3;
7058 }
7059
7060 /* Set up common device driver resources */
7061 error = lpfc_setup_driver_resource_phase2(phba);
7062 if (error) {
7063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7064 "1406 Failed to set up driver resource.\n");
7065 goto out_free_iocb_list;
7066 }
7067
7068 /* Create SCSI host to the physical port */
7069 error = lpfc_create_shost(phba);
7070 if (error) {
7071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7072 "1407 Failed to create scsi host.\n");
7073 goto out_unset_driver_resource;
7074 }
7075
7076 /* Configure sysfs attributes */
7077 vport = phba->pport;
7078 error = lpfc_alloc_sysfs_attr(vport);
7079 if (error) {
7080 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7081 "1476 Failed to allocate sysfs attr\n");
7082 goto out_destroy_shost;
7083 }
7084
James Smart6669f9b2009-10-02 15:16:45 -04007085 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04007086 /* Now, trying to enable interrupt and bring up the device */
7087 cfg_mode = phba->cfg_use_msi;
7088 while (true) {
7089 /* Put device to a known state before enabling interrupt */
7090 lpfc_stop_port(phba);
7091 /* Configure and enable interrupt */
7092 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7093 if (intr_mode == LPFC_INTR_ERROR) {
7094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7095 "0431 Failed to enable interrupt.\n");
7096 error = -ENODEV;
7097 goto out_free_sysfs_attr;
7098 }
7099 /* SLI-3 HBA setup */
7100 if (lpfc_sli_hba_setup(phba)) {
7101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7102 "1477 Failed to set up hba\n");
7103 error = -ENODEV;
7104 goto out_remove_device;
7105 }
7106
7107 /* Wait 50ms for the interrupts of previous mailbox commands */
7108 msleep(50);
7109 /* Check active interrupts on message signaled interrupts */
7110 if (intr_mode == 0 ||
7111 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7112 /* Log the current active interrupt mode */
7113 phba->intr_mode = intr_mode;
7114 lpfc_log_intr_mode(phba, intr_mode);
7115 break;
7116 } else {
7117 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7118 "0447 Configure interrupt mode (%d) "
7119 "failed active interrupt test.\n",
7120 intr_mode);
7121 /* Disable the current interrupt mode */
7122 lpfc_sli_disable_intr(phba);
7123 /* Try next level of interrupt mode */
7124 cfg_mode = --intr_mode;
7125 }
7126 }
7127
7128 /* Perform post initialization setup */
7129 lpfc_post_init_setup(phba);
7130
7131 /* Check if there are static vports to be created. */
7132 lpfc_create_static_vport(phba);
7133
7134 return 0;
7135
7136out_remove_device:
7137 lpfc_unset_hba(phba);
7138out_free_sysfs_attr:
7139 lpfc_free_sysfs_attr(vport);
7140out_destroy_shost:
7141 lpfc_destroy_shost(phba);
7142out_unset_driver_resource:
7143 lpfc_unset_driver_resource_phase2(phba);
7144out_free_iocb_list:
7145 lpfc_free_iocb_list(phba);
7146out_unset_driver_resource_s3:
7147 lpfc_sli_driver_resource_unset(phba);
7148out_unset_pci_mem_s3:
7149 lpfc_sli_pci_mem_unset(phba);
7150out_disable_pci_dev:
7151 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007152 if (shost)
7153 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007154out_free_phba:
7155 lpfc_hba_free(phba);
7156 return error;
7157}
7158
7159/**
7160 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007161 * @pdev: pointer to PCI device
7162 *
James Smart3772a992009-05-22 14:50:54 -04007163 * This routine is to be called to disattach a device with SLI-3 interface
7164 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7165 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7166 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007167 **/
dea31012005-04-17 16:05:31 -05007168static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007169lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007170{
James Smart2e0fef82007-06-17 19:56:36 -05007171 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7172 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007173 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007174 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007175 int i;
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05007176 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7177
James Smart549e55c2007-08-02 11:09:51 -04007178 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007179 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007180 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007181
James Smart858c9f62007-06-17 19:56:39 -05007182 lpfc_free_sysfs_attr(vport);
7183
James Smarteada2722008-12-04 22:39:13 -05007184 /* Release all the vports against this physical port */
7185 vports = lpfc_create_vport_work_array(phba);
7186 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007187 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007188 fc_vport_terminate(vports[i]->fc_vport);
7189 lpfc_destroy_vport_work_array(phba, vports);
7190
7191 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007192 fc_remove_host(shost);
7193 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007194 lpfc_cleanup(vport);
7195
James Smart2e0fef82007-06-17 19:56:36 -05007196 /*
7197 * Bring down the SLI Layer. This step disable all interrupts,
7198 * clears the rings, discards all mailbox commands, and resets
7199 * the HBA.
7200 */
James Smarta257bf92009-04-06 18:48:10 -04007201
7202 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007203 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007204 /* Stop kthread signal shall trigger work_done one more time */
7205 kthread_stop(phba->worker_thread);
7206 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007207 lpfc_sli_brdrestart(phba);
7208
James Smart3772a992009-05-22 14:50:54 -04007209 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007210 spin_lock_irq(&phba->hbalock);
7211 list_del_init(&vport->listentry);
7212 spin_unlock_irq(&phba->hbalock);
7213
James Smart858c9f62007-06-17 19:56:39 -05007214 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007215
James Smart5b75da22008-12-04 22:39:35 -05007216 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007217 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007218
7219 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007220 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007221
7222 /*
7223 * Call scsi_free before mem_free since scsi bufs are released to their
7224 * corresponding pools here.
7225 */
7226 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007227 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007228
James Smart34b02dc2008-08-24 21:49:55 -04007229 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7230 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007231
James Smart2e0fef82007-06-17 19:56:36 -05007232 /* Free resources associated with SLI2 interface */
7233 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007234 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007235
7236 /* unmap adapter SLIM and Control Registers */
7237 iounmap(phba->ctrl_regs_memmap_p);
7238 iounmap(phba->slim_memmap_p);
7239
James Smart3772a992009-05-22 14:50:54 -04007240 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007241
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05007242 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007243 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007244}
7245
Linas Vepstas8d63f372007-02-14 14:28:36 -06007246/**
James Smart3772a992009-05-22 14:50:54 -04007247 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007248 * @pdev: pointer to PCI device
7249 * @msg: power management message
7250 *
James Smart3772a992009-05-22 14:50:54 -04007251 * This routine is to be called from the kernel's PCI subsystem to support
7252 * system Power Management (PM) to device with SLI-3 interface spec. When
7253 * PM invokes this method, it quiesces the device by stopping the driver's
7254 * worker thread for the device, turning off device's interrupt and DMA,
7255 * and bring the device offline. Note that as the driver implements the
7256 * minimum PM requirements to a power-aware driver's PM support for the
7257 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7258 * to the suspend() method call will be treated as SUSPEND and the driver will
7259 * fully reinitialize its device during resume() method call, the driver will
7260 * set device to PCI_D3hot state in PCI config space instead of setting it
7261 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007262 *
7263 * Return code
James Smart3772a992009-05-22 14:50:54 -04007264 * 0 - driver suspended the device
7265 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007266 **/
7267static int
James Smart3772a992009-05-22 14:50:54 -04007268lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007269{
7270 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7271 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7272
7273 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7274 "0473 PCI device Power Management suspend.\n");
7275
7276 /* Bring down the device */
7277 lpfc_offline_prep(phba);
7278 lpfc_offline(phba);
7279 kthread_stop(phba->worker_thread);
7280
7281 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007282 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007283
7284 /* Save device state to PCI config space */
7285 pci_save_state(pdev);
7286 pci_set_power_state(pdev, PCI_D3hot);
7287
7288 return 0;
7289}
7290
7291/**
James Smart3772a992009-05-22 14:50:54 -04007292 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007293 * @pdev: pointer to PCI device
7294 *
James Smart3772a992009-05-22 14:50:54 -04007295 * This routine is to be called from the kernel's PCI subsystem to support
7296 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7297 * invokes this method, it restores the device's PCI config space state and
7298 * fully reinitializes the device and brings it online. Note that as the
7299 * driver implements the minimum PM requirements to a power-aware driver's
7300 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7301 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7302 * driver will fully reinitialize its device during resume() method call,
7303 * the device will be set to PCI_D0 directly in PCI config space before
7304 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007305 *
7306 * Return code
James Smart3772a992009-05-22 14:50:54 -04007307 * 0 - driver suspended the device
7308 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007309 **/
7310static int
James Smart3772a992009-05-22 14:50:54 -04007311lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007312{
7313 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7314 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007315 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007316 int error;
7317
7318 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7319 "0452 PCI device Power Management resume.\n");
7320
7321 /* Restore device state from PCI config space */
7322 pci_set_power_state(pdev, PCI_D0);
7323 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007324
James Smart1dfb5a42010-02-12 14:40:50 -05007325 /*
7326 * As the new kernel behavior of pci_restore_state() API call clears
7327 * device saved_state flag, need to save the restored state again.
7328 */
7329 pci_save_state(pdev);
7330
James Smart3a55b532008-12-04 22:38:54 -05007331 if (pdev->is_busmaster)
7332 pci_set_master(pdev);
7333
7334 /* Startup the kernel thread for this host adapter. */
7335 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7336 "lpfc_worker_%d", phba->brd_no);
7337 if (IS_ERR(phba->worker_thread)) {
7338 error = PTR_ERR(phba->worker_thread);
7339 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7340 "0434 PM resume failed to start worker "
7341 "thread: error=x%x.\n", error);
7342 return error;
7343 }
7344
James Smart5b75da22008-12-04 22:39:35 -05007345 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007346 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007347 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007349 "0430 PM resume Failed to enable interrupt\n");
7350 return -EIO;
7351 } else
7352 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007353
7354 /* Restart HBA and bring it online */
7355 lpfc_sli_brdrestart(phba);
7356 lpfc_online(phba);
7357
James Smart5b75da22008-12-04 22:39:35 -05007358 /* Log the current active interrupt mode */
7359 lpfc_log_intr_mode(phba, phba->intr_mode);
7360
James Smart3a55b532008-12-04 22:38:54 -05007361 return 0;
7362}
7363
7364/**
James Smart891478a2009-11-18 15:40:23 -05007365 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7366 * @phba: pointer to lpfc hba data structure.
7367 *
7368 * This routine is called to prepare the SLI3 device for PCI slot recover. It
7369 * aborts and stops all the on-going I/Os on the pci device.
7370 **/
7371static void
7372lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7373{
7374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7375 "2723 PCI channel I/O abort preparing for recovery\n");
7376 /* Prepare for bringing HBA offline */
7377 lpfc_offline_prep(phba);
7378 /* Clear sli active flag to prevent sysfs access to HBA */
7379 spin_lock_irq(&phba->hbalock);
7380 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7381 spin_unlock_irq(&phba->hbalock);
7382 /* Stop and flush all I/Os and bring HBA offline */
7383 lpfc_offline(phba);
7384}
7385
7386/**
James Smart0d878412009-10-02 15:16:56 -04007387 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7388 * @phba: pointer to lpfc hba data structure.
7389 *
7390 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7391 * disables the device interrupt and pci device, and aborts the internal FCP
7392 * pending I/Os.
7393 **/
7394static void
7395lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7396{
7397 struct lpfc_sli *psli = &phba->sli;
7398 struct lpfc_sli_ring *pring;
7399
7400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007401 "2710 PCI channel disable preparing for reset\n");
James Smart0d878412009-10-02 15:16:56 -04007402 /* Disable interrupt and pci device */
7403 lpfc_sli_disable_intr(phba);
7404 pci_disable_device(phba->pcidev);
7405 /*
7406 * There may be I/Os dropped by the firmware.
7407 * Error iocb (I/O) on txcmplq and let the SCSI layer
7408 * retry it after re-establishing link.
7409 */
7410 pring = &psli->ring[psli->fcp_ring];
7411 lpfc_sli_abort_iocb_ring(phba, pring);
7412}
7413
7414/**
7415 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7416 * @phba: pointer to lpfc hba data structure.
7417 *
7418 * This routine is called to prepare the SLI3 device for PCI slot permanently
7419 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7420 * pending I/Os.
7421 **/
7422static void
7423lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7424{
7425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007426 "2711 PCI channel permanent disable for failure\n");
James Smart0d878412009-10-02 15:16:56 -04007427 /* Clean up all driver's outstanding SCSI I/Os */
7428 lpfc_sli_flush_fcp_rings(phba);
7429}
7430
7431/**
James Smart3772a992009-05-22 14:50:54 -04007432 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007433 * @pdev: pointer to PCI device.
7434 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007435 *
James Smart3772a992009-05-22 14:50:54 -04007436 * This routine is called from the PCI subsystem for I/O error handling to
7437 * device with SLI-3 interface spec. This function is called by the PCI
7438 * subsystem after a PCI bus error affecting this device has been detected.
7439 * When this function is invoked, it will need to stop all the I/Os and
7440 * interrupt(s) to the device. Once that is done, it will return
7441 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7442 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007443 *
7444 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007445 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007446 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7447 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007448 **/
James Smart3772a992009-05-22 14:50:54 -04007449static pci_ers_result_t
7450lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007451{
James Smart51ef4c22007-08-02 11:10:31 -04007452 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7453 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007454
James Smartaacc20e2009-12-21 17:02:51 -05007455 /* Block all SCSI devices' I/Os on the host */
7456 lpfc_scsi_dev_block(phba);
7457
James Smart0d878412009-10-02 15:16:56 -04007458 switch (state) {
7459 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007460 /* Non-fatal error, prepare for recovery */
7461 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007462 return PCI_ERS_RESULT_CAN_RECOVER;
7463 case pci_channel_io_frozen:
7464 /* Fatal error, prepare for slot reset */
7465 lpfc_sli_prep_dev_for_reset(phba);
7466 return PCI_ERS_RESULT_NEED_RESET;
7467 case pci_channel_io_perm_failure:
7468 /* Permanent failure, prepare for device down */
7469 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007470 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007471 default:
7472 /* Unknown state, prepare and request slot reset */
7473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7474 "0472 Unknown PCI error state: x%x\n", state);
7475 lpfc_sli_prep_dev_for_reset(phba);
7476 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007477 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007478}
7479
7480/**
James Smart3772a992009-05-22 14:50:54 -04007481 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007482 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007483 *
James Smart3772a992009-05-22 14:50:54 -04007484 * This routine is called from the PCI subsystem for error handling to
7485 * device with SLI-3 interface spec. This is called after PCI bus has been
7486 * reset to restart the PCI card from scratch, as if from a cold-boot.
7487 * During the PCI subsystem error recovery, after driver returns
7488 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7489 * recovery and then call this routine before calling the .resume method
7490 * to recover the device. This function will initialize the HBA device,
7491 * enable the interrupt, but it will just put the HBA to offline state
7492 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007493 *
7494 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007495 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7496 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007497 */
James Smart3772a992009-05-22 14:50:54 -04007498static pci_ers_result_t
7499lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007500{
James Smart51ef4c22007-08-02 11:10:31 -04007501 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7502 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007503 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007504 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007505
7506 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007507 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007508 printk(KERN_ERR "lpfc: Cannot re-enable "
7509 "PCI device after reset.\n");
7510 return PCI_ERS_RESULT_DISCONNECT;
7511 }
7512
James Smart97207482008-12-04 22:39:19 -05007513 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05007514
7515 /*
7516 * As the new kernel behavior of pci_restore_state() API call clears
7517 * device saved_state flag, need to save the restored state again.
7518 */
7519 pci_save_state(pdev);
7520
James Smart97207482008-12-04 22:39:19 -05007521 if (pdev->is_busmaster)
7522 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007523
James Smart92d7f7b2007-06-17 19:56:38 -05007524 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007525 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007526 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007527
James Smart5b75da22008-12-04 22:39:35 -05007528 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007529 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007530 if (intr_mode == LPFC_INTR_ERROR) {
7531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7532 "0427 Cannot re-enable interrupt after "
7533 "slot reset.\n");
7534 return PCI_ERS_RESULT_DISCONNECT;
7535 } else
7536 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007537
7538 /* Take device offline; this will perform cleanup */
7539 lpfc_offline(phba);
7540 lpfc_sli_brdrestart(phba);
7541
James Smart5b75da22008-12-04 22:39:35 -05007542 /* Log the current active interrupt mode */
7543 lpfc_log_intr_mode(phba, phba->intr_mode);
7544
Linas Vepstas8d63f372007-02-14 14:28:36 -06007545 return PCI_ERS_RESULT_RECOVERED;
7546}
7547
7548/**
James Smart3772a992009-05-22 14:50:54 -04007549 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007550 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007551 *
James Smart3772a992009-05-22 14:50:54 -04007552 * This routine is called from the PCI subsystem for error handling to device
7553 * with SLI-3 interface spec. It is called when kernel error recovery tells
7554 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7555 * error recovery. After this call, traffic can start to flow from this device
7556 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007557 */
James Smart3772a992009-05-22 14:50:54 -04007558static void
7559lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007560{
James Smart51ef4c22007-08-02 11:10:31 -04007561 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7562 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007563
James Smart0d878412009-10-02 15:16:56 -04007564 /* Bring the device online */
James Smart58da1ff2008-04-07 10:15:56 -04007565 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04007566
7567 /* Clean up Advanced Error Reporting (AER) if needed */
7568 if (phba->hba_flag & HBA_AER_ENABLED)
7569 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007570}
7571
James Smart3772a992009-05-22 14:50:54 -04007572/**
James Smartda0436e2009-05-22 14:51:39 -04007573 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7574 * @phba: pointer to lpfc hba data structure.
7575 *
7576 * returns the number of ELS/CT IOCBs to reserve
7577 **/
7578int
7579lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7580{
7581 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7582
James Smartf1126682009-06-10 17:22:44 -04007583 if (phba->sli_rev == LPFC_SLI_REV4) {
7584 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04007585 return 10;
James Smartf1126682009-06-10 17:22:44 -04007586 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04007587 return 25;
James Smartf1126682009-06-10 17:22:44 -04007588 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04007589 return 50;
James Smartf1126682009-06-10 17:22:44 -04007590 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04007591 return 100;
James Smartf1126682009-06-10 17:22:44 -04007592 else
James Smart6a9c52c2009-10-02 15:16:51 -04007593 return 150;
James Smartf1126682009-06-10 17:22:44 -04007594 } else
7595 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007596}
7597
7598/**
7599 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7600 * @pdev: pointer to PCI device
7601 * @pid: pointer to PCI device identifier
7602 *
7603 * This routine is called from the kernel's PCI subsystem to device with
7604 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7605 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7606 * information of the device and driver to see if the driver state that it
7607 * can support this kind of device. If the match is successful, the driver
7608 * core invokes this routine. If this routine determines it can claim the HBA,
7609 * it does all the initialization that it needs to do to handle the HBA
7610 * properly.
7611 *
7612 * Return code
7613 * 0 - driver can claim the device
7614 * negative value - driver can not claim the device
7615 **/
7616static int __devinit
7617lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7618{
7619 struct lpfc_hba *phba;
7620 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007621 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007622 int error;
7623 uint32_t cfg_mode, intr_mode;
7624 int mcnt;
7625
7626 /* Allocate memory for HBA structure */
7627 phba = lpfc_hba_alloc(pdev);
7628 if (!phba)
7629 return -ENOMEM;
7630
7631 /* Perform generic PCI device enabling operation */
7632 error = lpfc_enable_pci_dev(phba);
7633 if (error) {
7634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7635 "1409 Failed to enable pci device.\n");
7636 goto out_free_phba;
7637 }
7638
7639 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7640 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7641 if (error)
7642 goto out_disable_pci_dev;
7643
7644 /* Set up SLI-4 specific device PCI memory space */
7645 error = lpfc_sli4_pci_mem_setup(phba);
7646 if (error) {
7647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7648 "1410 Failed to set up pci memory space.\n");
7649 goto out_disable_pci_dev;
7650 }
7651
7652 /* Set up phase-1 common device driver resources */
7653 error = lpfc_setup_driver_resource_phase1(phba);
7654 if (error) {
7655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7656 "1411 Failed to set up driver resource.\n");
7657 goto out_unset_pci_mem_s4;
7658 }
7659
7660 /* Set up SLI-4 Specific device driver resources */
7661 error = lpfc_sli4_driver_resource_setup(phba);
7662 if (error) {
7663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7664 "1412 Failed to set up driver resource.\n");
7665 goto out_unset_pci_mem_s4;
7666 }
7667
7668 /* Initialize and populate the iocb list per host */
7669 error = lpfc_init_iocb_list(phba,
7670 phba->sli4_hba.max_cfg_param.max_xri);
7671 if (error) {
7672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7673 "1413 Failed to initialize iocb list.\n");
7674 goto out_unset_driver_resource_s4;
7675 }
7676
7677 /* Set up common device driver resources */
7678 error = lpfc_setup_driver_resource_phase2(phba);
7679 if (error) {
7680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7681 "1414 Failed to set up driver resource.\n");
7682 goto out_free_iocb_list;
7683 }
7684
7685 /* Create SCSI host to the physical port */
7686 error = lpfc_create_shost(phba);
7687 if (error) {
7688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7689 "1415 Failed to create scsi host.\n");
7690 goto out_unset_driver_resource;
7691 }
7692
7693 /* Configure sysfs attributes */
7694 vport = phba->pport;
7695 error = lpfc_alloc_sysfs_attr(vport);
7696 if (error) {
7697 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7698 "1416 Failed to allocate sysfs attr\n");
7699 goto out_destroy_shost;
7700 }
7701
James Smart6669f9b2009-10-02 15:16:45 -04007702 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04007703 /* Now, trying to enable interrupt and bring up the device */
7704 cfg_mode = phba->cfg_use_msi;
7705 while (true) {
7706 /* Put device to a known state before enabling interrupt */
7707 lpfc_stop_port(phba);
7708 /* Configure and enable interrupt */
7709 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7710 if (intr_mode == LPFC_INTR_ERROR) {
7711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7712 "0426 Failed to enable interrupt.\n");
7713 error = -ENODEV;
7714 goto out_free_sysfs_attr;
7715 }
James Smartdef9c7a2009-12-21 17:02:28 -05007716 /* Default to single FCP EQ for non-MSI-X */
7717 if (phba->intr_type != MSIX)
7718 phba->cfg_fcp_eq_count = 1;
James Smartda0436e2009-05-22 14:51:39 -04007719 /* Set up SLI-4 HBA */
7720 if (lpfc_sli4_hba_setup(phba)) {
7721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7722 "1421 Failed to set up hba\n");
7723 error = -ENODEV;
7724 goto out_disable_intr;
7725 }
7726
7727 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7728 if (intr_mode != 0)
7729 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7730 LPFC_ACT_INTR_CNT);
7731
7732 /* Check active interrupts received only for MSI/MSI-X */
7733 if (intr_mode == 0 ||
7734 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7735 /* Log the current active interrupt mode */
7736 phba->intr_mode = intr_mode;
7737 lpfc_log_intr_mode(phba, intr_mode);
7738 break;
7739 }
7740 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7741 "0451 Configure interrupt mode (%d) "
7742 "failed active interrupt test.\n",
7743 intr_mode);
7744 /* Unset the preivous SLI-4 HBA setup */
7745 lpfc_sli4_unset_hba(phba);
7746 /* Try next level of interrupt mode */
7747 cfg_mode = --intr_mode;
7748 }
7749
7750 /* Perform post initialization setup */
7751 lpfc_post_init_setup(phba);
7752
James Smart1c6834a2009-07-19 10:01:26 -04007753 /* Check if there are static vports to be created. */
7754 lpfc_create_static_vport(phba);
7755
James Smartda0436e2009-05-22 14:51:39 -04007756 return 0;
7757
7758out_disable_intr:
7759 lpfc_sli4_disable_intr(phba);
7760out_free_sysfs_attr:
7761 lpfc_free_sysfs_attr(vport);
7762out_destroy_shost:
7763 lpfc_destroy_shost(phba);
7764out_unset_driver_resource:
7765 lpfc_unset_driver_resource_phase2(phba);
7766out_free_iocb_list:
7767 lpfc_free_iocb_list(phba);
7768out_unset_driver_resource_s4:
7769 lpfc_sli4_driver_resource_unset(phba);
7770out_unset_pci_mem_s4:
7771 lpfc_sli4_pci_mem_unset(phba);
7772out_disable_pci_dev:
7773 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007774 if (shost)
7775 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04007776out_free_phba:
7777 lpfc_hba_free(phba);
7778 return error;
7779}
7780
7781/**
7782 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
7783 * @pdev: pointer to PCI device
7784 *
7785 * This routine is called from the kernel's PCI subsystem to device with
7786 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7787 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7788 * device to be removed from the PCI subsystem properly.
7789 **/
7790static void __devexit
7791lpfc_pci_remove_one_s4(struct pci_dev *pdev)
7792{
7793 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7794 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7795 struct lpfc_vport **vports;
7796 struct lpfc_hba *phba = vport->phba;
7797 int i;
7798
7799 /* Mark the device unloading flag */
7800 spin_lock_irq(&phba->hbalock);
7801 vport->load_flag |= FC_UNLOADING;
7802 spin_unlock_irq(&phba->hbalock);
7803
7804 /* Free the HBA sysfs attributes */
7805 lpfc_free_sysfs_attr(vport);
7806
7807 /* Release all the vports against this physical port */
7808 vports = lpfc_create_vport_work_array(phba);
7809 if (vports != NULL)
7810 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7811 fc_vport_terminate(vports[i]->fc_vport);
7812 lpfc_destroy_vport_work_array(phba, vports);
7813
7814 /* Remove FC host and then SCSI host with the physical port */
7815 fc_remove_host(shost);
7816 scsi_remove_host(shost);
7817
7818 /* Perform cleanup on the physical port */
7819 lpfc_cleanup(vport);
7820
7821 /*
7822 * Bring down the SLI Layer. This step disables all interrupts,
7823 * clears the rings, discards all mailbox commands, and resets
7824 * the HBA FCoE function.
7825 */
7826 lpfc_debugfs_terminate(vport);
7827 lpfc_sli4_hba_unset(phba);
7828
7829 spin_lock_irq(&phba->hbalock);
7830 list_del_init(&vport->listentry);
7831 spin_unlock_irq(&phba->hbalock);
7832
7833 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7834 * buffers are released to their corresponding pools here.
7835 */
7836 lpfc_scsi_free(phba);
7837 lpfc_sli4_driver_resource_unset(phba);
7838
7839 /* Unmap adapter Control and Doorbell registers */
7840 lpfc_sli4_pci_mem_unset(phba);
7841
7842 /* Release PCI resources and disable device's PCI function */
7843 scsi_host_put(shost);
7844 lpfc_disable_pci_dev(phba);
7845
7846 /* Finally, free the driver's device data structure */
7847 lpfc_hba_free(phba);
7848
7849 return;
7850}
7851
7852/**
7853 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
7854 * @pdev: pointer to PCI device
7855 * @msg: power management message
7856 *
7857 * This routine is called from the kernel's PCI subsystem to support system
7858 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7859 * this method, it quiesces the device by stopping the driver's worker
7860 * thread for the device, turning off device's interrupt and DMA, and bring
7861 * the device offline. Note that as the driver implements the minimum PM
7862 * requirements to a power-aware driver's PM support for suspend/resume -- all
7863 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7864 * method call will be treated as SUSPEND and the driver will fully
7865 * reinitialize its device during resume() method call, the driver will set
7866 * device to PCI_D3hot state in PCI config space instead of setting it
7867 * according to the @msg provided by the PM.
7868 *
7869 * Return code
7870 * 0 - driver suspended the device
7871 * Error otherwise
7872 **/
7873static int
7874lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
7875{
7876 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7877 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7878
7879 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7880 "0298 PCI device Power Management suspend.\n");
7881
7882 /* Bring down the device */
7883 lpfc_offline_prep(phba);
7884 lpfc_offline(phba);
7885 kthread_stop(phba->worker_thread);
7886
7887 /* Disable interrupt from device */
7888 lpfc_sli4_disable_intr(phba);
7889
7890 /* Save device state to PCI config space */
7891 pci_save_state(pdev);
7892 pci_set_power_state(pdev, PCI_D3hot);
7893
7894 return 0;
7895}
7896
7897/**
7898 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
7899 * @pdev: pointer to PCI device
7900 *
7901 * This routine is called from the kernel's PCI subsystem to support system
7902 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
7903 * this method, it restores the device's PCI config space state and fully
7904 * reinitializes the device and brings it online. Note that as the driver
7905 * implements the minimum PM requirements to a power-aware driver's PM for
7906 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7907 * to the suspend() method call will be treated as SUSPEND and the driver
7908 * will fully reinitialize its device during resume() method call, the device
7909 * will be set to PCI_D0 directly in PCI config space before restoring the
7910 * state.
7911 *
7912 * Return code
7913 * 0 - driver suspended the device
7914 * Error otherwise
7915 **/
7916static int
7917lpfc_pci_resume_one_s4(struct pci_dev *pdev)
7918{
7919 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7920 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7921 uint32_t intr_mode;
7922 int error;
7923
7924 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7925 "0292 PCI device Power Management resume.\n");
7926
7927 /* Restore device state from PCI config space */
7928 pci_set_power_state(pdev, PCI_D0);
7929 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05007930
7931 /*
7932 * As the new kernel behavior of pci_restore_state() API call clears
7933 * device saved_state flag, need to save the restored state again.
7934 */
7935 pci_save_state(pdev);
7936
James Smartda0436e2009-05-22 14:51:39 -04007937 if (pdev->is_busmaster)
7938 pci_set_master(pdev);
7939
7940 /* Startup the kernel thread for this host adapter. */
7941 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7942 "lpfc_worker_%d", phba->brd_no);
7943 if (IS_ERR(phba->worker_thread)) {
7944 error = PTR_ERR(phba->worker_thread);
7945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7946 "0293 PM resume failed to start worker "
7947 "thread: error=x%x.\n", error);
7948 return error;
7949 }
7950
7951 /* Configure and enable interrupt */
7952 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
7953 if (intr_mode == LPFC_INTR_ERROR) {
7954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7955 "0294 PM resume Failed to enable interrupt\n");
7956 return -EIO;
7957 } else
7958 phba->intr_mode = intr_mode;
7959
7960 /* Restart HBA and bring it online */
7961 lpfc_sli_brdrestart(phba);
7962 lpfc_online(phba);
7963
7964 /* Log the current active interrupt mode */
7965 lpfc_log_intr_mode(phba, phba->intr_mode);
7966
7967 return 0;
7968}
7969
7970/**
7971 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
7972 * @pdev: pointer to PCI device.
7973 * @state: the current PCI connection state.
7974 *
7975 * This routine is called from the PCI subsystem for error handling to device
7976 * with SLI-4 interface spec. This function is called by the PCI subsystem
7977 * after a PCI bus error affecting this device has been detected. When this
7978 * function is invoked, it will need to stop all the I/Os and interrupt(s)
7979 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
7980 * for the PCI subsystem to perform proper recovery as desired.
7981 *
7982 * Return codes
7983 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7984 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7985 **/
7986static pci_ers_result_t
7987lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
7988{
7989 return PCI_ERS_RESULT_NEED_RESET;
7990}
7991
7992/**
7993 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
7994 * @pdev: pointer to PCI device.
7995 *
7996 * This routine is called from the PCI subsystem for error handling to device
7997 * with SLI-4 interface spec. It is called after PCI bus has been reset to
7998 * restart the PCI card from scratch, as if from a cold-boot. During the
7999 * PCI subsystem error recovery, after the driver returns
8000 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8001 * recovery and then call this routine before calling the .resume method to
8002 * recover the device. This function will initialize the HBA device, enable
8003 * the interrupt, but it will just put the HBA to offline state without
8004 * passing any I/O traffic.
8005 *
8006 * Return codes
8007 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8008 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8009 */
8010static pci_ers_result_t
8011lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8012{
8013 return PCI_ERS_RESULT_RECOVERED;
8014}
8015
8016/**
8017 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8018 * @pdev: pointer to PCI device
8019 *
8020 * This routine is called from the PCI subsystem for error handling to device
8021 * with SLI-4 interface spec. It is called when kernel error recovery tells
8022 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8023 * error recovery. After this call, traffic can start to flow from this device
8024 * again.
8025 **/
8026static void
8027lpfc_io_resume_s4(struct pci_dev *pdev)
8028{
8029 return;
8030}
8031
8032/**
James Smart3772a992009-05-22 14:50:54 -04008033 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8034 * @pdev: pointer to PCI device
8035 * @pid: pointer to PCI device identifier
8036 *
8037 * This routine is to be registered to the kernel's PCI subsystem. When an
8038 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8039 * at PCI device-specific information of the device and driver to see if the
8040 * driver state that it can support this kind of device. If the match is
8041 * successful, the driver core invokes this routine. This routine dispatches
8042 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8043 * do all the initialization that it needs to do to handle the HBA device
8044 * properly.
8045 *
8046 * Return code
8047 * 0 - driver can claim the device
8048 * negative value - driver can not claim the device
8049 **/
8050static int __devinit
8051lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8052{
8053 int rc;
James Smart8fa38512009-07-19 10:01:03 -04008054 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04008055
James Smart8fa38512009-07-19 10:01:03 -04008056 if (pci_read_config_dword(pdev, LPFC_SLIREV_CONF_WORD, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04008057 return -ENODEV;
8058
James Smart8fa38512009-07-19 10:01:03 -04008059 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
8060 (bf_get(lpfc_sli_intf_rev, &intf) == LPFC_SLIREV_CONF_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04008061 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008062 else
James Smart3772a992009-05-22 14:50:54 -04008063 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008064
James Smart3772a992009-05-22 14:50:54 -04008065 return rc;
8066}
8067
8068/**
8069 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8070 * @pdev: pointer to PCI device
8071 *
8072 * This routine is to be registered to the kernel's PCI subsystem. When an
8073 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8074 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8075 * remove routine, which will perform all the necessary cleanup for the
8076 * device to be removed from the PCI subsystem properly.
8077 **/
8078static void __devexit
8079lpfc_pci_remove_one(struct pci_dev *pdev)
8080{
8081 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8082 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8083
8084 switch (phba->pci_dev_grp) {
8085 case LPFC_PCI_DEV_LP:
8086 lpfc_pci_remove_one_s3(pdev);
8087 break;
James Smartda0436e2009-05-22 14:51:39 -04008088 case LPFC_PCI_DEV_OC:
8089 lpfc_pci_remove_one_s4(pdev);
8090 break;
James Smart3772a992009-05-22 14:50:54 -04008091 default:
8092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8093 "1424 Invalid PCI device group: 0x%x\n",
8094 phba->pci_dev_grp);
8095 break;
8096 }
8097 return;
8098}
8099
8100/**
8101 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8102 * @pdev: pointer to PCI device
8103 * @msg: power management message
8104 *
8105 * This routine is to be registered to the kernel's PCI subsystem to support
8106 * system Power Management (PM). When PM invokes this method, it dispatches
8107 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8108 * suspend the device.
8109 *
8110 * Return code
8111 * 0 - driver suspended the device
8112 * Error otherwise
8113 **/
8114static int
8115lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8116{
8117 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8118 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8119 int rc = -ENODEV;
8120
8121 switch (phba->pci_dev_grp) {
8122 case LPFC_PCI_DEV_LP:
8123 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8124 break;
James Smartda0436e2009-05-22 14:51:39 -04008125 case LPFC_PCI_DEV_OC:
8126 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8127 break;
James Smart3772a992009-05-22 14:50:54 -04008128 default:
8129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8130 "1425 Invalid PCI device group: 0x%x\n",
8131 phba->pci_dev_grp);
8132 break;
8133 }
8134 return rc;
8135}
8136
8137/**
8138 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8139 * @pdev: pointer to PCI device
8140 *
8141 * This routine is to be registered to the kernel's PCI subsystem to support
8142 * system Power Management (PM). When PM invokes this method, it dispatches
8143 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8144 * resume the device.
8145 *
8146 * Return code
8147 * 0 - driver suspended the device
8148 * Error otherwise
8149 **/
8150static int
8151lpfc_pci_resume_one(struct pci_dev *pdev)
8152{
8153 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8154 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8155 int rc = -ENODEV;
8156
8157 switch (phba->pci_dev_grp) {
8158 case LPFC_PCI_DEV_LP:
8159 rc = lpfc_pci_resume_one_s3(pdev);
8160 break;
James Smartda0436e2009-05-22 14:51:39 -04008161 case LPFC_PCI_DEV_OC:
8162 rc = lpfc_pci_resume_one_s4(pdev);
8163 break;
James Smart3772a992009-05-22 14:50:54 -04008164 default:
8165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8166 "1426 Invalid PCI device group: 0x%x\n",
8167 phba->pci_dev_grp);
8168 break;
8169 }
8170 return rc;
8171}
8172
8173/**
8174 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8175 * @pdev: pointer to PCI device.
8176 * @state: the current PCI connection state.
8177 *
8178 * This routine is registered to the PCI subsystem for error handling. This
8179 * function is called by the PCI subsystem after a PCI bus error affecting
8180 * this device has been detected. When this routine is invoked, it dispatches
8181 * the action to the proper SLI-3 or SLI-4 device error detected handling
8182 * routine, which will perform the proper error detected operation.
8183 *
8184 * Return codes
8185 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8186 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8187 **/
8188static pci_ers_result_t
8189lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8190{
8191 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8192 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8193 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8194
8195 switch (phba->pci_dev_grp) {
8196 case LPFC_PCI_DEV_LP:
8197 rc = lpfc_io_error_detected_s3(pdev, state);
8198 break;
James Smartda0436e2009-05-22 14:51:39 -04008199 case LPFC_PCI_DEV_OC:
8200 rc = lpfc_io_error_detected_s4(pdev, state);
8201 break;
James Smart3772a992009-05-22 14:50:54 -04008202 default:
8203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8204 "1427 Invalid PCI device group: 0x%x\n",
8205 phba->pci_dev_grp);
8206 break;
8207 }
8208 return rc;
8209}
8210
8211/**
8212 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8213 * @pdev: pointer to PCI device.
8214 *
8215 * This routine is registered to the PCI subsystem for error handling. This
8216 * function is called after PCI bus has been reset to restart the PCI card
8217 * from scratch, as if from a cold-boot. When this routine is invoked, it
8218 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8219 * routine, which will perform the proper device reset.
8220 *
8221 * Return codes
8222 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8223 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8224 **/
8225static pci_ers_result_t
8226lpfc_io_slot_reset(struct pci_dev *pdev)
8227{
8228 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8229 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8230 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8231
8232 switch (phba->pci_dev_grp) {
8233 case LPFC_PCI_DEV_LP:
8234 rc = lpfc_io_slot_reset_s3(pdev);
8235 break;
James Smartda0436e2009-05-22 14:51:39 -04008236 case LPFC_PCI_DEV_OC:
8237 rc = lpfc_io_slot_reset_s4(pdev);
8238 break;
James Smart3772a992009-05-22 14:50:54 -04008239 default:
8240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8241 "1428 Invalid PCI device group: 0x%x\n",
8242 phba->pci_dev_grp);
8243 break;
8244 }
8245 return rc;
8246}
8247
8248/**
8249 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8250 * @pdev: pointer to PCI device
8251 *
8252 * This routine is registered to the PCI subsystem for error handling. It
8253 * is called when kernel error recovery tells the lpfc driver that it is
8254 * OK to resume normal PCI operation after PCI bus error recovery. When
8255 * this routine is invoked, it dispatches the action to the proper SLI-3
8256 * or SLI-4 device io_resume routine, which will resume the device operation.
8257 **/
8258static void
8259lpfc_io_resume(struct pci_dev *pdev)
8260{
8261 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8262 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8263
8264 switch (phba->pci_dev_grp) {
8265 case LPFC_PCI_DEV_LP:
8266 lpfc_io_resume_s3(pdev);
8267 break;
James Smartda0436e2009-05-22 14:51:39 -04008268 case LPFC_PCI_DEV_OC:
8269 lpfc_io_resume_s4(pdev);
8270 break;
James Smart3772a992009-05-22 14:50:54 -04008271 default:
8272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8273 "1429 Invalid PCI device group: 0x%x\n",
8274 phba->pci_dev_grp);
8275 break;
8276 }
8277 return;
8278}
8279
dea31012005-04-17 16:05:31 -05008280static struct pci_device_id lpfc_id_table[] = {
8281 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8282 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008283 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8284 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008285 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8286 PCI_ANY_ID, PCI_ANY_ID, },
8287 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8288 PCI_ANY_ID, PCI_ANY_ID, },
8289 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8290 PCI_ANY_ID, PCI_ANY_ID, },
8291 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8292 PCI_ANY_ID, PCI_ANY_ID, },
8293 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8294 PCI_ANY_ID, PCI_ANY_ID, },
8295 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8296 PCI_ANY_ID, PCI_ANY_ID, },
8297 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8298 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008299 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8300 PCI_ANY_ID, PCI_ANY_ID, },
8301 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8302 PCI_ANY_ID, PCI_ANY_ID, },
8303 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8304 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008305 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8306 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008307 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8308 PCI_ANY_ID, PCI_ANY_ID, },
8309 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8310 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008311 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8312 PCI_ANY_ID, PCI_ANY_ID, },
8313 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8314 PCI_ANY_ID, PCI_ANY_ID, },
8315 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8316 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008317 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8318 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008319 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8320 PCI_ANY_ID, PCI_ANY_ID, },
8321 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8322 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008323 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8324 PCI_ANY_ID, PCI_ANY_ID, },
8325 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8326 PCI_ANY_ID, PCI_ANY_ID, },
8327 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8328 PCI_ANY_ID, PCI_ANY_ID, },
8329 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8330 PCI_ANY_ID, PCI_ANY_ID, },
8331 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8332 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008333 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8334 PCI_ANY_ID, PCI_ANY_ID, },
8335 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8336 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008337 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8338 PCI_ANY_ID, PCI_ANY_ID, },
8339 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8340 PCI_ANY_ID, PCI_ANY_ID, },
8341 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8342 PCI_ANY_ID, PCI_ANY_ID, },
8343 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8344 PCI_ANY_ID, PCI_ANY_ID, },
8345 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8346 PCI_ANY_ID, PCI_ANY_ID, },
8347 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8348 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008349 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8350 PCI_ANY_ID, PCI_ANY_ID, },
8351 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8352 PCI_ANY_ID, PCI_ANY_ID, },
8353 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8354 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008355 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8356 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05008357 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8358 PCI_ANY_ID, PCI_ANY_ID, },
8359 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04008360 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008361 { 0 }
8362};
8363
8364MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8365
Linas Vepstas8d63f372007-02-14 14:28:36 -06008366static struct pci_error_handlers lpfc_err_handler = {
8367 .error_detected = lpfc_io_error_detected,
8368 .slot_reset = lpfc_io_slot_reset,
8369 .resume = lpfc_io_resume,
8370};
8371
dea31012005-04-17 16:05:31 -05008372static struct pci_driver lpfc_driver = {
8373 .name = LPFC_DRIVER_NAME,
8374 .id_table = lpfc_id_table,
8375 .probe = lpfc_pci_probe_one,
8376 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008377 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008378 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008379 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008380};
8381
James Smarte59058c2008-08-24 21:49:00 -04008382/**
James Smart3621a712009-04-06 18:47:14 -04008383 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008384 *
8385 * This routine is to be invoked when the lpfc module is loaded into the
8386 * kernel. The special kernel macro module_init() is used to indicate the
8387 * role of this routine to the kernel as lpfc module entry point.
8388 *
8389 * Return codes
8390 * 0 - successful
8391 * -ENOMEM - FC attach transport failed
8392 * all others - failed
8393 */
dea31012005-04-17 16:05:31 -05008394static int __init
8395lpfc_init(void)
8396{
8397 int error = 0;
8398
8399 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008400 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008401
James Smart7ee5d432007-10-27 13:37:17 -04008402 if (lpfc_enable_npiv) {
8403 lpfc_transport_functions.vport_create = lpfc_vport_create;
8404 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8405 }
dea31012005-04-17 16:05:31 -05008406 lpfc_transport_template =
8407 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008408 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008409 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008410 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008411 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008412 fc_attach_transport(&lpfc_vport_transport_functions);
8413 if (lpfc_vport_transport_template == NULL) {
8414 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008415 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008416 }
James Smart7ee5d432007-10-27 13:37:17 -04008417 }
dea31012005-04-17 16:05:31 -05008418 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008419 if (error) {
dea31012005-04-17 16:05:31 -05008420 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008421 if (lpfc_enable_npiv)
8422 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008423 }
dea31012005-04-17 16:05:31 -05008424
8425 return error;
8426}
8427
James Smarte59058c2008-08-24 21:49:00 -04008428/**
James Smart3621a712009-04-06 18:47:14 -04008429 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008430 *
8431 * This routine is invoked when the lpfc module is removed from the kernel.
8432 * The special kernel macro module_exit() is used to indicate the role of
8433 * this routine to the kernel as lpfc module exit point.
8434 */
dea31012005-04-17 16:05:31 -05008435static void __exit
8436lpfc_exit(void)
8437{
8438 pci_unregister_driver(&lpfc_driver);
8439 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008440 if (lpfc_enable_npiv)
8441 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008442 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008443 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8444 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008445 (1L << _dump_buf_data_order), _dump_buf_data);
8446 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8447 }
8448
8449 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008450 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8451 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008452 (1L << _dump_buf_dif_order), _dump_buf_dif);
8453 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8454 }
dea31012005-04-17 16:05:31 -05008455}
8456
8457module_init(lpfc_init);
8458module_exit(lpfc_exit);
8459MODULE_LICENSE("GPL");
8460MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8461MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8462MODULE_VERSION("0:" LPFC_DRIVER_VERSION);