blob: b0b7bb39f054ebd663d01004bbccd7ae8f92706a [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) {
James Smart28baac72010-02-12 14:42:03 -05002446 shost->dma_boundary =
2447 phba->sli4_hba.pc_sli4_params.sge_supp_len;
James Smartda0436e2009-05-22 14:51:39 -04002448 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2449 }
James Smart81301a92008-12-04 22:39:46 -05002450
James Smart47a86172007-04-25 09:53:22 -04002451 /*
James Smart2e0fef82007-06-17 19:56:36 -05002452 * Set initial can_queue value since 0 is no longer supported and
2453 * scsi_add_host will fail. This will be adjusted later based on the
2454 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002455 */
James Smart2e0fef82007-06-17 19:56:36 -05002456 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002457 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002458 shost->transportt = lpfc_vport_transport_template;
2459 vport->port_type = LPFC_NPIV_PORT;
2460 } else {
2461 shost->transportt = lpfc_transport_template;
2462 vport->port_type = LPFC_PHYSICAL_PORT;
2463 }
James Smart47a86172007-04-25 09:53:22 -04002464
James Smart2e0fef82007-06-17 19:56:36 -05002465 /* Initialize all internally managed lists. */
2466 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002467 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002468 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002469
James Smart2e0fef82007-06-17 19:56:36 -05002470 init_timer(&vport->fc_disctmo);
2471 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002472 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002473
James Smart2e0fef82007-06-17 19:56:36 -05002474 init_timer(&vport->fc_fdmitmo);
2475 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002476 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002477
James Smart2e0fef82007-06-17 19:56:36 -05002478 init_timer(&vport->els_tmofunc);
2479 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002480 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002481
James Bottomleyd139b9b2009-11-05 13:33:12 -06002482 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002483 if (error)
2484 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002485
James Smart549e55c2007-08-02 11:09:51 -04002486 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002487 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002488 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002489 return vport;
James Smart47a86172007-04-25 09:53:22 -04002490
James Smart2e0fef82007-06-17 19:56:36 -05002491out_put_shost:
2492 scsi_host_put(shost);
2493out:
2494 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002495}
2496
James Smarte59058c2008-08-24 21:49:00 -04002497/**
James Smart3621a712009-04-06 18:47:14 -04002498 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002499 * @vport: pointer to an lpfc virtual N_Port data structure.
2500 *
2501 * This routine destroys a FC port from the upper layer protocol. All the
2502 * resources associated with the port are released.
2503 **/
James Smart2e0fef82007-06-17 19:56:36 -05002504void
2505destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002506{
James Smart92d7f7b2007-06-17 19:56:38 -05002507 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2508 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002509
James Smart858c9f62007-06-17 19:56:39 -05002510 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002511 fc_remove_host(shost);
2512 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002513
James Smart92d7f7b2007-06-17 19:56:38 -05002514 spin_lock_irq(&phba->hbalock);
2515 list_del_init(&vport->listentry);
2516 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002517
James Smart92d7f7b2007-06-17 19:56:38 -05002518 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002519 return;
James Smart47a86172007-04-25 09:53:22 -04002520}
2521
James Smarte59058c2008-08-24 21:49:00 -04002522/**
James Smart3621a712009-04-06 18:47:14 -04002523 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002524 *
2525 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2526 * uses the kernel idr facility to perform the task.
2527 *
2528 * Return codes:
2529 * instance - a unique integer ID allocated as the new instance.
2530 * -1 - lpfc get instance failed.
2531 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002532int
2533lpfc_get_instance(void)
2534{
2535 int instance = 0;
2536
2537 /* Assign an unused number */
2538 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2539 return -1;
2540 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2541 return -1;
2542 return instance;
2543}
2544
James Smarte59058c2008-08-24 21:49:00 -04002545/**
James Smart3621a712009-04-06 18:47:14 -04002546 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002547 * @shost: pointer to SCSI host data structure.
2548 * @time: elapsed time of the scan in jiffies.
2549 *
2550 * This routine is called by the SCSI layer with a SCSI host to determine
2551 * whether the scan host is finished.
2552 *
2553 * Note: there is no scan_start function as adapter initialization will have
2554 * asynchronously kicked off the link initialization.
2555 *
2556 * Return codes
2557 * 0 - SCSI host scan is not over yet.
2558 * 1 - SCSI host scan is over.
2559 **/
James Smart47a86172007-04-25 09:53:22 -04002560int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2561{
James Smart2e0fef82007-06-17 19:56:36 -05002562 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2563 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002564 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002565
James Smart858c9f62007-06-17 19:56:39 -05002566 spin_lock_irq(shost->host_lock);
2567
James Smart51ef4c22007-08-02 11:10:31 -04002568 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002569 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002570 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002571 }
James Smart2e0fef82007-06-17 19:56:36 -05002572 if (time >= 30 * HZ) {
2573 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002574 "0461 Scanning longer than 30 "
2575 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002576 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002577 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002578 }
2579 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2580 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002581 "0465 Link down longer than 15 "
2582 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002583 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002584 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002585 }
2586
James Smart2e0fef82007-06-17 19:56:36 -05002587 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002588 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002589 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002590 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002591 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002592 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002593 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002594 goto finished;
2595
2596 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002597
2598finished:
James Smart858c9f62007-06-17 19:56:39 -05002599 spin_unlock_irq(shost->host_lock);
2600 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002601}
2602
James Smarte59058c2008-08-24 21:49:00 -04002603/**
James Smart3621a712009-04-06 18:47:14 -04002604 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002605 * @shost: pointer to SCSI host data structure.
2606 *
2607 * This routine initializes a given SCSI host attributes on a FC port. The
2608 * SCSI host can be either on top of a physical port or a virtual port.
2609 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002610void lpfc_host_attrib_init(struct Scsi_Host *shost)
2611{
2612 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2613 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002614 /*
James Smart2e0fef82007-06-17 19:56:36 -05002615 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002616 */
2617
James Smart2e0fef82007-06-17 19:56:36 -05002618 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2619 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002620 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2621
2622 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002623 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002624 fc_host_supported_fc4s(shost)[2] = 1;
2625 fc_host_supported_fc4s(shost)[7] = 1;
2626
James Smart92d7f7b2007-06-17 19:56:38 -05002627 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2628 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002629
2630 fc_host_supported_speeds(shost) = 0;
2631 if (phba->lmt & LMT_10Gb)
2632 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002633 if (phba->lmt & LMT_8Gb)
2634 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002635 if (phba->lmt & LMT_4Gb)
2636 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2637 if (phba->lmt & LMT_2Gb)
2638 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2639 if (phba->lmt & LMT_1Gb)
2640 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2641
2642 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002643 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2644 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002645
2646 /* This value is also unchanging */
2647 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002648 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002649 fc_host_active_fc4s(shost)[2] = 1;
2650 fc_host_active_fc4s(shost)[7] = 1;
2651
James Smart92d7f7b2007-06-17 19:56:38 -05002652 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002653 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002654 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002655 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002656}
dea31012005-04-17 16:05:31 -05002657
James Smarte59058c2008-08-24 21:49:00 -04002658/**
James Smartda0436e2009-05-22 14:51:39 -04002659 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002660 * @phba: pointer to lpfc hba data structure.
2661 *
James Smartda0436e2009-05-22 14:51:39 -04002662 * This routine is invoked to stop an SLI3 device port, it stops the device
2663 * from generating interrupts and stops the device driver's timers for the
2664 * device.
James Smarte59058c2008-08-24 21:49:00 -04002665 **/
James Smartdb2378e2008-02-08 18:49:51 -05002666static void
James Smartda0436e2009-05-22 14:51:39 -04002667lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002668{
James Smartda0436e2009-05-22 14:51:39 -04002669 /* Clear all interrupt enable conditions */
2670 writel(0, phba->HCregaddr);
2671 readl(phba->HCregaddr); /* flush */
2672 /* Clear all pending interrupts */
2673 writel(0xffffffff, phba->HAregaddr);
2674 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002675
James Smartda0436e2009-05-22 14:51:39 -04002676 /* Reset some HBA SLI setup states */
2677 lpfc_stop_hba_timers(phba);
2678 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002679}
2680
James Smarte59058c2008-08-24 21:49:00 -04002681/**
James Smartda0436e2009-05-22 14:51:39 -04002682 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002683 * @phba: pointer to lpfc hba data structure.
2684 *
James Smartda0436e2009-05-22 14:51:39 -04002685 * This routine is invoked to stop an SLI4 device port, it stops the device
2686 * from generating interrupts and stops the device driver's timers for the
2687 * device.
2688 **/
2689static void
2690lpfc_stop_port_s4(struct lpfc_hba *phba)
2691{
2692 /* Reset some HBA SLI4 setup states */
2693 lpfc_stop_hba_timers(phba);
2694 phba->pport->work_port_events = 0;
2695 phba->sli4_hba.intr_enable = 0;
2696 /* Hard clear it for now, shall have more graceful way to wait later */
2697 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2698}
2699
2700/**
2701 * lpfc_stop_port - Wrapper function for stopping hba port
2702 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002703 *
James Smartda0436e2009-05-22 14:51:39 -04002704 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2705 * the API jump table function pointer from the lpfc_hba struct.
2706 **/
2707void
2708lpfc_stop_port(struct lpfc_hba *phba)
2709{
2710 phba->lpfc_stop_port(phba);
2711}
2712
2713/**
2714 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2715 * @phba: pointer to lpfc hba data structure.
2716 *
2717 * This routine is invoked to remove the driver default fcf record from
2718 * the port. This routine currently acts on FCF Index 0.
2719 *
2720 **/
2721void
2722lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2723{
2724 int rc = 0;
2725 LPFC_MBOXQ_t *mboxq;
2726 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2727 uint32_t mbox_tmo, req_len;
2728 uint32_t shdr_status, shdr_add_status;
2729
2730 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2731 if (!mboxq) {
2732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2733 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2734 return;
2735 }
2736
2737 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2738 sizeof(struct lpfc_sli4_cfg_mhdr);
2739 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2740 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2741 req_len, LPFC_SLI4_MBX_EMBED);
2742 /*
2743 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2744 * supports multiple FCF indices.
2745 */
2746 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2747 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2748 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
James Smartecfd03c2010-02-12 14:41:27 -05002749 phba->fcf.current_rec.fcf_indx);
James Smartda0436e2009-05-22 14:51:39 -04002750
2751 if (!phba->sli4_hba.intr_enable)
2752 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2753 else {
2754 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2755 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2756 }
2757 /* The IOCTL status is embedded in the mailbox subheader. */
2758 shdr_status = bf_get(lpfc_mbox_hdr_status,
2759 &del_fcf_record->header.cfg_shdr.response);
2760 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2761 &del_fcf_record->header.cfg_shdr.response);
2762 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2763 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2764 "2516 DEL FCF of default FCF Index failed "
2765 "mbx status x%x, status x%x add_status x%x\n",
2766 rc, shdr_status, shdr_add_status);
2767 }
2768 if (rc != MBX_TIMEOUT)
2769 mempool_free(mboxq, phba->mbox_mem_pool);
2770}
2771
2772/**
James Smartecfd03c2010-02-12 14:41:27 -05002773 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2774 * @phba: Pointer to hba for which this call is being executed.
2775 *
2776 * This routine starts the timer waiting for the FCF rediscovery to complete.
2777 **/
2778void
2779lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2780{
2781 unsigned long fcf_redisc_wait_tmo =
2782 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2783 /* Start fcf rediscovery wait period timer */
2784 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2785 spin_lock_irq(&phba->hbalock);
2786 /* Allow action to new fcf asynchronous event */
2787 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2788 /* Mark the FCF rediscovery pending state */
2789 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2790 spin_unlock_irq(&phba->hbalock);
2791}
2792
2793/**
2794 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2795 * @ptr: Map to lpfc_hba data structure pointer.
2796 *
2797 * This routine is invoked when waiting for FCF table rediscover has been
2798 * timed out. If new FCF record(s) has (have) been discovered during the
2799 * wait period, a new FCF event shall be added to the FCOE async event
2800 * list, and then worker thread shall be waked up for processing from the
2801 * worker thread context.
2802 **/
2803void
2804lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2805{
2806 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2807
2808 /* Don't send FCF rediscovery event if timer cancelled */
2809 spin_lock_irq(&phba->hbalock);
2810 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2811 spin_unlock_irq(&phba->hbalock);
2812 return;
2813 }
2814 /* Clear FCF rediscovery timer pending flag */
2815 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2816 /* FCF rediscovery event to worker thread */
2817 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2818 spin_unlock_irq(&phba->hbalock);
2819 /* wake up worker thread */
2820 lpfc_worker_wake_up(phba);
2821}
2822
2823/**
James Smart6669f9b2009-10-02 15:16:45 -04002824 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2825 * @phba: pointer to lpfc hba data structure.
2826 *
2827 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2828 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2829 * was successful and the firmware supports FCoE. Any other return indicates
2830 * a error. It is assumed that this function will be called before interrupts
2831 * are enabled.
2832 **/
2833static int
2834lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2835{
2836 int rc = 0;
2837 LPFC_MBOXQ_t *mboxq;
2838 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2839 uint32_t length;
2840 uint32_t shdr_status, shdr_add_status;
2841
2842 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2843 if (!mboxq) {
2844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2845 "2621 Failed to allocate mbox for "
2846 "query firmware config cmd\n");
2847 return -ENOMEM;
2848 }
2849 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2850 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2851 sizeof(struct lpfc_sli4_cfg_mhdr));
2852 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2853 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2854 length, LPFC_SLI4_MBX_EMBED);
2855 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2856 /* The IOCTL status is embedded in the mailbox subheader. */
2857 shdr_status = bf_get(lpfc_mbox_hdr_status,
2858 &query_fw_cfg->header.cfg_shdr.response);
2859 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2860 &query_fw_cfg->header.cfg_shdr.response);
2861 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2862 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2863 "2622 Query Firmware Config failed "
2864 "mbx status x%x, status x%x add_status x%x\n",
2865 rc, shdr_status, shdr_add_status);
2866 return -EINVAL;
2867 }
2868 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2870 "2623 FCoE Function not supported by firmware. "
2871 "Function mode = %08x\n",
2872 query_fw_cfg->function_mode);
2873 return -EINVAL;
2874 }
2875 if (rc != MBX_TIMEOUT)
2876 mempool_free(mboxq, phba->mbox_mem_pool);
2877 return 0;
2878}
2879
2880/**
James Smartda0436e2009-05-22 14:51:39 -04002881 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2882 * @phba: pointer to lpfc hba data structure.
2883 * @acqe_link: pointer to the async link completion queue entry.
2884 *
2885 * This routine is to parse the SLI4 link-attention link fault code and
2886 * translate it into the base driver's read link attention mailbox command
2887 * status.
2888 *
2889 * Return: Link-attention status in terms of base driver's coding.
2890 **/
2891static uint16_t
2892lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2893 struct lpfc_acqe_link *acqe_link)
2894{
2895 uint16_t latt_fault;
2896
2897 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2898 case LPFC_ASYNC_LINK_FAULT_NONE:
2899 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2900 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2901 latt_fault = 0;
2902 break;
2903 default:
2904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2905 "0398 Invalid link fault code: x%x\n",
2906 bf_get(lpfc_acqe_link_fault, acqe_link));
2907 latt_fault = MBXERR_ERROR;
2908 break;
2909 }
2910 return latt_fault;
2911}
2912
2913/**
2914 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2915 * @phba: pointer to lpfc hba data structure.
2916 * @acqe_link: pointer to the async link completion queue entry.
2917 *
2918 * This routine is to parse the SLI4 link attention type and translate it
2919 * into the base driver's link attention type coding.
2920 *
2921 * Return: Link attention type in terms of base driver's coding.
2922 **/
2923static uint8_t
2924lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2925 struct lpfc_acqe_link *acqe_link)
2926{
2927 uint8_t att_type;
2928
2929 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2930 case LPFC_ASYNC_LINK_STATUS_DOWN:
2931 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2932 att_type = AT_LINK_DOWN;
2933 break;
2934 case LPFC_ASYNC_LINK_STATUS_UP:
2935 /* Ignore physical link up events - wait for logical link up */
2936 att_type = AT_RESERVED;
2937 break;
2938 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2939 att_type = AT_LINK_UP;
2940 break;
2941 default:
2942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2943 "0399 Invalid link attention type: x%x\n",
2944 bf_get(lpfc_acqe_link_status, acqe_link));
2945 att_type = AT_RESERVED;
2946 break;
2947 }
2948 return att_type;
2949}
2950
2951/**
2952 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2953 * @phba: pointer to lpfc hba data structure.
2954 * @acqe_link: pointer to the async link completion queue entry.
2955 *
2956 * This routine is to parse the SLI4 link-attention link speed and translate
2957 * it into the base driver's link-attention link speed coding.
2958 *
2959 * Return: Link-attention link speed in terms of base driver's coding.
2960 **/
2961static uint8_t
2962lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2963 struct lpfc_acqe_link *acqe_link)
2964{
2965 uint8_t link_speed;
2966
2967 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2968 case LPFC_ASYNC_LINK_SPEED_ZERO:
2969 link_speed = LA_UNKNW_LINK;
2970 break;
2971 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2972 link_speed = LA_UNKNW_LINK;
2973 break;
2974 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2975 link_speed = LA_UNKNW_LINK;
2976 break;
2977 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2978 link_speed = LA_1GHZ_LINK;
2979 break;
2980 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2981 link_speed = LA_10GHZ_LINK;
2982 break;
2983 default:
2984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2985 "0483 Invalid link-attention link speed: x%x\n",
2986 bf_get(lpfc_acqe_link_speed, acqe_link));
2987 link_speed = LA_UNKNW_LINK;
2988 break;
2989 }
2990 return link_speed;
2991}
2992
2993/**
2994 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2995 * @phba: pointer to lpfc hba data structure.
2996 * @acqe_link: pointer to the async link completion queue entry.
2997 *
2998 * This routine is to handle the SLI4 asynchronous link event.
2999 **/
3000static void
3001lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3002 struct lpfc_acqe_link *acqe_link)
3003{
3004 struct lpfc_dmabuf *mp;
3005 LPFC_MBOXQ_t *pmb;
3006 MAILBOX_t *mb;
3007 READ_LA_VAR *la;
3008 uint8_t att_type;
3009
3010 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3011 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3012 return;
James Smart32b97932009-07-19 10:01:21 -04003013 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003014 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3015 if (!pmb) {
3016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3017 "0395 The mboxq allocation failed\n");
3018 return;
3019 }
3020 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3021 if (!mp) {
3022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3023 "0396 The lpfc_dmabuf allocation failed\n");
3024 goto out_free_pmb;
3025 }
3026 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3027 if (!mp->virt) {
3028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3029 "0397 The mbuf allocation failed\n");
3030 goto out_free_dmabuf;
3031 }
3032
3033 /* Cleanup any outstanding ELS commands */
3034 lpfc_els_flush_all_cmd(phba);
3035
3036 /* Block ELS IOCBs until we have done process link event */
3037 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3038
3039 /* Update link event statistics */
3040 phba->sli.slistat.link_event++;
3041
3042 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3043 lpfc_read_la(phba, pmb, mp);
3044 pmb->vport = phba->pport;
3045
3046 /* Parse and translate status field */
3047 mb = &pmb->u.mb;
3048 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3049
3050 /* Parse and translate link attention fields */
3051 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3052 la->eventTag = acqe_link->event_tag;
3053 la->attType = att_type;
3054 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3055
3056 /* Fake the the following irrelvant fields */
3057 la->topology = TOPOLOGY_PT_PT;
3058 la->granted_AL_PA = 0;
3059 la->il = 0;
3060 la->pb = 0;
3061 la->fa = 0;
3062 la->mm = 0;
3063
3064 /* Keep the link status for extra SLI4 state machine reference */
3065 phba->sli4_hba.link_state.speed =
3066 bf_get(lpfc_acqe_link_speed, acqe_link);
3067 phba->sli4_hba.link_state.duplex =
3068 bf_get(lpfc_acqe_link_duplex, acqe_link);
3069 phba->sli4_hba.link_state.status =
3070 bf_get(lpfc_acqe_link_status, acqe_link);
3071 phba->sli4_hba.link_state.physical =
3072 bf_get(lpfc_acqe_link_physical, acqe_link);
3073 phba->sli4_hba.link_state.fault =
3074 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003075 phba->sli4_hba.link_state.logical_speed =
3076 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003077
3078 /* Invoke the lpfc_handle_latt mailbox command callback function */
3079 lpfc_mbx_cmpl_read_la(phba, pmb);
3080
3081 return;
3082
3083out_free_dmabuf:
3084 kfree(mp);
3085out_free_pmb:
3086 mempool_free(pmb, phba->mbox_mem_pool);
3087}
3088
3089/**
3090 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3091 * @phba: pointer to lpfc hba data structure.
3092 * @acqe_link: pointer to the async fcoe completion queue entry.
3093 *
3094 * This routine is to handle the SLI4 asynchronous fcoe event.
3095 **/
3096static void
3097lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3098 struct lpfc_acqe_fcoe *acqe_fcoe)
3099{
3100 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3101 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003102 struct lpfc_vport *vport;
3103 struct lpfc_nodelist *ndlp;
3104 struct Scsi_Host *shost;
James Smart97957242009-12-21 17:03:15 -05003105 uint32_t link_state;
James Smart695a8142010-01-26 23:08:03 -05003106 int active_vlink_present;
3107 struct lpfc_vport **vports;
3108 int i;
James Smartda0436e2009-05-22 14:51:39 -04003109
James Smart4d9ab992009-10-02 15:16:39 -04003110 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003111 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003112 switch (event_type) {
3113 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
James Smartecfd03c2010-02-12 14:41:27 -05003114 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
James Smartda0436e2009-05-22 14:51:39 -04003115 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smarte4e74272009-07-19 10:01:38 -04003116 "2546 New FCF found index 0x%x tag 0x%x\n",
James Smart6669f9b2009-10-02 15:16:45 -04003117 acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003118 acqe_fcoe->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003119 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003120 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3121 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3122 /*
3123 * If the current FCF is in discovered state or
3124 * FCF discovery is in progress, do nothing.
3125 */
3126 spin_unlock_irq(&phba->hbalock);
3127 break;
3128 }
3129 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3130 /*
3131 * If fast FCF failover rescan event is pending,
3132 * do nothing.
3133 */
James Smartda0436e2009-05-22 14:51:39 -04003134 spin_unlock_irq(&phba->hbalock);
3135 break;
3136 }
3137 spin_unlock_irq(&phba->hbalock);
3138
3139 /* Read the FCF table and re-discover SAN. */
James Smart6a9c52c2009-10-02 15:16:51 -04003140 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003141 if (rc)
3142 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003143 "2547 Read FCF record failed 0x%x\n",
3144 rc);
James Smartda0436e2009-05-22 14:51:39 -04003145 break;
3146
3147 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3148 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003149 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003150 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3151 acqe_fcoe->event_tag);
3152 break;
3153
3154 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3155 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003156 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003157 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003158 acqe_fcoe->event_tag);
3159 /* If the event is not for currently used fcf do nothing */
James Smartecfd03c2010-02-12 14:41:27 -05003160 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003161 break;
3162 /*
3163 * Currently, driver support only one FCF - so treat this as
James Smart97957242009-12-21 17:03:15 -05003164 * a link down, but save the link state because we don't want
3165 * it to be changed to Link Down unless it is already down.
James Smartda0436e2009-05-22 14:51:39 -04003166 */
James Smart97957242009-12-21 17:03:15 -05003167 link_state = phba->link_state;
James Smartda0436e2009-05-22 14:51:39 -04003168 lpfc_linkdown(phba);
James Smart97957242009-12-21 17:03:15 -05003169 phba->link_state = link_state;
James Smartda0436e2009-05-22 14:51:39 -04003170 /* Unregister FCF if no devices connected to it */
3171 lpfc_unregister_unused_fcf(phba);
3172 break;
James Smart6669f9b2009-10-02 15:16:45 -04003173 case LPFC_FCOE_EVENT_TYPE_CVL:
3174 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3175 "2718 Clear Virtual Link Received for VPI 0x%x"
3176 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3177 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003178 acqe_fcoe->index - phba->vpi_base);
James Smart6669f9b2009-10-02 15:16:45 -04003179 if (!vport)
3180 break;
3181 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3182 if (!ndlp)
3183 break;
3184 shost = lpfc_shost_from_vport(vport);
James Smart695a8142010-01-26 23:08:03 -05003185 if (phba->pport->port_state <= LPFC_FLOGI)
3186 break;
3187 /* If virtual link is not yet instantiated ignore CVL */
3188 if (vport->port_state <= LPFC_FDISC)
3189 break;
3190
James Smart6669f9b2009-10-02 15:16:45 -04003191 lpfc_linkdown_port(vport);
James Smart695a8142010-01-26 23:08:03 -05003192 lpfc_cleanup_pending_mbox(vport);
3193 spin_lock_irq(shost->host_lock);
3194 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3195 spin_unlock_irq(shost->host_lock);
3196 active_vlink_present = 0;
3197
3198 vports = lpfc_create_vport_work_array(phba);
3199 if (vports) {
3200 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3201 i++) {
3202 if ((!(vports[i]->fc_flag &
3203 FC_VPORT_CVL_RCVD)) &&
3204 (vports[i]->port_state > LPFC_FDISC)) {
3205 active_vlink_present = 1;
3206 break;
3207 }
3208 }
3209 lpfc_destroy_vport_work_array(phba, vports);
3210 }
3211
3212 if (active_vlink_present) {
3213 /*
3214 * If there are other active VLinks present,
3215 * re-instantiate the Vlink using FDISC.
3216 */
James Smart6669f9b2009-10-02 15:16:45 -04003217 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3218 spin_lock_irq(shost->host_lock);
3219 ndlp->nlp_flag |= NLP_DELAY_TMO;
3220 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003221 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3222 vport->port_state = LPFC_FDISC;
3223 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003224 /*
3225 * Otherwise, we request port to rediscover
3226 * the entire FCF table for a fast recovery
3227 * from possible case that the current FCF
3228 * is no longer valid.
3229 */
3230 rc = lpfc_sli4_redisc_fcf_table(phba);
3231 if (rc)
3232 /*
3233 * Last resort will be re-try on the
3234 * the current registered FCF entry.
3235 */
3236 lpfc_retry_pport_discovery(phba);
James Smart6669f9b2009-10-02 15:16:45 -04003237 }
3238 break;
James Smartda0436e2009-05-22 14:51:39 -04003239 default:
3240 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3241 "0288 Unknown FCoE event type 0x%x event tag "
3242 "0x%x\n", event_type, acqe_fcoe->event_tag);
3243 break;
3244 }
3245}
3246
3247/**
3248 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3249 * @phba: pointer to lpfc hba data structure.
3250 * @acqe_link: pointer to the async dcbx completion queue entry.
3251 *
3252 * This routine is to handle the SLI4 asynchronous dcbx event.
3253 **/
3254static void
3255lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3256 struct lpfc_acqe_dcbx *acqe_dcbx)
3257{
James Smart4d9ab992009-10-02 15:16:39 -04003258 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3260 "0290 The SLI4 DCBX asynchronous event is not "
3261 "handled yet\n");
3262}
3263
3264/**
3265 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3266 * @phba: pointer to lpfc hba data structure.
3267 *
3268 * This routine is invoked by the worker thread to process all the pending
3269 * SLI4 asynchronous events.
3270 **/
3271void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3272{
3273 struct lpfc_cq_event *cq_event;
3274
3275 /* First, declare the async event has been handled */
3276 spin_lock_irq(&phba->hbalock);
3277 phba->hba_flag &= ~ASYNC_EVENT;
3278 spin_unlock_irq(&phba->hbalock);
3279 /* Now, handle all the async events */
3280 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3281 /* Get the first event from the head of the event queue */
3282 spin_lock_irq(&phba->hbalock);
3283 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3284 cq_event, struct lpfc_cq_event, list);
3285 spin_unlock_irq(&phba->hbalock);
3286 /* Process the asynchronous event */
3287 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3288 case LPFC_TRAILER_CODE_LINK:
3289 lpfc_sli4_async_link_evt(phba,
3290 &cq_event->cqe.acqe_link);
3291 break;
3292 case LPFC_TRAILER_CODE_FCOE:
3293 lpfc_sli4_async_fcoe_evt(phba,
3294 &cq_event->cqe.acqe_fcoe);
3295 break;
3296 case LPFC_TRAILER_CODE_DCBX:
3297 lpfc_sli4_async_dcbx_evt(phba,
3298 &cq_event->cqe.acqe_dcbx);
3299 break;
3300 default:
3301 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3302 "1804 Invalid asynchrous event code: "
3303 "x%x\n", bf_get(lpfc_trailer_code,
3304 &cq_event->cqe.mcqe_cmpl));
3305 break;
3306 }
3307 /* Free the completion event processed to the free pool */
3308 lpfc_sli4_cq_event_release(phba, cq_event);
3309 }
3310}
3311
3312/**
James Smartecfd03c2010-02-12 14:41:27 -05003313 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3314 * @phba: pointer to lpfc hba data structure.
3315 *
3316 * This routine is invoked by the worker thread to process FCF table
3317 * rediscovery pending completion event.
3318 **/
3319void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3320{
3321 int rc;
3322
3323 spin_lock_irq(&phba->hbalock);
3324 /* Clear FCF rediscovery timeout event */
3325 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3326 /* Clear driver fast failover FCF record flag */
3327 phba->fcf.failover_rec.flag = 0;
3328 /* Set state for FCF fast failover */
3329 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3330 spin_unlock_irq(&phba->hbalock);
3331
3332 /* Scan FCF table from the first entry to re-discover SAN */
3333 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
3334 if (rc)
3335 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3336 "2747 Post FCF rediscovery read FCF record "
3337 "failed 0x%x\n", rc);
3338}
3339
3340/**
James Smartda0436e2009-05-22 14:51:39 -04003341 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3342 * @phba: pointer to lpfc hba data structure.
3343 * @dev_grp: The HBA PCI-Device group number.
3344 *
3345 * This routine is invoked to set up the per HBA PCI-Device group function
3346 * API jump table entries.
3347 *
3348 * Return: 0 if success, otherwise -ENODEV
3349 **/
3350int
3351lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003352{
3353 int rc;
3354
James Smartda0436e2009-05-22 14:51:39 -04003355 /* Set up lpfc PCI-device group */
3356 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003357
James Smartda0436e2009-05-22 14:51:39 -04003358 /* The LPFC_PCI_DEV_OC uses SLI4 */
3359 if (dev_grp == LPFC_PCI_DEV_OC)
3360 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003361
James Smartda0436e2009-05-22 14:51:39 -04003362 /* Set up device INIT API function jump table */
3363 rc = lpfc_init_api_table_setup(phba, dev_grp);
3364 if (rc)
3365 return -ENODEV;
3366 /* Set up SCSI API function jump table */
3367 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3368 if (rc)
3369 return -ENODEV;
3370 /* Set up SLI API function jump table */
3371 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3372 if (rc)
3373 return -ENODEV;
3374 /* Set up MBOX API function jump table */
3375 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3376 if (rc)
3377 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003378
James Smartda0436e2009-05-22 14:51:39 -04003379 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003380}
3381
3382/**
James Smart3621a712009-04-06 18:47:14 -04003383 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003384 * @phba: pointer to lpfc hba data structure.
3385 * @intr_mode: active interrupt mode adopted.
3386 *
3387 * This routine it invoked to log the currently used active interrupt mode
3388 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003389 **/
3390static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003391{
3392 switch (intr_mode) {
3393 case 0:
3394 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3395 "0470 Enable INTx interrupt mode.\n");
3396 break;
3397 case 1:
3398 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3399 "0481 Enabled MSI interrupt mode.\n");
3400 break;
3401 case 2:
3402 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3403 "0480 Enabled MSI-X interrupt mode.\n");
3404 break;
3405 default:
3406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3407 "0482 Illegal interrupt mode.\n");
3408 break;
3409 }
3410 return;
3411}
3412
James Smart5b75da22008-12-04 22:39:35 -05003413/**
James Smart3772a992009-05-22 14:50:54 -04003414 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003415 * @phba: pointer to lpfc hba data structure.
3416 *
James Smart3772a992009-05-22 14:50:54 -04003417 * This routine is invoked to enable the PCI device that is common to all
3418 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003419 *
3420 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003421 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003422 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003423 **/
James Smart3772a992009-05-22 14:50:54 -04003424static int
3425lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003426{
James Smart3772a992009-05-22 14:50:54 -04003427 struct pci_dev *pdev;
3428 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003429
James Smart3772a992009-05-22 14:50:54 -04003430 /* Obtain PCI device reference */
3431 if (!phba->pcidev)
3432 goto out_error;
3433 else
3434 pdev = phba->pcidev;
3435 /* Select PCI BARs */
3436 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3437 /* Enable PCI device */
3438 if (pci_enable_device_mem(pdev))
3439 goto out_error;
3440 /* Request PCI resource for the device */
3441 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3442 goto out_disable_device;
3443 /* Set up device as PCI master and save state for EEH */
3444 pci_set_master(pdev);
3445 pci_try_set_mwi(pdev);
3446 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003447
James Smart3772a992009-05-22 14:50:54 -04003448 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003449
James Smart3772a992009-05-22 14:50:54 -04003450out_disable_device:
3451 pci_disable_device(pdev);
3452out_error:
3453 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003454}
3455
3456/**
James Smart3772a992009-05-22 14:50:54 -04003457 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003458 * @phba: pointer to lpfc hba data structure.
3459 *
James Smart3772a992009-05-22 14:50:54 -04003460 * This routine is invoked to disable the PCI device that is common to all
3461 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003462 **/
3463static void
James Smart3772a992009-05-22 14:50:54 -04003464lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003465{
James Smart3772a992009-05-22 14:50:54 -04003466 struct pci_dev *pdev;
3467 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003468
James Smart3772a992009-05-22 14:50:54 -04003469 /* Obtain PCI device reference */
3470 if (!phba->pcidev)
3471 return;
3472 else
3473 pdev = phba->pcidev;
3474 /* Select PCI BARs */
3475 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3476 /* Release PCI resource and disable PCI device */
3477 pci_release_selected_regions(pdev, bars);
3478 pci_disable_device(pdev);
3479 /* Null out PCI private reference to driver */
3480 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003481
3482 return;
3483}
3484
3485/**
James Smart3772a992009-05-22 14:50:54 -04003486 * lpfc_reset_hba - Reset a hba
3487 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003488 *
James Smart3772a992009-05-22 14:50:54 -04003489 * This routine is invoked to reset a hba device. It brings the HBA
3490 * offline, performs a board restart, and then brings the board back
3491 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3492 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003493 **/
James Smart3772a992009-05-22 14:50:54 -04003494void
3495lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003496{
James Smart3772a992009-05-22 14:50:54 -04003497 /* If resets are disabled then set error state and return. */
3498 if (!phba->cfg_enable_hba_reset) {
3499 phba->link_state = LPFC_HBA_ERROR;
3500 return;
3501 }
3502 lpfc_offline_prep(phba);
3503 lpfc_offline(phba);
3504 lpfc_sli_brdrestart(phba);
3505 lpfc_online(phba);
3506 lpfc_unblock_mgmt_io(phba);
3507}
dea31012005-04-17 16:05:31 -05003508
James Smart3772a992009-05-22 14:50:54 -04003509/**
3510 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3511 * @phba: pointer to lpfc hba data structure.
3512 *
3513 * This routine is invoked to set up the driver internal resources specific to
3514 * support the SLI-3 HBA device it attached to.
3515 *
3516 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003517 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003518 * other values - error
3519 **/
3520static int
3521lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3522{
3523 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003524
James Smart3772a992009-05-22 14:50:54 -04003525 /*
3526 * Initialize timers used by driver
3527 */
dea31012005-04-17 16:05:31 -05003528
James Smart3772a992009-05-22 14:50:54 -04003529 /* Heartbeat timer */
3530 init_timer(&phba->hb_tmofunc);
3531 phba->hb_tmofunc.function = lpfc_hb_timeout;
3532 phba->hb_tmofunc.data = (unsigned long)phba;
3533
3534 psli = &phba->sli;
3535 /* MBOX heartbeat timer */
3536 init_timer(&psli->mbox_tmo);
3537 psli->mbox_tmo.function = lpfc_mbox_timeout;
3538 psli->mbox_tmo.data = (unsigned long) phba;
3539 /* FCP polling mode timer */
3540 init_timer(&phba->fcp_poll_timer);
3541 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3542 phba->fcp_poll_timer.data = (unsigned long) phba;
3543 /* Fabric block timer */
3544 init_timer(&phba->fabric_block_timer);
3545 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3546 phba->fabric_block_timer.data = (unsigned long) phba;
3547 /* EA polling mode timer */
3548 init_timer(&phba->eratt_poll);
3549 phba->eratt_poll.function = lpfc_poll_eratt;
3550 phba->eratt_poll.data = (unsigned long) phba;
3551
3552 /* Host attention work mask setup */
3553 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3554 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3555
3556 /* Get all the module params for configuring this host */
3557 lpfc_get_cfgparam(phba);
3558 /*
3559 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3560 * used to create the sg_dma_buf_pool must be dynamically calculated.
3561 * 2 segments are added since the IOCB needs a command and response bde.
3562 */
3563 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3564 sizeof(struct fcp_rsp) +
3565 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3566
3567 if (phba->cfg_enable_bg) {
3568 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3569 phba->cfg_sg_dma_buf_size +=
3570 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3571 }
3572
3573 /* Also reinitialize the host templates with new values. */
3574 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3575 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3576
3577 phba->max_vpi = LPFC_MAX_VPI;
3578 /* This will be set to correct value after config_port mbox */
3579 phba->max_vports = 0;
3580
3581 /*
3582 * Initialize the SLI Layer to run with lpfc HBAs.
3583 */
3584 lpfc_sli_setup(phba);
3585 lpfc_sli_queue_setup(phba);
3586
3587 /* Allocate device driver memory */
3588 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3589 return -ENOMEM;
3590
3591 return 0;
3592}
3593
3594/**
3595 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3596 * @phba: pointer to lpfc hba data structure.
3597 *
3598 * This routine is invoked to unset the driver internal resources set up
3599 * specific for supporting the SLI-3 HBA device it attached to.
3600 **/
3601static void
3602lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3603{
3604 /* Free device driver memory allocated */
3605 lpfc_mem_free_all(phba);
3606
3607 return;
3608}
3609
3610/**
James Smartda0436e2009-05-22 14:51:39 -04003611 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3612 * @phba: pointer to lpfc hba data structure.
3613 *
3614 * This routine is invoked to set up the driver internal resources specific to
3615 * support the SLI-4 HBA device it attached to.
3616 *
3617 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003618 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04003619 * other values - error
3620 **/
3621static int
3622lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3623{
3624 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05003625 LPFC_MBOXQ_t *mboxq;
3626 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3627 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3628 struct lpfc_mqe *mqe;
James Smartda0436e2009-05-22 14:51:39 -04003629
3630 /* Before proceed, wait for POST done and device ready */
3631 rc = lpfc_sli4_post_status_check(phba);
3632 if (rc)
3633 return -ENODEV;
3634
3635 /*
3636 * Initialize timers used by driver
3637 */
3638
3639 /* Heartbeat timer */
3640 init_timer(&phba->hb_tmofunc);
3641 phba->hb_tmofunc.function = lpfc_hb_timeout;
3642 phba->hb_tmofunc.data = (unsigned long)phba;
3643
3644 psli = &phba->sli;
3645 /* MBOX heartbeat timer */
3646 init_timer(&psli->mbox_tmo);
3647 psli->mbox_tmo.function = lpfc_mbox_timeout;
3648 psli->mbox_tmo.data = (unsigned long) phba;
3649 /* Fabric block timer */
3650 init_timer(&phba->fabric_block_timer);
3651 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3652 phba->fabric_block_timer.data = (unsigned long) phba;
3653 /* EA polling mode timer */
3654 init_timer(&phba->eratt_poll);
3655 phba->eratt_poll.function = lpfc_poll_eratt;
3656 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05003657 /* FCF rediscover timer */
3658 init_timer(&phba->fcf.redisc_wait);
3659 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3660 phba->fcf.redisc_wait.data = (unsigned long)phba;
3661
James Smartda0436e2009-05-22 14:51:39 -04003662 /*
3663 * We need to do a READ_CONFIG mailbox command here before
3664 * calling lpfc_get_cfgparam. For VFs this will report the
3665 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3666 * All of the resources allocated
3667 * for this Port are tied to these values.
3668 */
3669 /* Get all the module params for configuring this host */
3670 lpfc_get_cfgparam(phba);
3671 phba->max_vpi = LPFC_MAX_VPI;
3672 /* This will be set to correct value after the read_config mbox */
3673 phba->max_vports = 0;
3674
3675 /* Program the default value of vlan_id and fc_map */
3676 phba->valid_vlan = 0;
3677 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3678 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3679 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3680
3681 /*
3682 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3683 * used to create the sg_dma_buf_pool must be dynamically calculated.
3684 * 2 segments are added since the IOCB needs a command and response bde.
3685 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05003686 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04003687 */
James Smart28baac72010-02-12 14:42:03 -05003688 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
3689 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
3690 /* Feature Level 1 hardware is limited to 2 pages */
3691 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
3692 LPFC_SLI_INTF_FEATURELEVEL1_1))
3693 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04003694 else
James Smart28baac72010-02-12 14:42:03 -05003695 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
3696 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
3697 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
3698 dma_buf_size = dma_buf_size << 1)
3699 ;
3700 if (dma_buf_size == max_buf_size)
3701 phba->cfg_sg_seg_cnt = (dma_buf_size -
3702 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
3703 (2 * sizeof(struct sli4_sge))) /
3704 sizeof(struct sli4_sge);
3705 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04003706
3707 /* Initialize buffer queue management fields */
3708 hbq_count = lpfc_sli_hbq_count();
3709 for (i = 0; i < hbq_count; ++i)
3710 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3711 INIT_LIST_HEAD(&phba->rb_pend_list);
3712 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3713 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3714
3715 /*
3716 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3717 */
3718 /* Initialize the Abort scsi buffer list used by driver */
3719 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3720 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3721 /* This abort list used by worker thread */
3722 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3723
3724 /*
3725 * Initialize dirver internal slow-path work queues
3726 */
3727
3728 /* Driver internel slow-path CQ Event pool */
3729 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3730 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04003731 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04003732 /* Asynchronous event CQ Event work queue list */
3733 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3734 /* Fast-path XRI aborted CQ Event work queue list */
3735 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3736 /* Slow-path XRI aborted CQ Event work queue list */
3737 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3738 /* Receive queue CQ Event work queue list */
3739 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3740
3741 /* Initialize the driver internal SLI layer lists. */
3742 lpfc_sli_setup(phba);
3743 lpfc_sli_queue_setup(phba);
3744
3745 /* Allocate device driver memory */
3746 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3747 if (rc)
3748 return -ENOMEM;
3749
3750 /* Create the bootstrap mailbox command */
3751 rc = lpfc_create_bootstrap_mbox(phba);
3752 if (unlikely(rc))
3753 goto out_free_mem;
3754
3755 /* Set up the host's endian order with the device. */
3756 rc = lpfc_setup_endian_order(phba);
3757 if (unlikely(rc))
3758 goto out_free_bsmbx;
3759
James Smart6669f9b2009-10-02 15:16:45 -04003760 rc = lpfc_sli4_fw_cfg_check(phba);
3761 if (unlikely(rc))
3762 goto out_free_bsmbx;
3763
James Smartda0436e2009-05-22 14:51:39 -04003764 /* Set up the hba's configuration parameters. */
3765 rc = lpfc_sli4_read_config(phba);
3766 if (unlikely(rc))
3767 goto out_free_bsmbx;
3768
3769 /* Perform a function reset */
3770 rc = lpfc_pci_function_reset(phba);
3771 if (unlikely(rc))
3772 goto out_free_bsmbx;
3773
3774 /* Create all the SLI4 queues */
3775 rc = lpfc_sli4_queue_create(phba);
3776 if (rc)
3777 goto out_free_bsmbx;
3778
3779 /* Create driver internal CQE event pool */
3780 rc = lpfc_sli4_cq_event_pool_create(phba);
3781 if (rc)
3782 goto out_destroy_queue;
3783
3784 /* Initialize and populate the iocb list per host */
3785 rc = lpfc_init_sgl_list(phba);
3786 if (rc) {
3787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3788 "1400 Failed to initialize sgl list.\n");
3789 goto out_destroy_cq_event_pool;
3790 }
3791 rc = lpfc_init_active_sgl_array(phba);
3792 if (rc) {
3793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3794 "1430 Failed to initialize sgl list.\n");
3795 goto out_free_sgl_list;
3796 }
3797
3798 rc = lpfc_sli4_init_rpi_hdrs(phba);
3799 if (rc) {
3800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3801 "1432 Failed to initialize rpi headers.\n");
3802 goto out_free_active_sgl;
3803 }
3804
3805 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3806 phba->cfg_fcp_eq_count), GFP_KERNEL);
3807 if (!phba->sli4_hba.fcp_eq_hdl) {
3808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3809 "2572 Failed allocate memory for fast-path "
3810 "per-EQ handle array\n");
3811 goto out_remove_rpi_hdrs;
3812 }
3813
3814 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3815 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3816 if (!phba->sli4_hba.msix_entries) {
3817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3818 "2573 Failed allocate memory for msi-x "
3819 "interrupt vector entries\n");
3820 goto out_free_fcp_eq_hdl;
3821 }
3822
James Smart28baac72010-02-12 14:42:03 -05003823 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3824 GFP_KERNEL);
3825 if (!mboxq) {
3826 rc = -ENOMEM;
3827 goto out_free_fcp_eq_hdl;
3828 }
3829
3830 /* Get the Supported Pages. It is always available. */
3831 lpfc_supported_pages(mboxq);
3832 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3833 if (unlikely(rc)) {
3834 rc = -EIO;
3835 mempool_free(mboxq, phba->mbox_mem_pool);
3836 goto out_free_fcp_eq_hdl;
3837 }
3838
3839 mqe = &mboxq->u.mqe;
3840 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
3841 LPFC_MAX_SUPPORTED_PAGES);
3842 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
3843 switch (pn_page[i]) {
3844 case LPFC_SLI4_PARAMETERS:
3845 phba->sli4_hba.pc_sli4_params.supported = 1;
3846 break;
3847 default:
3848 break;
3849 }
3850 }
3851
3852 /* Read the port's SLI4 Parameters capabilities if supported. */
3853 if (phba->sli4_hba.pc_sli4_params.supported)
3854 rc = lpfc_pc_sli4_params_get(phba, mboxq);
3855 mempool_free(mboxq, phba->mbox_mem_pool);
3856 if (rc) {
3857 rc = -EIO;
3858 goto out_free_fcp_eq_hdl;
3859 }
James Smartda0436e2009-05-22 14:51:39 -04003860 return rc;
3861
3862out_free_fcp_eq_hdl:
3863 kfree(phba->sli4_hba.fcp_eq_hdl);
3864out_remove_rpi_hdrs:
3865 lpfc_sli4_remove_rpi_hdrs(phba);
3866out_free_active_sgl:
3867 lpfc_free_active_sgl(phba);
3868out_free_sgl_list:
3869 lpfc_free_sgl_list(phba);
3870out_destroy_cq_event_pool:
3871 lpfc_sli4_cq_event_pool_destroy(phba);
3872out_destroy_queue:
3873 lpfc_sli4_queue_destroy(phba);
3874out_free_bsmbx:
3875 lpfc_destroy_bootstrap_mbox(phba);
3876out_free_mem:
3877 lpfc_mem_free(phba);
3878 return rc;
3879}
3880
3881/**
3882 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3883 * @phba: pointer to lpfc hba data structure.
3884 *
3885 * This routine is invoked to unset the driver internal resources set up
3886 * specific for supporting the SLI-4 HBA device it attached to.
3887 **/
3888static void
3889lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3890{
3891 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3892
3893 /* unregister default FCFI from the HBA */
3894 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3895
3896 /* Free the default FCR table */
3897 lpfc_sli_remove_dflt_fcf(phba);
3898
3899 /* Free memory allocated for msi-x interrupt vector entries */
3900 kfree(phba->sli4_hba.msix_entries);
3901
3902 /* Free memory allocated for fast-path work queue handles */
3903 kfree(phba->sli4_hba.fcp_eq_hdl);
3904
3905 /* Free the allocated rpi headers. */
3906 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04003907 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04003908
3909 /* Free the ELS sgl list */
3910 lpfc_free_active_sgl(phba);
3911 lpfc_free_sgl_list(phba);
3912
3913 /* Free the SCSI sgl management array */
3914 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3915
3916 /* Free the SLI4 queues */
3917 lpfc_sli4_queue_destroy(phba);
3918
3919 /* Free the completion queue EQ event pool */
3920 lpfc_sli4_cq_event_release_all(phba);
3921 lpfc_sli4_cq_event_pool_destroy(phba);
3922
3923 /* Reset SLI4 HBA FCoE function */
3924 lpfc_pci_function_reset(phba);
3925
3926 /* Free the bsmbx region. */
3927 lpfc_destroy_bootstrap_mbox(phba);
3928
3929 /* Free the SLI Layer memory with SLI4 HBAs */
3930 lpfc_mem_free_all(phba);
3931
3932 /* Free the current connect table */
3933 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04003934 &phba->fcf_conn_rec_list, list) {
3935 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04003936 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04003937 }
James Smartda0436e2009-05-22 14:51:39 -04003938
3939 return;
3940}
3941
3942/**
James Smart3772a992009-05-22 14:50:54 -04003943 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3944 * @phba: The hba struct for which this call is being executed.
3945 * @dev_grp: The HBA PCI-Device group number.
3946 *
3947 * This routine sets up the device INIT interface API function jump table
3948 * in @phba struct.
3949 *
3950 * Returns: 0 - success, -ENODEV - failure.
3951 **/
3952int
3953lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3954{
3955 switch (dev_grp) {
3956 case LPFC_PCI_DEV_LP:
3957 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3958 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3959 phba->lpfc_stop_port = lpfc_stop_port_s3;
3960 break;
James Smartda0436e2009-05-22 14:51:39 -04003961 case LPFC_PCI_DEV_OC:
3962 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3963 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3964 phba->lpfc_stop_port = lpfc_stop_port_s4;
3965 break;
James Smart3772a992009-05-22 14:50:54 -04003966 default:
3967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3968 "1431 Invalid HBA PCI-device group: 0x%x\n",
3969 dev_grp);
3970 return -ENODEV;
3971 break;
3972 }
3973 return 0;
3974}
3975
3976/**
3977 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3978 * @phba: pointer to lpfc hba data structure.
3979 *
3980 * This routine is invoked to set up the driver internal resources before the
3981 * device specific resource setup to support the HBA device it attached to.
3982 *
3983 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003984 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003985 * other values - error
3986 **/
3987static int
3988lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3989{
3990 /*
3991 * Driver resources common to all SLI revisions
3992 */
James Smartea2151b2008-09-07 11:52:10 -04003993 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003994 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003995
James Smarte47c9092008-02-08 18:49:26 -05003996 /* Initialize ndlp management spinlock */
3997 spin_lock_init(&phba->ndlp_lock);
3998
James Smart2e0fef82007-06-17 19:56:36 -05003999 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004000 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004001 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004002
James Smart3772a992009-05-22 14:50:54 -04004003 /* Initialize the wait queue head for the kernel thread */
4004 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004005
James Smart3772a992009-05-22 14:50:54 -04004006 /* Initialize the scsi buffer list used by driver for scsi IO */
4007 spin_lock_init(&phba->scsi_buf_list_lock);
4008 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004009
James Smart3772a992009-05-22 14:50:54 -04004010 /* Initialize the fabric iocb list */
4011 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004012
James Smart3772a992009-05-22 14:50:54 -04004013 /* Initialize list to save ELS buffers */
4014 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004015
James Smart3772a992009-05-22 14:50:54 -04004016 /* Initialize FCF connection rec list */
4017 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004018
James Smart3772a992009-05-22 14:50:54 -04004019 return 0;
4020}
dea31012005-04-17 16:05:31 -05004021
James Smart3772a992009-05-22 14:50:54 -04004022/**
4023 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4024 * @phba: pointer to lpfc hba data structure.
4025 *
4026 * This routine is invoked to set up the driver internal resources after the
4027 * device specific resource setup to support the HBA device it attached to.
4028 *
4029 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004030 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004031 * other values - error
4032 **/
4033static int
4034lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4035{
4036 int error;
4037
4038 /* Startup the kernel thread for this host adapter. */
4039 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4040 "lpfc_worker_%d", phba->brd_no);
4041 if (IS_ERR(phba->worker_thread)) {
4042 error = PTR_ERR(phba->worker_thread);
4043 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004044 }
4045
James Smart3772a992009-05-22 14:50:54 -04004046 return 0;
4047}
4048
4049/**
4050 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4051 * @phba: pointer to lpfc hba data structure.
4052 *
4053 * This routine is invoked to unset the driver internal resources set up after
4054 * the device specific resource setup for supporting the HBA device it
4055 * attached to.
4056 **/
4057static void
4058lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4059{
4060 /* Stop kernel worker thread */
4061 kthread_stop(phba->worker_thread);
4062}
4063
4064/**
4065 * lpfc_free_iocb_list - Free iocb list.
4066 * @phba: pointer to lpfc hba data structure.
4067 *
4068 * This routine is invoked to free the driver's IOCB list and memory.
4069 **/
4070static void
4071lpfc_free_iocb_list(struct lpfc_hba *phba)
4072{
4073 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4074
4075 spin_lock_irq(&phba->hbalock);
4076 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4077 &phba->lpfc_iocb_list, list) {
4078 list_del(&iocbq_entry->list);
4079 kfree(iocbq_entry);
4080 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004081 }
James Smart3772a992009-05-22 14:50:54 -04004082 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004083
James Smart3772a992009-05-22 14:50:54 -04004084 return;
4085}
dea31012005-04-17 16:05:31 -05004086
James Smart3772a992009-05-22 14:50:54 -04004087/**
4088 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4089 * @phba: pointer to lpfc hba data structure.
4090 *
4091 * This routine is invoked to allocate and initizlize the driver's IOCB
4092 * list and set up the IOCB tag array accordingly.
4093 *
4094 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004095 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004096 * other values - error
4097 **/
4098static int
4099lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4100{
4101 struct lpfc_iocbq *iocbq_entry = NULL;
4102 uint16_t iotag;
4103 int i;
dea31012005-04-17 16:05:31 -05004104
4105 /* Initialize and populate the iocb list per host. */
4106 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004107 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004108 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004109 if (iocbq_entry == NULL) {
4110 printk(KERN_ERR "%s: only allocated %d iocbs of "
4111 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004112 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004113 goto out_free_iocbq;
4114 }
4115
James Bottomley604a3e32005-10-29 10:28:33 -05004116 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4117 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004118 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004119 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004120 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004121 goto out_free_iocbq;
4122 }
James Smart3772a992009-05-22 14:50:54 -04004123 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004124
4125 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004126 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4127 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004128 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004129 }
4130
James Smart3772a992009-05-22 14:50:54 -04004131 return 0;
4132
4133out_free_iocbq:
4134 lpfc_free_iocb_list(phba);
4135
4136 return -ENOMEM;
4137}
4138
4139/**
James Smartda0436e2009-05-22 14:51:39 -04004140 * lpfc_free_sgl_list - Free sgl list.
4141 * @phba: pointer to lpfc hba data structure.
4142 *
4143 * This routine is invoked to free the driver's sgl list and memory.
4144 **/
4145static void
4146lpfc_free_sgl_list(struct lpfc_hba *phba)
4147{
4148 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4149 LIST_HEAD(sglq_list);
4150 int rc = 0;
4151
4152 spin_lock_irq(&phba->hbalock);
4153 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4154 spin_unlock_irq(&phba->hbalock);
4155
4156 list_for_each_entry_safe(sglq_entry, sglq_next,
4157 &sglq_list, list) {
4158 list_del(&sglq_entry->list);
4159 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4160 kfree(sglq_entry);
4161 phba->sli4_hba.total_sglq_bufs--;
4162 }
4163 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4164 if (rc) {
4165 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004166 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004167 }
4168 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4169}
4170
4171/**
4172 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4173 * @phba: pointer to lpfc hba data structure.
4174 *
4175 * This routine is invoked to allocate the driver's active sgl memory.
4176 * This array will hold the sglq_entry's for active IOs.
4177 **/
4178static int
4179lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4180{
4181 int size;
4182 size = sizeof(struct lpfc_sglq *);
4183 size *= phba->sli4_hba.max_cfg_param.max_xri;
4184
4185 phba->sli4_hba.lpfc_sglq_active_list =
4186 kzalloc(size, GFP_KERNEL);
4187 if (!phba->sli4_hba.lpfc_sglq_active_list)
4188 return -ENOMEM;
4189 return 0;
4190}
4191
4192/**
4193 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4194 * @phba: pointer to lpfc hba data structure.
4195 *
4196 * This routine is invoked to walk through the array of active sglq entries
4197 * and free all of the resources.
4198 * This is just a place holder for now.
4199 **/
4200static void
4201lpfc_free_active_sgl(struct lpfc_hba *phba)
4202{
4203 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4204}
4205
4206/**
4207 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4208 * @phba: pointer to lpfc hba data structure.
4209 *
4210 * This routine is invoked to allocate and initizlize the driver's sgl
4211 * list and set up the sgl xritag tag array accordingly.
4212 *
4213 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004214 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004215 * other values - error
4216 **/
4217static int
4218lpfc_init_sgl_list(struct lpfc_hba *phba)
4219{
4220 struct lpfc_sglq *sglq_entry = NULL;
4221 int i;
4222 int els_xri_cnt;
4223
4224 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4225 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4226 "2400 lpfc_init_sgl_list els %d.\n",
4227 els_xri_cnt);
4228 /* Initialize and populate the sglq list per host/VF. */
4229 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4230 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4231
4232 /* Sanity check on XRI management */
4233 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4235 "2562 No room left for SCSI XRI allocation: "
4236 "max_xri=%d, els_xri=%d\n",
4237 phba->sli4_hba.max_cfg_param.max_xri,
4238 els_xri_cnt);
4239 return -ENOMEM;
4240 }
4241
4242 /* Allocate memory for the ELS XRI management array */
4243 phba->sli4_hba.lpfc_els_sgl_array =
4244 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4245 GFP_KERNEL);
4246
4247 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4249 "2401 Failed to allocate memory for ELS "
4250 "XRI management array of size %d.\n",
4251 els_xri_cnt);
4252 return -ENOMEM;
4253 }
4254
4255 /* Keep the SCSI XRI into the XRI management array */
4256 phba->sli4_hba.scsi_xri_max =
4257 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4258 phba->sli4_hba.scsi_xri_cnt = 0;
4259
4260 phba->sli4_hba.lpfc_scsi_psb_array =
4261 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4262 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4263
4264 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4266 "2563 Failed to allocate memory for SCSI "
4267 "XRI management array of size %d.\n",
4268 phba->sli4_hba.scsi_xri_max);
4269 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4270 return -ENOMEM;
4271 }
4272
4273 for (i = 0; i < els_xri_cnt; i++) {
4274 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4275 if (sglq_entry == NULL) {
4276 printk(KERN_ERR "%s: only allocated %d sgls of "
4277 "expected %d count. Unloading driver.\n",
4278 __func__, i, els_xri_cnt);
4279 goto out_free_mem;
4280 }
4281
4282 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4283 if (sglq_entry->sli4_xritag == NO_XRI) {
4284 kfree(sglq_entry);
4285 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4286 "Unloading driver.\n", __func__);
4287 goto out_free_mem;
4288 }
4289 sglq_entry->buff_type = GEN_BUFF_TYPE;
4290 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4291 if (sglq_entry->virt == NULL) {
4292 kfree(sglq_entry);
4293 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4294 "Unloading driver.\n", __func__);
4295 goto out_free_mem;
4296 }
4297 sglq_entry->sgl = sglq_entry->virt;
4298 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4299
4300 /* The list order is used by later block SGL registraton */
4301 spin_lock_irq(&phba->hbalock);
4302 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4303 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4304 phba->sli4_hba.total_sglq_bufs++;
4305 spin_unlock_irq(&phba->hbalock);
4306 }
4307 return 0;
4308
4309out_free_mem:
4310 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4311 lpfc_free_sgl_list(phba);
4312 return -ENOMEM;
4313}
4314
4315/**
4316 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4317 * @phba: pointer to lpfc hba data structure.
4318 *
4319 * This routine is invoked to post rpi header templates to the
4320 * HBA consistent with the SLI-4 interface spec. This routine
4321 * posts a PAGE_SIZE memory region to the port to hold up to
4322 * PAGE_SIZE modulo 64 rpi context headers.
4323 * No locks are held here because this is an initialization routine
4324 * called only from probe or lpfc_online when interrupts are not
4325 * enabled and the driver is reinitializing the device.
4326 *
4327 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004328 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004329 * ENOMEM - No availble memory
4330 * EIO - The mailbox failed to complete successfully.
4331 **/
4332int
4333lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4334{
4335 int rc = 0;
4336 int longs;
4337 uint16_t rpi_count;
4338 struct lpfc_rpi_hdr *rpi_hdr;
4339
4340 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4341
4342 /*
4343 * Provision an rpi bitmask range for discovery. The total count
4344 * is the difference between max and base + 1.
4345 */
4346 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4347 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4348
4349 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4350 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4351 GFP_KERNEL);
4352 if (!phba->sli4_hba.rpi_bmask)
4353 return -ENOMEM;
4354
4355 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4356 if (!rpi_hdr) {
4357 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4358 "0391 Error during rpi post operation\n");
4359 lpfc_sli4_remove_rpis(phba);
4360 rc = -ENODEV;
4361 }
4362
4363 return rc;
4364}
4365
4366/**
4367 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4368 * @phba: pointer to lpfc hba data structure.
4369 *
4370 * This routine is invoked to allocate a single 4KB memory region to
4371 * support rpis and stores them in the phba. This single region
4372 * provides support for up to 64 rpis. The region is used globally
4373 * by the device.
4374 *
4375 * Returns:
4376 * A valid rpi hdr on success.
4377 * A NULL pointer on any failure.
4378 **/
4379struct lpfc_rpi_hdr *
4380lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4381{
4382 uint16_t rpi_limit, curr_rpi_range;
4383 struct lpfc_dmabuf *dmabuf;
4384 struct lpfc_rpi_hdr *rpi_hdr;
4385
4386 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4387 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4388
4389 spin_lock_irq(&phba->hbalock);
4390 curr_rpi_range = phba->sli4_hba.next_rpi;
4391 spin_unlock_irq(&phba->hbalock);
4392
4393 /*
4394 * The port has a limited number of rpis. The increment here
4395 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4396 * and to allow the full max_rpi range per port.
4397 */
4398 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4399 return NULL;
4400
4401 /*
4402 * First allocate the protocol header region for the port. The
4403 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4404 */
4405 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4406 if (!dmabuf)
4407 return NULL;
4408
4409 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4410 LPFC_HDR_TEMPLATE_SIZE,
4411 &dmabuf->phys,
4412 GFP_KERNEL);
4413 if (!dmabuf->virt) {
4414 rpi_hdr = NULL;
4415 goto err_free_dmabuf;
4416 }
4417
4418 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4419 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4420 rpi_hdr = NULL;
4421 goto err_free_coherent;
4422 }
4423
4424 /* Save the rpi header data for cleanup later. */
4425 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4426 if (!rpi_hdr)
4427 goto err_free_coherent;
4428
4429 rpi_hdr->dmabuf = dmabuf;
4430 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4431 rpi_hdr->page_count = 1;
4432 spin_lock_irq(&phba->hbalock);
4433 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4434 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4435
4436 /*
4437 * The next_rpi stores the next module-64 rpi value to post
4438 * in any subsequent rpi memory region postings.
4439 */
4440 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4441 spin_unlock_irq(&phba->hbalock);
4442 return rpi_hdr;
4443
4444 err_free_coherent:
4445 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4446 dmabuf->virt, dmabuf->phys);
4447 err_free_dmabuf:
4448 kfree(dmabuf);
4449 return NULL;
4450}
4451
4452/**
4453 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4454 * @phba: pointer to lpfc hba data structure.
4455 *
4456 * This routine is invoked to remove all memory resources allocated
4457 * to support rpis. This routine presumes the caller has released all
4458 * rpis consumed by fabric or port logins and is prepared to have
4459 * the header pages removed.
4460 **/
4461void
4462lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4463{
4464 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4465
4466 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4467 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4468 list_del(&rpi_hdr->list);
4469 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4470 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4471 kfree(rpi_hdr->dmabuf);
4472 kfree(rpi_hdr);
4473 }
4474
4475 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4476 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4477}
4478
4479/**
James Smart3772a992009-05-22 14:50:54 -04004480 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4481 * @pdev: pointer to pci device data structure.
4482 *
4483 * This routine is invoked to allocate the driver hba data structure for an
4484 * HBA device. If the allocation is successful, the phba reference to the
4485 * PCI device data structure is set.
4486 *
4487 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004488 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004489 * NULL - error
4490 **/
4491static struct lpfc_hba *
4492lpfc_hba_alloc(struct pci_dev *pdev)
4493{
4494 struct lpfc_hba *phba;
4495
4496 /* Allocate memory for HBA structure */
4497 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4498 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004499 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004500 return NULL;
4501 }
4502
4503 /* Set reference to PCI device in HBA structure */
4504 phba->pcidev = pdev;
4505
4506 /* Assign an unused board number */
4507 phba->brd_no = lpfc_get_instance();
4508 if (phba->brd_no < 0) {
4509 kfree(phba);
4510 return NULL;
4511 }
4512
James Smart4fede782010-01-26 23:08:55 -05004513 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04004514 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4515
James Smart3772a992009-05-22 14:50:54 -04004516 return phba;
4517}
4518
4519/**
4520 * lpfc_hba_free - Free driver hba data structure with a device.
4521 * @phba: pointer to lpfc hba data structure.
4522 *
4523 * This routine is invoked to free the driver hba data structure with an
4524 * HBA device.
4525 **/
4526static void
4527lpfc_hba_free(struct lpfc_hba *phba)
4528{
4529 /* Release the driver assigned board number */
4530 idr_remove(&lpfc_hba_index, phba->brd_no);
4531
4532 kfree(phba);
4533 return;
4534}
4535
4536/**
4537 * lpfc_create_shost - Create hba physical port with associated scsi host.
4538 * @phba: pointer to lpfc hba data structure.
4539 *
4540 * This routine is invoked to create HBA physical port and associate a SCSI
4541 * host with it.
4542 *
4543 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004544 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004545 * other values - error
4546 **/
4547static int
4548lpfc_create_shost(struct lpfc_hba *phba)
4549{
4550 struct lpfc_vport *vport;
4551 struct Scsi_Host *shost;
4552
4553 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004554 phba->fc_edtov = FF_DEF_EDTOV;
4555 phba->fc_ratov = FF_DEF_RATOV;
4556 phba->fc_altov = FF_DEF_ALTOV;
4557 phba->fc_arbtov = FF_DEF_ARBTOV;
4558
James Smart3de2a652007-08-02 11:09:59 -04004559 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004560 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004561 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004562
4563 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004564 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004565 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004566 /* Put reference to SCSI host to driver's device private data */
4567 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004568
James Smart3772a992009-05-22 14:50:54 -04004569 return 0;
4570}
dea31012005-04-17 16:05:31 -05004571
James Smart3772a992009-05-22 14:50:54 -04004572/**
4573 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4574 * @phba: pointer to lpfc hba data structure.
4575 *
4576 * This routine is invoked to destroy HBA physical port and the associated
4577 * SCSI host.
4578 **/
4579static void
4580lpfc_destroy_shost(struct lpfc_hba *phba)
4581{
4582 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004583
James Smart3772a992009-05-22 14:50:54 -04004584 /* Destroy physical port that associated with the SCSI host */
4585 destroy_port(vport);
4586
4587 return;
4588}
4589
4590/**
4591 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4592 * @phba: pointer to lpfc hba data structure.
4593 * @shost: the shost to be used to detect Block guard settings.
4594 *
4595 * This routine sets up the local Block guard protocol settings for @shost.
4596 * This routine also allocates memory for debugging bg buffers.
4597 **/
4598static void
4599lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4600{
4601 int pagecnt = 10;
4602 if (lpfc_prot_mask && lpfc_prot_guard) {
4603 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4604 "1478 Registering BlockGuard with the "
4605 "SCSI layer\n");
4606 scsi_host_set_prot(shost, lpfc_prot_mask);
4607 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004608 }
James Smart81301a92008-12-04 22:39:46 -05004609 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004610 while (pagecnt) {
4611 spin_lock_init(&_dump_buf_lock);
4612 _dump_buf_data =
4613 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4614 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004615 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4616 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004617 "_dump_buf_data at 0x%p\n",
4618 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004619 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004620 memset(_dump_buf_data, 0,
4621 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004622 break;
James Smart3772a992009-05-22 14:50:54 -04004623 } else
James Smart81301a92008-12-04 22:39:46 -05004624 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004625 }
James Smart81301a92008-12-04 22:39:46 -05004626 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004627 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4628 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004629 "memory for hexdump\n");
4630 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004631 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4632 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004633 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004634 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004635 while (pagecnt) {
4636 _dump_buf_dif =
4637 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4638 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004639 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4640 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004641 "_dump_buf_dif at 0x%p\n",
4642 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004643 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004644 memset(_dump_buf_dif, 0,
4645 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004646 break;
James Smart3772a992009-05-22 14:50:54 -04004647 } else
James Smart81301a92008-12-04 22:39:46 -05004648 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004649 }
James Smart81301a92008-12-04 22:39:46 -05004650 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004651 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4652 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004653 "memory for hexdump\n");
4654 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004655 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4656 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004657 _dump_buf_dif);
4658}
4659
4660/**
4661 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4662 * @phba: pointer to lpfc hba data structure.
4663 *
4664 * This routine is invoked to perform all the necessary post initialization
4665 * setup for the device.
4666 **/
4667static void
4668lpfc_post_init_setup(struct lpfc_hba *phba)
4669{
4670 struct Scsi_Host *shost;
4671 struct lpfc_adapter_event_header adapter_event;
4672
4673 /* Get the default values for Model Name and Description */
4674 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4675
4676 /*
4677 * hba setup may have changed the hba_queue_depth so we need to
4678 * adjust the value of can_queue.
4679 */
4680 shost = pci_get_drvdata(phba->pcidev);
4681 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4682 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4683 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004684
4685 lpfc_host_attrib_init(shost);
4686
James Smart2e0fef82007-06-17 19:56:36 -05004687 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4688 spin_lock_irq(shost->host_lock);
4689 lpfc_poll_start_timer(phba);
4690 spin_unlock_irq(shost->host_lock);
4691 }
James Smart8f6d98d2006-08-01 07:34:00 -04004692
James Smart93996272008-08-24 21:50:30 -04004693 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4694 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004695 /* Send board arrival event to upper layer */
4696 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4697 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4698 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004699 sizeof(adapter_event),
4700 (char *) &adapter_event,
4701 LPFC_NL_VENDOR_ID);
4702 return;
4703}
4704
4705/**
4706 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4707 * @phba: pointer to lpfc hba data structure.
4708 *
4709 * This routine is invoked to set up the PCI device memory space for device
4710 * with SLI-3 interface spec.
4711 *
4712 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004713 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004714 * other values - error
4715 **/
4716static int
4717lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4718{
4719 struct pci_dev *pdev;
4720 unsigned long bar0map_len, bar2map_len;
4721 int i, hbq_count;
4722 void *ptr;
4723 int error = -ENODEV;
4724
4725 /* Obtain PCI device reference */
4726 if (!phba->pcidev)
4727 return error;
4728 else
4729 pdev = phba->pcidev;
4730
4731 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05004732 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
4733 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
4734 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
4735 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04004736 return error;
Michael Reed8e685972009-09-18 12:02:05 -05004737 }
4738 }
James Smart3772a992009-05-22 14:50:54 -04004739
4740 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4741 * required by each mapping.
4742 */
4743 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4744 bar0map_len = pci_resource_len(pdev, 0);
4745
4746 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4747 bar2map_len = pci_resource_len(pdev, 2);
4748
4749 /* Map HBA SLIM to a kernel virtual address. */
4750 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4751 if (!phba->slim_memmap_p) {
4752 dev_printk(KERN_ERR, &pdev->dev,
4753 "ioremap failed for SLIM memory.\n");
4754 goto out;
4755 }
4756
4757 /* Map HBA Control Registers to a kernel virtual address. */
4758 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4759 if (!phba->ctrl_regs_memmap_p) {
4760 dev_printk(KERN_ERR, &pdev->dev,
4761 "ioremap failed for HBA control registers.\n");
4762 goto out_iounmap_slim;
4763 }
4764
4765 /* Allocate memory for SLI-2 structures */
4766 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4767 SLI2_SLIM_SIZE,
4768 &phba->slim2p.phys,
4769 GFP_KERNEL);
4770 if (!phba->slim2p.virt)
4771 goto out_iounmap;
4772
4773 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4774 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4775 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4776 phba->IOCBs = (phba->slim2p.virt +
4777 offsetof(struct lpfc_sli2_slim, IOCBs));
4778
4779 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4780 lpfc_sli_hbq_size(),
4781 &phba->hbqslimp.phys,
4782 GFP_KERNEL);
4783 if (!phba->hbqslimp.virt)
4784 goto out_free_slim;
4785
4786 hbq_count = lpfc_sli_hbq_count();
4787 ptr = phba->hbqslimp.virt;
4788 for (i = 0; i < hbq_count; ++i) {
4789 phba->hbqs[i].hbq_virt = ptr;
4790 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4791 ptr += (lpfc_hbq_defs[i]->entry_count *
4792 sizeof(struct lpfc_hbq_entry));
4793 }
4794 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4795 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4796
4797 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4798
4799 INIT_LIST_HEAD(&phba->rb_pend_list);
4800
4801 phba->MBslimaddr = phba->slim_memmap_p;
4802 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4803 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4804 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4805 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004806
dea31012005-04-17 16:05:31 -05004807 return 0;
4808
dea31012005-04-17 16:05:31 -05004809out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004810 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4811 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004812out_iounmap:
4813 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004814out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004815 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004816out:
4817 return error;
4818}
4819
James Smarte59058c2008-08-24 21:49:00 -04004820/**
James Smart3772a992009-05-22 14:50:54 -04004821 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4822 * @phba: pointer to lpfc hba data structure.
4823 *
4824 * This routine is invoked to unset the PCI device memory space for device
4825 * with SLI-3 interface spec.
4826 **/
4827static void
4828lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4829{
4830 struct pci_dev *pdev;
4831
4832 /* Obtain PCI device reference */
4833 if (!phba->pcidev)
4834 return;
4835 else
4836 pdev = phba->pcidev;
4837
4838 /* Free coherent DMA memory allocated */
4839 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4840 phba->hbqslimp.virt, phba->hbqslimp.phys);
4841 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4842 phba->slim2p.virt, phba->slim2p.phys);
4843
4844 /* I/O memory unmap */
4845 iounmap(phba->ctrl_regs_memmap_p);
4846 iounmap(phba->slim_memmap_p);
4847
4848 return;
4849}
4850
4851/**
James Smartda0436e2009-05-22 14:51:39 -04004852 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4853 * @phba: pointer to lpfc hba data structure.
4854 *
4855 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4856 * done and check status.
4857 *
4858 * Return 0 if successful, otherwise -ENODEV.
4859 **/
4860int
4861lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4862{
James Smart28baac72010-02-12 14:42:03 -05004863 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
James Smartda0436e2009-05-22 14:51:39 -04004864 int i, port_error = -ENODEV;
4865
4866 if (!phba->sli4_hba.STAregaddr)
4867 return -ENODEV;
4868
James Smartda0436e2009-05-22 14:51:39 -04004869 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4870 for (i = 0; i < 3000; i++) {
4871 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4872 /* Encounter fatal POST error, break out */
4873 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4874 port_error = -ENODEV;
4875 break;
4876 }
4877 if (LPFC_POST_STAGE_ARMFW_READY ==
4878 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4879 port_error = 0;
4880 break;
4881 }
4882 msleep(10);
4883 }
4884
4885 if (port_error)
4886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4887 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4888 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4889 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4890 bf_get(lpfc_hst_state_perr, &sta_reg),
4891 bf_get(lpfc_hst_state_sfi, &sta_reg),
4892 bf_get(lpfc_hst_state_nip, &sta_reg),
4893 bf_get(lpfc_hst_state_ipc, &sta_reg),
4894 bf_get(lpfc_hst_state_xrom, &sta_reg),
4895 bf_get(lpfc_hst_state_dl, &sta_reg),
4896 bf_get(lpfc_hst_state_port_status, &sta_reg));
4897
4898 /* Log device information */
James Smart28baac72010-02-12 14:42:03 -05004899 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
4900 if (bf_get(lpfc_sli_intf_valid,
4901 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
4902 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4903 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4904 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4905 bf_get(lpfc_sli_intf_sli_family,
4906 &phba->sli4_hba.sli_intf),
4907 bf_get(lpfc_sli_intf_slirev,
4908 &phba->sli4_hba.sli_intf),
4909 bf_get(lpfc_sli_intf_featurelevel1,
4910 &phba->sli4_hba.sli_intf),
4911 bf_get(lpfc_sli_intf_featurelevel2,
4912 &phba->sli4_hba.sli_intf));
4913 }
James Smarta747c9c2009-11-18 15:41:10 -05004914 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
4915 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04004916 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05004917 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4918 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4919 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
4920 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
4921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4922 "1422 HBA Unrecoverable error: "
4923 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4924 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
4925 uerrlo_reg.word0, uerrhi_reg.word0,
4926 phba->sli4_hba.ue_mask_lo,
4927 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04004928 return -ENODEV;
4929 }
4930
James Smartda0436e2009-05-22 14:51:39 -04004931 return port_error;
4932}
4933
4934/**
4935 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4936 * @phba: pointer to lpfc hba data structure.
4937 *
4938 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4939 * memory map.
4940 **/
4941static void
4942lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4943{
4944 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4945 LPFC_UERR_STATUS_LO;
4946 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4947 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05004948 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4949 LPFC_UE_MASK_LO;
4950 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4951 LPFC_UE_MASK_HI;
James Smart28baac72010-02-12 14:42:03 -05004952 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
4953 LPFC_SLI_INTF;
James Smartda0436e2009-05-22 14:51:39 -04004954}
4955
4956/**
4957 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4958 * @phba: pointer to lpfc hba data structure.
4959 *
4960 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4961 * memory map.
4962 **/
4963static void
4964lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4965{
4966
4967 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4968 LPFC_HST_STATE;
4969 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4970 LPFC_HST_ISR0;
4971 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4972 LPFC_HST_IMR0;
4973 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4974 LPFC_HST_ISCR0;
4975 return;
4976}
4977
4978/**
4979 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
4980 * @phba: pointer to lpfc hba data structure.
4981 * @vf: virtual function number
4982 *
4983 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4984 * based on the given viftual function number, @vf.
4985 *
4986 * Return 0 if successful, otherwise -ENODEV.
4987 **/
4988static int
4989lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
4990{
4991 if (vf > LPFC_VIR_FUNC_MAX)
4992 return -ENODEV;
4993
4994 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4995 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4996 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4997 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4998 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4999 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5000 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5001 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5002 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5003 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5004 return 0;
5005}
5006
5007/**
5008 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5009 * @phba: pointer to lpfc hba data structure.
5010 *
5011 * This routine is invoked to create the bootstrap mailbox
5012 * region consistent with the SLI-4 interface spec. This
5013 * routine allocates all memory necessary to communicate
5014 * mailbox commands to the port and sets up all alignment
5015 * needs. No locks are expected to be held when calling
5016 * this routine.
5017 *
5018 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005019 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005020 * ENOMEM - could not allocated memory.
5021 **/
5022static int
5023lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5024{
5025 uint32_t bmbx_size;
5026 struct lpfc_dmabuf *dmabuf;
5027 struct dma_address *dma_address;
5028 uint32_t pa_addr;
5029 uint64_t phys_addr;
5030
5031 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5032 if (!dmabuf)
5033 return -ENOMEM;
5034
5035 /*
5036 * The bootstrap mailbox region is comprised of 2 parts
5037 * plus an alignment restriction of 16 bytes.
5038 */
5039 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5040 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5041 bmbx_size,
5042 &dmabuf->phys,
5043 GFP_KERNEL);
5044 if (!dmabuf->virt) {
5045 kfree(dmabuf);
5046 return -ENOMEM;
5047 }
5048 memset(dmabuf->virt, 0, bmbx_size);
5049
5050 /*
5051 * Initialize the bootstrap mailbox pointers now so that the register
5052 * operations are simple later. The mailbox dma address is required
5053 * to be 16-byte aligned. Also align the virtual memory as each
5054 * maibox is copied into the bmbx mailbox region before issuing the
5055 * command to the port.
5056 */
5057 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5058 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5059
5060 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5061 LPFC_ALIGN_16_BYTE);
5062 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5063 LPFC_ALIGN_16_BYTE);
5064
5065 /*
5066 * Set the high and low physical addresses now. The SLI4 alignment
5067 * requirement is 16 bytes and the mailbox is posted to the port
5068 * as two 30-bit addresses. The other data is a bit marking whether
5069 * the 30-bit address is the high or low address.
5070 * Upcast bmbx aphys to 64bits so shift instruction compiles
5071 * clean on 32 bit machines.
5072 */
5073 dma_address = &phba->sli4_hba.bmbx.dma_address;
5074 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5075 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5076 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5077 LPFC_BMBX_BIT1_ADDR_HI);
5078
5079 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5080 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5081 LPFC_BMBX_BIT1_ADDR_LO);
5082 return 0;
5083}
5084
5085/**
5086 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5087 * @phba: pointer to lpfc hba data structure.
5088 *
5089 * This routine is invoked to teardown the bootstrap mailbox
5090 * region and release all host resources. This routine requires
5091 * the caller to ensure all mailbox commands recovered, no
5092 * additional mailbox comands are sent, and interrupts are disabled
5093 * before calling this routine.
5094 *
5095 **/
5096static void
5097lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5098{
5099 dma_free_coherent(&phba->pcidev->dev,
5100 phba->sli4_hba.bmbx.bmbx_size,
5101 phba->sli4_hba.bmbx.dmabuf->virt,
5102 phba->sli4_hba.bmbx.dmabuf->phys);
5103
5104 kfree(phba->sli4_hba.bmbx.dmabuf);
5105 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5106}
5107
5108/**
5109 * lpfc_sli4_read_config - Get the config parameters.
5110 * @phba: pointer to lpfc hba data structure.
5111 *
5112 * This routine is invoked to read the configuration parameters from the HBA.
5113 * The configuration parameters are used to set the base and maximum values
5114 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5115 * allocation for the port.
5116 *
5117 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005118 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005119 * ENOMEM - No availble memory
5120 * EIO - The mailbox failed to complete successfully.
5121 **/
5122static int
5123lpfc_sli4_read_config(struct lpfc_hba *phba)
5124{
5125 LPFC_MBOXQ_t *pmb;
5126 struct lpfc_mbx_read_config *rd_config;
5127 uint32_t rc = 0;
5128
5129 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5130 if (!pmb) {
5131 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5132 "2011 Unable to allocate memory for issuing "
5133 "SLI_CONFIG_SPECIAL mailbox command\n");
5134 return -ENOMEM;
5135 }
5136
5137 lpfc_read_config(phba, pmb);
5138
5139 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5140 if (rc != MBX_SUCCESS) {
5141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5142 "2012 Mailbox failed , mbxCmd x%x "
5143 "READ_CONFIG, mbxStatus x%x\n",
5144 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5145 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5146 rc = -EIO;
5147 } else {
5148 rd_config = &pmb->u.mqe.un.rd_config;
5149 phba->sli4_hba.max_cfg_param.max_xri =
5150 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5151 phba->sli4_hba.max_cfg_param.xri_base =
5152 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5153 phba->sli4_hba.max_cfg_param.max_vpi =
5154 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5155 phba->sli4_hba.max_cfg_param.vpi_base =
5156 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5157 phba->sli4_hba.max_cfg_param.max_rpi =
5158 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5159 phba->sli4_hba.max_cfg_param.rpi_base =
5160 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5161 phba->sli4_hba.max_cfg_param.max_vfi =
5162 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5163 phba->sli4_hba.max_cfg_param.vfi_base =
5164 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5165 phba->sli4_hba.max_cfg_param.max_fcfi =
5166 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5167 phba->sli4_hba.max_cfg_param.fcfi_base =
5168 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5169 phba->sli4_hba.max_cfg_param.max_eq =
5170 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5171 phba->sli4_hba.max_cfg_param.max_rq =
5172 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5173 phba->sli4_hba.max_cfg_param.max_wq =
5174 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5175 phba->sli4_hba.max_cfg_param.max_cq =
5176 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5177 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5178 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5179 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5180 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5181 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005182 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5183 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005184 phba->max_vports = phba->max_vpi;
5185 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5186 "2003 cfg params XRI(B:%d M:%d), "
5187 "VPI(B:%d M:%d) "
5188 "VFI(B:%d M:%d) "
5189 "RPI(B:%d M:%d) "
5190 "FCFI(B:%d M:%d)\n",
5191 phba->sli4_hba.max_cfg_param.xri_base,
5192 phba->sli4_hba.max_cfg_param.max_xri,
5193 phba->sli4_hba.max_cfg_param.vpi_base,
5194 phba->sli4_hba.max_cfg_param.max_vpi,
5195 phba->sli4_hba.max_cfg_param.vfi_base,
5196 phba->sli4_hba.max_cfg_param.max_vfi,
5197 phba->sli4_hba.max_cfg_param.rpi_base,
5198 phba->sli4_hba.max_cfg_param.max_rpi,
5199 phba->sli4_hba.max_cfg_param.fcfi_base,
5200 phba->sli4_hba.max_cfg_param.max_fcfi);
5201 }
5202 mempool_free(pmb, phba->mbox_mem_pool);
5203
5204 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5205 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5206 phba->cfg_hba_queue_depth =
5207 phba->sli4_hba.max_cfg_param.max_xri;
5208 return rc;
5209}
5210
5211/**
5212 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5213 * @phba: pointer to lpfc hba data structure.
5214 *
5215 * This routine is invoked to setup the host-side endian order to the
5216 * HBA consistent with the SLI-4 interface spec.
5217 *
5218 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005219 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005220 * ENOMEM - No availble memory
5221 * EIO - The mailbox failed to complete successfully.
5222 **/
5223static int
5224lpfc_setup_endian_order(struct lpfc_hba *phba)
5225{
5226 LPFC_MBOXQ_t *mboxq;
5227 uint32_t rc = 0;
5228 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5229 HOST_ENDIAN_HIGH_WORD1};
5230
5231 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5232 if (!mboxq) {
5233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5234 "0492 Unable to allocate memory for issuing "
5235 "SLI_CONFIG_SPECIAL mailbox command\n");
5236 return -ENOMEM;
5237 }
5238
5239 /*
5240 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5241 * words to contain special data values and no other data.
5242 */
5243 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5244 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5245 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5246 if (rc != MBX_SUCCESS) {
5247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5248 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5249 "status x%x\n",
5250 rc);
5251 rc = -EIO;
5252 }
5253
5254 mempool_free(mboxq, phba->mbox_mem_pool);
5255 return rc;
5256}
5257
5258/**
5259 * lpfc_sli4_queue_create - Create all the SLI4 queues
5260 * @phba: pointer to lpfc hba data structure.
5261 *
5262 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5263 * operation. For each SLI4 queue type, the parameters such as queue entry
5264 * count (queue depth) shall be taken from the module parameter. For now,
5265 * we just use some constant number as place holder.
5266 *
5267 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005268 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005269 * ENOMEM - No availble memory
5270 * EIO - The mailbox failed to complete successfully.
5271 **/
5272static int
5273lpfc_sli4_queue_create(struct lpfc_hba *phba)
5274{
5275 struct lpfc_queue *qdesc;
5276 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5277 int cfg_fcp_wq_count;
5278 int cfg_fcp_eq_count;
5279
5280 /*
5281 * Sanity check for confiugred queue parameters against the run-time
5282 * device parameters
5283 */
5284
5285 /* Sanity check on FCP fast-path WQ parameters */
5286 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5287 if (cfg_fcp_wq_count >
5288 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5289 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5290 LPFC_SP_WQN_DEF;
5291 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5293 "2581 Not enough WQs (%d) from "
5294 "the pci function for supporting "
5295 "FCP WQs (%d)\n",
5296 phba->sli4_hba.max_cfg_param.max_wq,
5297 phba->cfg_fcp_wq_count);
5298 goto out_error;
5299 }
5300 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5301 "2582 Not enough WQs (%d) from the pci "
5302 "function for supporting the requested "
5303 "FCP WQs (%d), the actual FCP WQs can "
5304 "be supported: %d\n",
5305 phba->sli4_hba.max_cfg_param.max_wq,
5306 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5307 }
5308 /* The actual number of FCP work queues adopted */
5309 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5310
5311 /* Sanity check on FCP fast-path EQ parameters */
5312 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5313 if (cfg_fcp_eq_count >
5314 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5315 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5316 LPFC_SP_EQN_DEF;
5317 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5318 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5319 "2574 Not enough EQs (%d) from the "
5320 "pci function for supporting FCP "
5321 "EQs (%d)\n",
5322 phba->sli4_hba.max_cfg_param.max_eq,
5323 phba->cfg_fcp_eq_count);
5324 goto out_error;
5325 }
5326 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5327 "2575 Not enough EQs (%d) from the pci "
5328 "function for supporting the requested "
5329 "FCP EQs (%d), the actual FCP EQs can "
5330 "be supported: %d\n",
5331 phba->sli4_hba.max_cfg_param.max_eq,
5332 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5333 }
5334 /* It does not make sense to have more EQs than WQs */
5335 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5336 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005337 "2593 The FCP EQ count(%d) cannot be greater "
5338 "than the FCP WQ count(%d), limiting the "
5339 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005340 phba->cfg_fcp_wq_count,
5341 phba->cfg_fcp_wq_count);
5342 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5343 }
5344 /* The actual number of FCP event queues adopted */
5345 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5346 /* The overall number of event queues used */
5347 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5348
5349 /*
5350 * Create Event Queues (EQs)
5351 */
5352
5353 /* Get EQ depth from module parameter, fake the default for now */
5354 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5355 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5356
5357 /* Create slow path event queue */
5358 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5359 phba->sli4_hba.eq_ecount);
5360 if (!qdesc) {
5361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5362 "0496 Failed allocate slow-path EQ\n");
5363 goto out_error;
5364 }
5365 phba->sli4_hba.sp_eq = qdesc;
5366
5367 /* Create fast-path FCP Event Queue(s) */
5368 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5369 phba->cfg_fcp_eq_count), GFP_KERNEL);
5370 if (!phba->sli4_hba.fp_eq) {
5371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5372 "2576 Failed allocate memory for fast-path "
5373 "EQ record array\n");
5374 goto out_free_sp_eq;
5375 }
5376 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5377 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5378 phba->sli4_hba.eq_ecount);
5379 if (!qdesc) {
5380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5381 "0497 Failed allocate fast-path EQ\n");
5382 goto out_free_fp_eq;
5383 }
5384 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5385 }
5386
5387 /*
5388 * Create Complete Queues (CQs)
5389 */
5390
5391 /* Get CQ depth from module parameter, fake the default for now */
5392 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5393 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5394
5395 /* Create slow-path Mailbox Command Complete Queue */
5396 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5397 phba->sli4_hba.cq_ecount);
5398 if (!qdesc) {
5399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5400 "0500 Failed allocate slow-path mailbox CQ\n");
5401 goto out_free_fp_eq;
5402 }
5403 phba->sli4_hba.mbx_cq = qdesc;
5404
5405 /* Create slow-path ELS Complete Queue */
5406 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5407 phba->sli4_hba.cq_ecount);
5408 if (!qdesc) {
5409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5410 "0501 Failed allocate slow-path ELS CQ\n");
5411 goto out_free_mbx_cq;
5412 }
5413 phba->sli4_hba.els_cq = qdesc;
5414
James Smartda0436e2009-05-22 14:51:39 -04005415
5416 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5417 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5418 phba->cfg_fcp_eq_count), GFP_KERNEL);
5419 if (!phba->sli4_hba.fcp_cq) {
5420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5421 "2577 Failed allocate memory for fast-path "
5422 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005423 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005424 }
5425 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5426 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5427 phba->sli4_hba.cq_ecount);
5428 if (!qdesc) {
5429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5430 "0499 Failed allocate fast-path FCP "
5431 "CQ (%d)\n", fcp_cqidx);
5432 goto out_free_fcp_cq;
5433 }
5434 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5435 }
5436
5437 /* Create Mailbox Command Queue */
5438 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5439 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5440
5441 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5442 phba->sli4_hba.mq_ecount);
5443 if (!qdesc) {
5444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5445 "0505 Failed allocate slow-path MQ\n");
5446 goto out_free_fcp_cq;
5447 }
5448 phba->sli4_hba.mbx_wq = qdesc;
5449
5450 /*
5451 * Create all the Work Queues (WQs)
5452 */
5453 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5454 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5455
5456 /* Create slow-path ELS Work Queue */
5457 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5458 phba->sli4_hba.wq_ecount);
5459 if (!qdesc) {
5460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5461 "0504 Failed allocate slow-path ELS WQ\n");
5462 goto out_free_mbx_wq;
5463 }
5464 phba->sli4_hba.els_wq = qdesc;
5465
5466 /* Create fast-path FCP Work Queue(s) */
5467 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5468 phba->cfg_fcp_wq_count), GFP_KERNEL);
5469 if (!phba->sli4_hba.fcp_wq) {
5470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5471 "2578 Failed allocate memory for fast-path "
5472 "WQ record array\n");
5473 goto out_free_els_wq;
5474 }
5475 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5476 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5477 phba->sli4_hba.wq_ecount);
5478 if (!qdesc) {
5479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5480 "0503 Failed allocate fast-path FCP "
5481 "WQ (%d)\n", fcp_wqidx);
5482 goto out_free_fcp_wq;
5483 }
5484 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5485 }
5486
5487 /*
5488 * Create Receive Queue (RQ)
5489 */
5490 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5491 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5492
5493 /* Create Receive Queue for header */
5494 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5495 phba->sli4_hba.rq_ecount);
5496 if (!qdesc) {
5497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5498 "0506 Failed allocate receive HRQ\n");
5499 goto out_free_fcp_wq;
5500 }
5501 phba->sli4_hba.hdr_rq = qdesc;
5502
5503 /* Create Receive Queue for data */
5504 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5505 phba->sli4_hba.rq_ecount);
5506 if (!qdesc) {
5507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5508 "0507 Failed allocate receive DRQ\n");
5509 goto out_free_hdr_rq;
5510 }
5511 phba->sli4_hba.dat_rq = qdesc;
5512
5513 return 0;
5514
5515out_free_hdr_rq:
5516 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5517 phba->sli4_hba.hdr_rq = NULL;
5518out_free_fcp_wq:
5519 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5520 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5521 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5522 }
5523 kfree(phba->sli4_hba.fcp_wq);
5524out_free_els_wq:
5525 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5526 phba->sli4_hba.els_wq = NULL;
5527out_free_mbx_wq:
5528 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5529 phba->sli4_hba.mbx_wq = NULL;
5530out_free_fcp_cq:
5531 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5532 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5533 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5534 }
5535 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005536out_free_els_cq:
5537 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5538 phba->sli4_hba.els_cq = NULL;
5539out_free_mbx_cq:
5540 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5541 phba->sli4_hba.mbx_cq = NULL;
5542out_free_fp_eq:
5543 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5544 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5545 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5546 }
5547 kfree(phba->sli4_hba.fp_eq);
5548out_free_sp_eq:
5549 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5550 phba->sli4_hba.sp_eq = NULL;
5551out_error:
5552 return -ENOMEM;
5553}
5554
5555/**
5556 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5557 * @phba: pointer to lpfc hba data structure.
5558 *
5559 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5560 * operation.
5561 *
5562 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005563 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005564 * ENOMEM - No availble memory
5565 * EIO - The mailbox failed to complete successfully.
5566 **/
5567static void
5568lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5569{
5570 int fcp_qidx;
5571
5572 /* Release mailbox command work queue */
5573 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5574 phba->sli4_hba.mbx_wq = NULL;
5575
5576 /* Release ELS work queue */
5577 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5578 phba->sli4_hba.els_wq = NULL;
5579
5580 /* Release FCP work queue */
5581 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5582 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5583 kfree(phba->sli4_hba.fcp_wq);
5584 phba->sli4_hba.fcp_wq = NULL;
5585
5586 /* Release unsolicited receive queue */
5587 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5588 phba->sli4_hba.hdr_rq = NULL;
5589 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5590 phba->sli4_hba.dat_rq = NULL;
5591
James Smartda0436e2009-05-22 14:51:39 -04005592 /* Release ELS complete queue */
5593 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5594 phba->sli4_hba.els_cq = NULL;
5595
5596 /* Release mailbox command complete queue */
5597 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5598 phba->sli4_hba.mbx_cq = NULL;
5599
5600 /* Release FCP response complete queue */
5601 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5602 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5603 kfree(phba->sli4_hba.fcp_cq);
5604 phba->sli4_hba.fcp_cq = NULL;
5605
5606 /* Release fast-path event queue */
5607 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5608 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5609 kfree(phba->sli4_hba.fp_eq);
5610 phba->sli4_hba.fp_eq = NULL;
5611
5612 /* Release slow-path event queue */
5613 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5614 phba->sli4_hba.sp_eq = NULL;
5615
5616 return;
5617}
5618
5619/**
5620 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5621 * @phba: pointer to lpfc hba data structure.
5622 *
5623 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5624 * operation.
5625 *
5626 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005627 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005628 * ENOMEM - No availble memory
5629 * EIO - The mailbox failed to complete successfully.
5630 **/
5631int
5632lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5633{
5634 int rc = -ENOMEM;
5635 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5636 int fcp_cq_index = 0;
5637
5638 /*
5639 * Set up Event Queues (EQs)
5640 */
5641
5642 /* Set up slow-path event queue */
5643 if (!phba->sli4_hba.sp_eq) {
5644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5645 "0520 Slow-path EQ not allocated\n");
5646 goto out_error;
5647 }
5648 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5649 LPFC_SP_DEF_IMAX);
5650 if (rc) {
5651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5652 "0521 Failed setup of slow-path EQ: "
5653 "rc = 0x%x\n", rc);
5654 goto out_error;
5655 }
5656 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5657 "2583 Slow-path EQ setup: queue-id=%d\n",
5658 phba->sli4_hba.sp_eq->queue_id);
5659
5660 /* Set up fast-path event queue */
5661 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5662 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5664 "0522 Fast-path EQ (%d) not "
5665 "allocated\n", fcp_eqidx);
5666 goto out_destroy_fp_eq;
5667 }
5668 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5669 phba->cfg_fcp_imax);
5670 if (rc) {
5671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5672 "0523 Failed setup of fast-path EQ "
5673 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5674 goto out_destroy_fp_eq;
5675 }
5676 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5677 "2584 Fast-path EQ setup: "
5678 "queue[%d]-id=%d\n", fcp_eqidx,
5679 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5680 }
5681
5682 /*
5683 * Set up Complete Queues (CQs)
5684 */
5685
5686 /* Set up slow-path MBOX Complete Queue as the first CQ */
5687 if (!phba->sli4_hba.mbx_cq) {
5688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5689 "0528 Mailbox CQ not allocated\n");
5690 goto out_destroy_fp_eq;
5691 }
5692 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5693 LPFC_MCQ, LPFC_MBOX);
5694 if (rc) {
5695 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5696 "0529 Failed setup of slow-path mailbox CQ: "
5697 "rc = 0x%x\n", rc);
5698 goto out_destroy_fp_eq;
5699 }
5700 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5701 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5702 phba->sli4_hba.mbx_cq->queue_id,
5703 phba->sli4_hba.sp_eq->queue_id);
5704
5705 /* Set up slow-path ELS Complete Queue */
5706 if (!phba->sli4_hba.els_cq) {
5707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5708 "0530 ELS CQ not allocated\n");
5709 goto out_destroy_mbx_cq;
5710 }
5711 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5712 LPFC_WCQ, LPFC_ELS);
5713 if (rc) {
5714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5715 "0531 Failed setup of slow-path ELS CQ: "
5716 "rc = 0x%x\n", rc);
5717 goto out_destroy_mbx_cq;
5718 }
5719 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5720 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5721 phba->sli4_hba.els_cq->queue_id,
5722 phba->sli4_hba.sp_eq->queue_id);
5723
James Smartda0436e2009-05-22 14:51:39 -04005724 /* Set up fast-path FCP Response Complete Queue */
5725 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5726 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5728 "0526 Fast-path FCP CQ (%d) not "
5729 "allocated\n", fcp_cqidx);
5730 goto out_destroy_fcp_cq;
5731 }
5732 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5733 phba->sli4_hba.fp_eq[fcp_cqidx],
5734 LPFC_WCQ, LPFC_FCP);
5735 if (rc) {
5736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5737 "0527 Failed setup of fast-path FCP "
5738 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5739 goto out_destroy_fcp_cq;
5740 }
5741 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5742 "2588 FCP CQ setup: cq[%d]-id=%d, "
5743 "parent eq[%d]-id=%d\n",
5744 fcp_cqidx,
5745 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5746 fcp_cqidx,
5747 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5748 }
5749
5750 /*
5751 * Set up all the Work Queues (WQs)
5752 */
5753
5754 /* Set up Mailbox Command Queue */
5755 if (!phba->sli4_hba.mbx_wq) {
5756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5757 "0538 Slow-path MQ not allocated\n");
5758 goto out_destroy_fcp_cq;
5759 }
5760 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5761 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5762 if (rc) {
5763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5764 "0539 Failed setup of slow-path MQ: "
5765 "rc = 0x%x\n", rc);
5766 goto out_destroy_fcp_cq;
5767 }
5768 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5769 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5770 phba->sli4_hba.mbx_wq->queue_id,
5771 phba->sli4_hba.mbx_cq->queue_id);
5772
5773 /* Set up slow-path ELS Work Queue */
5774 if (!phba->sli4_hba.els_wq) {
5775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5776 "0536 Slow-path ELS WQ not allocated\n");
5777 goto out_destroy_mbx_wq;
5778 }
5779 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5780 phba->sli4_hba.els_cq, LPFC_ELS);
5781 if (rc) {
5782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5783 "0537 Failed setup of slow-path ELS WQ: "
5784 "rc = 0x%x\n", rc);
5785 goto out_destroy_mbx_wq;
5786 }
5787 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5788 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5789 phba->sli4_hba.els_wq->queue_id,
5790 phba->sli4_hba.els_cq->queue_id);
5791
5792 /* Set up fast-path FCP Work Queue */
5793 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5794 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5796 "0534 Fast-path FCP WQ (%d) not "
5797 "allocated\n", fcp_wqidx);
5798 goto out_destroy_fcp_wq;
5799 }
5800 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5801 phba->sli4_hba.fcp_cq[fcp_cq_index],
5802 LPFC_FCP);
5803 if (rc) {
5804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5805 "0535 Failed setup of fast-path FCP "
5806 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5807 goto out_destroy_fcp_wq;
5808 }
5809 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5810 "2591 FCP WQ setup: wq[%d]-id=%d, "
5811 "parent cq[%d]-id=%d\n",
5812 fcp_wqidx,
5813 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5814 fcp_cq_index,
5815 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5816 /* Round robin FCP Work Queue's Completion Queue assignment */
5817 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5818 }
5819
5820 /*
5821 * Create Receive Queue (RQ)
5822 */
5823 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5825 "0540 Receive Queue not allocated\n");
5826 goto out_destroy_fcp_wq;
5827 }
5828 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04005829 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04005830 if (rc) {
5831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5832 "0541 Failed setup of Receive Queue: "
5833 "rc = 0x%x\n", rc);
5834 goto out_destroy_fcp_wq;
5835 }
5836 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5837 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5838 "parent cq-id=%d\n",
5839 phba->sli4_hba.hdr_rq->queue_id,
5840 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04005841 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04005842 return 0;
5843
5844out_destroy_fcp_wq:
5845 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5846 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5847 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5848out_destroy_mbx_wq:
5849 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5850out_destroy_fcp_cq:
5851 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5852 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04005853 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5854out_destroy_mbx_cq:
5855 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5856out_destroy_fp_eq:
5857 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5858 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5859 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5860out_error:
5861 return rc;
5862}
5863
5864/**
5865 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5866 * @phba: pointer to lpfc hba data structure.
5867 *
5868 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5869 * operation.
5870 *
5871 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005872 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005873 * ENOMEM - No availble memory
5874 * EIO - The mailbox failed to complete successfully.
5875 **/
5876void
5877lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5878{
5879 int fcp_qidx;
5880
5881 /* Unset mailbox command work queue */
5882 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5883 /* Unset ELS work queue */
5884 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5885 /* Unset unsolicited receive queue */
5886 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5887 /* Unset FCP work queue */
5888 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5889 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5890 /* Unset mailbox command complete queue */
5891 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5892 /* Unset ELS complete queue */
5893 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04005894 /* Unset FCP response complete queue */
5895 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5896 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5897 /* Unset fast-path event queue */
5898 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5899 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5900 /* Unset slow-path event queue */
5901 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5902}
5903
5904/**
5905 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5906 * @phba: pointer to lpfc hba data structure.
5907 *
5908 * This routine is invoked to allocate and set up a pool of completion queue
5909 * events. The body of the completion queue event is a completion queue entry
5910 * CQE. For now, this pool is used for the interrupt service routine to queue
5911 * the following HBA completion queue events for the worker thread to process:
5912 * - Mailbox asynchronous events
5913 * - Receive queue completion unsolicited events
5914 * Later, this can be used for all the slow-path events.
5915 *
5916 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005917 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005918 * -ENOMEM - No availble memory
5919 **/
5920static int
5921lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5922{
5923 struct lpfc_cq_event *cq_event;
5924 int i;
5925
5926 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5927 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5928 if (!cq_event)
5929 goto out_pool_create_fail;
5930 list_add_tail(&cq_event->list,
5931 &phba->sli4_hba.sp_cqe_event_pool);
5932 }
5933 return 0;
5934
5935out_pool_create_fail:
5936 lpfc_sli4_cq_event_pool_destroy(phba);
5937 return -ENOMEM;
5938}
5939
5940/**
5941 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5942 * @phba: pointer to lpfc hba data structure.
5943 *
5944 * This routine is invoked to free the pool of completion queue events at
5945 * driver unload time. Note that, it is the responsibility of the driver
5946 * cleanup routine to free all the outstanding completion-queue events
5947 * allocated from this pool back into the pool before invoking this routine
5948 * to destroy the pool.
5949 **/
5950static void
5951lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5952{
5953 struct lpfc_cq_event *cq_event, *next_cq_event;
5954
5955 list_for_each_entry_safe(cq_event, next_cq_event,
5956 &phba->sli4_hba.sp_cqe_event_pool, list) {
5957 list_del(&cq_event->list);
5958 kfree(cq_event);
5959 }
5960}
5961
5962/**
5963 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5964 * @phba: pointer to lpfc hba data structure.
5965 *
5966 * This routine is the lock free version of the API invoked to allocate a
5967 * completion-queue event from the free pool.
5968 *
5969 * Return: Pointer to the newly allocated completion-queue event if successful
5970 * NULL otherwise.
5971 **/
5972struct lpfc_cq_event *
5973__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5974{
5975 struct lpfc_cq_event *cq_event = NULL;
5976
5977 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5978 struct lpfc_cq_event, list);
5979 return cq_event;
5980}
5981
5982/**
5983 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5984 * @phba: pointer to lpfc hba data structure.
5985 *
5986 * This routine is the lock version of the API invoked to allocate a
5987 * completion-queue event from the free pool.
5988 *
5989 * Return: Pointer to the newly allocated completion-queue event if successful
5990 * NULL otherwise.
5991 **/
5992struct lpfc_cq_event *
5993lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5994{
5995 struct lpfc_cq_event *cq_event;
5996 unsigned long iflags;
5997
5998 spin_lock_irqsave(&phba->hbalock, iflags);
5999 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6000 spin_unlock_irqrestore(&phba->hbalock, iflags);
6001 return cq_event;
6002}
6003
6004/**
6005 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6006 * @phba: pointer to lpfc hba data structure.
6007 * @cq_event: pointer to the completion queue event to be freed.
6008 *
6009 * This routine is the lock free version of the API invoked to release a
6010 * completion-queue event back into the free pool.
6011 **/
6012void
6013__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6014 struct lpfc_cq_event *cq_event)
6015{
6016 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6017}
6018
6019/**
6020 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6021 * @phba: pointer to lpfc hba data structure.
6022 * @cq_event: pointer to the completion queue event to be freed.
6023 *
6024 * This routine is the lock version of the API invoked to release a
6025 * completion-queue event back into the free pool.
6026 **/
6027void
6028lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6029 struct lpfc_cq_event *cq_event)
6030{
6031 unsigned long iflags;
6032 spin_lock_irqsave(&phba->hbalock, iflags);
6033 __lpfc_sli4_cq_event_release(phba, cq_event);
6034 spin_unlock_irqrestore(&phba->hbalock, iflags);
6035}
6036
6037/**
6038 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6039 * @phba: pointer to lpfc hba data structure.
6040 *
6041 * This routine is to free all the pending completion-queue events to the
6042 * back into the free pool for device reset.
6043 **/
6044static void
6045lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6046{
6047 LIST_HEAD(cqelist);
6048 struct lpfc_cq_event *cqe;
6049 unsigned long iflags;
6050
6051 /* Retrieve all the pending WCQEs from pending WCQE lists */
6052 spin_lock_irqsave(&phba->hbalock, iflags);
6053 /* Pending FCP XRI abort events */
6054 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6055 &cqelist);
6056 /* Pending ELS XRI abort events */
6057 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6058 &cqelist);
6059 /* Pending asynnc events */
6060 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6061 &cqelist);
6062 spin_unlock_irqrestore(&phba->hbalock, iflags);
6063
6064 while (!list_empty(&cqelist)) {
6065 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6066 lpfc_sli4_cq_event_release(phba, cqe);
6067 }
6068}
6069
6070/**
6071 * lpfc_pci_function_reset - Reset pci function.
6072 * @phba: pointer to lpfc hba data structure.
6073 *
6074 * This routine is invoked to request a PCI function reset. It will destroys
6075 * all resources assigned to the PCI function which originates this request.
6076 *
6077 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006078 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006079 * ENOMEM - No availble memory
6080 * EIO - The mailbox failed to complete successfully.
6081 **/
6082int
6083lpfc_pci_function_reset(struct lpfc_hba *phba)
6084{
6085 LPFC_MBOXQ_t *mboxq;
6086 uint32_t rc = 0;
6087 uint32_t shdr_status, shdr_add_status;
6088 union lpfc_sli4_cfg_shdr *shdr;
6089
6090 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6091 if (!mboxq) {
6092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6093 "0494 Unable to allocate memory for issuing "
6094 "SLI_FUNCTION_RESET mailbox command\n");
6095 return -ENOMEM;
6096 }
6097
6098 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6099 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6100 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6101 LPFC_SLI4_MBX_EMBED);
6102 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6103 shdr = (union lpfc_sli4_cfg_shdr *)
6104 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6105 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6106 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6107 if (rc != MBX_TIMEOUT)
6108 mempool_free(mboxq, phba->mbox_mem_pool);
6109 if (shdr_status || shdr_add_status || rc) {
6110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6111 "0495 SLI_FUNCTION_RESET mailbox failed with "
6112 "status x%x add_status x%x, mbx status x%x\n",
6113 shdr_status, shdr_add_status, rc);
6114 rc = -ENXIO;
6115 }
6116 return rc;
6117}
6118
6119/**
6120 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6121 * @phba: pointer to lpfc hba data structure.
6122 * @cnt: number of nop mailbox commands to send.
6123 *
6124 * This routine is invoked to send a number @cnt of NOP mailbox command and
6125 * wait for each command to complete.
6126 *
6127 * Return: the number of NOP mailbox command completed.
6128 **/
6129static int
6130lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6131{
6132 LPFC_MBOXQ_t *mboxq;
6133 int length, cmdsent;
6134 uint32_t mbox_tmo;
6135 uint32_t rc = 0;
6136 uint32_t shdr_status, shdr_add_status;
6137 union lpfc_sli4_cfg_shdr *shdr;
6138
6139 if (cnt == 0) {
6140 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6141 "2518 Requested to send 0 NOP mailbox cmd\n");
6142 return cnt;
6143 }
6144
6145 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6146 if (!mboxq) {
6147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6148 "2519 Unable to allocate memory for issuing "
6149 "NOP mailbox command\n");
6150 return 0;
6151 }
6152
6153 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6154 length = (sizeof(struct lpfc_mbx_nop) -
6155 sizeof(struct lpfc_sli4_cfg_mhdr));
6156 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6157 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6158
6159 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6160 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6161 if (!phba->sli4_hba.intr_enable)
6162 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6163 else
6164 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6165 if (rc == MBX_TIMEOUT)
6166 break;
6167 /* Check return status */
6168 shdr = (union lpfc_sli4_cfg_shdr *)
6169 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6170 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6171 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6172 &shdr->response);
6173 if (shdr_status || shdr_add_status || rc) {
6174 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6175 "2520 NOP mailbox command failed "
6176 "status x%x add_status x%x mbx "
6177 "status x%x\n", shdr_status,
6178 shdr_add_status, rc);
6179 break;
6180 }
6181 }
6182
6183 if (rc != MBX_TIMEOUT)
6184 mempool_free(mboxq, phba->mbox_mem_pool);
6185
6186 return cmdsent;
6187}
6188
6189/**
6190 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6191 * @phba: pointer to lpfc hba data structure.
6192 * @fcfi: fcf index.
6193 *
6194 * This routine is invoked to unregister a FCFI from device.
6195 **/
6196void
6197lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6198{
6199 LPFC_MBOXQ_t *mbox;
6200 uint32_t mbox_tmo;
6201 int rc;
6202 unsigned long flags;
6203
6204 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6205
6206 if (!mbox)
6207 return;
6208
6209 lpfc_unreg_fcfi(mbox, fcfi);
6210
6211 if (!phba->sli4_hba.intr_enable)
6212 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6213 else {
6214 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6215 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6216 }
6217 if (rc != MBX_TIMEOUT)
6218 mempool_free(mbox, phba->mbox_mem_pool);
6219 if (rc != MBX_SUCCESS)
6220 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6221 "2517 Unregister FCFI command failed "
6222 "status %d, mbxStatus x%x\n", rc,
6223 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6224 else {
6225 spin_lock_irqsave(&phba->hbalock, flags);
6226 /* Mark the FCFI is no longer registered */
6227 phba->fcf.fcf_flag &=
James Smartecfd03c2010-02-12 14:41:27 -05006228 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
James Smartda0436e2009-05-22 14:51:39 -04006229 spin_unlock_irqrestore(&phba->hbalock, flags);
6230 }
6231}
6232
6233/**
6234 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6235 * @phba: pointer to lpfc hba data structure.
6236 *
6237 * This routine is invoked to set up the PCI device memory space for device
6238 * with SLI-4 interface spec.
6239 *
6240 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006241 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006242 * other values - error
6243 **/
6244static int
6245lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6246{
6247 struct pci_dev *pdev;
6248 unsigned long bar0map_len, bar1map_len, bar2map_len;
6249 int error = -ENODEV;
6250
6251 /* Obtain PCI device reference */
6252 if (!phba->pcidev)
6253 return error;
6254 else
6255 pdev = phba->pcidev;
6256
6257 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006258 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6259 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6260 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6261 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006262 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006263 }
6264 }
James Smartda0436e2009-05-22 14:51:39 -04006265
6266 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6267 * number of bytes required by each mapping. They are actually
James Smart1dfb5a42010-02-12 14:40:50 -05006268 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006269 */
James Smart1dfb5a42010-02-12 14:40:50 -05006270 if (pci_resource_start(pdev, 0)) {
6271 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6272 bar0map_len = pci_resource_len(pdev, 0);
6273 } else {
6274 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6275 bar0map_len = pci_resource_len(pdev, 1);
6276 }
6277 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6278 bar1map_len = pci_resource_len(pdev, 2);
James Smartda0436e2009-05-22 14:51:39 -04006279
James Smart1dfb5a42010-02-12 14:40:50 -05006280 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6281 bar2map_len = pci_resource_len(pdev, 4);
James Smartda0436e2009-05-22 14:51:39 -04006282
6283 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6284 phba->sli4_hba.conf_regs_memmap_p =
6285 ioremap(phba->pci_bar0_map, bar0map_len);
6286 if (!phba->sli4_hba.conf_regs_memmap_p) {
6287 dev_printk(KERN_ERR, &pdev->dev,
6288 "ioremap failed for SLI4 PCI config registers.\n");
6289 goto out;
6290 }
6291
6292 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6293 phba->sli4_hba.ctrl_regs_memmap_p =
6294 ioremap(phba->pci_bar1_map, bar1map_len);
6295 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6296 dev_printk(KERN_ERR, &pdev->dev,
6297 "ioremap failed for SLI4 HBA control registers.\n");
6298 goto out_iounmap_conf;
6299 }
6300
6301 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6302 phba->sli4_hba.drbl_regs_memmap_p =
6303 ioremap(phba->pci_bar2_map, bar2map_len);
6304 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6305 dev_printk(KERN_ERR, &pdev->dev,
6306 "ioremap failed for SLI4 HBA doorbell registers.\n");
6307 goto out_iounmap_ctrl;
6308 }
6309
6310 /* Set up BAR0 PCI config space register memory map */
6311 lpfc_sli4_bar0_register_memmap(phba);
6312
6313 /* Set up BAR1 register memory map */
6314 lpfc_sli4_bar1_register_memmap(phba);
6315
6316 /* Set up BAR2 register memory map */
6317 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6318 if (error)
6319 goto out_iounmap_all;
6320
6321 return 0;
6322
6323out_iounmap_all:
6324 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6325out_iounmap_ctrl:
6326 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6327out_iounmap_conf:
6328 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6329out:
6330 return error;
6331}
6332
6333/**
6334 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6335 * @phba: pointer to lpfc hba data structure.
6336 *
6337 * This routine is invoked to unset the PCI device memory space for device
6338 * with SLI-4 interface spec.
6339 **/
6340static void
6341lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6342{
6343 struct pci_dev *pdev;
6344
6345 /* Obtain PCI device reference */
6346 if (!phba->pcidev)
6347 return;
6348 else
6349 pdev = phba->pcidev;
6350
6351 /* Free coherent DMA memory allocated */
6352
6353 /* Unmap I/O memory space */
6354 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6355 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6356 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6357
6358 return;
6359}
6360
6361/**
James Smart3772a992009-05-22 14:50:54 -04006362 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6363 * @phba: pointer to lpfc hba data structure.
6364 *
6365 * This routine is invoked to enable the MSI-X interrupt vectors to device
6366 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6367 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6368 * invoked, enables either all or nothing, depending on the current
6369 * availability of PCI vector resources. The device driver is responsible
6370 * for calling the individual request_irq() to register each MSI-X vector
6371 * with a interrupt handler, which is done in this function. Note that
6372 * later when device is unloading, the driver should always call free_irq()
6373 * on all MSI-X vectors it has done request_irq() on before calling
6374 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6375 * will be left with MSI-X enabled and leaks its vectors.
6376 *
6377 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006378 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006379 * other values - error
6380 **/
6381static int
6382lpfc_sli_enable_msix(struct lpfc_hba *phba)
6383{
6384 int rc, i;
6385 LPFC_MBOXQ_t *pmb;
6386
6387 /* Set up MSI-X multi-message vectors */
6388 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6389 phba->msix_entries[i].entry = i;
6390
6391 /* Configure MSI-X capability structure */
6392 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6393 ARRAY_SIZE(phba->msix_entries));
6394 if (rc) {
6395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6396 "0420 PCI enable MSI-X failed (%d)\n", rc);
6397 goto msi_fail_out;
6398 }
6399 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6400 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6401 "0477 MSI-X entry[%d]: vector=x%x "
6402 "message=%d\n", i,
6403 phba->msix_entries[i].vector,
6404 phba->msix_entries[i].entry);
6405 /*
6406 * Assign MSI-X vectors to interrupt handlers
6407 */
6408
6409 /* vector-0 is associated to slow-path handler */
6410 rc = request_irq(phba->msix_entries[0].vector,
6411 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6412 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6413 if (rc) {
6414 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6415 "0421 MSI-X slow-path request_irq failed "
6416 "(%d)\n", rc);
6417 goto msi_fail_out;
6418 }
6419
6420 /* vector-1 is associated to fast-path handler */
6421 rc = request_irq(phba->msix_entries[1].vector,
6422 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6423 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6424
6425 if (rc) {
6426 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6427 "0429 MSI-X fast-path request_irq failed "
6428 "(%d)\n", rc);
6429 goto irq_fail_out;
6430 }
6431
6432 /*
6433 * Configure HBA MSI-X attention conditions to messages
6434 */
6435 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6436
6437 if (!pmb) {
6438 rc = -ENOMEM;
6439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6440 "0474 Unable to allocate memory for issuing "
6441 "MBOX_CONFIG_MSI command\n");
6442 goto mem_fail_out;
6443 }
6444 rc = lpfc_config_msi(phba, pmb);
6445 if (rc)
6446 goto mbx_fail_out;
6447 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6448 if (rc != MBX_SUCCESS) {
6449 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6450 "0351 Config MSI mailbox command failed, "
6451 "mbxCmd x%x, mbxStatus x%x\n",
6452 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6453 goto mbx_fail_out;
6454 }
6455
6456 /* Free memory allocated for mailbox command */
6457 mempool_free(pmb, phba->mbox_mem_pool);
6458 return rc;
6459
6460mbx_fail_out:
6461 /* Free memory allocated for mailbox command */
6462 mempool_free(pmb, phba->mbox_mem_pool);
6463
6464mem_fail_out:
6465 /* free the irq already requested */
6466 free_irq(phba->msix_entries[1].vector, phba);
6467
6468irq_fail_out:
6469 /* free the irq already requested */
6470 free_irq(phba->msix_entries[0].vector, phba);
6471
6472msi_fail_out:
6473 /* Unconfigure MSI-X capability structure */
6474 pci_disable_msix(phba->pcidev);
6475 return rc;
6476}
6477
6478/**
6479 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6480 * @phba: pointer to lpfc hba data structure.
6481 *
6482 * This routine is invoked to release the MSI-X vectors and then disable the
6483 * MSI-X interrupt mode to device with SLI-3 interface spec.
6484 **/
6485static void
6486lpfc_sli_disable_msix(struct lpfc_hba *phba)
6487{
6488 int i;
6489
6490 /* Free up MSI-X multi-message vectors */
6491 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6492 free_irq(phba->msix_entries[i].vector, phba);
6493 /* Disable MSI-X */
6494 pci_disable_msix(phba->pcidev);
6495
6496 return;
6497}
6498
6499/**
6500 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6501 * @phba: pointer to lpfc hba data structure.
6502 *
6503 * This routine is invoked to enable the MSI interrupt mode to device with
6504 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6505 * enable the MSI vector. The device driver is responsible for calling the
6506 * request_irq() to register MSI vector with a interrupt the handler, which
6507 * is done in this function.
6508 *
6509 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006510 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006511 * other values - error
6512 */
6513static int
6514lpfc_sli_enable_msi(struct lpfc_hba *phba)
6515{
6516 int rc;
6517
6518 rc = pci_enable_msi(phba->pcidev);
6519 if (!rc)
6520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6521 "0462 PCI enable MSI mode success.\n");
6522 else {
6523 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6524 "0471 PCI enable MSI mode failed (%d)\n", rc);
6525 return rc;
6526 }
6527
6528 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6529 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6530 if (rc) {
6531 pci_disable_msi(phba->pcidev);
6532 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6533 "0478 MSI request_irq failed (%d)\n", rc);
6534 }
6535 return rc;
6536}
6537
6538/**
6539 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6540 * @phba: pointer to lpfc hba data structure.
6541 *
6542 * This routine is invoked to disable the MSI interrupt mode to device with
6543 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6544 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6545 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6546 * its vector.
6547 */
6548static void
6549lpfc_sli_disable_msi(struct lpfc_hba *phba)
6550{
6551 free_irq(phba->pcidev->irq, phba);
6552 pci_disable_msi(phba->pcidev);
6553 return;
6554}
6555
6556/**
6557 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6558 * @phba: pointer to lpfc hba data structure.
6559 *
6560 * This routine is invoked to enable device interrupt and associate driver's
6561 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6562 * spec. Depends on the interrupt mode configured to the driver, the driver
6563 * will try to fallback from the configured interrupt mode to an interrupt
6564 * mode which is supported by the platform, kernel, and device in the order
6565 * of:
6566 * MSI-X -> MSI -> IRQ.
6567 *
6568 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006569 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006570 * other values - error
6571 **/
6572static uint32_t
6573lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6574{
6575 uint32_t intr_mode = LPFC_INTR_ERROR;
6576 int retval;
6577
6578 if (cfg_mode == 2) {
6579 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6580 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6581 if (!retval) {
6582 /* Now, try to enable MSI-X interrupt mode */
6583 retval = lpfc_sli_enable_msix(phba);
6584 if (!retval) {
6585 /* Indicate initialization to MSI-X mode */
6586 phba->intr_type = MSIX;
6587 intr_mode = 2;
6588 }
6589 }
6590 }
6591
6592 /* Fallback to MSI if MSI-X initialization failed */
6593 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6594 retval = lpfc_sli_enable_msi(phba);
6595 if (!retval) {
6596 /* Indicate initialization to MSI mode */
6597 phba->intr_type = MSI;
6598 intr_mode = 1;
6599 }
6600 }
6601
6602 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6603 if (phba->intr_type == NONE) {
6604 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6605 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6606 if (!retval) {
6607 /* Indicate initialization to INTx mode */
6608 phba->intr_type = INTx;
6609 intr_mode = 0;
6610 }
6611 }
6612 return intr_mode;
6613}
6614
6615/**
6616 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6617 * @phba: pointer to lpfc hba data structure.
6618 *
6619 * This routine is invoked to disable device interrupt and disassociate the
6620 * driver's interrupt handler(s) from interrupt vector(s) to device with
6621 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6622 * release the interrupt vector(s) for the message signaled interrupt.
6623 **/
6624static void
6625lpfc_sli_disable_intr(struct lpfc_hba *phba)
6626{
6627 /* Disable the currently initialized interrupt mode */
6628 if (phba->intr_type == MSIX)
6629 lpfc_sli_disable_msix(phba);
6630 else if (phba->intr_type == MSI)
6631 lpfc_sli_disable_msi(phba);
6632 else if (phba->intr_type == INTx)
6633 free_irq(phba->pcidev->irq, phba);
6634
6635 /* Reset interrupt management states */
6636 phba->intr_type = NONE;
6637 phba->sli.slistat.sli_intr = 0;
6638
6639 return;
6640}
6641
6642/**
James Smartda0436e2009-05-22 14:51:39 -04006643 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6644 * @phba: pointer to lpfc hba data structure.
6645 *
6646 * This routine is invoked to enable the MSI-X interrupt vectors to device
6647 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6648 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6649 * enables either all or nothing, depending on the current availability of
6650 * PCI vector resources. The device driver is responsible for calling the
6651 * individual request_irq() to register each MSI-X vector with a interrupt
6652 * handler, which is done in this function. Note that later when device is
6653 * unloading, the driver should always call free_irq() on all MSI-X vectors
6654 * it has done request_irq() on before calling pci_disable_msix(). Failure
6655 * to do so results in a BUG_ON() and a device will be left with MSI-X
6656 * enabled and leaks its vectors.
6657 *
6658 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006659 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006660 * other values - error
6661 **/
6662static int
6663lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6664{
6665 int rc, index;
6666
6667 /* Set up MSI-X multi-message vectors */
6668 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6669 phba->sli4_hba.msix_entries[index].entry = index;
6670
6671 /* Configure MSI-X capability structure */
6672 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6673 phba->sli4_hba.cfg_eqn);
6674 if (rc) {
6675 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6676 "0484 PCI enable MSI-X failed (%d)\n", rc);
6677 goto msi_fail_out;
6678 }
6679 /* Log MSI-X vector assignment */
6680 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6681 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6682 "0489 MSI-X entry[%d]: vector=x%x "
6683 "message=%d\n", index,
6684 phba->sli4_hba.msix_entries[index].vector,
6685 phba->sli4_hba.msix_entries[index].entry);
6686 /*
6687 * Assign MSI-X vectors to interrupt handlers
6688 */
6689
6690 /* The first vector must associated to slow-path handler for MQ */
6691 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6692 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6693 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6694 if (rc) {
6695 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6696 "0485 MSI-X slow-path request_irq failed "
6697 "(%d)\n", rc);
6698 goto msi_fail_out;
6699 }
6700
6701 /* The rest of the vector(s) are associated to fast-path handler(s) */
6702 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6703 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6704 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6705 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6706 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6707 LPFC_FP_DRIVER_HANDLER_NAME,
6708 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6709 if (rc) {
6710 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6711 "0486 MSI-X fast-path (%d) "
6712 "request_irq failed (%d)\n", index, rc);
6713 goto cfg_fail_out;
6714 }
6715 }
6716
6717 return rc;
6718
6719cfg_fail_out:
6720 /* free the irq already requested */
6721 for (--index; index >= 1; index--)
6722 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6723 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6724
6725 /* free the irq already requested */
6726 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6727
6728msi_fail_out:
6729 /* Unconfigure MSI-X capability structure */
6730 pci_disable_msix(phba->pcidev);
6731 return rc;
6732}
6733
6734/**
6735 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6736 * @phba: pointer to lpfc hba data structure.
6737 *
6738 * This routine is invoked to release the MSI-X vectors and then disable the
6739 * MSI-X interrupt mode to device with SLI-4 interface spec.
6740 **/
6741static void
6742lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6743{
6744 int index;
6745
6746 /* Free up MSI-X multi-message vectors */
6747 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6748
6749 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6750 free_irq(phba->sli4_hba.msix_entries[index].vector,
6751 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6752 /* Disable MSI-X */
6753 pci_disable_msix(phba->pcidev);
6754
6755 return;
6756}
6757
6758/**
6759 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6760 * @phba: pointer to lpfc hba data structure.
6761 *
6762 * This routine is invoked to enable the MSI interrupt mode to device with
6763 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6764 * to enable the MSI vector. The device driver is responsible for calling
6765 * the request_irq() to register MSI vector with a interrupt the handler,
6766 * which is done in this function.
6767 *
6768 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006769 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006770 * other values - error
6771 **/
6772static int
6773lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6774{
6775 int rc, index;
6776
6777 rc = pci_enable_msi(phba->pcidev);
6778 if (!rc)
6779 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6780 "0487 PCI enable MSI mode success.\n");
6781 else {
6782 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6783 "0488 PCI enable MSI mode failed (%d)\n", rc);
6784 return rc;
6785 }
6786
6787 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6788 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6789 if (rc) {
6790 pci_disable_msi(phba->pcidev);
6791 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6792 "0490 MSI request_irq failed (%d)\n", rc);
6793 }
6794
6795 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6796 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6797 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6798 }
6799
6800 return rc;
6801}
6802
6803/**
6804 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6805 * @phba: pointer to lpfc hba data structure.
6806 *
6807 * This routine is invoked to disable the MSI interrupt mode to device with
6808 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6809 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6810 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6811 * its vector.
6812 **/
6813static void
6814lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6815{
6816 free_irq(phba->pcidev->irq, phba);
6817 pci_disable_msi(phba->pcidev);
6818 return;
6819}
6820
6821/**
6822 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6823 * @phba: pointer to lpfc hba data structure.
6824 *
6825 * This routine is invoked to enable device interrupt and associate driver's
6826 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6827 * interface spec. Depends on the interrupt mode configured to the driver,
6828 * the driver will try to fallback from the configured interrupt mode to an
6829 * interrupt mode which is supported by the platform, kernel, and device in
6830 * the order of:
6831 * MSI-X -> MSI -> IRQ.
6832 *
6833 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006834 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006835 * other values - error
6836 **/
6837static uint32_t
6838lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6839{
6840 uint32_t intr_mode = LPFC_INTR_ERROR;
6841 int retval, index;
6842
6843 if (cfg_mode == 2) {
6844 /* Preparation before conf_msi mbox cmd */
6845 retval = 0;
6846 if (!retval) {
6847 /* Now, try to enable MSI-X interrupt mode */
6848 retval = lpfc_sli4_enable_msix(phba);
6849 if (!retval) {
6850 /* Indicate initialization to MSI-X mode */
6851 phba->intr_type = MSIX;
6852 intr_mode = 2;
6853 }
6854 }
6855 }
6856
6857 /* Fallback to MSI if MSI-X initialization failed */
6858 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6859 retval = lpfc_sli4_enable_msi(phba);
6860 if (!retval) {
6861 /* Indicate initialization to MSI mode */
6862 phba->intr_type = MSI;
6863 intr_mode = 1;
6864 }
6865 }
6866
6867 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6868 if (phba->intr_type == NONE) {
6869 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6870 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6871 if (!retval) {
6872 /* Indicate initialization to INTx mode */
6873 phba->intr_type = INTx;
6874 intr_mode = 0;
6875 for (index = 0; index < phba->cfg_fcp_eq_count;
6876 index++) {
6877 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6878 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6879 }
6880 }
6881 }
6882 return intr_mode;
6883}
6884
6885/**
6886 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6887 * @phba: pointer to lpfc hba data structure.
6888 *
6889 * This routine is invoked to disable device interrupt and disassociate
6890 * the driver's interrupt handler(s) from interrupt vector(s) to device
6891 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6892 * will release the interrupt vector(s) for the message signaled interrupt.
6893 **/
6894static void
6895lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6896{
6897 /* Disable the currently initialized interrupt mode */
6898 if (phba->intr_type == MSIX)
6899 lpfc_sli4_disable_msix(phba);
6900 else if (phba->intr_type == MSI)
6901 lpfc_sli4_disable_msi(phba);
6902 else if (phba->intr_type == INTx)
6903 free_irq(phba->pcidev->irq, phba);
6904
6905 /* Reset interrupt management states */
6906 phba->intr_type = NONE;
6907 phba->sli.slistat.sli_intr = 0;
6908
6909 return;
6910}
6911
6912/**
James Smart3772a992009-05-22 14:50:54 -04006913 * lpfc_unset_hba - Unset SLI3 hba device initialization
6914 * @phba: pointer to lpfc hba data structure.
6915 *
6916 * This routine is invoked to unset the HBA device initialization steps to
6917 * a device with SLI-3 interface spec.
6918 **/
6919static void
6920lpfc_unset_hba(struct lpfc_hba *phba)
6921{
6922 struct lpfc_vport *vport = phba->pport;
6923 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6924
6925 spin_lock_irq(shost->host_lock);
6926 vport->load_flag |= FC_UNLOADING;
6927 spin_unlock_irq(shost->host_lock);
6928
6929 lpfc_stop_hba_timers(phba);
6930
6931 phba->pport->work_port_events = 0;
6932
6933 lpfc_sli_hba_down(phba);
6934
6935 lpfc_sli_brdrestart(phba);
6936
6937 lpfc_sli_disable_intr(phba);
6938
6939 return;
6940}
6941
6942/**
James Smartda0436e2009-05-22 14:51:39 -04006943 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6944 * @phba: pointer to lpfc hba data structure.
6945 *
6946 * This routine is invoked to unset the HBA device initialization steps to
6947 * a device with SLI-4 interface spec.
6948 **/
6949static void
6950lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6951{
6952 struct lpfc_vport *vport = phba->pport;
6953 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6954
6955 spin_lock_irq(shost->host_lock);
6956 vport->load_flag |= FC_UNLOADING;
6957 spin_unlock_irq(shost->host_lock);
6958
6959 phba->pport->work_port_events = 0;
6960
6961 lpfc_sli4_hba_down(phba);
6962
6963 lpfc_sli4_disable_intr(phba);
6964
6965 return;
6966}
6967
6968/**
6969 * lpfc_sli4_hba_unset - Unset the fcoe hba
6970 * @phba: Pointer to HBA context object.
6971 *
6972 * This function is called in the SLI4 code path to reset the HBA's FCoE
6973 * function. The caller is not required to hold any lock. This routine
6974 * issues PCI function reset mailbox command to reset the FCoE function.
6975 * At the end of the function, it calls lpfc_hba_down_post function to
6976 * free any pending commands.
6977 **/
6978static void
6979lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6980{
6981 int wait_cnt = 0;
6982 LPFC_MBOXQ_t *mboxq;
6983
6984 lpfc_stop_hba_timers(phba);
6985 phba->sli4_hba.intr_enable = 0;
6986
6987 /*
6988 * Gracefully wait out the potential current outstanding asynchronous
6989 * mailbox command.
6990 */
6991
6992 /* First, block any pending async mailbox command from posted */
6993 spin_lock_irq(&phba->hbalock);
6994 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6995 spin_unlock_irq(&phba->hbalock);
6996 /* Now, trying to wait it out if we can */
6997 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6998 msleep(10);
6999 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7000 break;
7001 }
7002 /* Forcefully release the outstanding mailbox command if timed out */
7003 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7004 spin_lock_irq(&phba->hbalock);
7005 mboxq = phba->sli.mbox_active;
7006 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7007 __lpfc_mbox_cmpl_put(phba, mboxq);
7008 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7009 phba->sli.mbox_active = NULL;
7010 spin_unlock_irq(&phba->hbalock);
7011 }
7012
7013 /* Tear down the queues in the HBA */
7014 lpfc_sli4_queue_unset(phba);
7015
7016 /* Disable PCI subsystem interrupt */
7017 lpfc_sli4_disable_intr(phba);
7018
7019 /* Stop kthread signal shall trigger work_done one more time */
7020 kthread_stop(phba->worker_thread);
7021
7022 /* Stop the SLI4 device port */
7023 phba->pport->work_port_events = 0;
7024}
7025
James Smart28baac72010-02-12 14:42:03 -05007026 /**
7027 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7028 * @phba: Pointer to HBA context object.
7029 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7030 *
7031 * This function is called in the SLI4 code path to read the port's
7032 * sli4 capabilities.
7033 *
7034 * This function may be be called from any context that can block-wait
7035 * for the completion. The expectation is that this routine is called
7036 * typically from probe_one or from the online routine.
7037 **/
7038int
7039lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7040{
7041 int rc;
7042 struct lpfc_mqe *mqe;
7043 struct lpfc_pc_sli4_params *sli4_params;
7044 uint32_t mbox_tmo;
7045
7046 rc = 0;
7047 mqe = &mboxq->u.mqe;
7048
7049 /* Read the port's SLI4 Parameters port capabilities */
7050 lpfc_sli4_params(mboxq);
7051 if (!phba->sli4_hba.intr_enable)
7052 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7053 else {
7054 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7055 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7056 }
7057
7058 if (unlikely(rc))
7059 return 1;
7060
7061 sli4_params = &phba->sli4_hba.pc_sli4_params;
7062 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7063 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7064 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7065 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7066 &mqe->un.sli4_params);
7067 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7068 &mqe->un.sli4_params);
7069 sli4_params->proto_types = mqe->un.sli4_params.word3;
7070 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7071 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7072 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7073 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7074 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7075 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7076 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7077 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7078 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7079 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7080 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7081 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7082 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7083 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7084 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7085 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7086 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7087 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7088 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7089 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7090 return rc;
7091}
7092
James Smartda0436e2009-05-22 14:51:39 -04007093/**
James Smart3772a992009-05-22 14:50:54 -04007094 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7095 * @pdev: pointer to PCI device
7096 * @pid: pointer to PCI device identifier
7097 *
7098 * This routine is to be called to attach a device with SLI-3 interface spec
7099 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7100 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7101 * information of the device and driver to see if the driver state that it can
7102 * support this kind of device. If the match is successful, the driver core
7103 * invokes this routine. If this routine determines it can claim the HBA, it
7104 * does all the initialization that it needs to do to handle the HBA properly.
7105 *
7106 * Return code
7107 * 0 - driver can claim the device
7108 * negative value - driver can not claim the device
7109 **/
7110static int __devinit
7111lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7112{
7113 struct lpfc_hba *phba;
7114 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007115 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007116 int error;
7117 uint32_t cfg_mode, intr_mode;
7118
7119 /* Allocate memory for HBA structure */
7120 phba = lpfc_hba_alloc(pdev);
7121 if (!phba)
7122 return -ENOMEM;
7123
7124 /* Perform generic PCI device enabling operation */
7125 error = lpfc_enable_pci_dev(phba);
7126 if (error) {
7127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7128 "1401 Failed to enable pci device.\n");
7129 goto out_free_phba;
7130 }
7131
7132 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7133 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7134 if (error)
7135 goto out_disable_pci_dev;
7136
7137 /* Set up SLI-3 specific device PCI memory space */
7138 error = lpfc_sli_pci_mem_setup(phba);
7139 if (error) {
7140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7141 "1402 Failed to set up pci memory space.\n");
7142 goto out_disable_pci_dev;
7143 }
7144
7145 /* Set up phase-1 common device driver resources */
7146 error = lpfc_setup_driver_resource_phase1(phba);
7147 if (error) {
7148 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7149 "1403 Failed to set up driver resource.\n");
7150 goto out_unset_pci_mem_s3;
7151 }
7152
7153 /* Set up SLI-3 specific device driver resources */
7154 error = lpfc_sli_driver_resource_setup(phba);
7155 if (error) {
7156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7157 "1404 Failed to set up driver resource.\n");
7158 goto out_unset_pci_mem_s3;
7159 }
7160
7161 /* Initialize and populate the iocb list per host */
7162 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7163 if (error) {
7164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7165 "1405 Failed to initialize iocb list.\n");
7166 goto out_unset_driver_resource_s3;
7167 }
7168
7169 /* Set up common device driver resources */
7170 error = lpfc_setup_driver_resource_phase2(phba);
7171 if (error) {
7172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7173 "1406 Failed to set up driver resource.\n");
7174 goto out_free_iocb_list;
7175 }
7176
7177 /* Create SCSI host to the physical port */
7178 error = lpfc_create_shost(phba);
7179 if (error) {
7180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7181 "1407 Failed to create scsi host.\n");
7182 goto out_unset_driver_resource;
7183 }
7184
7185 /* Configure sysfs attributes */
7186 vport = phba->pport;
7187 error = lpfc_alloc_sysfs_attr(vport);
7188 if (error) {
7189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7190 "1476 Failed to allocate sysfs attr\n");
7191 goto out_destroy_shost;
7192 }
7193
James Smart6669f9b2009-10-02 15:16:45 -04007194 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04007195 /* Now, trying to enable interrupt and bring up the device */
7196 cfg_mode = phba->cfg_use_msi;
7197 while (true) {
7198 /* Put device to a known state before enabling interrupt */
7199 lpfc_stop_port(phba);
7200 /* Configure and enable interrupt */
7201 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7202 if (intr_mode == LPFC_INTR_ERROR) {
7203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7204 "0431 Failed to enable interrupt.\n");
7205 error = -ENODEV;
7206 goto out_free_sysfs_attr;
7207 }
7208 /* SLI-3 HBA setup */
7209 if (lpfc_sli_hba_setup(phba)) {
7210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7211 "1477 Failed to set up hba\n");
7212 error = -ENODEV;
7213 goto out_remove_device;
7214 }
7215
7216 /* Wait 50ms for the interrupts of previous mailbox commands */
7217 msleep(50);
7218 /* Check active interrupts on message signaled interrupts */
7219 if (intr_mode == 0 ||
7220 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7221 /* Log the current active interrupt mode */
7222 phba->intr_mode = intr_mode;
7223 lpfc_log_intr_mode(phba, intr_mode);
7224 break;
7225 } else {
7226 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7227 "0447 Configure interrupt mode (%d) "
7228 "failed active interrupt test.\n",
7229 intr_mode);
7230 /* Disable the current interrupt mode */
7231 lpfc_sli_disable_intr(phba);
7232 /* Try next level of interrupt mode */
7233 cfg_mode = --intr_mode;
7234 }
7235 }
7236
7237 /* Perform post initialization setup */
7238 lpfc_post_init_setup(phba);
7239
7240 /* Check if there are static vports to be created. */
7241 lpfc_create_static_vport(phba);
7242
7243 return 0;
7244
7245out_remove_device:
7246 lpfc_unset_hba(phba);
7247out_free_sysfs_attr:
7248 lpfc_free_sysfs_attr(vport);
7249out_destroy_shost:
7250 lpfc_destroy_shost(phba);
7251out_unset_driver_resource:
7252 lpfc_unset_driver_resource_phase2(phba);
7253out_free_iocb_list:
7254 lpfc_free_iocb_list(phba);
7255out_unset_driver_resource_s3:
7256 lpfc_sli_driver_resource_unset(phba);
7257out_unset_pci_mem_s3:
7258 lpfc_sli_pci_mem_unset(phba);
7259out_disable_pci_dev:
7260 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007261 if (shost)
7262 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007263out_free_phba:
7264 lpfc_hba_free(phba);
7265 return error;
7266}
7267
7268/**
7269 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007270 * @pdev: pointer to PCI device
7271 *
James Smart3772a992009-05-22 14:50:54 -04007272 * This routine is to be called to disattach a device with SLI-3 interface
7273 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7274 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7275 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007276 **/
dea31012005-04-17 16:05:31 -05007277static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007278lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007279{
James Smart2e0fef82007-06-17 19:56:36 -05007280 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7281 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007282 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007283 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007284 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007285 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7286
James Smart549e55c2007-08-02 11:09:51 -04007287 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007288 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007289 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007290
James Smart858c9f62007-06-17 19:56:39 -05007291 lpfc_free_sysfs_attr(vport);
7292
James Smarteada2722008-12-04 22:39:13 -05007293 /* Release all the vports against this physical port */
7294 vports = lpfc_create_vport_work_array(phba);
7295 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007296 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007297 fc_vport_terminate(vports[i]->fc_vport);
7298 lpfc_destroy_vport_work_array(phba, vports);
7299
7300 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007301 fc_remove_host(shost);
7302 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007303 lpfc_cleanup(vport);
7304
James Smart2e0fef82007-06-17 19:56:36 -05007305 /*
7306 * Bring down the SLI Layer. This step disable all interrupts,
7307 * clears the rings, discards all mailbox commands, and resets
7308 * the HBA.
7309 */
James Smarta257bf92009-04-06 18:48:10 -04007310
7311 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007312 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007313 /* Stop kthread signal shall trigger work_done one more time */
7314 kthread_stop(phba->worker_thread);
7315 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007316 lpfc_sli_brdrestart(phba);
7317
James Smart3772a992009-05-22 14:50:54 -04007318 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007319 spin_lock_irq(&phba->hbalock);
7320 list_del_init(&vport->listentry);
7321 spin_unlock_irq(&phba->hbalock);
7322
James Smart858c9f62007-06-17 19:56:39 -05007323 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007324
James Smart5b75da22008-12-04 22:39:35 -05007325 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007326 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007327
7328 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007329 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007330
7331 /*
7332 * Call scsi_free before mem_free since scsi bufs are released to their
7333 * corresponding pools here.
7334 */
7335 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007336 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007337
James Smart34b02dc2008-08-24 21:49:55 -04007338 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7339 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007340
James Smart2e0fef82007-06-17 19:56:36 -05007341 /* Free resources associated with SLI2 interface */
7342 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007343 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007344
7345 /* unmap adapter SLIM and Control Registers */
7346 iounmap(phba->ctrl_regs_memmap_p);
7347 iounmap(phba->slim_memmap_p);
7348
James Smart3772a992009-05-22 14:50:54 -04007349 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007350
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007351 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007352 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007353}
7354
Linas Vepstas8d63f372007-02-14 14:28:36 -06007355/**
James Smart3772a992009-05-22 14:50:54 -04007356 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007357 * @pdev: pointer to PCI device
7358 * @msg: power management message
7359 *
James Smart3772a992009-05-22 14:50:54 -04007360 * This routine is to be called from the kernel's PCI subsystem to support
7361 * system Power Management (PM) to device with SLI-3 interface spec. When
7362 * PM invokes this method, it quiesces the device by stopping the driver's
7363 * worker thread for the device, turning off device's interrupt and DMA,
7364 * and bring the device offline. Note that as the driver implements the
7365 * minimum PM requirements to a power-aware driver's PM support for the
7366 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7367 * to the suspend() method call will be treated as SUSPEND and the driver will
7368 * fully reinitialize its device during resume() method call, the driver will
7369 * set device to PCI_D3hot state in PCI config space instead of setting it
7370 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007371 *
7372 * Return code
James Smart3772a992009-05-22 14:50:54 -04007373 * 0 - driver suspended the device
7374 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007375 **/
7376static int
James Smart3772a992009-05-22 14:50:54 -04007377lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007378{
7379 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7380 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7381
7382 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7383 "0473 PCI device Power Management suspend.\n");
7384
7385 /* Bring down the device */
7386 lpfc_offline_prep(phba);
7387 lpfc_offline(phba);
7388 kthread_stop(phba->worker_thread);
7389
7390 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007391 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007392
7393 /* Save device state to PCI config space */
7394 pci_save_state(pdev);
7395 pci_set_power_state(pdev, PCI_D3hot);
7396
7397 return 0;
7398}
7399
7400/**
James Smart3772a992009-05-22 14:50:54 -04007401 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007402 * @pdev: pointer to PCI device
7403 *
James Smart3772a992009-05-22 14:50:54 -04007404 * This routine is to be called from the kernel's PCI subsystem to support
7405 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7406 * invokes this method, it restores the device's PCI config space state and
7407 * fully reinitializes the device and brings it online. Note that as the
7408 * driver implements the minimum PM requirements to a power-aware driver's
7409 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7410 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7411 * driver will fully reinitialize its device during resume() method call,
7412 * the device will be set to PCI_D0 directly in PCI config space before
7413 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007414 *
7415 * Return code
James Smart3772a992009-05-22 14:50:54 -04007416 * 0 - driver suspended the device
7417 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007418 **/
7419static int
James Smart3772a992009-05-22 14:50:54 -04007420lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007421{
7422 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7423 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007424 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007425 int error;
7426
7427 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7428 "0452 PCI device Power Management resume.\n");
7429
7430 /* Restore device state from PCI config space */
7431 pci_set_power_state(pdev, PCI_D0);
7432 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007433
James Smart1dfb5a42010-02-12 14:40:50 -05007434 /*
7435 * As the new kernel behavior of pci_restore_state() API call clears
7436 * device saved_state flag, need to save the restored state again.
7437 */
7438 pci_save_state(pdev);
7439
James Smart3a55b532008-12-04 22:38:54 -05007440 if (pdev->is_busmaster)
7441 pci_set_master(pdev);
7442
7443 /* Startup the kernel thread for this host adapter. */
7444 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7445 "lpfc_worker_%d", phba->brd_no);
7446 if (IS_ERR(phba->worker_thread)) {
7447 error = PTR_ERR(phba->worker_thread);
7448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7449 "0434 PM resume failed to start worker "
7450 "thread: error=x%x.\n", error);
7451 return error;
7452 }
7453
James Smart5b75da22008-12-04 22:39:35 -05007454 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007455 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007456 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007458 "0430 PM resume Failed to enable interrupt\n");
7459 return -EIO;
7460 } else
7461 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007462
7463 /* Restart HBA and bring it online */
7464 lpfc_sli_brdrestart(phba);
7465 lpfc_online(phba);
7466
James Smart5b75da22008-12-04 22:39:35 -05007467 /* Log the current active interrupt mode */
7468 lpfc_log_intr_mode(phba, phba->intr_mode);
7469
James Smart3a55b532008-12-04 22:38:54 -05007470 return 0;
7471}
7472
7473/**
James Smart891478a2009-11-18 15:40:23 -05007474 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7475 * @phba: pointer to lpfc hba data structure.
7476 *
7477 * This routine is called to prepare the SLI3 device for PCI slot recover. It
7478 * aborts and stops all the on-going I/Os on the pci device.
7479 **/
7480static void
7481lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7482{
7483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7484 "2723 PCI channel I/O abort preparing for recovery\n");
7485 /* Prepare for bringing HBA offline */
7486 lpfc_offline_prep(phba);
7487 /* Clear sli active flag to prevent sysfs access to HBA */
7488 spin_lock_irq(&phba->hbalock);
7489 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7490 spin_unlock_irq(&phba->hbalock);
7491 /* Stop and flush all I/Os and bring HBA offline */
7492 lpfc_offline(phba);
7493}
7494
7495/**
James Smart0d878412009-10-02 15:16:56 -04007496 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7497 * @phba: pointer to lpfc hba data structure.
7498 *
7499 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7500 * disables the device interrupt and pci device, and aborts the internal FCP
7501 * pending I/Os.
7502 **/
7503static void
7504lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7505{
7506 struct lpfc_sli *psli = &phba->sli;
7507 struct lpfc_sli_ring *pring;
7508
7509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007510 "2710 PCI channel disable preparing for reset\n");
James Smart0d878412009-10-02 15:16:56 -04007511 /* Disable interrupt and pci device */
7512 lpfc_sli_disable_intr(phba);
7513 pci_disable_device(phba->pcidev);
7514 /*
7515 * There may be I/Os dropped by the firmware.
7516 * Error iocb (I/O) on txcmplq and let the SCSI layer
7517 * retry it after re-establishing link.
7518 */
7519 pring = &psli->ring[psli->fcp_ring];
7520 lpfc_sli_abort_iocb_ring(phba, pring);
7521}
7522
7523/**
7524 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7525 * @phba: pointer to lpfc hba data structure.
7526 *
7527 * This routine is called to prepare the SLI3 device for PCI slot permanently
7528 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7529 * pending I/Os.
7530 **/
7531static void
7532lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7533{
7534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007535 "2711 PCI channel permanent disable for failure\n");
James Smart0d878412009-10-02 15:16:56 -04007536 /* Clean up all driver's outstanding SCSI I/Os */
7537 lpfc_sli_flush_fcp_rings(phba);
7538}
7539
7540/**
James Smart3772a992009-05-22 14:50:54 -04007541 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007542 * @pdev: pointer to PCI device.
7543 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007544 *
James Smart3772a992009-05-22 14:50:54 -04007545 * This routine is called from the PCI subsystem for I/O error handling to
7546 * device with SLI-3 interface spec. This function is called by the PCI
7547 * subsystem after a PCI bus error affecting this device has been detected.
7548 * When this function is invoked, it will need to stop all the I/Os and
7549 * interrupt(s) to the device. Once that is done, it will return
7550 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7551 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007552 *
7553 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007554 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007555 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7556 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007557 **/
James Smart3772a992009-05-22 14:50:54 -04007558static pci_ers_result_t
7559lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
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 Smartaacc20e2009-12-21 17:02:51 -05007564 /* Block all SCSI devices' I/Os on the host */
7565 lpfc_scsi_dev_block(phba);
7566
James Smart0d878412009-10-02 15:16:56 -04007567 switch (state) {
7568 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007569 /* Non-fatal error, prepare for recovery */
7570 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007571 return PCI_ERS_RESULT_CAN_RECOVER;
7572 case pci_channel_io_frozen:
7573 /* Fatal error, prepare for slot reset */
7574 lpfc_sli_prep_dev_for_reset(phba);
7575 return PCI_ERS_RESULT_NEED_RESET;
7576 case pci_channel_io_perm_failure:
7577 /* Permanent failure, prepare for device down */
7578 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007579 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007580 default:
7581 /* Unknown state, prepare and request slot reset */
7582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7583 "0472 Unknown PCI error state: x%x\n", state);
7584 lpfc_sli_prep_dev_for_reset(phba);
7585 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007586 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007587}
7588
7589/**
James Smart3772a992009-05-22 14:50:54 -04007590 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007591 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007592 *
James Smart3772a992009-05-22 14:50:54 -04007593 * This routine is called from the PCI subsystem for error handling to
7594 * device with SLI-3 interface spec. This is called after PCI bus has been
7595 * reset to restart the PCI card from scratch, as if from a cold-boot.
7596 * During the PCI subsystem error recovery, after driver returns
7597 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7598 * recovery and then call this routine before calling the .resume method
7599 * to recover the device. This function will initialize the HBA device,
7600 * enable the interrupt, but it will just put the HBA to offline state
7601 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007602 *
7603 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007604 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7605 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007606 */
James Smart3772a992009-05-22 14:50:54 -04007607static pci_ers_result_t
7608lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007609{
James Smart51ef4c22007-08-02 11:10:31 -04007610 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7611 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007612 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007613 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007614
7615 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007616 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007617 printk(KERN_ERR "lpfc: Cannot re-enable "
7618 "PCI device after reset.\n");
7619 return PCI_ERS_RESULT_DISCONNECT;
7620 }
7621
James Smart97207482008-12-04 22:39:19 -05007622 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05007623
7624 /*
7625 * As the new kernel behavior of pci_restore_state() API call clears
7626 * device saved_state flag, need to save the restored state again.
7627 */
7628 pci_save_state(pdev);
7629
James Smart97207482008-12-04 22:39:19 -05007630 if (pdev->is_busmaster)
7631 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007632
James Smart92d7f7b2007-06-17 19:56:38 -05007633 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007634 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007635 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007636
James Smart5b75da22008-12-04 22:39:35 -05007637 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007638 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007639 if (intr_mode == LPFC_INTR_ERROR) {
7640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7641 "0427 Cannot re-enable interrupt after "
7642 "slot reset.\n");
7643 return PCI_ERS_RESULT_DISCONNECT;
7644 } else
7645 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007646
7647 /* Take device offline; this will perform cleanup */
7648 lpfc_offline(phba);
7649 lpfc_sli_brdrestart(phba);
7650
James Smart5b75da22008-12-04 22:39:35 -05007651 /* Log the current active interrupt mode */
7652 lpfc_log_intr_mode(phba, phba->intr_mode);
7653
Linas Vepstas8d63f372007-02-14 14:28:36 -06007654 return PCI_ERS_RESULT_RECOVERED;
7655}
7656
7657/**
James Smart3772a992009-05-22 14:50:54 -04007658 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007659 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007660 *
James Smart3772a992009-05-22 14:50:54 -04007661 * This routine is called from the PCI subsystem for error handling to device
7662 * with SLI-3 interface spec. It is called when kernel error recovery tells
7663 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7664 * error recovery. After this call, traffic can start to flow from this device
7665 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007666 */
James Smart3772a992009-05-22 14:50:54 -04007667static void
7668lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007669{
James Smart51ef4c22007-08-02 11:10:31 -04007670 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7671 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007672
James Smart0d878412009-10-02 15:16:56 -04007673 /* Bring the device online */
James Smart58da1ff2008-04-07 10:15:56 -04007674 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04007675
7676 /* Clean up Advanced Error Reporting (AER) if needed */
7677 if (phba->hba_flag & HBA_AER_ENABLED)
7678 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007679}
7680
James Smart3772a992009-05-22 14:50:54 -04007681/**
James Smartda0436e2009-05-22 14:51:39 -04007682 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7683 * @phba: pointer to lpfc hba data structure.
7684 *
7685 * returns the number of ELS/CT IOCBs to reserve
7686 **/
7687int
7688lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7689{
7690 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7691
James Smartf1126682009-06-10 17:22:44 -04007692 if (phba->sli_rev == LPFC_SLI_REV4) {
7693 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04007694 return 10;
James Smartf1126682009-06-10 17:22:44 -04007695 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04007696 return 25;
James Smartf1126682009-06-10 17:22:44 -04007697 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04007698 return 50;
James Smartf1126682009-06-10 17:22:44 -04007699 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04007700 return 100;
James Smartf1126682009-06-10 17:22:44 -04007701 else
James Smart6a9c52c2009-10-02 15:16:51 -04007702 return 150;
James Smartf1126682009-06-10 17:22:44 -04007703 } else
7704 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007705}
7706
7707/**
7708 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7709 * @pdev: pointer to PCI device
7710 * @pid: pointer to PCI device identifier
7711 *
7712 * This routine is called from the kernel's PCI subsystem to device with
7713 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7714 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7715 * information of the device and driver to see if the driver state that it
7716 * can support this kind of device. If the match is successful, the driver
7717 * core invokes this routine. If this routine determines it can claim the HBA,
7718 * it does all the initialization that it needs to do to handle the HBA
7719 * properly.
7720 *
7721 * Return code
7722 * 0 - driver can claim the device
7723 * negative value - driver can not claim the device
7724 **/
7725static int __devinit
7726lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7727{
7728 struct lpfc_hba *phba;
7729 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007730 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007731 int error;
7732 uint32_t cfg_mode, intr_mode;
7733 int mcnt;
7734
7735 /* Allocate memory for HBA structure */
7736 phba = lpfc_hba_alloc(pdev);
7737 if (!phba)
7738 return -ENOMEM;
7739
7740 /* Perform generic PCI device enabling operation */
7741 error = lpfc_enable_pci_dev(phba);
7742 if (error) {
7743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7744 "1409 Failed to enable pci device.\n");
7745 goto out_free_phba;
7746 }
7747
7748 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7749 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7750 if (error)
7751 goto out_disable_pci_dev;
7752
7753 /* Set up SLI-4 specific device PCI memory space */
7754 error = lpfc_sli4_pci_mem_setup(phba);
7755 if (error) {
7756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7757 "1410 Failed to set up pci memory space.\n");
7758 goto out_disable_pci_dev;
7759 }
7760
7761 /* Set up phase-1 common device driver resources */
7762 error = lpfc_setup_driver_resource_phase1(phba);
7763 if (error) {
7764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7765 "1411 Failed to set up driver resource.\n");
7766 goto out_unset_pci_mem_s4;
7767 }
7768
7769 /* Set up SLI-4 Specific device driver resources */
7770 error = lpfc_sli4_driver_resource_setup(phba);
7771 if (error) {
7772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7773 "1412 Failed to set up driver resource.\n");
7774 goto out_unset_pci_mem_s4;
7775 }
7776
7777 /* Initialize and populate the iocb list per host */
7778 error = lpfc_init_iocb_list(phba,
7779 phba->sli4_hba.max_cfg_param.max_xri);
7780 if (error) {
7781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7782 "1413 Failed to initialize iocb list.\n");
7783 goto out_unset_driver_resource_s4;
7784 }
7785
7786 /* Set up common device driver resources */
7787 error = lpfc_setup_driver_resource_phase2(phba);
7788 if (error) {
7789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7790 "1414 Failed to set up driver resource.\n");
7791 goto out_free_iocb_list;
7792 }
7793
7794 /* Create SCSI host to the physical port */
7795 error = lpfc_create_shost(phba);
7796 if (error) {
7797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7798 "1415 Failed to create scsi host.\n");
7799 goto out_unset_driver_resource;
7800 }
7801
7802 /* Configure sysfs attributes */
7803 vport = phba->pport;
7804 error = lpfc_alloc_sysfs_attr(vport);
7805 if (error) {
7806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7807 "1416 Failed to allocate sysfs attr\n");
7808 goto out_destroy_shost;
7809 }
7810
James Smart6669f9b2009-10-02 15:16:45 -04007811 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04007812 /* Now, trying to enable interrupt and bring up the device */
7813 cfg_mode = phba->cfg_use_msi;
7814 while (true) {
7815 /* Put device to a known state before enabling interrupt */
7816 lpfc_stop_port(phba);
7817 /* Configure and enable interrupt */
7818 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7819 if (intr_mode == LPFC_INTR_ERROR) {
7820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7821 "0426 Failed to enable interrupt.\n");
7822 error = -ENODEV;
7823 goto out_free_sysfs_attr;
7824 }
James Smartdef9c7a2009-12-21 17:02:28 -05007825 /* Default to single FCP EQ for non-MSI-X */
7826 if (phba->intr_type != MSIX)
7827 phba->cfg_fcp_eq_count = 1;
James Smartda0436e2009-05-22 14:51:39 -04007828 /* Set up SLI-4 HBA */
7829 if (lpfc_sli4_hba_setup(phba)) {
7830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7831 "1421 Failed to set up hba\n");
7832 error = -ENODEV;
7833 goto out_disable_intr;
7834 }
7835
7836 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7837 if (intr_mode != 0)
7838 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7839 LPFC_ACT_INTR_CNT);
7840
7841 /* Check active interrupts received only for MSI/MSI-X */
7842 if (intr_mode == 0 ||
7843 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7844 /* Log the current active interrupt mode */
7845 phba->intr_mode = intr_mode;
7846 lpfc_log_intr_mode(phba, intr_mode);
7847 break;
7848 }
7849 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7850 "0451 Configure interrupt mode (%d) "
7851 "failed active interrupt test.\n",
7852 intr_mode);
7853 /* Unset the preivous SLI-4 HBA setup */
7854 lpfc_sli4_unset_hba(phba);
7855 /* Try next level of interrupt mode */
7856 cfg_mode = --intr_mode;
7857 }
7858
7859 /* Perform post initialization setup */
7860 lpfc_post_init_setup(phba);
7861
James Smart1c6834a2009-07-19 10:01:26 -04007862 /* Check if there are static vports to be created. */
7863 lpfc_create_static_vport(phba);
7864
James Smartda0436e2009-05-22 14:51:39 -04007865 return 0;
7866
7867out_disable_intr:
7868 lpfc_sli4_disable_intr(phba);
7869out_free_sysfs_attr:
7870 lpfc_free_sysfs_attr(vport);
7871out_destroy_shost:
7872 lpfc_destroy_shost(phba);
7873out_unset_driver_resource:
7874 lpfc_unset_driver_resource_phase2(phba);
7875out_free_iocb_list:
7876 lpfc_free_iocb_list(phba);
7877out_unset_driver_resource_s4:
7878 lpfc_sli4_driver_resource_unset(phba);
7879out_unset_pci_mem_s4:
7880 lpfc_sli4_pci_mem_unset(phba);
7881out_disable_pci_dev:
7882 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007883 if (shost)
7884 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04007885out_free_phba:
7886 lpfc_hba_free(phba);
7887 return error;
7888}
7889
7890/**
7891 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
7892 * @pdev: pointer to PCI device
7893 *
7894 * This routine is called from the kernel's PCI subsystem to device with
7895 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7896 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7897 * device to be removed from the PCI subsystem properly.
7898 **/
7899static void __devexit
7900lpfc_pci_remove_one_s4(struct pci_dev *pdev)
7901{
7902 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7903 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7904 struct lpfc_vport **vports;
7905 struct lpfc_hba *phba = vport->phba;
7906 int i;
7907
7908 /* Mark the device unloading flag */
7909 spin_lock_irq(&phba->hbalock);
7910 vport->load_flag |= FC_UNLOADING;
7911 spin_unlock_irq(&phba->hbalock);
7912
7913 /* Free the HBA sysfs attributes */
7914 lpfc_free_sysfs_attr(vport);
7915
7916 /* Release all the vports against this physical port */
7917 vports = lpfc_create_vport_work_array(phba);
7918 if (vports != NULL)
7919 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7920 fc_vport_terminate(vports[i]->fc_vport);
7921 lpfc_destroy_vport_work_array(phba, vports);
7922
7923 /* Remove FC host and then SCSI host with the physical port */
7924 fc_remove_host(shost);
7925 scsi_remove_host(shost);
7926
7927 /* Perform cleanup on the physical port */
7928 lpfc_cleanup(vport);
7929
7930 /*
7931 * Bring down the SLI Layer. This step disables all interrupts,
7932 * clears the rings, discards all mailbox commands, and resets
7933 * the HBA FCoE function.
7934 */
7935 lpfc_debugfs_terminate(vport);
7936 lpfc_sli4_hba_unset(phba);
7937
7938 spin_lock_irq(&phba->hbalock);
7939 list_del_init(&vport->listentry);
7940 spin_unlock_irq(&phba->hbalock);
7941
7942 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7943 * buffers are released to their corresponding pools here.
7944 */
7945 lpfc_scsi_free(phba);
7946 lpfc_sli4_driver_resource_unset(phba);
7947
7948 /* Unmap adapter Control and Doorbell registers */
7949 lpfc_sli4_pci_mem_unset(phba);
7950
7951 /* Release PCI resources and disable device's PCI function */
7952 scsi_host_put(shost);
7953 lpfc_disable_pci_dev(phba);
7954
7955 /* Finally, free the driver's device data structure */
7956 lpfc_hba_free(phba);
7957
7958 return;
7959}
7960
7961/**
7962 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
7963 * @pdev: pointer to PCI device
7964 * @msg: power management message
7965 *
7966 * This routine is called from the kernel's PCI subsystem to support system
7967 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7968 * this method, it quiesces the device by stopping the driver's worker
7969 * thread for the device, turning off device's interrupt and DMA, and bring
7970 * the device offline. Note that as the driver implements the minimum PM
7971 * requirements to a power-aware driver's PM support for suspend/resume -- all
7972 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7973 * method call will be treated as SUSPEND and the driver will fully
7974 * reinitialize its device during resume() method call, the driver will set
7975 * device to PCI_D3hot state in PCI config space instead of setting it
7976 * according to the @msg provided by the PM.
7977 *
7978 * Return code
7979 * 0 - driver suspended the device
7980 * Error otherwise
7981 **/
7982static int
7983lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
7984{
7985 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7986 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7987
7988 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7989 "0298 PCI device Power Management suspend.\n");
7990
7991 /* Bring down the device */
7992 lpfc_offline_prep(phba);
7993 lpfc_offline(phba);
7994 kthread_stop(phba->worker_thread);
7995
7996 /* Disable interrupt from device */
7997 lpfc_sli4_disable_intr(phba);
7998
7999 /* Save device state to PCI config space */
8000 pci_save_state(pdev);
8001 pci_set_power_state(pdev, PCI_D3hot);
8002
8003 return 0;
8004}
8005
8006/**
8007 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8008 * @pdev: pointer to PCI device
8009 *
8010 * This routine is called from the kernel's PCI subsystem to support system
8011 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8012 * this method, it restores the device's PCI config space state and fully
8013 * reinitializes the device and brings it online. Note that as the driver
8014 * implements the minimum PM requirements to a power-aware driver's PM for
8015 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8016 * to the suspend() method call will be treated as SUSPEND and the driver
8017 * will fully reinitialize its device during resume() method call, the device
8018 * will be set to PCI_D0 directly in PCI config space before restoring the
8019 * state.
8020 *
8021 * Return code
8022 * 0 - driver suspended the device
8023 * Error otherwise
8024 **/
8025static int
8026lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8027{
8028 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8029 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8030 uint32_t intr_mode;
8031 int error;
8032
8033 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8034 "0292 PCI device Power Management resume.\n");
8035
8036 /* Restore device state from PCI config space */
8037 pci_set_power_state(pdev, PCI_D0);
8038 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008039
8040 /*
8041 * As the new kernel behavior of pci_restore_state() API call clears
8042 * device saved_state flag, need to save the restored state again.
8043 */
8044 pci_save_state(pdev);
8045
James Smartda0436e2009-05-22 14:51:39 -04008046 if (pdev->is_busmaster)
8047 pci_set_master(pdev);
8048
8049 /* Startup the kernel thread for this host adapter. */
8050 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8051 "lpfc_worker_%d", phba->brd_no);
8052 if (IS_ERR(phba->worker_thread)) {
8053 error = PTR_ERR(phba->worker_thread);
8054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8055 "0293 PM resume failed to start worker "
8056 "thread: error=x%x.\n", error);
8057 return error;
8058 }
8059
8060 /* Configure and enable interrupt */
8061 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8062 if (intr_mode == LPFC_INTR_ERROR) {
8063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8064 "0294 PM resume Failed to enable interrupt\n");
8065 return -EIO;
8066 } else
8067 phba->intr_mode = intr_mode;
8068
8069 /* Restart HBA and bring it online */
8070 lpfc_sli_brdrestart(phba);
8071 lpfc_online(phba);
8072
8073 /* Log the current active interrupt mode */
8074 lpfc_log_intr_mode(phba, phba->intr_mode);
8075
8076 return 0;
8077}
8078
8079/**
8080 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
8081 * @pdev: pointer to PCI device.
8082 * @state: the current PCI connection state.
8083 *
8084 * This routine is called from the PCI subsystem for error handling to device
8085 * with SLI-4 interface spec. This function is called by the PCI subsystem
8086 * after a PCI bus error affecting this device has been detected. When this
8087 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8088 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8089 * for the PCI subsystem to perform proper recovery as desired.
8090 *
8091 * Return codes
8092 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8093 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8094 **/
8095static pci_ers_result_t
8096lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8097{
8098 return PCI_ERS_RESULT_NEED_RESET;
8099}
8100
8101/**
8102 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
8103 * @pdev: pointer to PCI device.
8104 *
8105 * This routine is called from the PCI subsystem for error handling to device
8106 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8107 * restart the PCI card from scratch, as if from a cold-boot. During the
8108 * PCI subsystem error recovery, after the driver returns
8109 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8110 * recovery and then call this routine before calling the .resume method to
8111 * recover the device. This function will initialize the HBA device, enable
8112 * the interrupt, but it will just put the HBA to offline state without
8113 * passing any I/O traffic.
8114 *
8115 * Return codes
8116 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8117 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8118 */
8119static pci_ers_result_t
8120lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8121{
8122 return PCI_ERS_RESULT_RECOVERED;
8123}
8124
8125/**
8126 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8127 * @pdev: pointer to PCI device
8128 *
8129 * This routine is called from the PCI subsystem for error handling to device
8130 * with SLI-4 interface spec. It is called when kernel error recovery tells
8131 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8132 * error recovery. After this call, traffic can start to flow from this device
8133 * again.
8134 **/
8135static void
8136lpfc_io_resume_s4(struct pci_dev *pdev)
8137{
8138 return;
8139}
8140
8141/**
James Smart3772a992009-05-22 14:50:54 -04008142 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8143 * @pdev: pointer to PCI device
8144 * @pid: pointer to PCI device identifier
8145 *
8146 * This routine is to be registered to the kernel's PCI subsystem. When an
8147 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8148 * at PCI device-specific information of the device and driver to see if the
8149 * driver state that it can support this kind of device. If the match is
8150 * successful, the driver core invokes this routine. This routine dispatches
8151 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8152 * do all the initialization that it needs to do to handle the HBA device
8153 * properly.
8154 *
8155 * Return code
8156 * 0 - driver can claim the device
8157 * negative value - driver can not claim the device
8158 **/
8159static int __devinit
8160lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8161{
8162 int rc;
James Smart8fa38512009-07-19 10:01:03 -04008163 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04008164
James Smart28baac72010-02-12 14:42:03 -05008165 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04008166 return -ENODEV;
8167
James Smart8fa38512009-07-19 10:01:03 -04008168 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05008169 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04008170 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008171 else
James Smart3772a992009-05-22 14:50:54 -04008172 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008173
James Smart3772a992009-05-22 14:50:54 -04008174 return rc;
8175}
8176
8177/**
8178 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8179 * @pdev: pointer to PCI device
8180 *
8181 * This routine is to be registered to the kernel's PCI subsystem. When an
8182 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8183 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8184 * remove routine, which will perform all the necessary cleanup for the
8185 * device to be removed from the PCI subsystem properly.
8186 **/
8187static void __devexit
8188lpfc_pci_remove_one(struct pci_dev *pdev)
8189{
8190 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8191 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8192
8193 switch (phba->pci_dev_grp) {
8194 case LPFC_PCI_DEV_LP:
8195 lpfc_pci_remove_one_s3(pdev);
8196 break;
James Smartda0436e2009-05-22 14:51:39 -04008197 case LPFC_PCI_DEV_OC:
8198 lpfc_pci_remove_one_s4(pdev);
8199 break;
James Smart3772a992009-05-22 14:50:54 -04008200 default:
8201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8202 "1424 Invalid PCI device group: 0x%x\n",
8203 phba->pci_dev_grp);
8204 break;
8205 }
8206 return;
8207}
8208
8209/**
8210 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8211 * @pdev: pointer to PCI device
8212 * @msg: power management message
8213 *
8214 * This routine is to be registered to the kernel's PCI subsystem to support
8215 * system Power Management (PM). When PM invokes this method, it dispatches
8216 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8217 * suspend the device.
8218 *
8219 * Return code
8220 * 0 - driver suspended the device
8221 * Error otherwise
8222 **/
8223static int
8224lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8225{
8226 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8227 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8228 int rc = -ENODEV;
8229
8230 switch (phba->pci_dev_grp) {
8231 case LPFC_PCI_DEV_LP:
8232 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8233 break;
James Smartda0436e2009-05-22 14:51:39 -04008234 case LPFC_PCI_DEV_OC:
8235 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8236 break;
James Smart3772a992009-05-22 14:50:54 -04008237 default:
8238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8239 "1425 Invalid PCI device group: 0x%x\n",
8240 phba->pci_dev_grp);
8241 break;
8242 }
8243 return rc;
8244}
8245
8246/**
8247 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8248 * @pdev: pointer to PCI device
8249 *
8250 * This routine is to be registered to the kernel's PCI subsystem to support
8251 * system Power Management (PM). When PM invokes this method, it dispatches
8252 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8253 * resume the device.
8254 *
8255 * Return code
8256 * 0 - driver suspended the device
8257 * Error otherwise
8258 **/
8259static int
8260lpfc_pci_resume_one(struct pci_dev *pdev)
8261{
8262 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8263 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8264 int rc = -ENODEV;
8265
8266 switch (phba->pci_dev_grp) {
8267 case LPFC_PCI_DEV_LP:
8268 rc = lpfc_pci_resume_one_s3(pdev);
8269 break;
James Smartda0436e2009-05-22 14:51:39 -04008270 case LPFC_PCI_DEV_OC:
8271 rc = lpfc_pci_resume_one_s4(pdev);
8272 break;
James Smart3772a992009-05-22 14:50:54 -04008273 default:
8274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8275 "1426 Invalid PCI device group: 0x%x\n",
8276 phba->pci_dev_grp);
8277 break;
8278 }
8279 return rc;
8280}
8281
8282/**
8283 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8284 * @pdev: pointer to PCI device.
8285 * @state: the current PCI connection state.
8286 *
8287 * This routine is registered to the PCI subsystem for error handling. This
8288 * function is called by the PCI subsystem after a PCI bus error affecting
8289 * this device has been detected. When this routine is invoked, it dispatches
8290 * the action to the proper SLI-3 or SLI-4 device error detected handling
8291 * routine, which will perform the proper error detected operation.
8292 *
8293 * Return codes
8294 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8295 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8296 **/
8297static pci_ers_result_t
8298lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8299{
8300 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8301 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8302 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8303
8304 switch (phba->pci_dev_grp) {
8305 case LPFC_PCI_DEV_LP:
8306 rc = lpfc_io_error_detected_s3(pdev, state);
8307 break;
James Smartda0436e2009-05-22 14:51:39 -04008308 case LPFC_PCI_DEV_OC:
8309 rc = lpfc_io_error_detected_s4(pdev, state);
8310 break;
James Smart3772a992009-05-22 14:50:54 -04008311 default:
8312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8313 "1427 Invalid PCI device group: 0x%x\n",
8314 phba->pci_dev_grp);
8315 break;
8316 }
8317 return rc;
8318}
8319
8320/**
8321 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8322 * @pdev: pointer to PCI device.
8323 *
8324 * This routine is registered to the PCI subsystem for error handling. This
8325 * function is called after PCI bus has been reset to restart the PCI card
8326 * from scratch, as if from a cold-boot. When this routine is invoked, it
8327 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8328 * routine, which will perform the proper device reset.
8329 *
8330 * Return codes
8331 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8332 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8333 **/
8334static pci_ers_result_t
8335lpfc_io_slot_reset(struct pci_dev *pdev)
8336{
8337 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8338 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8339 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8340
8341 switch (phba->pci_dev_grp) {
8342 case LPFC_PCI_DEV_LP:
8343 rc = lpfc_io_slot_reset_s3(pdev);
8344 break;
James Smartda0436e2009-05-22 14:51:39 -04008345 case LPFC_PCI_DEV_OC:
8346 rc = lpfc_io_slot_reset_s4(pdev);
8347 break;
James Smart3772a992009-05-22 14:50:54 -04008348 default:
8349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8350 "1428 Invalid PCI device group: 0x%x\n",
8351 phba->pci_dev_grp);
8352 break;
8353 }
8354 return rc;
8355}
8356
8357/**
8358 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8359 * @pdev: pointer to PCI device
8360 *
8361 * This routine is registered to the PCI subsystem for error handling. It
8362 * is called when kernel error recovery tells the lpfc driver that it is
8363 * OK to resume normal PCI operation after PCI bus error recovery. When
8364 * this routine is invoked, it dispatches the action to the proper SLI-3
8365 * or SLI-4 device io_resume routine, which will resume the device operation.
8366 **/
8367static void
8368lpfc_io_resume(struct pci_dev *pdev)
8369{
8370 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8371 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8372
8373 switch (phba->pci_dev_grp) {
8374 case LPFC_PCI_DEV_LP:
8375 lpfc_io_resume_s3(pdev);
8376 break;
James Smartda0436e2009-05-22 14:51:39 -04008377 case LPFC_PCI_DEV_OC:
8378 lpfc_io_resume_s4(pdev);
8379 break;
James Smart3772a992009-05-22 14:50:54 -04008380 default:
8381 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8382 "1429 Invalid PCI device group: 0x%x\n",
8383 phba->pci_dev_grp);
8384 break;
8385 }
8386 return;
8387}
8388
dea31012005-04-17 16:05:31 -05008389static struct pci_device_id lpfc_id_table[] = {
8390 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8391 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008392 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8393 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008394 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8395 PCI_ANY_ID, PCI_ANY_ID, },
8396 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8397 PCI_ANY_ID, PCI_ANY_ID, },
8398 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8399 PCI_ANY_ID, PCI_ANY_ID, },
8400 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8401 PCI_ANY_ID, PCI_ANY_ID, },
8402 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8403 PCI_ANY_ID, PCI_ANY_ID, },
8404 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8405 PCI_ANY_ID, PCI_ANY_ID, },
8406 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8407 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008408 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8409 PCI_ANY_ID, PCI_ANY_ID, },
8410 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8411 PCI_ANY_ID, PCI_ANY_ID, },
8412 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8413 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008414 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8415 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008416 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8417 PCI_ANY_ID, PCI_ANY_ID, },
8418 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8419 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008420 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8421 PCI_ANY_ID, PCI_ANY_ID, },
8422 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8423 PCI_ANY_ID, PCI_ANY_ID, },
8424 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8425 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008426 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8427 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008428 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8429 PCI_ANY_ID, PCI_ANY_ID, },
8430 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8431 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008432 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8433 PCI_ANY_ID, PCI_ANY_ID, },
8434 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8435 PCI_ANY_ID, PCI_ANY_ID, },
8436 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8437 PCI_ANY_ID, PCI_ANY_ID, },
8438 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8439 PCI_ANY_ID, PCI_ANY_ID, },
8440 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8441 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008442 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8443 PCI_ANY_ID, PCI_ANY_ID, },
8444 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8445 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008446 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8447 PCI_ANY_ID, PCI_ANY_ID, },
8448 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8449 PCI_ANY_ID, PCI_ANY_ID, },
8450 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8451 PCI_ANY_ID, PCI_ANY_ID, },
8452 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8453 PCI_ANY_ID, PCI_ANY_ID, },
8454 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8455 PCI_ANY_ID, PCI_ANY_ID, },
8456 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8457 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008458 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8459 PCI_ANY_ID, PCI_ANY_ID, },
8460 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8461 PCI_ANY_ID, PCI_ANY_ID, },
8462 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8463 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008464 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8465 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05008466 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8467 PCI_ANY_ID, PCI_ANY_ID, },
8468 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04008469 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008470 { 0 }
8471};
8472
8473MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8474
Linas Vepstas8d63f372007-02-14 14:28:36 -06008475static struct pci_error_handlers lpfc_err_handler = {
8476 .error_detected = lpfc_io_error_detected,
8477 .slot_reset = lpfc_io_slot_reset,
8478 .resume = lpfc_io_resume,
8479};
8480
dea31012005-04-17 16:05:31 -05008481static struct pci_driver lpfc_driver = {
8482 .name = LPFC_DRIVER_NAME,
8483 .id_table = lpfc_id_table,
8484 .probe = lpfc_pci_probe_one,
8485 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008486 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008487 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008488 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008489};
8490
James Smarte59058c2008-08-24 21:49:00 -04008491/**
James Smart3621a712009-04-06 18:47:14 -04008492 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008493 *
8494 * This routine is to be invoked when the lpfc module is loaded into the
8495 * kernel. The special kernel macro module_init() is used to indicate the
8496 * role of this routine to the kernel as lpfc module entry point.
8497 *
8498 * Return codes
8499 * 0 - successful
8500 * -ENOMEM - FC attach transport failed
8501 * all others - failed
8502 */
dea31012005-04-17 16:05:31 -05008503static int __init
8504lpfc_init(void)
8505{
8506 int error = 0;
8507
8508 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008509 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008510
James Smart7ee5d432007-10-27 13:37:17 -04008511 if (lpfc_enable_npiv) {
8512 lpfc_transport_functions.vport_create = lpfc_vport_create;
8513 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8514 }
dea31012005-04-17 16:05:31 -05008515 lpfc_transport_template =
8516 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008517 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008518 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008519 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008520 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008521 fc_attach_transport(&lpfc_vport_transport_functions);
8522 if (lpfc_vport_transport_template == NULL) {
8523 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008524 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008525 }
James Smart7ee5d432007-10-27 13:37:17 -04008526 }
dea31012005-04-17 16:05:31 -05008527 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008528 if (error) {
dea31012005-04-17 16:05:31 -05008529 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008530 if (lpfc_enable_npiv)
8531 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008532 }
dea31012005-04-17 16:05:31 -05008533
8534 return error;
8535}
8536
James Smarte59058c2008-08-24 21:49:00 -04008537/**
James Smart3621a712009-04-06 18:47:14 -04008538 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008539 *
8540 * This routine is invoked when the lpfc module is removed from the kernel.
8541 * The special kernel macro module_exit() is used to indicate the role of
8542 * this routine to the kernel as lpfc module exit point.
8543 */
dea31012005-04-17 16:05:31 -05008544static void __exit
8545lpfc_exit(void)
8546{
8547 pci_unregister_driver(&lpfc_driver);
8548 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008549 if (lpfc_enable_npiv)
8550 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008551 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008552 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8553 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008554 (1L << _dump_buf_data_order), _dump_buf_data);
8555 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8556 }
8557
8558 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008559 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8560 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008561 (1L << _dump_buf_dif_order), _dump_buf_dif);
8562 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8563 }
dea31012005-04-17 16:05:31 -05008564}
8565
8566module_init(lpfc_init);
8567module_exit(lpfc_exit);
8568MODULE_LICENSE("GPL");
8569MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8570MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8571MODULE_VERSION("0:" LPFC_DRIVER_VERSION);