blob: 88e02a453e0e4f2eac692ef5edbe06a0811ea9d3 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart4fede782010-01-26 23:08:55 -05004 * Copyright (C) 2004-2010 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040031#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050032
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040033#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050034#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_transport_fc.h>
37
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
40#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
44#include "lpfc_scsi.h"
45#include "lpfc.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_version.h"
50
James Smart81301a92008-12-04 22:39:46 -050051char *_dump_buf_data;
52unsigned long _dump_buf_data_order;
53char *_dump_buf_dif;
54unsigned long _dump_buf_dif_order;
55spinlock_t _dump_buf_lock;
56
dea31012005-04-17 16:05:31 -050057static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
58static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040059static int lpfc_sli4_queue_create(struct lpfc_hba *);
60static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
61static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
62static int lpfc_setup_endian_order(struct lpfc_hba *);
63static int lpfc_sli4_read_config(struct lpfc_hba *);
64static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
65static void lpfc_free_sgl_list(struct lpfc_hba *);
66static int lpfc_init_sgl_list(struct lpfc_hba *);
67static int lpfc_init_active_sgl_array(struct lpfc_hba *);
68static void lpfc_free_active_sgl(struct lpfc_hba *);
69static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
70static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
71static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
72static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
73static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050074
75static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050076static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050077static DEFINE_IDR(lpfc_hba_index);
78
James Smarte59058c2008-08-24 21:49:00 -040079/**
James Smart3621a712009-04-06 18:47:14 -040080 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040081 * @phba: pointer to lpfc hba data structure.
82 *
83 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
84 * mailbox command. It retrieves the revision information from the HBA and
85 * collects the Vital Product Data (VPD) about the HBA for preparing the
86 * configuration of the HBA.
87 *
88 * Return codes:
89 * 0 - success.
90 * -ERESTART - requests the SLI layer to reset the HBA and try again.
91 * Any other value - indicates an error.
92 **/
dea31012005-04-17 16:05:31 -050093int
James Smart2e0fef82007-06-17 19:56:36 -050094lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050095{
96 lpfc_vpd_t *vp = &phba->vpd;
97 int i = 0, rc;
98 LPFC_MBOXQ_t *pmb;
99 MAILBOX_t *mb;
100 char *lpfc_vpd_data = NULL;
101 uint16_t offset = 0;
102 static char licensed[56] =
103 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400104 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500105
106 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
107 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500108 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500109 return -ENOMEM;
110 }
111
James Smart04c68492009-05-22 14:52:52 -0400112 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500113 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500114
115 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400116 if (init_key) {
117 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500118
James Smart65a29c12006-07-06 15:50:50 -0400119 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
120 *ptext = cpu_to_be32(*ptext);
121 init_key = 0;
122 }
dea31012005-04-17 16:05:31 -0500123
124 lpfc_read_nv(phba, pmb);
125 memset((char*)mb->un.varRDnvp.rsvd3, 0,
126 sizeof (mb->un.varRDnvp.rsvd3));
127 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
128 sizeof (licensed));
129
130 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
131
132 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500133 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400134 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500135 "error, mbxCmd x%x READ_NVPARM, "
136 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500137 mb->mbxCommand, mb->mbxStatus);
138 mempool_free(pmb, phba->mbox_mem_pool);
139 return -ERESTART;
140 }
141 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500142 sizeof(phba->wwnn));
143 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
144 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500145 }
146
James Smart92d7f7b2007-06-17 19:56:38 -0500147 phba->sli3_options = 0x0;
148
dea31012005-04-17 16:05:31 -0500149 /* Setup and issue mailbox READ REV command */
150 lpfc_read_rev(phba, pmb);
151 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
152 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400154 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500155 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500156 mb->mbxCommand, mb->mbxStatus);
157 mempool_free( pmb, phba->mbox_mem_pool);
158 return -ERESTART;
159 }
160
James Smart92d7f7b2007-06-17 19:56:38 -0500161
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500162 /*
163 * The value of rr must be 1 since the driver set the cv field to 1.
164 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500165 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500166 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500167 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400169 "0440 Adapter failed to init, READ_REV has "
170 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500171 mempool_free(pmb, phba->mbox_mem_pool);
172 return -ERESTART;
dea31012005-04-17 16:05:31 -0500173 }
174
James Smart495a7142008-06-14 22:52:59 -0400175 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
176 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500177 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400178 }
James Smarted957682007-06-17 19:56:37 -0500179
dea31012005-04-17 16:05:31 -0500180 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500181 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500182 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500183 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
184 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
185 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
186 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500187 vp->rev.biuRev = mb->un.varRdRev.biuRev;
188 vp->rev.smRev = mb->un.varRdRev.smRev;
189 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
190 vp->rev.endecRev = mb->un.varRdRev.endecRev;
191 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
192 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
193 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
194 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
195 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
196 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
197
James Smart92d7f7b2007-06-17 19:56:38 -0500198 /* If the sli feature level is less then 9, we must
199 * tear down all RPIs and VPIs on link down if NPIV
200 * is enabled.
201 */
202 if (vp->rev.feaLevelHigh < 9)
203 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
204
dea31012005-04-17 16:05:31 -0500205 if (lpfc_is_LC_HBA(phba->pcidev->device))
206 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
207 sizeof (phba->RandomData));
208
dea31012005-04-17 16:05:31 -0500209 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500210 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
211 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400212 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500213
214 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400215 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500216 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
217
218 if (rc != MBX_SUCCESS) {
219 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400220 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500221 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500222 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500223 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500224 }
James Smart04c68492009-05-22 14:52:52 -0400225 /* dump mem may return a zero when finished or we got a
226 * mailbox error, either way we are done.
227 */
228 if (mb->un.varDmp.word_cnt == 0)
229 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500230 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
231 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400232 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
233 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500234 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500235 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500236 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
237 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500238
239 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500240out_free_mbox:
241 mempool_free(pmb, phba->mbox_mem_pool);
242 return 0;
243}
244
James Smarte59058c2008-08-24 21:49:00 -0400245/**
James Smart3621a712009-04-06 18:47:14 -0400246 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400247 * @phba: pointer to lpfc hba data structure.
248 * @pmboxq: pointer to the driver internal queue element for mailbox command.
249 *
250 * This is the completion handler for driver's configuring asynchronous event
251 * mailbox command to the device. If the mailbox command returns successfully,
252 * it will set internal async event support flag to 1; otherwise, it will
253 * set internal async event support flag to 0.
254 **/
James Smart57127f12007-10-27 13:37:05 -0400255static void
256lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
257{
James Smart04c68492009-05-22 14:52:52 -0400258 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400259 phba->temp_sensor_support = 1;
260 else
261 phba->temp_sensor_support = 0;
262 mempool_free(pmboxq, phba->mbox_mem_pool);
263 return;
264}
265
James Smarte59058c2008-08-24 21:49:00 -0400266/**
James Smart3621a712009-04-06 18:47:14 -0400267 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500268 * @phba: pointer to lpfc hba data structure.
269 * @pmboxq: pointer to the driver internal queue element for mailbox command.
270 *
271 * This is the completion handler for dump mailbox command for getting
272 * wake up parameters. When this command complete, the response contain
273 * Option rom version of the HBA. This function translate the version number
274 * into a human readable string and store it in OptionROMVersion.
275 **/
276static void
277lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
278{
279 struct prog_id *prg;
280 uint32_t prog_id_word;
281 char dist = ' ';
282 /* character array used for decoding dist type. */
283 char dist_char[] = "nabx";
284
James Smart04c68492009-05-22 14:52:52 -0400285 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500286 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500287 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500288 }
James Smart97207482008-12-04 22:39:19 -0500289
290 prg = (struct prog_id *) &prog_id_word;
291
292 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400293 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500294
295 /* Decode the Option rom version word to a readable string */
296 if (prg->dist < 4)
297 dist = dist_char[prg->dist];
298
299 if ((prg->dist == 3) && (prg->num == 0))
300 sprintf(phba->OptionROMVersion, "%d.%d%d",
301 prg->ver, prg->rev, prg->lev);
302 else
303 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
304 prg->ver, prg->rev, prg->lev,
305 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500306 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500307 return;
308}
309
310/**
James Smart3621a712009-04-06 18:47:14 -0400311 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400312 * @phba: pointer to lpfc hba data structure.
313 *
314 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
315 * command call. It performs all internal resource and state setups on the
316 * port: post IOCB buffers, enable appropriate host interrupt attentions,
317 * ELS ring timers, etc.
318 *
319 * Return codes
320 * 0 - success.
321 * Any other value - error.
322 **/
dea31012005-04-17 16:05:31 -0500323int
James Smart2e0fef82007-06-17 19:56:36 -0500324lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500325{
James Smart2e0fef82007-06-17 19:56:36 -0500326 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400327 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500328 LPFC_MBOXQ_t *pmb;
329 MAILBOX_t *mb;
330 struct lpfc_dmabuf *mp;
331 struct lpfc_sli *psli = &phba->sli;
332 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500333 int i, j;
334 int rc;
dea31012005-04-17 16:05:31 -0500335
James Smart7af67052007-10-27 13:38:11 -0400336 spin_lock_irq(&phba->hbalock);
337 /*
338 * If the Config port completed correctly the HBA is not
339 * over heated any more.
340 */
341 if (phba->over_temp_state == HBA_OVER_TEMP)
342 phba->over_temp_state = HBA_NORMAL_TEMP;
343 spin_unlock_irq(&phba->hbalock);
344
dea31012005-04-17 16:05:31 -0500345 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
346 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500347 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500348 return -ENOMEM;
349 }
James Smart04c68492009-05-22 14:52:52 -0400350 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500351
dea31012005-04-17 16:05:31 -0500352 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500353 rc = lpfc_read_sparam(phba, pmb, 0);
354 if (rc) {
355 mempool_free(pmb, phba->mbox_mem_pool);
356 return -ENOMEM;
357 }
358
James Smarted957682007-06-17 19:56:37 -0500359 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500360 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400362 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500363 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500364 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500365 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500366 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500367 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500368 lpfc_mbuf_free(phba, mp->virt, mp->phys);
369 kfree(mp);
370 return -EIO;
371 }
372
373 mp = (struct lpfc_dmabuf *) pmb->context1;
374
James Smart2e0fef82007-06-17 19:56:36 -0500375 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500376 lpfc_mbuf_free(phba, mp->virt, mp->phys);
377 kfree(mp);
378 pmb->context1 = NULL;
379
James Smarta12e07b2006-12-02 13:35:30 -0500380 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500381 u64_to_wwn(phba->cfg_soft_wwnn,
382 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400383 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500384 u64_to_wwn(phba->cfg_soft_wwpn,
385 vport->fc_sparam.portName.u.wwn);
386 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500387 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500388 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500389 sizeof (struct lpfc_name));
James Smarta257bf92009-04-06 18:48:10 -0400390
391 /* Update the fc_host data structures with new wwn. */
392 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
393 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400394 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400395
dea31012005-04-17 16:05:31 -0500396 /* If no serial number in VPD data, use low 6 bytes of WWNN */
397 /* This should be consolidated into parse_vpd ? - mr */
398 if (phba->SerialNumber[0] == 0) {
399 uint8_t *outptr;
400
James Smart2e0fef82007-06-17 19:56:36 -0500401 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500402 for (i = 0; i < 12; i++) {
403 status = *outptr++;
404 j = ((status & 0xf0) >> 4);
405 if (j <= 9)
406 phba->SerialNumber[i] =
407 (char)((uint8_t) 0x30 + (uint8_t) j);
408 else
409 phba->SerialNumber[i] =
410 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
411 i++;
412 j = (status & 0xf);
413 if (j <= 9)
414 phba->SerialNumber[i] =
415 (char)((uint8_t) 0x30 + (uint8_t) j);
416 else
417 phba->SerialNumber[i] =
418 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
419 }
420 }
421
dea31012005-04-17 16:05:31 -0500422 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500423 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500424 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400426 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500427 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500428 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500429 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500430 mempool_free( pmb, phba->mbox_mem_pool);
431 return -EIO;
432 }
433
James Smarta0c87cb2009-07-19 10:01:10 -0400434 /* Check if the port is disabled */
435 lpfc_sli_read_link_ste(phba);
436
dea31012005-04-17 16:05:31 -0500437 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
438 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
439 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400440 (mb->un.varRdConfig.max_xri + 1) -
441 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500442
443 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500444
445 /* Get the default values for Model Name and Description */
446 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
447
448 if ((phba->cfg_link_speed > LINK_SPEED_10G)
449 || ((phba->cfg_link_speed == LINK_SPEED_1G)
450 && !(phba->lmt & LMT_1Gb))
451 || ((phba->cfg_link_speed == LINK_SPEED_2G)
452 && !(phba->lmt & LMT_2Gb))
453 || ((phba->cfg_link_speed == LINK_SPEED_4G)
454 && !(phba->lmt & LMT_4Gb))
455 || ((phba->cfg_link_speed == LINK_SPEED_8G)
456 && !(phba->lmt & LMT_8Gb))
457 || ((phba->cfg_link_speed == LINK_SPEED_10G)
458 && !(phba->lmt & LMT_10Gb))) {
459 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500460 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400461 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500462 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500463 phba->cfg_link_speed);
464 phba->cfg_link_speed = LINK_SPEED_AUTO;
465 }
466
James Smart2e0fef82007-06-17 19:56:36 -0500467 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500468
James Smart0b727fe2007-10-27 13:37:25 -0400469 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500470 if (psli->ring[psli->extra_ring].cmdringaddr)
471 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500472 if (psli->ring[psli->fcp_ring].cmdringaddr)
473 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
474 if (psli->ring[psli->next_ring].cmdringaddr)
475 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
476
477 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500478 if (phba->sli_rev != 3)
479 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500480
James Smart93996272008-08-24 21:50:30 -0400481 /*
482 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
483 */
484 if (phba->intr_type == MSIX) {
485 rc = lpfc_config_msi(phba, pmb);
486 if (rc) {
487 mempool_free(pmb, phba->mbox_mem_pool);
488 return -EIO;
489 }
490 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
491 if (rc != MBX_SUCCESS) {
492 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
493 "0352 Config MSI mailbox command "
494 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400495 pmb->u.mb.mbxCommand,
496 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400497 mempool_free(pmb, phba->mbox_mem_pool);
498 return -EIO;
499 }
500 }
501
James Smart04c68492009-05-22 14:52:52 -0400502 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400503 /* Initialize ERATT handling flag */
504 phba->hba_flag &= ~HBA_ERATT_HANDLED;
505
dea31012005-04-17 16:05:31 -0500506 /* Enable appropriate host interrupts */
dea31012005-04-17 16:05:31 -0500507 status = readl(phba->HCregaddr);
508 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
509 if (psli->num_rings > 0)
510 status |= HC_R0INT_ENA;
511 if (psli->num_rings > 1)
512 status |= HC_R1INT_ENA;
513 if (psli->num_rings > 2)
514 status |= HC_R2INT_ENA;
515 if (psli->num_rings > 3)
516 status |= HC_R3INT_ENA;
517
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500518 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
519 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400520 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500521
dea31012005-04-17 16:05:31 -0500522 writel(status, phba->HCregaddr);
523 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500524 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500525
James Smart93996272008-08-24 21:50:30 -0400526 /* Set up ring-0 (ELS) timer */
527 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500528 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400529 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500530 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
531 phba->hb_outstanding = 0;
532 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400533 /* Set up error attention (ERATT) polling timer */
534 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500535
James Smarta0c87cb2009-07-19 10:01:10 -0400536 if (phba->hba_flag & LINK_DISABLED) {
537 lpfc_printf_log(phba,
538 KERN_ERR, LOG_INIT,
539 "2598 Adapter Link is disabled.\n");
540 lpfc_down_link(phba, pmb);
541 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
542 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
543 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
544 lpfc_printf_log(phba,
545 KERN_ERR, LOG_INIT,
546 "2599 Adapter failed to issue DOWN_LINK"
547 " mbox command rc 0x%x\n", rc);
548
549 mempool_free(pmb, phba->mbox_mem_pool);
550 return -EIO;
551 }
James Smarte40a02c2010-02-26 14:13:54 -0500552 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smarta0c87cb2009-07-19 10:01:10 -0400553 lpfc_init_link(phba, pmb, phba->cfg_topology,
554 phba->cfg_link_speed);
555 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
556 lpfc_set_loopback_flag(phba);
557 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
558 if (rc != MBX_SUCCESS) {
559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400560 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500561 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500562 mb->mbxCommand, mb->mbxStatus);
563
James Smarta0c87cb2009-07-19 10:01:10 -0400564 /* Clear all interrupt enable conditions */
565 writel(0, phba->HCregaddr);
566 readl(phba->HCregaddr); /* flush */
567 /* Clear all pending interrupts */
568 writel(0xffffffff, phba->HAregaddr);
569 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500570
James Smarta0c87cb2009-07-19 10:01:10 -0400571 phba->link_state = LPFC_HBA_ERROR;
572 if (rc != MBX_BUSY)
573 mempool_free(pmb, phba->mbox_mem_pool);
574 return -EIO;
575 }
dea31012005-04-17 16:05:31 -0500576 }
577 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400578 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500579 if (!pmb) {
580 phba->link_state = LPFC_HBA_ERROR;
581 return -ENOMEM;
582 }
583
James Smart57127f12007-10-27 13:37:05 -0400584 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
585 pmb->mbox_cmpl = lpfc_config_async_cmpl;
586 pmb->vport = phba->pport;
587 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500588
James Smart57127f12007-10-27 13:37:05 -0400589 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
590 lpfc_printf_log(phba,
591 KERN_ERR,
592 LOG_INIT,
593 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400594 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400595 rc);
596 mempool_free(pmb, phba->mbox_mem_pool);
597 }
James Smart97207482008-12-04 22:39:19 -0500598
599 /* Get Option rom version */
600 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500601 if (!pmb) {
602 phba->link_state = LPFC_HBA_ERROR;
603 return -ENOMEM;
604 }
605
James Smart97207482008-12-04 22:39:19 -0500606 lpfc_dump_wakeup_param(phba, pmb);
607 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
608 pmb->vport = phba->pport;
609 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
610
611 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400613 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500614 mempool_free(pmb, phba->mbox_mem_pool);
615 }
616
James Smartd7c255b2008-08-24 21:50:00 -0400617 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400618}
619
James Smarte59058c2008-08-24 21:49:00 -0400620/**
James Smart84d1b002010-02-12 14:42:33 -0500621 * lpfc_hba_init_link - Initialize the FC link
622 * @phba: pointer to lpfc hba data structure.
623 *
624 * This routine will issue the INIT_LINK mailbox command call.
625 * It is available to other drivers through the lpfc_hba data
626 * structure for use as a delayed link up mechanism with the
627 * module parameter lpfc_suppress_link_up.
628 *
629 * Return code
630 * 0 - success
631 * Any other value - error
632 **/
633int
634lpfc_hba_init_link(struct lpfc_hba *phba)
635{
636 struct lpfc_vport *vport = phba->pport;
637 LPFC_MBOXQ_t *pmb;
638 MAILBOX_t *mb;
639 int rc;
640
641 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
642 if (!pmb) {
643 phba->link_state = LPFC_HBA_ERROR;
644 return -ENOMEM;
645 }
646 mb = &pmb->u.mb;
647 pmb->vport = vport;
648
649 lpfc_init_link(phba, pmb, phba->cfg_topology,
650 phba->cfg_link_speed);
651 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
652 lpfc_set_loopback_flag(phba);
653 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
654 if (rc != MBX_SUCCESS) {
655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
656 "0498 Adapter failed to init, mbxCmd x%x "
657 "INIT_LINK, mbxStatus x%x\n",
658 mb->mbxCommand, mb->mbxStatus);
659 /* Clear all interrupt enable conditions */
660 writel(0, phba->HCregaddr);
661 readl(phba->HCregaddr); /* flush */
662 /* Clear all pending interrupts */
663 writel(0xffffffff, phba->HAregaddr);
664 readl(phba->HAregaddr); /* flush */
665 phba->link_state = LPFC_HBA_ERROR;
666 if (rc != MBX_BUSY)
667 mempool_free(pmb, phba->mbox_mem_pool);
668 return -EIO;
669 }
James Smarte40a02c2010-02-26 14:13:54 -0500670 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart84d1b002010-02-12 14:42:33 -0500671
672 return 0;
673}
674
675/**
676 * lpfc_hba_down_link - this routine downs the FC link
677 *
678 * This routine will issue the DOWN_LINK mailbox command call.
679 * It is available to other drivers through the lpfc_hba data
680 * structure for use to stop the link.
681 *
682 * Return code
683 * 0 - success
684 * Any other value - error
685 **/
686int
687lpfc_hba_down_link(struct lpfc_hba *phba)
688{
689 LPFC_MBOXQ_t *pmb;
690 int rc;
691
692 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
693 if (!pmb) {
694 phba->link_state = LPFC_HBA_ERROR;
695 return -ENOMEM;
696 }
697
698 lpfc_printf_log(phba,
699 KERN_ERR, LOG_INIT,
700 "0491 Adapter Link is disabled.\n");
701 lpfc_down_link(phba, pmb);
702 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
703 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
704 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
705 lpfc_printf_log(phba,
706 KERN_ERR, LOG_INIT,
707 "2522 Adapter failed to issue DOWN_LINK"
708 " mbox command rc 0x%x\n", rc);
709
710 mempool_free(pmb, phba->mbox_mem_pool);
711 return -EIO;
712 }
713 return 0;
714}
715
716/**
James Smart3621a712009-04-06 18:47:14 -0400717 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400718 * @phba: pointer to lpfc HBA data structure.
719 *
720 * This routine will do LPFC uninitialization before the HBA is reset when
721 * bringing down the SLI Layer.
722 *
723 * Return codes
724 * 0 - success.
725 * Any other value - error.
726 **/
dea31012005-04-17 16:05:31 -0500727int
James Smart2e0fef82007-06-17 19:56:36 -0500728lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500729{
James Smart1b32f6a2008-02-08 18:49:39 -0500730 struct lpfc_vport **vports;
731 int i;
James Smart3772a992009-05-22 14:50:54 -0400732
733 if (phba->sli_rev <= LPFC_SLI_REV3) {
734 /* Disable interrupts */
735 writel(0, phba->HCregaddr);
736 readl(phba->HCregaddr); /* flush */
737 }
dea31012005-04-17 16:05:31 -0500738
James Smart1b32f6a2008-02-08 18:49:39 -0500739 if (phba->pport->load_flag & FC_UNLOADING)
740 lpfc_cleanup_discovery_resources(phba->pport);
741 else {
742 vports = lpfc_create_vport_work_array(phba);
743 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400744 for (i = 0; i <= phba->max_vports &&
745 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500746 lpfc_cleanup_discovery_resources(vports[i]);
747 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500748 }
749 return 0;
dea31012005-04-17 16:05:31 -0500750}
751
James Smarte59058c2008-08-24 21:49:00 -0400752/**
James Smart3772a992009-05-22 14:50:54 -0400753 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400754 * @phba: pointer to lpfc HBA data structure.
755 *
756 * This routine will do uninitialization after the HBA is reset when bring
757 * down the SLI Layer.
758 *
759 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200760 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400761 * Any other value - error.
762 **/
James Smart3772a992009-05-22 14:50:54 -0400763static int
764lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500765{
766 struct lpfc_sli *psli = &phba->sli;
767 struct lpfc_sli_ring *pring;
768 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500769 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500770 int i;
771
James Smart92d7f7b2007-06-17 19:56:38 -0500772 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
773 lpfc_sli_hbqbuf_free_all(phba);
774 else {
775 /* Cleanup preposted buffers on the ELS ring */
776 pring = &psli->ring[LPFC_ELS_RING];
777 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
778 list_del(&mp->list);
779 pring->postbufq_cnt--;
780 lpfc_mbuf_free(phba, mp->virt, mp->phys);
781 kfree(mp);
782 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500783 }
784
James Smart09372822008-01-11 01:52:54 -0500785 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500786 for (i = 0; i < psli->num_rings; i++) {
787 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500788
789 /* At this point in time the HBA is either reset or DOA. Either
790 * way, nothing should be on txcmplq as it will NEVER complete.
791 */
792 list_splice_init(&pring->txcmplq, &completions);
793 pring->txcmplq_cnt = 0;
794 spin_unlock_irq(&phba->hbalock);
795
James Smarta257bf92009-04-06 18:48:10 -0400796 /* Cancel all the IOCBs from the completions list */
797 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
798 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500799
Jamie Wellnitz41415862006-02-28 19:25:27 -0500800 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500801 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500802 }
James Smart09372822008-01-11 01:52:54 -0500803 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500804
805 return 0;
806}
James Smartda0436e2009-05-22 14:51:39 -0400807/**
808 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
809 * @phba: pointer to lpfc HBA data structure.
810 *
811 * This routine will do uninitialization after the HBA is reset when bring
812 * down the SLI Layer.
813 *
814 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200815 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400816 * Any other value - error.
817 **/
818static int
819lpfc_hba_down_post_s4(struct lpfc_hba *phba)
820{
821 struct lpfc_scsi_buf *psb, *psb_next;
822 LIST_HEAD(aborts);
823 int ret;
824 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500825 struct lpfc_sglq *sglq_entry = NULL;
826
James Smartda0436e2009-05-22 14:51:39 -0400827 ret = lpfc_hba_down_post_s3(phba);
828 if (ret)
829 return ret;
830 /* At this point in time the HBA is either reset or DOA. Either
831 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
832 * on the lpfc_sgl_list so that it can either be freed if the
833 * driver is unloading or reposted if the driver is restarting
834 * the port.
835 */
836 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
837 /* scsl_buf_list */
838 /* abts_sgl_list_lock required because worker thread uses this
839 * list.
840 */
841 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500842 list_for_each_entry(sglq_entry,
843 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
844 sglq_entry->state = SGL_FREED;
845
James Smartda0436e2009-05-22 14:51:39 -0400846 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
847 &phba->sli4_hba.lpfc_sgl_list);
848 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
849 /* abts_scsi_buf_list_lock required because worker thread uses this
850 * list.
851 */
852 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
853 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
854 &aborts);
855 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
856 spin_unlock_irq(&phba->hbalock);
857
858 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
859 psb->pCmd = NULL;
860 psb->status = IOSTAT_SUCCESS;
861 }
862 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
863 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
864 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
865 return 0;
866}
867
868/**
869 * lpfc_hba_down_post - Wrapper func for hba down post routine
870 * @phba: pointer to lpfc HBA data structure.
871 *
872 * This routine wraps the actual SLI3 or SLI4 routine for performing
873 * uninitialization after the HBA is reset when bring down the SLI Layer.
874 *
875 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200876 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400877 * Any other value - error.
878 **/
879int
880lpfc_hba_down_post(struct lpfc_hba *phba)
881{
882 return (*phba->lpfc_hba_down_post)(phba);
883}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500884
James Smarte59058c2008-08-24 21:49:00 -0400885/**
James Smart3621a712009-04-06 18:47:14 -0400886 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400887 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
888 *
889 * This is the HBA-timer timeout handler registered to the lpfc driver. When
890 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
891 * work-port-events bitmap and the worker thread is notified. This timeout
892 * event will be used by the worker thread to invoke the actual timeout
893 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
894 * be performed in the timeout handler and the HBA timeout event bit shall
895 * be cleared by the worker thread after it has taken the event bitmap out.
896 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100897static void
James Smart858c9f62007-06-17 19:56:39 -0500898lpfc_hb_timeout(unsigned long ptr)
899{
900 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400901 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500902 unsigned long iflag;
903
904 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400905
906 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500907 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400908 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
909 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500910 phba->pport->work_port_events |= WORKER_HB_TMO;
911 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
912
James Smart93996272008-08-24 21:50:30 -0400913 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400914 if (!tmo_posted)
915 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500916 return;
917}
918
James Smarte59058c2008-08-24 21:49:00 -0400919/**
James Smart3621a712009-04-06 18:47:14 -0400920 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400921 * @phba: pointer to lpfc hba data structure.
922 * @pmboxq: pointer to the driver internal queue element for mailbox command.
923 *
924 * This is the callback function to the lpfc heart-beat mailbox command.
925 * If configured, the lpfc driver issues the heart-beat mailbox command to
926 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
927 * heart-beat mailbox command is issued, the driver shall set up heart-beat
928 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
929 * heart-beat outstanding state. Once the mailbox command comes back and
930 * no error conditions detected, the heart-beat mailbox command timer is
931 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
932 * state is cleared for the next heart-beat. If the timer expired with the
933 * heart-beat outstanding state set, the driver will put the HBA offline.
934 **/
James Smart858c9f62007-06-17 19:56:39 -0500935static void
936lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
937{
938 unsigned long drvr_flag;
939
940 spin_lock_irqsave(&phba->hbalock, drvr_flag);
941 phba->hb_outstanding = 0;
942 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
943
James Smart93996272008-08-24 21:50:30 -0400944 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500945 mempool_free(pmboxq, phba->mbox_mem_pool);
946 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
947 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400948 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500949 mod_timer(&phba->hb_tmofunc,
950 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
951 return;
952}
953
James Smarte59058c2008-08-24 21:49:00 -0400954/**
James Smart3621a712009-04-06 18:47:14 -0400955 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400956 * @phba: pointer to lpfc hba data structure.
957 *
958 * This is the actual HBA-timer timeout handler to be invoked by the worker
959 * thread whenever the HBA timer fired and HBA-timeout event posted. This
960 * handler performs any periodic operations needed for the device. If such
961 * periodic event has already been attended to either in the interrupt handler
962 * or by processing slow-ring or fast-ring events within the HBA-timer
963 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
964 * the timer for the next timeout period. If lpfc heart-beat mailbox command
965 * is configured and there is no heart-beat mailbox command outstanding, a
966 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
967 * has been a heart-beat mailbox command outstanding, the HBA shall be put
968 * to offline.
969 **/
James Smart858c9f62007-06-17 19:56:39 -0500970void
971lpfc_hb_timeout_handler(struct lpfc_hba *phba)
972{
James Smart45ed1192009-10-02 15:17:02 -0400973 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -0500974 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500975 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -0400976 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -0500977 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500978 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500979
James Smart45ed1192009-10-02 15:17:02 -0400980 vports = lpfc_create_vport_work_array(phba);
981 if (vports != NULL)
982 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
983 lpfc_rcv_seq_check_edtov(vports[i]);
984 lpfc_destroy_vport_work_array(phba, vports);
985
James Smart858c9f62007-06-17 19:56:39 -0500986 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400987 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500988 (phba->pport->fc_flag & FC_OFFLINE_MODE))
989 return;
990
991 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -0500992
993 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
994 jiffies)) {
995 spin_unlock_irq(&phba->pport->work_port_lock);
996 if (!phba->hb_outstanding)
997 mod_timer(&phba->hb_tmofunc,
998 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
999 else
1000 mod_timer(&phba->hb_tmofunc,
1001 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1002 return;
1003 }
1004 spin_unlock_irq(&phba->pport->work_port_lock);
1005
James Smart0ff10d42008-01-11 01:52:36 -05001006 if (phba->elsbuf_cnt &&
1007 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1008 spin_lock_irq(&phba->hbalock);
1009 list_splice_init(&phba->elsbuf, &completions);
1010 phba->elsbuf_cnt = 0;
1011 phba->elsbuf_prev_cnt = 0;
1012 spin_unlock_irq(&phba->hbalock);
1013
1014 while (!list_empty(&completions)) {
1015 list_remove_head(&completions, buf_ptr,
1016 struct lpfc_dmabuf, list);
1017 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1018 kfree(buf_ptr);
1019 }
1020 }
1021 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1022
James Smart858c9f62007-06-17 19:56:39 -05001023 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001024 if (phba->cfg_enable_hba_heartbeat) {
1025 if (!phba->hb_outstanding) {
1026 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
1027 if (!pmboxq) {
1028 mod_timer(&phba->hb_tmofunc,
1029 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1030 return;
1031 }
1032
1033 lpfc_heart_beat(phba, pmboxq);
1034 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1035 pmboxq->vport = phba->pport;
1036 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
1037
1038 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
1039 mempool_free(pmboxq, phba->mbox_mem_pool);
1040 mod_timer(&phba->hb_tmofunc,
1041 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1042 return;
1043 }
James Smart858c9f62007-06-17 19:56:39 -05001044 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001045 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1046 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -05001047 return;
James Smart13815c82008-01-11 01:52:48 -05001048 } else {
1049 /*
1050 * If heart beat timeout called with hb_outstanding set
1051 * we need to take the HBA offline.
1052 */
1053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1054 "0459 Adapter heartbeat failure, "
1055 "taking this port offline.\n");
1056
1057 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001058 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart13815c82008-01-11 01:52:48 -05001059 spin_unlock_irq(&phba->hbalock);
1060
1061 lpfc_offline_prep(phba);
1062 lpfc_offline(phba);
1063 lpfc_unblock_mgmt_io(phba);
1064 phba->link_state = LPFC_HBA_ERROR;
1065 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -05001066 }
James Smart858c9f62007-06-17 19:56:39 -05001067 }
1068}
1069
James Smarte59058c2008-08-24 21:49:00 -04001070/**
James Smart3621a712009-04-06 18:47:14 -04001071 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001072 * @phba: pointer to lpfc hba data structure.
1073 *
1074 * This routine is called to bring the HBA offline when HBA hardware error
1075 * other than Port Error 6 has been detected.
1076 **/
James Smart09372822008-01-11 01:52:54 -05001077static void
1078lpfc_offline_eratt(struct lpfc_hba *phba)
1079{
1080 struct lpfc_sli *psli = &phba->sli;
1081
1082 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001083 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001084 spin_unlock_irq(&phba->hbalock);
1085 lpfc_offline_prep(phba);
1086
1087 lpfc_offline(phba);
1088 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001089 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001090 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001091 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001092 lpfc_hba_down_post(phba);
1093 lpfc_sli_brdready(phba, HS_MBRDY);
1094 lpfc_unblock_mgmt_io(phba);
1095 phba->link_state = LPFC_HBA_ERROR;
1096 return;
1097}
1098
James Smarte59058c2008-08-24 21:49:00 -04001099/**
James Smartda0436e2009-05-22 14:51:39 -04001100 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1101 * @phba: pointer to lpfc hba data structure.
1102 *
1103 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1104 * other than Port Error 6 has been detected.
1105 **/
1106static void
1107lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1108{
1109 lpfc_offline_prep(phba);
1110 lpfc_offline(phba);
1111 lpfc_sli4_brdreset(phba);
1112 lpfc_hba_down_post(phba);
1113 lpfc_sli4_post_status_check(phba);
1114 lpfc_unblock_mgmt_io(phba);
1115 phba->link_state = LPFC_HBA_ERROR;
1116}
1117
1118/**
James Smarta257bf92009-04-06 18:48:10 -04001119 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1120 * @phba: pointer to lpfc hba data structure.
1121 *
1122 * This routine is invoked to handle the deferred HBA hardware error
1123 * conditions. This type of error is indicated by HBA by setting ER1
1124 * and another ER bit in the host status register. The driver will
1125 * wait until the ER1 bit clears before handling the error condition.
1126 **/
1127static void
1128lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1129{
1130 uint32_t old_host_status = phba->work_hs;
1131 struct lpfc_sli_ring *pring;
1132 struct lpfc_sli *psli = &phba->sli;
1133
James Smartf4b4c682009-05-22 14:53:12 -04001134 /* If the pci channel is offline, ignore possible errors,
1135 * since we cannot communicate with the pci card anyway.
1136 */
1137 if (pci_channel_offline(phba->pcidev)) {
1138 spin_lock_irq(&phba->hbalock);
1139 phba->hba_flag &= ~DEFER_ERATT;
1140 spin_unlock_irq(&phba->hbalock);
1141 return;
1142 }
1143
James Smarta257bf92009-04-06 18:48:10 -04001144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1145 "0479 Deferred Adapter Hardware Error "
1146 "Data: x%x x%x x%x\n",
1147 phba->work_hs,
1148 phba->work_status[0], phba->work_status[1]);
1149
1150 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001151 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001152 spin_unlock_irq(&phba->hbalock);
1153
1154
1155 /*
1156 * Firmware stops when it triggred erratt. That could cause the I/Os
1157 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1158 * SCSI layer retry it after re-establishing link.
1159 */
1160 pring = &psli->ring[psli->fcp_ring];
1161 lpfc_sli_abort_iocb_ring(phba, pring);
1162
1163 /*
1164 * There was a firmware error. Take the hba offline and then
1165 * attempt to restart it.
1166 */
1167 lpfc_offline_prep(phba);
1168 lpfc_offline(phba);
1169
1170 /* Wait for the ER1 bit to clear.*/
1171 while (phba->work_hs & HS_FFER1) {
1172 msleep(100);
1173 phba->work_hs = readl(phba->HSregaddr);
1174 /* If driver is unloading let the worker thread continue */
1175 if (phba->pport->load_flag & FC_UNLOADING) {
1176 phba->work_hs = 0;
1177 break;
1178 }
1179 }
1180
1181 /*
1182 * This is to ptrotect against a race condition in which
1183 * first write to the host attention register clear the
1184 * host status register.
1185 */
1186 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1187 phba->work_hs = old_host_status & ~HS_FFER1;
1188
James Smart3772a992009-05-22 14:50:54 -04001189 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001190 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001191 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001192 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1193 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1194}
1195
James Smart3772a992009-05-22 14:50:54 -04001196static void
1197lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1198{
1199 struct lpfc_board_event_header board_event;
1200 struct Scsi_Host *shost;
1201
1202 board_event.event_type = FC_REG_BOARD_EVENT;
1203 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1204 shost = lpfc_shost_from_vport(phba->pport);
1205 fc_host_post_vendor_event(shost, fc_get_event_number(),
1206 sizeof(board_event),
1207 (char *) &board_event,
1208 LPFC_NL_VENDOR_ID);
1209}
1210
James Smarta257bf92009-04-06 18:48:10 -04001211/**
James Smart3772a992009-05-22 14:50:54 -04001212 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001213 * @phba: pointer to lpfc hba data structure.
1214 *
1215 * This routine is invoked to handle the following HBA hardware error
1216 * conditions:
1217 * 1 - HBA error attention interrupt
1218 * 2 - DMA ring index out of range
1219 * 3 - Mailbox command came back as unknown
1220 **/
James Smart3772a992009-05-22 14:50:54 -04001221static void
1222lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001223{
James Smart2e0fef82007-06-17 19:56:36 -05001224 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001225 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001226 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001227 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001228 unsigned long temperature;
1229 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001230 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001231
Linas Vepstas8d63f372007-02-14 14:28:36 -06001232 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001233 * since we cannot communicate with the pci card anyway.
1234 */
1235 if (pci_channel_offline(phba->pcidev)) {
1236 spin_lock_irq(&phba->hbalock);
1237 phba->hba_flag &= ~DEFER_ERATT;
1238 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001239 return;
James Smart3772a992009-05-22 14:50:54 -04001240 }
1241
James Smart13815c82008-01-11 01:52:48 -05001242 /* If resets are disabled then leave the HBA alone and return */
1243 if (!phba->cfg_enable_hba_reset)
1244 return;
dea31012005-04-17 16:05:31 -05001245
James Smartea2151b2008-09-07 11:52:10 -04001246 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001247 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001248
James Smarta257bf92009-04-06 18:48:10 -04001249 if (phba->hba_flag & DEFER_ERATT)
1250 lpfc_handle_deferred_eratt(phba);
1251
James Smart97eab632008-04-07 10:16:05 -04001252 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -05001253 /* Re-establishing Link */
1254 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001255 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -05001256 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001257 phba->work_hs,
dea31012005-04-17 16:05:31 -05001258 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001259
James Smart92d7f7b2007-06-17 19:56:38 -05001260 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001261 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001262 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001263
1264 /*
1265 * Firmware stops when it triggled erratt with HS_FFER6.
1266 * That could cause the I/Os dropped by the firmware.
1267 * Error iocb (I/O) on txcmplq and let the SCSI layer
1268 * retry it after re-establishing link.
1269 */
1270 pring = &psli->ring[psli->fcp_ring];
1271 lpfc_sli_abort_iocb_ring(phba, pring);
1272
dea31012005-04-17 16:05:31 -05001273 /*
1274 * There was a firmware error. Take the hba offline and then
1275 * attempt to restart it.
1276 */
James Smart46fa3112007-04-25 09:51:45 -04001277 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001278 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001279 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001280 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001281 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001282 return;
1283 }
James Smart46fa3112007-04-25 09:51:45 -04001284 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001285 } else if (phba->work_hs & HS_CRIT_TEMP) {
1286 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1287 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1288 temp_event_data.event_code = LPFC_CRIT_TEMP;
1289 temp_event_data.data = (uint32_t)temperature;
1290
1291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001292 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001293 "(%ld), taking this port offline "
1294 "Data: x%x x%x x%x\n",
1295 temperature, phba->work_hs,
1296 phba->work_status[0], phba->work_status[1]);
1297
1298 shost = lpfc_shost_from_vport(phba->pport);
1299 fc_host_post_vendor_event(shost, fc_get_event_number(),
1300 sizeof(temp_event_data),
1301 (char *) &temp_event_data,
1302 SCSI_NL_VID_TYPE_PCI
1303 | PCI_VENDOR_ID_EMULEX);
1304
James Smart7af67052007-10-27 13:38:11 -04001305 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001306 phba->over_temp_state = HBA_OVER_TEMP;
1307 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001308 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001309
dea31012005-04-17 16:05:31 -05001310 } else {
1311 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001312 * failure is a value other than FFER6. Do not call the offline
1313 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001314 */
1315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001316 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001317 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001318 phba->work_hs,
dea31012005-04-17 16:05:31 -05001319 phba->work_status[0], phba->work_status[1]);
1320
James Smartd2873e42006-08-18 17:46:43 -04001321 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001322 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001323 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001324 sizeof(event_data), (char *) &event_data,
1325 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1326
James Smart09372822008-01-11 01:52:54 -05001327 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001328 }
James Smart93996272008-08-24 21:50:30 -04001329 return;
dea31012005-04-17 16:05:31 -05001330}
1331
James Smarte59058c2008-08-24 21:49:00 -04001332/**
James Smartda0436e2009-05-22 14:51:39 -04001333 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1334 * @phba: pointer to lpfc hba data structure.
1335 *
1336 * This routine is invoked to handle the SLI4 HBA hardware error attention
1337 * conditions.
1338 **/
1339static void
1340lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1341{
1342 struct lpfc_vport *vport = phba->pport;
1343 uint32_t event_data;
1344 struct Scsi_Host *shost;
1345
1346 /* If the pci channel is offline, ignore possible errors, since
1347 * we cannot communicate with the pci card anyway.
1348 */
1349 if (pci_channel_offline(phba->pcidev))
1350 return;
1351 /* If resets are disabled then leave the HBA alone and return */
1352 if (!phba->cfg_enable_hba_reset)
1353 return;
1354
1355 /* Send an internal error event to mgmt application */
1356 lpfc_board_errevt_to_mgmt(phba);
1357
1358 /* For now, the actual action for SLI4 device handling is not
1359 * specified yet, just treated it as adaptor hardware failure
1360 */
1361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1362 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1363 phba->work_status[0], phba->work_status[1]);
1364
1365 event_data = FC_REG_DUMP_EVENT;
1366 shost = lpfc_shost_from_vport(vport);
1367 fc_host_post_vendor_event(shost, fc_get_event_number(),
1368 sizeof(event_data), (char *) &event_data,
1369 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1370
1371 lpfc_sli4_offline_eratt(phba);
1372}
1373
1374/**
1375 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1376 * @phba: pointer to lpfc HBA data structure.
1377 *
1378 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1379 * routine from the API jump table function pointer from the lpfc_hba struct.
1380 *
1381 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001382 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001383 * Any other value - error.
1384 **/
1385void
1386lpfc_handle_eratt(struct lpfc_hba *phba)
1387{
1388 (*phba->lpfc_handle_eratt)(phba);
1389}
1390
1391/**
James Smart3621a712009-04-06 18:47:14 -04001392 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001393 * @phba: pointer to lpfc hba data structure.
1394 *
1395 * This routine is invoked from the worker thread to handle a HBA host
1396 * attention link event.
1397 **/
dea31012005-04-17 16:05:31 -05001398void
James Smart2e0fef82007-06-17 19:56:36 -05001399lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001400{
James Smart2e0fef82007-06-17 19:56:36 -05001401 struct lpfc_vport *vport = phba->pport;
1402 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001403 LPFC_MBOXQ_t *pmb;
1404 volatile uint32_t control;
1405 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001406 int rc = 0;
dea31012005-04-17 16:05:31 -05001407
1408 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001409 if (!pmb) {
1410 rc = 1;
dea31012005-04-17 16:05:31 -05001411 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001412 }
dea31012005-04-17 16:05:31 -05001413
1414 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001415 if (!mp) {
1416 rc = 2;
dea31012005-04-17 16:05:31 -05001417 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001418 }
dea31012005-04-17 16:05:31 -05001419
1420 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001421 if (!mp->virt) {
1422 rc = 3;
dea31012005-04-17 16:05:31 -05001423 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001424 }
dea31012005-04-17 16:05:31 -05001425
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001426 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001427 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001428
1429 psli->slistat.link_event++;
1430 lpfc_read_la(phba, pmb, mp);
1431 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001432 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001433 /* Block ELS IOCBs until we have processed this mbox command */
1434 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001435 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001436 if (rc == MBX_NOT_FINISHED) {
1437 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001438 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001439 }
dea31012005-04-17 16:05:31 -05001440
1441 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001442 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001443 writel(HA_LATT, phba->HAregaddr);
1444 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001445 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001446
1447 return;
1448
James Smart14691152006-12-02 13:34:28 -05001449lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001450 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001451 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001452lpfc_handle_latt_free_mp:
1453 kfree(mp);
1454lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001455 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001456lpfc_handle_latt_err_exit:
1457 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001458 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001459 psli->sli_flag |= LPFC_PROCESS_LA;
1460 control = readl(phba->HCregaddr);
1461 control |= HC_LAINT_ENA;
1462 writel(control, phba->HCregaddr);
1463 readl(phba->HCregaddr); /* flush */
1464
1465 /* Clear Link Attention in HA REG */
1466 writel(HA_LATT, phba->HAregaddr);
1467 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001468 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001469 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001470 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001471
James Smart09372822008-01-11 01:52:54 -05001472 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1473 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001474
1475 return;
1476}
1477
James Smarte59058c2008-08-24 21:49:00 -04001478/**
James Smart3621a712009-04-06 18:47:14 -04001479 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001480 * @phba: pointer to lpfc hba data structure.
1481 * @vpd: pointer to the vital product data.
1482 * @len: length of the vital product data in bytes.
1483 *
1484 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1485 * an array of characters. In this routine, the ModelName, ProgramType, and
1486 * ModelDesc, etc. fields of the phba data structure will be populated.
1487 *
1488 * Return codes
1489 * 0 - pointer to the VPD passed in is NULL
1490 * 1 - success
1491 **/
James Smart3772a992009-05-22 14:50:54 -04001492int
James Smart2e0fef82007-06-17 19:56:36 -05001493lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001494{
1495 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001496 int Length;
dea31012005-04-17 16:05:31 -05001497 int i, j;
1498 int finished = 0;
1499 int index = 0;
1500
1501 if (!vpd)
1502 return 0;
1503
1504 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001505 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001506 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001507 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1508 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001509 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001510 switch (vpd[index]) {
1511 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001512 case 0x91:
dea31012005-04-17 16:05:31 -05001513 index += 1;
1514 lenlo = vpd[index];
1515 index += 1;
1516 lenhi = vpd[index];
1517 index += 1;
1518 i = ((((unsigned short)lenhi) << 8) + lenlo);
1519 index += i;
1520 break;
1521 case 0x90:
1522 index += 1;
1523 lenlo = vpd[index];
1524 index += 1;
1525 lenhi = vpd[index];
1526 index += 1;
1527 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001528 if (Length > len - index)
1529 Length = len - index;
dea31012005-04-17 16:05:31 -05001530 while (Length > 0) {
1531 /* Look for Serial Number */
1532 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1533 index += 2;
1534 i = vpd[index];
1535 index += 1;
1536 j = 0;
1537 Length -= (3+i);
1538 while(i--) {
1539 phba->SerialNumber[j++] = vpd[index++];
1540 if (j == 31)
1541 break;
1542 }
1543 phba->SerialNumber[j] = 0;
1544 continue;
1545 }
1546 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1547 phba->vpd_flag |= VPD_MODEL_DESC;
1548 index += 2;
1549 i = vpd[index];
1550 index += 1;
1551 j = 0;
1552 Length -= (3+i);
1553 while(i--) {
1554 phba->ModelDesc[j++] = vpd[index++];
1555 if (j == 255)
1556 break;
1557 }
1558 phba->ModelDesc[j] = 0;
1559 continue;
1560 }
1561 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1562 phba->vpd_flag |= VPD_MODEL_NAME;
1563 index += 2;
1564 i = vpd[index];
1565 index += 1;
1566 j = 0;
1567 Length -= (3+i);
1568 while(i--) {
1569 phba->ModelName[j++] = vpd[index++];
1570 if (j == 79)
1571 break;
1572 }
1573 phba->ModelName[j] = 0;
1574 continue;
1575 }
1576 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1577 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1578 index += 2;
1579 i = vpd[index];
1580 index += 1;
1581 j = 0;
1582 Length -= (3+i);
1583 while(i--) {
1584 phba->ProgramType[j++] = vpd[index++];
1585 if (j == 255)
1586 break;
1587 }
1588 phba->ProgramType[j] = 0;
1589 continue;
1590 }
1591 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1592 phba->vpd_flag |= VPD_PORT;
1593 index += 2;
1594 i = vpd[index];
1595 index += 1;
1596 j = 0;
1597 Length -= (3+i);
1598 while(i--) {
1599 phba->Port[j++] = vpd[index++];
1600 if (j == 19)
1601 break;
1602 }
1603 phba->Port[j] = 0;
1604 continue;
1605 }
1606 else {
1607 index += 2;
1608 i = vpd[index];
1609 index += 1;
1610 index += i;
1611 Length -= (3 + i);
1612 }
1613 }
1614 finished = 0;
1615 break;
1616 case 0x78:
1617 finished = 1;
1618 break;
1619 default:
1620 index ++;
1621 break;
1622 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001623 }
dea31012005-04-17 16:05:31 -05001624
1625 return(1);
1626}
1627
James Smarte59058c2008-08-24 21:49:00 -04001628/**
James Smart3621a712009-04-06 18:47:14 -04001629 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001630 * @phba: pointer to lpfc hba data structure.
1631 * @mdp: pointer to the data structure to hold the derived model name.
1632 * @descp: pointer to the data structure to hold the derived description.
1633 *
1634 * This routine retrieves HBA's description based on its registered PCI device
1635 * ID. The @descp passed into this function points to an array of 256 chars. It
1636 * shall be returned with the model name, maximum speed, and the host bus type.
1637 * The @mdp passed into this function points to an array of 80 chars. When the
1638 * function returns, the @mdp will be filled with the model name.
1639 **/
dea31012005-04-17 16:05:31 -05001640static void
James Smart2e0fef82007-06-17 19:56:36 -05001641lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001642{
1643 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001644 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001645 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001646 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001647 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001648 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001649 char *name;
1650 char *bus;
1651 char *function;
1652 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001653
1654 if (mdp && mdp[0] != '\0'
1655 && descp && descp[0] != '\0')
1656 return;
1657
1658 if (phba->lmt & LMT_10Gb)
1659 max_speed = 10;
1660 else if (phba->lmt & LMT_8Gb)
1661 max_speed = 8;
1662 else if (phba->lmt & LMT_4Gb)
1663 max_speed = 4;
1664 else if (phba->lmt & LMT_2Gb)
1665 max_speed = 2;
1666 else
1667 max_speed = 1;
dea31012005-04-17 16:05:31 -05001668
1669 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001670
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001671 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001672 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001673 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001674 break;
dea31012005-04-17 16:05:31 -05001675 case PCI_DEVICE_ID_SUPERFLY:
1676 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001677 m = (typeof(m)){"LP7000", "PCI",
1678 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001679 else
James Smarta747c9c2009-11-18 15:41:10 -05001680 m = (typeof(m)){"LP7000E", "PCI",
1681 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001682 break;
1683 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001684 m = (typeof(m)){"LP8000", "PCI",
1685 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001686 break;
1687 case PCI_DEVICE_ID_CENTAUR:
1688 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001689 m = (typeof(m)){"LP9002", "PCI",
1690 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001691 else
James Smarta747c9c2009-11-18 15:41:10 -05001692 m = (typeof(m)){"LP9000", "PCI",
1693 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001694 break;
1695 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001696 m = (typeof(m)){"LP952", "PCI",
1697 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001698 break;
1699 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001700 m = (typeof(m)){"LP9802", "PCI-X",
1701 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001702 break;
1703 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001704 m = (typeof(m)){"LP10000", "PCI-X",
1705 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001706 break;
1707 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001708 m = (typeof(m)){"LPX1000", "PCI-X",
1709 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001710 break;
1711 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001712 m = (typeof(m)){"LP982", "PCI-X",
1713 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001714 break;
1715 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001716 m = (typeof(m)){"LP1050", "PCI-X",
1717 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001718 break;
1719 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001720 m = (typeof(m)){"LP11000", "PCI-X2",
1721 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001722 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001723 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001724 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1725 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001726 break;
1727 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001728 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1729 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001730 break;
1731 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001732 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001733 break;
1734 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001735 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001736 break;
1737 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001738 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001739 break;
dea31012005-04-17 16:05:31 -05001740 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001741 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001742 break;
1743 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001744 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001745 break;
1746 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001747 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001748 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001749 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001750 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001751 break;
1752 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001753 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001754 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001755 break;
dea31012005-04-17 16:05:31 -05001756 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001757 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001758 break;
1759 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001760 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001761 break;
1762 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001763 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001764 break;
1765 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001766 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001767 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001768 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001769 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001770 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001771 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001772 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001773 break;
James Smartb87eab32007-04-25 09:53:28 -04001774 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001775 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001776 break;
1777 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001778 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001779 break;
1780 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001781 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001782 break;
1783 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001784 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001785 break;
1786 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001787 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001788 break;
1789 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001790 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001791 break;
James Smart84774a42008-08-24 21:50:06 -04001792 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001793 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001794 GE = 1;
1795 break;
1796 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001797 m = (typeof(m)){"LPev12000", "PCIe IOV",
1798 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001799 break;
1800 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001801 m = (typeof(m)){"LPev12000", "PCIe IOV",
1802 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001803 break;
1804 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001805 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1806 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001807 break;
James Smartda0436e2009-05-22 14:51:39 -04001808 case PCI_DEVICE_ID_TIGERSHARK:
1809 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001810 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04001811 break;
James Smarta747c9c2009-11-18 15:41:10 -05001812 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04001813 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001814 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1815 break;
1816 case PCI_DEVICE_ID_FALCON:
1817 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1818 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04001819 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001820 default:
James Smarta747c9c2009-11-18 15:41:10 -05001821 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001822 break;
dea31012005-04-17 16:05:31 -05001823 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001824
1825 if (mdp && mdp[0] == '\0')
1826 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001827 /* oneConnect hba requires special processing, they are all initiators
1828 * and we put the port number on the end
1829 */
1830 if (descp && descp[0] == '\0') {
1831 if (oneConnect)
1832 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05001833 "Emulex OneConnect %s, %s Initiator, Port %s",
1834 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04001835 phba->Port);
1836 else
1837 snprintf(descp, 255,
1838 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05001839 m.name, max_speed, (GE) ? "GE" : "Gb",
1840 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04001841 }
dea31012005-04-17 16:05:31 -05001842}
1843
James Smarte59058c2008-08-24 21:49:00 -04001844/**
James Smart3621a712009-04-06 18:47:14 -04001845 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001846 * @phba: pointer to lpfc hba data structure.
1847 * @pring: pointer to a IOCB ring.
1848 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1849 *
1850 * This routine posts a given number of IOCBs with the associated DMA buffer
1851 * descriptors specified by the cnt argument to the given IOCB ring.
1852 *
1853 * Return codes
1854 * The number of IOCBs NOT able to be posted to the IOCB ring.
1855 **/
dea31012005-04-17 16:05:31 -05001856int
James Smart495a7142008-06-14 22:52:59 -04001857lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001858{
1859 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001860 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001861 struct lpfc_dmabuf *mp1, *mp2;
1862
1863 cnt += pring->missbufcnt;
1864
1865 /* While there are buffers to post */
1866 while (cnt > 0) {
1867 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001868 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001869 if (iocb == NULL) {
1870 pring->missbufcnt = cnt;
1871 return cnt;
1872 }
dea31012005-04-17 16:05:31 -05001873 icmd = &iocb->iocb;
1874
1875 /* 2 buffers can be posted per command */
1876 /* Allocate buffer to post */
1877 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1878 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001879 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1880 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001881 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001882 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001883 pring->missbufcnt = cnt;
1884 return cnt;
1885 }
1886
1887 INIT_LIST_HEAD(&mp1->list);
1888 /* Allocate buffer to post */
1889 if (cnt > 1) {
1890 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1891 if (mp2)
1892 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1893 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001894 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001895 kfree(mp2);
dea31012005-04-17 16:05:31 -05001896 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1897 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001898 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001899 pring->missbufcnt = cnt;
1900 return cnt;
1901 }
1902
1903 INIT_LIST_HEAD(&mp2->list);
1904 } else {
1905 mp2 = NULL;
1906 }
1907
1908 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1909 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1910 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1911 icmd->ulpBdeCount = 1;
1912 cnt--;
1913 if (mp2) {
1914 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1915 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1916 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1917 cnt--;
1918 icmd->ulpBdeCount = 2;
1919 }
1920
1921 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1922 icmd->ulpLe = 1;
1923
James Smart3772a992009-05-22 14:50:54 -04001924 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1925 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001926 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1927 kfree(mp1);
1928 cnt++;
1929 if (mp2) {
1930 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1931 kfree(mp2);
1932 cnt++;
1933 }
James Bottomley604a3e32005-10-29 10:28:33 -05001934 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001935 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001936 return cnt;
1937 }
dea31012005-04-17 16:05:31 -05001938 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001939 if (mp2)
dea31012005-04-17 16:05:31 -05001940 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001941 }
1942 pring->missbufcnt = 0;
1943 return 0;
1944}
1945
James Smarte59058c2008-08-24 21:49:00 -04001946/**
James Smart3621a712009-04-06 18:47:14 -04001947 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001948 * @phba: pointer to lpfc hba data structure.
1949 *
1950 * This routine posts initial receive IOCB buffers to the ELS ring. The
1951 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1952 * set to 64 IOCBs.
1953 *
1954 * Return codes
1955 * 0 - success (currently always success)
1956 **/
dea31012005-04-17 16:05:31 -05001957static int
James Smart2e0fef82007-06-17 19:56:36 -05001958lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001959{
1960 struct lpfc_sli *psli = &phba->sli;
1961
1962 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001963 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001964 /* Ring 2 - FCP no buffers needed */
1965
1966 return 0;
1967}
1968
1969#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1970
James Smarte59058c2008-08-24 21:49:00 -04001971/**
James Smart3621a712009-04-06 18:47:14 -04001972 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001973 * @HashResultPointer: pointer to an array as hash table.
1974 *
1975 * This routine sets up the initial values to the array of hash table entries
1976 * for the LC HBAs.
1977 **/
dea31012005-04-17 16:05:31 -05001978static void
1979lpfc_sha_init(uint32_t * HashResultPointer)
1980{
1981 HashResultPointer[0] = 0x67452301;
1982 HashResultPointer[1] = 0xEFCDAB89;
1983 HashResultPointer[2] = 0x98BADCFE;
1984 HashResultPointer[3] = 0x10325476;
1985 HashResultPointer[4] = 0xC3D2E1F0;
1986}
1987
James Smarte59058c2008-08-24 21:49:00 -04001988/**
James Smart3621a712009-04-06 18:47:14 -04001989 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001990 * @HashResultPointer: pointer to an initial/result hash table.
1991 * @HashWorkingPointer: pointer to an working hash table.
1992 *
1993 * This routine iterates an initial hash table pointed by @HashResultPointer
1994 * with the values from the working hash table pointeed by @HashWorkingPointer.
1995 * The results are putting back to the initial hash table, returned through
1996 * the @HashResultPointer as the result hash table.
1997 **/
dea31012005-04-17 16:05:31 -05001998static void
1999lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2000{
2001 int t;
2002 uint32_t TEMP;
2003 uint32_t A, B, C, D, E;
2004 t = 16;
2005 do {
2006 HashWorkingPointer[t] =
2007 S(1,
2008 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2009 8] ^
2010 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2011 } while (++t <= 79);
2012 t = 0;
2013 A = HashResultPointer[0];
2014 B = HashResultPointer[1];
2015 C = HashResultPointer[2];
2016 D = HashResultPointer[3];
2017 E = HashResultPointer[4];
2018
2019 do {
2020 if (t < 20) {
2021 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2022 } else if (t < 40) {
2023 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2024 } else if (t < 60) {
2025 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2026 } else {
2027 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2028 }
2029 TEMP += S(5, A) + E + HashWorkingPointer[t];
2030 E = D;
2031 D = C;
2032 C = S(30, B);
2033 B = A;
2034 A = TEMP;
2035 } while (++t <= 79);
2036
2037 HashResultPointer[0] += A;
2038 HashResultPointer[1] += B;
2039 HashResultPointer[2] += C;
2040 HashResultPointer[3] += D;
2041 HashResultPointer[4] += E;
2042
2043}
2044
James Smarte59058c2008-08-24 21:49:00 -04002045/**
James Smart3621a712009-04-06 18:47:14 -04002046 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002047 * @RandomChallenge: pointer to the entry of host challenge random number array.
2048 * @HashWorking: pointer to the entry of the working hash array.
2049 *
2050 * This routine calculates the working hash array referred by @HashWorking
2051 * from the challenge random numbers associated with the host, referred by
2052 * @RandomChallenge. The result is put into the entry of the working hash
2053 * array and returned by reference through @HashWorking.
2054 **/
dea31012005-04-17 16:05:31 -05002055static void
2056lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2057{
2058 *HashWorking = (*RandomChallenge ^ *HashWorking);
2059}
2060
James Smarte59058c2008-08-24 21:49:00 -04002061/**
James Smart3621a712009-04-06 18:47:14 -04002062 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002063 * @phba: pointer to lpfc hba data structure.
2064 * @hbainit: pointer to an array of unsigned 32-bit integers.
2065 *
2066 * This routine performs the special handling for LC HBA initialization.
2067 **/
dea31012005-04-17 16:05:31 -05002068void
2069lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2070{
2071 int t;
2072 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002073 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002074
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002075 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002076 if (!HashWorking)
2077 return;
2078
dea31012005-04-17 16:05:31 -05002079 HashWorking[0] = HashWorking[78] = *pwwnn++;
2080 HashWorking[1] = HashWorking[79] = *pwwnn;
2081
2082 for (t = 0; t < 7; t++)
2083 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2084
2085 lpfc_sha_init(hbainit);
2086 lpfc_sha_iterate(hbainit, HashWorking);
2087 kfree(HashWorking);
2088}
2089
James Smarte59058c2008-08-24 21:49:00 -04002090/**
James Smart3621a712009-04-06 18:47:14 -04002091 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002092 * @vport: pointer to a virtual N_Port data structure.
2093 *
2094 * This routine performs the necessary cleanups before deleting the @vport.
2095 * It invokes the discovery state machine to perform necessary state
2096 * transitions and to release the ndlps associated with the @vport. Note,
2097 * the physical port is treated as @vport 0.
2098 **/
James Smart87af33f2007-10-27 13:37:43 -04002099void
James Smart2e0fef82007-06-17 19:56:36 -05002100lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002101{
James Smart87af33f2007-10-27 13:37:43 -04002102 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002103 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002104 int i = 0;
dea31012005-04-17 16:05:31 -05002105
James Smart87af33f2007-10-27 13:37:43 -04002106 if (phba->link_state > LPFC_LINK_DOWN)
2107 lpfc_port_link_failure(vport);
2108
2109 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002110 if (!NLP_CHK_NODE_ACT(ndlp)) {
2111 ndlp = lpfc_enable_node(vport, ndlp,
2112 NLP_STE_UNUSED_NODE);
2113 if (!ndlp)
2114 continue;
2115 spin_lock_irq(&phba->ndlp_lock);
2116 NLP_SET_FREE_REQ(ndlp);
2117 spin_unlock_irq(&phba->ndlp_lock);
2118 /* Trigger the release of the ndlp memory */
2119 lpfc_nlp_put(ndlp);
2120 continue;
2121 }
2122 spin_lock_irq(&phba->ndlp_lock);
2123 if (NLP_CHK_FREE_REQ(ndlp)) {
2124 /* The ndlp should not be in memory free mode already */
2125 spin_unlock_irq(&phba->ndlp_lock);
2126 continue;
2127 } else
2128 /* Indicate request for freeing ndlp memory */
2129 NLP_SET_FREE_REQ(ndlp);
2130 spin_unlock_irq(&phba->ndlp_lock);
2131
James Smart58da1ff2008-04-07 10:15:56 -04002132 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2133 ndlp->nlp_DID == Fabric_DID) {
2134 /* Just free up ndlp with Fabric_DID for vports */
2135 lpfc_nlp_put(ndlp);
2136 continue;
2137 }
2138
James Smart87af33f2007-10-27 13:37:43 -04002139 if (ndlp->nlp_type & NLP_FABRIC)
2140 lpfc_disc_state_machine(vport, ndlp, NULL,
2141 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002142
James Smart87af33f2007-10-27 13:37:43 -04002143 lpfc_disc_state_machine(vport, ndlp, NULL,
2144 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002145
James Smart87af33f2007-10-27 13:37:43 -04002146 }
2147
James Smarta8adb832007-10-27 13:37:53 -04002148 /* At this point, ALL ndlp's should be gone
2149 * because of the previous NLP_EVT_DEVICE_RM.
2150 * Lets wait for this to happen, if needed.
2151 */
James Smart87af33f2007-10-27 13:37:43 -04002152 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002153 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002154 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002155 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002156 list_for_each_entry_safe(ndlp, next_ndlp,
2157 &vport->fc_nodes, nlp_listp) {
2158 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2159 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002160 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002161 "usgmap:x%x refcnt:%d\n",
2162 ndlp->nlp_DID, (void *)ndlp,
2163 ndlp->nlp_usg_map,
2164 atomic_read(
2165 &ndlp->kref.refcount));
2166 }
James Smarta8adb832007-10-27 13:37:53 -04002167 break;
James Smart87af33f2007-10-27 13:37:43 -04002168 }
James Smarta8adb832007-10-27 13:37:53 -04002169
2170 /* Wait for any activity on ndlps to settle */
2171 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002172 }
dea31012005-04-17 16:05:31 -05002173}
2174
James Smarte59058c2008-08-24 21:49:00 -04002175/**
James Smart3621a712009-04-06 18:47:14 -04002176 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002177 * @vport: pointer to a virtual N_Port data structure.
2178 *
2179 * This routine stops all the timers associated with a @vport. This function
2180 * is invoked before disabling or deleting a @vport. Note that the physical
2181 * port is treated as @vport 0.
2182 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002183void
2184lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002185{
James Smart92d7f7b2007-06-17 19:56:38 -05002186 del_timer_sync(&vport->els_tmofunc);
2187 del_timer_sync(&vport->fc_fdmitmo);
2188 lpfc_can_disctmo(vport);
2189 return;
dea31012005-04-17 16:05:31 -05002190}
2191
James Smarte59058c2008-08-24 21:49:00 -04002192/**
James Smartecfd03c2010-02-12 14:41:27 -05002193 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2194 * @phba: pointer to lpfc hba data structure.
2195 *
2196 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2197 * caller of this routine should already hold the host lock.
2198 **/
2199void
2200__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2201{
2202 /* Clear pending FCF rediscovery wait timer */
2203 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2204 /* Now, try to stop the timer */
2205 del_timer(&phba->fcf.redisc_wait);
2206}
2207
2208/**
2209 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2210 * @phba: pointer to lpfc hba data structure.
2211 *
2212 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2213 * checks whether the FCF rediscovery wait timer is pending with the host
2214 * lock held before proceeding with disabling the timer and clearing the
2215 * wait timer pendig flag.
2216 **/
2217void
2218lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2219{
2220 spin_lock_irq(&phba->hbalock);
2221 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2222 /* FCF rediscovery timer already fired or stopped */
2223 spin_unlock_irq(&phba->hbalock);
2224 return;
2225 }
2226 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2227 spin_unlock_irq(&phba->hbalock);
2228}
2229
2230/**
James Smart3772a992009-05-22 14:50:54 -04002231 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002232 * @phba: pointer to lpfc hba data structure.
2233 *
2234 * This routine stops all the timers associated with a HBA. This function is
2235 * invoked before either putting a HBA offline or unloading the driver.
2236 **/
James Smart3772a992009-05-22 14:50:54 -04002237void
2238lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002239{
James Smart51ef4c22007-08-02 11:10:31 -04002240 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002241 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002242 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002243 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002244 del_timer_sync(&phba->hb_tmofunc);
2245 phba->hb_outstanding = 0;
2246
2247 switch (phba->pci_dev_grp) {
2248 case LPFC_PCI_DEV_LP:
2249 /* Stop any LightPulse device specific driver timers */
2250 del_timer_sync(&phba->fcp_poll_timer);
2251 break;
2252 case LPFC_PCI_DEV_OC:
2253 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002254 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002255 break;
2256 default:
2257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2258 "0297 Invalid device group (x%x)\n",
2259 phba->pci_dev_grp);
2260 break;
2261 }
James Smart2e0fef82007-06-17 19:56:36 -05002262 return;
dea31012005-04-17 16:05:31 -05002263}
2264
James Smarte59058c2008-08-24 21:49:00 -04002265/**
James Smart3621a712009-04-06 18:47:14 -04002266 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002267 * @phba: pointer to lpfc hba data structure.
2268 *
2269 * This routine marks a HBA's management interface as blocked. Once the HBA's
2270 * management interface is marked as blocked, all the user space access to
2271 * the HBA, whether they are from sysfs interface or libdfc interface will
2272 * all be blocked. The HBA is set to block the management interface when the
2273 * driver prepares the HBA interface for online or offline.
2274 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002275static void
2276lpfc_block_mgmt_io(struct lpfc_hba * phba)
2277{
2278 unsigned long iflag;
2279
2280 spin_lock_irqsave(&phba->hbalock, iflag);
2281 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2282 spin_unlock_irqrestore(&phba->hbalock, iflag);
2283}
2284
James Smarte59058c2008-08-24 21:49:00 -04002285/**
James Smart3621a712009-04-06 18:47:14 -04002286 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002287 * @phba: pointer to lpfc hba data structure.
2288 *
2289 * This routine initializes the HBA and brings a HBA online. During this
2290 * process, the management interface is blocked to prevent user space access
2291 * to the HBA interfering with the driver initialization.
2292 *
2293 * Return codes
2294 * 0 - successful
2295 * 1 - failed
2296 **/
dea31012005-04-17 16:05:31 -05002297int
James Smart2e0fef82007-06-17 19:56:36 -05002298lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002299{
Julia Lawall372bd282008-12-16 16:15:08 +01002300 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002301 struct lpfc_vport **vports;
2302 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002303
dea31012005-04-17 16:05:31 -05002304 if (!phba)
2305 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002306 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002307
James Smart2e0fef82007-06-17 19:56:36 -05002308 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002309 return 0;
2310
James Smarted957682007-06-17 19:56:37 -05002311 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002312 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002313
James Smart46fa3112007-04-25 09:51:45 -04002314 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002315
James Smart46fa3112007-04-25 09:51:45 -04002316 if (!lpfc_sli_queue_setup(phba)) {
2317 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002318 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002319 }
2320
James Smartda0436e2009-05-22 14:51:39 -04002321 if (phba->sli_rev == LPFC_SLI_REV4) {
2322 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2323 lpfc_unblock_mgmt_io(phba);
2324 return 1;
2325 }
2326 } else {
2327 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2328 lpfc_unblock_mgmt_io(phba);
2329 return 1;
2330 }
James Smart46fa3112007-04-25 09:51:45 -04002331 }
dea31012005-04-17 16:05:31 -05002332
James Smart549e55c2007-08-02 11:09:51 -04002333 vports = lpfc_create_vport_work_array(phba);
2334 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002335 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002336 struct Scsi_Host *shost;
2337 shost = lpfc_shost_from_vport(vports[i]);
2338 spin_lock_irq(shost->host_lock);
2339 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2340 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2341 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002342 if (phba->sli_rev == LPFC_SLI_REV4)
2343 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002344 spin_unlock_irq(shost->host_lock);
2345 }
James Smart09372822008-01-11 01:52:54 -05002346 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002347
James Smart46fa3112007-04-25 09:51:45 -04002348 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002349 return 0;
2350}
2351
James Smarte59058c2008-08-24 21:49:00 -04002352/**
James Smart3621a712009-04-06 18:47:14 -04002353 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002354 * @phba: pointer to lpfc hba data structure.
2355 *
2356 * This routine marks a HBA's management interface as not blocked. Once the
2357 * HBA's management interface is marked as not blocked, all the user space
2358 * access to the HBA, whether they are from sysfs interface or libdfc
2359 * interface will be allowed. The HBA is set to block the management interface
2360 * when the driver prepares the HBA interface for online or offline and then
2361 * set to unblock the management interface afterwards.
2362 **/
James Smart46fa3112007-04-25 09:51:45 -04002363void
James Smart46fa3112007-04-25 09:51:45 -04002364lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2365{
2366 unsigned long iflag;
2367
James Smart2e0fef82007-06-17 19:56:36 -05002368 spin_lock_irqsave(&phba->hbalock, iflag);
2369 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2370 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002371}
2372
James Smarte59058c2008-08-24 21:49:00 -04002373/**
James Smart3621a712009-04-06 18:47:14 -04002374 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002375 * @phba: pointer to lpfc hba data structure.
2376 *
2377 * This routine is invoked to prepare a HBA to be brought offline. It performs
2378 * unregistration login to all the nodes on all vports and flushes the mailbox
2379 * queue to make it ready to be brought offline.
2380 **/
James Smart46fa3112007-04-25 09:51:45 -04002381void
2382lpfc_offline_prep(struct lpfc_hba * phba)
2383{
James Smart2e0fef82007-06-17 19:56:36 -05002384 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002385 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002386 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002387 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002388 int i;
dea31012005-04-17 16:05:31 -05002389
James Smart2e0fef82007-06-17 19:56:36 -05002390 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002391 return;
dea31012005-04-17 16:05:31 -05002392
James Smart46fa3112007-04-25 09:51:45 -04002393 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002394
2395 lpfc_linkdown(phba);
2396
James Smart87af33f2007-10-27 13:37:43 -04002397 /* Issue an unreg_login to all nodes on all vports */
2398 vports = lpfc_create_vport_work_array(phba);
2399 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002400 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002401 if (vports[i]->load_flag & FC_UNLOADING)
2402 continue;
James Smart72100cc2010-02-12 14:43:01 -05002403 shost = lpfc_shost_from_vport(vports[i]);
2404 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002405 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002406 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2407 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002408 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002409
James Smart87af33f2007-10-27 13:37:43 -04002410 shost = lpfc_shost_from_vport(vports[i]);
2411 list_for_each_entry_safe(ndlp, next_ndlp,
2412 &vports[i]->fc_nodes,
2413 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002414 if (!NLP_CHK_NODE_ACT(ndlp))
2415 continue;
James Smart87af33f2007-10-27 13:37:43 -04002416 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2417 continue;
2418 if (ndlp->nlp_type & NLP_FABRIC) {
2419 lpfc_disc_state_machine(vports[i], ndlp,
2420 NULL, NLP_EVT_DEVICE_RECOVERY);
2421 lpfc_disc_state_machine(vports[i], ndlp,
2422 NULL, NLP_EVT_DEVICE_RM);
2423 }
2424 spin_lock_irq(shost->host_lock);
2425 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2426 spin_unlock_irq(shost->host_lock);
2427 lpfc_unreg_rpi(vports[i], ndlp);
2428 }
2429 }
2430 }
James Smart09372822008-01-11 01:52:54 -05002431 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002432
James Smart04c68492009-05-22 14:52:52 -04002433 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002434}
2435
James Smarte59058c2008-08-24 21:49:00 -04002436/**
James Smart3621a712009-04-06 18:47:14 -04002437 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002438 * @phba: pointer to lpfc hba data structure.
2439 *
2440 * This routine actually brings a HBA offline. It stops all the timers
2441 * associated with the HBA, brings down the SLI layer, and eventually
2442 * marks the HBA as in offline state for the upper layer protocol.
2443 **/
James Smart46fa3112007-04-25 09:51:45 -04002444void
James Smart2e0fef82007-06-17 19:56:36 -05002445lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002446{
James Smart549e55c2007-08-02 11:09:51 -04002447 struct Scsi_Host *shost;
2448 struct lpfc_vport **vports;
2449 int i;
James Smart46fa3112007-04-25 09:51:45 -04002450
James Smart549e55c2007-08-02 11:09:51 -04002451 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002452 return;
James Smart688a8862006-07-06 15:49:56 -04002453
James Smartda0436e2009-05-22 14:51:39 -04002454 /* stop port and all timers associated with this hba */
2455 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002456 vports = lpfc_create_vport_work_array(phba);
2457 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002458 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002459 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002460 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002461 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002462 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002463 /* Bring down the SLI Layer and cleanup. The HBA is offline
2464 now. */
2465 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002466 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002467 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002468 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002469 vports = lpfc_create_vport_work_array(phba);
2470 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002471 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002472 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002473 spin_lock_irq(shost->host_lock);
2474 vports[i]->work_port_events = 0;
2475 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2476 spin_unlock_irq(shost->host_lock);
2477 }
James Smart09372822008-01-11 01:52:54 -05002478 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002479}
2480
James Smarte59058c2008-08-24 21:49:00 -04002481/**
James Smart3621a712009-04-06 18:47:14 -04002482 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002483 * @phba: pointer to lpfc hba data structure.
2484 *
2485 * This routine is to free all the SCSI buffers and IOCBs from the driver
2486 * list back to kernel. It is called from lpfc_pci_remove_one to free
2487 * the internal resources before the device is removed from the system.
2488 *
2489 * Return codes
2490 * 0 - successful (for now, it always returns 0)
2491 **/
dea31012005-04-17 16:05:31 -05002492static int
James Smart2e0fef82007-06-17 19:56:36 -05002493lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002494{
2495 struct lpfc_scsi_buf *sb, *sb_next;
2496 struct lpfc_iocbq *io, *io_next;
2497
James Smart2e0fef82007-06-17 19:56:36 -05002498 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002499 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002500 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002501 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2502 list_del(&sb->list);
2503 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002504 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002505 kfree(sb);
2506 phba->total_scsi_bufs--;
2507 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002508 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002509
2510 /* Release all the lpfc_iocbq entries maintained by this host. */
2511 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2512 list_del(&io->list);
2513 kfree(io);
2514 phba->total_iocbq_bufs--;
2515 }
James Smart2e0fef82007-06-17 19:56:36 -05002516 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002517 return 0;
2518}
2519
James Smarte59058c2008-08-24 21:49:00 -04002520/**
James Smart3621a712009-04-06 18:47:14 -04002521 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002522 * @phba: pointer to lpfc hba data structure.
2523 * @instance: a unique integer ID to this FC port.
2524 * @dev: pointer to the device data structure.
2525 *
2526 * This routine creates a FC port for the upper layer protocol. The FC port
2527 * can be created on top of either a physical port or a virtual port provided
2528 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2529 * and associates the FC port created before adding the shost into the SCSI
2530 * layer.
2531 *
2532 * Return codes
2533 * @vport - pointer to the virtual N_Port data structure.
2534 * NULL - port create failed.
2535 **/
James Smart2e0fef82007-06-17 19:56:36 -05002536struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002537lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002538{
James Smart2e0fef82007-06-17 19:56:36 -05002539 struct lpfc_vport *vport;
2540 struct Scsi_Host *shost;
2541 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002542
James Smart3de2a652007-08-02 11:09:59 -04002543 if (dev != &phba->pcidev->dev)
2544 shost = scsi_host_alloc(&lpfc_vport_template,
2545 sizeof(struct lpfc_vport));
2546 else
2547 shost = scsi_host_alloc(&lpfc_template,
2548 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002549 if (!shost)
2550 goto out;
James Smart47a86172007-04-25 09:53:22 -04002551
James Smart2e0fef82007-06-17 19:56:36 -05002552 vport = (struct lpfc_vport *) shost->hostdata;
2553 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002554 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002555 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002556 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002557
James Smart3de2a652007-08-02 11:09:59 -04002558 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002559 shost->unique_id = instance;
2560 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002561 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002562 shost->this_id = -1;
2563 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002564 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002565 shost->dma_boundary =
2566 phba->sli4_hba.pc_sli4_params.sge_supp_len;
James Smartda0436e2009-05-22 14:51:39 -04002567 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2568 }
James Smart81301a92008-12-04 22:39:46 -05002569
James Smart47a86172007-04-25 09:53:22 -04002570 /*
James Smart2e0fef82007-06-17 19:56:36 -05002571 * Set initial can_queue value since 0 is no longer supported and
2572 * scsi_add_host will fail. This will be adjusted later based on the
2573 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002574 */
James Smart2e0fef82007-06-17 19:56:36 -05002575 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002576 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002577 shost->transportt = lpfc_vport_transport_template;
2578 vport->port_type = LPFC_NPIV_PORT;
2579 } else {
2580 shost->transportt = lpfc_transport_template;
2581 vport->port_type = LPFC_PHYSICAL_PORT;
2582 }
James Smart47a86172007-04-25 09:53:22 -04002583
James Smart2e0fef82007-06-17 19:56:36 -05002584 /* Initialize all internally managed lists. */
2585 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002586 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002587 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002588
James Smart2e0fef82007-06-17 19:56:36 -05002589 init_timer(&vport->fc_disctmo);
2590 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002591 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002592
James Smart2e0fef82007-06-17 19:56:36 -05002593 init_timer(&vport->fc_fdmitmo);
2594 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002595 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002596
James Smart2e0fef82007-06-17 19:56:36 -05002597 init_timer(&vport->els_tmofunc);
2598 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002599 vport->els_tmofunc.data = (unsigned long)vport;
James Smarte2aed292010-02-26 14:15:00 -05002600 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
2601 phba->menlo_flag |= HBA_MENLO_SUPPORT;
2602 /* check for menlo minimum sg count */
2603 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT) {
2604 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
2605 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2606 }
2607 }
James Smart47a86172007-04-25 09:53:22 -04002608
James Bottomleyd139b9b2009-11-05 13:33:12 -06002609 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002610 if (error)
2611 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002612
James Smart549e55c2007-08-02 11:09:51 -04002613 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002614 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002615 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002616 return vport;
James Smart47a86172007-04-25 09:53:22 -04002617
James Smart2e0fef82007-06-17 19:56:36 -05002618out_put_shost:
2619 scsi_host_put(shost);
2620out:
2621 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002622}
2623
James Smarte59058c2008-08-24 21:49:00 -04002624/**
James Smart3621a712009-04-06 18:47:14 -04002625 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002626 * @vport: pointer to an lpfc virtual N_Port data structure.
2627 *
2628 * This routine destroys a FC port from the upper layer protocol. All the
2629 * resources associated with the port are released.
2630 **/
James Smart2e0fef82007-06-17 19:56:36 -05002631void
2632destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002633{
James Smart92d7f7b2007-06-17 19:56:38 -05002634 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2635 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002636
James Smart858c9f62007-06-17 19:56:39 -05002637 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002638 fc_remove_host(shost);
2639 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002640
James Smart92d7f7b2007-06-17 19:56:38 -05002641 spin_lock_irq(&phba->hbalock);
2642 list_del_init(&vport->listentry);
2643 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002644
James Smart92d7f7b2007-06-17 19:56:38 -05002645 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002646 return;
James Smart47a86172007-04-25 09:53:22 -04002647}
2648
James Smarte59058c2008-08-24 21:49:00 -04002649/**
James Smart3621a712009-04-06 18:47:14 -04002650 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002651 *
2652 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2653 * uses the kernel idr facility to perform the task.
2654 *
2655 * Return codes:
2656 * instance - a unique integer ID allocated as the new instance.
2657 * -1 - lpfc get instance failed.
2658 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002659int
2660lpfc_get_instance(void)
2661{
2662 int instance = 0;
2663
2664 /* Assign an unused number */
2665 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2666 return -1;
2667 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2668 return -1;
2669 return instance;
2670}
2671
James Smarte59058c2008-08-24 21:49:00 -04002672/**
James Smart3621a712009-04-06 18:47:14 -04002673 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002674 * @shost: pointer to SCSI host data structure.
2675 * @time: elapsed time of the scan in jiffies.
2676 *
2677 * This routine is called by the SCSI layer with a SCSI host to determine
2678 * whether the scan host is finished.
2679 *
2680 * Note: there is no scan_start function as adapter initialization will have
2681 * asynchronously kicked off the link initialization.
2682 *
2683 * Return codes
2684 * 0 - SCSI host scan is not over yet.
2685 * 1 - SCSI host scan is over.
2686 **/
James Smart47a86172007-04-25 09:53:22 -04002687int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2688{
James Smart2e0fef82007-06-17 19:56:36 -05002689 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2690 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002691 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002692
James Smart858c9f62007-06-17 19:56:39 -05002693 spin_lock_irq(shost->host_lock);
2694
James Smart51ef4c22007-08-02 11:10:31 -04002695 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002696 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002697 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002698 }
James Smart2e0fef82007-06-17 19:56:36 -05002699 if (time >= 30 * HZ) {
2700 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002701 "0461 Scanning longer than 30 "
2702 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002703 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002704 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002705 }
2706 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2707 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002708 "0465 Link down longer than 15 "
2709 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002710 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002711 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002712 }
2713
James Smart2e0fef82007-06-17 19:56:36 -05002714 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002715 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002716 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002717 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002718 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002719 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002720 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002721 goto finished;
2722
2723 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002724
2725finished:
James Smart858c9f62007-06-17 19:56:39 -05002726 spin_unlock_irq(shost->host_lock);
2727 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002728}
2729
James Smarte59058c2008-08-24 21:49:00 -04002730/**
James Smart3621a712009-04-06 18:47:14 -04002731 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002732 * @shost: pointer to SCSI host data structure.
2733 *
2734 * This routine initializes a given SCSI host attributes on a FC port. The
2735 * SCSI host can be either on top of a physical port or a virtual port.
2736 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002737void lpfc_host_attrib_init(struct Scsi_Host *shost)
2738{
2739 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2740 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002741 /*
James Smart2e0fef82007-06-17 19:56:36 -05002742 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002743 */
2744
James Smart2e0fef82007-06-17 19:56:36 -05002745 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2746 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002747 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2748
2749 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002750 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002751 fc_host_supported_fc4s(shost)[2] = 1;
2752 fc_host_supported_fc4s(shost)[7] = 1;
2753
James Smart92d7f7b2007-06-17 19:56:38 -05002754 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2755 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002756
2757 fc_host_supported_speeds(shost) = 0;
2758 if (phba->lmt & LMT_10Gb)
2759 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002760 if (phba->lmt & LMT_8Gb)
2761 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002762 if (phba->lmt & LMT_4Gb)
2763 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2764 if (phba->lmt & LMT_2Gb)
2765 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2766 if (phba->lmt & LMT_1Gb)
2767 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2768
2769 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002770 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2771 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002772
2773 /* This value is also unchanging */
2774 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002775 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002776 fc_host_active_fc4s(shost)[2] = 1;
2777 fc_host_active_fc4s(shost)[7] = 1;
2778
James Smart92d7f7b2007-06-17 19:56:38 -05002779 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002780 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002781 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002782 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002783}
dea31012005-04-17 16:05:31 -05002784
James Smarte59058c2008-08-24 21:49:00 -04002785/**
James Smartda0436e2009-05-22 14:51:39 -04002786 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002787 * @phba: pointer to lpfc hba data structure.
2788 *
James Smartda0436e2009-05-22 14:51:39 -04002789 * This routine is invoked to stop an SLI3 device port, it stops the device
2790 * from generating interrupts and stops the device driver's timers for the
2791 * device.
James Smarte59058c2008-08-24 21:49:00 -04002792 **/
James Smartdb2378e2008-02-08 18:49:51 -05002793static void
James Smartda0436e2009-05-22 14:51:39 -04002794lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002795{
James Smartda0436e2009-05-22 14:51:39 -04002796 /* Clear all interrupt enable conditions */
2797 writel(0, phba->HCregaddr);
2798 readl(phba->HCregaddr); /* flush */
2799 /* Clear all pending interrupts */
2800 writel(0xffffffff, phba->HAregaddr);
2801 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002802
James Smartda0436e2009-05-22 14:51:39 -04002803 /* Reset some HBA SLI setup states */
2804 lpfc_stop_hba_timers(phba);
2805 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002806}
2807
James Smarte59058c2008-08-24 21:49:00 -04002808/**
James Smartda0436e2009-05-22 14:51:39 -04002809 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002810 * @phba: pointer to lpfc hba data structure.
2811 *
James Smartda0436e2009-05-22 14:51:39 -04002812 * This routine is invoked to stop an SLI4 device port, it stops the device
2813 * from generating interrupts and stops the device driver's timers for the
2814 * device.
2815 **/
2816static void
2817lpfc_stop_port_s4(struct lpfc_hba *phba)
2818{
2819 /* Reset some HBA SLI4 setup states */
2820 lpfc_stop_hba_timers(phba);
2821 phba->pport->work_port_events = 0;
2822 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04002823}
2824
2825/**
2826 * lpfc_stop_port - Wrapper function for stopping hba port
2827 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002828 *
James Smartda0436e2009-05-22 14:51:39 -04002829 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2830 * the API jump table function pointer from the lpfc_hba struct.
2831 **/
2832void
2833lpfc_stop_port(struct lpfc_hba *phba)
2834{
2835 phba->lpfc_stop_port(phba);
2836}
2837
2838/**
2839 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2840 * @phba: pointer to lpfc hba data structure.
2841 *
2842 * This routine is invoked to remove the driver default fcf record from
2843 * the port. This routine currently acts on FCF Index 0.
2844 *
2845 **/
2846void
2847lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2848{
2849 int rc = 0;
2850 LPFC_MBOXQ_t *mboxq;
2851 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2852 uint32_t mbox_tmo, req_len;
2853 uint32_t shdr_status, shdr_add_status;
2854
2855 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2856 if (!mboxq) {
2857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2858 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2859 return;
2860 }
2861
2862 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2863 sizeof(struct lpfc_sli4_cfg_mhdr);
2864 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2865 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2866 req_len, LPFC_SLI4_MBX_EMBED);
2867 /*
2868 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2869 * supports multiple FCF indices.
2870 */
2871 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2872 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2873 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
James Smartecfd03c2010-02-12 14:41:27 -05002874 phba->fcf.current_rec.fcf_indx);
James Smartda0436e2009-05-22 14:51:39 -04002875
2876 if (!phba->sli4_hba.intr_enable)
2877 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2878 else {
2879 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2880 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2881 }
2882 /* The IOCTL status is embedded in the mailbox subheader. */
2883 shdr_status = bf_get(lpfc_mbox_hdr_status,
2884 &del_fcf_record->header.cfg_shdr.response);
2885 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2886 &del_fcf_record->header.cfg_shdr.response);
2887 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2889 "2516 DEL FCF of default FCF Index failed "
2890 "mbx status x%x, status x%x add_status x%x\n",
2891 rc, shdr_status, shdr_add_status);
2892 }
2893 if (rc != MBX_TIMEOUT)
2894 mempool_free(mboxq, phba->mbox_mem_pool);
2895}
2896
2897/**
James Smartecfd03c2010-02-12 14:41:27 -05002898 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2899 * @phba: Pointer to hba for which this call is being executed.
2900 *
2901 * This routine starts the timer waiting for the FCF rediscovery to complete.
2902 **/
2903void
2904lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2905{
2906 unsigned long fcf_redisc_wait_tmo =
2907 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2908 /* Start fcf rediscovery wait period timer */
2909 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2910 spin_lock_irq(&phba->hbalock);
2911 /* Allow action to new fcf asynchronous event */
2912 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2913 /* Mark the FCF rediscovery pending state */
2914 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2915 spin_unlock_irq(&phba->hbalock);
2916}
2917
2918/**
2919 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2920 * @ptr: Map to lpfc_hba data structure pointer.
2921 *
2922 * This routine is invoked when waiting for FCF table rediscover has been
2923 * timed out. If new FCF record(s) has (have) been discovered during the
2924 * wait period, a new FCF event shall be added to the FCOE async event
2925 * list, and then worker thread shall be waked up for processing from the
2926 * worker thread context.
2927 **/
2928void
2929lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2930{
2931 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2932
2933 /* Don't send FCF rediscovery event if timer cancelled */
2934 spin_lock_irq(&phba->hbalock);
2935 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2936 spin_unlock_irq(&phba->hbalock);
2937 return;
2938 }
2939 /* Clear FCF rediscovery timer pending flag */
2940 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2941 /* FCF rediscovery event to worker thread */
2942 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2943 spin_unlock_irq(&phba->hbalock);
2944 /* wake up worker thread */
2945 lpfc_worker_wake_up(phba);
2946}
2947
2948/**
James Smart6669f9b2009-10-02 15:16:45 -04002949 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2950 * @phba: pointer to lpfc hba data structure.
2951 *
2952 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2953 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2954 * was successful and the firmware supports FCoE. Any other return indicates
2955 * a error. It is assumed that this function will be called before interrupts
2956 * are enabled.
2957 **/
2958static int
2959lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2960{
2961 int rc = 0;
2962 LPFC_MBOXQ_t *mboxq;
2963 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2964 uint32_t length;
2965 uint32_t shdr_status, shdr_add_status;
2966
2967 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2968 if (!mboxq) {
2969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2970 "2621 Failed to allocate mbox for "
2971 "query firmware config cmd\n");
2972 return -ENOMEM;
2973 }
2974 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2975 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2976 sizeof(struct lpfc_sli4_cfg_mhdr));
2977 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2978 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2979 length, LPFC_SLI4_MBX_EMBED);
2980 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2981 /* The IOCTL status is embedded in the mailbox subheader. */
2982 shdr_status = bf_get(lpfc_mbox_hdr_status,
2983 &query_fw_cfg->header.cfg_shdr.response);
2984 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2985 &query_fw_cfg->header.cfg_shdr.response);
2986 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2988 "2622 Query Firmware Config failed "
2989 "mbx status x%x, status x%x add_status x%x\n",
2990 rc, shdr_status, shdr_add_status);
2991 return -EINVAL;
2992 }
2993 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2994 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2995 "2623 FCoE Function not supported by firmware. "
2996 "Function mode = %08x\n",
2997 query_fw_cfg->function_mode);
2998 return -EINVAL;
2999 }
3000 if (rc != MBX_TIMEOUT)
3001 mempool_free(mboxq, phba->mbox_mem_pool);
3002 return 0;
3003}
3004
3005/**
James Smartda0436e2009-05-22 14:51:39 -04003006 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3007 * @phba: pointer to lpfc hba data structure.
3008 * @acqe_link: pointer to the async link completion queue entry.
3009 *
3010 * This routine is to parse the SLI4 link-attention link fault code and
3011 * translate it into the base driver's read link attention mailbox command
3012 * status.
3013 *
3014 * Return: Link-attention status in terms of base driver's coding.
3015 **/
3016static uint16_t
3017lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3018 struct lpfc_acqe_link *acqe_link)
3019{
3020 uint16_t latt_fault;
3021
3022 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3023 case LPFC_ASYNC_LINK_FAULT_NONE:
3024 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3025 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3026 latt_fault = 0;
3027 break;
3028 default:
3029 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3030 "0398 Invalid link fault code: x%x\n",
3031 bf_get(lpfc_acqe_link_fault, acqe_link));
3032 latt_fault = MBXERR_ERROR;
3033 break;
3034 }
3035 return latt_fault;
3036}
3037
3038/**
3039 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3040 * @phba: pointer to lpfc hba data structure.
3041 * @acqe_link: pointer to the async link completion queue entry.
3042 *
3043 * This routine is to parse the SLI4 link attention type and translate it
3044 * into the base driver's link attention type coding.
3045 *
3046 * Return: Link attention type in terms of base driver's coding.
3047 **/
3048static uint8_t
3049lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3050 struct lpfc_acqe_link *acqe_link)
3051{
3052 uint8_t att_type;
3053
3054 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3055 case LPFC_ASYNC_LINK_STATUS_DOWN:
3056 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3057 att_type = AT_LINK_DOWN;
3058 break;
3059 case LPFC_ASYNC_LINK_STATUS_UP:
3060 /* Ignore physical link up events - wait for logical link up */
3061 att_type = AT_RESERVED;
3062 break;
3063 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3064 att_type = AT_LINK_UP;
3065 break;
3066 default:
3067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3068 "0399 Invalid link attention type: x%x\n",
3069 bf_get(lpfc_acqe_link_status, acqe_link));
3070 att_type = AT_RESERVED;
3071 break;
3072 }
3073 return att_type;
3074}
3075
3076/**
3077 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3078 * @phba: pointer to lpfc hba data structure.
3079 * @acqe_link: pointer to the async link completion queue entry.
3080 *
3081 * This routine is to parse the SLI4 link-attention link speed and translate
3082 * it into the base driver's link-attention link speed coding.
3083 *
3084 * Return: Link-attention link speed in terms of base driver's coding.
3085 **/
3086static uint8_t
3087lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3088 struct lpfc_acqe_link *acqe_link)
3089{
3090 uint8_t link_speed;
3091
3092 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3093 case LPFC_ASYNC_LINK_SPEED_ZERO:
3094 link_speed = LA_UNKNW_LINK;
3095 break;
3096 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3097 link_speed = LA_UNKNW_LINK;
3098 break;
3099 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3100 link_speed = LA_UNKNW_LINK;
3101 break;
3102 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3103 link_speed = LA_1GHZ_LINK;
3104 break;
3105 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3106 link_speed = LA_10GHZ_LINK;
3107 break;
3108 default:
3109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3110 "0483 Invalid link-attention link speed: x%x\n",
3111 bf_get(lpfc_acqe_link_speed, acqe_link));
3112 link_speed = LA_UNKNW_LINK;
3113 break;
3114 }
3115 return link_speed;
3116}
3117
3118/**
3119 * lpfc_sli4_async_link_evt - Process the asynchronous link event
3120 * @phba: pointer to lpfc hba data structure.
3121 * @acqe_link: pointer to the async link completion queue entry.
3122 *
3123 * This routine is to handle the SLI4 asynchronous link event.
3124 **/
3125static void
3126lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3127 struct lpfc_acqe_link *acqe_link)
3128{
3129 struct lpfc_dmabuf *mp;
3130 LPFC_MBOXQ_t *pmb;
3131 MAILBOX_t *mb;
3132 READ_LA_VAR *la;
3133 uint8_t att_type;
3134
3135 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3136 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3137 return;
James Smart32b97932009-07-19 10:01:21 -04003138 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003139 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3140 if (!pmb) {
3141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3142 "0395 The mboxq allocation failed\n");
3143 return;
3144 }
3145 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3146 if (!mp) {
3147 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3148 "0396 The lpfc_dmabuf allocation failed\n");
3149 goto out_free_pmb;
3150 }
3151 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3152 if (!mp->virt) {
3153 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3154 "0397 The mbuf allocation failed\n");
3155 goto out_free_dmabuf;
3156 }
3157
3158 /* Cleanup any outstanding ELS commands */
3159 lpfc_els_flush_all_cmd(phba);
3160
3161 /* Block ELS IOCBs until we have done process link event */
3162 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3163
3164 /* Update link event statistics */
3165 phba->sli.slistat.link_event++;
3166
3167 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3168 lpfc_read_la(phba, pmb, mp);
3169 pmb->vport = phba->pport;
3170
3171 /* Parse and translate status field */
3172 mb = &pmb->u.mb;
3173 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3174
3175 /* Parse and translate link attention fields */
3176 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3177 la->eventTag = acqe_link->event_tag;
3178 la->attType = att_type;
3179 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3180
3181 /* Fake the the following irrelvant fields */
3182 la->topology = TOPOLOGY_PT_PT;
3183 la->granted_AL_PA = 0;
3184 la->il = 0;
3185 la->pb = 0;
3186 la->fa = 0;
3187 la->mm = 0;
3188
3189 /* Keep the link status for extra SLI4 state machine reference */
3190 phba->sli4_hba.link_state.speed =
3191 bf_get(lpfc_acqe_link_speed, acqe_link);
3192 phba->sli4_hba.link_state.duplex =
3193 bf_get(lpfc_acqe_link_duplex, acqe_link);
3194 phba->sli4_hba.link_state.status =
3195 bf_get(lpfc_acqe_link_status, acqe_link);
3196 phba->sli4_hba.link_state.physical =
3197 bf_get(lpfc_acqe_link_physical, acqe_link);
3198 phba->sli4_hba.link_state.fault =
3199 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003200 phba->sli4_hba.link_state.logical_speed =
3201 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003202
3203 /* Invoke the lpfc_handle_latt mailbox command callback function */
3204 lpfc_mbx_cmpl_read_la(phba, pmb);
3205
3206 return;
3207
3208out_free_dmabuf:
3209 kfree(mp);
3210out_free_pmb:
3211 mempool_free(pmb, phba->mbox_mem_pool);
3212}
3213
3214/**
3215 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3216 * @phba: pointer to lpfc hba data structure.
3217 * @acqe_link: pointer to the async fcoe completion queue entry.
3218 *
3219 * This routine is to handle the SLI4 asynchronous fcoe event.
3220 **/
3221static void
3222lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3223 struct lpfc_acqe_fcoe *acqe_fcoe)
3224{
3225 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3226 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003227 struct lpfc_vport *vport;
3228 struct lpfc_nodelist *ndlp;
3229 struct Scsi_Host *shost;
James Smart97957242009-12-21 17:03:15 -05003230 uint32_t link_state;
James Smart695a8142010-01-26 23:08:03 -05003231 int active_vlink_present;
3232 struct lpfc_vport **vports;
3233 int i;
James Smartda0436e2009-05-22 14:51:39 -04003234
James Smart4d9ab992009-10-02 15:16:39 -04003235 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003236 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003237 switch (event_type) {
3238 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
James Smartecfd03c2010-02-12 14:41:27 -05003239 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
James Smartda0436e2009-05-22 14:51:39 -04003240 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smarte4e74272009-07-19 10:01:38 -04003241 "2546 New FCF found index 0x%x tag 0x%x\n",
James Smart6669f9b2009-10-02 15:16:45 -04003242 acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003243 acqe_fcoe->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003244 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003245 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3246 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3247 /*
3248 * If the current FCF is in discovered state or
3249 * FCF discovery is in progress, do nothing.
3250 */
3251 spin_unlock_irq(&phba->hbalock);
3252 break;
3253 }
3254 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3255 /*
3256 * If fast FCF failover rescan event is pending,
3257 * do nothing.
3258 */
James Smartda0436e2009-05-22 14:51:39 -04003259 spin_unlock_irq(&phba->hbalock);
3260 break;
3261 }
3262 spin_unlock_irq(&phba->hbalock);
3263
3264 /* Read the FCF table and re-discover SAN. */
James Smart6a9c52c2009-10-02 15:16:51 -04003265 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003266 if (rc)
3267 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003268 "2547 Read FCF record failed 0x%x\n",
3269 rc);
James Smartda0436e2009-05-22 14:51:39 -04003270 break;
3271
3272 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3273 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003274 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003275 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3276 acqe_fcoe->event_tag);
3277 break;
3278
3279 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3280 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003281 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003282 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003283 acqe_fcoe->event_tag);
3284 /* If the event is not for currently used fcf do nothing */
James Smartecfd03c2010-02-12 14:41:27 -05003285 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003286 break;
3287 /*
3288 * Currently, driver support only one FCF - so treat this as
James Smart97957242009-12-21 17:03:15 -05003289 * a link down, but save the link state because we don't want
3290 * it to be changed to Link Down unless it is already down.
James Smartda0436e2009-05-22 14:51:39 -04003291 */
James Smart97957242009-12-21 17:03:15 -05003292 link_state = phba->link_state;
James Smartda0436e2009-05-22 14:51:39 -04003293 lpfc_linkdown(phba);
James Smart97957242009-12-21 17:03:15 -05003294 phba->link_state = link_state;
James Smartda0436e2009-05-22 14:51:39 -04003295 /* Unregister FCF if no devices connected to it */
3296 lpfc_unregister_unused_fcf(phba);
3297 break;
James Smart6669f9b2009-10-02 15:16:45 -04003298 case LPFC_FCOE_EVENT_TYPE_CVL:
3299 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3300 "2718 Clear Virtual Link Received for VPI 0x%x"
3301 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3302 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003303 acqe_fcoe->index - phba->vpi_base);
James Smart6669f9b2009-10-02 15:16:45 -04003304 if (!vport)
3305 break;
3306 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3307 if (!ndlp)
3308 break;
3309 shost = lpfc_shost_from_vport(vport);
James Smart695a8142010-01-26 23:08:03 -05003310 if (phba->pport->port_state <= LPFC_FLOGI)
3311 break;
3312 /* If virtual link is not yet instantiated ignore CVL */
3313 if (vport->port_state <= LPFC_FDISC)
3314 break;
3315
James Smart6669f9b2009-10-02 15:16:45 -04003316 lpfc_linkdown_port(vport);
James Smart695a8142010-01-26 23:08:03 -05003317 lpfc_cleanup_pending_mbox(vport);
3318 spin_lock_irq(shost->host_lock);
3319 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3320 spin_unlock_irq(shost->host_lock);
3321 active_vlink_present = 0;
3322
3323 vports = lpfc_create_vport_work_array(phba);
3324 if (vports) {
3325 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3326 i++) {
3327 if ((!(vports[i]->fc_flag &
3328 FC_VPORT_CVL_RCVD)) &&
3329 (vports[i]->port_state > LPFC_FDISC)) {
3330 active_vlink_present = 1;
3331 break;
3332 }
3333 }
3334 lpfc_destroy_vport_work_array(phba, vports);
3335 }
3336
3337 if (active_vlink_present) {
3338 /*
3339 * If there are other active VLinks present,
3340 * re-instantiate the Vlink using FDISC.
3341 */
James Smart6669f9b2009-10-02 15:16:45 -04003342 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3343 spin_lock_irq(shost->host_lock);
3344 ndlp->nlp_flag |= NLP_DELAY_TMO;
3345 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003346 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3347 vport->port_state = LPFC_FDISC;
3348 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003349 /*
3350 * Otherwise, we request port to rediscover
3351 * the entire FCF table for a fast recovery
3352 * from possible case that the current FCF
3353 * is no longer valid.
3354 */
3355 rc = lpfc_sli4_redisc_fcf_table(phba);
3356 if (rc)
3357 /*
3358 * Last resort will be re-try on the
3359 * the current registered FCF entry.
3360 */
3361 lpfc_retry_pport_discovery(phba);
James Smart6669f9b2009-10-02 15:16:45 -04003362 }
3363 break;
James Smartda0436e2009-05-22 14:51:39 -04003364 default:
3365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3366 "0288 Unknown FCoE event type 0x%x event tag "
3367 "0x%x\n", event_type, acqe_fcoe->event_tag);
3368 break;
3369 }
3370}
3371
3372/**
3373 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3374 * @phba: pointer to lpfc hba data structure.
3375 * @acqe_link: pointer to the async dcbx completion queue entry.
3376 *
3377 * This routine is to handle the SLI4 asynchronous dcbx event.
3378 **/
3379static void
3380lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3381 struct lpfc_acqe_dcbx *acqe_dcbx)
3382{
James Smart4d9ab992009-10-02 15:16:39 -04003383 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003384 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3385 "0290 The SLI4 DCBX asynchronous event is not "
3386 "handled yet\n");
3387}
3388
3389/**
3390 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3391 * @phba: pointer to lpfc hba data structure.
3392 *
3393 * This routine is invoked by the worker thread to process all the pending
3394 * SLI4 asynchronous events.
3395 **/
3396void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3397{
3398 struct lpfc_cq_event *cq_event;
3399
3400 /* First, declare the async event has been handled */
3401 spin_lock_irq(&phba->hbalock);
3402 phba->hba_flag &= ~ASYNC_EVENT;
3403 spin_unlock_irq(&phba->hbalock);
3404 /* Now, handle all the async events */
3405 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3406 /* Get the first event from the head of the event queue */
3407 spin_lock_irq(&phba->hbalock);
3408 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3409 cq_event, struct lpfc_cq_event, list);
3410 spin_unlock_irq(&phba->hbalock);
3411 /* Process the asynchronous event */
3412 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3413 case LPFC_TRAILER_CODE_LINK:
3414 lpfc_sli4_async_link_evt(phba,
3415 &cq_event->cqe.acqe_link);
3416 break;
3417 case LPFC_TRAILER_CODE_FCOE:
3418 lpfc_sli4_async_fcoe_evt(phba,
3419 &cq_event->cqe.acqe_fcoe);
3420 break;
3421 case LPFC_TRAILER_CODE_DCBX:
3422 lpfc_sli4_async_dcbx_evt(phba,
3423 &cq_event->cqe.acqe_dcbx);
3424 break;
3425 default:
3426 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3427 "1804 Invalid asynchrous event code: "
3428 "x%x\n", bf_get(lpfc_trailer_code,
3429 &cq_event->cqe.mcqe_cmpl));
3430 break;
3431 }
3432 /* Free the completion event processed to the free pool */
3433 lpfc_sli4_cq_event_release(phba, cq_event);
3434 }
3435}
3436
3437/**
James Smartecfd03c2010-02-12 14:41:27 -05003438 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3439 * @phba: pointer to lpfc hba data structure.
3440 *
3441 * This routine is invoked by the worker thread to process FCF table
3442 * rediscovery pending completion event.
3443 **/
3444void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3445{
3446 int rc;
3447
3448 spin_lock_irq(&phba->hbalock);
3449 /* Clear FCF rediscovery timeout event */
3450 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3451 /* Clear driver fast failover FCF record flag */
3452 phba->fcf.failover_rec.flag = 0;
3453 /* Set state for FCF fast failover */
3454 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3455 spin_unlock_irq(&phba->hbalock);
3456
3457 /* Scan FCF table from the first entry to re-discover SAN */
3458 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
3459 if (rc)
3460 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3461 "2747 Post FCF rediscovery read FCF record "
3462 "failed 0x%x\n", rc);
3463}
3464
3465/**
James Smartda0436e2009-05-22 14:51:39 -04003466 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3467 * @phba: pointer to lpfc hba data structure.
3468 * @dev_grp: The HBA PCI-Device group number.
3469 *
3470 * This routine is invoked to set up the per HBA PCI-Device group function
3471 * API jump table entries.
3472 *
3473 * Return: 0 if success, otherwise -ENODEV
3474 **/
3475int
3476lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003477{
3478 int rc;
3479
James Smartda0436e2009-05-22 14:51:39 -04003480 /* Set up lpfc PCI-device group */
3481 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003482
James Smartda0436e2009-05-22 14:51:39 -04003483 /* The LPFC_PCI_DEV_OC uses SLI4 */
3484 if (dev_grp == LPFC_PCI_DEV_OC)
3485 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003486
James Smartda0436e2009-05-22 14:51:39 -04003487 /* Set up device INIT API function jump table */
3488 rc = lpfc_init_api_table_setup(phba, dev_grp);
3489 if (rc)
3490 return -ENODEV;
3491 /* Set up SCSI API function jump table */
3492 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3493 if (rc)
3494 return -ENODEV;
3495 /* Set up SLI API function jump table */
3496 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3497 if (rc)
3498 return -ENODEV;
3499 /* Set up MBOX API function jump table */
3500 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3501 if (rc)
3502 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003503
James Smartda0436e2009-05-22 14:51:39 -04003504 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003505}
3506
3507/**
James Smart3621a712009-04-06 18:47:14 -04003508 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003509 * @phba: pointer to lpfc hba data structure.
3510 * @intr_mode: active interrupt mode adopted.
3511 *
3512 * This routine it invoked to log the currently used active interrupt mode
3513 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003514 **/
3515static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003516{
3517 switch (intr_mode) {
3518 case 0:
3519 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3520 "0470 Enable INTx interrupt mode.\n");
3521 break;
3522 case 1:
3523 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3524 "0481 Enabled MSI interrupt mode.\n");
3525 break;
3526 case 2:
3527 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3528 "0480 Enabled MSI-X interrupt mode.\n");
3529 break;
3530 default:
3531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3532 "0482 Illegal interrupt mode.\n");
3533 break;
3534 }
3535 return;
3536}
3537
James Smart5b75da22008-12-04 22:39:35 -05003538/**
James Smart3772a992009-05-22 14:50:54 -04003539 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003540 * @phba: pointer to lpfc hba data structure.
3541 *
James Smart3772a992009-05-22 14:50:54 -04003542 * This routine is invoked to enable the PCI device that is common to all
3543 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003544 *
3545 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003546 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003547 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003548 **/
James Smart3772a992009-05-22 14:50:54 -04003549static int
3550lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003551{
James Smart3772a992009-05-22 14:50:54 -04003552 struct pci_dev *pdev;
3553 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003554
James Smart3772a992009-05-22 14:50:54 -04003555 /* Obtain PCI device reference */
3556 if (!phba->pcidev)
3557 goto out_error;
3558 else
3559 pdev = phba->pcidev;
3560 /* Select PCI BARs */
3561 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3562 /* Enable PCI device */
3563 if (pci_enable_device_mem(pdev))
3564 goto out_error;
3565 /* Request PCI resource for the device */
3566 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3567 goto out_disable_device;
3568 /* Set up device as PCI master and save state for EEH */
3569 pci_set_master(pdev);
3570 pci_try_set_mwi(pdev);
3571 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003572
James Smart3772a992009-05-22 14:50:54 -04003573 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003574
James Smart3772a992009-05-22 14:50:54 -04003575out_disable_device:
3576 pci_disable_device(pdev);
3577out_error:
3578 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003579}
3580
3581/**
James Smart3772a992009-05-22 14:50:54 -04003582 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003583 * @phba: pointer to lpfc hba data structure.
3584 *
James Smart3772a992009-05-22 14:50:54 -04003585 * This routine is invoked to disable the PCI device that is common to all
3586 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003587 **/
3588static void
James Smart3772a992009-05-22 14:50:54 -04003589lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003590{
James Smart3772a992009-05-22 14:50:54 -04003591 struct pci_dev *pdev;
3592 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003593
James Smart3772a992009-05-22 14:50:54 -04003594 /* Obtain PCI device reference */
3595 if (!phba->pcidev)
3596 return;
3597 else
3598 pdev = phba->pcidev;
3599 /* Select PCI BARs */
3600 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3601 /* Release PCI resource and disable PCI device */
3602 pci_release_selected_regions(pdev, bars);
3603 pci_disable_device(pdev);
3604 /* Null out PCI private reference to driver */
3605 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003606
3607 return;
3608}
3609
3610/**
James Smart3772a992009-05-22 14:50:54 -04003611 * lpfc_reset_hba - Reset a hba
3612 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003613 *
James Smart3772a992009-05-22 14:50:54 -04003614 * This routine is invoked to reset a hba device. It brings the HBA
3615 * offline, performs a board restart, and then brings the board back
3616 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3617 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003618 **/
James Smart3772a992009-05-22 14:50:54 -04003619void
3620lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003621{
James Smart3772a992009-05-22 14:50:54 -04003622 /* If resets are disabled then set error state and return. */
3623 if (!phba->cfg_enable_hba_reset) {
3624 phba->link_state = LPFC_HBA_ERROR;
3625 return;
3626 }
3627 lpfc_offline_prep(phba);
3628 lpfc_offline(phba);
3629 lpfc_sli_brdrestart(phba);
3630 lpfc_online(phba);
3631 lpfc_unblock_mgmt_io(phba);
3632}
dea31012005-04-17 16:05:31 -05003633
James Smart3772a992009-05-22 14:50:54 -04003634/**
3635 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3636 * @phba: pointer to lpfc hba data structure.
3637 *
3638 * This routine is invoked to set up the driver internal resources specific to
3639 * support the SLI-3 HBA device it attached to.
3640 *
3641 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003642 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003643 * other values - error
3644 **/
3645static int
3646lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3647{
3648 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003649
James Smart3772a992009-05-22 14:50:54 -04003650 /*
3651 * Initialize timers used by driver
3652 */
dea31012005-04-17 16:05:31 -05003653
James Smart3772a992009-05-22 14:50:54 -04003654 /* Heartbeat timer */
3655 init_timer(&phba->hb_tmofunc);
3656 phba->hb_tmofunc.function = lpfc_hb_timeout;
3657 phba->hb_tmofunc.data = (unsigned long)phba;
3658
3659 psli = &phba->sli;
3660 /* MBOX heartbeat timer */
3661 init_timer(&psli->mbox_tmo);
3662 psli->mbox_tmo.function = lpfc_mbox_timeout;
3663 psli->mbox_tmo.data = (unsigned long) phba;
3664 /* FCP polling mode timer */
3665 init_timer(&phba->fcp_poll_timer);
3666 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3667 phba->fcp_poll_timer.data = (unsigned long) phba;
3668 /* Fabric block timer */
3669 init_timer(&phba->fabric_block_timer);
3670 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3671 phba->fabric_block_timer.data = (unsigned long) phba;
3672 /* EA polling mode timer */
3673 init_timer(&phba->eratt_poll);
3674 phba->eratt_poll.function = lpfc_poll_eratt;
3675 phba->eratt_poll.data = (unsigned long) phba;
3676
3677 /* Host attention work mask setup */
3678 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3679 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3680
3681 /* Get all the module params for configuring this host */
3682 lpfc_get_cfgparam(phba);
3683 /*
3684 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3685 * used to create the sg_dma_buf_pool must be dynamically calculated.
3686 * 2 segments are added since the IOCB needs a command and response bde.
3687 */
3688 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3689 sizeof(struct fcp_rsp) +
3690 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3691
3692 if (phba->cfg_enable_bg) {
3693 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3694 phba->cfg_sg_dma_buf_size +=
3695 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3696 }
3697
3698 /* Also reinitialize the host templates with new values. */
3699 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3700 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3701
3702 phba->max_vpi = LPFC_MAX_VPI;
3703 /* This will be set to correct value after config_port mbox */
3704 phba->max_vports = 0;
3705
3706 /*
3707 * Initialize the SLI Layer to run with lpfc HBAs.
3708 */
3709 lpfc_sli_setup(phba);
3710 lpfc_sli_queue_setup(phba);
3711
3712 /* Allocate device driver memory */
3713 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3714 return -ENOMEM;
3715
3716 return 0;
3717}
3718
3719/**
3720 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3721 * @phba: pointer to lpfc hba data structure.
3722 *
3723 * This routine is invoked to unset the driver internal resources set up
3724 * specific for supporting the SLI-3 HBA device it attached to.
3725 **/
3726static void
3727lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3728{
3729 /* Free device driver memory allocated */
3730 lpfc_mem_free_all(phba);
3731
3732 return;
3733}
3734
3735/**
James Smartda0436e2009-05-22 14:51:39 -04003736 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3737 * @phba: pointer to lpfc hba data structure.
3738 *
3739 * This routine is invoked to set up the driver internal resources specific to
3740 * support the SLI-4 HBA device it attached to.
3741 *
3742 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003743 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04003744 * other values - error
3745 **/
3746static int
3747lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3748{
3749 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05003750 LPFC_MBOXQ_t *mboxq;
3751 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3752 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3753 struct lpfc_mqe *mqe;
James Smartda0436e2009-05-22 14:51:39 -04003754
3755 /* Before proceed, wait for POST done and device ready */
3756 rc = lpfc_sli4_post_status_check(phba);
3757 if (rc)
3758 return -ENODEV;
3759
3760 /*
3761 * Initialize timers used by driver
3762 */
3763
3764 /* Heartbeat timer */
3765 init_timer(&phba->hb_tmofunc);
3766 phba->hb_tmofunc.function = lpfc_hb_timeout;
3767 phba->hb_tmofunc.data = (unsigned long)phba;
3768
3769 psli = &phba->sli;
3770 /* MBOX heartbeat timer */
3771 init_timer(&psli->mbox_tmo);
3772 psli->mbox_tmo.function = lpfc_mbox_timeout;
3773 psli->mbox_tmo.data = (unsigned long) phba;
3774 /* Fabric block timer */
3775 init_timer(&phba->fabric_block_timer);
3776 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3777 phba->fabric_block_timer.data = (unsigned long) phba;
3778 /* EA polling mode timer */
3779 init_timer(&phba->eratt_poll);
3780 phba->eratt_poll.function = lpfc_poll_eratt;
3781 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05003782 /* FCF rediscover timer */
3783 init_timer(&phba->fcf.redisc_wait);
3784 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3785 phba->fcf.redisc_wait.data = (unsigned long)phba;
3786
James Smartda0436e2009-05-22 14:51:39 -04003787 /*
3788 * We need to do a READ_CONFIG mailbox command here before
3789 * calling lpfc_get_cfgparam. For VFs this will report the
3790 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3791 * All of the resources allocated
3792 * for this Port are tied to these values.
3793 */
3794 /* Get all the module params for configuring this host */
3795 lpfc_get_cfgparam(phba);
3796 phba->max_vpi = LPFC_MAX_VPI;
3797 /* This will be set to correct value after the read_config mbox */
3798 phba->max_vports = 0;
3799
3800 /* Program the default value of vlan_id and fc_map */
3801 phba->valid_vlan = 0;
3802 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3803 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3804 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3805
3806 /*
3807 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3808 * used to create the sg_dma_buf_pool must be dynamically calculated.
3809 * 2 segments are added since the IOCB needs a command and response bde.
3810 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05003811 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04003812 */
James Smart28baac72010-02-12 14:42:03 -05003813 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
3814 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
3815 /* Feature Level 1 hardware is limited to 2 pages */
3816 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
3817 LPFC_SLI_INTF_FEATURELEVEL1_1))
3818 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04003819 else
James Smart28baac72010-02-12 14:42:03 -05003820 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
3821 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
3822 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
3823 dma_buf_size = dma_buf_size << 1)
3824 ;
3825 if (dma_buf_size == max_buf_size)
3826 phba->cfg_sg_seg_cnt = (dma_buf_size -
3827 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
3828 (2 * sizeof(struct sli4_sge))) /
3829 sizeof(struct sli4_sge);
3830 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04003831
3832 /* Initialize buffer queue management fields */
3833 hbq_count = lpfc_sli_hbq_count();
3834 for (i = 0; i < hbq_count; ++i)
3835 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3836 INIT_LIST_HEAD(&phba->rb_pend_list);
3837 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3838 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3839
3840 /*
3841 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3842 */
3843 /* Initialize the Abort scsi buffer list used by driver */
3844 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3845 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3846 /* This abort list used by worker thread */
3847 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3848
3849 /*
3850 * Initialize dirver internal slow-path work queues
3851 */
3852
3853 /* Driver internel slow-path CQ Event pool */
3854 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3855 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04003856 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04003857 /* Asynchronous event CQ Event work queue list */
3858 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3859 /* Fast-path XRI aborted CQ Event work queue list */
3860 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3861 /* Slow-path XRI aborted CQ Event work queue list */
3862 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3863 /* Receive queue CQ Event work queue list */
3864 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3865
3866 /* Initialize the driver internal SLI layer lists. */
3867 lpfc_sli_setup(phba);
3868 lpfc_sli_queue_setup(phba);
3869
3870 /* Allocate device driver memory */
3871 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3872 if (rc)
3873 return -ENOMEM;
3874
3875 /* Create the bootstrap mailbox command */
3876 rc = lpfc_create_bootstrap_mbox(phba);
3877 if (unlikely(rc))
3878 goto out_free_mem;
3879
3880 /* Set up the host's endian order with the device. */
3881 rc = lpfc_setup_endian_order(phba);
3882 if (unlikely(rc))
3883 goto out_free_bsmbx;
3884
James Smart6669f9b2009-10-02 15:16:45 -04003885 rc = lpfc_sli4_fw_cfg_check(phba);
3886 if (unlikely(rc))
3887 goto out_free_bsmbx;
3888
James Smartda0436e2009-05-22 14:51:39 -04003889 /* Set up the hba's configuration parameters. */
3890 rc = lpfc_sli4_read_config(phba);
3891 if (unlikely(rc))
3892 goto out_free_bsmbx;
3893
3894 /* Perform a function reset */
3895 rc = lpfc_pci_function_reset(phba);
3896 if (unlikely(rc))
3897 goto out_free_bsmbx;
3898
3899 /* Create all the SLI4 queues */
3900 rc = lpfc_sli4_queue_create(phba);
3901 if (rc)
3902 goto out_free_bsmbx;
3903
3904 /* Create driver internal CQE event pool */
3905 rc = lpfc_sli4_cq_event_pool_create(phba);
3906 if (rc)
3907 goto out_destroy_queue;
3908
3909 /* Initialize and populate the iocb list per host */
3910 rc = lpfc_init_sgl_list(phba);
3911 if (rc) {
3912 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3913 "1400 Failed to initialize sgl list.\n");
3914 goto out_destroy_cq_event_pool;
3915 }
3916 rc = lpfc_init_active_sgl_array(phba);
3917 if (rc) {
3918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3919 "1430 Failed to initialize sgl list.\n");
3920 goto out_free_sgl_list;
3921 }
3922
3923 rc = lpfc_sli4_init_rpi_hdrs(phba);
3924 if (rc) {
3925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3926 "1432 Failed to initialize rpi headers.\n");
3927 goto out_free_active_sgl;
3928 }
3929
3930 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3931 phba->cfg_fcp_eq_count), GFP_KERNEL);
3932 if (!phba->sli4_hba.fcp_eq_hdl) {
3933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3934 "2572 Failed allocate memory for fast-path "
3935 "per-EQ handle array\n");
3936 goto out_remove_rpi_hdrs;
3937 }
3938
3939 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3940 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3941 if (!phba->sli4_hba.msix_entries) {
3942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3943 "2573 Failed allocate memory for msi-x "
3944 "interrupt vector entries\n");
3945 goto out_free_fcp_eq_hdl;
3946 }
3947
James Smart28baac72010-02-12 14:42:03 -05003948 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3949 GFP_KERNEL);
3950 if (!mboxq) {
3951 rc = -ENOMEM;
3952 goto out_free_fcp_eq_hdl;
3953 }
3954
3955 /* Get the Supported Pages. It is always available. */
3956 lpfc_supported_pages(mboxq);
3957 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3958 if (unlikely(rc)) {
3959 rc = -EIO;
3960 mempool_free(mboxq, phba->mbox_mem_pool);
3961 goto out_free_fcp_eq_hdl;
3962 }
3963
3964 mqe = &mboxq->u.mqe;
3965 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
3966 LPFC_MAX_SUPPORTED_PAGES);
3967 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
3968 switch (pn_page[i]) {
3969 case LPFC_SLI4_PARAMETERS:
3970 phba->sli4_hba.pc_sli4_params.supported = 1;
3971 break;
3972 default:
3973 break;
3974 }
3975 }
3976
3977 /* Read the port's SLI4 Parameters capabilities if supported. */
3978 if (phba->sli4_hba.pc_sli4_params.supported)
3979 rc = lpfc_pc_sli4_params_get(phba, mboxq);
3980 mempool_free(mboxq, phba->mbox_mem_pool);
3981 if (rc) {
3982 rc = -EIO;
3983 goto out_free_fcp_eq_hdl;
3984 }
James Smartda0436e2009-05-22 14:51:39 -04003985 return rc;
3986
3987out_free_fcp_eq_hdl:
3988 kfree(phba->sli4_hba.fcp_eq_hdl);
3989out_remove_rpi_hdrs:
3990 lpfc_sli4_remove_rpi_hdrs(phba);
3991out_free_active_sgl:
3992 lpfc_free_active_sgl(phba);
3993out_free_sgl_list:
3994 lpfc_free_sgl_list(phba);
3995out_destroy_cq_event_pool:
3996 lpfc_sli4_cq_event_pool_destroy(phba);
3997out_destroy_queue:
3998 lpfc_sli4_queue_destroy(phba);
3999out_free_bsmbx:
4000 lpfc_destroy_bootstrap_mbox(phba);
4001out_free_mem:
4002 lpfc_mem_free(phba);
4003 return rc;
4004}
4005
4006/**
4007 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4008 * @phba: pointer to lpfc hba data structure.
4009 *
4010 * This routine is invoked to unset the driver internal resources set up
4011 * specific for supporting the SLI-4 HBA device it attached to.
4012 **/
4013static void
4014lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4015{
4016 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4017
4018 /* unregister default FCFI from the HBA */
4019 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4020
4021 /* Free the default FCR table */
4022 lpfc_sli_remove_dflt_fcf(phba);
4023
4024 /* Free memory allocated for msi-x interrupt vector entries */
4025 kfree(phba->sli4_hba.msix_entries);
4026
4027 /* Free memory allocated for fast-path work queue handles */
4028 kfree(phba->sli4_hba.fcp_eq_hdl);
4029
4030 /* Free the allocated rpi headers. */
4031 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004032 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004033
4034 /* Free the ELS sgl list */
4035 lpfc_free_active_sgl(phba);
4036 lpfc_free_sgl_list(phba);
4037
4038 /* Free the SCSI sgl management array */
4039 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4040
4041 /* Free the SLI4 queues */
4042 lpfc_sli4_queue_destroy(phba);
4043
4044 /* Free the completion queue EQ event pool */
4045 lpfc_sli4_cq_event_release_all(phba);
4046 lpfc_sli4_cq_event_pool_destroy(phba);
4047
4048 /* Reset SLI4 HBA FCoE function */
4049 lpfc_pci_function_reset(phba);
4050
4051 /* Free the bsmbx region. */
4052 lpfc_destroy_bootstrap_mbox(phba);
4053
4054 /* Free the SLI Layer memory with SLI4 HBAs */
4055 lpfc_mem_free_all(phba);
4056
4057 /* Free the current connect table */
4058 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004059 &phba->fcf_conn_rec_list, list) {
4060 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004061 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004062 }
James Smartda0436e2009-05-22 14:51:39 -04004063
4064 return;
4065}
4066
4067/**
James Smart3772a992009-05-22 14:50:54 -04004068 * lpfc_init_api_table_setup - Set up init api fucntion jump table
4069 * @phba: The hba struct for which this call is being executed.
4070 * @dev_grp: The HBA PCI-Device group number.
4071 *
4072 * This routine sets up the device INIT interface API function jump table
4073 * in @phba struct.
4074 *
4075 * Returns: 0 - success, -ENODEV - failure.
4076 **/
4077int
4078lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4079{
James Smart84d1b002010-02-12 14:42:33 -05004080 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4081 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart3772a992009-05-22 14:50:54 -04004082 switch (dev_grp) {
4083 case LPFC_PCI_DEV_LP:
4084 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4085 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4086 phba->lpfc_stop_port = lpfc_stop_port_s3;
4087 break;
James Smartda0436e2009-05-22 14:51:39 -04004088 case LPFC_PCI_DEV_OC:
4089 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4090 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4091 phba->lpfc_stop_port = lpfc_stop_port_s4;
4092 break;
James Smart3772a992009-05-22 14:50:54 -04004093 default:
4094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4095 "1431 Invalid HBA PCI-device group: 0x%x\n",
4096 dev_grp);
4097 return -ENODEV;
4098 break;
4099 }
4100 return 0;
4101}
4102
4103/**
4104 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4105 * @phba: pointer to lpfc hba data structure.
4106 *
4107 * This routine is invoked to set up the driver internal resources before the
4108 * device specific resource setup to support the HBA device it attached to.
4109 *
4110 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004111 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004112 * other values - error
4113 **/
4114static int
4115lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4116{
4117 /*
4118 * Driver resources common to all SLI revisions
4119 */
James Smartea2151b2008-09-07 11:52:10 -04004120 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004121 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004122
James Smarte47c9092008-02-08 18:49:26 -05004123 /* Initialize ndlp management spinlock */
4124 spin_lock_init(&phba->ndlp_lock);
4125
James Smart2e0fef82007-06-17 19:56:36 -05004126 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004127 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004128 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004129
James Smart3772a992009-05-22 14:50:54 -04004130 /* Initialize the wait queue head for the kernel thread */
4131 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004132
James Smart3772a992009-05-22 14:50:54 -04004133 /* Initialize the scsi buffer list used by driver for scsi IO */
4134 spin_lock_init(&phba->scsi_buf_list_lock);
4135 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004136
James Smart3772a992009-05-22 14:50:54 -04004137 /* Initialize the fabric iocb list */
4138 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004139
James Smart3772a992009-05-22 14:50:54 -04004140 /* Initialize list to save ELS buffers */
4141 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004142
James Smart3772a992009-05-22 14:50:54 -04004143 /* Initialize FCF connection rec list */
4144 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004145
James Smart3772a992009-05-22 14:50:54 -04004146 return 0;
4147}
dea31012005-04-17 16:05:31 -05004148
James Smart3772a992009-05-22 14:50:54 -04004149/**
4150 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4151 * @phba: pointer to lpfc hba data structure.
4152 *
4153 * This routine is invoked to set up the driver internal resources after the
4154 * device specific resource setup to support the HBA device it attached to.
4155 *
4156 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004157 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004158 * other values - error
4159 **/
4160static int
4161lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4162{
4163 int error;
4164
4165 /* Startup the kernel thread for this host adapter. */
4166 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4167 "lpfc_worker_%d", phba->brd_no);
4168 if (IS_ERR(phba->worker_thread)) {
4169 error = PTR_ERR(phba->worker_thread);
4170 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004171 }
4172
James Smart3772a992009-05-22 14:50:54 -04004173 return 0;
4174}
4175
4176/**
4177 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4178 * @phba: pointer to lpfc hba data structure.
4179 *
4180 * This routine is invoked to unset the driver internal resources set up after
4181 * the device specific resource setup for supporting the HBA device it
4182 * attached to.
4183 **/
4184static void
4185lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4186{
4187 /* Stop kernel worker thread */
4188 kthread_stop(phba->worker_thread);
4189}
4190
4191/**
4192 * lpfc_free_iocb_list - Free iocb list.
4193 * @phba: pointer to lpfc hba data structure.
4194 *
4195 * This routine is invoked to free the driver's IOCB list and memory.
4196 **/
4197static void
4198lpfc_free_iocb_list(struct lpfc_hba *phba)
4199{
4200 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4201
4202 spin_lock_irq(&phba->hbalock);
4203 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4204 &phba->lpfc_iocb_list, list) {
4205 list_del(&iocbq_entry->list);
4206 kfree(iocbq_entry);
4207 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004208 }
James Smart3772a992009-05-22 14:50:54 -04004209 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004210
James Smart3772a992009-05-22 14:50:54 -04004211 return;
4212}
dea31012005-04-17 16:05:31 -05004213
James Smart3772a992009-05-22 14:50:54 -04004214/**
4215 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4216 * @phba: pointer to lpfc hba data structure.
4217 *
4218 * This routine is invoked to allocate and initizlize the driver's IOCB
4219 * list and set up the IOCB tag array accordingly.
4220 *
4221 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004222 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004223 * other values - error
4224 **/
4225static int
4226lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4227{
4228 struct lpfc_iocbq *iocbq_entry = NULL;
4229 uint16_t iotag;
4230 int i;
dea31012005-04-17 16:05:31 -05004231
4232 /* Initialize and populate the iocb list per host. */
4233 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004234 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004235 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004236 if (iocbq_entry == NULL) {
4237 printk(KERN_ERR "%s: only allocated %d iocbs of "
4238 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004239 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004240 goto out_free_iocbq;
4241 }
4242
James Bottomley604a3e32005-10-29 10:28:33 -05004243 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4244 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004245 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004246 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004247 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004248 goto out_free_iocbq;
4249 }
James Smart3772a992009-05-22 14:50:54 -04004250 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004251
4252 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004253 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4254 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004255 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004256 }
4257
James Smart3772a992009-05-22 14:50:54 -04004258 return 0;
4259
4260out_free_iocbq:
4261 lpfc_free_iocb_list(phba);
4262
4263 return -ENOMEM;
4264}
4265
4266/**
James Smartda0436e2009-05-22 14:51:39 -04004267 * lpfc_free_sgl_list - Free sgl list.
4268 * @phba: pointer to lpfc hba data structure.
4269 *
4270 * This routine is invoked to free the driver's sgl list and memory.
4271 **/
4272static void
4273lpfc_free_sgl_list(struct lpfc_hba *phba)
4274{
4275 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4276 LIST_HEAD(sglq_list);
4277 int rc = 0;
4278
4279 spin_lock_irq(&phba->hbalock);
4280 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4281 spin_unlock_irq(&phba->hbalock);
4282
4283 list_for_each_entry_safe(sglq_entry, sglq_next,
4284 &sglq_list, list) {
4285 list_del(&sglq_entry->list);
4286 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4287 kfree(sglq_entry);
4288 phba->sli4_hba.total_sglq_bufs--;
4289 }
4290 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4291 if (rc) {
4292 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004293 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004294 }
4295 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4296}
4297
4298/**
4299 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4300 * @phba: pointer to lpfc hba data structure.
4301 *
4302 * This routine is invoked to allocate the driver's active sgl memory.
4303 * This array will hold the sglq_entry's for active IOs.
4304 **/
4305static int
4306lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4307{
4308 int size;
4309 size = sizeof(struct lpfc_sglq *);
4310 size *= phba->sli4_hba.max_cfg_param.max_xri;
4311
4312 phba->sli4_hba.lpfc_sglq_active_list =
4313 kzalloc(size, GFP_KERNEL);
4314 if (!phba->sli4_hba.lpfc_sglq_active_list)
4315 return -ENOMEM;
4316 return 0;
4317}
4318
4319/**
4320 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4321 * @phba: pointer to lpfc hba data structure.
4322 *
4323 * This routine is invoked to walk through the array of active sglq entries
4324 * and free all of the resources.
4325 * This is just a place holder for now.
4326 **/
4327static void
4328lpfc_free_active_sgl(struct lpfc_hba *phba)
4329{
4330 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4331}
4332
4333/**
4334 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4335 * @phba: pointer to lpfc hba data structure.
4336 *
4337 * This routine is invoked to allocate and initizlize the driver's sgl
4338 * list and set up the sgl xritag tag array accordingly.
4339 *
4340 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004341 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004342 * other values - error
4343 **/
4344static int
4345lpfc_init_sgl_list(struct lpfc_hba *phba)
4346{
4347 struct lpfc_sglq *sglq_entry = NULL;
4348 int i;
4349 int els_xri_cnt;
4350
4351 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4352 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4353 "2400 lpfc_init_sgl_list els %d.\n",
4354 els_xri_cnt);
4355 /* Initialize and populate the sglq list per host/VF. */
4356 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4357 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4358
4359 /* Sanity check on XRI management */
4360 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4361 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4362 "2562 No room left for SCSI XRI allocation: "
4363 "max_xri=%d, els_xri=%d\n",
4364 phba->sli4_hba.max_cfg_param.max_xri,
4365 els_xri_cnt);
4366 return -ENOMEM;
4367 }
4368
4369 /* Allocate memory for the ELS XRI management array */
4370 phba->sli4_hba.lpfc_els_sgl_array =
4371 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4372 GFP_KERNEL);
4373
4374 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4375 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4376 "2401 Failed to allocate memory for ELS "
4377 "XRI management array of size %d.\n",
4378 els_xri_cnt);
4379 return -ENOMEM;
4380 }
4381
4382 /* Keep the SCSI XRI into the XRI management array */
4383 phba->sli4_hba.scsi_xri_max =
4384 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4385 phba->sli4_hba.scsi_xri_cnt = 0;
4386
4387 phba->sli4_hba.lpfc_scsi_psb_array =
4388 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4389 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4390
4391 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4392 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4393 "2563 Failed to allocate memory for SCSI "
4394 "XRI management array of size %d.\n",
4395 phba->sli4_hba.scsi_xri_max);
4396 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4397 return -ENOMEM;
4398 }
4399
4400 for (i = 0; i < els_xri_cnt; i++) {
4401 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4402 if (sglq_entry == NULL) {
4403 printk(KERN_ERR "%s: only allocated %d sgls of "
4404 "expected %d count. Unloading driver.\n",
4405 __func__, i, els_xri_cnt);
4406 goto out_free_mem;
4407 }
4408
4409 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4410 if (sglq_entry->sli4_xritag == NO_XRI) {
4411 kfree(sglq_entry);
4412 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4413 "Unloading driver.\n", __func__);
4414 goto out_free_mem;
4415 }
4416 sglq_entry->buff_type = GEN_BUFF_TYPE;
4417 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4418 if (sglq_entry->virt == NULL) {
4419 kfree(sglq_entry);
4420 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4421 "Unloading driver.\n", __func__);
4422 goto out_free_mem;
4423 }
4424 sglq_entry->sgl = sglq_entry->virt;
4425 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4426
4427 /* The list order is used by later block SGL registraton */
4428 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05004429 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04004430 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4431 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4432 phba->sli4_hba.total_sglq_bufs++;
4433 spin_unlock_irq(&phba->hbalock);
4434 }
4435 return 0;
4436
4437out_free_mem:
4438 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4439 lpfc_free_sgl_list(phba);
4440 return -ENOMEM;
4441}
4442
4443/**
4444 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4445 * @phba: pointer to lpfc hba data structure.
4446 *
4447 * This routine is invoked to post rpi header templates to the
4448 * HBA consistent with the SLI-4 interface spec. This routine
4449 * posts a PAGE_SIZE memory region to the port to hold up to
4450 * PAGE_SIZE modulo 64 rpi context headers.
4451 * No locks are held here because this is an initialization routine
4452 * called only from probe or lpfc_online when interrupts are not
4453 * enabled and the driver is reinitializing the device.
4454 *
4455 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004456 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004457 * ENOMEM - No availble memory
4458 * EIO - The mailbox failed to complete successfully.
4459 **/
4460int
4461lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4462{
4463 int rc = 0;
4464 int longs;
4465 uint16_t rpi_count;
4466 struct lpfc_rpi_hdr *rpi_hdr;
4467
4468 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4469
4470 /*
4471 * Provision an rpi bitmask range for discovery. The total count
4472 * is the difference between max and base + 1.
4473 */
4474 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4475 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4476
4477 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4478 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4479 GFP_KERNEL);
4480 if (!phba->sli4_hba.rpi_bmask)
4481 return -ENOMEM;
4482
4483 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4484 if (!rpi_hdr) {
4485 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4486 "0391 Error during rpi post operation\n");
4487 lpfc_sli4_remove_rpis(phba);
4488 rc = -ENODEV;
4489 }
4490
4491 return rc;
4492}
4493
4494/**
4495 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4496 * @phba: pointer to lpfc hba data structure.
4497 *
4498 * This routine is invoked to allocate a single 4KB memory region to
4499 * support rpis and stores them in the phba. This single region
4500 * provides support for up to 64 rpis. The region is used globally
4501 * by the device.
4502 *
4503 * Returns:
4504 * A valid rpi hdr on success.
4505 * A NULL pointer on any failure.
4506 **/
4507struct lpfc_rpi_hdr *
4508lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4509{
4510 uint16_t rpi_limit, curr_rpi_range;
4511 struct lpfc_dmabuf *dmabuf;
4512 struct lpfc_rpi_hdr *rpi_hdr;
4513
4514 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4515 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4516
4517 spin_lock_irq(&phba->hbalock);
4518 curr_rpi_range = phba->sli4_hba.next_rpi;
4519 spin_unlock_irq(&phba->hbalock);
4520
4521 /*
4522 * The port has a limited number of rpis. The increment here
4523 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4524 * and to allow the full max_rpi range per port.
4525 */
4526 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4527 return NULL;
4528
4529 /*
4530 * First allocate the protocol header region for the port. The
4531 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4532 */
4533 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4534 if (!dmabuf)
4535 return NULL;
4536
4537 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4538 LPFC_HDR_TEMPLATE_SIZE,
4539 &dmabuf->phys,
4540 GFP_KERNEL);
4541 if (!dmabuf->virt) {
4542 rpi_hdr = NULL;
4543 goto err_free_dmabuf;
4544 }
4545
4546 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4547 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4548 rpi_hdr = NULL;
4549 goto err_free_coherent;
4550 }
4551
4552 /* Save the rpi header data for cleanup later. */
4553 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4554 if (!rpi_hdr)
4555 goto err_free_coherent;
4556
4557 rpi_hdr->dmabuf = dmabuf;
4558 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4559 rpi_hdr->page_count = 1;
4560 spin_lock_irq(&phba->hbalock);
4561 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4562 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4563
4564 /*
4565 * The next_rpi stores the next module-64 rpi value to post
4566 * in any subsequent rpi memory region postings.
4567 */
4568 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4569 spin_unlock_irq(&phba->hbalock);
4570 return rpi_hdr;
4571
4572 err_free_coherent:
4573 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4574 dmabuf->virt, dmabuf->phys);
4575 err_free_dmabuf:
4576 kfree(dmabuf);
4577 return NULL;
4578}
4579
4580/**
4581 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4582 * @phba: pointer to lpfc hba data structure.
4583 *
4584 * This routine is invoked to remove all memory resources allocated
4585 * to support rpis. This routine presumes the caller has released all
4586 * rpis consumed by fabric or port logins and is prepared to have
4587 * the header pages removed.
4588 **/
4589void
4590lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4591{
4592 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4593
4594 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4595 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4596 list_del(&rpi_hdr->list);
4597 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4598 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4599 kfree(rpi_hdr->dmabuf);
4600 kfree(rpi_hdr);
4601 }
4602
4603 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4604 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4605}
4606
4607/**
James Smart3772a992009-05-22 14:50:54 -04004608 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4609 * @pdev: pointer to pci device data structure.
4610 *
4611 * This routine is invoked to allocate the driver hba data structure for an
4612 * HBA device. If the allocation is successful, the phba reference to the
4613 * PCI device data structure is set.
4614 *
4615 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004616 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004617 * NULL - error
4618 **/
4619static struct lpfc_hba *
4620lpfc_hba_alloc(struct pci_dev *pdev)
4621{
4622 struct lpfc_hba *phba;
4623
4624 /* Allocate memory for HBA structure */
4625 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4626 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004627 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004628 return NULL;
4629 }
4630
4631 /* Set reference to PCI device in HBA structure */
4632 phba->pcidev = pdev;
4633
4634 /* Assign an unused board number */
4635 phba->brd_no = lpfc_get_instance();
4636 if (phba->brd_no < 0) {
4637 kfree(phba);
4638 return NULL;
4639 }
4640
James Smart4fede782010-01-26 23:08:55 -05004641 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04004642 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4643
James Smart3772a992009-05-22 14:50:54 -04004644 return phba;
4645}
4646
4647/**
4648 * lpfc_hba_free - Free driver hba data structure with a device.
4649 * @phba: pointer to lpfc hba data structure.
4650 *
4651 * This routine is invoked to free the driver hba data structure with an
4652 * HBA device.
4653 **/
4654static void
4655lpfc_hba_free(struct lpfc_hba *phba)
4656{
4657 /* Release the driver assigned board number */
4658 idr_remove(&lpfc_hba_index, phba->brd_no);
4659
4660 kfree(phba);
4661 return;
4662}
4663
4664/**
4665 * lpfc_create_shost - Create hba physical port with associated scsi host.
4666 * @phba: pointer to lpfc hba data structure.
4667 *
4668 * This routine is invoked to create HBA physical port and associate a SCSI
4669 * host with it.
4670 *
4671 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004672 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004673 * other values - error
4674 **/
4675static int
4676lpfc_create_shost(struct lpfc_hba *phba)
4677{
4678 struct lpfc_vport *vport;
4679 struct Scsi_Host *shost;
4680
4681 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004682 phba->fc_edtov = FF_DEF_EDTOV;
4683 phba->fc_ratov = FF_DEF_RATOV;
4684 phba->fc_altov = FF_DEF_ALTOV;
4685 phba->fc_arbtov = FF_DEF_ARBTOV;
4686
James Smart3de2a652007-08-02 11:09:59 -04004687 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004688 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004689 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004690
4691 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004692 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004693 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004694 /* Put reference to SCSI host to driver's device private data */
4695 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004696
James Smart3772a992009-05-22 14:50:54 -04004697 return 0;
4698}
dea31012005-04-17 16:05:31 -05004699
James Smart3772a992009-05-22 14:50:54 -04004700/**
4701 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4702 * @phba: pointer to lpfc hba data structure.
4703 *
4704 * This routine is invoked to destroy HBA physical port and the associated
4705 * SCSI host.
4706 **/
4707static void
4708lpfc_destroy_shost(struct lpfc_hba *phba)
4709{
4710 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004711
James Smart3772a992009-05-22 14:50:54 -04004712 /* Destroy physical port that associated with the SCSI host */
4713 destroy_port(vport);
4714
4715 return;
4716}
4717
4718/**
4719 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4720 * @phba: pointer to lpfc hba data structure.
4721 * @shost: the shost to be used to detect Block guard settings.
4722 *
4723 * This routine sets up the local Block guard protocol settings for @shost.
4724 * This routine also allocates memory for debugging bg buffers.
4725 **/
4726static void
4727lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4728{
4729 int pagecnt = 10;
4730 if (lpfc_prot_mask && lpfc_prot_guard) {
4731 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4732 "1478 Registering BlockGuard with the "
4733 "SCSI layer\n");
4734 scsi_host_set_prot(shost, lpfc_prot_mask);
4735 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004736 }
James Smart81301a92008-12-04 22:39:46 -05004737 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004738 while (pagecnt) {
4739 spin_lock_init(&_dump_buf_lock);
4740 _dump_buf_data =
4741 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4742 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004743 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4744 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004745 "_dump_buf_data at 0x%p\n",
4746 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004747 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004748 memset(_dump_buf_data, 0,
4749 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004750 break;
James Smart3772a992009-05-22 14:50:54 -04004751 } else
James Smart81301a92008-12-04 22:39:46 -05004752 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004753 }
James Smart81301a92008-12-04 22:39:46 -05004754 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004755 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4756 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004757 "memory for hexdump\n");
4758 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004759 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4760 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004761 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004762 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004763 while (pagecnt) {
4764 _dump_buf_dif =
4765 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4766 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004767 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4768 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004769 "_dump_buf_dif at 0x%p\n",
4770 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004771 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004772 memset(_dump_buf_dif, 0,
4773 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004774 break;
James Smart3772a992009-05-22 14:50:54 -04004775 } else
James Smart81301a92008-12-04 22:39:46 -05004776 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004777 }
James Smart81301a92008-12-04 22:39:46 -05004778 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004779 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4780 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004781 "memory for hexdump\n");
4782 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004783 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4784 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004785 _dump_buf_dif);
4786}
4787
4788/**
4789 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4790 * @phba: pointer to lpfc hba data structure.
4791 *
4792 * This routine is invoked to perform all the necessary post initialization
4793 * setup for the device.
4794 **/
4795static void
4796lpfc_post_init_setup(struct lpfc_hba *phba)
4797{
4798 struct Scsi_Host *shost;
4799 struct lpfc_adapter_event_header adapter_event;
4800
4801 /* Get the default values for Model Name and Description */
4802 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4803
4804 /*
4805 * hba setup may have changed the hba_queue_depth so we need to
4806 * adjust the value of can_queue.
4807 */
4808 shost = pci_get_drvdata(phba->pcidev);
4809 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4810 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4811 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004812
4813 lpfc_host_attrib_init(shost);
4814
James Smart2e0fef82007-06-17 19:56:36 -05004815 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4816 spin_lock_irq(shost->host_lock);
4817 lpfc_poll_start_timer(phba);
4818 spin_unlock_irq(shost->host_lock);
4819 }
James Smart8f6d98d2006-08-01 07:34:00 -04004820
James Smart93996272008-08-24 21:50:30 -04004821 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4822 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004823 /* Send board arrival event to upper layer */
4824 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4825 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4826 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004827 sizeof(adapter_event),
4828 (char *) &adapter_event,
4829 LPFC_NL_VENDOR_ID);
4830 return;
4831}
4832
4833/**
4834 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4835 * @phba: pointer to lpfc hba data structure.
4836 *
4837 * This routine is invoked to set up the PCI device memory space for device
4838 * with SLI-3 interface spec.
4839 *
4840 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004841 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004842 * other values - error
4843 **/
4844static int
4845lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4846{
4847 struct pci_dev *pdev;
4848 unsigned long bar0map_len, bar2map_len;
4849 int i, hbq_count;
4850 void *ptr;
4851 int error = -ENODEV;
4852
4853 /* Obtain PCI device reference */
4854 if (!phba->pcidev)
4855 return error;
4856 else
4857 pdev = phba->pcidev;
4858
4859 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05004860 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
4861 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
4862 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
4863 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04004864 return error;
Michael Reed8e685972009-09-18 12:02:05 -05004865 }
4866 }
James Smart3772a992009-05-22 14:50:54 -04004867
4868 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4869 * required by each mapping.
4870 */
4871 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4872 bar0map_len = pci_resource_len(pdev, 0);
4873
4874 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4875 bar2map_len = pci_resource_len(pdev, 2);
4876
4877 /* Map HBA SLIM to a kernel virtual address. */
4878 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4879 if (!phba->slim_memmap_p) {
4880 dev_printk(KERN_ERR, &pdev->dev,
4881 "ioremap failed for SLIM memory.\n");
4882 goto out;
4883 }
4884
4885 /* Map HBA Control Registers to a kernel virtual address. */
4886 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4887 if (!phba->ctrl_regs_memmap_p) {
4888 dev_printk(KERN_ERR, &pdev->dev,
4889 "ioremap failed for HBA control registers.\n");
4890 goto out_iounmap_slim;
4891 }
4892
4893 /* Allocate memory for SLI-2 structures */
4894 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4895 SLI2_SLIM_SIZE,
4896 &phba->slim2p.phys,
4897 GFP_KERNEL);
4898 if (!phba->slim2p.virt)
4899 goto out_iounmap;
4900
4901 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4902 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4903 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4904 phba->IOCBs = (phba->slim2p.virt +
4905 offsetof(struct lpfc_sli2_slim, IOCBs));
4906
4907 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4908 lpfc_sli_hbq_size(),
4909 &phba->hbqslimp.phys,
4910 GFP_KERNEL);
4911 if (!phba->hbqslimp.virt)
4912 goto out_free_slim;
4913
4914 hbq_count = lpfc_sli_hbq_count();
4915 ptr = phba->hbqslimp.virt;
4916 for (i = 0; i < hbq_count; ++i) {
4917 phba->hbqs[i].hbq_virt = ptr;
4918 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4919 ptr += (lpfc_hbq_defs[i]->entry_count *
4920 sizeof(struct lpfc_hbq_entry));
4921 }
4922 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4923 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4924
4925 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4926
4927 INIT_LIST_HEAD(&phba->rb_pend_list);
4928
4929 phba->MBslimaddr = phba->slim_memmap_p;
4930 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4931 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4932 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4933 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004934
dea31012005-04-17 16:05:31 -05004935 return 0;
4936
dea31012005-04-17 16:05:31 -05004937out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004938 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4939 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004940out_iounmap:
4941 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004942out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004943 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004944out:
4945 return error;
4946}
4947
James Smarte59058c2008-08-24 21:49:00 -04004948/**
James Smart3772a992009-05-22 14:50:54 -04004949 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4950 * @phba: pointer to lpfc hba data structure.
4951 *
4952 * This routine is invoked to unset the PCI device memory space for device
4953 * with SLI-3 interface spec.
4954 **/
4955static void
4956lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4957{
4958 struct pci_dev *pdev;
4959
4960 /* Obtain PCI device reference */
4961 if (!phba->pcidev)
4962 return;
4963 else
4964 pdev = phba->pcidev;
4965
4966 /* Free coherent DMA memory allocated */
4967 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4968 phba->hbqslimp.virt, phba->hbqslimp.phys);
4969 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4970 phba->slim2p.virt, phba->slim2p.phys);
4971
4972 /* I/O memory unmap */
4973 iounmap(phba->ctrl_regs_memmap_p);
4974 iounmap(phba->slim_memmap_p);
4975
4976 return;
4977}
4978
4979/**
James Smartda0436e2009-05-22 14:51:39 -04004980 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4981 * @phba: pointer to lpfc hba data structure.
4982 *
4983 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4984 * done and check status.
4985 *
4986 * Return 0 if successful, otherwise -ENODEV.
4987 **/
4988int
4989lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4990{
James Smart28baac72010-02-12 14:42:03 -05004991 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
James Smartda0436e2009-05-22 14:51:39 -04004992 int i, port_error = -ENODEV;
4993
4994 if (!phba->sli4_hba.STAregaddr)
4995 return -ENODEV;
4996
James Smartda0436e2009-05-22 14:51:39 -04004997 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4998 for (i = 0; i < 3000; i++) {
4999 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
5000 /* Encounter fatal POST error, break out */
5001 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
5002 port_error = -ENODEV;
5003 break;
5004 }
5005 if (LPFC_POST_STAGE_ARMFW_READY ==
5006 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
5007 port_error = 0;
5008 break;
5009 }
5010 msleep(10);
5011 }
5012
5013 if (port_error)
5014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5015 "1408 Failure HBA POST Status: sta_reg=0x%x, "
5016 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5017 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5018 bf_get(lpfc_hst_state_perr, &sta_reg),
5019 bf_get(lpfc_hst_state_sfi, &sta_reg),
5020 bf_get(lpfc_hst_state_nip, &sta_reg),
5021 bf_get(lpfc_hst_state_ipc, &sta_reg),
5022 bf_get(lpfc_hst_state_xrom, &sta_reg),
5023 bf_get(lpfc_hst_state_dl, &sta_reg),
5024 bf_get(lpfc_hst_state_port_status, &sta_reg));
5025
5026 /* Log device information */
James Smart28baac72010-02-12 14:42:03 -05005027 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5028 if (bf_get(lpfc_sli_intf_valid,
5029 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5030 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5031 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5032 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5033 bf_get(lpfc_sli_intf_sli_family,
5034 &phba->sli4_hba.sli_intf),
5035 bf_get(lpfc_sli_intf_slirev,
5036 &phba->sli4_hba.sli_intf),
5037 bf_get(lpfc_sli_intf_featurelevel1,
5038 &phba->sli4_hba.sli_intf),
5039 bf_get(lpfc_sli_intf_featurelevel2,
5040 &phba->sli4_hba.sli_intf));
5041 }
James Smarta747c9c2009-11-18 15:41:10 -05005042 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5043 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04005044 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05005045 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5046 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5047 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5048 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5050 "1422 HBA Unrecoverable error: "
5051 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5052 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5053 uerrlo_reg.word0, uerrhi_reg.word0,
5054 phba->sli4_hba.ue_mask_lo,
5055 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04005056 return -ENODEV;
5057 }
5058
James Smartda0436e2009-05-22 14:51:39 -04005059 return port_error;
5060}
5061
5062/**
5063 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5064 * @phba: pointer to lpfc hba data structure.
5065 *
5066 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5067 * memory map.
5068 **/
5069static void
5070lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5071{
5072 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5073 LPFC_UERR_STATUS_LO;
5074 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5075 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05005076 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5077 LPFC_UE_MASK_LO;
5078 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5079 LPFC_UE_MASK_HI;
James Smart28baac72010-02-12 14:42:03 -05005080 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5081 LPFC_SLI_INTF;
James Smartda0436e2009-05-22 14:51:39 -04005082}
5083
5084/**
5085 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5086 * @phba: pointer to lpfc hba data structure.
5087 *
5088 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5089 * memory map.
5090 **/
5091static void
5092lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5093{
5094
5095 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5096 LPFC_HST_STATE;
5097 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5098 LPFC_HST_ISR0;
5099 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5100 LPFC_HST_IMR0;
5101 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5102 LPFC_HST_ISCR0;
5103 return;
5104}
5105
5106/**
5107 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5108 * @phba: pointer to lpfc hba data structure.
5109 * @vf: virtual function number
5110 *
5111 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5112 * based on the given viftual function number, @vf.
5113 *
5114 * Return 0 if successful, otherwise -ENODEV.
5115 **/
5116static int
5117lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5118{
5119 if (vf > LPFC_VIR_FUNC_MAX)
5120 return -ENODEV;
5121
5122 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5123 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5124 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5125 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5126 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5127 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5128 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5129 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5130 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5131 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5132 return 0;
5133}
5134
5135/**
5136 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5137 * @phba: pointer to lpfc hba data structure.
5138 *
5139 * This routine is invoked to create the bootstrap mailbox
5140 * region consistent with the SLI-4 interface spec. This
5141 * routine allocates all memory necessary to communicate
5142 * mailbox commands to the port and sets up all alignment
5143 * needs. No locks are expected to be held when calling
5144 * this routine.
5145 *
5146 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005147 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005148 * ENOMEM - could not allocated memory.
5149 **/
5150static int
5151lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5152{
5153 uint32_t bmbx_size;
5154 struct lpfc_dmabuf *dmabuf;
5155 struct dma_address *dma_address;
5156 uint32_t pa_addr;
5157 uint64_t phys_addr;
5158
5159 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5160 if (!dmabuf)
5161 return -ENOMEM;
5162
5163 /*
5164 * The bootstrap mailbox region is comprised of 2 parts
5165 * plus an alignment restriction of 16 bytes.
5166 */
5167 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5168 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5169 bmbx_size,
5170 &dmabuf->phys,
5171 GFP_KERNEL);
5172 if (!dmabuf->virt) {
5173 kfree(dmabuf);
5174 return -ENOMEM;
5175 }
5176 memset(dmabuf->virt, 0, bmbx_size);
5177
5178 /*
5179 * Initialize the bootstrap mailbox pointers now so that the register
5180 * operations are simple later. The mailbox dma address is required
5181 * to be 16-byte aligned. Also align the virtual memory as each
5182 * maibox is copied into the bmbx mailbox region before issuing the
5183 * command to the port.
5184 */
5185 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5186 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5187
5188 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5189 LPFC_ALIGN_16_BYTE);
5190 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5191 LPFC_ALIGN_16_BYTE);
5192
5193 /*
5194 * Set the high and low physical addresses now. The SLI4 alignment
5195 * requirement is 16 bytes and the mailbox is posted to the port
5196 * as two 30-bit addresses. The other data is a bit marking whether
5197 * the 30-bit address is the high or low address.
5198 * Upcast bmbx aphys to 64bits so shift instruction compiles
5199 * clean on 32 bit machines.
5200 */
5201 dma_address = &phba->sli4_hba.bmbx.dma_address;
5202 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5203 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5204 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5205 LPFC_BMBX_BIT1_ADDR_HI);
5206
5207 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5208 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5209 LPFC_BMBX_BIT1_ADDR_LO);
5210 return 0;
5211}
5212
5213/**
5214 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5215 * @phba: pointer to lpfc hba data structure.
5216 *
5217 * This routine is invoked to teardown the bootstrap mailbox
5218 * region and release all host resources. This routine requires
5219 * the caller to ensure all mailbox commands recovered, no
5220 * additional mailbox comands are sent, and interrupts are disabled
5221 * before calling this routine.
5222 *
5223 **/
5224static void
5225lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5226{
5227 dma_free_coherent(&phba->pcidev->dev,
5228 phba->sli4_hba.bmbx.bmbx_size,
5229 phba->sli4_hba.bmbx.dmabuf->virt,
5230 phba->sli4_hba.bmbx.dmabuf->phys);
5231
5232 kfree(phba->sli4_hba.bmbx.dmabuf);
5233 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5234}
5235
5236/**
5237 * lpfc_sli4_read_config - Get the config parameters.
5238 * @phba: pointer to lpfc hba data structure.
5239 *
5240 * This routine is invoked to read the configuration parameters from the HBA.
5241 * The configuration parameters are used to set the base and maximum values
5242 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5243 * allocation for the port.
5244 *
5245 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005246 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005247 * ENOMEM - No availble memory
5248 * EIO - The mailbox failed to complete successfully.
5249 **/
5250static int
5251lpfc_sli4_read_config(struct lpfc_hba *phba)
5252{
5253 LPFC_MBOXQ_t *pmb;
5254 struct lpfc_mbx_read_config *rd_config;
5255 uint32_t rc = 0;
5256
5257 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5258 if (!pmb) {
5259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5260 "2011 Unable to allocate memory for issuing "
5261 "SLI_CONFIG_SPECIAL mailbox command\n");
5262 return -ENOMEM;
5263 }
5264
5265 lpfc_read_config(phba, pmb);
5266
5267 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5268 if (rc != MBX_SUCCESS) {
5269 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5270 "2012 Mailbox failed , mbxCmd x%x "
5271 "READ_CONFIG, mbxStatus x%x\n",
5272 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5273 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5274 rc = -EIO;
5275 } else {
5276 rd_config = &pmb->u.mqe.un.rd_config;
5277 phba->sli4_hba.max_cfg_param.max_xri =
5278 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5279 phba->sli4_hba.max_cfg_param.xri_base =
5280 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5281 phba->sli4_hba.max_cfg_param.max_vpi =
5282 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5283 phba->sli4_hba.max_cfg_param.vpi_base =
5284 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5285 phba->sli4_hba.max_cfg_param.max_rpi =
5286 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5287 phba->sli4_hba.max_cfg_param.rpi_base =
5288 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5289 phba->sli4_hba.max_cfg_param.max_vfi =
5290 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5291 phba->sli4_hba.max_cfg_param.vfi_base =
5292 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5293 phba->sli4_hba.max_cfg_param.max_fcfi =
5294 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5295 phba->sli4_hba.max_cfg_param.fcfi_base =
5296 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5297 phba->sli4_hba.max_cfg_param.max_eq =
5298 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5299 phba->sli4_hba.max_cfg_param.max_rq =
5300 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5301 phba->sli4_hba.max_cfg_param.max_wq =
5302 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5303 phba->sli4_hba.max_cfg_param.max_cq =
5304 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5305 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5306 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5307 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5308 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5309 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005310 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5311 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005312 phba->max_vports = phba->max_vpi;
5313 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5314 "2003 cfg params XRI(B:%d M:%d), "
5315 "VPI(B:%d M:%d) "
5316 "VFI(B:%d M:%d) "
5317 "RPI(B:%d M:%d) "
5318 "FCFI(B:%d M:%d)\n",
5319 phba->sli4_hba.max_cfg_param.xri_base,
5320 phba->sli4_hba.max_cfg_param.max_xri,
5321 phba->sli4_hba.max_cfg_param.vpi_base,
5322 phba->sli4_hba.max_cfg_param.max_vpi,
5323 phba->sli4_hba.max_cfg_param.vfi_base,
5324 phba->sli4_hba.max_cfg_param.max_vfi,
5325 phba->sli4_hba.max_cfg_param.rpi_base,
5326 phba->sli4_hba.max_cfg_param.max_rpi,
5327 phba->sli4_hba.max_cfg_param.fcfi_base,
5328 phba->sli4_hba.max_cfg_param.max_fcfi);
5329 }
5330 mempool_free(pmb, phba->mbox_mem_pool);
5331
5332 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5333 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5334 phba->cfg_hba_queue_depth =
5335 phba->sli4_hba.max_cfg_param.max_xri;
5336 return rc;
5337}
5338
5339/**
5340 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5341 * @phba: pointer to lpfc hba data structure.
5342 *
5343 * This routine is invoked to setup the host-side endian order to the
5344 * HBA consistent with the SLI-4 interface spec.
5345 *
5346 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005347 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005348 * ENOMEM - No availble memory
5349 * EIO - The mailbox failed to complete successfully.
5350 **/
5351static int
5352lpfc_setup_endian_order(struct lpfc_hba *phba)
5353{
5354 LPFC_MBOXQ_t *mboxq;
5355 uint32_t rc = 0;
5356 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5357 HOST_ENDIAN_HIGH_WORD1};
5358
5359 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5360 if (!mboxq) {
5361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5362 "0492 Unable to allocate memory for issuing "
5363 "SLI_CONFIG_SPECIAL mailbox command\n");
5364 return -ENOMEM;
5365 }
5366
5367 /*
5368 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5369 * words to contain special data values and no other data.
5370 */
5371 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5372 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5373 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5374 if (rc != MBX_SUCCESS) {
5375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5376 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5377 "status x%x\n",
5378 rc);
5379 rc = -EIO;
5380 }
5381
5382 mempool_free(mboxq, phba->mbox_mem_pool);
5383 return rc;
5384}
5385
5386/**
5387 * lpfc_sli4_queue_create - Create all the SLI4 queues
5388 * @phba: pointer to lpfc hba data structure.
5389 *
5390 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5391 * operation. For each SLI4 queue type, the parameters such as queue entry
5392 * count (queue depth) shall be taken from the module parameter. For now,
5393 * we just use some constant number as place holder.
5394 *
5395 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005396 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005397 * ENOMEM - No availble memory
5398 * EIO - The mailbox failed to complete successfully.
5399 **/
5400static int
5401lpfc_sli4_queue_create(struct lpfc_hba *phba)
5402{
5403 struct lpfc_queue *qdesc;
5404 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5405 int cfg_fcp_wq_count;
5406 int cfg_fcp_eq_count;
5407
5408 /*
5409 * Sanity check for confiugred queue parameters against the run-time
5410 * device parameters
5411 */
5412
5413 /* Sanity check on FCP fast-path WQ parameters */
5414 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5415 if (cfg_fcp_wq_count >
5416 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5417 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5418 LPFC_SP_WQN_DEF;
5419 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5421 "2581 Not enough WQs (%d) from "
5422 "the pci function for supporting "
5423 "FCP WQs (%d)\n",
5424 phba->sli4_hba.max_cfg_param.max_wq,
5425 phba->cfg_fcp_wq_count);
5426 goto out_error;
5427 }
5428 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5429 "2582 Not enough WQs (%d) from the pci "
5430 "function for supporting the requested "
5431 "FCP WQs (%d), the actual FCP WQs can "
5432 "be supported: %d\n",
5433 phba->sli4_hba.max_cfg_param.max_wq,
5434 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5435 }
5436 /* The actual number of FCP work queues adopted */
5437 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5438
5439 /* Sanity check on FCP fast-path EQ parameters */
5440 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5441 if (cfg_fcp_eq_count >
5442 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5443 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5444 LPFC_SP_EQN_DEF;
5445 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5447 "2574 Not enough EQs (%d) from the "
5448 "pci function for supporting FCP "
5449 "EQs (%d)\n",
5450 phba->sli4_hba.max_cfg_param.max_eq,
5451 phba->cfg_fcp_eq_count);
5452 goto out_error;
5453 }
5454 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5455 "2575 Not enough EQs (%d) from the pci "
5456 "function for supporting the requested "
5457 "FCP EQs (%d), the actual FCP EQs can "
5458 "be supported: %d\n",
5459 phba->sli4_hba.max_cfg_param.max_eq,
5460 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5461 }
5462 /* It does not make sense to have more EQs than WQs */
5463 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5464 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005465 "2593 The FCP EQ count(%d) cannot be greater "
5466 "than the FCP WQ count(%d), limiting the "
5467 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005468 phba->cfg_fcp_wq_count,
5469 phba->cfg_fcp_wq_count);
5470 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5471 }
5472 /* The actual number of FCP event queues adopted */
5473 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5474 /* The overall number of event queues used */
5475 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5476
5477 /*
5478 * Create Event Queues (EQs)
5479 */
5480
5481 /* Get EQ depth from module parameter, fake the default for now */
5482 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5483 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5484
5485 /* Create slow path event queue */
5486 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5487 phba->sli4_hba.eq_ecount);
5488 if (!qdesc) {
5489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5490 "0496 Failed allocate slow-path EQ\n");
5491 goto out_error;
5492 }
5493 phba->sli4_hba.sp_eq = qdesc;
5494
5495 /* Create fast-path FCP Event Queue(s) */
5496 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5497 phba->cfg_fcp_eq_count), GFP_KERNEL);
5498 if (!phba->sli4_hba.fp_eq) {
5499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5500 "2576 Failed allocate memory for fast-path "
5501 "EQ record array\n");
5502 goto out_free_sp_eq;
5503 }
5504 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5505 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5506 phba->sli4_hba.eq_ecount);
5507 if (!qdesc) {
5508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5509 "0497 Failed allocate fast-path EQ\n");
5510 goto out_free_fp_eq;
5511 }
5512 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5513 }
5514
5515 /*
5516 * Create Complete Queues (CQs)
5517 */
5518
5519 /* Get CQ depth from module parameter, fake the default for now */
5520 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5521 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5522
5523 /* Create slow-path Mailbox Command Complete Queue */
5524 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5525 phba->sli4_hba.cq_ecount);
5526 if (!qdesc) {
5527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5528 "0500 Failed allocate slow-path mailbox CQ\n");
5529 goto out_free_fp_eq;
5530 }
5531 phba->sli4_hba.mbx_cq = qdesc;
5532
5533 /* Create slow-path ELS Complete Queue */
5534 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5535 phba->sli4_hba.cq_ecount);
5536 if (!qdesc) {
5537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5538 "0501 Failed allocate slow-path ELS CQ\n");
5539 goto out_free_mbx_cq;
5540 }
5541 phba->sli4_hba.els_cq = qdesc;
5542
James Smartda0436e2009-05-22 14:51:39 -04005543
5544 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5545 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5546 phba->cfg_fcp_eq_count), GFP_KERNEL);
5547 if (!phba->sli4_hba.fcp_cq) {
5548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5549 "2577 Failed allocate memory for fast-path "
5550 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005551 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005552 }
5553 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5554 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5555 phba->sli4_hba.cq_ecount);
5556 if (!qdesc) {
5557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5558 "0499 Failed allocate fast-path FCP "
5559 "CQ (%d)\n", fcp_cqidx);
5560 goto out_free_fcp_cq;
5561 }
5562 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5563 }
5564
5565 /* Create Mailbox Command Queue */
5566 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5567 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5568
5569 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5570 phba->sli4_hba.mq_ecount);
5571 if (!qdesc) {
5572 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5573 "0505 Failed allocate slow-path MQ\n");
5574 goto out_free_fcp_cq;
5575 }
5576 phba->sli4_hba.mbx_wq = qdesc;
5577
5578 /*
5579 * Create all the Work Queues (WQs)
5580 */
5581 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5582 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5583
5584 /* Create slow-path ELS Work Queue */
5585 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5586 phba->sli4_hba.wq_ecount);
5587 if (!qdesc) {
5588 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5589 "0504 Failed allocate slow-path ELS WQ\n");
5590 goto out_free_mbx_wq;
5591 }
5592 phba->sli4_hba.els_wq = qdesc;
5593
5594 /* Create fast-path FCP Work Queue(s) */
5595 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5596 phba->cfg_fcp_wq_count), GFP_KERNEL);
5597 if (!phba->sli4_hba.fcp_wq) {
5598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5599 "2578 Failed allocate memory for fast-path "
5600 "WQ record array\n");
5601 goto out_free_els_wq;
5602 }
5603 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5604 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5605 phba->sli4_hba.wq_ecount);
5606 if (!qdesc) {
5607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5608 "0503 Failed allocate fast-path FCP "
5609 "WQ (%d)\n", fcp_wqidx);
5610 goto out_free_fcp_wq;
5611 }
5612 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5613 }
5614
5615 /*
5616 * Create Receive Queue (RQ)
5617 */
5618 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5619 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5620
5621 /* Create Receive Queue for header */
5622 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5623 phba->sli4_hba.rq_ecount);
5624 if (!qdesc) {
5625 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5626 "0506 Failed allocate receive HRQ\n");
5627 goto out_free_fcp_wq;
5628 }
5629 phba->sli4_hba.hdr_rq = qdesc;
5630
5631 /* Create Receive Queue for data */
5632 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5633 phba->sli4_hba.rq_ecount);
5634 if (!qdesc) {
5635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5636 "0507 Failed allocate receive DRQ\n");
5637 goto out_free_hdr_rq;
5638 }
5639 phba->sli4_hba.dat_rq = qdesc;
5640
5641 return 0;
5642
5643out_free_hdr_rq:
5644 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5645 phba->sli4_hba.hdr_rq = NULL;
5646out_free_fcp_wq:
5647 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5648 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5649 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5650 }
5651 kfree(phba->sli4_hba.fcp_wq);
5652out_free_els_wq:
5653 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5654 phba->sli4_hba.els_wq = NULL;
5655out_free_mbx_wq:
5656 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5657 phba->sli4_hba.mbx_wq = NULL;
5658out_free_fcp_cq:
5659 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5660 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5661 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5662 }
5663 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005664out_free_els_cq:
5665 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5666 phba->sli4_hba.els_cq = NULL;
5667out_free_mbx_cq:
5668 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5669 phba->sli4_hba.mbx_cq = NULL;
5670out_free_fp_eq:
5671 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5672 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5673 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5674 }
5675 kfree(phba->sli4_hba.fp_eq);
5676out_free_sp_eq:
5677 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5678 phba->sli4_hba.sp_eq = NULL;
5679out_error:
5680 return -ENOMEM;
5681}
5682
5683/**
5684 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5685 * @phba: pointer to lpfc hba data structure.
5686 *
5687 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5688 * operation.
5689 *
5690 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005691 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005692 * ENOMEM - No availble memory
5693 * EIO - The mailbox failed to complete successfully.
5694 **/
5695static void
5696lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5697{
5698 int fcp_qidx;
5699
5700 /* Release mailbox command work queue */
5701 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5702 phba->sli4_hba.mbx_wq = NULL;
5703
5704 /* Release ELS work queue */
5705 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5706 phba->sli4_hba.els_wq = NULL;
5707
5708 /* Release FCP work queue */
5709 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5710 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5711 kfree(phba->sli4_hba.fcp_wq);
5712 phba->sli4_hba.fcp_wq = NULL;
5713
5714 /* Release unsolicited receive queue */
5715 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5716 phba->sli4_hba.hdr_rq = NULL;
5717 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5718 phba->sli4_hba.dat_rq = NULL;
5719
James Smartda0436e2009-05-22 14:51:39 -04005720 /* Release ELS complete queue */
5721 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5722 phba->sli4_hba.els_cq = NULL;
5723
5724 /* Release mailbox command complete queue */
5725 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5726 phba->sli4_hba.mbx_cq = NULL;
5727
5728 /* Release FCP response complete queue */
5729 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5730 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5731 kfree(phba->sli4_hba.fcp_cq);
5732 phba->sli4_hba.fcp_cq = NULL;
5733
5734 /* Release fast-path event queue */
5735 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5736 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5737 kfree(phba->sli4_hba.fp_eq);
5738 phba->sli4_hba.fp_eq = NULL;
5739
5740 /* Release slow-path event queue */
5741 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5742 phba->sli4_hba.sp_eq = NULL;
5743
5744 return;
5745}
5746
5747/**
5748 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5749 * @phba: pointer to lpfc hba data structure.
5750 *
5751 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5752 * operation.
5753 *
5754 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005755 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005756 * ENOMEM - No availble memory
5757 * EIO - The mailbox failed to complete successfully.
5758 **/
5759int
5760lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5761{
5762 int rc = -ENOMEM;
5763 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5764 int fcp_cq_index = 0;
5765
5766 /*
5767 * Set up Event Queues (EQs)
5768 */
5769
5770 /* Set up slow-path event queue */
5771 if (!phba->sli4_hba.sp_eq) {
5772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5773 "0520 Slow-path EQ not allocated\n");
5774 goto out_error;
5775 }
5776 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5777 LPFC_SP_DEF_IMAX);
5778 if (rc) {
5779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5780 "0521 Failed setup of slow-path EQ: "
5781 "rc = 0x%x\n", rc);
5782 goto out_error;
5783 }
5784 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5785 "2583 Slow-path EQ setup: queue-id=%d\n",
5786 phba->sli4_hba.sp_eq->queue_id);
5787
5788 /* Set up fast-path event queue */
5789 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5790 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5792 "0522 Fast-path EQ (%d) not "
5793 "allocated\n", fcp_eqidx);
5794 goto out_destroy_fp_eq;
5795 }
5796 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5797 phba->cfg_fcp_imax);
5798 if (rc) {
5799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5800 "0523 Failed setup of fast-path EQ "
5801 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5802 goto out_destroy_fp_eq;
5803 }
5804 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5805 "2584 Fast-path EQ setup: "
5806 "queue[%d]-id=%d\n", fcp_eqidx,
5807 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5808 }
5809
5810 /*
5811 * Set up Complete Queues (CQs)
5812 */
5813
5814 /* Set up slow-path MBOX Complete Queue as the first CQ */
5815 if (!phba->sli4_hba.mbx_cq) {
5816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5817 "0528 Mailbox CQ not allocated\n");
5818 goto out_destroy_fp_eq;
5819 }
5820 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5821 LPFC_MCQ, LPFC_MBOX);
5822 if (rc) {
5823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5824 "0529 Failed setup of slow-path mailbox CQ: "
5825 "rc = 0x%x\n", rc);
5826 goto out_destroy_fp_eq;
5827 }
5828 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5829 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5830 phba->sli4_hba.mbx_cq->queue_id,
5831 phba->sli4_hba.sp_eq->queue_id);
5832
5833 /* Set up slow-path ELS Complete Queue */
5834 if (!phba->sli4_hba.els_cq) {
5835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5836 "0530 ELS CQ not allocated\n");
5837 goto out_destroy_mbx_cq;
5838 }
5839 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5840 LPFC_WCQ, LPFC_ELS);
5841 if (rc) {
5842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5843 "0531 Failed setup of slow-path ELS CQ: "
5844 "rc = 0x%x\n", rc);
5845 goto out_destroy_mbx_cq;
5846 }
5847 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5848 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5849 phba->sli4_hba.els_cq->queue_id,
5850 phba->sli4_hba.sp_eq->queue_id);
5851
James Smartda0436e2009-05-22 14:51:39 -04005852 /* Set up fast-path FCP Response Complete Queue */
5853 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5854 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5856 "0526 Fast-path FCP CQ (%d) not "
5857 "allocated\n", fcp_cqidx);
5858 goto out_destroy_fcp_cq;
5859 }
5860 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5861 phba->sli4_hba.fp_eq[fcp_cqidx],
5862 LPFC_WCQ, LPFC_FCP);
5863 if (rc) {
5864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5865 "0527 Failed setup of fast-path FCP "
5866 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5867 goto out_destroy_fcp_cq;
5868 }
5869 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5870 "2588 FCP CQ setup: cq[%d]-id=%d, "
5871 "parent eq[%d]-id=%d\n",
5872 fcp_cqidx,
5873 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5874 fcp_cqidx,
5875 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5876 }
5877
5878 /*
5879 * Set up all the Work Queues (WQs)
5880 */
5881
5882 /* Set up Mailbox Command Queue */
5883 if (!phba->sli4_hba.mbx_wq) {
5884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5885 "0538 Slow-path MQ not allocated\n");
5886 goto out_destroy_fcp_cq;
5887 }
5888 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5889 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5890 if (rc) {
5891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5892 "0539 Failed setup of slow-path MQ: "
5893 "rc = 0x%x\n", rc);
5894 goto out_destroy_fcp_cq;
5895 }
5896 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5897 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5898 phba->sli4_hba.mbx_wq->queue_id,
5899 phba->sli4_hba.mbx_cq->queue_id);
5900
5901 /* Set up slow-path ELS Work Queue */
5902 if (!phba->sli4_hba.els_wq) {
5903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5904 "0536 Slow-path ELS WQ not allocated\n");
5905 goto out_destroy_mbx_wq;
5906 }
5907 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5908 phba->sli4_hba.els_cq, LPFC_ELS);
5909 if (rc) {
5910 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5911 "0537 Failed setup of slow-path ELS WQ: "
5912 "rc = 0x%x\n", rc);
5913 goto out_destroy_mbx_wq;
5914 }
5915 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5916 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5917 phba->sli4_hba.els_wq->queue_id,
5918 phba->sli4_hba.els_cq->queue_id);
5919
5920 /* Set up fast-path FCP Work Queue */
5921 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5922 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5924 "0534 Fast-path FCP WQ (%d) not "
5925 "allocated\n", fcp_wqidx);
5926 goto out_destroy_fcp_wq;
5927 }
5928 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5929 phba->sli4_hba.fcp_cq[fcp_cq_index],
5930 LPFC_FCP);
5931 if (rc) {
5932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5933 "0535 Failed setup of fast-path FCP "
5934 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5935 goto out_destroy_fcp_wq;
5936 }
5937 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5938 "2591 FCP WQ setup: wq[%d]-id=%d, "
5939 "parent cq[%d]-id=%d\n",
5940 fcp_wqidx,
5941 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5942 fcp_cq_index,
5943 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5944 /* Round robin FCP Work Queue's Completion Queue assignment */
5945 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5946 }
5947
5948 /*
5949 * Create Receive Queue (RQ)
5950 */
5951 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5953 "0540 Receive Queue not allocated\n");
5954 goto out_destroy_fcp_wq;
5955 }
5956 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04005957 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04005958 if (rc) {
5959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5960 "0541 Failed setup of Receive Queue: "
5961 "rc = 0x%x\n", rc);
5962 goto out_destroy_fcp_wq;
5963 }
5964 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5965 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5966 "parent cq-id=%d\n",
5967 phba->sli4_hba.hdr_rq->queue_id,
5968 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04005969 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04005970 return 0;
5971
5972out_destroy_fcp_wq:
5973 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5974 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5975 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5976out_destroy_mbx_wq:
5977 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5978out_destroy_fcp_cq:
5979 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5980 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04005981 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5982out_destroy_mbx_cq:
5983 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5984out_destroy_fp_eq:
5985 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5986 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5987 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5988out_error:
5989 return rc;
5990}
5991
5992/**
5993 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5994 * @phba: pointer to lpfc hba data structure.
5995 *
5996 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5997 * operation.
5998 *
5999 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006000 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006001 * ENOMEM - No availble memory
6002 * EIO - The mailbox failed to complete successfully.
6003 **/
6004void
6005lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6006{
6007 int fcp_qidx;
6008
6009 /* Unset mailbox command work queue */
6010 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6011 /* Unset ELS work queue */
6012 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6013 /* Unset unsolicited receive queue */
6014 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6015 /* Unset FCP work queue */
6016 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6017 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6018 /* Unset mailbox command complete queue */
6019 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6020 /* Unset ELS complete queue */
6021 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006022 /* Unset FCP response complete queue */
6023 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6024 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6025 /* Unset fast-path event queue */
6026 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6027 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6028 /* Unset slow-path event queue */
6029 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6030}
6031
6032/**
6033 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6034 * @phba: pointer to lpfc hba data structure.
6035 *
6036 * This routine is invoked to allocate and set up a pool of completion queue
6037 * events. The body of the completion queue event is a completion queue entry
6038 * CQE. For now, this pool is used for the interrupt service routine to queue
6039 * the following HBA completion queue events for the worker thread to process:
6040 * - Mailbox asynchronous events
6041 * - Receive queue completion unsolicited events
6042 * Later, this can be used for all the slow-path events.
6043 *
6044 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006045 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006046 * -ENOMEM - No availble memory
6047 **/
6048static int
6049lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6050{
6051 struct lpfc_cq_event *cq_event;
6052 int i;
6053
6054 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6055 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6056 if (!cq_event)
6057 goto out_pool_create_fail;
6058 list_add_tail(&cq_event->list,
6059 &phba->sli4_hba.sp_cqe_event_pool);
6060 }
6061 return 0;
6062
6063out_pool_create_fail:
6064 lpfc_sli4_cq_event_pool_destroy(phba);
6065 return -ENOMEM;
6066}
6067
6068/**
6069 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6070 * @phba: pointer to lpfc hba data structure.
6071 *
6072 * This routine is invoked to free the pool of completion queue events at
6073 * driver unload time. Note that, it is the responsibility of the driver
6074 * cleanup routine to free all the outstanding completion-queue events
6075 * allocated from this pool back into the pool before invoking this routine
6076 * to destroy the pool.
6077 **/
6078static void
6079lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6080{
6081 struct lpfc_cq_event *cq_event, *next_cq_event;
6082
6083 list_for_each_entry_safe(cq_event, next_cq_event,
6084 &phba->sli4_hba.sp_cqe_event_pool, list) {
6085 list_del(&cq_event->list);
6086 kfree(cq_event);
6087 }
6088}
6089
6090/**
6091 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6092 * @phba: pointer to lpfc hba data structure.
6093 *
6094 * This routine is the lock free version of the API invoked to allocate a
6095 * completion-queue event from the free pool.
6096 *
6097 * Return: Pointer to the newly allocated completion-queue event if successful
6098 * NULL otherwise.
6099 **/
6100struct lpfc_cq_event *
6101__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6102{
6103 struct lpfc_cq_event *cq_event = NULL;
6104
6105 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6106 struct lpfc_cq_event, list);
6107 return cq_event;
6108}
6109
6110/**
6111 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6112 * @phba: pointer to lpfc hba data structure.
6113 *
6114 * This routine is the lock version of the API invoked to allocate a
6115 * completion-queue event from the free pool.
6116 *
6117 * Return: Pointer to the newly allocated completion-queue event if successful
6118 * NULL otherwise.
6119 **/
6120struct lpfc_cq_event *
6121lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6122{
6123 struct lpfc_cq_event *cq_event;
6124 unsigned long iflags;
6125
6126 spin_lock_irqsave(&phba->hbalock, iflags);
6127 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6128 spin_unlock_irqrestore(&phba->hbalock, iflags);
6129 return cq_event;
6130}
6131
6132/**
6133 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6134 * @phba: pointer to lpfc hba data structure.
6135 * @cq_event: pointer to the completion queue event to be freed.
6136 *
6137 * This routine is the lock free version of the API invoked to release a
6138 * completion-queue event back into the free pool.
6139 **/
6140void
6141__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6142 struct lpfc_cq_event *cq_event)
6143{
6144 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6145}
6146
6147/**
6148 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6149 * @phba: pointer to lpfc hba data structure.
6150 * @cq_event: pointer to the completion queue event to be freed.
6151 *
6152 * This routine is the lock version of the API invoked to release a
6153 * completion-queue event back into the free pool.
6154 **/
6155void
6156lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6157 struct lpfc_cq_event *cq_event)
6158{
6159 unsigned long iflags;
6160 spin_lock_irqsave(&phba->hbalock, iflags);
6161 __lpfc_sli4_cq_event_release(phba, cq_event);
6162 spin_unlock_irqrestore(&phba->hbalock, iflags);
6163}
6164
6165/**
6166 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6167 * @phba: pointer to lpfc hba data structure.
6168 *
6169 * This routine is to free all the pending completion-queue events to the
6170 * back into the free pool for device reset.
6171 **/
6172static void
6173lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6174{
6175 LIST_HEAD(cqelist);
6176 struct lpfc_cq_event *cqe;
6177 unsigned long iflags;
6178
6179 /* Retrieve all the pending WCQEs from pending WCQE lists */
6180 spin_lock_irqsave(&phba->hbalock, iflags);
6181 /* Pending FCP XRI abort events */
6182 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6183 &cqelist);
6184 /* Pending ELS XRI abort events */
6185 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6186 &cqelist);
6187 /* Pending asynnc events */
6188 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6189 &cqelist);
6190 spin_unlock_irqrestore(&phba->hbalock, iflags);
6191
6192 while (!list_empty(&cqelist)) {
6193 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6194 lpfc_sli4_cq_event_release(phba, cqe);
6195 }
6196}
6197
6198/**
6199 * lpfc_pci_function_reset - Reset pci function.
6200 * @phba: pointer to lpfc hba data structure.
6201 *
6202 * This routine is invoked to request a PCI function reset. It will destroys
6203 * all resources assigned to the PCI function which originates this request.
6204 *
6205 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006206 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006207 * ENOMEM - No availble memory
6208 * EIO - The mailbox failed to complete successfully.
6209 **/
6210int
6211lpfc_pci_function_reset(struct lpfc_hba *phba)
6212{
6213 LPFC_MBOXQ_t *mboxq;
6214 uint32_t rc = 0;
6215 uint32_t shdr_status, shdr_add_status;
6216 union lpfc_sli4_cfg_shdr *shdr;
6217
6218 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6219 if (!mboxq) {
6220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6221 "0494 Unable to allocate memory for issuing "
6222 "SLI_FUNCTION_RESET mailbox command\n");
6223 return -ENOMEM;
6224 }
6225
6226 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6227 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6228 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6229 LPFC_SLI4_MBX_EMBED);
6230 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6231 shdr = (union lpfc_sli4_cfg_shdr *)
6232 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6233 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6234 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6235 if (rc != MBX_TIMEOUT)
6236 mempool_free(mboxq, phba->mbox_mem_pool);
6237 if (shdr_status || shdr_add_status || rc) {
6238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6239 "0495 SLI_FUNCTION_RESET mailbox failed with "
6240 "status x%x add_status x%x, mbx status x%x\n",
6241 shdr_status, shdr_add_status, rc);
6242 rc = -ENXIO;
6243 }
6244 return rc;
6245}
6246
6247/**
6248 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6249 * @phba: pointer to lpfc hba data structure.
6250 * @cnt: number of nop mailbox commands to send.
6251 *
6252 * This routine is invoked to send a number @cnt of NOP mailbox command and
6253 * wait for each command to complete.
6254 *
6255 * Return: the number of NOP mailbox command completed.
6256 **/
6257static int
6258lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6259{
6260 LPFC_MBOXQ_t *mboxq;
6261 int length, cmdsent;
6262 uint32_t mbox_tmo;
6263 uint32_t rc = 0;
6264 uint32_t shdr_status, shdr_add_status;
6265 union lpfc_sli4_cfg_shdr *shdr;
6266
6267 if (cnt == 0) {
6268 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6269 "2518 Requested to send 0 NOP mailbox cmd\n");
6270 return cnt;
6271 }
6272
6273 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6274 if (!mboxq) {
6275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6276 "2519 Unable to allocate memory for issuing "
6277 "NOP mailbox command\n");
6278 return 0;
6279 }
6280
6281 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6282 length = (sizeof(struct lpfc_mbx_nop) -
6283 sizeof(struct lpfc_sli4_cfg_mhdr));
6284 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6285 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6286
6287 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6288 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6289 if (!phba->sli4_hba.intr_enable)
6290 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6291 else
6292 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6293 if (rc == MBX_TIMEOUT)
6294 break;
6295 /* Check return status */
6296 shdr = (union lpfc_sli4_cfg_shdr *)
6297 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6298 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6299 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6300 &shdr->response);
6301 if (shdr_status || shdr_add_status || rc) {
6302 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6303 "2520 NOP mailbox command failed "
6304 "status x%x add_status x%x mbx "
6305 "status x%x\n", shdr_status,
6306 shdr_add_status, rc);
6307 break;
6308 }
6309 }
6310
6311 if (rc != MBX_TIMEOUT)
6312 mempool_free(mboxq, phba->mbox_mem_pool);
6313
6314 return cmdsent;
6315}
6316
6317/**
6318 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6319 * @phba: pointer to lpfc hba data structure.
6320 * @fcfi: fcf index.
6321 *
6322 * This routine is invoked to unregister a FCFI from device.
6323 **/
6324void
6325lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6326{
6327 LPFC_MBOXQ_t *mbox;
6328 uint32_t mbox_tmo;
6329 int rc;
6330 unsigned long flags;
6331
6332 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6333
6334 if (!mbox)
6335 return;
6336
6337 lpfc_unreg_fcfi(mbox, fcfi);
6338
6339 if (!phba->sli4_hba.intr_enable)
6340 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6341 else {
6342 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6343 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6344 }
6345 if (rc != MBX_TIMEOUT)
6346 mempool_free(mbox, phba->mbox_mem_pool);
6347 if (rc != MBX_SUCCESS)
6348 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6349 "2517 Unregister FCFI command failed "
6350 "status %d, mbxStatus x%x\n", rc,
6351 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6352 else {
6353 spin_lock_irqsave(&phba->hbalock, flags);
6354 /* Mark the FCFI is no longer registered */
6355 phba->fcf.fcf_flag &=
James Smartecfd03c2010-02-12 14:41:27 -05006356 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
James Smartda0436e2009-05-22 14:51:39 -04006357 spin_unlock_irqrestore(&phba->hbalock, flags);
6358 }
6359}
6360
6361/**
6362 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6363 * @phba: pointer to lpfc hba data structure.
6364 *
6365 * This routine is invoked to set up the PCI device memory space for device
6366 * with SLI-4 interface spec.
6367 *
6368 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006369 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006370 * other values - error
6371 **/
6372static int
6373lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6374{
6375 struct pci_dev *pdev;
6376 unsigned long bar0map_len, bar1map_len, bar2map_len;
6377 int error = -ENODEV;
6378
6379 /* Obtain PCI device reference */
6380 if (!phba->pcidev)
6381 return error;
6382 else
6383 pdev = phba->pcidev;
6384
6385 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006386 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6387 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6388 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6389 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006390 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006391 }
6392 }
James Smartda0436e2009-05-22 14:51:39 -04006393
6394 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6395 * number of bytes required by each mapping. They are actually
James Smart1dfb5a42010-02-12 14:40:50 -05006396 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006397 */
James Smart1dfb5a42010-02-12 14:40:50 -05006398 if (pci_resource_start(pdev, 0)) {
6399 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6400 bar0map_len = pci_resource_len(pdev, 0);
6401 } else {
6402 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6403 bar0map_len = pci_resource_len(pdev, 1);
6404 }
6405 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6406 bar1map_len = pci_resource_len(pdev, 2);
James Smartda0436e2009-05-22 14:51:39 -04006407
James Smart1dfb5a42010-02-12 14:40:50 -05006408 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6409 bar2map_len = pci_resource_len(pdev, 4);
James Smartda0436e2009-05-22 14:51:39 -04006410
6411 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6412 phba->sli4_hba.conf_regs_memmap_p =
6413 ioremap(phba->pci_bar0_map, bar0map_len);
6414 if (!phba->sli4_hba.conf_regs_memmap_p) {
6415 dev_printk(KERN_ERR, &pdev->dev,
6416 "ioremap failed for SLI4 PCI config registers.\n");
6417 goto out;
6418 }
6419
6420 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6421 phba->sli4_hba.ctrl_regs_memmap_p =
6422 ioremap(phba->pci_bar1_map, bar1map_len);
6423 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6424 dev_printk(KERN_ERR, &pdev->dev,
6425 "ioremap failed for SLI4 HBA control registers.\n");
6426 goto out_iounmap_conf;
6427 }
6428
6429 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6430 phba->sli4_hba.drbl_regs_memmap_p =
6431 ioremap(phba->pci_bar2_map, bar2map_len);
6432 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6433 dev_printk(KERN_ERR, &pdev->dev,
6434 "ioremap failed for SLI4 HBA doorbell registers.\n");
6435 goto out_iounmap_ctrl;
6436 }
6437
6438 /* Set up BAR0 PCI config space register memory map */
6439 lpfc_sli4_bar0_register_memmap(phba);
6440
6441 /* Set up BAR1 register memory map */
6442 lpfc_sli4_bar1_register_memmap(phba);
6443
6444 /* Set up BAR2 register memory map */
6445 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6446 if (error)
6447 goto out_iounmap_all;
6448
6449 return 0;
6450
6451out_iounmap_all:
6452 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6453out_iounmap_ctrl:
6454 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6455out_iounmap_conf:
6456 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6457out:
6458 return error;
6459}
6460
6461/**
6462 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6463 * @phba: pointer to lpfc hba data structure.
6464 *
6465 * This routine is invoked to unset the PCI device memory space for device
6466 * with SLI-4 interface spec.
6467 **/
6468static void
6469lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6470{
6471 struct pci_dev *pdev;
6472
6473 /* Obtain PCI device reference */
6474 if (!phba->pcidev)
6475 return;
6476 else
6477 pdev = phba->pcidev;
6478
6479 /* Free coherent DMA memory allocated */
6480
6481 /* Unmap I/O memory space */
6482 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6483 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6484 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6485
6486 return;
6487}
6488
6489/**
James Smart3772a992009-05-22 14:50:54 -04006490 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6491 * @phba: pointer to lpfc hba data structure.
6492 *
6493 * This routine is invoked to enable the MSI-X interrupt vectors to device
6494 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6495 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6496 * invoked, enables either all or nothing, depending on the current
6497 * availability of PCI vector resources. The device driver is responsible
6498 * for calling the individual request_irq() to register each MSI-X vector
6499 * with a interrupt handler, which is done in this function. Note that
6500 * later when device is unloading, the driver should always call free_irq()
6501 * on all MSI-X vectors it has done request_irq() on before calling
6502 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6503 * will be left with MSI-X enabled and leaks its vectors.
6504 *
6505 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006506 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006507 * other values - error
6508 **/
6509static int
6510lpfc_sli_enable_msix(struct lpfc_hba *phba)
6511{
6512 int rc, i;
6513 LPFC_MBOXQ_t *pmb;
6514
6515 /* Set up MSI-X multi-message vectors */
6516 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6517 phba->msix_entries[i].entry = i;
6518
6519 /* Configure MSI-X capability structure */
6520 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6521 ARRAY_SIZE(phba->msix_entries));
6522 if (rc) {
6523 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6524 "0420 PCI enable MSI-X failed (%d)\n", rc);
6525 goto msi_fail_out;
6526 }
6527 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6528 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6529 "0477 MSI-X entry[%d]: vector=x%x "
6530 "message=%d\n", i,
6531 phba->msix_entries[i].vector,
6532 phba->msix_entries[i].entry);
6533 /*
6534 * Assign MSI-X vectors to interrupt handlers
6535 */
6536
6537 /* vector-0 is associated to slow-path handler */
6538 rc = request_irq(phba->msix_entries[0].vector,
6539 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6540 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6541 if (rc) {
6542 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6543 "0421 MSI-X slow-path request_irq failed "
6544 "(%d)\n", rc);
6545 goto msi_fail_out;
6546 }
6547
6548 /* vector-1 is associated to fast-path handler */
6549 rc = request_irq(phba->msix_entries[1].vector,
6550 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6551 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6552
6553 if (rc) {
6554 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6555 "0429 MSI-X fast-path request_irq failed "
6556 "(%d)\n", rc);
6557 goto irq_fail_out;
6558 }
6559
6560 /*
6561 * Configure HBA MSI-X attention conditions to messages
6562 */
6563 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6564
6565 if (!pmb) {
6566 rc = -ENOMEM;
6567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6568 "0474 Unable to allocate memory for issuing "
6569 "MBOX_CONFIG_MSI command\n");
6570 goto mem_fail_out;
6571 }
6572 rc = lpfc_config_msi(phba, pmb);
6573 if (rc)
6574 goto mbx_fail_out;
6575 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6576 if (rc != MBX_SUCCESS) {
6577 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6578 "0351 Config MSI mailbox command failed, "
6579 "mbxCmd x%x, mbxStatus x%x\n",
6580 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6581 goto mbx_fail_out;
6582 }
6583
6584 /* Free memory allocated for mailbox command */
6585 mempool_free(pmb, phba->mbox_mem_pool);
6586 return rc;
6587
6588mbx_fail_out:
6589 /* Free memory allocated for mailbox command */
6590 mempool_free(pmb, phba->mbox_mem_pool);
6591
6592mem_fail_out:
6593 /* free the irq already requested */
6594 free_irq(phba->msix_entries[1].vector, phba);
6595
6596irq_fail_out:
6597 /* free the irq already requested */
6598 free_irq(phba->msix_entries[0].vector, phba);
6599
6600msi_fail_out:
6601 /* Unconfigure MSI-X capability structure */
6602 pci_disable_msix(phba->pcidev);
6603 return rc;
6604}
6605
6606/**
6607 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6608 * @phba: pointer to lpfc hba data structure.
6609 *
6610 * This routine is invoked to release the MSI-X vectors and then disable the
6611 * MSI-X interrupt mode to device with SLI-3 interface spec.
6612 **/
6613static void
6614lpfc_sli_disable_msix(struct lpfc_hba *phba)
6615{
6616 int i;
6617
6618 /* Free up MSI-X multi-message vectors */
6619 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6620 free_irq(phba->msix_entries[i].vector, phba);
6621 /* Disable MSI-X */
6622 pci_disable_msix(phba->pcidev);
6623
6624 return;
6625}
6626
6627/**
6628 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6629 * @phba: pointer to lpfc hba data structure.
6630 *
6631 * This routine is invoked to enable the MSI interrupt mode to device with
6632 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6633 * enable the MSI vector. The device driver is responsible for calling the
6634 * request_irq() to register MSI vector with a interrupt the handler, which
6635 * is done in this function.
6636 *
6637 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006638 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006639 * other values - error
6640 */
6641static int
6642lpfc_sli_enable_msi(struct lpfc_hba *phba)
6643{
6644 int rc;
6645
6646 rc = pci_enable_msi(phba->pcidev);
6647 if (!rc)
6648 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6649 "0462 PCI enable MSI mode success.\n");
6650 else {
6651 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6652 "0471 PCI enable MSI mode failed (%d)\n", rc);
6653 return rc;
6654 }
6655
6656 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6657 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6658 if (rc) {
6659 pci_disable_msi(phba->pcidev);
6660 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6661 "0478 MSI request_irq failed (%d)\n", rc);
6662 }
6663 return rc;
6664}
6665
6666/**
6667 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6668 * @phba: pointer to lpfc hba data structure.
6669 *
6670 * This routine is invoked to disable the MSI interrupt mode to device with
6671 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6672 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6673 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6674 * its vector.
6675 */
6676static void
6677lpfc_sli_disable_msi(struct lpfc_hba *phba)
6678{
6679 free_irq(phba->pcidev->irq, phba);
6680 pci_disable_msi(phba->pcidev);
6681 return;
6682}
6683
6684/**
6685 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6686 * @phba: pointer to lpfc hba data structure.
6687 *
6688 * This routine is invoked to enable device interrupt and associate driver's
6689 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6690 * spec. Depends on the interrupt mode configured to the driver, the driver
6691 * will try to fallback from the configured interrupt mode to an interrupt
6692 * mode which is supported by the platform, kernel, and device in the order
6693 * of:
6694 * MSI-X -> MSI -> IRQ.
6695 *
6696 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006697 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006698 * other values - error
6699 **/
6700static uint32_t
6701lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6702{
6703 uint32_t intr_mode = LPFC_INTR_ERROR;
6704 int retval;
6705
6706 if (cfg_mode == 2) {
6707 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6708 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6709 if (!retval) {
6710 /* Now, try to enable MSI-X interrupt mode */
6711 retval = lpfc_sli_enable_msix(phba);
6712 if (!retval) {
6713 /* Indicate initialization to MSI-X mode */
6714 phba->intr_type = MSIX;
6715 intr_mode = 2;
6716 }
6717 }
6718 }
6719
6720 /* Fallback to MSI if MSI-X initialization failed */
6721 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6722 retval = lpfc_sli_enable_msi(phba);
6723 if (!retval) {
6724 /* Indicate initialization to MSI mode */
6725 phba->intr_type = MSI;
6726 intr_mode = 1;
6727 }
6728 }
6729
6730 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6731 if (phba->intr_type == NONE) {
6732 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6733 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6734 if (!retval) {
6735 /* Indicate initialization to INTx mode */
6736 phba->intr_type = INTx;
6737 intr_mode = 0;
6738 }
6739 }
6740 return intr_mode;
6741}
6742
6743/**
6744 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6745 * @phba: pointer to lpfc hba data structure.
6746 *
6747 * This routine is invoked to disable device interrupt and disassociate the
6748 * driver's interrupt handler(s) from interrupt vector(s) to device with
6749 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6750 * release the interrupt vector(s) for the message signaled interrupt.
6751 **/
6752static void
6753lpfc_sli_disable_intr(struct lpfc_hba *phba)
6754{
6755 /* Disable the currently initialized interrupt mode */
6756 if (phba->intr_type == MSIX)
6757 lpfc_sli_disable_msix(phba);
6758 else if (phba->intr_type == MSI)
6759 lpfc_sli_disable_msi(phba);
6760 else if (phba->intr_type == INTx)
6761 free_irq(phba->pcidev->irq, phba);
6762
6763 /* Reset interrupt management states */
6764 phba->intr_type = NONE;
6765 phba->sli.slistat.sli_intr = 0;
6766
6767 return;
6768}
6769
6770/**
James Smartda0436e2009-05-22 14:51:39 -04006771 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6772 * @phba: pointer to lpfc hba data structure.
6773 *
6774 * This routine is invoked to enable the MSI-X interrupt vectors to device
6775 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6776 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6777 * enables either all or nothing, depending on the current availability of
6778 * PCI vector resources. The device driver is responsible for calling the
6779 * individual request_irq() to register each MSI-X vector with a interrupt
6780 * handler, which is done in this function. Note that later when device is
6781 * unloading, the driver should always call free_irq() on all MSI-X vectors
6782 * it has done request_irq() on before calling pci_disable_msix(). Failure
6783 * to do so results in a BUG_ON() and a device will be left with MSI-X
6784 * enabled and leaks its vectors.
6785 *
6786 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006787 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006788 * other values - error
6789 **/
6790static int
6791lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6792{
6793 int rc, index;
6794
6795 /* Set up MSI-X multi-message vectors */
6796 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6797 phba->sli4_hba.msix_entries[index].entry = index;
6798
6799 /* Configure MSI-X capability structure */
6800 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6801 phba->sli4_hba.cfg_eqn);
6802 if (rc) {
6803 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6804 "0484 PCI enable MSI-X failed (%d)\n", rc);
6805 goto msi_fail_out;
6806 }
6807 /* Log MSI-X vector assignment */
6808 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6809 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6810 "0489 MSI-X entry[%d]: vector=x%x "
6811 "message=%d\n", index,
6812 phba->sli4_hba.msix_entries[index].vector,
6813 phba->sli4_hba.msix_entries[index].entry);
6814 /*
6815 * Assign MSI-X vectors to interrupt handlers
6816 */
6817
6818 /* The first vector must associated to slow-path handler for MQ */
6819 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6820 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6821 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6822 if (rc) {
6823 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6824 "0485 MSI-X slow-path request_irq failed "
6825 "(%d)\n", rc);
6826 goto msi_fail_out;
6827 }
6828
6829 /* The rest of the vector(s) are associated to fast-path handler(s) */
6830 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6831 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6832 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6833 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6834 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6835 LPFC_FP_DRIVER_HANDLER_NAME,
6836 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6837 if (rc) {
6838 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6839 "0486 MSI-X fast-path (%d) "
6840 "request_irq failed (%d)\n", index, rc);
6841 goto cfg_fail_out;
6842 }
6843 }
6844
6845 return rc;
6846
6847cfg_fail_out:
6848 /* free the irq already requested */
6849 for (--index; index >= 1; index--)
6850 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6851 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6852
6853 /* free the irq already requested */
6854 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6855
6856msi_fail_out:
6857 /* Unconfigure MSI-X capability structure */
6858 pci_disable_msix(phba->pcidev);
6859 return rc;
6860}
6861
6862/**
6863 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6864 * @phba: pointer to lpfc hba data structure.
6865 *
6866 * This routine is invoked to release the MSI-X vectors and then disable the
6867 * MSI-X interrupt mode to device with SLI-4 interface spec.
6868 **/
6869static void
6870lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6871{
6872 int index;
6873
6874 /* Free up MSI-X multi-message vectors */
6875 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6876
6877 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6878 free_irq(phba->sli4_hba.msix_entries[index].vector,
6879 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6880 /* Disable MSI-X */
6881 pci_disable_msix(phba->pcidev);
6882
6883 return;
6884}
6885
6886/**
6887 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6888 * @phba: pointer to lpfc hba data structure.
6889 *
6890 * This routine is invoked to enable the MSI interrupt mode to device with
6891 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6892 * to enable the MSI vector. The device driver is responsible for calling
6893 * the request_irq() to register MSI vector with a interrupt the handler,
6894 * which is done in this function.
6895 *
6896 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006897 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006898 * other values - error
6899 **/
6900static int
6901lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6902{
6903 int rc, index;
6904
6905 rc = pci_enable_msi(phba->pcidev);
6906 if (!rc)
6907 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6908 "0487 PCI enable MSI mode success.\n");
6909 else {
6910 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6911 "0488 PCI enable MSI mode failed (%d)\n", rc);
6912 return rc;
6913 }
6914
6915 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6916 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6917 if (rc) {
6918 pci_disable_msi(phba->pcidev);
6919 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6920 "0490 MSI request_irq failed (%d)\n", rc);
6921 }
6922
6923 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6924 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6925 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6926 }
6927
6928 return rc;
6929}
6930
6931/**
6932 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6933 * @phba: pointer to lpfc hba data structure.
6934 *
6935 * This routine is invoked to disable the MSI interrupt mode to device with
6936 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6937 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6938 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6939 * its vector.
6940 **/
6941static void
6942lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6943{
6944 free_irq(phba->pcidev->irq, phba);
6945 pci_disable_msi(phba->pcidev);
6946 return;
6947}
6948
6949/**
6950 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6951 * @phba: pointer to lpfc hba data structure.
6952 *
6953 * This routine is invoked to enable device interrupt and associate driver's
6954 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6955 * interface spec. Depends on the interrupt mode configured to the driver,
6956 * the driver will try to fallback from the configured interrupt mode to an
6957 * interrupt mode which is supported by the platform, kernel, and device in
6958 * the order of:
6959 * MSI-X -> MSI -> IRQ.
6960 *
6961 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006962 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006963 * other values - error
6964 **/
6965static uint32_t
6966lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6967{
6968 uint32_t intr_mode = LPFC_INTR_ERROR;
6969 int retval, index;
6970
6971 if (cfg_mode == 2) {
6972 /* Preparation before conf_msi mbox cmd */
6973 retval = 0;
6974 if (!retval) {
6975 /* Now, try to enable MSI-X interrupt mode */
6976 retval = lpfc_sli4_enable_msix(phba);
6977 if (!retval) {
6978 /* Indicate initialization to MSI-X mode */
6979 phba->intr_type = MSIX;
6980 intr_mode = 2;
6981 }
6982 }
6983 }
6984
6985 /* Fallback to MSI if MSI-X initialization failed */
6986 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6987 retval = lpfc_sli4_enable_msi(phba);
6988 if (!retval) {
6989 /* Indicate initialization to MSI mode */
6990 phba->intr_type = MSI;
6991 intr_mode = 1;
6992 }
6993 }
6994
6995 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6996 if (phba->intr_type == NONE) {
6997 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6998 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6999 if (!retval) {
7000 /* Indicate initialization to INTx mode */
7001 phba->intr_type = INTx;
7002 intr_mode = 0;
7003 for (index = 0; index < phba->cfg_fcp_eq_count;
7004 index++) {
7005 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7006 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7007 }
7008 }
7009 }
7010 return intr_mode;
7011}
7012
7013/**
7014 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7015 * @phba: pointer to lpfc hba data structure.
7016 *
7017 * This routine is invoked to disable device interrupt and disassociate
7018 * the driver's interrupt handler(s) from interrupt vector(s) to device
7019 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7020 * will release the interrupt vector(s) for the message signaled interrupt.
7021 **/
7022static void
7023lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7024{
7025 /* Disable the currently initialized interrupt mode */
7026 if (phba->intr_type == MSIX)
7027 lpfc_sli4_disable_msix(phba);
7028 else if (phba->intr_type == MSI)
7029 lpfc_sli4_disable_msi(phba);
7030 else if (phba->intr_type == INTx)
7031 free_irq(phba->pcidev->irq, phba);
7032
7033 /* Reset interrupt management states */
7034 phba->intr_type = NONE;
7035 phba->sli.slistat.sli_intr = 0;
7036
7037 return;
7038}
7039
7040/**
James Smart3772a992009-05-22 14:50:54 -04007041 * lpfc_unset_hba - Unset SLI3 hba device initialization
7042 * @phba: pointer to lpfc hba data structure.
7043 *
7044 * This routine is invoked to unset the HBA device initialization steps to
7045 * a device with SLI-3 interface spec.
7046 **/
7047static void
7048lpfc_unset_hba(struct lpfc_hba *phba)
7049{
7050 struct lpfc_vport *vport = phba->pport;
7051 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7052
7053 spin_lock_irq(shost->host_lock);
7054 vport->load_flag |= FC_UNLOADING;
7055 spin_unlock_irq(shost->host_lock);
7056
7057 lpfc_stop_hba_timers(phba);
7058
7059 phba->pport->work_port_events = 0;
7060
7061 lpfc_sli_hba_down(phba);
7062
7063 lpfc_sli_brdrestart(phba);
7064
7065 lpfc_sli_disable_intr(phba);
7066
7067 return;
7068}
7069
7070/**
James Smartda0436e2009-05-22 14:51:39 -04007071 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7072 * @phba: pointer to lpfc hba data structure.
7073 *
7074 * This routine is invoked to unset the HBA device initialization steps to
7075 * a device with SLI-4 interface spec.
7076 **/
7077static void
7078lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7079{
7080 struct lpfc_vport *vport = phba->pport;
7081 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7082
7083 spin_lock_irq(shost->host_lock);
7084 vport->load_flag |= FC_UNLOADING;
7085 spin_unlock_irq(shost->host_lock);
7086
7087 phba->pport->work_port_events = 0;
7088
7089 lpfc_sli4_hba_down(phba);
7090
7091 lpfc_sli4_disable_intr(phba);
7092
7093 return;
7094}
7095
7096/**
7097 * lpfc_sli4_hba_unset - Unset the fcoe hba
7098 * @phba: Pointer to HBA context object.
7099 *
7100 * This function is called in the SLI4 code path to reset the HBA's FCoE
7101 * function. The caller is not required to hold any lock. This routine
7102 * issues PCI function reset mailbox command to reset the FCoE function.
7103 * At the end of the function, it calls lpfc_hba_down_post function to
7104 * free any pending commands.
7105 **/
7106static void
7107lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7108{
7109 int wait_cnt = 0;
7110 LPFC_MBOXQ_t *mboxq;
7111
7112 lpfc_stop_hba_timers(phba);
7113 phba->sli4_hba.intr_enable = 0;
7114
7115 /*
7116 * Gracefully wait out the potential current outstanding asynchronous
7117 * mailbox command.
7118 */
7119
7120 /* First, block any pending async mailbox command from posted */
7121 spin_lock_irq(&phba->hbalock);
7122 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7123 spin_unlock_irq(&phba->hbalock);
7124 /* Now, trying to wait it out if we can */
7125 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7126 msleep(10);
7127 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7128 break;
7129 }
7130 /* Forcefully release the outstanding mailbox command if timed out */
7131 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7132 spin_lock_irq(&phba->hbalock);
7133 mboxq = phba->sli.mbox_active;
7134 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7135 __lpfc_mbox_cmpl_put(phba, mboxq);
7136 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7137 phba->sli.mbox_active = NULL;
7138 spin_unlock_irq(&phba->hbalock);
7139 }
7140
7141 /* Tear down the queues in the HBA */
7142 lpfc_sli4_queue_unset(phba);
7143
7144 /* Disable PCI subsystem interrupt */
7145 lpfc_sli4_disable_intr(phba);
7146
7147 /* Stop kthread signal shall trigger work_done one more time */
7148 kthread_stop(phba->worker_thread);
7149
7150 /* Stop the SLI4 device port */
7151 phba->pport->work_port_events = 0;
7152}
7153
James Smart28baac72010-02-12 14:42:03 -05007154 /**
7155 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7156 * @phba: Pointer to HBA context object.
7157 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7158 *
7159 * This function is called in the SLI4 code path to read the port's
7160 * sli4 capabilities.
7161 *
7162 * This function may be be called from any context that can block-wait
7163 * for the completion. The expectation is that this routine is called
7164 * typically from probe_one or from the online routine.
7165 **/
7166int
7167lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7168{
7169 int rc;
7170 struct lpfc_mqe *mqe;
7171 struct lpfc_pc_sli4_params *sli4_params;
7172 uint32_t mbox_tmo;
7173
7174 rc = 0;
7175 mqe = &mboxq->u.mqe;
7176
7177 /* Read the port's SLI4 Parameters port capabilities */
7178 lpfc_sli4_params(mboxq);
7179 if (!phba->sli4_hba.intr_enable)
7180 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7181 else {
7182 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7183 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7184 }
7185
7186 if (unlikely(rc))
7187 return 1;
7188
7189 sli4_params = &phba->sli4_hba.pc_sli4_params;
7190 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7191 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7192 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7193 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7194 &mqe->un.sli4_params);
7195 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7196 &mqe->un.sli4_params);
7197 sli4_params->proto_types = mqe->un.sli4_params.word3;
7198 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7199 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7200 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7201 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7202 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7203 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7204 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7205 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7206 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7207 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7208 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7209 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7210 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7211 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7212 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7213 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7214 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7215 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7216 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7217 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7218 return rc;
7219}
7220
James Smartda0436e2009-05-22 14:51:39 -04007221/**
James Smart3772a992009-05-22 14:50:54 -04007222 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7223 * @pdev: pointer to PCI device
7224 * @pid: pointer to PCI device identifier
7225 *
7226 * This routine is to be called to attach a device with SLI-3 interface spec
7227 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7228 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7229 * information of the device and driver to see if the driver state that it can
7230 * support this kind of device. If the match is successful, the driver core
7231 * invokes this routine. If this routine determines it can claim the HBA, it
7232 * does all the initialization that it needs to do to handle the HBA properly.
7233 *
7234 * Return code
7235 * 0 - driver can claim the device
7236 * negative value - driver can not claim the device
7237 **/
7238static int __devinit
7239lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7240{
7241 struct lpfc_hba *phba;
7242 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007243 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007244 int error;
7245 uint32_t cfg_mode, intr_mode;
7246
7247 /* Allocate memory for HBA structure */
7248 phba = lpfc_hba_alloc(pdev);
7249 if (!phba)
7250 return -ENOMEM;
7251
7252 /* Perform generic PCI device enabling operation */
7253 error = lpfc_enable_pci_dev(phba);
7254 if (error) {
7255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7256 "1401 Failed to enable pci device.\n");
7257 goto out_free_phba;
7258 }
7259
7260 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7261 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7262 if (error)
7263 goto out_disable_pci_dev;
7264
7265 /* Set up SLI-3 specific device PCI memory space */
7266 error = lpfc_sli_pci_mem_setup(phba);
7267 if (error) {
7268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7269 "1402 Failed to set up pci memory space.\n");
7270 goto out_disable_pci_dev;
7271 }
7272
7273 /* Set up phase-1 common device driver resources */
7274 error = lpfc_setup_driver_resource_phase1(phba);
7275 if (error) {
7276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7277 "1403 Failed to set up driver resource.\n");
7278 goto out_unset_pci_mem_s3;
7279 }
7280
7281 /* Set up SLI-3 specific device driver resources */
7282 error = lpfc_sli_driver_resource_setup(phba);
7283 if (error) {
7284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7285 "1404 Failed to set up driver resource.\n");
7286 goto out_unset_pci_mem_s3;
7287 }
7288
7289 /* Initialize and populate the iocb list per host */
7290 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7291 if (error) {
7292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7293 "1405 Failed to initialize iocb list.\n");
7294 goto out_unset_driver_resource_s3;
7295 }
7296
7297 /* Set up common device driver resources */
7298 error = lpfc_setup_driver_resource_phase2(phba);
7299 if (error) {
7300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7301 "1406 Failed to set up driver resource.\n");
7302 goto out_free_iocb_list;
7303 }
7304
7305 /* Create SCSI host to the physical port */
7306 error = lpfc_create_shost(phba);
7307 if (error) {
7308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7309 "1407 Failed to create scsi host.\n");
7310 goto out_unset_driver_resource;
7311 }
7312
7313 /* Configure sysfs attributes */
7314 vport = phba->pport;
7315 error = lpfc_alloc_sysfs_attr(vport);
7316 if (error) {
7317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7318 "1476 Failed to allocate sysfs attr\n");
7319 goto out_destroy_shost;
7320 }
7321
James Smart6669f9b2009-10-02 15:16:45 -04007322 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04007323 /* Now, trying to enable interrupt and bring up the device */
7324 cfg_mode = phba->cfg_use_msi;
7325 while (true) {
7326 /* Put device to a known state before enabling interrupt */
7327 lpfc_stop_port(phba);
7328 /* Configure and enable interrupt */
7329 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7330 if (intr_mode == LPFC_INTR_ERROR) {
7331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7332 "0431 Failed to enable interrupt.\n");
7333 error = -ENODEV;
7334 goto out_free_sysfs_attr;
7335 }
7336 /* SLI-3 HBA setup */
7337 if (lpfc_sli_hba_setup(phba)) {
7338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7339 "1477 Failed to set up hba\n");
7340 error = -ENODEV;
7341 goto out_remove_device;
7342 }
7343
7344 /* Wait 50ms for the interrupts of previous mailbox commands */
7345 msleep(50);
7346 /* Check active interrupts on message signaled interrupts */
7347 if (intr_mode == 0 ||
7348 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7349 /* Log the current active interrupt mode */
7350 phba->intr_mode = intr_mode;
7351 lpfc_log_intr_mode(phba, intr_mode);
7352 break;
7353 } else {
7354 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7355 "0447 Configure interrupt mode (%d) "
7356 "failed active interrupt test.\n",
7357 intr_mode);
7358 /* Disable the current interrupt mode */
7359 lpfc_sli_disable_intr(phba);
7360 /* Try next level of interrupt mode */
7361 cfg_mode = --intr_mode;
7362 }
7363 }
7364
7365 /* Perform post initialization setup */
7366 lpfc_post_init_setup(phba);
7367
7368 /* Check if there are static vports to be created. */
7369 lpfc_create_static_vport(phba);
7370
7371 return 0;
7372
7373out_remove_device:
7374 lpfc_unset_hba(phba);
7375out_free_sysfs_attr:
7376 lpfc_free_sysfs_attr(vport);
7377out_destroy_shost:
7378 lpfc_destroy_shost(phba);
7379out_unset_driver_resource:
7380 lpfc_unset_driver_resource_phase2(phba);
7381out_free_iocb_list:
7382 lpfc_free_iocb_list(phba);
7383out_unset_driver_resource_s3:
7384 lpfc_sli_driver_resource_unset(phba);
7385out_unset_pci_mem_s3:
7386 lpfc_sli_pci_mem_unset(phba);
7387out_disable_pci_dev:
7388 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007389 if (shost)
7390 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007391out_free_phba:
7392 lpfc_hba_free(phba);
7393 return error;
7394}
7395
7396/**
7397 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007398 * @pdev: pointer to PCI device
7399 *
James Smart3772a992009-05-22 14:50:54 -04007400 * This routine is to be called to disattach a device with SLI-3 interface
7401 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7402 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7403 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007404 **/
dea31012005-04-17 16:05:31 -05007405static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007406lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007407{
James Smart2e0fef82007-06-17 19:56:36 -05007408 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7409 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007410 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007411 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007412 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007413 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7414
James Smart549e55c2007-08-02 11:09:51 -04007415 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007416 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007417 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007418
James Smart858c9f62007-06-17 19:56:39 -05007419 lpfc_free_sysfs_attr(vport);
7420
James Smarteada2722008-12-04 22:39:13 -05007421 /* Release all the vports against this physical port */
7422 vports = lpfc_create_vport_work_array(phba);
7423 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007424 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007425 fc_vport_terminate(vports[i]->fc_vport);
7426 lpfc_destroy_vport_work_array(phba, vports);
7427
7428 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007429 fc_remove_host(shost);
7430 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007431 lpfc_cleanup(vport);
7432
James Smart2e0fef82007-06-17 19:56:36 -05007433 /*
7434 * Bring down the SLI Layer. This step disable all interrupts,
7435 * clears the rings, discards all mailbox commands, and resets
7436 * the HBA.
7437 */
James Smarta257bf92009-04-06 18:48:10 -04007438
7439 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007440 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007441 /* Stop kthread signal shall trigger work_done one more time */
7442 kthread_stop(phba->worker_thread);
7443 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007444 lpfc_sli_brdrestart(phba);
7445
James Smart3772a992009-05-22 14:50:54 -04007446 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007447 spin_lock_irq(&phba->hbalock);
7448 list_del_init(&vport->listentry);
7449 spin_unlock_irq(&phba->hbalock);
7450
James Smart858c9f62007-06-17 19:56:39 -05007451 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007452
James Smart5b75da22008-12-04 22:39:35 -05007453 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007454 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007455
7456 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007457 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007458
7459 /*
7460 * Call scsi_free before mem_free since scsi bufs are released to their
7461 * corresponding pools here.
7462 */
7463 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007464 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007465
James Smart34b02dc2008-08-24 21:49:55 -04007466 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7467 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007468
James Smart2e0fef82007-06-17 19:56:36 -05007469 /* Free resources associated with SLI2 interface */
7470 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007471 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007472
7473 /* unmap adapter SLIM and Control Registers */
7474 iounmap(phba->ctrl_regs_memmap_p);
7475 iounmap(phba->slim_memmap_p);
7476
James Smart3772a992009-05-22 14:50:54 -04007477 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007478
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007479 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007480 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007481}
7482
Linas Vepstas8d63f372007-02-14 14:28:36 -06007483/**
James Smart3772a992009-05-22 14:50:54 -04007484 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007485 * @pdev: pointer to PCI device
7486 * @msg: power management message
7487 *
James Smart3772a992009-05-22 14:50:54 -04007488 * This routine is to be called from the kernel's PCI subsystem to support
7489 * system Power Management (PM) to device with SLI-3 interface spec. When
7490 * PM invokes this method, it quiesces the device by stopping the driver's
7491 * worker thread for the device, turning off device's interrupt and DMA,
7492 * and bring the device offline. Note that as the driver implements the
7493 * minimum PM requirements to a power-aware driver's PM support for the
7494 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7495 * to the suspend() method call will be treated as SUSPEND and the driver will
7496 * fully reinitialize its device during resume() method call, the driver will
7497 * set device to PCI_D3hot state in PCI config space instead of setting it
7498 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007499 *
7500 * Return code
James Smart3772a992009-05-22 14:50:54 -04007501 * 0 - driver suspended the device
7502 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007503 **/
7504static int
James Smart3772a992009-05-22 14:50:54 -04007505lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007506{
7507 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7508 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7509
7510 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7511 "0473 PCI device Power Management suspend.\n");
7512
7513 /* Bring down the device */
7514 lpfc_offline_prep(phba);
7515 lpfc_offline(phba);
7516 kthread_stop(phba->worker_thread);
7517
7518 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007519 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007520
7521 /* Save device state to PCI config space */
7522 pci_save_state(pdev);
7523 pci_set_power_state(pdev, PCI_D3hot);
7524
7525 return 0;
7526}
7527
7528/**
James Smart3772a992009-05-22 14:50:54 -04007529 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007530 * @pdev: pointer to PCI device
7531 *
James Smart3772a992009-05-22 14:50:54 -04007532 * This routine is to be called from the kernel's PCI subsystem to support
7533 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7534 * invokes this method, it restores the device's PCI config space state and
7535 * fully reinitializes the device and brings it online. Note that as the
7536 * driver implements the minimum PM requirements to a power-aware driver's
7537 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7538 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7539 * driver will fully reinitialize its device during resume() method call,
7540 * the device will be set to PCI_D0 directly in PCI config space before
7541 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007542 *
7543 * Return code
James Smart3772a992009-05-22 14:50:54 -04007544 * 0 - driver suspended the device
7545 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007546 **/
7547static int
James Smart3772a992009-05-22 14:50:54 -04007548lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007549{
7550 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7551 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007552 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007553 int error;
7554
7555 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7556 "0452 PCI device Power Management resume.\n");
7557
7558 /* Restore device state from PCI config space */
7559 pci_set_power_state(pdev, PCI_D0);
7560 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007561
James Smart1dfb5a42010-02-12 14:40:50 -05007562 /*
7563 * As the new kernel behavior of pci_restore_state() API call clears
7564 * device saved_state flag, need to save the restored state again.
7565 */
7566 pci_save_state(pdev);
7567
James Smart3a55b532008-12-04 22:38:54 -05007568 if (pdev->is_busmaster)
7569 pci_set_master(pdev);
7570
7571 /* Startup the kernel thread for this host adapter. */
7572 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7573 "lpfc_worker_%d", phba->brd_no);
7574 if (IS_ERR(phba->worker_thread)) {
7575 error = PTR_ERR(phba->worker_thread);
7576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7577 "0434 PM resume failed to start worker "
7578 "thread: error=x%x.\n", error);
7579 return error;
7580 }
7581
James Smart5b75da22008-12-04 22:39:35 -05007582 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007583 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007584 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007586 "0430 PM resume Failed to enable interrupt\n");
7587 return -EIO;
7588 } else
7589 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007590
7591 /* Restart HBA and bring it online */
7592 lpfc_sli_brdrestart(phba);
7593 lpfc_online(phba);
7594
James Smart5b75da22008-12-04 22:39:35 -05007595 /* Log the current active interrupt mode */
7596 lpfc_log_intr_mode(phba, phba->intr_mode);
7597
James Smart3a55b532008-12-04 22:38:54 -05007598 return 0;
7599}
7600
7601/**
James Smart891478a2009-11-18 15:40:23 -05007602 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7603 * @phba: pointer to lpfc hba data structure.
7604 *
7605 * This routine is called to prepare the SLI3 device for PCI slot recover. It
7606 * aborts and stops all the on-going I/Os on the pci device.
7607 **/
7608static void
7609lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7610{
7611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7612 "2723 PCI channel I/O abort preparing for recovery\n");
7613 /* Prepare for bringing HBA offline */
7614 lpfc_offline_prep(phba);
7615 /* Clear sli active flag to prevent sysfs access to HBA */
7616 spin_lock_irq(&phba->hbalock);
7617 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7618 spin_unlock_irq(&phba->hbalock);
7619 /* Stop and flush all I/Os and bring HBA offline */
7620 lpfc_offline(phba);
7621}
7622
7623/**
James Smart0d878412009-10-02 15:16:56 -04007624 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7625 * @phba: pointer to lpfc hba data structure.
7626 *
7627 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7628 * disables the device interrupt and pci device, and aborts the internal FCP
7629 * pending I/Os.
7630 **/
7631static void
7632lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7633{
7634 struct lpfc_sli *psli = &phba->sli;
7635 struct lpfc_sli_ring *pring;
7636
7637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007638 "2710 PCI channel disable preparing for reset\n");
James Smart0d878412009-10-02 15:16:56 -04007639 /* Disable interrupt and pci device */
7640 lpfc_sli_disable_intr(phba);
7641 pci_disable_device(phba->pcidev);
7642 /*
7643 * There may be I/Os dropped by the firmware.
7644 * Error iocb (I/O) on txcmplq and let the SCSI layer
7645 * retry it after re-establishing link.
7646 */
7647 pring = &psli->ring[psli->fcp_ring];
7648 lpfc_sli_abort_iocb_ring(phba, pring);
7649}
7650
7651/**
7652 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7653 * @phba: pointer to lpfc hba data structure.
7654 *
7655 * This routine is called to prepare the SLI3 device for PCI slot permanently
7656 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7657 * pending I/Os.
7658 **/
7659static void
7660lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7661{
7662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007663 "2711 PCI channel permanent disable for failure\n");
James Smart0d878412009-10-02 15:16:56 -04007664 /* Clean up all driver's outstanding SCSI I/Os */
7665 lpfc_sli_flush_fcp_rings(phba);
7666}
7667
7668/**
James Smart3772a992009-05-22 14:50:54 -04007669 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007670 * @pdev: pointer to PCI device.
7671 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007672 *
James Smart3772a992009-05-22 14:50:54 -04007673 * This routine is called from the PCI subsystem for I/O error handling to
7674 * device with SLI-3 interface spec. This function is called by the PCI
7675 * subsystem after a PCI bus error affecting this device has been detected.
7676 * When this function is invoked, it will need to stop all the I/Os and
7677 * interrupt(s) to the device. Once that is done, it will return
7678 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7679 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007680 *
7681 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007682 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007683 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7684 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007685 **/
James Smart3772a992009-05-22 14:50:54 -04007686static pci_ers_result_t
7687lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007688{
James Smart51ef4c22007-08-02 11:10:31 -04007689 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7690 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007691
James Smartaacc20e2009-12-21 17:02:51 -05007692 /* Block all SCSI devices' I/Os on the host */
7693 lpfc_scsi_dev_block(phba);
7694
James Smart0d878412009-10-02 15:16:56 -04007695 switch (state) {
7696 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007697 /* Non-fatal error, prepare for recovery */
7698 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007699 return PCI_ERS_RESULT_CAN_RECOVER;
7700 case pci_channel_io_frozen:
7701 /* Fatal error, prepare for slot reset */
7702 lpfc_sli_prep_dev_for_reset(phba);
7703 return PCI_ERS_RESULT_NEED_RESET;
7704 case pci_channel_io_perm_failure:
7705 /* Permanent failure, prepare for device down */
7706 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007707 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007708 default:
7709 /* Unknown state, prepare and request slot reset */
7710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7711 "0472 Unknown PCI error state: x%x\n", state);
7712 lpfc_sli_prep_dev_for_reset(phba);
7713 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007714 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007715}
7716
7717/**
James Smart3772a992009-05-22 14:50:54 -04007718 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007719 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007720 *
James Smart3772a992009-05-22 14:50:54 -04007721 * This routine is called from the PCI subsystem for error handling to
7722 * device with SLI-3 interface spec. This is called after PCI bus has been
7723 * reset to restart the PCI card from scratch, as if from a cold-boot.
7724 * During the PCI subsystem error recovery, after driver returns
7725 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7726 * recovery and then call this routine before calling the .resume method
7727 * to recover the device. This function will initialize the HBA device,
7728 * enable the interrupt, but it will just put the HBA to offline state
7729 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007730 *
7731 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007732 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7733 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007734 */
James Smart3772a992009-05-22 14:50:54 -04007735static pci_ers_result_t
7736lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007737{
James Smart51ef4c22007-08-02 11:10:31 -04007738 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7739 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007740 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007741 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007742
7743 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007744 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007745 printk(KERN_ERR "lpfc: Cannot re-enable "
7746 "PCI device after reset.\n");
7747 return PCI_ERS_RESULT_DISCONNECT;
7748 }
7749
James Smart97207482008-12-04 22:39:19 -05007750 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05007751
7752 /*
7753 * As the new kernel behavior of pci_restore_state() API call clears
7754 * device saved_state flag, need to save the restored state again.
7755 */
7756 pci_save_state(pdev);
7757
James Smart97207482008-12-04 22:39:19 -05007758 if (pdev->is_busmaster)
7759 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007760
James Smart92d7f7b2007-06-17 19:56:38 -05007761 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007762 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007763 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007764
James Smart5b75da22008-12-04 22:39:35 -05007765 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007766 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007767 if (intr_mode == LPFC_INTR_ERROR) {
7768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7769 "0427 Cannot re-enable interrupt after "
7770 "slot reset.\n");
7771 return PCI_ERS_RESULT_DISCONNECT;
7772 } else
7773 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007774
7775 /* Take device offline; this will perform cleanup */
7776 lpfc_offline(phba);
7777 lpfc_sli_brdrestart(phba);
7778
James Smart5b75da22008-12-04 22:39:35 -05007779 /* Log the current active interrupt mode */
7780 lpfc_log_intr_mode(phba, phba->intr_mode);
7781
Linas Vepstas8d63f372007-02-14 14:28:36 -06007782 return PCI_ERS_RESULT_RECOVERED;
7783}
7784
7785/**
James Smart3772a992009-05-22 14:50:54 -04007786 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007787 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007788 *
James Smart3772a992009-05-22 14:50:54 -04007789 * This routine is called from the PCI subsystem for error handling to device
7790 * with SLI-3 interface spec. It is called when kernel error recovery tells
7791 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7792 * error recovery. After this call, traffic can start to flow from this device
7793 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007794 */
James Smart3772a992009-05-22 14:50:54 -04007795static void
7796lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007797{
James Smart51ef4c22007-08-02 11:10:31 -04007798 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7799 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007800
James Smart0d878412009-10-02 15:16:56 -04007801 /* Bring the device online */
James Smart58da1ff2008-04-07 10:15:56 -04007802 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04007803
7804 /* Clean up Advanced Error Reporting (AER) if needed */
7805 if (phba->hba_flag & HBA_AER_ENABLED)
7806 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007807}
7808
James Smart3772a992009-05-22 14:50:54 -04007809/**
James Smartda0436e2009-05-22 14:51:39 -04007810 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7811 * @phba: pointer to lpfc hba data structure.
7812 *
7813 * returns the number of ELS/CT IOCBs to reserve
7814 **/
7815int
7816lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7817{
7818 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7819
James Smartf1126682009-06-10 17:22:44 -04007820 if (phba->sli_rev == LPFC_SLI_REV4) {
7821 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04007822 return 10;
James Smartf1126682009-06-10 17:22:44 -04007823 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04007824 return 25;
James Smartf1126682009-06-10 17:22:44 -04007825 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04007826 return 50;
James Smartf1126682009-06-10 17:22:44 -04007827 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04007828 return 100;
James Smartf1126682009-06-10 17:22:44 -04007829 else
James Smart6a9c52c2009-10-02 15:16:51 -04007830 return 150;
James Smartf1126682009-06-10 17:22:44 -04007831 } else
7832 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007833}
7834
7835/**
7836 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7837 * @pdev: pointer to PCI device
7838 * @pid: pointer to PCI device identifier
7839 *
7840 * This routine is called from the kernel's PCI subsystem to device with
7841 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7842 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7843 * information of the device and driver to see if the driver state that it
7844 * can support this kind of device. If the match is successful, the driver
7845 * core invokes this routine. If this routine determines it can claim the HBA,
7846 * it does all the initialization that it needs to do to handle the HBA
7847 * properly.
7848 *
7849 * Return code
7850 * 0 - driver can claim the device
7851 * negative value - driver can not claim the device
7852 **/
7853static int __devinit
7854lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7855{
7856 struct lpfc_hba *phba;
7857 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007858 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007859 int error;
7860 uint32_t cfg_mode, intr_mode;
7861 int mcnt;
7862
7863 /* Allocate memory for HBA structure */
7864 phba = lpfc_hba_alloc(pdev);
7865 if (!phba)
7866 return -ENOMEM;
7867
7868 /* Perform generic PCI device enabling operation */
7869 error = lpfc_enable_pci_dev(phba);
7870 if (error) {
7871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7872 "1409 Failed to enable pci device.\n");
7873 goto out_free_phba;
7874 }
7875
7876 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7877 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7878 if (error)
7879 goto out_disable_pci_dev;
7880
7881 /* Set up SLI-4 specific device PCI memory space */
7882 error = lpfc_sli4_pci_mem_setup(phba);
7883 if (error) {
7884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7885 "1410 Failed to set up pci memory space.\n");
7886 goto out_disable_pci_dev;
7887 }
7888
7889 /* Set up phase-1 common device driver resources */
7890 error = lpfc_setup_driver_resource_phase1(phba);
7891 if (error) {
7892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7893 "1411 Failed to set up driver resource.\n");
7894 goto out_unset_pci_mem_s4;
7895 }
7896
7897 /* Set up SLI-4 Specific device driver resources */
7898 error = lpfc_sli4_driver_resource_setup(phba);
7899 if (error) {
7900 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7901 "1412 Failed to set up driver resource.\n");
7902 goto out_unset_pci_mem_s4;
7903 }
7904
7905 /* Initialize and populate the iocb list per host */
7906 error = lpfc_init_iocb_list(phba,
7907 phba->sli4_hba.max_cfg_param.max_xri);
7908 if (error) {
7909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7910 "1413 Failed to initialize iocb list.\n");
7911 goto out_unset_driver_resource_s4;
7912 }
7913
7914 /* Set up common device driver resources */
7915 error = lpfc_setup_driver_resource_phase2(phba);
7916 if (error) {
7917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7918 "1414 Failed to set up driver resource.\n");
7919 goto out_free_iocb_list;
7920 }
7921
7922 /* Create SCSI host to the physical port */
7923 error = lpfc_create_shost(phba);
7924 if (error) {
7925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7926 "1415 Failed to create scsi host.\n");
7927 goto out_unset_driver_resource;
7928 }
7929
7930 /* Configure sysfs attributes */
7931 vport = phba->pport;
7932 error = lpfc_alloc_sysfs_attr(vport);
7933 if (error) {
7934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7935 "1416 Failed to allocate sysfs attr\n");
7936 goto out_destroy_shost;
7937 }
7938
James Smart6669f9b2009-10-02 15:16:45 -04007939 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04007940 /* Now, trying to enable interrupt and bring up the device */
7941 cfg_mode = phba->cfg_use_msi;
7942 while (true) {
7943 /* Put device to a known state before enabling interrupt */
7944 lpfc_stop_port(phba);
7945 /* Configure and enable interrupt */
7946 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7947 if (intr_mode == LPFC_INTR_ERROR) {
7948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7949 "0426 Failed to enable interrupt.\n");
7950 error = -ENODEV;
7951 goto out_free_sysfs_attr;
7952 }
James Smartdef9c7a2009-12-21 17:02:28 -05007953 /* Default to single FCP EQ for non-MSI-X */
7954 if (phba->intr_type != MSIX)
7955 phba->cfg_fcp_eq_count = 1;
James Smartda0436e2009-05-22 14:51:39 -04007956 /* Set up SLI-4 HBA */
7957 if (lpfc_sli4_hba_setup(phba)) {
7958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7959 "1421 Failed to set up hba\n");
7960 error = -ENODEV;
7961 goto out_disable_intr;
7962 }
7963
7964 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7965 if (intr_mode != 0)
7966 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7967 LPFC_ACT_INTR_CNT);
7968
7969 /* Check active interrupts received only for MSI/MSI-X */
7970 if (intr_mode == 0 ||
7971 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7972 /* Log the current active interrupt mode */
7973 phba->intr_mode = intr_mode;
7974 lpfc_log_intr_mode(phba, intr_mode);
7975 break;
7976 }
7977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7978 "0451 Configure interrupt mode (%d) "
7979 "failed active interrupt test.\n",
7980 intr_mode);
7981 /* Unset the preivous SLI-4 HBA setup */
7982 lpfc_sli4_unset_hba(phba);
7983 /* Try next level of interrupt mode */
7984 cfg_mode = --intr_mode;
7985 }
7986
7987 /* Perform post initialization setup */
7988 lpfc_post_init_setup(phba);
7989
James Smart1c6834a2009-07-19 10:01:26 -04007990 /* Check if there are static vports to be created. */
7991 lpfc_create_static_vport(phba);
7992
James Smartda0436e2009-05-22 14:51:39 -04007993 return 0;
7994
7995out_disable_intr:
7996 lpfc_sli4_disable_intr(phba);
7997out_free_sysfs_attr:
7998 lpfc_free_sysfs_attr(vport);
7999out_destroy_shost:
8000 lpfc_destroy_shost(phba);
8001out_unset_driver_resource:
8002 lpfc_unset_driver_resource_phase2(phba);
8003out_free_iocb_list:
8004 lpfc_free_iocb_list(phba);
8005out_unset_driver_resource_s4:
8006 lpfc_sli4_driver_resource_unset(phba);
8007out_unset_pci_mem_s4:
8008 lpfc_sli4_pci_mem_unset(phba);
8009out_disable_pci_dev:
8010 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008011 if (shost)
8012 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04008013out_free_phba:
8014 lpfc_hba_free(phba);
8015 return error;
8016}
8017
8018/**
8019 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8020 * @pdev: pointer to PCI device
8021 *
8022 * This routine is called from the kernel's PCI subsystem to device with
8023 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8024 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8025 * device to be removed from the PCI subsystem properly.
8026 **/
8027static void __devexit
8028lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8029{
8030 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8031 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8032 struct lpfc_vport **vports;
8033 struct lpfc_hba *phba = vport->phba;
8034 int i;
8035
8036 /* Mark the device unloading flag */
8037 spin_lock_irq(&phba->hbalock);
8038 vport->load_flag |= FC_UNLOADING;
8039 spin_unlock_irq(&phba->hbalock);
8040
8041 /* Free the HBA sysfs attributes */
8042 lpfc_free_sysfs_attr(vport);
8043
8044 /* Release all the vports against this physical port */
8045 vports = lpfc_create_vport_work_array(phba);
8046 if (vports != NULL)
8047 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8048 fc_vport_terminate(vports[i]->fc_vport);
8049 lpfc_destroy_vport_work_array(phba, vports);
8050
8051 /* Remove FC host and then SCSI host with the physical port */
8052 fc_remove_host(shost);
8053 scsi_remove_host(shost);
8054
8055 /* Perform cleanup on the physical port */
8056 lpfc_cleanup(vport);
8057
8058 /*
8059 * Bring down the SLI Layer. This step disables all interrupts,
8060 * clears the rings, discards all mailbox commands, and resets
8061 * the HBA FCoE function.
8062 */
8063 lpfc_debugfs_terminate(vport);
8064 lpfc_sli4_hba_unset(phba);
8065
8066 spin_lock_irq(&phba->hbalock);
8067 list_del_init(&vport->listentry);
8068 spin_unlock_irq(&phba->hbalock);
8069
8070 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
8071 * buffers are released to their corresponding pools here.
8072 */
8073 lpfc_scsi_free(phba);
8074 lpfc_sli4_driver_resource_unset(phba);
8075
8076 /* Unmap adapter Control and Doorbell registers */
8077 lpfc_sli4_pci_mem_unset(phba);
8078
8079 /* Release PCI resources and disable device's PCI function */
8080 scsi_host_put(shost);
8081 lpfc_disable_pci_dev(phba);
8082
8083 /* Finally, free the driver's device data structure */
8084 lpfc_hba_free(phba);
8085
8086 return;
8087}
8088
8089/**
8090 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
8091 * @pdev: pointer to PCI device
8092 * @msg: power management message
8093 *
8094 * This routine is called from the kernel's PCI subsystem to support system
8095 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8096 * this method, it quiesces the device by stopping the driver's worker
8097 * thread for the device, turning off device's interrupt and DMA, and bring
8098 * the device offline. Note that as the driver implements the minimum PM
8099 * requirements to a power-aware driver's PM support for suspend/resume -- all
8100 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8101 * method call will be treated as SUSPEND and the driver will fully
8102 * reinitialize its device during resume() method call, the driver will set
8103 * device to PCI_D3hot state in PCI config space instead of setting it
8104 * according to the @msg provided by the PM.
8105 *
8106 * Return code
8107 * 0 - driver suspended the device
8108 * Error otherwise
8109 **/
8110static int
8111lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
8112{
8113 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8114 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8115
8116 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8117 "0298 PCI device Power Management suspend.\n");
8118
8119 /* Bring down the device */
8120 lpfc_offline_prep(phba);
8121 lpfc_offline(phba);
8122 kthread_stop(phba->worker_thread);
8123
8124 /* Disable interrupt from device */
8125 lpfc_sli4_disable_intr(phba);
8126
8127 /* Save device state to PCI config space */
8128 pci_save_state(pdev);
8129 pci_set_power_state(pdev, PCI_D3hot);
8130
8131 return 0;
8132}
8133
8134/**
8135 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8136 * @pdev: pointer to PCI device
8137 *
8138 * This routine is called from the kernel's PCI subsystem to support system
8139 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8140 * this method, it restores the device's PCI config space state and fully
8141 * reinitializes the device and brings it online. Note that as the driver
8142 * implements the minimum PM requirements to a power-aware driver's PM for
8143 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8144 * to the suspend() method call will be treated as SUSPEND and the driver
8145 * will fully reinitialize its device during resume() method call, the device
8146 * will be set to PCI_D0 directly in PCI config space before restoring the
8147 * state.
8148 *
8149 * Return code
8150 * 0 - driver suspended the device
8151 * Error otherwise
8152 **/
8153static int
8154lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8155{
8156 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8157 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8158 uint32_t intr_mode;
8159 int error;
8160
8161 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8162 "0292 PCI device Power Management resume.\n");
8163
8164 /* Restore device state from PCI config space */
8165 pci_set_power_state(pdev, PCI_D0);
8166 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008167
8168 /*
8169 * As the new kernel behavior of pci_restore_state() API call clears
8170 * device saved_state flag, need to save the restored state again.
8171 */
8172 pci_save_state(pdev);
8173
James Smartda0436e2009-05-22 14:51:39 -04008174 if (pdev->is_busmaster)
8175 pci_set_master(pdev);
8176
8177 /* Startup the kernel thread for this host adapter. */
8178 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8179 "lpfc_worker_%d", phba->brd_no);
8180 if (IS_ERR(phba->worker_thread)) {
8181 error = PTR_ERR(phba->worker_thread);
8182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8183 "0293 PM resume failed to start worker "
8184 "thread: error=x%x.\n", error);
8185 return error;
8186 }
8187
8188 /* Configure and enable interrupt */
8189 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8190 if (intr_mode == LPFC_INTR_ERROR) {
8191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8192 "0294 PM resume Failed to enable interrupt\n");
8193 return -EIO;
8194 } else
8195 phba->intr_mode = intr_mode;
8196
8197 /* Restart HBA and bring it online */
8198 lpfc_sli_brdrestart(phba);
8199 lpfc_online(phba);
8200
8201 /* Log the current active interrupt mode */
8202 lpfc_log_intr_mode(phba, phba->intr_mode);
8203
8204 return 0;
8205}
8206
8207/**
8208 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
8209 * @pdev: pointer to PCI device.
8210 * @state: the current PCI connection state.
8211 *
8212 * This routine is called from the PCI subsystem for error handling to device
8213 * with SLI-4 interface spec. This function is called by the PCI subsystem
8214 * after a PCI bus error affecting this device has been detected. When this
8215 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8216 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8217 * for the PCI subsystem to perform proper recovery as desired.
8218 *
8219 * Return codes
8220 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8221 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8222 **/
8223static pci_ers_result_t
8224lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8225{
8226 return PCI_ERS_RESULT_NEED_RESET;
8227}
8228
8229/**
8230 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
8231 * @pdev: pointer to PCI device.
8232 *
8233 * This routine is called from the PCI subsystem for error handling to device
8234 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8235 * restart the PCI card from scratch, as if from a cold-boot. During the
8236 * PCI subsystem error recovery, after the driver returns
8237 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8238 * recovery and then call this routine before calling the .resume method to
8239 * recover the device. This function will initialize the HBA device, enable
8240 * the interrupt, but it will just put the HBA to offline state without
8241 * passing any I/O traffic.
8242 *
8243 * Return codes
8244 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8245 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8246 */
8247static pci_ers_result_t
8248lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8249{
8250 return PCI_ERS_RESULT_RECOVERED;
8251}
8252
8253/**
8254 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8255 * @pdev: pointer to PCI device
8256 *
8257 * This routine is called from the PCI subsystem for error handling to device
8258 * with SLI-4 interface spec. It is called when kernel error recovery tells
8259 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8260 * error recovery. After this call, traffic can start to flow from this device
8261 * again.
8262 **/
8263static void
8264lpfc_io_resume_s4(struct pci_dev *pdev)
8265{
8266 return;
8267}
8268
8269/**
James Smart3772a992009-05-22 14:50:54 -04008270 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8271 * @pdev: pointer to PCI device
8272 * @pid: pointer to PCI device identifier
8273 *
8274 * This routine is to be registered to the kernel's PCI subsystem. When an
8275 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8276 * at PCI device-specific information of the device and driver to see if the
8277 * driver state that it can support this kind of device. If the match is
8278 * successful, the driver core invokes this routine. This routine dispatches
8279 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8280 * do all the initialization that it needs to do to handle the HBA device
8281 * properly.
8282 *
8283 * Return code
8284 * 0 - driver can claim the device
8285 * negative value - driver can not claim the device
8286 **/
8287static int __devinit
8288lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8289{
8290 int rc;
James Smart8fa38512009-07-19 10:01:03 -04008291 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04008292
James Smart28baac72010-02-12 14:42:03 -05008293 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04008294 return -ENODEV;
8295
James Smart8fa38512009-07-19 10:01:03 -04008296 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05008297 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04008298 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008299 else
James Smart3772a992009-05-22 14:50:54 -04008300 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008301
James Smart3772a992009-05-22 14:50:54 -04008302 return rc;
8303}
8304
8305/**
8306 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8307 * @pdev: pointer to PCI device
8308 *
8309 * This routine is to be registered to the kernel's PCI subsystem. When an
8310 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8311 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8312 * remove routine, which will perform all the necessary cleanup for the
8313 * device to be removed from the PCI subsystem properly.
8314 **/
8315static void __devexit
8316lpfc_pci_remove_one(struct pci_dev *pdev)
8317{
8318 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8319 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8320
8321 switch (phba->pci_dev_grp) {
8322 case LPFC_PCI_DEV_LP:
8323 lpfc_pci_remove_one_s3(pdev);
8324 break;
James Smartda0436e2009-05-22 14:51:39 -04008325 case LPFC_PCI_DEV_OC:
8326 lpfc_pci_remove_one_s4(pdev);
8327 break;
James Smart3772a992009-05-22 14:50:54 -04008328 default:
8329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8330 "1424 Invalid PCI device group: 0x%x\n",
8331 phba->pci_dev_grp);
8332 break;
8333 }
8334 return;
8335}
8336
8337/**
8338 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8339 * @pdev: pointer to PCI device
8340 * @msg: power management message
8341 *
8342 * This routine is to be registered to the kernel's PCI subsystem to support
8343 * system Power Management (PM). When PM invokes this method, it dispatches
8344 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8345 * suspend the device.
8346 *
8347 * Return code
8348 * 0 - driver suspended the device
8349 * Error otherwise
8350 **/
8351static int
8352lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8353{
8354 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8355 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8356 int rc = -ENODEV;
8357
8358 switch (phba->pci_dev_grp) {
8359 case LPFC_PCI_DEV_LP:
8360 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8361 break;
James Smartda0436e2009-05-22 14:51:39 -04008362 case LPFC_PCI_DEV_OC:
8363 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8364 break;
James Smart3772a992009-05-22 14:50:54 -04008365 default:
8366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8367 "1425 Invalid PCI device group: 0x%x\n",
8368 phba->pci_dev_grp);
8369 break;
8370 }
8371 return rc;
8372}
8373
8374/**
8375 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8376 * @pdev: pointer to PCI device
8377 *
8378 * This routine is to be registered to the kernel's PCI subsystem to support
8379 * system Power Management (PM). When PM invokes this method, it dispatches
8380 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8381 * resume the device.
8382 *
8383 * Return code
8384 * 0 - driver suspended the device
8385 * Error otherwise
8386 **/
8387static int
8388lpfc_pci_resume_one(struct pci_dev *pdev)
8389{
8390 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8391 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8392 int rc = -ENODEV;
8393
8394 switch (phba->pci_dev_grp) {
8395 case LPFC_PCI_DEV_LP:
8396 rc = lpfc_pci_resume_one_s3(pdev);
8397 break;
James Smartda0436e2009-05-22 14:51:39 -04008398 case LPFC_PCI_DEV_OC:
8399 rc = lpfc_pci_resume_one_s4(pdev);
8400 break;
James Smart3772a992009-05-22 14:50:54 -04008401 default:
8402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8403 "1426 Invalid PCI device group: 0x%x\n",
8404 phba->pci_dev_grp);
8405 break;
8406 }
8407 return rc;
8408}
8409
8410/**
8411 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8412 * @pdev: pointer to PCI device.
8413 * @state: the current PCI connection state.
8414 *
8415 * This routine is registered to the PCI subsystem for error handling. This
8416 * function is called by the PCI subsystem after a PCI bus error affecting
8417 * this device has been detected. When this routine is invoked, it dispatches
8418 * the action to the proper SLI-3 or SLI-4 device error detected handling
8419 * routine, which will perform the proper error detected operation.
8420 *
8421 * Return codes
8422 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8423 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8424 **/
8425static pci_ers_result_t
8426lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8427{
8428 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8429 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8430 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8431
8432 switch (phba->pci_dev_grp) {
8433 case LPFC_PCI_DEV_LP:
8434 rc = lpfc_io_error_detected_s3(pdev, state);
8435 break;
James Smartda0436e2009-05-22 14:51:39 -04008436 case LPFC_PCI_DEV_OC:
8437 rc = lpfc_io_error_detected_s4(pdev, state);
8438 break;
James Smart3772a992009-05-22 14:50:54 -04008439 default:
8440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8441 "1427 Invalid PCI device group: 0x%x\n",
8442 phba->pci_dev_grp);
8443 break;
8444 }
8445 return rc;
8446}
8447
8448/**
8449 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8450 * @pdev: pointer to PCI device.
8451 *
8452 * This routine is registered to the PCI subsystem for error handling. This
8453 * function is called after PCI bus has been reset to restart the PCI card
8454 * from scratch, as if from a cold-boot. When this routine is invoked, it
8455 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8456 * routine, which will perform the proper device reset.
8457 *
8458 * Return codes
8459 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8460 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8461 **/
8462static pci_ers_result_t
8463lpfc_io_slot_reset(struct pci_dev *pdev)
8464{
8465 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8466 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8467 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8468
8469 switch (phba->pci_dev_grp) {
8470 case LPFC_PCI_DEV_LP:
8471 rc = lpfc_io_slot_reset_s3(pdev);
8472 break;
James Smartda0436e2009-05-22 14:51:39 -04008473 case LPFC_PCI_DEV_OC:
8474 rc = lpfc_io_slot_reset_s4(pdev);
8475 break;
James Smart3772a992009-05-22 14:50:54 -04008476 default:
8477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8478 "1428 Invalid PCI device group: 0x%x\n",
8479 phba->pci_dev_grp);
8480 break;
8481 }
8482 return rc;
8483}
8484
8485/**
8486 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8487 * @pdev: pointer to PCI device
8488 *
8489 * This routine is registered to the PCI subsystem for error handling. It
8490 * is called when kernel error recovery tells the lpfc driver that it is
8491 * OK to resume normal PCI operation after PCI bus error recovery. When
8492 * this routine is invoked, it dispatches the action to the proper SLI-3
8493 * or SLI-4 device io_resume routine, which will resume the device operation.
8494 **/
8495static void
8496lpfc_io_resume(struct pci_dev *pdev)
8497{
8498 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8499 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8500
8501 switch (phba->pci_dev_grp) {
8502 case LPFC_PCI_DEV_LP:
8503 lpfc_io_resume_s3(pdev);
8504 break;
James Smartda0436e2009-05-22 14:51:39 -04008505 case LPFC_PCI_DEV_OC:
8506 lpfc_io_resume_s4(pdev);
8507 break;
James Smart3772a992009-05-22 14:50:54 -04008508 default:
8509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8510 "1429 Invalid PCI device group: 0x%x\n",
8511 phba->pci_dev_grp);
8512 break;
8513 }
8514 return;
8515}
8516
dea31012005-04-17 16:05:31 -05008517static struct pci_device_id lpfc_id_table[] = {
8518 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8519 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008520 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8521 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008522 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8523 PCI_ANY_ID, PCI_ANY_ID, },
8524 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8525 PCI_ANY_ID, PCI_ANY_ID, },
8526 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8527 PCI_ANY_ID, PCI_ANY_ID, },
8528 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8529 PCI_ANY_ID, PCI_ANY_ID, },
8530 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8531 PCI_ANY_ID, PCI_ANY_ID, },
8532 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8533 PCI_ANY_ID, PCI_ANY_ID, },
8534 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8535 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008536 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8537 PCI_ANY_ID, PCI_ANY_ID, },
8538 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8539 PCI_ANY_ID, PCI_ANY_ID, },
8540 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8541 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008542 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8543 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008544 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8545 PCI_ANY_ID, PCI_ANY_ID, },
8546 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8547 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008548 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8549 PCI_ANY_ID, PCI_ANY_ID, },
8550 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8551 PCI_ANY_ID, PCI_ANY_ID, },
8552 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8553 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008554 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8555 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008556 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8557 PCI_ANY_ID, PCI_ANY_ID, },
8558 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8559 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008560 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8561 PCI_ANY_ID, PCI_ANY_ID, },
8562 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8563 PCI_ANY_ID, PCI_ANY_ID, },
8564 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8565 PCI_ANY_ID, PCI_ANY_ID, },
8566 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8567 PCI_ANY_ID, PCI_ANY_ID, },
8568 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8569 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008570 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8571 PCI_ANY_ID, PCI_ANY_ID, },
8572 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8573 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008574 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8575 PCI_ANY_ID, PCI_ANY_ID, },
8576 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8577 PCI_ANY_ID, PCI_ANY_ID, },
8578 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8579 PCI_ANY_ID, PCI_ANY_ID, },
8580 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8581 PCI_ANY_ID, PCI_ANY_ID, },
8582 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8583 PCI_ANY_ID, PCI_ANY_ID, },
8584 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8585 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008586 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8587 PCI_ANY_ID, PCI_ANY_ID, },
8588 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8589 PCI_ANY_ID, PCI_ANY_ID, },
8590 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8591 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008592 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8593 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05008594 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8595 PCI_ANY_ID, PCI_ANY_ID, },
8596 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04008597 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008598 { 0 }
8599};
8600
8601MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8602
Linas Vepstas8d63f372007-02-14 14:28:36 -06008603static struct pci_error_handlers lpfc_err_handler = {
8604 .error_detected = lpfc_io_error_detected,
8605 .slot_reset = lpfc_io_slot_reset,
8606 .resume = lpfc_io_resume,
8607};
8608
dea31012005-04-17 16:05:31 -05008609static struct pci_driver lpfc_driver = {
8610 .name = LPFC_DRIVER_NAME,
8611 .id_table = lpfc_id_table,
8612 .probe = lpfc_pci_probe_one,
8613 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008614 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008615 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008616 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008617};
8618
James Smarte59058c2008-08-24 21:49:00 -04008619/**
James Smart3621a712009-04-06 18:47:14 -04008620 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008621 *
8622 * This routine is to be invoked when the lpfc module is loaded into the
8623 * kernel. The special kernel macro module_init() is used to indicate the
8624 * role of this routine to the kernel as lpfc module entry point.
8625 *
8626 * Return codes
8627 * 0 - successful
8628 * -ENOMEM - FC attach transport failed
8629 * all others - failed
8630 */
dea31012005-04-17 16:05:31 -05008631static int __init
8632lpfc_init(void)
8633{
8634 int error = 0;
8635
8636 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008637 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008638
James Smart7ee5d432007-10-27 13:37:17 -04008639 if (lpfc_enable_npiv) {
8640 lpfc_transport_functions.vport_create = lpfc_vport_create;
8641 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8642 }
dea31012005-04-17 16:05:31 -05008643 lpfc_transport_template =
8644 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008645 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008646 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008647 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008648 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008649 fc_attach_transport(&lpfc_vport_transport_functions);
8650 if (lpfc_vport_transport_template == NULL) {
8651 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008652 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008653 }
James Smart7ee5d432007-10-27 13:37:17 -04008654 }
dea31012005-04-17 16:05:31 -05008655 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008656 if (error) {
dea31012005-04-17 16:05:31 -05008657 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008658 if (lpfc_enable_npiv)
8659 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008660 }
dea31012005-04-17 16:05:31 -05008661
8662 return error;
8663}
8664
James Smarte59058c2008-08-24 21:49:00 -04008665/**
James Smart3621a712009-04-06 18:47:14 -04008666 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008667 *
8668 * This routine is invoked when the lpfc module is removed from the kernel.
8669 * The special kernel macro module_exit() is used to indicate the role of
8670 * this routine to the kernel as lpfc module exit point.
8671 */
dea31012005-04-17 16:05:31 -05008672static void __exit
8673lpfc_exit(void)
8674{
8675 pci_unregister_driver(&lpfc_driver);
8676 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008677 if (lpfc_enable_npiv)
8678 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008679 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008680 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8681 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008682 (1L << _dump_buf_data_order), _dump_buf_data);
8683 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8684 }
8685
8686 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008687 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8688 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008689 (1L << _dump_buf_dif_order), _dump_buf_dif);
8690 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8691 }
dea31012005-04-17 16:05:31 -05008692}
8693
8694module_init(lpfc_init);
8695module_exit(lpfc_exit);
8696MODULE_LICENSE("GPL");
8697MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8698MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8699MODULE_VERSION("0:" LPFC_DRIVER_VERSION);