blob: 4d20c4148fae113857cd92042e61778da00b4165 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
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 Smart3772a992009-05-22 14:50:54 -04002076 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002077 * @phba: pointer to lpfc hba data structure.
2078 *
2079 * This routine stops all the timers associated with a HBA. This function is
2080 * invoked before either putting a HBA offline or unloading the driver.
2081 **/
James Smart3772a992009-05-22 14:50:54 -04002082void
2083lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002084{
James Smart51ef4c22007-08-02 11:10:31 -04002085 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002086 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002087 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002088 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002089 del_timer_sync(&phba->hb_tmofunc);
2090 phba->hb_outstanding = 0;
2091
2092 switch (phba->pci_dev_grp) {
2093 case LPFC_PCI_DEV_LP:
2094 /* Stop any LightPulse device specific driver timers */
2095 del_timer_sync(&phba->fcp_poll_timer);
2096 break;
2097 case LPFC_PCI_DEV_OC:
2098 /* Stop any OneConnect device sepcific driver timers */
2099 break;
2100 default:
2101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2102 "0297 Invalid device group (x%x)\n",
2103 phba->pci_dev_grp);
2104 break;
2105 }
James Smart2e0fef82007-06-17 19:56:36 -05002106 return;
dea31012005-04-17 16:05:31 -05002107}
2108
James Smarte59058c2008-08-24 21:49:00 -04002109/**
James Smart3621a712009-04-06 18:47:14 -04002110 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002111 * @phba: pointer to lpfc hba data structure.
2112 *
2113 * This routine marks a HBA's management interface as blocked. Once the HBA's
2114 * management interface is marked as blocked, all the user space access to
2115 * the HBA, whether they are from sysfs interface or libdfc interface will
2116 * all be blocked. The HBA is set to block the management interface when the
2117 * driver prepares the HBA interface for online or offline.
2118 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002119static void
2120lpfc_block_mgmt_io(struct lpfc_hba * phba)
2121{
2122 unsigned long iflag;
2123
2124 spin_lock_irqsave(&phba->hbalock, iflag);
2125 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2126 spin_unlock_irqrestore(&phba->hbalock, iflag);
2127}
2128
James Smarte59058c2008-08-24 21:49:00 -04002129/**
James Smart3621a712009-04-06 18:47:14 -04002130 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002131 * @phba: pointer to lpfc hba data structure.
2132 *
2133 * This routine initializes the HBA and brings a HBA online. During this
2134 * process, the management interface is blocked to prevent user space access
2135 * to the HBA interfering with the driver initialization.
2136 *
2137 * Return codes
2138 * 0 - successful
2139 * 1 - failed
2140 **/
dea31012005-04-17 16:05:31 -05002141int
James Smart2e0fef82007-06-17 19:56:36 -05002142lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002143{
Julia Lawall372bd282008-12-16 16:15:08 +01002144 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002145 struct lpfc_vport **vports;
2146 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002147
dea31012005-04-17 16:05:31 -05002148 if (!phba)
2149 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002150 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002151
James Smart2e0fef82007-06-17 19:56:36 -05002152 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002153 return 0;
2154
James Smarted957682007-06-17 19:56:37 -05002155 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002156 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002157
James Smart46fa3112007-04-25 09:51:45 -04002158 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002159
James Smart46fa3112007-04-25 09:51:45 -04002160 if (!lpfc_sli_queue_setup(phba)) {
2161 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002162 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002163 }
2164
James Smartda0436e2009-05-22 14:51:39 -04002165 if (phba->sli_rev == LPFC_SLI_REV4) {
2166 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2167 lpfc_unblock_mgmt_io(phba);
2168 return 1;
2169 }
2170 } else {
2171 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2172 lpfc_unblock_mgmt_io(phba);
2173 return 1;
2174 }
James Smart46fa3112007-04-25 09:51:45 -04002175 }
dea31012005-04-17 16:05:31 -05002176
James Smart549e55c2007-08-02 11:09:51 -04002177 vports = lpfc_create_vport_work_array(phba);
2178 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002179 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002180 struct Scsi_Host *shost;
2181 shost = lpfc_shost_from_vport(vports[i]);
2182 spin_lock_irq(shost->host_lock);
2183 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2184 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2185 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002186 if (phba->sli_rev == LPFC_SLI_REV4)
2187 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002188 spin_unlock_irq(shost->host_lock);
2189 }
James Smart09372822008-01-11 01:52:54 -05002190 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002191
James Smart46fa3112007-04-25 09:51:45 -04002192 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002193 return 0;
2194}
2195
James Smarte59058c2008-08-24 21:49:00 -04002196/**
James Smart3621a712009-04-06 18:47:14 -04002197 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002198 * @phba: pointer to lpfc hba data structure.
2199 *
2200 * This routine marks a HBA's management interface as not blocked. Once the
2201 * HBA's management interface is marked as not blocked, all the user space
2202 * access to the HBA, whether they are from sysfs interface or libdfc
2203 * interface will be allowed. The HBA is set to block the management interface
2204 * when the driver prepares the HBA interface for online or offline and then
2205 * set to unblock the management interface afterwards.
2206 **/
James Smart46fa3112007-04-25 09:51:45 -04002207void
James Smart46fa3112007-04-25 09:51:45 -04002208lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2209{
2210 unsigned long iflag;
2211
James Smart2e0fef82007-06-17 19:56:36 -05002212 spin_lock_irqsave(&phba->hbalock, iflag);
2213 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2214 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002215}
2216
James Smarte59058c2008-08-24 21:49:00 -04002217/**
James Smart3621a712009-04-06 18:47:14 -04002218 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002219 * @phba: pointer to lpfc hba data structure.
2220 *
2221 * This routine is invoked to prepare a HBA to be brought offline. It performs
2222 * unregistration login to all the nodes on all vports and flushes the mailbox
2223 * queue to make it ready to be brought offline.
2224 **/
James Smart46fa3112007-04-25 09:51:45 -04002225void
2226lpfc_offline_prep(struct lpfc_hba * phba)
2227{
James Smart2e0fef82007-06-17 19:56:36 -05002228 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002229 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002230 struct lpfc_vport **vports;
2231 int i;
dea31012005-04-17 16:05:31 -05002232
James Smart2e0fef82007-06-17 19:56:36 -05002233 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002234 return;
dea31012005-04-17 16:05:31 -05002235
James Smart46fa3112007-04-25 09:51:45 -04002236 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002237
2238 lpfc_linkdown(phba);
2239
James Smart87af33f2007-10-27 13:37:43 -04002240 /* Issue an unreg_login to all nodes on all vports */
2241 vports = lpfc_create_vport_work_array(phba);
2242 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002243 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart87af33f2007-10-27 13:37:43 -04002244 struct Scsi_Host *shost;
2245
James Smarta8adb832007-10-27 13:37:53 -04002246 if (vports[i]->load_flag & FC_UNLOADING)
2247 continue;
James Smartc8685952009-11-18 15:39:16 -05002248 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002249 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2250 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2251
James Smart87af33f2007-10-27 13:37:43 -04002252 shost = lpfc_shost_from_vport(vports[i]);
2253 list_for_each_entry_safe(ndlp, next_ndlp,
2254 &vports[i]->fc_nodes,
2255 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002256 if (!NLP_CHK_NODE_ACT(ndlp))
2257 continue;
James Smart87af33f2007-10-27 13:37:43 -04002258 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2259 continue;
2260 if (ndlp->nlp_type & NLP_FABRIC) {
2261 lpfc_disc_state_machine(vports[i], ndlp,
2262 NULL, NLP_EVT_DEVICE_RECOVERY);
2263 lpfc_disc_state_machine(vports[i], ndlp,
2264 NULL, NLP_EVT_DEVICE_RM);
2265 }
2266 spin_lock_irq(shost->host_lock);
2267 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2268 spin_unlock_irq(shost->host_lock);
2269 lpfc_unreg_rpi(vports[i], ndlp);
2270 }
2271 }
2272 }
James Smart09372822008-01-11 01:52:54 -05002273 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002274
James Smart04c68492009-05-22 14:52:52 -04002275 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002276}
2277
James Smarte59058c2008-08-24 21:49:00 -04002278/**
James Smart3621a712009-04-06 18:47:14 -04002279 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002280 * @phba: pointer to lpfc hba data structure.
2281 *
2282 * This routine actually brings a HBA offline. It stops all the timers
2283 * associated with the HBA, brings down the SLI layer, and eventually
2284 * marks the HBA as in offline state for the upper layer protocol.
2285 **/
James Smart46fa3112007-04-25 09:51:45 -04002286void
James Smart2e0fef82007-06-17 19:56:36 -05002287lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002288{
James Smart549e55c2007-08-02 11:09:51 -04002289 struct Scsi_Host *shost;
2290 struct lpfc_vport **vports;
2291 int i;
James Smart46fa3112007-04-25 09:51:45 -04002292
James Smart549e55c2007-08-02 11:09:51 -04002293 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002294 return;
James Smart688a8862006-07-06 15:49:56 -04002295
James Smartda0436e2009-05-22 14:51:39 -04002296 /* stop port and all timers associated with this hba */
2297 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002298 vports = lpfc_create_vport_work_array(phba);
2299 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002300 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002301 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002302 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002303 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002304 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002305 /* Bring down the SLI Layer and cleanup. The HBA is offline
2306 now. */
2307 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002308 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002309 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002310 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002311 vports = lpfc_create_vport_work_array(phba);
2312 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002313 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002314 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002315 spin_lock_irq(shost->host_lock);
2316 vports[i]->work_port_events = 0;
2317 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2318 spin_unlock_irq(shost->host_lock);
2319 }
James Smart09372822008-01-11 01:52:54 -05002320 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002321}
2322
James Smarte59058c2008-08-24 21:49:00 -04002323/**
James Smart3621a712009-04-06 18:47:14 -04002324 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002325 * @phba: pointer to lpfc hba data structure.
2326 *
2327 * This routine is to free all the SCSI buffers and IOCBs from the driver
2328 * list back to kernel. It is called from lpfc_pci_remove_one to free
2329 * the internal resources before the device is removed from the system.
2330 *
2331 * Return codes
2332 * 0 - successful (for now, it always returns 0)
2333 **/
dea31012005-04-17 16:05:31 -05002334static int
James Smart2e0fef82007-06-17 19:56:36 -05002335lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002336{
2337 struct lpfc_scsi_buf *sb, *sb_next;
2338 struct lpfc_iocbq *io, *io_next;
2339
James Smart2e0fef82007-06-17 19:56:36 -05002340 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002341 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002342 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002343 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2344 list_del(&sb->list);
2345 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002346 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002347 kfree(sb);
2348 phba->total_scsi_bufs--;
2349 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002350 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002351
2352 /* Release all the lpfc_iocbq entries maintained by this host. */
2353 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2354 list_del(&io->list);
2355 kfree(io);
2356 phba->total_iocbq_bufs--;
2357 }
James Smart2e0fef82007-06-17 19:56:36 -05002358 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002359 return 0;
2360}
2361
James Smarte59058c2008-08-24 21:49:00 -04002362/**
James Smart3621a712009-04-06 18:47:14 -04002363 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002364 * @phba: pointer to lpfc hba data structure.
2365 * @instance: a unique integer ID to this FC port.
2366 * @dev: pointer to the device data structure.
2367 *
2368 * This routine creates a FC port for the upper layer protocol. The FC port
2369 * can be created on top of either a physical port or a virtual port provided
2370 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2371 * and associates the FC port created before adding the shost into the SCSI
2372 * layer.
2373 *
2374 * Return codes
2375 * @vport - pointer to the virtual N_Port data structure.
2376 * NULL - port create failed.
2377 **/
James Smart2e0fef82007-06-17 19:56:36 -05002378struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002379lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002380{
James Smart2e0fef82007-06-17 19:56:36 -05002381 struct lpfc_vport *vport;
2382 struct Scsi_Host *shost;
2383 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002384
James Smart3de2a652007-08-02 11:09:59 -04002385 if (dev != &phba->pcidev->dev)
2386 shost = scsi_host_alloc(&lpfc_vport_template,
2387 sizeof(struct lpfc_vport));
2388 else
2389 shost = scsi_host_alloc(&lpfc_template,
2390 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002391 if (!shost)
2392 goto out;
James Smart47a86172007-04-25 09:53:22 -04002393
James Smart2e0fef82007-06-17 19:56:36 -05002394 vport = (struct lpfc_vport *) shost->hostdata;
2395 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002396 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002397 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002398 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002399
James Smart3de2a652007-08-02 11:09:59 -04002400 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002401 shost->unique_id = instance;
2402 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002403 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002404 shost->this_id = -1;
2405 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002406 if (phba->sli_rev == LPFC_SLI_REV4) {
2407 shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2408 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2409 }
James Smart81301a92008-12-04 22:39:46 -05002410
James Smart47a86172007-04-25 09:53:22 -04002411 /*
James Smart2e0fef82007-06-17 19:56:36 -05002412 * Set initial can_queue value since 0 is no longer supported and
2413 * scsi_add_host will fail. This will be adjusted later based on the
2414 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002415 */
James Smart2e0fef82007-06-17 19:56:36 -05002416 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002417 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002418 shost->transportt = lpfc_vport_transport_template;
2419 vport->port_type = LPFC_NPIV_PORT;
2420 } else {
2421 shost->transportt = lpfc_transport_template;
2422 vport->port_type = LPFC_PHYSICAL_PORT;
2423 }
James Smart47a86172007-04-25 09:53:22 -04002424
James Smart2e0fef82007-06-17 19:56:36 -05002425 /* Initialize all internally managed lists. */
2426 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002427 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002428 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002429
James Smart2e0fef82007-06-17 19:56:36 -05002430 init_timer(&vport->fc_disctmo);
2431 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002432 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002433
James Smart2e0fef82007-06-17 19:56:36 -05002434 init_timer(&vport->fc_fdmitmo);
2435 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002436 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002437
James Smart2e0fef82007-06-17 19:56:36 -05002438 init_timer(&vport->els_tmofunc);
2439 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002440 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002441
James Bottomleyd139b9b2009-11-05 13:33:12 -06002442 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002443 if (error)
2444 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002445
James Smart549e55c2007-08-02 11:09:51 -04002446 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002447 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002448 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002449 return vport;
James Smart47a86172007-04-25 09:53:22 -04002450
James Smart2e0fef82007-06-17 19:56:36 -05002451out_put_shost:
2452 scsi_host_put(shost);
2453out:
2454 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002455}
2456
James Smarte59058c2008-08-24 21:49:00 -04002457/**
James Smart3621a712009-04-06 18:47:14 -04002458 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002459 * @vport: pointer to an lpfc virtual N_Port data structure.
2460 *
2461 * This routine destroys a FC port from the upper layer protocol. All the
2462 * resources associated with the port are released.
2463 **/
James Smart2e0fef82007-06-17 19:56:36 -05002464void
2465destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002466{
James Smart92d7f7b2007-06-17 19:56:38 -05002467 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2468 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002469
James Smart858c9f62007-06-17 19:56:39 -05002470 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002471 fc_remove_host(shost);
2472 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002473
James Smart92d7f7b2007-06-17 19:56:38 -05002474 spin_lock_irq(&phba->hbalock);
2475 list_del_init(&vport->listentry);
2476 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002477
James Smart92d7f7b2007-06-17 19:56:38 -05002478 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002479 return;
James Smart47a86172007-04-25 09:53:22 -04002480}
2481
James Smarte59058c2008-08-24 21:49:00 -04002482/**
James Smart3621a712009-04-06 18:47:14 -04002483 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002484 *
2485 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2486 * uses the kernel idr facility to perform the task.
2487 *
2488 * Return codes:
2489 * instance - a unique integer ID allocated as the new instance.
2490 * -1 - lpfc get instance failed.
2491 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002492int
2493lpfc_get_instance(void)
2494{
2495 int instance = 0;
2496
2497 /* Assign an unused number */
2498 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2499 return -1;
2500 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2501 return -1;
2502 return instance;
2503}
2504
James Smarte59058c2008-08-24 21:49:00 -04002505/**
James Smart3621a712009-04-06 18:47:14 -04002506 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002507 * @shost: pointer to SCSI host data structure.
2508 * @time: elapsed time of the scan in jiffies.
2509 *
2510 * This routine is called by the SCSI layer with a SCSI host to determine
2511 * whether the scan host is finished.
2512 *
2513 * Note: there is no scan_start function as adapter initialization will have
2514 * asynchronously kicked off the link initialization.
2515 *
2516 * Return codes
2517 * 0 - SCSI host scan is not over yet.
2518 * 1 - SCSI host scan is over.
2519 **/
James Smart47a86172007-04-25 09:53:22 -04002520int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2521{
James Smart2e0fef82007-06-17 19:56:36 -05002522 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2523 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002524 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002525
James Smart858c9f62007-06-17 19:56:39 -05002526 spin_lock_irq(shost->host_lock);
2527
James Smart51ef4c22007-08-02 11:10:31 -04002528 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002529 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002530 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002531 }
James Smart2e0fef82007-06-17 19:56:36 -05002532 if (time >= 30 * HZ) {
2533 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002534 "0461 Scanning longer than 30 "
2535 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002536 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002537 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002538 }
2539 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2540 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002541 "0465 Link down longer than 15 "
2542 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002543 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002544 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002545 }
2546
James Smart2e0fef82007-06-17 19:56:36 -05002547 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002548 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002549 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002550 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002551 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002552 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002553 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002554 goto finished;
2555
2556 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002557
2558finished:
James Smart858c9f62007-06-17 19:56:39 -05002559 spin_unlock_irq(shost->host_lock);
2560 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002561}
2562
James Smarte59058c2008-08-24 21:49:00 -04002563/**
James Smart3621a712009-04-06 18:47:14 -04002564 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002565 * @shost: pointer to SCSI host data structure.
2566 *
2567 * This routine initializes a given SCSI host attributes on a FC port. The
2568 * SCSI host can be either on top of a physical port or a virtual port.
2569 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002570void lpfc_host_attrib_init(struct Scsi_Host *shost)
2571{
2572 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2573 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002574 /*
James Smart2e0fef82007-06-17 19:56:36 -05002575 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002576 */
2577
James Smart2e0fef82007-06-17 19:56:36 -05002578 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2579 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002580 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2581
2582 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002583 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002584 fc_host_supported_fc4s(shost)[2] = 1;
2585 fc_host_supported_fc4s(shost)[7] = 1;
2586
James Smart92d7f7b2007-06-17 19:56:38 -05002587 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2588 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002589
2590 fc_host_supported_speeds(shost) = 0;
2591 if (phba->lmt & LMT_10Gb)
2592 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002593 if (phba->lmt & LMT_8Gb)
2594 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002595 if (phba->lmt & LMT_4Gb)
2596 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2597 if (phba->lmt & LMT_2Gb)
2598 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2599 if (phba->lmt & LMT_1Gb)
2600 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2601
2602 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002603 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2604 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002605
2606 /* This value is also unchanging */
2607 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002608 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002609 fc_host_active_fc4s(shost)[2] = 1;
2610 fc_host_active_fc4s(shost)[7] = 1;
2611
James Smart92d7f7b2007-06-17 19:56:38 -05002612 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002613 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002614 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002615 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002616}
dea31012005-04-17 16:05:31 -05002617
James Smarte59058c2008-08-24 21:49:00 -04002618/**
James Smartda0436e2009-05-22 14:51:39 -04002619 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002620 * @phba: pointer to lpfc hba data structure.
2621 *
James Smartda0436e2009-05-22 14:51:39 -04002622 * This routine is invoked to stop an SLI3 device port, it stops the device
2623 * from generating interrupts and stops the device driver's timers for the
2624 * device.
James Smarte59058c2008-08-24 21:49:00 -04002625 **/
James Smartdb2378e2008-02-08 18:49:51 -05002626static void
James Smartda0436e2009-05-22 14:51:39 -04002627lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002628{
James Smartda0436e2009-05-22 14:51:39 -04002629 /* Clear all interrupt enable conditions */
2630 writel(0, phba->HCregaddr);
2631 readl(phba->HCregaddr); /* flush */
2632 /* Clear all pending interrupts */
2633 writel(0xffffffff, phba->HAregaddr);
2634 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002635
James Smartda0436e2009-05-22 14:51:39 -04002636 /* Reset some HBA SLI setup states */
2637 lpfc_stop_hba_timers(phba);
2638 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002639}
2640
James Smarte59058c2008-08-24 21:49:00 -04002641/**
James Smartda0436e2009-05-22 14:51:39 -04002642 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002643 * @phba: pointer to lpfc hba data structure.
2644 *
James Smartda0436e2009-05-22 14:51:39 -04002645 * This routine is invoked to stop an SLI4 device port, it stops the device
2646 * from generating interrupts and stops the device driver's timers for the
2647 * device.
2648 **/
2649static void
2650lpfc_stop_port_s4(struct lpfc_hba *phba)
2651{
2652 /* Reset some HBA SLI4 setup states */
2653 lpfc_stop_hba_timers(phba);
2654 phba->pport->work_port_events = 0;
2655 phba->sli4_hba.intr_enable = 0;
2656 /* Hard clear it for now, shall have more graceful way to wait later */
2657 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2658}
2659
2660/**
2661 * lpfc_stop_port - Wrapper function for stopping hba port
2662 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002663 *
James Smartda0436e2009-05-22 14:51:39 -04002664 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2665 * the API jump table function pointer from the lpfc_hba struct.
2666 **/
2667void
2668lpfc_stop_port(struct lpfc_hba *phba)
2669{
2670 phba->lpfc_stop_port(phba);
2671}
2672
2673/**
2674 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2675 * @phba: pointer to lpfc hba data structure.
2676 *
2677 * This routine is invoked to remove the driver default fcf record from
2678 * the port. This routine currently acts on FCF Index 0.
2679 *
2680 **/
2681void
2682lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2683{
2684 int rc = 0;
2685 LPFC_MBOXQ_t *mboxq;
2686 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2687 uint32_t mbox_tmo, req_len;
2688 uint32_t shdr_status, shdr_add_status;
2689
2690 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2691 if (!mboxq) {
2692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2693 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2694 return;
2695 }
2696
2697 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2698 sizeof(struct lpfc_sli4_cfg_mhdr);
2699 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2700 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2701 req_len, LPFC_SLI4_MBX_EMBED);
2702 /*
2703 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2704 * supports multiple FCF indices.
2705 */
2706 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2707 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2708 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2709 phba->fcf.fcf_indx);
2710
2711 if (!phba->sli4_hba.intr_enable)
2712 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2713 else {
2714 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2715 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2716 }
2717 /* The IOCTL status is embedded in the mailbox subheader. */
2718 shdr_status = bf_get(lpfc_mbox_hdr_status,
2719 &del_fcf_record->header.cfg_shdr.response);
2720 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2721 &del_fcf_record->header.cfg_shdr.response);
2722 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2723 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2724 "2516 DEL FCF of default FCF Index failed "
2725 "mbx status x%x, status x%x add_status x%x\n",
2726 rc, shdr_status, shdr_add_status);
2727 }
2728 if (rc != MBX_TIMEOUT)
2729 mempool_free(mboxq, phba->mbox_mem_pool);
2730}
2731
2732/**
James Smart6669f9b2009-10-02 15:16:45 -04002733 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2734 * @phba: pointer to lpfc hba data structure.
2735 *
2736 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2737 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2738 * was successful and the firmware supports FCoE. Any other return indicates
2739 * a error. It is assumed that this function will be called before interrupts
2740 * are enabled.
2741 **/
2742static int
2743lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2744{
2745 int rc = 0;
2746 LPFC_MBOXQ_t *mboxq;
2747 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2748 uint32_t length;
2749 uint32_t shdr_status, shdr_add_status;
2750
2751 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2752 if (!mboxq) {
2753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2754 "2621 Failed to allocate mbox for "
2755 "query firmware config cmd\n");
2756 return -ENOMEM;
2757 }
2758 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2759 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2760 sizeof(struct lpfc_sli4_cfg_mhdr));
2761 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2762 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2763 length, LPFC_SLI4_MBX_EMBED);
2764 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2765 /* The IOCTL status is embedded in the mailbox subheader. */
2766 shdr_status = bf_get(lpfc_mbox_hdr_status,
2767 &query_fw_cfg->header.cfg_shdr.response);
2768 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2769 &query_fw_cfg->header.cfg_shdr.response);
2770 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2772 "2622 Query Firmware Config failed "
2773 "mbx status x%x, status x%x add_status x%x\n",
2774 rc, shdr_status, shdr_add_status);
2775 return -EINVAL;
2776 }
2777 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2778 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2779 "2623 FCoE Function not supported by firmware. "
2780 "Function mode = %08x\n",
2781 query_fw_cfg->function_mode);
2782 return -EINVAL;
2783 }
2784 if (rc != MBX_TIMEOUT)
2785 mempool_free(mboxq, phba->mbox_mem_pool);
2786 return 0;
2787}
2788
2789/**
James Smartda0436e2009-05-22 14:51:39 -04002790 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2791 * @phba: pointer to lpfc hba data structure.
2792 * @acqe_link: pointer to the async link completion queue entry.
2793 *
2794 * This routine is to parse the SLI4 link-attention link fault code and
2795 * translate it into the base driver's read link attention mailbox command
2796 * status.
2797 *
2798 * Return: Link-attention status in terms of base driver's coding.
2799 **/
2800static uint16_t
2801lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2802 struct lpfc_acqe_link *acqe_link)
2803{
2804 uint16_t latt_fault;
2805
2806 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2807 case LPFC_ASYNC_LINK_FAULT_NONE:
2808 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2809 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2810 latt_fault = 0;
2811 break;
2812 default:
2813 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2814 "0398 Invalid link fault code: x%x\n",
2815 bf_get(lpfc_acqe_link_fault, acqe_link));
2816 latt_fault = MBXERR_ERROR;
2817 break;
2818 }
2819 return latt_fault;
2820}
2821
2822/**
2823 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2824 * @phba: pointer to lpfc hba data structure.
2825 * @acqe_link: pointer to the async link completion queue entry.
2826 *
2827 * This routine is to parse the SLI4 link attention type and translate it
2828 * into the base driver's link attention type coding.
2829 *
2830 * Return: Link attention type in terms of base driver's coding.
2831 **/
2832static uint8_t
2833lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2834 struct lpfc_acqe_link *acqe_link)
2835{
2836 uint8_t att_type;
2837
2838 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2839 case LPFC_ASYNC_LINK_STATUS_DOWN:
2840 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2841 att_type = AT_LINK_DOWN;
2842 break;
2843 case LPFC_ASYNC_LINK_STATUS_UP:
2844 /* Ignore physical link up events - wait for logical link up */
2845 att_type = AT_RESERVED;
2846 break;
2847 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2848 att_type = AT_LINK_UP;
2849 break;
2850 default:
2851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2852 "0399 Invalid link attention type: x%x\n",
2853 bf_get(lpfc_acqe_link_status, acqe_link));
2854 att_type = AT_RESERVED;
2855 break;
2856 }
2857 return att_type;
2858}
2859
2860/**
2861 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2862 * @phba: pointer to lpfc hba data structure.
2863 * @acqe_link: pointer to the async link completion queue entry.
2864 *
2865 * This routine is to parse the SLI4 link-attention link speed and translate
2866 * it into the base driver's link-attention link speed coding.
2867 *
2868 * Return: Link-attention link speed in terms of base driver's coding.
2869 **/
2870static uint8_t
2871lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2872 struct lpfc_acqe_link *acqe_link)
2873{
2874 uint8_t link_speed;
2875
2876 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2877 case LPFC_ASYNC_LINK_SPEED_ZERO:
2878 link_speed = LA_UNKNW_LINK;
2879 break;
2880 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2881 link_speed = LA_UNKNW_LINK;
2882 break;
2883 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2884 link_speed = LA_UNKNW_LINK;
2885 break;
2886 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2887 link_speed = LA_1GHZ_LINK;
2888 break;
2889 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2890 link_speed = LA_10GHZ_LINK;
2891 break;
2892 default:
2893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2894 "0483 Invalid link-attention link speed: x%x\n",
2895 bf_get(lpfc_acqe_link_speed, acqe_link));
2896 link_speed = LA_UNKNW_LINK;
2897 break;
2898 }
2899 return link_speed;
2900}
2901
2902/**
2903 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2904 * @phba: pointer to lpfc hba data structure.
2905 * @acqe_link: pointer to the async link completion queue entry.
2906 *
2907 * This routine is to handle the SLI4 asynchronous link event.
2908 **/
2909static void
2910lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
2911 struct lpfc_acqe_link *acqe_link)
2912{
2913 struct lpfc_dmabuf *mp;
2914 LPFC_MBOXQ_t *pmb;
2915 MAILBOX_t *mb;
2916 READ_LA_VAR *la;
2917 uint8_t att_type;
2918
2919 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
2920 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
2921 return;
James Smart32b97932009-07-19 10:01:21 -04002922 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04002923 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2924 if (!pmb) {
2925 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2926 "0395 The mboxq allocation failed\n");
2927 return;
2928 }
2929 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2930 if (!mp) {
2931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2932 "0396 The lpfc_dmabuf allocation failed\n");
2933 goto out_free_pmb;
2934 }
2935 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2936 if (!mp->virt) {
2937 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2938 "0397 The mbuf allocation failed\n");
2939 goto out_free_dmabuf;
2940 }
2941
2942 /* Cleanup any outstanding ELS commands */
2943 lpfc_els_flush_all_cmd(phba);
2944
2945 /* Block ELS IOCBs until we have done process link event */
2946 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2947
2948 /* Update link event statistics */
2949 phba->sli.slistat.link_event++;
2950
2951 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
2952 lpfc_read_la(phba, pmb, mp);
2953 pmb->vport = phba->pport;
2954
2955 /* Parse and translate status field */
2956 mb = &pmb->u.mb;
2957 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
2958
2959 /* Parse and translate link attention fields */
2960 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2961 la->eventTag = acqe_link->event_tag;
2962 la->attType = att_type;
2963 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
2964
2965 /* Fake the the following irrelvant fields */
2966 la->topology = TOPOLOGY_PT_PT;
2967 la->granted_AL_PA = 0;
2968 la->il = 0;
2969 la->pb = 0;
2970 la->fa = 0;
2971 la->mm = 0;
2972
2973 /* Keep the link status for extra SLI4 state machine reference */
2974 phba->sli4_hba.link_state.speed =
2975 bf_get(lpfc_acqe_link_speed, acqe_link);
2976 phba->sli4_hba.link_state.duplex =
2977 bf_get(lpfc_acqe_link_duplex, acqe_link);
2978 phba->sli4_hba.link_state.status =
2979 bf_get(lpfc_acqe_link_status, acqe_link);
2980 phba->sli4_hba.link_state.physical =
2981 bf_get(lpfc_acqe_link_physical, acqe_link);
2982 phba->sli4_hba.link_state.fault =
2983 bf_get(lpfc_acqe_link_fault, acqe_link);
2984
2985 /* Invoke the lpfc_handle_latt mailbox command callback function */
2986 lpfc_mbx_cmpl_read_la(phba, pmb);
2987
2988 return;
2989
2990out_free_dmabuf:
2991 kfree(mp);
2992out_free_pmb:
2993 mempool_free(pmb, phba->mbox_mem_pool);
2994}
2995
2996/**
2997 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
2998 * @phba: pointer to lpfc hba data structure.
2999 * @acqe_link: pointer to the async fcoe completion queue entry.
3000 *
3001 * This routine is to handle the SLI4 asynchronous fcoe event.
3002 **/
3003static void
3004lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3005 struct lpfc_acqe_fcoe *acqe_fcoe)
3006{
3007 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3008 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003009 struct lpfc_vport *vport;
3010 struct lpfc_nodelist *ndlp;
3011 struct Scsi_Host *shost;
James Smart97957242009-12-21 17:03:15 -05003012 uint32_t link_state;
James Smart695a8142010-01-26 23:08:03 -05003013 int active_vlink_present;
3014 struct lpfc_vport **vports;
3015 int i;
James Smartda0436e2009-05-22 14:51:39 -04003016
James Smart4d9ab992009-10-02 15:16:39 -04003017 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003018 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003019 switch (event_type) {
3020 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
3021 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smarte4e74272009-07-19 10:01:38 -04003022 "2546 New FCF found index 0x%x tag 0x%x\n",
James Smart6669f9b2009-10-02 15:16:45 -04003023 acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003024 acqe_fcoe->event_tag);
3025 /*
James Smart32b97932009-07-19 10:01:21 -04003026 * If the current FCF is in discovered state, or
3027 * FCF discovery is in progress do nothing.
James Smartda0436e2009-05-22 14:51:39 -04003028 */
3029 spin_lock_irq(&phba->hbalock);
James Smart32b97932009-07-19 10:01:21 -04003030 if ((phba->fcf.fcf_flag & FCF_DISCOVERED) ||
3031 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
James Smartda0436e2009-05-22 14:51:39 -04003032 spin_unlock_irq(&phba->hbalock);
3033 break;
3034 }
3035 spin_unlock_irq(&phba->hbalock);
3036
3037 /* Read the FCF table and re-discover SAN. */
James Smart6a9c52c2009-10-02 15:16:51 -04003038 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003039 if (rc)
3040 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003041 "2547 Read FCF record failed 0x%x\n",
3042 rc);
James Smartda0436e2009-05-22 14:51:39 -04003043 break;
3044
3045 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3046 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003047 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003048 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3049 acqe_fcoe->event_tag);
3050 break;
3051
3052 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3053 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003054 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003055 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003056 acqe_fcoe->event_tag);
3057 /* If the event is not for currently used fcf do nothing */
James Smart6669f9b2009-10-02 15:16:45 -04003058 if (phba->fcf.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003059 break;
3060 /*
3061 * Currently, driver support only one FCF - so treat this as
James Smart97957242009-12-21 17:03:15 -05003062 * a link down, but save the link state because we don't want
3063 * it to be changed to Link Down unless it is already down.
James Smartda0436e2009-05-22 14:51:39 -04003064 */
James Smart97957242009-12-21 17:03:15 -05003065 link_state = phba->link_state;
James Smartda0436e2009-05-22 14:51:39 -04003066 lpfc_linkdown(phba);
James Smart97957242009-12-21 17:03:15 -05003067 phba->link_state = link_state;
James Smartda0436e2009-05-22 14:51:39 -04003068 /* Unregister FCF if no devices connected to it */
3069 lpfc_unregister_unused_fcf(phba);
3070 break;
James Smart6669f9b2009-10-02 15:16:45 -04003071 case LPFC_FCOE_EVENT_TYPE_CVL:
3072 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3073 "2718 Clear Virtual Link Received for VPI 0x%x"
3074 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3075 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003076 acqe_fcoe->index - phba->vpi_base);
James Smart6669f9b2009-10-02 15:16:45 -04003077 if (!vport)
3078 break;
3079 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3080 if (!ndlp)
3081 break;
3082 shost = lpfc_shost_from_vport(vport);
James Smart695a8142010-01-26 23:08:03 -05003083 if (phba->pport->port_state <= LPFC_FLOGI)
3084 break;
3085 /* If virtual link is not yet instantiated ignore CVL */
3086 if (vport->port_state <= LPFC_FDISC)
3087 break;
3088
James Smart6669f9b2009-10-02 15:16:45 -04003089 lpfc_linkdown_port(vport);
James Smart695a8142010-01-26 23:08:03 -05003090 lpfc_cleanup_pending_mbox(vport);
3091 spin_lock_irq(shost->host_lock);
3092 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3093 spin_unlock_irq(shost->host_lock);
3094 active_vlink_present = 0;
3095
3096 vports = lpfc_create_vport_work_array(phba);
3097 if (vports) {
3098 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3099 i++) {
3100 if ((!(vports[i]->fc_flag &
3101 FC_VPORT_CVL_RCVD)) &&
3102 (vports[i]->port_state > LPFC_FDISC)) {
3103 active_vlink_present = 1;
3104 break;
3105 }
3106 }
3107 lpfc_destroy_vport_work_array(phba, vports);
3108 }
3109
3110 if (active_vlink_present) {
3111 /*
3112 * If there are other active VLinks present,
3113 * re-instantiate the Vlink using FDISC.
3114 */
James Smart6669f9b2009-10-02 15:16:45 -04003115 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3116 spin_lock_irq(shost->host_lock);
3117 ndlp->nlp_flag |= NLP_DELAY_TMO;
3118 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003119 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3120 vport->port_state = LPFC_FDISC;
3121 } else {
3122 lpfc_retry_pport_discovery(phba);
James Smart6669f9b2009-10-02 15:16:45 -04003123 }
3124 break;
James Smartda0436e2009-05-22 14:51:39 -04003125 default:
3126 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3127 "0288 Unknown FCoE event type 0x%x event tag "
3128 "0x%x\n", event_type, acqe_fcoe->event_tag);
3129 break;
3130 }
3131}
3132
3133/**
3134 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3135 * @phba: pointer to lpfc hba data structure.
3136 * @acqe_link: pointer to the async dcbx completion queue entry.
3137 *
3138 * This routine is to handle the SLI4 asynchronous dcbx event.
3139 **/
3140static void
3141lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3142 struct lpfc_acqe_dcbx *acqe_dcbx)
3143{
James Smart4d9ab992009-10-02 15:16:39 -04003144 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003145 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3146 "0290 The SLI4 DCBX asynchronous event is not "
3147 "handled yet\n");
3148}
3149
3150/**
3151 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3152 * @phba: pointer to lpfc hba data structure.
3153 *
3154 * This routine is invoked by the worker thread to process all the pending
3155 * SLI4 asynchronous events.
3156 **/
3157void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3158{
3159 struct lpfc_cq_event *cq_event;
3160
3161 /* First, declare the async event has been handled */
3162 spin_lock_irq(&phba->hbalock);
3163 phba->hba_flag &= ~ASYNC_EVENT;
3164 spin_unlock_irq(&phba->hbalock);
3165 /* Now, handle all the async events */
3166 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3167 /* Get the first event from the head of the event queue */
3168 spin_lock_irq(&phba->hbalock);
3169 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3170 cq_event, struct lpfc_cq_event, list);
3171 spin_unlock_irq(&phba->hbalock);
3172 /* Process the asynchronous event */
3173 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3174 case LPFC_TRAILER_CODE_LINK:
3175 lpfc_sli4_async_link_evt(phba,
3176 &cq_event->cqe.acqe_link);
3177 break;
3178 case LPFC_TRAILER_CODE_FCOE:
3179 lpfc_sli4_async_fcoe_evt(phba,
3180 &cq_event->cqe.acqe_fcoe);
3181 break;
3182 case LPFC_TRAILER_CODE_DCBX:
3183 lpfc_sli4_async_dcbx_evt(phba,
3184 &cq_event->cqe.acqe_dcbx);
3185 break;
3186 default:
3187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3188 "1804 Invalid asynchrous event code: "
3189 "x%x\n", bf_get(lpfc_trailer_code,
3190 &cq_event->cqe.mcqe_cmpl));
3191 break;
3192 }
3193 /* Free the completion event processed to the free pool */
3194 lpfc_sli4_cq_event_release(phba, cq_event);
3195 }
3196}
3197
3198/**
3199 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3200 * @phba: pointer to lpfc hba data structure.
3201 * @dev_grp: The HBA PCI-Device group number.
3202 *
3203 * This routine is invoked to set up the per HBA PCI-Device group function
3204 * API jump table entries.
3205 *
3206 * Return: 0 if success, otherwise -ENODEV
3207 **/
3208int
3209lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003210{
3211 int rc;
3212
James Smartda0436e2009-05-22 14:51:39 -04003213 /* Set up lpfc PCI-device group */
3214 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003215
James Smartda0436e2009-05-22 14:51:39 -04003216 /* The LPFC_PCI_DEV_OC uses SLI4 */
3217 if (dev_grp == LPFC_PCI_DEV_OC)
3218 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003219
James Smartda0436e2009-05-22 14:51:39 -04003220 /* Set up device INIT API function jump table */
3221 rc = lpfc_init_api_table_setup(phba, dev_grp);
3222 if (rc)
3223 return -ENODEV;
3224 /* Set up SCSI API function jump table */
3225 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3226 if (rc)
3227 return -ENODEV;
3228 /* Set up SLI API function jump table */
3229 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3230 if (rc)
3231 return -ENODEV;
3232 /* Set up MBOX API function jump table */
3233 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3234 if (rc)
3235 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003236
James Smartda0436e2009-05-22 14:51:39 -04003237 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003238}
3239
3240/**
James Smart3621a712009-04-06 18:47:14 -04003241 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003242 * @phba: pointer to lpfc hba data structure.
3243 * @intr_mode: active interrupt mode adopted.
3244 *
3245 * This routine it invoked to log the currently used active interrupt mode
3246 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003247 **/
3248static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003249{
3250 switch (intr_mode) {
3251 case 0:
3252 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3253 "0470 Enable INTx interrupt mode.\n");
3254 break;
3255 case 1:
3256 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3257 "0481 Enabled MSI interrupt mode.\n");
3258 break;
3259 case 2:
3260 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3261 "0480 Enabled MSI-X interrupt mode.\n");
3262 break;
3263 default:
3264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3265 "0482 Illegal interrupt mode.\n");
3266 break;
3267 }
3268 return;
3269}
3270
James Smart5b75da22008-12-04 22:39:35 -05003271/**
James Smart3772a992009-05-22 14:50:54 -04003272 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003273 * @phba: pointer to lpfc hba data structure.
3274 *
James Smart3772a992009-05-22 14:50:54 -04003275 * This routine is invoked to enable the PCI device that is common to all
3276 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003277 *
3278 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003279 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003280 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003281 **/
James Smart3772a992009-05-22 14:50:54 -04003282static int
3283lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003284{
James Smart3772a992009-05-22 14:50:54 -04003285 struct pci_dev *pdev;
3286 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003287
James Smart3772a992009-05-22 14:50:54 -04003288 /* Obtain PCI device reference */
3289 if (!phba->pcidev)
3290 goto out_error;
3291 else
3292 pdev = phba->pcidev;
3293 /* Select PCI BARs */
3294 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3295 /* Enable PCI device */
3296 if (pci_enable_device_mem(pdev))
3297 goto out_error;
3298 /* Request PCI resource for the device */
3299 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3300 goto out_disable_device;
3301 /* Set up device as PCI master and save state for EEH */
3302 pci_set_master(pdev);
3303 pci_try_set_mwi(pdev);
3304 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003305
James Smart3772a992009-05-22 14:50:54 -04003306 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003307
James Smart3772a992009-05-22 14:50:54 -04003308out_disable_device:
3309 pci_disable_device(pdev);
3310out_error:
3311 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003312}
3313
3314/**
James Smart3772a992009-05-22 14:50:54 -04003315 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003316 * @phba: pointer to lpfc hba data structure.
3317 *
James Smart3772a992009-05-22 14:50:54 -04003318 * This routine is invoked to disable the PCI device that is common to all
3319 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003320 **/
3321static void
James Smart3772a992009-05-22 14:50:54 -04003322lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003323{
James Smart3772a992009-05-22 14:50:54 -04003324 struct pci_dev *pdev;
3325 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003326
James Smart3772a992009-05-22 14:50:54 -04003327 /* Obtain PCI device reference */
3328 if (!phba->pcidev)
3329 return;
3330 else
3331 pdev = phba->pcidev;
3332 /* Select PCI BARs */
3333 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3334 /* Release PCI resource and disable PCI device */
3335 pci_release_selected_regions(pdev, bars);
3336 pci_disable_device(pdev);
3337 /* Null out PCI private reference to driver */
3338 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003339
3340 return;
3341}
3342
3343/**
James Smart3772a992009-05-22 14:50:54 -04003344 * lpfc_reset_hba - Reset a hba
3345 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003346 *
James Smart3772a992009-05-22 14:50:54 -04003347 * This routine is invoked to reset a hba device. It brings the HBA
3348 * offline, performs a board restart, and then brings the board back
3349 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3350 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003351 **/
James Smart3772a992009-05-22 14:50:54 -04003352void
3353lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003354{
James Smart3772a992009-05-22 14:50:54 -04003355 /* If resets are disabled then set error state and return. */
3356 if (!phba->cfg_enable_hba_reset) {
3357 phba->link_state = LPFC_HBA_ERROR;
3358 return;
3359 }
3360 lpfc_offline_prep(phba);
3361 lpfc_offline(phba);
3362 lpfc_sli_brdrestart(phba);
3363 lpfc_online(phba);
3364 lpfc_unblock_mgmt_io(phba);
3365}
dea31012005-04-17 16:05:31 -05003366
James Smart3772a992009-05-22 14:50:54 -04003367/**
3368 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3369 * @phba: pointer to lpfc hba data structure.
3370 *
3371 * This routine is invoked to set up the driver internal resources specific to
3372 * support the SLI-3 HBA device it attached to.
3373 *
3374 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003375 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003376 * other values - error
3377 **/
3378static int
3379lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3380{
3381 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003382
James Smart3772a992009-05-22 14:50:54 -04003383 /*
3384 * Initialize timers used by driver
3385 */
dea31012005-04-17 16:05:31 -05003386
James Smart3772a992009-05-22 14:50:54 -04003387 /* Heartbeat timer */
3388 init_timer(&phba->hb_tmofunc);
3389 phba->hb_tmofunc.function = lpfc_hb_timeout;
3390 phba->hb_tmofunc.data = (unsigned long)phba;
3391
3392 psli = &phba->sli;
3393 /* MBOX heartbeat timer */
3394 init_timer(&psli->mbox_tmo);
3395 psli->mbox_tmo.function = lpfc_mbox_timeout;
3396 psli->mbox_tmo.data = (unsigned long) phba;
3397 /* FCP polling mode timer */
3398 init_timer(&phba->fcp_poll_timer);
3399 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3400 phba->fcp_poll_timer.data = (unsigned long) phba;
3401 /* Fabric block timer */
3402 init_timer(&phba->fabric_block_timer);
3403 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3404 phba->fabric_block_timer.data = (unsigned long) phba;
3405 /* EA polling mode timer */
3406 init_timer(&phba->eratt_poll);
3407 phba->eratt_poll.function = lpfc_poll_eratt;
3408 phba->eratt_poll.data = (unsigned long) phba;
3409
3410 /* Host attention work mask setup */
3411 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3412 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3413
3414 /* Get all the module params for configuring this host */
3415 lpfc_get_cfgparam(phba);
3416 /*
3417 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3418 * used to create the sg_dma_buf_pool must be dynamically calculated.
3419 * 2 segments are added since the IOCB needs a command and response bde.
3420 */
3421 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3422 sizeof(struct fcp_rsp) +
3423 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3424
3425 if (phba->cfg_enable_bg) {
3426 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3427 phba->cfg_sg_dma_buf_size +=
3428 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3429 }
3430
3431 /* Also reinitialize the host templates with new values. */
3432 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3433 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3434
3435 phba->max_vpi = LPFC_MAX_VPI;
3436 /* This will be set to correct value after config_port mbox */
3437 phba->max_vports = 0;
3438
3439 /*
3440 * Initialize the SLI Layer to run with lpfc HBAs.
3441 */
3442 lpfc_sli_setup(phba);
3443 lpfc_sli_queue_setup(phba);
3444
3445 /* Allocate device driver memory */
3446 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3447 return -ENOMEM;
3448
3449 return 0;
3450}
3451
3452/**
3453 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3454 * @phba: pointer to lpfc hba data structure.
3455 *
3456 * This routine is invoked to unset the driver internal resources set up
3457 * specific for supporting the SLI-3 HBA device it attached to.
3458 **/
3459static void
3460lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3461{
3462 /* Free device driver memory allocated */
3463 lpfc_mem_free_all(phba);
3464
3465 return;
3466}
3467
3468/**
James Smartda0436e2009-05-22 14:51:39 -04003469 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3470 * @phba: pointer to lpfc hba data structure.
3471 *
3472 * This routine is invoked to set up the driver internal resources specific to
3473 * support the SLI-4 HBA device it attached to.
3474 *
3475 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003476 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04003477 * other values - error
3478 **/
3479static int
3480lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3481{
3482 struct lpfc_sli *psli;
3483 int rc;
3484 int i, hbq_count;
3485
3486 /* Before proceed, wait for POST done and device ready */
3487 rc = lpfc_sli4_post_status_check(phba);
3488 if (rc)
3489 return -ENODEV;
3490
3491 /*
3492 * Initialize timers used by driver
3493 */
3494
3495 /* Heartbeat timer */
3496 init_timer(&phba->hb_tmofunc);
3497 phba->hb_tmofunc.function = lpfc_hb_timeout;
3498 phba->hb_tmofunc.data = (unsigned long)phba;
3499
3500 psli = &phba->sli;
3501 /* MBOX heartbeat timer */
3502 init_timer(&psli->mbox_tmo);
3503 psli->mbox_tmo.function = lpfc_mbox_timeout;
3504 psli->mbox_tmo.data = (unsigned long) phba;
3505 /* Fabric block timer */
3506 init_timer(&phba->fabric_block_timer);
3507 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3508 phba->fabric_block_timer.data = (unsigned long) phba;
3509 /* EA polling mode timer */
3510 init_timer(&phba->eratt_poll);
3511 phba->eratt_poll.function = lpfc_poll_eratt;
3512 phba->eratt_poll.data = (unsigned long) phba;
3513 /*
3514 * We need to do a READ_CONFIG mailbox command here before
3515 * calling lpfc_get_cfgparam. For VFs this will report the
3516 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3517 * All of the resources allocated
3518 * for this Port are tied to these values.
3519 */
3520 /* Get all the module params for configuring this host */
3521 lpfc_get_cfgparam(phba);
3522 phba->max_vpi = LPFC_MAX_VPI;
3523 /* This will be set to correct value after the read_config mbox */
3524 phba->max_vports = 0;
3525
3526 /* Program the default value of vlan_id and fc_map */
3527 phba->valid_vlan = 0;
3528 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3529 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3530 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3531
3532 /*
3533 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3534 * used to create the sg_dma_buf_pool must be dynamically calculated.
3535 * 2 segments are added since the IOCB needs a command and response bde.
3536 * To insure that the scsi sgl does not cross a 4k page boundary only
3537 * sgl sizes of 1k, 2k, 4k, and 8k are supported.
3538 * Table of sgl sizes and seg_cnt:
3539 * sgl size, sg_seg_cnt total seg
3540 * 1k 50 52
3541 * 2k 114 116
3542 * 4k 242 244
3543 * 8k 498 500
3544 * cmd(32) + rsp(160) + (52 * sizeof(sli4_sge)) = 1024
3545 * cmd(32) + rsp(160) + (116 * sizeof(sli4_sge)) = 2048
3546 * cmd(32) + rsp(160) + (244 * sizeof(sli4_sge)) = 4096
3547 * cmd(32) + rsp(160) + (500 * sizeof(sli4_sge)) = 8192
3548 */
3549 if (phba->cfg_sg_seg_cnt <= LPFC_DEFAULT_SG_SEG_CNT)
3550 phba->cfg_sg_seg_cnt = 50;
3551 else if (phba->cfg_sg_seg_cnt <= 114)
3552 phba->cfg_sg_seg_cnt = 114;
3553 else if (phba->cfg_sg_seg_cnt <= 242)
3554 phba->cfg_sg_seg_cnt = 242;
3555 else
3556 phba->cfg_sg_seg_cnt = 498;
3557
3558 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd)
3559 + sizeof(struct fcp_rsp);
3560 phba->cfg_sg_dma_buf_size +=
3561 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
3562
3563 /* Initialize buffer queue management fields */
3564 hbq_count = lpfc_sli_hbq_count();
3565 for (i = 0; i < hbq_count; ++i)
3566 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3567 INIT_LIST_HEAD(&phba->rb_pend_list);
3568 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3569 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3570
3571 /*
3572 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3573 */
3574 /* Initialize the Abort scsi buffer list used by driver */
3575 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3576 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3577 /* This abort list used by worker thread */
3578 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3579
3580 /*
3581 * Initialize dirver internal slow-path work queues
3582 */
3583
3584 /* Driver internel slow-path CQ Event pool */
3585 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3586 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04003587 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04003588 /* Asynchronous event CQ Event work queue list */
3589 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3590 /* Fast-path XRI aborted CQ Event work queue list */
3591 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3592 /* Slow-path XRI aborted CQ Event work queue list */
3593 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3594 /* Receive queue CQ Event work queue list */
3595 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3596
3597 /* Initialize the driver internal SLI layer lists. */
3598 lpfc_sli_setup(phba);
3599 lpfc_sli_queue_setup(phba);
3600
3601 /* Allocate device driver memory */
3602 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3603 if (rc)
3604 return -ENOMEM;
3605
3606 /* Create the bootstrap mailbox command */
3607 rc = lpfc_create_bootstrap_mbox(phba);
3608 if (unlikely(rc))
3609 goto out_free_mem;
3610
3611 /* Set up the host's endian order with the device. */
3612 rc = lpfc_setup_endian_order(phba);
3613 if (unlikely(rc))
3614 goto out_free_bsmbx;
3615
James Smart6669f9b2009-10-02 15:16:45 -04003616 rc = lpfc_sli4_fw_cfg_check(phba);
3617 if (unlikely(rc))
3618 goto out_free_bsmbx;
3619
James Smartda0436e2009-05-22 14:51:39 -04003620 /* Set up the hba's configuration parameters. */
3621 rc = lpfc_sli4_read_config(phba);
3622 if (unlikely(rc))
3623 goto out_free_bsmbx;
3624
3625 /* Perform a function reset */
3626 rc = lpfc_pci_function_reset(phba);
3627 if (unlikely(rc))
3628 goto out_free_bsmbx;
3629
3630 /* Create all the SLI4 queues */
3631 rc = lpfc_sli4_queue_create(phba);
3632 if (rc)
3633 goto out_free_bsmbx;
3634
3635 /* Create driver internal CQE event pool */
3636 rc = lpfc_sli4_cq_event_pool_create(phba);
3637 if (rc)
3638 goto out_destroy_queue;
3639
3640 /* Initialize and populate the iocb list per host */
3641 rc = lpfc_init_sgl_list(phba);
3642 if (rc) {
3643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3644 "1400 Failed to initialize sgl list.\n");
3645 goto out_destroy_cq_event_pool;
3646 }
3647 rc = lpfc_init_active_sgl_array(phba);
3648 if (rc) {
3649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3650 "1430 Failed to initialize sgl list.\n");
3651 goto out_free_sgl_list;
3652 }
3653
3654 rc = lpfc_sli4_init_rpi_hdrs(phba);
3655 if (rc) {
3656 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3657 "1432 Failed to initialize rpi headers.\n");
3658 goto out_free_active_sgl;
3659 }
3660
3661 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3662 phba->cfg_fcp_eq_count), GFP_KERNEL);
3663 if (!phba->sli4_hba.fcp_eq_hdl) {
3664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3665 "2572 Failed allocate memory for fast-path "
3666 "per-EQ handle array\n");
3667 goto out_remove_rpi_hdrs;
3668 }
3669
3670 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3671 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3672 if (!phba->sli4_hba.msix_entries) {
3673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3674 "2573 Failed allocate memory for msi-x "
3675 "interrupt vector entries\n");
3676 goto out_free_fcp_eq_hdl;
3677 }
3678
3679 return rc;
3680
3681out_free_fcp_eq_hdl:
3682 kfree(phba->sli4_hba.fcp_eq_hdl);
3683out_remove_rpi_hdrs:
3684 lpfc_sli4_remove_rpi_hdrs(phba);
3685out_free_active_sgl:
3686 lpfc_free_active_sgl(phba);
3687out_free_sgl_list:
3688 lpfc_free_sgl_list(phba);
3689out_destroy_cq_event_pool:
3690 lpfc_sli4_cq_event_pool_destroy(phba);
3691out_destroy_queue:
3692 lpfc_sli4_queue_destroy(phba);
3693out_free_bsmbx:
3694 lpfc_destroy_bootstrap_mbox(phba);
3695out_free_mem:
3696 lpfc_mem_free(phba);
3697 return rc;
3698}
3699
3700/**
3701 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3702 * @phba: pointer to lpfc hba data structure.
3703 *
3704 * This routine is invoked to unset the driver internal resources set up
3705 * specific for supporting the SLI-4 HBA device it attached to.
3706 **/
3707static void
3708lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3709{
3710 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3711
3712 /* unregister default FCFI from the HBA */
3713 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3714
3715 /* Free the default FCR table */
3716 lpfc_sli_remove_dflt_fcf(phba);
3717
3718 /* Free memory allocated for msi-x interrupt vector entries */
3719 kfree(phba->sli4_hba.msix_entries);
3720
3721 /* Free memory allocated for fast-path work queue handles */
3722 kfree(phba->sli4_hba.fcp_eq_hdl);
3723
3724 /* Free the allocated rpi headers. */
3725 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04003726 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04003727
3728 /* Free the ELS sgl list */
3729 lpfc_free_active_sgl(phba);
3730 lpfc_free_sgl_list(phba);
3731
3732 /* Free the SCSI sgl management array */
3733 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3734
3735 /* Free the SLI4 queues */
3736 lpfc_sli4_queue_destroy(phba);
3737
3738 /* Free the completion queue EQ event pool */
3739 lpfc_sli4_cq_event_release_all(phba);
3740 lpfc_sli4_cq_event_pool_destroy(phba);
3741
3742 /* Reset SLI4 HBA FCoE function */
3743 lpfc_pci_function_reset(phba);
3744
3745 /* Free the bsmbx region. */
3746 lpfc_destroy_bootstrap_mbox(phba);
3747
3748 /* Free the SLI Layer memory with SLI4 HBAs */
3749 lpfc_mem_free_all(phba);
3750
3751 /* Free the current connect table */
3752 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04003753 &phba->fcf_conn_rec_list, list) {
3754 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04003755 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04003756 }
James Smartda0436e2009-05-22 14:51:39 -04003757
3758 return;
3759}
3760
3761/**
James Smart3772a992009-05-22 14:50:54 -04003762 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3763 * @phba: The hba struct for which this call is being executed.
3764 * @dev_grp: The HBA PCI-Device group number.
3765 *
3766 * This routine sets up the device INIT interface API function jump table
3767 * in @phba struct.
3768 *
3769 * Returns: 0 - success, -ENODEV - failure.
3770 **/
3771int
3772lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3773{
3774 switch (dev_grp) {
3775 case LPFC_PCI_DEV_LP:
3776 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3777 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3778 phba->lpfc_stop_port = lpfc_stop_port_s3;
3779 break;
James Smartda0436e2009-05-22 14:51:39 -04003780 case LPFC_PCI_DEV_OC:
3781 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3782 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3783 phba->lpfc_stop_port = lpfc_stop_port_s4;
3784 break;
James Smart3772a992009-05-22 14:50:54 -04003785 default:
3786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3787 "1431 Invalid HBA PCI-device group: 0x%x\n",
3788 dev_grp);
3789 return -ENODEV;
3790 break;
3791 }
3792 return 0;
3793}
3794
3795/**
3796 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3797 * @phba: pointer to lpfc hba data structure.
3798 *
3799 * This routine is invoked to set up the driver internal resources before the
3800 * device specific resource setup to support the HBA device it attached to.
3801 *
3802 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003803 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003804 * other values - error
3805 **/
3806static int
3807lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3808{
3809 /*
3810 * Driver resources common to all SLI revisions
3811 */
James Smartea2151b2008-09-07 11:52:10 -04003812 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003813 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003814
James Smarte47c9092008-02-08 18:49:26 -05003815 /* Initialize ndlp management spinlock */
3816 spin_lock_init(&phba->ndlp_lock);
3817
James Smart2e0fef82007-06-17 19:56:36 -05003818 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04003819 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04003820 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05003821
James Smart3772a992009-05-22 14:50:54 -04003822 /* Initialize the wait queue head for the kernel thread */
3823 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05003824
James Smart3772a992009-05-22 14:50:54 -04003825 /* Initialize the scsi buffer list used by driver for scsi IO */
3826 spin_lock_init(&phba->scsi_buf_list_lock);
3827 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05003828
James Smart3772a992009-05-22 14:50:54 -04003829 /* Initialize the fabric iocb list */
3830 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05003831
James Smart3772a992009-05-22 14:50:54 -04003832 /* Initialize list to save ELS buffers */
3833 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05003834
James Smart3772a992009-05-22 14:50:54 -04003835 /* Initialize FCF connection rec list */
3836 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05003837
James Smart3772a992009-05-22 14:50:54 -04003838 return 0;
3839}
dea31012005-04-17 16:05:31 -05003840
James Smart3772a992009-05-22 14:50:54 -04003841/**
3842 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
3843 * @phba: pointer to lpfc hba data structure.
3844 *
3845 * This routine is invoked to set up the driver internal resources after the
3846 * device specific resource setup to support the HBA device it attached to.
3847 *
3848 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003849 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003850 * other values - error
3851 **/
3852static int
3853lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
3854{
3855 int error;
3856
3857 /* Startup the kernel thread for this host adapter. */
3858 phba->worker_thread = kthread_run(lpfc_do_work, phba,
3859 "lpfc_worker_%d", phba->brd_no);
3860 if (IS_ERR(phba->worker_thread)) {
3861 error = PTR_ERR(phba->worker_thread);
3862 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003863 }
3864
James Smart3772a992009-05-22 14:50:54 -04003865 return 0;
3866}
3867
3868/**
3869 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
3870 * @phba: pointer to lpfc hba data structure.
3871 *
3872 * This routine is invoked to unset the driver internal resources set up after
3873 * the device specific resource setup for supporting the HBA device it
3874 * attached to.
3875 **/
3876static void
3877lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
3878{
3879 /* Stop kernel worker thread */
3880 kthread_stop(phba->worker_thread);
3881}
3882
3883/**
3884 * lpfc_free_iocb_list - Free iocb list.
3885 * @phba: pointer to lpfc hba data structure.
3886 *
3887 * This routine is invoked to free the driver's IOCB list and memory.
3888 **/
3889static void
3890lpfc_free_iocb_list(struct lpfc_hba *phba)
3891{
3892 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
3893
3894 spin_lock_irq(&phba->hbalock);
3895 list_for_each_entry_safe(iocbq_entry, iocbq_next,
3896 &phba->lpfc_iocb_list, list) {
3897 list_del(&iocbq_entry->list);
3898 kfree(iocbq_entry);
3899 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003900 }
James Smart3772a992009-05-22 14:50:54 -04003901 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003902
James Smart3772a992009-05-22 14:50:54 -04003903 return;
3904}
dea31012005-04-17 16:05:31 -05003905
James Smart3772a992009-05-22 14:50:54 -04003906/**
3907 * lpfc_init_iocb_list - Allocate and initialize iocb list.
3908 * @phba: pointer to lpfc hba data structure.
3909 *
3910 * This routine is invoked to allocate and initizlize the driver's IOCB
3911 * list and set up the IOCB tag array accordingly.
3912 *
3913 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003914 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003915 * other values - error
3916 **/
3917static int
3918lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
3919{
3920 struct lpfc_iocbq *iocbq_entry = NULL;
3921 uint16_t iotag;
3922 int i;
dea31012005-04-17 16:05:31 -05003923
3924 /* Initialize and populate the iocb list per host. */
3925 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04003926 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003927 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003928 if (iocbq_entry == NULL) {
3929 printk(KERN_ERR "%s: only allocated %d iocbs of "
3930 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003931 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05003932 goto out_free_iocbq;
3933 }
3934
James Bottomley604a3e32005-10-29 10:28:33 -05003935 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
3936 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04003937 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05003938 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04003939 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05003940 goto out_free_iocbq;
3941 }
James Smart3772a992009-05-22 14:50:54 -04003942 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05003943
3944 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003945 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
3946 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05003947 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003948 }
3949
James Smart3772a992009-05-22 14:50:54 -04003950 return 0;
3951
3952out_free_iocbq:
3953 lpfc_free_iocb_list(phba);
3954
3955 return -ENOMEM;
3956}
3957
3958/**
James Smartda0436e2009-05-22 14:51:39 -04003959 * lpfc_free_sgl_list - Free sgl list.
3960 * @phba: pointer to lpfc hba data structure.
3961 *
3962 * This routine is invoked to free the driver's sgl list and memory.
3963 **/
3964static void
3965lpfc_free_sgl_list(struct lpfc_hba *phba)
3966{
3967 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
3968 LIST_HEAD(sglq_list);
3969 int rc = 0;
3970
3971 spin_lock_irq(&phba->hbalock);
3972 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
3973 spin_unlock_irq(&phba->hbalock);
3974
3975 list_for_each_entry_safe(sglq_entry, sglq_next,
3976 &sglq_list, list) {
3977 list_del(&sglq_entry->list);
3978 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
3979 kfree(sglq_entry);
3980 phba->sli4_hba.total_sglq_bufs--;
3981 }
3982 rc = lpfc_sli4_remove_all_sgl_pages(phba);
3983 if (rc) {
3984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04003985 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003986 }
3987 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3988}
3989
3990/**
3991 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
3992 * @phba: pointer to lpfc hba data structure.
3993 *
3994 * This routine is invoked to allocate the driver's active sgl memory.
3995 * This array will hold the sglq_entry's for active IOs.
3996 **/
3997static int
3998lpfc_init_active_sgl_array(struct lpfc_hba *phba)
3999{
4000 int size;
4001 size = sizeof(struct lpfc_sglq *);
4002 size *= phba->sli4_hba.max_cfg_param.max_xri;
4003
4004 phba->sli4_hba.lpfc_sglq_active_list =
4005 kzalloc(size, GFP_KERNEL);
4006 if (!phba->sli4_hba.lpfc_sglq_active_list)
4007 return -ENOMEM;
4008 return 0;
4009}
4010
4011/**
4012 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4013 * @phba: pointer to lpfc hba data structure.
4014 *
4015 * This routine is invoked to walk through the array of active sglq entries
4016 * and free all of the resources.
4017 * This is just a place holder for now.
4018 **/
4019static void
4020lpfc_free_active_sgl(struct lpfc_hba *phba)
4021{
4022 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4023}
4024
4025/**
4026 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4027 * @phba: pointer to lpfc hba data structure.
4028 *
4029 * This routine is invoked to allocate and initizlize the driver's sgl
4030 * list and set up the sgl xritag tag array accordingly.
4031 *
4032 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004033 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004034 * other values - error
4035 **/
4036static int
4037lpfc_init_sgl_list(struct lpfc_hba *phba)
4038{
4039 struct lpfc_sglq *sglq_entry = NULL;
4040 int i;
4041 int els_xri_cnt;
4042
4043 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4044 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4045 "2400 lpfc_init_sgl_list els %d.\n",
4046 els_xri_cnt);
4047 /* Initialize and populate the sglq list per host/VF. */
4048 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4049 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4050
4051 /* Sanity check on XRI management */
4052 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4053 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4054 "2562 No room left for SCSI XRI allocation: "
4055 "max_xri=%d, els_xri=%d\n",
4056 phba->sli4_hba.max_cfg_param.max_xri,
4057 els_xri_cnt);
4058 return -ENOMEM;
4059 }
4060
4061 /* Allocate memory for the ELS XRI management array */
4062 phba->sli4_hba.lpfc_els_sgl_array =
4063 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4064 GFP_KERNEL);
4065
4066 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4067 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4068 "2401 Failed to allocate memory for ELS "
4069 "XRI management array of size %d.\n",
4070 els_xri_cnt);
4071 return -ENOMEM;
4072 }
4073
4074 /* Keep the SCSI XRI into the XRI management array */
4075 phba->sli4_hba.scsi_xri_max =
4076 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4077 phba->sli4_hba.scsi_xri_cnt = 0;
4078
4079 phba->sli4_hba.lpfc_scsi_psb_array =
4080 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4081 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4082
4083 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4085 "2563 Failed to allocate memory for SCSI "
4086 "XRI management array of size %d.\n",
4087 phba->sli4_hba.scsi_xri_max);
4088 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4089 return -ENOMEM;
4090 }
4091
4092 for (i = 0; i < els_xri_cnt; i++) {
4093 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4094 if (sglq_entry == NULL) {
4095 printk(KERN_ERR "%s: only allocated %d sgls of "
4096 "expected %d count. Unloading driver.\n",
4097 __func__, i, els_xri_cnt);
4098 goto out_free_mem;
4099 }
4100
4101 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4102 if (sglq_entry->sli4_xritag == NO_XRI) {
4103 kfree(sglq_entry);
4104 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4105 "Unloading driver.\n", __func__);
4106 goto out_free_mem;
4107 }
4108 sglq_entry->buff_type = GEN_BUFF_TYPE;
4109 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4110 if (sglq_entry->virt == NULL) {
4111 kfree(sglq_entry);
4112 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4113 "Unloading driver.\n", __func__);
4114 goto out_free_mem;
4115 }
4116 sglq_entry->sgl = sglq_entry->virt;
4117 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4118
4119 /* The list order is used by later block SGL registraton */
4120 spin_lock_irq(&phba->hbalock);
4121 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4122 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4123 phba->sli4_hba.total_sglq_bufs++;
4124 spin_unlock_irq(&phba->hbalock);
4125 }
4126 return 0;
4127
4128out_free_mem:
4129 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4130 lpfc_free_sgl_list(phba);
4131 return -ENOMEM;
4132}
4133
4134/**
4135 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4136 * @phba: pointer to lpfc hba data structure.
4137 *
4138 * This routine is invoked to post rpi header templates to the
4139 * HBA consistent with the SLI-4 interface spec. This routine
4140 * posts a PAGE_SIZE memory region to the port to hold up to
4141 * PAGE_SIZE modulo 64 rpi context headers.
4142 * No locks are held here because this is an initialization routine
4143 * called only from probe or lpfc_online when interrupts are not
4144 * enabled and the driver is reinitializing the device.
4145 *
4146 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004147 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004148 * ENOMEM - No availble memory
4149 * EIO - The mailbox failed to complete successfully.
4150 **/
4151int
4152lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4153{
4154 int rc = 0;
4155 int longs;
4156 uint16_t rpi_count;
4157 struct lpfc_rpi_hdr *rpi_hdr;
4158
4159 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4160
4161 /*
4162 * Provision an rpi bitmask range for discovery. The total count
4163 * is the difference between max and base + 1.
4164 */
4165 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4166 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4167
4168 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4169 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4170 GFP_KERNEL);
4171 if (!phba->sli4_hba.rpi_bmask)
4172 return -ENOMEM;
4173
4174 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4175 if (!rpi_hdr) {
4176 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4177 "0391 Error during rpi post operation\n");
4178 lpfc_sli4_remove_rpis(phba);
4179 rc = -ENODEV;
4180 }
4181
4182 return rc;
4183}
4184
4185/**
4186 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4187 * @phba: pointer to lpfc hba data structure.
4188 *
4189 * This routine is invoked to allocate a single 4KB memory region to
4190 * support rpis and stores them in the phba. This single region
4191 * provides support for up to 64 rpis. The region is used globally
4192 * by the device.
4193 *
4194 * Returns:
4195 * A valid rpi hdr on success.
4196 * A NULL pointer on any failure.
4197 **/
4198struct lpfc_rpi_hdr *
4199lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4200{
4201 uint16_t rpi_limit, curr_rpi_range;
4202 struct lpfc_dmabuf *dmabuf;
4203 struct lpfc_rpi_hdr *rpi_hdr;
4204
4205 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4206 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4207
4208 spin_lock_irq(&phba->hbalock);
4209 curr_rpi_range = phba->sli4_hba.next_rpi;
4210 spin_unlock_irq(&phba->hbalock);
4211
4212 /*
4213 * The port has a limited number of rpis. The increment here
4214 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4215 * and to allow the full max_rpi range per port.
4216 */
4217 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4218 return NULL;
4219
4220 /*
4221 * First allocate the protocol header region for the port. The
4222 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4223 */
4224 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4225 if (!dmabuf)
4226 return NULL;
4227
4228 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4229 LPFC_HDR_TEMPLATE_SIZE,
4230 &dmabuf->phys,
4231 GFP_KERNEL);
4232 if (!dmabuf->virt) {
4233 rpi_hdr = NULL;
4234 goto err_free_dmabuf;
4235 }
4236
4237 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4238 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4239 rpi_hdr = NULL;
4240 goto err_free_coherent;
4241 }
4242
4243 /* Save the rpi header data for cleanup later. */
4244 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4245 if (!rpi_hdr)
4246 goto err_free_coherent;
4247
4248 rpi_hdr->dmabuf = dmabuf;
4249 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4250 rpi_hdr->page_count = 1;
4251 spin_lock_irq(&phba->hbalock);
4252 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4253 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4254
4255 /*
4256 * The next_rpi stores the next module-64 rpi value to post
4257 * in any subsequent rpi memory region postings.
4258 */
4259 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4260 spin_unlock_irq(&phba->hbalock);
4261 return rpi_hdr;
4262
4263 err_free_coherent:
4264 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4265 dmabuf->virt, dmabuf->phys);
4266 err_free_dmabuf:
4267 kfree(dmabuf);
4268 return NULL;
4269}
4270
4271/**
4272 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4273 * @phba: pointer to lpfc hba data structure.
4274 *
4275 * This routine is invoked to remove all memory resources allocated
4276 * to support rpis. This routine presumes the caller has released all
4277 * rpis consumed by fabric or port logins and is prepared to have
4278 * the header pages removed.
4279 **/
4280void
4281lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4282{
4283 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4284
4285 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4286 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4287 list_del(&rpi_hdr->list);
4288 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4289 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4290 kfree(rpi_hdr->dmabuf);
4291 kfree(rpi_hdr);
4292 }
4293
4294 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4295 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4296}
4297
4298/**
James Smart3772a992009-05-22 14:50:54 -04004299 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4300 * @pdev: pointer to pci device data structure.
4301 *
4302 * This routine is invoked to allocate the driver hba data structure for an
4303 * HBA device. If the allocation is successful, the phba reference to the
4304 * PCI device data structure is set.
4305 *
4306 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004307 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004308 * NULL - error
4309 **/
4310static struct lpfc_hba *
4311lpfc_hba_alloc(struct pci_dev *pdev)
4312{
4313 struct lpfc_hba *phba;
4314
4315 /* Allocate memory for HBA structure */
4316 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4317 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004318 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004319 return NULL;
4320 }
4321
4322 /* Set reference to PCI device in HBA structure */
4323 phba->pcidev = pdev;
4324
4325 /* Assign an unused board number */
4326 phba->brd_no = lpfc_get_instance();
4327 if (phba->brd_no < 0) {
4328 kfree(phba);
4329 return NULL;
4330 }
4331
James Smartf1c3b0f2009-07-19 10:01:32 -04004332 mutex_init(&phba->ct_event_mutex);
4333 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4334
James Smart3772a992009-05-22 14:50:54 -04004335 return phba;
4336}
4337
4338/**
4339 * lpfc_hba_free - Free driver hba data structure with a device.
4340 * @phba: pointer to lpfc hba data structure.
4341 *
4342 * This routine is invoked to free the driver hba data structure with an
4343 * HBA device.
4344 **/
4345static void
4346lpfc_hba_free(struct lpfc_hba *phba)
4347{
4348 /* Release the driver assigned board number */
4349 idr_remove(&lpfc_hba_index, phba->brd_no);
4350
4351 kfree(phba);
4352 return;
4353}
4354
4355/**
4356 * lpfc_create_shost - Create hba physical port with associated scsi host.
4357 * @phba: pointer to lpfc hba data structure.
4358 *
4359 * This routine is invoked to create HBA physical port and associate a SCSI
4360 * host with it.
4361 *
4362 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004363 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004364 * other values - error
4365 **/
4366static int
4367lpfc_create_shost(struct lpfc_hba *phba)
4368{
4369 struct lpfc_vport *vport;
4370 struct Scsi_Host *shost;
4371
4372 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004373 phba->fc_edtov = FF_DEF_EDTOV;
4374 phba->fc_ratov = FF_DEF_RATOV;
4375 phba->fc_altov = FF_DEF_ALTOV;
4376 phba->fc_arbtov = FF_DEF_ARBTOV;
4377
James Smart3de2a652007-08-02 11:09:59 -04004378 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004379 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004380 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004381
4382 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004383 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004384 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004385 /* Put reference to SCSI host to driver's device private data */
4386 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004387
James Smart3772a992009-05-22 14:50:54 -04004388 return 0;
4389}
dea31012005-04-17 16:05:31 -05004390
James Smart3772a992009-05-22 14:50:54 -04004391/**
4392 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4393 * @phba: pointer to lpfc hba data structure.
4394 *
4395 * This routine is invoked to destroy HBA physical port and the associated
4396 * SCSI host.
4397 **/
4398static void
4399lpfc_destroy_shost(struct lpfc_hba *phba)
4400{
4401 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004402
James Smart3772a992009-05-22 14:50:54 -04004403 /* Destroy physical port that associated with the SCSI host */
4404 destroy_port(vport);
4405
4406 return;
4407}
4408
4409/**
4410 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4411 * @phba: pointer to lpfc hba data structure.
4412 * @shost: the shost to be used to detect Block guard settings.
4413 *
4414 * This routine sets up the local Block guard protocol settings for @shost.
4415 * This routine also allocates memory for debugging bg buffers.
4416 **/
4417static void
4418lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4419{
4420 int pagecnt = 10;
4421 if (lpfc_prot_mask && lpfc_prot_guard) {
4422 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4423 "1478 Registering BlockGuard with the "
4424 "SCSI layer\n");
4425 scsi_host_set_prot(shost, lpfc_prot_mask);
4426 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004427 }
James Smart81301a92008-12-04 22:39:46 -05004428 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004429 while (pagecnt) {
4430 spin_lock_init(&_dump_buf_lock);
4431 _dump_buf_data =
4432 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4433 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004434 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4435 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004436 "_dump_buf_data at 0x%p\n",
4437 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004438 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004439 memset(_dump_buf_data, 0,
4440 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004441 break;
James Smart3772a992009-05-22 14:50:54 -04004442 } else
James Smart81301a92008-12-04 22:39:46 -05004443 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004444 }
James Smart81301a92008-12-04 22:39:46 -05004445 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004446 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4447 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004448 "memory for hexdump\n");
4449 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004450 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4451 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004452 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004453 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004454 while (pagecnt) {
4455 _dump_buf_dif =
4456 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4457 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004458 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4459 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004460 "_dump_buf_dif at 0x%p\n",
4461 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004462 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004463 memset(_dump_buf_dif, 0,
4464 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004465 break;
James Smart3772a992009-05-22 14:50:54 -04004466 } else
James Smart81301a92008-12-04 22:39:46 -05004467 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004468 }
James Smart81301a92008-12-04 22:39:46 -05004469 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004470 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4471 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004472 "memory for hexdump\n");
4473 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004474 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4475 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004476 _dump_buf_dif);
4477}
4478
4479/**
4480 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4481 * @phba: pointer to lpfc hba data structure.
4482 *
4483 * This routine is invoked to perform all the necessary post initialization
4484 * setup for the device.
4485 **/
4486static void
4487lpfc_post_init_setup(struct lpfc_hba *phba)
4488{
4489 struct Scsi_Host *shost;
4490 struct lpfc_adapter_event_header adapter_event;
4491
4492 /* Get the default values for Model Name and Description */
4493 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4494
4495 /*
4496 * hba setup may have changed the hba_queue_depth so we need to
4497 * adjust the value of can_queue.
4498 */
4499 shost = pci_get_drvdata(phba->pcidev);
4500 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4501 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4502 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004503
4504 lpfc_host_attrib_init(shost);
4505
James Smart2e0fef82007-06-17 19:56:36 -05004506 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4507 spin_lock_irq(shost->host_lock);
4508 lpfc_poll_start_timer(phba);
4509 spin_unlock_irq(shost->host_lock);
4510 }
James Smart8f6d98d2006-08-01 07:34:00 -04004511
James Smart93996272008-08-24 21:50:30 -04004512 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4513 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004514 /* Send board arrival event to upper layer */
4515 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4516 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4517 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004518 sizeof(adapter_event),
4519 (char *) &adapter_event,
4520 LPFC_NL_VENDOR_ID);
4521 return;
4522}
4523
4524/**
4525 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4526 * @phba: pointer to lpfc hba data structure.
4527 *
4528 * This routine is invoked to set up the PCI device memory space for device
4529 * with SLI-3 interface spec.
4530 *
4531 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004532 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004533 * other values - error
4534 **/
4535static int
4536lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4537{
4538 struct pci_dev *pdev;
4539 unsigned long bar0map_len, bar2map_len;
4540 int i, hbq_count;
4541 void *ptr;
4542 int error = -ENODEV;
4543
4544 /* Obtain PCI device reference */
4545 if (!phba->pcidev)
4546 return error;
4547 else
4548 pdev = phba->pcidev;
4549
4550 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05004551 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
4552 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
4553 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
4554 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04004555 return error;
Michael Reed8e685972009-09-18 12:02:05 -05004556 }
4557 }
James Smart3772a992009-05-22 14:50:54 -04004558
4559 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4560 * required by each mapping.
4561 */
4562 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4563 bar0map_len = pci_resource_len(pdev, 0);
4564
4565 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4566 bar2map_len = pci_resource_len(pdev, 2);
4567
4568 /* Map HBA SLIM to a kernel virtual address. */
4569 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4570 if (!phba->slim_memmap_p) {
4571 dev_printk(KERN_ERR, &pdev->dev,
4572 "ioremap failed for SLIM memory.\n");
4573 goto out;
4574 }
4575
4576 /* Map HBA Control Registers to a kernel virtual address. */
4577 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4578 if (!phba->ctrl_regs_memmap_p) {
4579 dev_printk(KERN_ERR, &pdev->dev,
4580 "ioremap failed for HBA control registers.\n");
4581 goto out_iounmap_slim;
4582 }
4583
4584 /* Allocate memory for SLI-2 structures */
4585 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4586 SLI2_SLIM_SIZE,
4587 &phba->slim2p.phys,
4588 GFP_KERNEL);
4589 if (!phba->slim2p.virt)
4590 goto out_iounmap;
4591
4592 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4593 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4594 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4595 phba->IOCBs = (phba->slim2p.virt +
4596 offsetof(struct lpfc_sli2_slim, IOCBs));
4597
4598 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4599 lpfc_sli_hbq_size(),
4600 &phba->hbqslimp.phys,
4601 GFP_KERNEL);
4602 if (!phba->hbqslimp.virt)
4603 goto out_free_slim;
4604
4605 hbq_count = lpfc_sli_hbq_count();
4606 ptr = phba->hbqslimp.virt;
4607 for (i = 0; i < hbq_count; ++i) {
4608 phba->hbqs[i].hbq_virt = ptr;
4609 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4610 ptr += (lpfc_hbq_defs[i]->entry_count *
4611 sizeof(struct lpfc_hbq_entry));
4612 }
4613 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4614 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4615
4616 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4617
4618 INIT_LIST_HEAD(&phba->rb_pend_list);
4619
4620 phba->MBslimaddr = phba->slim_memmap_p;
4621 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4622 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4623 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4624 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004625
dea31012005-04-17 16:05:31 -05004626 return 0;
4627
dea31012005-04-17 16:05:31 -05004628out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004629 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4630 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004631out_iounmap:
4632 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004633out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004634 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004635out:
4636 return error;
4637}
4638
James Smarte59058c2008-08-24 21:49:00 -04004639/**
James Smart3772a992009-05-22 14:50:54 -04004640 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4641 * @phba: pointer to lpfc hba data structure.
4642 *
4643 * This routine is invoked to unset the PCI device memory space for device
4644 * with SLI-3 interface spec.
4645 **/
4646static void
4647lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4648{
4649 struct pci_dev *pdev;
4650
4651 /* Obtain PCI device reference */
4652 if (!phba->pcidev)
4653 return;
4654 else
4655 pdev = phba->pcidev;
4656
4657 /* Free coherent DMA memory allocated */
4658 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4659 phba->hbqslimp.virt, phba->hbqslimp.phys);
4660 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4661 phba->slim2p.virt, phba->slim2p.phys);
4662
4663 /* I/O memory unmap */
4664 iounmap(phba->ctrl_regs_memmap_p);
4665 iounmap(phba->slim_memmap_p);
4666
4667 return;
4668}
4669
4670/**
James Smartda0436e2009-05-22 14:51:39 -04004671 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4672 * @phba: pointer to lpfc hba data structure.
4673 *
4674 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4675 * done and check status.
4676 *
4677 * Return 0 if successful, otherwise -ENODEV.
4678 **/
4679int
4680lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4681{
4682 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg, scratchpad;
James Smartda0436e2009-05-22 14:51:39 -04004683 int i, port_error = -ENODEV;
4684
4685 if (!phba->sli4_hba.STAregaddr)
4686 return -ENODEV;
4687
James Smartda0436e2009-05-22 14:51:39 -04004688 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4689 for (i = 0; i < 3000; i++) {
4690 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4691 /* Encounter fatal POST error, break out */
4692 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4693 port_error = -ENODEV;
4694 break;
4695 }
4696 if (LPFC_POST_STAGE_ARMFW_READY ==
4697 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4698 port_error = 0;
4699 break;
4700 }
4701 msleep(10);
4702 }
4703
4704 if (port_error)
4705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4706 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4707 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4708 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4709 bf_get(lpfc_hst_state_perr, &sta_reg),
4710 bf_get(lpfc_hst_state_sfi, &sta_reg),
4711 bf_get(lpfc_hst_state_nip, &sta_reg),
4712 bf_get(lpfc_hst_state_ipc, &sta_reg),
4713 bf_get(lpfc_hst_state_xrom, &sta_reg),
4714 bf_get(lpfc_hst_state_dl, &sta_reg),
4715 bf_get(lpfc_hst_state_port_status, &sta_reg));
4716
4717 /* Log device information */
4718 scratchpad.word0 = readl(phba->sli4_hba.SCRATCHPADregaddr);
4719 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4720 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4721 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4722 bf_get(lpfc_scratchpad_chiptype, &scratchpad),
4723 bf_get(lpfc_scratchpad_slirev, &scratchpad),
4724 bf_get(lpfc_scratchpad_featurelevel1, &scratchpad),
4725 bf_get(lpfc_scratchpad_featurelevel2, &scratchpad));
James Smarta747c9c2009-11-18 15:41:10 -05004726 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
4727 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04004728 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05004729 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4730 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4731 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
4732 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
4733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4734 "1422 HBA Unrecoverable error: "
4735 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4736 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
4737 uerrlo_reg.word0, uerrhi_reg.word0,
4738 phba->sli4_hba.ue_mask_lo,
4739 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04004740 return -ENODEV;
4741 }
4742
James Smartda0436e2009-05-22 14:51:39 -04004743 return port_error;
4744}
4745
4746/**
4747 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4748 * @phba: pointer to lpfc hba data structure.
4749 *
4750 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4751 * memory map.
4752 **/
4753static void
4754lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4755{
4756 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4757 LPFC_UERR_STATUS_LO;
4758 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4759 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05004760 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4761 LPFC_UE_MASK_LO;
4762 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4763 LPFC_UE_MASK_HI;
James Smartda0436e2009-05-22 14:51:39 -04004764 phba->sli4_hba.SCRATCHPADregaddr = phba->sli4_hba.conf_regs_memmap_p +
4765 LPFC_SCRATCHPAD;
4766}
4767
4768/**
4769 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4770 * @phba: pointer to lpfc hba data structure.
4771 *
4772 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4773 * memory map.
4774 **/
4775static void
4776lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4777{
4778
4779 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4780 LPFC_HST_STATE;
4781 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4782 LPFC_HST_ISR0;
4783 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4784 LPFC_HST_IMR0;
4785 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4786 LPFC_HST_ISCR0;
4787 return;
4788}
4789
4790/**
4791 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
4792 * @phba: pointer to lpfc hba data structure.
4793 * @vf: virtual function number
4794 *
4795 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4796 * based on the given viftual function number, @vf.
4797 *
4798 * Return 0 if successful, otherwise -ENODEV.
4799 **/
4800static int
4801lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
4802{
4803 if (vf > LPFC_VIR_FUNC_MAX)
4804 return -ENODEV;
4805
4806 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4807 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4808 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4809 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4810 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4811 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
4812 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4813 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
4814 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4815 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
4816 return 0;
4817}
4818
4819/**
4820 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
4821 * @phba: pointer to lpfc hba data structure.
4822 *
4823 * This routine is invoked to create the bootstrap mailbox
4824 * region consistent with the SLI-4 interface spec. This
4825 * routine allocates all memory necessary to communicate
4826 * mailbox commands to the port and sets up all alignment
4827 * needs. No locks are expected to be held when calling
4828 * this routine.
4829 *
4830 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004831 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004832 * ENOMEM - could not allocated memory.
4833 **/
4834static int
4835lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
4836{
4837 uint32_t bmbx_size;
4838 struct lpfc_dmabuf *dmabuf;
4839 struct dma_address *dma_address;
4840 uint32_t pa_addr;
4841 uint64_t phys_addr;
4842
4843 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4844 if (!dmabuf)
4845 return -ENOMEM;
4846
4847 /*
4848 * The bootstrap mailbox region is comprised of 2 parts
4849 * plus an alignment restriction of 16 bytes.
4850 */
4851 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
4852 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4853 bmbx_size,
4854 &dmabuf->phys,
4855 GFP_KERNEL);
4856 if (!dmabuf->virt) {
4857 kfree(dmabuf);
4858 return -ENOMEM;
4859 }
4860 memset(dmabuf->virt, 0, bmbx_size);
4861
4862 /*
4863 * Initialize the bootstrap mailbox pointers now so that the register
4864 * operations are simple later. The mailbox dma address is required
4865 * to be 16-byte aligned. Also align the virtual memory as each
4866 * maibox is copied into the bmbx mailbox region before issuing the
4867 * command to the port.
4868 */
4869 phba->sli4_hba.bmbx.dmabuf = dmabuf;
4870 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
4871
4872 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
4873 LPFC_ALIGN_16_BYTE);
4874 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
4875 LPFC_ALIGN_16_BYTE);
4876
4877 /*
4878 * Set the high and low physical addresses now. The SLI4 alignment
4879 * requirement is 16 bytes and the mailbox is posted to the port
4880 * as two 30-bit addresses. The other data is a bit marking whether
4881 * the 30-bit address is the high or low address.
4882 * Upcast bmbx aphys to 64bits so shift instruction compiles
4883 * clean on 32 bit machines.
4884 */
4885 dma_address = &phba->sli4_hba.bmbx.dma_address;
4886 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
4887 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
4888 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
4889 LPFC_BMBX_BIT1_ADDR_HI);
4890
4891 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
4892 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
4893 LPFC_BMBX_BIT1_ADDR_LO);
4894 return 0;
4895}
4896
4897/**
4898 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
4899 * @phba: pointer to lpfc hba data structure.
4900 *
4901 * This routine is invoked to teardown the bootstrap mailbox
4902 * region and release all host resources. This routine requires
4903 * the caller to ensure all mailbox commands recovered, no
4904 * additional mailbox comands are sent, and interrupts are disabled
4905 * before calling this routine.
4906 *
4907 **/
4908static void
4909lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
4910{
4911 dma_free_coherent(&phba->pcidev->dev,
4912 phba->sli4_hba.bmbx.bmbx_size,
4913 phba->sli4_hba.bmbx.dmabuf->virt,
4914 phba->sli4_hba.bmbx.dmabuf->phys);
4915
4916 kfree(phba->sli4_hba.bmbx.dmabuf);
4917 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
4918}
4919
4920/**
4921 * lpfc_sli4_read_config - Get the config parameters.
4922 * @phba: pointer to lpfc hba data structure.
4923 *
4924 * This routine is invoked to read the configuration parameters from the HBA.
4925 * The configuration parameters are used to set the base and maximum values
4926 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
4927 * allocation for the port.
4928 *
4929 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004930 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004931 * ENOMEM - No availble memory
4932 * EIO - The mailbox failed to complete successfully.
4933 **/
4934static int
4935lpfc_sli4_read_config(struct lpfc_hba *phba)
4936{
4937 LPFC_MBOXQ_t *pmb;
4938 struct lpfc_mbx_read_config *rd_config;
4939 uint32_t rc = 0;
4940
4941 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4942 if (!pmb) {
4943 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4944 "2011 Unable to allocate memory for issuing "
4945 "SLI_CONFIG_SPECIAL mailbox command\n");
4946 return -ENOMEM;
4947 }
4948
4949 lpfc_read_config(phba, pmb);
4950
4951 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4952 if (rc != MBX_SUCCESS) {
4953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4954 "2012 Mailbox failed , mbxCmd x%x "
4955 "READ_CONFIG, mbxStatus x%x\n",
4956 bf_get(lpfc_mqe_command, &pmb->u.mqe),
4957 bf_get(lpfc_mqe_status, &pmb->u.mqe));
4958 rc = -EIO;
4959 } else {
4960 rd_config = &pmb->u.mqe.un.rd_config;
4961 phba->sli4_hba.max_cfg_param.max_xri =
4962 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
4963 phba->sli4_hba.max_cfg_param.xri_base =
4964 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
4965 phba->sli4_hba.max_cfg_param.max_vpi =
4966 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
4967 phba->sli4_hba.max_cfg_param.vpi_base =
4968 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
4969 phba->sli4_hba.max_cfg_param.max_rpi =
4970 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
4971 phba->sli4_hba.max_cfg_param.rpi_base =
4972 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
4973 phba->sli4_hba.max_cfg_param.max_vfi =
4974 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
4975 phba->sli4_hba.max_cfg_param.vfi_base =
4976 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
4977 phba->sli4_hba.max_cfg_param.max_fcfi =
4978 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
4979 phba->sli4_hba.max_cfg_param.fcfi_base =
4980 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
4981 phba->sli4_hba.max_cfg_param.max_eq =
4982 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
4983 phba->sli4_hba.max_cfg_param.max_rq =
4984 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
4985 phba->sli4_hba.max_cfg_param.max_wq =
4986 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
4987 phba->sli4_hba.max_cfg_param.max_cq =
4988 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
4989 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
4990 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
4991 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
4992 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
4993 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05004994 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
4995 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04004996 phba->max_vports = phba->max_vpi;
4997 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4998 "2003 cfg params XRI(B:%d M:%d), "
4999 "VPI(B:%d M:%d) "
5000 "VFI(B:%d M:%d) "
5001 "RPI(B:%d M:%d) "
5002 "FCFI(B:%d M:%d)\n",
5003 phba->sli4_hba.max_cfg_param.xri_base,
5004 phba->sli4_hba.max_cfg_param.max_xri,
5005 phba->sli4_hba.max_cfg_param.vpi_base,
5006 phba->sli4_hba.max_cfg_param.max_vpi,
5007 phba->sli4_hba.max_cfg_param.vfi_base,
5008 phba->sli4_hba.max_cfg_param.max_vfi,
5009 phba->sli4_hba.max_cfg_param.rpi_base,
5010 phba->sli4_hba.max_cfg_param.max_rpi,
5011 phba->sli4_hba.max_cfg_param.fcfi_base,
5012 phba->sli4_hba.max_cfg_param.max_fcfi);
5013 }
5014 mempool_free(pmb, phba->mbox_mem_pool);
5015
5016 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5017 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5018 phba->cfg_hba_queue_depth =
5019 phba->sli4_hba.max_cfg_param.max_xri;
5020 return rc;
5021}
5022
5023/**
5024 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5025 * @phba: pointer to lpfc hba data structure.
5026 *
5027 * This routine is invoked to setup the host-side endian order to the
5028 * HBA consistent with the SLI-4 interface spec.
5029 *
5030 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005031 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005032 * ENOMEM - No availble memory
5033 * EIO - The mailbox failed to complete successfully.
5034 **/
5035static int
5036lpfc_setup_endian_order(struct lpfc_hba *phba)
5037{
5038 LPFC_MBOXQ_t *mboxq;
5039 uint32_t rc = 0;
5040 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5041 HOST_ENDIAN_HIGH_WORD1};
5042
5043 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5044 if (!mboxq) {
5045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5046 "0492 Unable to allocate memory for issuing "
5047 "SLI_CONFIG_SPECIAL mailbox command\n");
5048 return -ENOMEM;
5049 }
5050
5051 /*
5052 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5053 * words to contain special data values and no other data.
5054 */
5055 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5056 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5057 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5058 if (rc != MBX_SUCCESS) {
5059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5060 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5061 "status x%x\n",
5062 rc);
5063 rc = -EIO;
5064 }
5065
5066 mempool_free(mboxq, phba->mbox_mem_pool);
5067 return rc;
5068}
5069
5070/**
5071 * lpfc_sli4_queue_create - Create all the SLI4 queues
5072 * @phba: pointer to lpfc hba data structure.
5073 *
5074 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5075 * operation. For each SLI4 queue type, the parameters such as queue entry
5076 * count (queue depth) shall be taken from the module parameter. For now,
5077 * we just use some constant number as place holder.
5078 *
5079 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005080 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005081 * ENOMEM - No availble memory
5082 * EIO - The mailbox failed to complete successfully.
5083 **/
5084static int
5085lpfc_sli4_queue_create(struct lpfc_hba *phba)
5086{
5087 struct lpfc_queue *qdesc;
5088 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5089 int cfg_fcp_wq_count;
5090 int cfg_fcp_eq_count;
5091
5092 /*
5093 * Sanity check for confiugred queue parameters against the run-time
5094 * device parameters
5095 */
5096
5097 /* Sanity check on FCP fast-path WQ parameters */
5098 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5099 if (cfg_fcp_wq_count >
5100 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5101 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5102 LPFC_SP_WQN_DEF;
5103 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5105 "2581 Not enough WQs (%d) from "
5106 "the pci function for supporting "
5107 "FCP WQs (%d)\n",
5108 phba->sli4_hba.max_cfg_param.max_wq,
5109 phba->cfg_fcp_wq_count);
5110 goto out_error;
5111 }
5112 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5113 "2582 Not enough WQs (%d) from the pci "
5114 "function for supporting the requested "
5115 "FCP WQs (%d), the actual FCP WQs can "
5116 "be supported: %d\n",
5117 phba->sli4_hba.max_cfg_param.max_wq,
5118 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5119 }
5120 /* The actual number of FCP work queues adopted */
5121 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5122
5123 /* Sanity check on FCP fast-path EQ parameters */
5124 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5125 if (cfg_fcp_eq_count >
5126 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5127 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5128 LPFC_SP_EQN_DEF;
5129 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5131 "2574 Not enough EQs (%d) from the "
5132 "pci function for supporting FCP "
5133 "EQs (%d)\n",
5134 phba->sli4_hba.max_cfg_param.max_eq,
5135 phba->cfg_fcp_eq_count);
5136 goto out_error;
5137 }
5138 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5139 "2575 Not enough EQs (%d) from the pci "
5140 "function for supporting the requested "
5141 "FCP EQs (%d), the actual FCP EQs can "
5142 "be supported: %d\n",
5143 phba->sli4_hba.max_cfg_param.max_eq,
5144 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5145 }
5146 /* It does not make sense to have more EQs than WQs */
5147 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5148 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005149 "2593 The FCP EQ count(%d) cannot be greater "
5150 "than the FCP WQ count(%d), limiting the "
5151 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005152 phba->cfg_fcp_wq_count,
5153 phba->cfg_fcp_wq_count);
5154 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5155 }
5156 /* The actual number of FCP event queues adopted */
5157 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5158 /* The overall number of event queues used */
5159 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5160
5161 /*
5162 * Create Event Queues (EQs)
5163 */
5164
5165 /* Get EQ depth from module parameter, fake the default for now */
5166 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5167 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5168
5169 /* Create slow path event queue */
5170 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5171 phba->sli4_hba.eq_ecount);
5172 if (!qdesc) {
5173 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5174 "0496 Failed allocate slow-path EQ\n");
5175 goto out_error;
5176 }
5177 phba->sli4_hba.sp_eq = qdesc;
5178
5179 /* Create fast-path FCP Event Queue(s) */
5180 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5181 phba->cfg_fcp_eq_count), GFP_KERNEL);
5182 if (!phba->sli4_hba.fp_eq) {
5183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5184 "2576 Failed allocate memory for fast-path "
5185 "EQ record array\n");
5186 goto out_free_sp_eq;
5187 }
5188 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5189 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5190 phba->sli4_hba.eq_ecount);
5191 if (!qdesc) {
5192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5193 "0497 Failed allocate fast-path EQ\n");
5194 goto out_free_fp_eq;
5195 }
5196 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5197 }
5198
5199 /*
5200 * Create Complete Queues (CQs)
5201 */
5202
5203 /* Get CQ depth from module parameter, fake the default for now */
5204 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5205 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5206
5207 /* Create slow-path Mailbox Command Complete Queue */
5208 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5209 phba->sli4_hba.cq_ecount);
5210 if (!qdesc) {
5211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5212 "0500 Failed allocate slow-path mailbox CQ\n");
5213 goto out_free_fp_eq;
5214 }
5215 phba->sli4_hba.mbx_cq = qdesc;
5216
5217 /* Create slow-path ELS Complete Queue */
5218 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5219 phba->sli4_hba.cq_ecount);
5220 if (!qdesc) {
5221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5222 "0501 Failed allocate slow-path ELS CQ\n");
5223 goto out_free_mbx_cq;
5224 }
5225 phba->sli4_hba.els_cq = qdesc;
5226
James Smartda0436e2009-05-22 14:51:39 -04005227
5228 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5229 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5230 phba->cfg_fcp_eq_count), GFP_KERNEL);
5231 if (!phba->sli4_hba.fcp_cq) {
5232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5233 "2577 Failed allocate memory for fast-path "
5234 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005235 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005236 }
5237 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5238 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5239 phba->sli4_hba.cq_ecount);
5240 if (!qdesc) {
5241 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5242 "0499 Failed allocate fast-path FCP "
5243 "CQ (%d)\n", fcp_cqidx);
5244 goto out_free_fcp_cq;
5245 }
5246 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5247 }
5248
5249 /* Create Mailbox Command Queue */
5250 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5251 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5252
5253 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5254 phba->sli4_hba.mq_ecount);
5255 if (!qdesc) {
5256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5257 "0505 Failed allocate slow-path MQ\n");
5258 goto out_free_fcp_cq;
5259 }
5260 phba->sli4_hba.mbx_wq = qdesc;
5261
5262 /*
5263 * Create all the Work Queues (WQs)
5264 */
5265 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5266 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5267
5268 /* Create slow-path ELS Work Queue */
5269 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5270 phba->sli4_hba.wq_ecount);
5271 if (!qdesc) {
5272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5273 "0504 Failed allocate slow-path ELS WQ\n");
5274 goto out_free_mbx_wq;
5275 }
5276 phba->sli4_hba.els_wq = qdesc;
5277
5278 /* Create fast-path FCP Work Queue(s) */
5279 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5280 phba->cfg_fcp_wq_count), GFP_KERNEL);
5281 if (!phba->sli4_hba.fcp_wq) {
5282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5283 "2578 Failed allocate memory for fast-path "
5284 "WQ record array\n");
5285 goto out_free_els_wq;
5286 }
5287 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5288 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5289 phba->sli4_hba.wq_ecount);
5290 if (!qdesc) {
5291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5292 "0503 Failed allocate fast-path FCP "
5293 "WQ (%d)\n", fcp_wqidx);
5294 goto out_free_fcp_wq;
5295 }
5296 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5297 }
5298
5299 /*
5300 * Create Receive Queue (RQ)
5301 */
5302 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5303 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5304
5305 /* Create Receive Queue for header */
5306 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5307 phba->sli4_hba.rq_ecount);
5308 if (!qdesc) {
5309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5310 "0506 Failed allocate receive HRQ\n");
5311 goto out_free_fcp_wq;
5312 }
5313 phba->sli4_hba.hdr_rq = qdesc;
5314
5315 /* Create Receive Queue for data */
5316 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5317 phba->sli4_hba.rq_ecount);
5318 if (!qdesc) {
5319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5320 "0507 Failed allocate receive DRQ\n");
5321 goto out_free_hdr_rq;
5322 }
5323 phba->sli4_hba.dat_rq = qdesc;
5324
5325 return 0;
5326
5327out_free_hdr_rq:
5328 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5329 phba->sli4_hba.hdr_rq = NULL;
5330out_free_fcp_wq:
5331 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5332 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5333 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5334 }
5335 kfree(phba->sli4_hba.fcp_wq);
5336out_free_els_wq:
5337 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5338 phba->sli4_hba.els_wq = NULL;
5339out_free_mbx_wq:
5340 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5341 phba->sli4_hba.mbx_wq = NULL;
5342out_free_fcp_cq:
5343 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5344 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5345 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5346 }
5347 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005348out_free_els_cq:
5349 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5350 phba->sli4_hba.els_cq = NULL;
5351out_free_mbx_cq:
5352 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5353 phba->sli4_hba.mbx_cq = NULL;
5354out_free_fp_eq:
5355 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5356 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5357 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5358 }
5359 kfree(phba->sli4_hba.fp_eq);
5360out_free_sp_eq:
5361 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5362 phba->sli4_hba.sp_eq = NULL;
5363out_error:
5364 return -ENOMEM;
5365}
5366
5367/**
5368 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5369 * @phba: pointer to lpfc hba data structure.
5370 *
5371 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5372 * operation.
5373 *
5374 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005375 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005376 * ENOMEM - No availble memory
5377 * EIO - The mailbox failed to complete successfully.
5378 **/
5379static void
5380lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5381{
5382 int fcp_qidx;
5383
5384 /* Release mailbox command work queue */
5385 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5386 phba->sli4_hba.mbx_wq = NULL;
5387
5388 /* Release ELS work queue */
5389 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5390 phba->sli4_hba.els_wq = NULL;
5391
5392 /* Release FCP work queue */
5393 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5394 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5395 kfree(phba->sli4_hba.fcp_wq);
5396 phba->sli4_hba.fcp_wq = NULL;
5397
5398 /* Release unsolicited receive queue */
5399 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5400 phba->sli4_hba.hdr_rq = NULL;
5401 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5402 phba->sli4_hba.dat_rq = NULL;
5403
James Smartda0436e2009-05-22 14:51:39 -04005404 /* Release ELS complete queue */
5405 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5406 phba->sli4_hba.els_cq = NULL;
5407
5408 /* Release mailbox command complete queue */
5409 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5410 phba->sli4_hba.mbx_cq = NULL;
5411
5412 /* Release FCP response complete queue */
5413 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5414 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5415 kfree(phba->sli4_hba.fcp_cq);
5416 phba->sli4_hba.fcp_cq = NULL;
5417
5418 /* Release fast-path event queue */
5419 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5420 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5421 kfree(phba->sli4_hba.fp_eq);
5422 phba->sli4_hba.fp_eq = NULL;
5423
5424 /* Release slow-path event queue */
5425 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5426 phba->sli4_hba.sp_eq = NULL;
5427
5428 return;
5429}
5430
5431/**
5432 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5433 * @phba: pointer to lpfc hba data structure.
5434 *
5435 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5436 * operation.
5437 *
5438 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005439 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005440 * ENOMEM - No availble memory
5441 * EIO - The mailbox failed to complete successfully.
5442 **/
5443int
5444lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5445{
5446 int rc = -ENOMEM;
5447 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5448 int fcp_cq_index = 0;
5449
5450 /*
5451 * Set up Event Queues (EQs)
5452 */
5453
5454 /* Set up slow-path event queue */
5455 if (!phba->sli4_hba.sp_eq) {
5456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5457 "0520 Slow-path EQ not allocated\n");
5458 goto out_error;
5459 }
5460 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5461 LPFC_SP_DEF_IMAX);
5462 if (rc) {
5463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5464 "0521 Failed setup of slow-path EQ: "
5465 "rc = 0x%x\n", rc);
5466 goto out_error;
5467 }
5468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5469 "2583 Slow-path EQ setup: queue-id=%d\n",
5470 phba->sli4_hba.sp_eq->queue_id);
5471
5472 /* Set up fast-path event queue */
5473 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5474 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5476 "0522 Fast-path EQ (%d) not "
5477 "allocated\n", fcp_eqidx);
5478 goto out_destroy_fp_eq;
5479 }
5480 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5481 phba->cfg_fcp_imax);
5482 if (rc) {
5483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5484 "0523 Failed setup of fast-path EQ "
5485 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5486 goto out_destroy_fp_eq;
5487 }
5488 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5489 "2584 Fast-path EQ setup: "
5490 "queue[%d]-id=%d\n", fcp_eqidx,
5491 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5492 }
5493
5494 /*
5495 * Set up Complete Queues (CQs)
5496 */
5497
5498 /* Set up slow-path MBOX Complete Queue as the first CQ */
5499 if (!phba->sli4_hba.mbx_cq) {
5500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5501 "0528 Mailbox CQ not allocated\n");
5502 goto out_destroy_fp_eq;
5503 }
5504 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5505 LPFC_MCQ, LPFC_MBOX);
5506 if (rc) {
5507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5508 "0529 Failed setup of slow-path mailbox CQ: "
5509 "rc = 0x%x\n", rc);
5510 goto out_destroy_fp_eq;
5511 }
5512 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5513 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5514 phba->sli4_hba.mbx_cq->queue_id,
5515 phba->sli4_hba.sp_eq->queue_id);
5516
5517 /* Set up slow-path ELS Complete Queue */
5518 if (!phba->sli4_hba.els_cq) {
5519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5520 "0530 ELS CQ not allocated\n");
5521 goto out_destroy_mbx_cq;
5522 }
5523 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5524 LPFC_WCQ, LPFC_ELS);
5525 if (rc) {
5526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5527 "0531 Failed setup of slow-path ELS CQ: "
5528 "rc = 0x%x\n", rc);
5529 goto out_destroy_mbx_cq;
5530 }
5531 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5532 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5533 phba->sli4_hba.els_cq->queue_id,
5534 phba->sli4_hba.sp_eq->queue_id);
5535
James Smartda0436e2009-05-22 14:51:39 -04005536 /* Set up fast-path FCP Response Complete Queue */
5537 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5538 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5540 "0526 Fast-path FCP CQ (%d) not "
5541 "allocated\n", fcp_cqidx);
5542 goto out_destroy_fcp_cq;
5543 }
5544 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5545 phba->sli4_hba.fp_eq[fcp_cqidx],
5546 LPFC_WCQ, LPFC_FCP);
5547 if (rc) {
5548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5549 "0527 Failed setup of fast-path FCP "
5550 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5551 goto out_destroy_fcp_cq;
5552 }
5553 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5554 "2588 FCP CQ setup: cq[%d]-id=%d, "
5555 "parent eq[%d]-id=%d\n",
5556 fcp_cqidx,
5557 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5558 fcp_cqidx,
5559 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5560 }
5561
5562 /*
5563 * Set up all the Work Queues (WQs)
5564 */
5565
5566 /* Set up Mailbox Command Queue */
5567 if (!phba->sli4_hba.mbx_wq) {
5568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5569 "0538 Slow-path MQ not allocated\n");
5570 goto out_destroy_fcp_cq;
5571 }
5572 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5573 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5574 if (rc) {
5575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5576 "0539 Failed setup of slow-path MQ: "
5577 "rc = 0x%x\n", rc);
5578 goto out_destroy_fcp_cq;
5579 }
5580 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5581 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5582 phba->sli4_hba.mbx_wq->queue_id,
5583 phba->sli4_hba.mbx_cq->queue_id);
5584
5585 /* Set up slow-path ELS Work Queue */
5586 if (!phba->sli4_hba.els_wq) {
5587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5588 "0536 Slow-path ELS WQ not allocated\n");
5589 goto out_destroy_mbx_wq;
5590 }
5591 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5592 phba->sli4_hba.els_cq, LPFC_ELS);
5593 if (rc) {
5594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5595 "0537 Failed setup of slow-path ELS WQ: "
5596 "rc = 0x%x\n", rc);
5597 goto out_destroy_mbx_wq;
5598 }
5599 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5600 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5601 phba->sli4_hba.els_wq->queue_id,
5602 phba->sli4_hba.els_cq->queue_id);
5603
5604 /* Set up fast-path FCP Work Queue */
5605 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5606 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5608 "0534 Fast-path FCP WQ (%d) not "
5609 "allocated\n", fcp_wqidx);
5610 goto out_destroy_fcp_wq;
5611 }
5612 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5613 phba->sli4_hba.fcp_cq[fcp_cq_index],
5614 LPFC_FCP);
5615 if (rc) {
5616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5617 "0535 Failed setup of fast-path FCP "
5618 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5619 goto out_destroy_fcp_wq;
5620 }
5621 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5622 "2591 FCP WQ setup: wq[%d]-id=%d, "
5623 "parent cq[%d]-id=%d\n",
5624 fcp_wqidx,
5625 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5626 fcp_cq_index,
5627 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5628 /* Round robin FCP Work Queue's Completion Queue assignment */
5629 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5630 }
5631
5632 /*
5633 * Create Receive Queue (RQ)
5634 */
5635 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5637 "0540 Receive Queue not allocated\n");
5638 goto out_destroy_fcp_wq;
5639 }
5640 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04005641 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04005642 if (rc) {
5643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5644 "0541 Failed setup of Receive Queue: "
5645 "rc = 0x%x\n", rc);
5646 goto out_destroy_fcp_wq;
5647 }
5648 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5649 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5650 "parent cq-id=%d\n",
5651 phba->sli4_hba.hdr_rq->queue_id,
5652 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04005653 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04005654 return 0;
5655
5656out_destroy_fcp_wq:
5657 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5658 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5659 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5660out_destroy_mbx_wq:
5661 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5662out_destroy_fcp_cq:
5663 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5664 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04005665 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5666out_destroy_mbx_cq:
5667 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5668out_destroy_fp_eq:
5669 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5670 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5671 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5672out_error:
5673 return rc;
5674}
5675
5676/**
5677 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5678 * @phba: pointer to lpfc hba data structure.
5679 *
5680 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5681 * operation.
5682 *
5683 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005684 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005685 * ENOMEM - No availble memory
5686 * EIO - The mailbox failed to complete successfully.
5687 **/
5688void
5689lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5690{
5691 int fcp_qidx;
5692
5693 /* Unset mailbox command work queue */
5694 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5695 /* Unset ELS work queue */
5696 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5697 /* Unset unsolicited receive queue */
5698 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5699 /* Unset FCP work queue */
5700 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5701 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5702 /* Unset mailbox command complete queue */
5703 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5704 /* Unset ELS complete queue */
5705 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04005706 /* Unset FCP response complete queue */
5707 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5708 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5709 /* Unset fast-path event queue */
5710 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5711 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5712 /* Unset slow-path event queue */
5713 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5714}
5715
5716/**
5717 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5718 * @phba: pointer to lpfc hba data structure.
5719 *
5720 * This routine is invoked to allocate and set up a pool of completion queue
5721 * events. The body of the completion queue event is a completion queue entry
5722 * CQE. For now, this pool is used for the interrupt service routine to queue
5723 * the following HBA completion queue events for the worker thread to process:
5724 * - Mailbox asynchronous events
5725 * - Receive queue completion unsolicited events
5726 * Later, this can be used for all the slow-path events.
5727 *
5728 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005729 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005730 * -ENOMEM - No availble memory
5731 **/
5732static int
5733lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5734{
5735 struct lpfc_cq_event *cq_event;
5736 int i;
5737
5738 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5739 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5740 if (!cq_event)
5741 goto out_pool_create_fail;
5742 list_add_tail(&cq_event->list,
5743 &phba->sli4_hba.sp_cqe_event_pool);
5744 }
5745 return 0;
5746
5747out_pool_create_fail:
5748 lpfc_sli4_cq_event_pool_destroy(phba);
5749 return -ENOMEM;
5750}
5751
5752/**
5753 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5754 * @phba: pointer to lpfc hba data structure.
5755 *
5756 * This routine is invoked to free the pool of completion queue events at
5757 * driver unload time. Note that, it is the responsibility of the driver
5758 * cleanup routine to free all the outstanding completion-queue events
5759 * allocated from this pool back into the pool before invoking this routine
5760 * to destroy the pool.
5761 **/
5762static void
5763lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5764{
5765 struct lpfc_cq_event *cq_event, *next_cq_event;
5766
5767 list_for_each_entry_safe(cq_event, next_cq_event,
5768 &phba->sli4_hba.sp_cqe_event_pool, list) {
5769 list_del(&cq_event->list);
5770 kfree(cq_event);
5771 }
5772}
5773
5774/**
5775 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5776 * @phba: pointer to lpfc hba data structure.
5777 *
5778 * This routine is the lock free version of the API invoked to allocate a
5779 * completion-queue event from the free pool.
5780 *
5781 * Return: Pointer to the newly allocated completion-queue event if successful
5782 * NULL otherwise.
5783 **/
5784struct lpfc_cq_event *
5785__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5786{
5787 struct lpfc_cq_event *cq_event = NULL;
5788
5789 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5790 struct lpfc_cq_event, list);
5791 return cq_event;
5792}
5793
5794/**
5795 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5796 * @phba: pointer to lpfc hba data structure.
5797 *
5798 * This routine is the lock version of the API invoked to allocate a
5799 * completion-queue event from the free pool.
5800 *
5801 * Return: Pointer to the newly allocated completion-queue event if successful
5802 * NULL otherwise.
5803 **/
5804struct lpfc_cq_event *
5805lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5806{
5807 struct lpfc_cq_event *cq_event;
5808 unsigned long iflags;
5809
5810 spin_lock_irqsave(&phba->hbalock, iflags);
5811 cq_event = __lpfc_sli4_cq_event_alloc(phba);
5812 spin_unlock_irqrestore(&phba->hbalock, iflags);
5813 return cq_event;
5814}
5815
5816/**
5817 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5818 * @phba: pointer to lpfc hba data structure.
5819 * @cq_event: pointer to the completion queue event to be freed.
5820 *
5821 * This routine is the lock free version of the API invoked to release a
5822 * completion-queue event back into the free pool.
5823 **/
5824void
5825__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5826 struct lpfc_cq_event *cq_event)
5827{
5828 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
5829}
5830
5831/**
5832 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5833 * @phba: pointer to lpfc hba data structure.
5834 * @cq_event: pointer to the completion queue event to be freed.
5835 *
5836 * This routine is the lock version of the API invoked to release a
5837 * completion-queue event back into the free pool.
5838 **/
5839void
5840lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5841 struct lpfc_cq_event *cq_event)
5842{
5843 unsigned long iflags;
5844 spin_lock_irqsave(&phba->hbalock, iflags);
5845 __lpfc_sli4_cq_event_release(phba, cq_event);
5846 spin_unlock_irqrestore(&phba->hbalock, iflags);
5847}
5848
5849/**
5850 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
5851 * @phba: pointer to lpfc hba data structure.
5852 *
5853 * This routine is to free all the pending completion-queue events to the
5854 * back into the free pool for device reset.
5855 **/
5856static void
5857lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
5858{
5859 LIST_HEAD(cqelist);
5860 struct lpfc_cq_event *cqe;
5861 unsigned long iflags;
5862
5863 /* Retrieve all the pending WCQEs from pending WCQE lists */
5864 spin_lock_irqsave(&phba->hbalock, iflags);
5865 /* Pending FCP XRI abort events */
5866 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
5867 &cqelist);
5868 /* Pending ELS XRI abort events */
5869 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
5870 &cqelist);
5871 /* Pending asynnc events */
5872 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
5873 &cqelist);
5874 spin_unlock_irqrestore(&phba->hbalock, iflags);
5875
5876 while (!list_empty(&cqelist)) {
5877 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
5878 lpfc_sli4_cq_event_release(phba, cqe);
5879 }
5880}
5881
5882/**
5883 * lpfc_pci_function_reset - Reset pci function.
5884 * @phba: pointer to lpfc hba data structure.
5885 *
5886 * This routine is invoked to request a PCI function reset. It will destroys
5887 * all resources assigned to the PCI function which originates this request.
5888 *
5889 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005890 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005891 * ENOMEM - No availble memory
5892 * EIO - The mailbox failed to complete successfully.
5893 **/
5894int
5895lpfc_pci_function_reset(struct lpfc_hba *phba)
5896{
5897 LPFC_MBOXQ_t *mboxq;
5898 uint32_t rc = 0;
5899 uint32_t shdr_status, shdr_add_status;
5900 union lpfc_sli4_cfg_shdr *shdr;
5901
5902 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5903 if (!mboxq) {
5904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5905 "0494 Unable to allocate memory for issuing "
5906 "SLI_FUNCTION_RESET mailbox command\n");
5907 return -ENOMEM;
5908 }
5909
5910 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
5911 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5912 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
5913 LPFC_SLI4_MBX_EMBED);
5914 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5915 shdr = (union lpfc_sli4_cfg_shdr *)
5916 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5917 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5918 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5919 if (rc != MBX_TIMEOUT)
5920 mempool_free(mboxq, phba->mbox_mem_pool);
5921 if (shdr_status || shdr_add_status || rc) {
5922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5923 "0495 SLI_FUNCTION_RESET mailbox failed with "
5924 "status x%x add_status x%x, mbx status x%x\n",
5925 shdr_status, shdr_add_status, rc);
5926 rc = -ENXIO;
5927 }
5928 return rc;
5929}
5930
5931/**
5932 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
5933 * @phba: pointer to lpfc hba data structure.
5934 * @cnt: number of nop mailbox commands to send.
5935 *
5936 * This routine is invoked to send a number @cnt of NOP mailbox command and
5937 * wait for each command to complete.
5938 *
5939 * Return: the number of NOP mailbox command completed.
5940 **/
5941static int
5942lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
5943{
5944 LPFC_MBOXQ_t *mboxq;
5945 int length, cmdsent;
5946 uint32_t mbox_tmo;
5947 uint32_t rc = 0;
5948 uint32_t shdr_status, shdr_add_status;
5949 union lpfc_sli4_cfg_shdr *shdr;
5950
5951 if (cnt == 0) {
5952 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5953 "2518 Requested to send 0 NOP mailbox cmd\n");
5954 return cnt;
5955 }
5956
5957 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5958 if (!mboxq) {
5959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5960 "2519 Unable to allocate memory for issuing "
5961 "NOP mailbox command\n");
5962 return 0;
5963 }
5964
5965 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
5966 length = (sizeof(struct lpfc_mbx_nop) -
5967 sizeof(struct lpfc_sli4_cfg_mhdr));
5968 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5969 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
5970
5971 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5972 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
5973 if (!phba->sli4_hba.intr_enable)
5974 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5975 else
5976 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
5977 if (rc == MBX_TIMEOUT)
5978 break;
5979 /* Check return status */
5980 shdr = (union lpfc_sli4_cfg_shdr *)
5981 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5982 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5983 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
5984 &shdr->response);
5985 if (shdr_status || shdr_add_status || rc) {
5986 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5987 "2520 NOP mailbox command failed "
5988 "status x%x add_status x%x mbx "
5989 "status x%x\n", shdr_status,
5990 shdr_add_status, rc);
5991 break;
5992 }
5993 }
5994
5995 if (rc != MBX_TIMEOUT)
5996 mempool_free(mboxq, phba->mbox_mem_pool);
5997
5998 return cmdsent;
5999}
6000
6001/**
6002 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6003 * @phba: pointer to lpfc hba data structure.
6004 * @fcfi: fcf index.
6005 *
6006 * This routine is invoked to unregister a FCFI from device.
6007 **/
6008void
6009lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6010{
6011 LPFC_MBOXQ_t *mbox;
6012 uint32_t mbox_tmo;
6013 int rc;
6014 unsigned long flags;
6015
6016 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6017
6018 if (!mbox)
6019 return;
6020
6021 lpfc_unreg_fcfi(mbox, fcfi);
6022
6023 if (!phba->sli4_hba.intr_enable)
6024 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6025 else {
6026 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6027 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6028 }
6029 if (rc != MBX_TIMEOUT)
6030 mempool_free(mbox, phba->mbox_mem_pool);
6031 if (rc != MBX_SUCCESS)
6032 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6033 "2517 Unregister FCFI command failed "
6034 "status %d, mbxStatus x%x\n", rc,
6035 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6036 else {
6037 spin_lock_irqsave(&phba->hbalock, flags);
6038 /* Mark the FCFI is no longer registered */
6039 phba->fcf.fcf_flag &=
6040 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_DISCOVERED);
6041 spin_unlock_irqrestore(&phba->hbalock, flags);
6042 }
6043}
6044
6045/**
6046 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6047 * @phba: pointer to lpfc hba data structure.
6048 *
6049 * This routine is invoked to set up the PCI device memory space for device
6050 * with SLI-4 interface spec.
6051 *
6052 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006053 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006054 * other values - error
6055 **/
6056static int
6057lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6058{
6059 struct pci_dev *pdev;
6060 unsigned long bar0map_len, bar1map_len, bar2map_len;
6061 int error = -ENODEV;
6062
6063 /* Obtain PCI device reference */
6064 if (!phba->pcidev)
6065 return error;
6066 else
6067 pdev = phba->pcidev;
6068
6069 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006070 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6071 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6072 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6073 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006074 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006075 }
6076 }
James Smartda0436e2009-05-22 14:51:39 -04006077
6078 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6079 * number of bytes required by each mapping. They are actually
6080 * mapping to the PCI BAR regions 1, 2, and 4 by the SLI4 device.
6081 */
6082 phba->pci_bar0_map = pci_resource_start(pdev, LPFC_SLI4_BAR0);
6083 bar0map_len = pci_resource_len(pdev, LPFC_SLI4_BAR0);
6084
6085 phba->pci_bar1_map = pci_resource_start(pdev, LPFC_SLI4_BAR1);
6086 bar1map_len = pci_resource_len(pdev, LPFC_SLI4_BAR1);
6087
6088 phba->pci_bar2_map = pci_resource_start(pdev, LPFC_SLI4_BAR2);
6089 bar2map_len = pci_resource_len(pdev, LPFC_SLI4_BAR2);
6090
6091 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6092 phba->sli4_hba.conf_regs_memmap_p =
6093 ioremap(phba->pci_bar0_map, bar0map_len);
6094 if (!phba->sli4_hba.conf_regs_memmap_p) {
6095 dev_printk(KERN_ERR, &pdev->dev,
6096 "ioremap failed for SLI4 PCI config registers.\n");
6097 goto out;
6098 }
6099
6100 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6101 phba->sli4_hba.ctrl_regs_memmap_p =
6102 ioremap(phba->pci_bar1_map, bar1map_len);
6103 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6104 dev_printk(KERN_ERR, &pdev->dev,
6105 "ioremap failed for SLI4 HBA control registers.\n");
6106 goto out_iounmap_conf;
6107 }
6108
6109 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6110 phba->sli4_hba.drbl_regs_memmap_p =
6111 ioremap(phba->pci_bar2_map, bar2map_len);
6112 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6113 dev_printk(KERN_ERR, &pdev->dev,
6114 "ioremap failed for SLI4 HBA doorbell registers.\n");
6115 goto out_iounmap_ctrl;
6116 }
6117
6118 /* Set up BAR0 PCI config space register memory map */
6119 lpfc_sli4_bar0_register_memmap(phba);
6120
6121 /* Set up BAR1 register memory map */
6122 lpfc_sli4_bar1_register_memmap(phba);
6123
6124 /* Set up BAR2 register memory map */
6125 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6126 if (error)
6127 goto out_iounmap_all;
6128
6129 return 0;
6130
6131out_iounmap_all:
6132 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6133out_iounmap_ctrl:
6134 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6135out_iounmap_conf:
6136 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6137out:
6138 return error;
6139}
6140
6141/**
6142 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6143 * @phba: pointer to lpfc hba data structure.
6144 *
6145 * This routine is invoked to unset the PCI device memory space for device
6146 * with SLI-4 interface spec.
6147 **/
6148static void
6149lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6150{
6151 struct pci_dev *pdev;
6152
6153 /* Obtain PCI device reference */
6154 if (!phba->pcidev)
6155 return;
6156 else
6157 pdev = phba->pcidev;
6158
6159 /* Free coherent DMA memory allocated */
6160
6161 /* Unmap I/O memory space */
6162 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6163 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6164 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6165
6166 return;
6167}
6168
6169/**
James Smart3772a992009-05-22 14:50:54 -04006170 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6171 * @phba: pointer to lpfc hba data structure.
6172 *
6173 * This routine is invoked to enable the MSI-X interrupt vectors to device
6174 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6175 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6176 * invoked, enables either all or nothing, depending on the current
6177 * availability of PCI vector resources. The device driver is responsible
6178 * for calling the individual request_irq() to register each MSI-X vector
6179 * with a interrupt handler, which is done in this function. Note that
6180 * later when device is unloading, the driver should always call free_irq()
6181 * on all MSI-X vectors it has done request_irq() on before calling
6182 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6183 * will be left with MSI-X enabled and leaks its vectors.
6184 *
6185 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006186 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006187 * other values - error
6188 **/
6189static int
6190lpfc_sli_enable_msix(struct lpfc_hba *phba)
6191{
6192 int rc, i;
6193 LPFC_MBOXQ_t *pmb;
6194
6195 /* Set up MSI-X multi-message vectors */
6196 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6197 phba->msix_entries[i].entry = i;
6198
6199 /* Configure MSI-X capability structure */
6200 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6201 ARRAY_SIZE(phba->msix_entries));
6202 if (rc) {
6203 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6204 "0420 PCI enable MSI-X failed (%d)\n", rc);
6205 goto msi_fail_out;
6206 }
6207 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6208 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6209 "0477 MSI-X entry[%d]: vector=x%x "
6210 "message=%d\n", i,
6211 phba->msix_entries[i].vector,
6212 phba->msix_entries[i].entry);
6213 /*
6214 * Assign MSI-X vectors to interrupt handlers
6215 */
6216
6217 /* vector-0 is associated to slow-path handler */
6218 rc = request_irq(phba->msix_entries[0].vector,
6219 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6220 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6221 if (rc) {
6222 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6223 "0421 MSI-X slow-path request_irq failed "
6224 "(%d)\n", rc);
6225 goto msi_fail_out;
6226 }
6227
6228 /* vector-1 is associated to fast-path handler */
6229 rc = request_irq(phba->msix_entries[1].vector,
6230 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6231 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6232
6233 if (rc) {
6234 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6235 "0429 MSI-X fast-path request_irq failed "
6236 "(%d)\n", rc);
6237 goto irq_fail_out;
6238 }
6239
6240 /*
6241 * Configure HBA MSI-X attention conditions to messages
6242 */
6243 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6244
6245 if (!pmb) {
6246 rc = -ENOMEM;
6247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6248 "0474 Unable to allocate memory for issuing "
6249 "MBOX_CONFIG_MSI command\n");
6250 goto mem_fail_out;
6251 }
6252 rc = lpfc_config_msi(phba, pmb);
6253 if (rc)
6254 goto mbx_fail_out;
6255 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6256 if (rc != MBX_SUCCESS) {
6257 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6258 "0351 Config MSI mailbox command failed, "
6259 "mbxCmd x%x, mbxStatus x%x\n",
6260 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6261 goto mbx_fail_out;
6262 }
6263
6264 /* Free memory allocated for mailbox command */
6265 mempool_free(pmb, phba->mbox_mem_pool);
6266 return rc;
6267
6268mbx_fail_out:
6269 /* Free memory allocated for mailbox command */
6270 mempool_free(pmb, phba->mbox_mem_pool);
6271
6272mem_fail_out:
6273 /* free the irq already requested */
6274 free_irq(phba->msix_entries[1].vector, phba);
6275
6276irq_fail_out:
6277 /* free the irq already requested */
6278 free_irq(phba->msix_entries[0].vector, phba);
6279
6280msi_fail_out:
6281 /* Unconfigure MSI-X capability structure */
6282 pci_disable_msix(phba->pcidev);
6283 return rc;
6284}
6285
6286/**
6287 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6288 * @phba: pointer to lpfc hba data structure.
6289 *
6290 * This routine is invoked to release the MSI-X vectors and then disable the
6291 * MSI-X interrupt mode to device with SLI-3 interface spec.
6292 **/
6293static void
6294lpfc_sli_disable_msix(struct lpfc_hba *phba)
6295{
6296 int i;
6297
6298 /* Free up MSI-X multi-message vectors */
6299 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6300 free_irq(phba->msix_entries[i].vector, phba);
6301 /* Disable MSI-X */
6302 pci_disable_msix(phba->pcidev);
6303
6304 return;
6305}
6306
6307/**
6308 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6309 * @phba: pointer to lpfc hba data structure.
6310 *
6311 * This routine is invoked to enable the MSI interrupt mode to device with
6312 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6313 * enable the MSI vector. The device driver is responsible for calling the
6314 * request_irq() to register MSI vector with a interrupt the handler, which
6315 * is done in this function.
6316 *
6317 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006318 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006319 * other values - error
6320 */
6321static int
6322lpfc_sli_enable_msi(struct lpfc_hba *phba)
6323{
6324 int rc;
6325
6326 rc = pci_enable_msi(phba->pcidev);
6327 if (!rc)
6328 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6329 "0462 PCI enable MSI mode success.\n");
6330 else {
6331 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6332 "0471 PCI enable MSI mode failed (%d)\n", rc);
6333 return rc;
6334 }
6335
6336 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6337 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6338 if (rc) {
6339 pci_disable_msi(phba->pcidev);
6340 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6341 "0478 MSI request_irq failed (%d)\n", rc);
6342 }
6343 return rc;
6344}
6345
6346/**
6347 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6348 * @phba: pointer to lpfc hba data structure.
6349 *
6350 * This routine is invoked to disable the MSI interrupt mode to device with
6351 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6352 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6353 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6354 * its vector.
6355 */
6356static void
6357lpfc_sli_disable_msi(struct lpfc_hba *phba)
6358{
6359 free_irq(phba->pcidev->irq, phba);
6360 pci_disable_msi(phba->pcidev);
6361 return;
6362}
6363
6364/**
6365 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6366 * @phba: pointer to lpfc hba data structure.
6367 *
6368 * This routine is invoked to enable device interrupt and associate driver's
6369 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6370 * spec. Depends on the interrupt mode configured to the driver, the driver
6371 * will try to fallback from the configured interrupt mode to an interrupt
6372 * mode which is supported by the platform, kernel, and device in the order
6373 * of:
6374 * MSI-X -> MSI -> IRQ.
6375 *
6376 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006377 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006378 * other values - error
6379 **/
6380static uint32_t
6381lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6382{
6383 uint32_t intr_mode = LPFC_INTR_ERROR;
6384 int retval;
6385
6386 if (cfg_mode == 2) {
6387 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6388 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6389 if (!retval) {
6390 /* Now, try to enable MSI-X interrupt mode */
6391 retval = lpfc_sli_enable_msix(phba);
6392 if (!retval) {
6393 /* Indicate initialization to MSI-X mode */
6394 phba->intr_type = MSIX;
6395 intr_mode = 2;
6396 }
6397 }
6398 }
6399
6400 /* Fallback to MSI if MSI-X initialization failed */
6401 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6402 retval = lpfc_sli_enable_msi(phba);
6403 if (!retval) {
6404 /* Indicate initialization to MSI mode */
6405 phba->intr_type = MSI;
6406 intr_mode = 1;
6407 }
6408 }
6409
6410 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6411 if (phba->intr_type == NONE) {
6412 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6413 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6414 if (!retval) {
6415 /* Indicate initialization to INTx mode */
6416 phba->intr_type = INTx;
6417 intr_mode = 0;
6418 }
6419 }
6420 return intr_mode;
6421}
6422
6423/**
6424 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6425 * @phba: pointer to lpfc hba data structure.
6426 *
6427 * This routine is invoked to disable device interrupt and disassociate the
6428 * driver's interrupt handler(s) from interrupt vector(s) to device with
6429 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6430 * release the interrupt vector(s) for the message signaled interrupt.
6431 **/
6432static void
6433lpfc_sli_disable_intr(struct lpfc_hba *phba)
6434{
6435 /* Disable the currently initialized interrupt mode */
6436 if (phba->intr_type == MSIX)
6437 lpfc_sli_disable_msix(phba);
6438 else if (phba->intr_type == MSI)
6439 lpfc_sli_disable_msi(phba);
6440 else if (phba->intr_type == INTx)
6441 free_irq(phba->pcidev->irq, phba);
6442
6443 /* Reset interrupt management states */
6444 phba->intr_type = NONE;
6445 phba->sli.slistat.sli_intr = 0;
6446
6447 return;
6448}
6449
6450/**
James Smartda0436e2009-05-22 14:51:39 -04006451 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6452 * @phba: pointer to lpfc hba data structure.
6453 *
6454 * This routine is invoked to enable the MSI-X interrupt vectors to device
6455 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6456 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6457 * enables either all or nothing, depending on the current availability of
6458 * PCI vector resources. The device driver is responsible for calling the
6459 * individual request_irq() to register each MSI-X vector with a interrupt
6460 * handler, which is done in this function. Note that later when device is
6461 * unloading, the driver should always call free_irq() on all MSI-X vectors
6462 * it has done request_irq() on before calling pci_disable_msix(). Failure
6463 * to do so results in a BUG_ON() and a device will be left with MSI-X
6464 * enabled and leaks its vectors.
6465 *
6466 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006467 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006468 * other values - error
6469 **/
6470static int
6471lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6472{
6473 int rc, index;
6474
6475 /* Set up MSI-X multi-message vectors */
6476 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6477 phba->sli4_hba.msix_entries[index].entry = index;
6478
6479 /* Configure MSI-X capability structure */
6480 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6481 phba->sli4_hba.cfg_eqn);
6482 if (rc) {
6483 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6484 "0484 PCI enable MSI-X failed (%d)\n", rc);
6485 goto msi_fail_out;
6486 }
6487 /* Log MSI-X vector assignment */
6488 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6489 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6490 "0489 MSI-X entry[%d]: vector=x%x "
6491 "message=%d\n", index,
6492 phba->sli4_hba.msix_entries[index].vector,
6493 phba->sli4_hba.msix_entries[index].entry);
6494 /*
6495 * Assign MSI-X vectors to interrupt handlers
6496 */
6497
6498 /* The first vector must associated to slow-path handler for MQ */
6499 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6500 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6501 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6502 if (rc) {
6503 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6504 "0485 MSI-X slow-path request_irq failed "
6505 "(%d)\n", rc);
6506 goto msi_fail_out;
6507 }
6508
6509 /* The rest of the vector(s) are associated to fast-path handler(s) */
6510 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6511 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6512 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6513 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6514 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6515 LPFC_FP_DRIVER_HANDLER_NAME,
6516 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6517 if (rc) {
6518 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6519 "0486 MSI-X fast-path (%d) "
6520 "request_irq failed (%d)\n", index, rc);
6521 goto cfg_fail_out;
6522 }
6523 }
6524
6525 return rc;
6526
6527cfg_fail_out:
6528 /* free the irq already requested */
6529 for (--index; index >= 1; index--)
6530 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6531 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6532
6533 /* free the irq already requested */
6534 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6535
6536msi_fail_out:
6537 /* Unconfigure MSI-X capability structure */
6538 pci_disable_msix(phba->pcidev);
6539 return rc;
6540}
6541
6542/**
6543 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6544 * @phba: pointer to lpfc hba data structure.
6545 *
6546 * This routine is invoked to release the MSI-X vectors and then disable the
6547 * MSI-X interrupt mode to device with SLI-4 interface spec.
6548 **/
6549static void
6550lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6551{
6552 int index;
6553
6554 /* Free up MSI-X multi-message vectors */
6555 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6556
6557 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6558 free_irq(phba->sli4_hba.msix_entries[index].vector,
6559 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6560 /* Disable MSI-X */
6561 pci_disable_msix(phba->pcidev);
6562
6563 return;
6564}
6565
6566/**
6567 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6568 * @phba: pointer to lpfc hba data structure.
6569 *
6570 * This routine is invoked to enable the MSI interrupt mode to device with
6571 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6572 * to enable the MSI vector. The device driver is responsible for calling
6573 * the request_irq() to register MSI vector with a interrupt the handler,
6574 * which is done in this function.
6575 *
6576 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006577 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006578 * other values - error
6579 **/
6580static int
6581lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6582{
6583 int rc, index;
6584
6585 rc = pci_enable_msi(phba->pcidev);
6586 if (!rc)
6587 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6588 "0487 PCI enable MSI mode success.\n");
6589 else {
6590 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6591 "0488 PCI enable MSI mode failed (%d)\n", rc);
6592 return rc;
6593 }
6594
6595 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6596 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6597 if (rc) {
6598 pci_disable_msi(phba->pcidev);
6599 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6600 "0490 MSI request_irq failed (%d)\n", rc);
6601 }
6602
6603 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6604 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6605 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6606 }
6607
6608 return rc;
6609}
6610
6611/**
6612 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6613 * @phba: pointer to lpfc hba data structure.
6614 *
6615 * This routine is invoked to disable the MSI interrupt mode to device with
6616 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6617 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6618 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6619 * its vector.
6620 **/
6621static void
6622lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6623{
6624 free_irq(phba->pcidev->irq, phba);
6625 pci_disable_msi(phba->pcidev);
6626 return;
6627}
6628
6629/**
6630 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6631 * @phba: pointer to lpfc hba data structure.
6632 *
6633 * This routine is invoked to enable device interrupt and associate driver's
6634 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6635 * interface spec. Depends on the interrupt mode configured to the driver,
6636 * the driver will try to fallback from the configured interrupt mode to an
6637 * interrupt mode which is supported by the platform, kernel, and device in
6638 * the order of:
6639 * MSI-X -> MSI -> IRQ.
6640 *
6641 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006642 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006643 * other values - error
6644 **/
6645static uint32_t
6646lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6647{
6648 uint32_t intr_mode = LPFC_INTR_ERROR;
6649 int retval, index;
6650
6651 if (cfg_mode == 2) {
6652 /* Preparation before conf_msi mbox cmd */
6653 retval = 0;
6654 if (!retval) {
6655 /* Now, try to enable MSI-X interrupt mode */
6656 retval = lpfc_sli4_enable_msix(phba);
6657 if (!retval) {
6658 /* Indicate initialization to MSI-X mode */
6659 phba->intr_type = MSIX;
6660 intr_mode = 2;
6661 }
6662 }
6663 }
6664
6665 /* Fallback to MSI if MSI-X initialization failed */
6666 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6667 retval = lpfc_sli4_enable_msi(phba);
6668 if (!retval) {
6669 /* Indicate initialization to MSI mode */
6670 phba->intr_type = MSI;
6671 intr_mode = 1;
6672 }
6673 }
6674
6675 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6676 if (phba->intr_type == NONE) {
6677 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6678 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6679 if (!retval) {
6680 /* Indicate initialization to INTx mode */
6681 phba->intr_type = INTx;
6682 intr_mode = 0;
6683 for (index = 0; index < phba->cfg_fcp_eq_count;
6684 index++) {
6685 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6686 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6687 }
6688 }
6689 }
6690 return intr_mode;
6691}
6692
6693/**
6694 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6695 * @phba: pointer to lpfc hba data structure.
6696 *
6697 * This routine is invoked to disable device interrupt and disassociate
6698 * the driver's interrupt handler(s) from interrupt vector(s) to device
6699 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6700 * will release the interrupt vector(s) for the message signaled interrupt.
6701 **/
6702static void
6703lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6704{
6705 /* Disable the currently initialized interrupt mode */
6706 if (phba->intr_type == MSIX)
6707 lpfc_sli4_disable_msix(phba);
6708 else if (phba->intr_type == MSI)
6709 lpfc_sli4_disable_msi(phba);
6710 else if (phba->intr_type == INTx)
6711 free_irq(phba->pcidev->irq, phba);
6712
6713 /* Reset interrupt management states */
6714 phba->intr_type = NONE;
6715 phba->sli.slistat.sli_intr = 0;
6716
6717 return;
6718}
6719
6720/**
James Smart3772a992009-05-22 14:50:54 -04006721 * lpfc_unset_hba - Unset SLI3 hba device initialization
6722 * @phba: pointer to lpfc hba data structure.
6723 *
6724 * This routine is invoked to unset the HBA device initialization steps to
6725 * a device with SLI-3 interface spec.
6726 **/
6727static void
6728lpfc_unset_hba(struct lpfc_hba *phba)
6729{
6730 struct lpfc_vport *vport = phba->pport;
6731 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6732
6733 spin_lock_irq(shost->host_lock);
6734 vport->load_flag |= FC_UNLOADING;
6735 spin_unlock_irq(shost->host_lock);
6736
6737 lpfc_stop_hba_timers(phba);
6738
6739 phba->pport->work_port_events = 0;
6740
6741 lpfc_sli_hba_down(phba);
6742
6743 lpfc_sli_brdrestart(phba);
6744
6745 lpfc_sli_disable_intr(phba);
6746
6747 return;
6748}
6749
6750/**
James Smartda0436e2009-05-22 14:51:39 -04006751 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6752 * @phba: pointer to lpfc hba data structure.
6753 *
6754 * This routine is invoked to unset the HBA device initialization steps to
6755 * a device with SLI-4 interface spec.
6756 **/
6757static void
6758lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6759{
6760 struct lpfc_vport *vport = phba->pport;
6761 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6762
6763 spin_lock_irq(shost->host_lock);
6764 vport->load_flag |= FC_UNLOADING;
6765 spin_unlock_irq(shost->host_lock);
6766
6767 phba->pport->work_port_events = 0;
6768
6769 lpfc_sli4_hba_down(phba);
6770
6771 lpfc_sli4_disable_intr(phba);
6772
6773 return;
6774}
6775
6776/**
6777 * lpfc_sli4_hba_unset - Unset the fcoe hba
6778 * @phba: Pointer to HBA context object.
6779 *
6780 * This function is called in the SLI4 code path to reset the HBA's FCoE
6781 * function. The caller is not required to hold any lock. This routine
6782 * issues PCI function reset mailbox command to reset the FCoE function.
6783 * At the end of the function, it calls lpfc_hba_down_post function to
6784 * free any pending commands.
6785 **/
6786static void
6787lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6788{
6789 int wait_cnt = 0;
6790 LPFC_MBOXQ_t *mboxq;
6791
6792 lpfc_stop_hba_timers(phba);
6793 phba->sli4_hba.intr_enable = 0;
6794
6795 /*
6796 * Gracefully wait out the potential current outstanding asynchronous
6797 * mailbox command.
6798 */
6799
6800 /* First, block any pending async mailbox command from posted */
6801 spin_lock_irq(&phba->hbalock);
6802 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6803 spin_unlock_irq(&phba->hbalock);
6804 /* Now, trying to wait it out if we can */
6805 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6806 msleep(10);
6807 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
6808 break;
6809 }
6810 /* Forcefully release the outstanding mailbox command if timed out */
6811 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6812 spin_lock_irq(&phba->hbalock);
6813 mboxq = phba->sli.mbox_active;
6814 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6815 __lpfc_mbox_cmpl_put(phba, mboxq);
6816 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6817 phba->sli.mbox_active = NULL;
6818 spin_unlock_irq(&phba->hbalock);
6819 }
6820
6821 /* Tear down the queues in the HBA */
6822 lpfc_sli4_queue_unset(phba);
6823
6824 /* Disable PCI subsystem interrupt */
6825 lpfc_sli4_disable_intr(phba);
6826
6827 /* Stop kthread signal shall trigger work_done one more time */
6828 kthread_stop(phba->worker_thread);
6829
6830 /* Stop the SLI4 device port */
6831 phba->pport->work_port_events = 0;
6832}
6833
6834/**
James Smart3772a992009-05-22 14:50:54 -04006835 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
6836 * @pdev: pointer to PCI device
6837 * @pid: pointer to PCI device identifier
6838 *
6839 * This routine is to be called to attach a device with SLI-3 interface spec
6840 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6841 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
6842 * information of the device and driver to see if the driver state that it can
6843 * support this kind of device. If the match is successful, the driver core
6844 * invokes this routine. If this routine determines it can claim the HBA, it
6845 * does all the initialization that it needs to do to handle the HBA properly.
6846 *
6847 * Return code
6848 * 0 - driver can claim the device
6849 * negative value - driver can not claim the device
6850 **/
6851static int __devinit
6852lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
6853{
6854 struct lpfc_hba *phba;
6855 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04006856 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04006857 int error;
6858 uint32_t cfg_mode, intr_mode;
6859
6860 /* Allocate memory for HBA structure */
6861 phba = lpfc_hba_alloc(pdev);
6862 if (!phba)
6863 return -ENOMEM;
6864
6865 /* Perform generic PCI device enabling operation */
6866 error = lpfc_enable_pci_dev(phba);
6867 if (error) {
6868 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6869 "1401 Failed to enable pci device.\n");
6870 goto out_free_phba;
6871 }
6872
6873 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
6874 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
6875 if (error)
6876 goto out_disable_pci_dev;
6877
6878 /* Set up SLI-3 specific device PCI memory space */
6879 error = lpfc_sli_pci_mem_setup(phba);
6880 if (error) {
6881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6882 "1402 Failed to set up pci memory space.\n");
6883 goto out_disable_pci_dev;
6884 }
6885
6886 /* Set up phase-1 common device driver resources */
6887 error = lpfc_setup_driver_resource_phase1(phba);
6888 if (error) {
6889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6890 "1403 Failed to set up driver resource.\n");
6891 goto out_unset_pci_mem_s3;
6892 }
6893
6894 /* Set up SLI-3 specific device driver resources */
6895 error = lpfc_sli_driver_resource_setup(phba);
6896 if (error) {
6897 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6898 "1404 Failed to set up driver resource.\n");
6899 goto out_unset_pci_mem_s3;
6900 }
6901
6902 /* Initialize and populate the iocb list per host */
6903 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
6904 if (error) {
6905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6906 "1405 Failed to initialize iocb list.\n");
6907 goto out_unset_driver_resource_s3;
6908 }
6909
6910 /* Set up common device driver resources */
6911 error = lpfc_setup_driver_resource_phase2(phba);
6912 if (error) {
6913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6914 "1406 Failed to set up driver resource.\n");
6915 goto out_free_iocb_list;
6916 }
6917
6918 /* Create SCSI host to the physical port */
6919 error = lpfc_create_shost(phba);
6920 if (error) {
6921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6922 "1407 Failed to create scsi host.\n");
6923 goto out_unset_driver_resource;
6924 }
6925
6926 /* Configure sysfs attributes */
6927 vport = phba->pport;
6928 error = lpfc_alloc_sysfs_attr(vport);
6929 if (error) {
6930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6931 "1476 Failed to allocate sysfs attr\n");
6932 goto out_destroy_shost;
6933 }
6934
James Smart6669f9b2009-10-02 15:16:45 -04006935 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04006936 /* Now, trying to enable interrupt and bring up the device */
6937 cfg_mode = phba->cfg_use_msi;
6938 while (true) {
6939 /* Put device to a known state before enabling interrupt */
6940 lpfc_stop_port(phba);
6941 /* Configure and enable interrupt */
6942 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
6943 if (intr_mode == LPFC_INTR_ERROR) {
6944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6945 "0431 Failed to enable interrupt.\n");
6946 error = -ENODEV;
6947 goto out_free_sysfs_attr;
6948 }
6949 /* SLI-3 HBA setup */
6950 if (lpfc_sli_hba_setup(phba)) {
6951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6952 "1477 Failed to set up hba\n");
6953 error = -ENODEV;
6954 goto out_remove_device;
6955 }
6956
6957 /* Wait 50ms for the interrupts of previous mailbox commands */
6958 msleep(50);
6959 /* Check active interrupts on message signaled interrupts */
6960 if (intr_mode == 0 ||
6961 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
6962 /* Log the current active interrupt mode */
6963 phba->intr_mode = intr_mode;
6964 lpfc_log_intr_mode(phba, intr_mode);
6965 break;
6966 } else {
6967 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6968 "0447 Configure interrupt mode (%d) "
6969 "failed active interrupt test.\n",
6970 intr_mode);
6971 /* Disable the current interrupt mode */
6972 lpfc_sli_disable_intr(phba);
6973 /* Try next level of interrupt mode */
6974 cfg_mode = --intr_mode;
6975 }
6976 }
6977
6978 /* Perform post initialization setup */
6979 lpfc_post_init_setup(phba);
6980
6981 /* Check if there are static vports to be created. */
6982 lpfc_create_static_vport(phba);
6983
6984 return 0;
6985
6986out_remove_device:
6987 lpfc_unset_hba(phba);
6988out_free_sysfs_attr:
6989 lpfc_free_sysfs_attr(vport);
6990out_destroy_shost:
6991 lpfc_destroy_shost(phba);
6992out_unset_driver_resource:
6993 lpfc_unset_driver_resource_phase2(phba);
6994out_free_iocb_list:
6995 lpfc_free_iocb_list(phba);
6996out_unset_driver_resource_s3:
6997 lpfc_sli_driver_resource_unset(phba);
6998out_unset_pci_mem_s3:
6999 lpfc_sli_pci_mem_unset(phba);
7000out_disable_pci_dev:
7001 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007002 if (shost)
7003 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007004out_free_phba:
7005 lpfc_hba_free(phba);
7006 return error;
7007}
7008
7009/**
7010 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007011 * @pdev: pointer to PCI device
7012 *
James Smart3772a992009-05-22 14:50:54 -04007013 * This routine is to be called to disattach a device with SLI-3 interface
7014 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7015 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7016 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007017 **/
dea31012005-04-17 16:05:31 -05007018static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007019lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007020{
James Smart2e0fef82007-06-17 19:56:36 -05007021 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7022 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007023 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007024 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007025 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007026 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7027
James Smart549e55c2007-08-02 11:09:51 -04007028 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007029 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007030 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007031
James Smart858c9f62007-06-17 19:56:39 -05007032 lpfc_free_sysfs_attr(vport);
7033
James Smarteada2722008-12-04 22:39:13 -05007034 /* Release all the vports against this physical port */
7035 vports = lpfc_create_vport_work_array(phba);
7036 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007037 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007038 fc_vport_terminate(vports[i]->fc_vport);
7039 lpfc_destroy_vport_work_array(phba, vports);
7040
7041 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007042 fc_remove_host(shost);
7043 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007044 lpfc_cleanup(vport);
7045
James Smart2e0fef82007-06-17 19:56:36 -05007046 /*
7047 * Bring down the SLI Layer. This step disable all interrupts,
7048 * clears the rings, discards all mailbox commands, and resets
7049 * the HBA.
7050 */
James Smarta257bf92009-04-06 18:48:10 -04007051
7052 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007053 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007054 /* Stop kthread signal shall trigger work_done one more time */
7055 kthread_stop(phba->worker_thread);
7056 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007057 lpfc_sli_brdrestart(phba);
7058
James Smart3772a992009-05-22 14:50:54 -04007059 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007060 spin_lock_irq(&phba->hbalock);
7061 list_del_init(&vport->listentry);
7062 spin_unlock_irq(&phba->hbalock);
7063
James Smart858c9f62007-06-17 19:56:39 -05007064 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007065
James Smart5b75da22008-12-04 22:39:35 -05007066 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007067 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007068
7069 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007070 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007071
7072 /*
7073 * Call scsi_free before mem_free since scsi bufs are released to their
7074 * corresponding pools here.
7075 */
7076 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007077 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007078
James Smart34b02dc2008-08-24 21:49:55 -04007079 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7080 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007081
James Smart2e0fef82007-06-17 19:56:36 -05007082 /* Free resources associated with SLI2 interface */
7083 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007084 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007085
7086 /* unmap adapter SLIM and Control Registers */
7087 iounmap(phba->ctrl_regs_memmap_p);
7088 iounmap(phba->slim_memmap_p);
7089
James Smart3772a992009-05-22 14:50:54 -04007090 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007091
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007092 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007093 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007094}
7095
Linas Vepstas8d63f372007-02-14 14:28:36 -06007096/**
James Smart3772a992009-05-22 14:50:54 -04007097 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007098 * @pdev: pointer to PCI device
7099 * @msg: power management message
7100 *
James Smart3772a992009-05-22 14:50:54 -04007101 * This routine is to be called from the kernel's PCI subsystem to support
7102 * system Power Management (PM) to device with SLI-3 interface spec. When
7103 * PM invokes this method, it quiesces the device by stopping the driver's
7104 * worker thread for the device, turning off device's interrupt and DMA,
7105 * and bring the device offline. Note that as the driver implements the
7106 * minimum PM requirements to a power-aware driver's PM support for the
7107 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7108 * to the suspend() method call will be treated as SUSPEND and the driver will
7109 * fully reinitialize its device during resume() method call, the driver will
7110 * set device to PCI_D3hot state in PCI config space instead of setting it
7111 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007112 *
7113 * Return code
James Smart3772a992009-05-22 14:50:54 -04007114 * 0 - driver suspended the device
7115 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007116 **/
7117static int
James Smart3772a992009-05-22 14:50:54 -04007118lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007119{
7120 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7121 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7122
7123 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7124 "0473 PCI device Power Management suspend.\n");
7125
7126 /* Bring down the device */
7127 lpfc_offline_prep(phba);
7128 lpfc_offline(phba);
7129 kthread_stop(phba->worker_thread);
7130
7131 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007132 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007133
7134 /* Save device state to PCI config space */
7135 pci_save_state(pdev);
7136 pci_set_power_state(pdev, PCI_D3hot);
7137
7138 return 0;
7139}
7140
7141/**
James Smart3772a992009-05-22 14:50:54 -04007142 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007143 * @pdev: pointer to PCI device
7144 *
James Smart3772a992009-05-22 14:50:54 -04007145 * This routine is to be called from the kernel's PCI subsystem to support
7146 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7147 * invokes this method, it restores the device's PCI config space state and
7148 * fully reinitializes the device and brings it online. Note that as the
7149 * driver implements the minimum PM requirements to a power-aware driver's
7150 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7151 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7152 * driver will fully reinitialize its device during resume() method call,
7153 * the device will be set to PCI_D0 directly in PCI config space before
7154 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007155 *
7156 * Return code
James Smart3772a992009-05-22 14:50:54 -04007157 * 0 - driver suspended the device
7158 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007159 **/
7160static int
James Smart3772a992009-05-22 14:50:54 -04007161lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007162{
7163 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7164 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007165 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007166 int error;
7167
7168 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7169 "0452 PCI device Power Management resume.\n");
7170
7171 /* Restore device state from PCI config space */
7172 pci_set_power_state(pdev, PCI_D0);
7173 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007174
James Smart3a55b532008-12-04 22:38:54 -05007175 if (pdev->is_busmaster)
7176 pci_set_master(pdev);
7177
7178 /* Startup the kernel thread for this host adapter. */
7179 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7180 "lpfc_worker_%d", phba->brd_no);
7181 if (IS_ERR(phba->worker_thread)) {
7182 error = PTR_ERR(phba->worker_thread);
7183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7184 "0434 PM resume failed to start worker "
7185 "thread: error=x%x.\n", error);
7186 return error;
7187 }
7188
James Smart5b75da22008-12-04 22:39:35 -05007189 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007190 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007191 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007193 "0430 PM resume Failed to enable interrupt\n");
7194 return -EIO;
7195 } else
7196 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007197
7198 /* Restart HBA and bring it online */
7199 lpfc_sli_brdrestart(phba);
7200 lpfc_online(phba);
7201
James Smart5b75da22008-12-04 22:39:35 -05007202 /* Log the current active interrupt mode */
7203 lpfc_log_intr_mode(phba, phba->intr_mode);
7204
James Smart3a55b532008-12-04 22:38:54 -05007205 return 0;
7206}
7207
7208/**
James Smart891478a2009-11-18 15:40:23 -05007209 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7210 * @phba: pointer to lpfc hba data structure.
7211 *
7212 * This routine is called to prepare the SLI3 device for PCI slot recover. It
7213 * aborts and stops all the on-going I/Os on the pci device.
7214 **/
7215static void
7216lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7217{
7218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7219 "2723 PCI channel I/O abort preparing for recovery\n");
7220 /* Prepare for bringing HBA offline */
7221 lpfc_offline_prep(phba);
7222 /* Clear sli active flag to prevent sysfs access to HBA */
7223 spin_lock_irq(&phba->hbalock);
7224 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7225 spin_unlock_irq(&phba->hbalock);
7226 /* Stop and flush all I/Os and bring HBA offline */
7227 lpfc_offline(phba);
7228}
7229
7230/**
James Smart0d878412009-10-02 15:16:56 -04007231 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7232 * @phba: pointer to lpfc hba data structure.
7233 *
7234 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7235 * disables the device interrupt and pci device, and aborts the internal FCP
7236 * pending I/Os.
7237 **/
7238static void
7239lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7240{
7241 struct lpfc_sli *psli = &phba->sli;
7242 struct lpfc_sli_ring *pring;
7243
7244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007245 "2710 PCI channel disable preparing for reset\n");
James Smart0d878412009-10-02 15:16:56 -04007246 /* Disable interrupt and pci device */
7247 lpfc_sli_disable_intr(phba);
7248 pci_disable_device(phba->pcidev);
7249 /*
7250 * There may be I/Os dropped by the firmware.
7251 * Error iocb (I/O) on txcmplq and let the SCSI layer
7252 * retry it after re-establishing link.
7253 */
7254 pring = &psli->ring[psli->fcp_ring];
7255 lpfc_sli_abort_iocb_ring(phba, pring);
7256}
7257
7258/**
7259 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7260 * @phba: pointer to lpfc hba data structure.
7261 *
7262 * This routine is called to prepare the SLI3 device for PCI slot permanently
7263 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7264 * pending I/Os.
7265 **/
7266static void
7267lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7268{
7269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007270 "2711 PCI channel permanent disable for failure\n");
James Smart0d878412009-10-02 15:16:56 -04007271 /* Clean up all driver's outstanding SCSI I/Os */
7272 lpfc_sli_flush_fcp_rings(phba);
7273}
7274
7275/**
James Smart3772a992009-05-22 14:50:54 -04007276 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007277 * @pdev: pointer to PCI device.
7278 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007279 *
James Smart3772a992009-05-22 14:50:54 -04007280 * This routine is called from the PCI subsystem for I/O error handling to
7281 * device with SLI-3 interface spec. This function is called by the PCI
7282 * subsystem after a PCI bus error affecting this device has been detected.
7283 * When this function is invoked, it will need to stop all the I/Os and
7284 * interrupt(s) to the device. Once that is done, it will return
7285 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7286 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007287 *
7288 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007289 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007290 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7291 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007292 **/
James Smart3772a992009-05-22 14:50:54 -04007293static pci_ers_result_t
7294lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007295{
James Smart51ef4c22007-08-02 11:10:31 -04007296 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7297 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007298
James Smartaacc20e2009-12-21 17:02:51 -05007299 /* Block all SCSI devices' I/Os on the host */
7300 lpfc_scsi_dev_block(phba);
7301
James Smart0d878412009-10-02 15:16:56 -04007302 switch (state) {
7303 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007304 /* Non-fatal error, prepare for recovery */
7305 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007306 return PCI_ERS_RESULT_CAN_RECOVER;
7307 case pci_channel_io_frozen:
7308 /* Fatal error, prepare for slot reset */
7309 lpfc_sli_prep_dev_for_reset(phba);
7310 return PCI_ERS_RESULT_NEED_RESET;
7311 case pci_channel_io_perm_failure:
7312 /* Permanent failure, prepare for device down */
7313 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007314 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007315 default:
7316 /* Unknown state, prepare and request slot reset */
7317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7318 "0472 Unknown PCI error state: x%x\n", state);
7319 lpfc_sli_prep_dev_for_reset(phba);
7320 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007321 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007322}
7323
7324/**
James Smart3772a992009-05-22 14:50:54 -04007325 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007326 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007327 *
James Smart3772a992009-05-22 14:50:54 -04007328 * This routine is called from the PCI subsystem for error handling to
7329 * device with SLI-3 interface spec. This is called after PCI bus has been
7330 * reset to restart the PCI card from scratch, as if from a cold-boot.
7331 * During the PCI subsystem error recovery, after driver returns
7332 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7333 * recovery and then call this routine before calling the .resume method
7334 * to recover the device. This function will initialize the HBA device,
7335 * enable the interrupt, but it will just put the HBA to offline state
7336 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007337 *
7338 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007339 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7340 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007341 */
James Smart3772a992009-05-22 14:50:54 -04007342static pci_ers_result_t
7343lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007344{
James Smart51ef4c22007-08-02 11:10:31 -04007345 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7346 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007347 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007348 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007349
7350 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007351 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007352 printk(KERN_ERR "lpfc: Cannot re-enable "
7353 "PCI device after reset.\n");
7354 return PCI_ERS_RESULT_DISCONNECT;
7355 }
7356
James Smart97207482008-12-04 22:39:19 -05007357 pci_restore_state(pdev);
7358 if (pdev->is_busmaster)
7359 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007360
James Smart92d7f7b2007-06-17 19:56:38 -05007361 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007362 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007363 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007364
James Smart5b75da22008-12-04 22:39:35 -05007365 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007366 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007367 if (intr_mode == LPFC_INTR_ERROR) {
7368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7369 "0427 Cannot re-enable interrupt after "
7370 "slot reset.\n");
7371 return PCI_ERS_RESULT_DISCONNECT;
7372 } else
7373 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007374
7375 /* Take device offline; this will perform cleanup */
7376 lpfc_offline(phba);
7377 lpfc_sli_brdrestart(phba);
7378
James Smart5b75da22008-12-04 22:39:35 -05007379 /* Log the current active interrupt mode */
7380 lpfc_log_intr_mode(phba, phba->intr_mode);
7381
Linas Vepstas8d63f372007-02-14 14:28:36 -06007382 return PCI_ERS_RESULT_RECOVERED;
7383}
7384
7385/**
James Smart3772a992009-05-22 14:50:54 -04007386 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007387 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007388 *
James Smart3772a992009-05-22 14:50:54 -04007389 * This routine is called from the PCI subsystem for error handling to device
7390 * with SLI-3 interface spec. It is called when kernel error recovery tells
7391 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7392 * error recovery. After this call, traffic can start to flow from this device
7393 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007394 */
James Smart3772a992009-05-22 14:50:54 -04007395static void
7396lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007397{
James Smart51ef4c22007-08-02 11:10:31 -04007398 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7399 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007400
James Smart0d878412009-10-02 15:16:56 -04007401 /* Bring the device online */
James Smart58da1ff2008-04-07 10:15:56 -04007402 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04007403
7404 /* Clean up Advanced Error Reporting (AER) if needed */
7405 if (phba->hba_flag & HBA_AER_ENABLED)
7406 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007407}
7408
James Smart3772a992009-05-22 14:50:54 -04007409/**
James Smartda0436e2009-05-22 14:51:39 -04007410 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7411 * @phba: pointer to lpfc hba data structure.
7412 *
7413 * returns the number of ELS/CT IOCBs to reserve
7414 **/
7415int
7416lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7417{
7418 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7419
James Smartf1126682009-06-10 17:22:44 -04007420 if (phba->sli_rev == LPFC_SLI_REV4) {
7421 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04007422 return 10;
James Smartf1126682009-06-10 17:22:44 -04007423 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04007424 return 25;
James Smartf1126682009-06-10 17:22:44 -04007425 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04007426 return 50;
James Smartf1126682009-06-10 17:22:44 -04007427 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04007428 return 100;
James Smartf1126682009-06-10 17:22:44 -04007429 else
James Smart6a9c52c2009-10-02 15:16:51 -04007430 return 150;
James Smartf1126682009-06-10 17:22:44 -04007431 } else
7432 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007433}
7434
7435/**
7436 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7437 * @pdev: pointer to PCI device
7438 * @pid: pointer to PCI device identifier
7439 *
7440 * This routine is called from the kernel's PCI subsystem to device with
7441 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7442 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7443 * information of the device and driver to see if the driver state that it
7444 * can support this kind of device. If the match is successful, the driver
7445 * core invokes this routine. If this routine determines it can claim the HBA,
7446 * it does all the initialization that it needs to do to handle the HBA
7447 * properly.
7448 *
7449 * Return code
7450 * 0 - driver can claim the device
7451 * negative value - driver can not claim the device
7452 **/
7453static int __devinit
7454lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7455{
7456 struct lpfc_hba *phba;
7457 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007458 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007459 int error;
7460 uint32_t cfg_mode, intr_mode;
7461 int mcnt;
7462
7463 /* Allocate memory for HBA structure */
7464 phba = lpfc_hba_alloc(pdev);
7465 if (!phba)
7466 return -ENOMEM;
7467
7468 /* Perform generic PCI device enabling operation */
7469 error = lpfc_enable_pci_dev(phba);
7470 if (error) {
7471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7472 "1409 Failed to enable pci device.\n");
7473 goto out_free_phba;
7474 }
7475
7476 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7477 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7478 if (error)
7479 goto out_disable_pci_dev;
7480
7481 /* Set up SLI-4 specific device PCI memory space */
7482 error = lpfc_sli4_pci_mem_setup(phba);
7483 if (error) {
7484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7485 "1410 Failed to set up pci memory space.\n");
7486 goto out_disable_pci_dev;
7487 }
7488
7489 /* Set up phase-1 common device driver resources */
7490 error = lpfc_setup_driver_resource_phase1(phba);
7491 if (error) {
7492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7493 "1411 Failed to set up driver resource.\n");
7494 goto out_unset_pci_mem_s4;
7495 }
7496
7497 /* Set up SLI-4 Specific device driver resources */
7498 error = lpfc_sli4_driver_resource_setup(phba);
7499 if (error) {
7500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7501 "1412 Failed to set up driver resource.\n");
7502 goto out_unset_pci_mem_s4;
7503 }
7504
7505 /* Initialize and populate the iocb list per host */
7506 error = lpfc_init_iocb_list(phba,
7507 phba->sli4_hba.max_cfg_param.max_xri);
7508 if (error) {
7509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7510 "1413 Failed to initialize iocb list.\n");
7511 goto out_unset_driver_resource_s4;
7512 }
7513
7514 /* Set up common device driver resources */
7515 error = lpfc_setup_driver_resource_phase2(phba);
7516 if (error) {
7517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7518 "1414 Failed to set up driver resource.\n");
7519 goto out_free_iocb_list;
7520 }
7521
7522 /* Create SCSI host to the physical port */
7523 error = lpfc_create_shost(phba);
7524 if (error) {
7525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7526 "1415 Failed to create scsi host.\n");
7527 goto out_unset_driver_resource;
7528 }
7529
7530 /* Configure sysfs attributes */
7531 vport = phba->pport;
7532 error = lpfc_alloc_sysfs_attr(vport);
7533 if (error) {
7534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7535 "1416 Failed to allocate sysfs attr\n");
7536 goto out_destroy_shost;
7537 }
7538
James Smart6669f9b2009-10-02 15:16:45 -04007539 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04007540 /* Now, trying to enable interrupt and bring up the device */
7541 cfg_mode = phba->cfg_use_msi;
7542 while (true) {
7543 /* Put device to a known state before enabling interrupt */
7544 lpfc_stop_port(phba);
7545 /* Configure and enable interrupt */
7546 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7547 if (intr_mode == LPFC_INTR_ERROR) {
7548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7549 "0426 Failed to enable interrupt.\n");
7550 error = -ENODEV;
7551 goto out_free_sysfs_attr;
7552 }
James Smartdef9c7a2009-12-21 17:02:28 -05007553 /* Default to single FCP EQ for non-MSI-X */
7554 if (phba->intr_type != MSIX)
7555 phba->cfg_fcp_eq_count = 1;
James Smartda0436e2009-05-22 14:51:39 -04007556 /* Set up SLI-4 HBA */
7557 if (lpfc_sli4_hba_setup(phba)) {
7558 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7559 "1421 Failed to set up hba\n");
7560 error = -ENODEV;
7561 goto out_disable_intr;
7562 }
7563
7564 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7565 if (intr_mode != 0)
7566 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7567 LPFC_ACT_INTR_CNT);
7568
7569 /* Check active interrupts received only for MSI/MSI-X */
7570 if (intr_mode == 0 ||
7571 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7572 /* Log the current active interrupt mode */
7573 phba->intr_mode = intr_mode;
7574 lpfc_log_intr_mode(phba, intr_mode);
7575 break;
7576 }
7577 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7578 "0451 Configure interrupt mode (%d) "
7579 "failed active interrupt test.\n",
7580 intr_mode);
7581 /* Unset the preivous SLI-4 HBA setup */
7582 lpfc_sli4_unset_hba(phba);
7583 /* Try next level of interrupt mode */
7584 cfg_mode = --intr_mode;
7585 }
7586
7587 /* Perform post initialization setup */
7588 lpfc_post_init_setup(phba);
7589
James Smart1c6834a2009-07-19 10:01:26 -04007590 /* Check if there are static vports to be created. */
7591 lpfc_create_static_vport(phba);
7592
James Smartda0436e2009-05-22 14:51:39 -04007593 return 0;
7594
7595out_disable_intr:
7596 lpfc_sli4_disable_intr(phba);
7597out_free_sysfs_attr:
7598 lpfc_free_sysfs_attr(vport);
7599out_destroy_shost:
7600 lpfc_destroy_shost(phba);
7601out_unset_driver_resource:
7602 lpfc_unset_driver_resource_phase2(phba);
7603out_free_iocb_list:
7604 lpfc_free_iocb_list(phba);
7605out_unset_driver_resource_s4:
7606 lpfc_sli4_driver_resource_unset(phba);
7607out_unset_pci_mem_s4:
7608 lpfc_sli4_pci_mem_unset(phba);
7609out_disable_pci_dev:
7610 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007611 if (shost)
7612 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04007613out_free_phba:
7614 lpfc_hba_free(phba);
7615 return error;
7616}
7617
7618/**
7619 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
7620 * @pdev: pointer to PCI device
7621 *
7622 * This routine is called from the kernel's PCI subsystem to device with
7623 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7624 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7625 * device to be removed from the PCI subsystem properly.
7626 **/
7627static void __devexit
7628lpfc_pci_remove_one_s4(struct pci_dev *pdev)
7629{
7630 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7631 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7632 struct lpfc_vport **vports;
7633 struct lpfc_hba *phba = vport->phba;
7634 int i;
7635
7636 /* Mark the device unloading flag */
7637 spin_lock_irq(&phba->hbalock);
7638 vport->load_flag |= FC_UNLOADING;
7639 spin_unlock_irq(&phba->hbalock);
7640
7641 /* Free the HBA sysfs attributes */
7642 lpfc_free_sysfs_attr(vport);
7643
7644 /* Release all the vports against this physical port */
7645 vports = lpfc_create_vport_work_array(phba);
7646 if (vports != NULL)
7647 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7648 fc_vport_terminate(vports[i]->fc_vport);
7649 lpfc_destroy_vport_work_array(phba, vports);
7650
7651 /* Remove FC host and then SCSI host with the physical port */
7652 fc_remove_host(shost);
7653 scsi_remove_host(shost);
7654
7655 /* Perform cleanup on the physical port */
7656 lpfc_cleanup(vport);
7657
7658 /*
7659 * Bring down the SLI Layer. This step disables all interrupts,
7660 * clears the rings, discards all mailbox commands, and resets
7661 * the HBA FCoE function.
7662 */
7663 lpfc_debugfs_terminate(vport);
7664 lpfc_sli4_hba_unset(phba);
7665
7666 spin_lock_irq(&phba->hbalock);
7667 list_del_init(&vport->listentry);
7668 spin_unlock_irq(&phba->hbalock);
7669
7670 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7671 * buffers are released to their corresponding pools here.
7672 */
7673 lpfc_scsi_free(phba);
7674 lpfc_sli4_driver_resource_unset(phba);
7675
7676 /* Unmap adapter Control and Doorbell registers */
7677 lpfc_sli4_pci_mem_unset(phba);
7678
7679 /* Release PCI resources and disable device's PCI function */
7680 scsi_host_put(shost);
7681 lpfc_disable_pci_dev(phba);
7682
7683 /* Finally, free the driver's device data structure */
7684 lpfc_hba_free(phba);
7685
7686 return;
7687}
7688
7689/**
7690 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
7691 * @pdev: pointer to PCI device
7692 * @msg: power management message
7693 *
7694 * This routine is called from the kernel's PCI subsystem to support system
7695 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7696 * this method, it quiesces the device by stopping the driver's worker
7697 * thread for the device, turning off device's interrupt and DMA, and bring
7698 * the device offline. Note that as the driver implements the minimum PM
7699 * requirements to a power-aware driver's PM support for suspend/resume -- all
7700 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7701 * method call will be treated as SUSPEND and the driver will fully
7702 * reinitialize its device during resume() method call, the driver will set
7703 * device to PCI_D3hot state in PCI config space instead of setting it
7704 * according to the @msg provided by the PM.
7705 *
7706 * Return code
7707 * 0 - driver suspended the device
7708 * Error otherwise
7709 **/
7710static int
7711lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
7712{
7713 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7714 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7715
7716 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7717 "0298 PCI device Power Management suspend.\n");
7718
7719 /* Bring down the device */
7720 lpfc_offline_prep(phba);
7721 lpfc_offline(phba);
7722 kthread_stop(phba->worker_thread);
7723
7724 /* Disable interrupt from device */
7725 lpfc_sli4_disable_intr(phba);
7726
7727 /* Save device state to PCI config space */
7728 pci_save_state(pdev);
7729 pci_set_power_state(pdev, PCI_D3hot);
7730
7731 return 0;
7732}
7733
7734/**
7735 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
7736 * @pdev: pointer to PCI device
7737 *
7738 * This routine is called from the kernel's PCI subsystem to support system
7739 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
7740 * this method, it restores the device's PCI config space state and fully
7741 * reinitializes the device and brings it online. Note that as the driver
7742 * implements the minimum PM requirements to a power-aware driver's PM for
7743 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7744 * to the suspend() method call will be treated as SUSPEND and the driver
7745 * will fully reinitialize its device during resume() method call, the device
7746 * will be set to PCI_D0 directly in PCI config space before restoring the
7747 * state.
7748 *
7749 * Return code
7750 * 0 - driver suspended the device
7751 * Error otherwise
7752 **/
7753static int
7754lpfc_pci_resume_one_s4(struct pci_dev *pdev)
7755{
7756 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7757 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7758 uint32_t intr_mode;
7759 int error;
7760
7761 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7762 "0292 PCI device Power Management resume.\n");
7763
7764 /* Restore device state from PCI config space */
7765 pci_set_power_state(pdev, PCI_D0);
7766 pci_restore_state(pdev);
7767 if (pdev->is_busmaster)
7768 pci_set_master(pdev);
7769
7770 /* Startup the kernel thread for this host adapter. */
7771 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7772 "lpfc_worker_%d", phba->brd_no);
7773 if (IS_ERR(phba->worker_thread)) {
7774 error = PTR_ERR(phba->worker_thread);
7775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7776 "0293 PM resume failed to start worker "
7777 "thread: error=x%x.\n", error);
7778 return error;
7779 }
7780
7781 /* Configure and enable interrupt */
7782 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
7783 if (intr_mode == LPFC_INTR_ERROR) {
7784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7785 "0294 PM resume Failed to enable interrupt\n");
7786 return -EIO;
7787 } else
7788 phba->intr_mode = intr_mode;
7789
7790 /* Restart HBA and bring it online */
7791 lpfc_sli_brdrestart(phba);
7792 lpfc_online(phba);
7793
7794 /* Log the current active interrupt mode */
7795 lpfc_log_intr_mode(phba, phba->intr_mode);
7796
7797 return 0;
7798}
7799
7800/**
7801 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
7802 * @pdev: pointer to PCI device.
7803 * @state: the current PCI connection state.
7804 *
7805 * This routine is called from the PCI subsystem for error handling to device
7806 * with SLI-4 interface spec. This function is called by the PCI subsystem
7807 * after a PCI bus error affecting this device has been detected. When this
7808 * function is invoked, it will need to stop all the I/Os and interrupt(s)
7809 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
7810 * for the PCI subsystem to perform proper recovery as desired.
7811 *
7812 * Return codes
7813 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7814 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7815 **/
7816static pci_ers_result_t
7817lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
7818{
7819 return PCI_ERS_RESULT_NEED_RESET;
7820}
7821
7822/**
7823 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
7824 * @pdev: pointer to PCI device.
7825 *
7826 * This routine is called from the PCI subsystem for error handling to device
7827 * with SLI-4 interface spec. It is called after PCI bus has been reset to
7828 * restart the PCI card from scratch, as if from a cold-boot. During the
7829 * PCI subsystem error recovery, after the driver returns
7830 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7831 * recovery and then call this routine before calling the .resume method to
7832 * recover the device. This function will initialize the HBA device, enable
7833 * the interrupt, but it will just put the HBA to offline state without
7834 * passing any I/O traffic.
7835 *
7836 * Return codes
7837 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7838 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7839 */
7840static pci_ers_result_t
7841lpfc_io_slot_reset_s4(struct pci_dev *pdev)
7842{
7843 return PCI_ERS_RESULT_RECOVERED;
7844}
7845
7846/**
7847 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
7848 * @pdev: pointer to PCI device
7849 *
7850 * This routine is called from the PCI subsystem for error handling to device
7851 * with SLI-4 interface spec. It is called when kernel error recovery tells
7852 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7853 * error recovery. After this call, traffic can start to flow from this device
7854 * again.
7855 **/
7856static void
7857lpfc_io_resume_s4(struct pci_dev *pdev)
7858{
7859 return;
7860}
7861
7862/**
James Smart3772a992009-05-22 14:50:54 -04007863 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
7864 * @pdev: pointer to PCI device
7865 * @pid: pointer to PCI device identifier
7866 *
7867 * This routine is to be registered to the kernel's PCI subsystem. When an
7868 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
7869 * at PCI device-specific information of the device and driver to see if the
7870 * driver state that it can support this kind of device. If the match is
7871 * successful, the driver core invokes this routine. This routine dispatches
7872 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
7873 * do all the initialization that it needs to do to handle the HBA device
7874 * properly.
7875 *
7876 * Return code
7877 * 0 - driver can claim the device
7878 * negative value - driver can not claim the device
7879 **/
7880static int __devinit
7881lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
7882{
7883 int rc;
James Smart8fa38512009-07-19 10:01:03 -04007884 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04007885
James Smart8fa38512009-07-19 10:01:03 -04007886 if (pci_read_config_dword(pdev, LPFC_SLIREV_CONF_WORD, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04007887 return -ENODEV;
7888
James Smart8fa38512009-07-19 10:01:03 -04007889 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
7890 (bf_get(lpfc_sli_intf_rev, &intf) == LPFC_SLIREV_CONF_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04007891 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007892 else
James Smart3772a992009-05-22 14:50:54 -04007893 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007894
James Smart3772a992009-05-22 14:50:54 -04007895 return rc;
7896}
7897
7898/**
7899 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
7900 * @pdev: pointer to PCI device
7901 *
7902 * This routine is to be registered to the kernel's PCI subsystem. When an
7903 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
7904 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
7905 * remove routine, which will perform all the necessary cleanup for the
7906 * device to be removed from the PCI subsystem properly.
7907 **/
7908static void __devexit
7909lpfc_pci_remove_one(struct pci_dev *pdev)
7910{
7911 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7912 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7913
7914 switch (phba->pci_dev_grp) {
7915 case LPFC_PCI_DEV_LP:
7916 lpfc_pci_remove_one_s3(pdev);
7917 break;
James Smartda0436e2009-05-22 14:51:39 -04007918 case LPFC_PCI_DEV_OC:
7919 lpfc_pci_remove_one_s4(pdev);
7920 break;
James Smart3772a992009-05-22 14:50:54 -04007921 default:
7922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7923 "1424 Invalid PCI device group: 0x%x\n",
7924 phba->pci_dev_grp);
7925 break;
7926 }
7927 return;
7928}
7929
7930/**
7931 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
7932 * @pdev: pointer to PCI device
7933 * @msg: power management message
7934 *
7935 * This routine is to be registered to the kernel's PCI subsystem to support
7936 * system Power Management (PM). When PM invokes this method, it dispatches
7937 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
7938 * suspend the device.
7939 *
7940 * Return code
7941 * 0 - driver suspended the device
7942 * Error otherwise
7943 **/
7944static int
7945lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
7946{
7947 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7948 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7949 int rc = -ENODEV;
7950
7951 switch (phba->pci_dev_grp) {
7952 case LPFC_PCI_DEV_LP:
7953 rc = lpfc_pci_suspend_one_s3(pdev, msg);
7954 break;
James Smartda0436e2009-05-22 14:51:39 -04007955 case LPFC_PCI_DEV_OC:
7956 rc = lpfc_pci_suspend_one_s4(pdev, msg);
7957 break;
James Smart3772a992009-05-22 14:50:54 -04007958 default:
7959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7960 "1425 Invalid PCI device group: 0x%x\n",
7961 phba->pci_dev_grp);
7962 break;
7963 }
7964 return rc;
7965}
7966
7967/**
7968 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
7969 * @pdev: pointer to PCI device
7970 *
7971 * This routine is to be registered to the kernel's PCI subsystem to support
7972 * system Power Management (PM). When PM invokes this method, it dispatches
7973 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
7974 * resume the device.
7975 *
7976 * Return code
7977 * 0 - driver suspended the device
7978 * Error otherwise
7979 **/
7980static int
7981lpfc_pci_resume_one(struct pci_dev *pdev)
7982{
7983 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7984 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7985 int rc = -ENODEV;
7986
7987 switch (phba->pci_dev_grp) {
7988 case LPFC_PCI_DEV_LP:
7989 rc = lpfc_pci_resume_one_s3(pdev);
7990 break;
James Smartda0436e2009-05-22 14:51:39 -04007991 case LPFC_PCI_DEV_OC:
7992 rc = lpfc_pci_resume_one_s4(pdev);
7993 break;
James Smart3772a992009-05-22 14:50:54 -04007994 default:
7995 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7996 "1426 Invalid PCI device group: 0x%x\n",
7997 phba->pci_dev_grp);
7998 break;
7999 }
8000 return rc;
8001}
8002
8003/**
8004 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8005 * @pdev: pointer to PCI device.
8006 * @state: the current PCI connection state.
8007 *
8008 * This routine is registered to the PCI subsystem for error handling. This
8009 * function is called by the PCI subsystem after a PCI bus error affecting
8010 * this device has been detected. When this routine is invoked, it dispatches
8011 * the action to the proper SLI-3 or SLI-4 device error detected handling
8012 * routine, which will perform the proper error detected operation.
8013 *
8014 * Return codes
8015 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8016 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8017 **/
8018static pci_ers_result_t
8019lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8020{
8021 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8022 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8023 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8024
8025 switch (phba->pci_dev_grp) {
8026 case LPFC_PCI_DEV_LP:
8027 rc = lpfc_io_error_detected_s3(pdev, state);
8028 break;
James Smartda0436e2009-05-22 14:51:39 -04008029 case LPFC_PCI_DEV_OC:
8030 rc = lpfc_io_error_detected_s4(pdev, state);
8031 break;
James Smart3772a992009-05-22 14:50:54 -04008032 default:
8033 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8034 "1427 Invalid PCI device group: 0x%x\n",
8035 phba->pci_dev_grp);
8036 break;
8037 }
8038 return rc;
8039}
8040
8041/**
8042 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8043 * @pdev: pointer to PCI device.
8044 *
8045 * This routine is registered to the PCI subsystem for error handling. This
8046 * function is called after PCI bus has been reset to restart the PCI card
8047 * from scratch, as if from a cold-boot. When this routine is invoked, it
8048 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8049 * routine, which will perform the proper device reset.
8050 *
8051 * Return codes
8052 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8053 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8054 **/
8055static pci_ers_result_t
8056lpfc_io_slot_reset(struct pci_dev *pdev)
8057{
8058 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8059 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8060 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8061
8062 switch (phba->pci_dev_grp) {
8063 case LPFC_PCI_DEV_LP:
8064 rc = lpfc_io_slot_reset_s3(pdev);
8065 break;
James Smartda0436e2009-05-22 14:51:39 -04008066 case LPFC_PCI_DEV_OC:
8067 rc = lpfc_io_slot_reset_s4(pdev);
8068 break;
James Smart3772a992009-05-22 14:50:54 -04008069 default:
8070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8071 "1428 Invalid PCI device group: 0x%x\n",
8072 phba->pci_dev_grp);
8073 break;
8074 }
8075 return rc;
8076}
8077
8078/**
8079 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8080 * @pdev: pointer to PCI device
8081 *
8082 * This routine is registered to the PCI subsystem for error handling. It
8083 * is called when kernel error recovery tells the lpfc driver that it is
8084 * OK to resume normal PCI operation after PCI bus error recovery. When
8085 * this routine is invoked, it dispatches the action to the proper SLI-3
8086 * or SLI-4 device io_resume routine, which will resume the device operation.
8087 **/
8088static void
8089lpfc_io_resume(struct pci_dev *pdev)
8090{
8091 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8092 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8093
8094 switch (phba->pci_dev_grp) {
8095 case LPFC_PCI_DEV_LP:
8096 lpfc_io_resume_s3(pdev);
8097 break;
James Smartda0436e2009-05-22 14:51:39 -04008098 case LPFC_PCI_DEV_OC:
8099 lpfc_io_resume_s4(pdev);
8100 break;
James Smart3772a992009-05-22 14:50:54 -04008101 default:
8102 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8103 "1429 Invalid PCI device group: 0x%x\n",
8104 phba->pci_dev_grp);
8105 break;
8106 }
8107 return;
8108}
8109
dea31012005-04-17 16:05:31 -05008110static struct pci_device_id lpfc_id_table[] = {
8111 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8112 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008113 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8114 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008115 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8116 PCI_ANY_ID, PCI_ANY_ID, },
8117 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8118 PCI_ANY_ID, PCI_ANY_ID, },
8119 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8120 PCI_ANY_ID, PCI_ANY_ID, },
8121 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8122 PCI_ANY_ID, PCI_ANY_ID, },
8123 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8124 PCI_ANY_ID, PCI_ANY_ID, },
8125 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8126 PCI_ANY_ID, PCI_ANY_ID, },
8127 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8128 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008129 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8130 PCI_ANY_ID, PCI_ANY_ID, },
8131 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8132 PCI_ANY_ID, PCI_ANY_ID, },
8133 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8134 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008135 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8136 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008137 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8138 PCI_ANY_ID, PCI_ANY_ID, },
8139 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8140 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008141 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8142 PCI_ANY_ID, PCI_ANY_ID, },
8143 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8144 PCI_ANY_ID, PCI_ANY_ID, },
8145 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8146 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008147 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8148 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008149 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8150 PCI_ANY_ID, PCI_ANY_ID, },
8151 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8152 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008153 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8154 PCI_ANY_ID, PCI_ANY_ID, },
8155 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8156 PCI_ANY_ID, PCI_ANY_ID, },
8157 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8158 PCI_ANY_ID, PCI_ANY_ID, },
8159 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8160 PCI_ANY_ID, PCI_ANY_ID, },
8161 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8162 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008163 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8164 PCI_ANY_ID, PCI_ANY_ID, },
8165 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8166 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008167 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8168 PCI_ANY_ID, PCI_ANY_ID, },
8169 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8170 PCI_ANY_ID, PCI_ANY_ID, },
8171 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8172 PCI_ANY_ID, PCI_ANY_ID, },
8173 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8174 PCI_ANY_ID, PCI_ANY_ID, },
8175 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8176 PCI_ANY_ID, PCI_ANY_ID, },
8177 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8178 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008179 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8180 PCI_ANY_ID, PCI_ANY_ID, },
8181 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8182 PCI_ANY_ID, PCI_ANY_ID, },
8183 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8184 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008185 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8186 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05008187 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8188 PCI_ANY_ID, PCI_ANY_ID, },
8189 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04008190 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008191 { 0 }
8192};
8193
8194MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8195
Linas Vepstas8d63f372007-02-14 14:28:36 -06008196static struct pci_error_handlers lpfc_err_handler = {
8197 .error_detected = lpfc_io_error_detected,
8198 .slot_reset = lpfc_io_slot_reset,
8199 .resume = lpfc_io_resume,
8200};
8201
dea31012005-04-17 16:05:31 -05008202static struct pci_driver lpfc_driver = {
8203 .name = LPFC_DRIVER_NAME,
8204 .id_table = lpfc_id_table,
8205 .probe = lpfc_pci_probe_one,
8206 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008207 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008208 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008209 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008210};
8211
James Smarte59058c2008-08-24 21:49:00 -04008212/**
James Smart3621a712009-04-06 18:47:14 -04008213 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008214 *
8215 * This routine is to be invoked when the lpfc module is loaded into the
8216 * kernel. The special kernel macro module_init() is used to indicate the
8217 * role of this routine to the kernel as lpfc module entry point.
8218 *
8219 * Return codes
8220 * 0 - successful
8221 * -ENOMEM - FC attach transport failed
8222 * all others - failed
8223 */
dea31012005-04-17 16:05:31 -05008224static int __init
8225lpfc_init(void)
8226{
8227 int error = 0;
8228
8229 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008230 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008231
James Smart7ee5d432007-10-27 13:37:17 -04008232 if (lpfc_enable_npiv) {
8233 lpfc_transport_functions.vport_create = lpfc_vport_create;
8234 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8235 }
dea31012005-04-17 16:05:31 -05008236 lpfc_transport_template =
8237 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008238 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008239 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008240 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008241 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008242 fc_attach_transport(&lpfc_vport_transport_functions);
8243 if (lpfc_vport_transport_template == NULL) {
8244 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008245 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008246 }
James Smart7ee5d432007-10-27 13:37:17 -04008247 }
dea31012005-04-17 16:05:31 -05008248 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008249 if (error) {
dea31012005-04-17 16:05:31 -05008250 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008251 if (lpfc_enable_npiv)
8252 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008253 }
dea31012005-04-17 16:05:31 -05008254
8255 return error;
8256}
8257
James Smarte59058c2008-08-24 21:49:00 -04008258/**
James Smart3621a712009-04-06 18:47:14 -04008259 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008260 *
8261 * This routine is invoked when the lpfc module is removed from the kernel.
8262 * The special kernel macro module_exit() is used to indicate the role of
8263 * this routine to the kernel as lpfc module exit point.
8264 */
dea31012005-04-17 16:05:31 -05008265static void __exit
8266lpfc_exit(void)
8267{
8268 pci_unregister_driver(&lpfc_driver);
8269 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008270 if (lpfc_enable_npiv)
8271 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008272 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008273 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8274 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008275 (1L << _dump_buf_data_order), _dump_buf_data);
8276 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8277 }
8278
8279 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008280 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8281 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008282 (1L << _dump_buf_dif_order), _dump_buf_dif);
8283 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8284 }
dea31012005-04-17 16:05:31 -05008285}
8286
8287module_init(lpfc_init);
8288module_exit(lpfc_exit);
8289MODULE_LICENSE("GPL");
8290MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8291MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8292MODULE_VERSION("0:" LPFC_DRIVER_VERSION);