blob: b7889c53fe231b17ab6ba5326f8f3178ce463c2d [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 Smart47a86172007-04-25 09:53:22 -04002600
James Bottomleyd139b9b2009-11-05 13:33:12 -06002601 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002602 if (error)
2603 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002604
James Smart549e55c2007-08-02 11:09:51 -04002605 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002606 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002607 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002608 return vport;
James Smart47a86172007-04-25 09:53:22 -04002609
James Smart2e0fef82007-06-17 19:56:36 -05002610out_put_shost:
2611 scsi_host_put(shost);
2612out:
2613 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002614}
2615
James Smarte59058c2008-08-24 21:49:00 -04002616/**
James Smart3621a712009-04-06 18:47:14 -04002617 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002618 * @vport: pointer to an lpfc virtual N_Port data structure.
2619 *
2620 * This routine destroys a FC port from the upper layer protocol. All the
2621 * resources associated with the port are released.
2622 **/
James Smart2e0fef82007-06-17 19:56:36 -05002623void
2624destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002625{
James Smart92d7f7b2007-06-17 19:56:38 -05002626 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2627 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002628
James Smart858c9f62007-06-17 19:56:39 -05002629 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002630 fc_remove_host(shost);
2631 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002632
James Smart92d7f7b2007-06-17 19:56:38 -05002633 spin_lock_irq(&phba->hbalock);
2634 list_del_init(&vport->listentry);
2635 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002636
James Smart92d7f7b2007-06-17 19:56:38 -05002637 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002638 return;
James Smart47a86172007-04-25 09:53:22 -04002639}
2640
James Smarte59058c2008-08-24 21:49:00 -04002641/**
James Smart3621a712009-04-06 18:47:14 -04002642 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002643 *
2644 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2645 * uses the kernel idr facility to perform the task.
2646 *
2647 * Return codes:
2648 * instance - a unique integer ID allocated as the new instance.
2649 * -1 - lpfc get instance failed.
2650 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002651int
2652lpfc_get_instance(void)
2653{
2654 int instance = 0;
2655
2656 /* Assign an unused number */
2657 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2658 return -1;
2659 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2660 return -1;
2661 return instance;
2662}
2663
James Smarte59058c2008-08-24 21:49:00 -04002664/**
James Smart3621a712009-04-06 18:47:14 -04002665 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002666 * @shost: pointer to SCSI host data structure.
2667 * @time: elapsed time of the scan in jiffies.
2668 *
2669 * This routine is called by the SCSI layer with a SCSI host to determine
2670 * whether the scan host is finished.
2671 *
2672 * Note: there is no scan_start function as adapter initialization will have
2673 * asynchronously kicked off the link initialization.
2674 *
2675 * Return codes
2676 * 0 - SCSI host scan is not over yet.
2677 * 1 - SCSI host scan is over.
2678 **/
James Smart47a86172007-04-25 09:53:22 -04002679int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2680{
James Smart2e0fef82007-06-17 19:56:36 -05002681 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2682 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002683 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002684
James Smart858c9f62007-06-17 19:56:39 -05002685 spin_lock_irq(shost->host_lock);
2686
James Smart51ef4c22007-08-02 11:10:31 -04002687 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002688 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002689 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002690 }
James Smart2e0fef82007-06-17 19:56:36 -05002691 if (time >= 30 * HZ) {
2692 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002693 "0461 Scanning longer than 30 "
2694 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002695 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002696 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002697 }
2698 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2699 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002700 "0465 Link down longer than 15 "
2701 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002702 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002703 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002704 }
2705
James Smart2e0fef82007-06-17 19:56:36 -05002706 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002707 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002708 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002709 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002710 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002711 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002712 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002713 goto finished;
2714
2715 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002716
2717finished:
James Smart858c9f62007-06-17 19:56:39 -05002718 spin_unlock_irq(shost->host_lock);
2719 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002720}
2721
James Smarte59058c2008-08-24 21:49:00 -04002722/**
James Smart3621a712009-04-06 18:47:14 -04002723 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002724 * @shost: pointer to SCSI host data structure.
2725 *
2726 * This routine initializes a given SCSI host attributes on a FC port. The
2727 * SCSI host can be either on top of a physical port or a virtual port.
2728 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002729void lpfc_host_attrib_init(struct Scsi_Host *shost)
2730{
2731 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2732 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002733 /*
James Smart2e0fef82007-06-17 19:56:36 -05002734 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002735 */
2736
James Smart2e0fef82007-06-17 19:56:36 -05002737 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2738 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002739 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2740
2741 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002742 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002743 fc_host_supported_fc4s(shost)[2] = 1;
2744 fc_host_supported_fc4s(shost)[7] = 1;
2745
James Smart92d7f7b2007-06-17 19:56:38 -05002746 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2747 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002748
2749 fc_host_supported_speeds(shost) = 0;
2750 if (phba->lmt & LMT_10Gb)
2751 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002752 if (phba->lmt & LMT_8Gb)
2753 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002754 if (phba->lmt & LMT_4Gb)
2755 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2756 if (phba->lmt & LMT_2Gb)
2757 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2758 if (phba->lmt & LMT_1Gb)
2759 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2760
2761 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002762 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2763 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002764
2765 /* This value is also unchanging */
2766 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002767 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002768 fc_host_active_fc4s(shost)[2] = 1;
2769 fc_host_active_fc4s(shost)[7] = 1;
2770
James Smart92d7f7b2007-06-17 19:56:38 -05002771 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002772 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002773 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002774 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002775}
dea31012005-04-17 16:05:31 -05002776
James Smarte59058c2008-08-24 21:49:00 -04002777/**
James Smartda0436e2009-05-22 14:51:39 -04002778 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002779 * @phba: pointer to lpfc hba data structure.
2780 *
James Smartda0436e2009-05-22 14:51:39 -04002781 * This routine is invoked to stop an SLI3 device port, it stops the device
2782 * from generating interrupts and stops the device driver's timers for the
2783 * device.
James Smarte59058c2008-08-24 21:49:00 -04002784 **/
James Smartdb2378e2008-02-08 18:49:51 -05002785static void
James Smartda0436e2009-05-22 14:51:39 -04002786lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002787{
James Smartda0436e2009-05-22 14:51:39 -04002788 /* Clear all interrupt enable conditions */
2789 writel(0, phba->HCregaddr);
2790 readl(phba->HCregaddr); /* flush */
2791 /* Clear all pending interrupts */
2792 writel(0xffffffff, phba->HAregaddr);
2793 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002794
James Smartda0436e2009-05-22 14:51:39 -04002795 /* Reset some HBA SLI setup states */
2796 lpfc_stop_hba_timers(phba);
2797 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002798}
2799
James Smarte59058c2008-08-24 21:49:00 -04002800/**
James Smartda0436e2009-05-22 14:51:39 -04002801 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002802 * @phba: pointer to lpfc hba data structure.
2803 *
James Smartda0436e2009-05-22 14:51:39 -04002804 * This routine is invoked to stop an SLI4 device port, it stops the device
2805 * from generating interrupts and stops the device driver's timers for the
2806 * device.
2807 **/
2808static void
2809lpfc_stop_port_s4(struct lpfc_hba *phba)
2810{
2811 /* Reset some HBA SLI4 setup states */
2812 lpfc_stop_hba_timers(phba);
2813 phba->pport->work_port_events = 0;
2814 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04002815}
2816
2817/**
2818 * lpfc_stop_port - Wrapper function for stopping hba port
2819 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002820 *
James Smartda0436e2009-05-22 14:51:39 -04002821 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2822 * the API jump table function pointer from the lpfc_hba struct.
2823 **/
2824void
2825lpfc_stop_port(struct lpfc_hba *phba)
2826{
2827 phba->lpfc_stop_port(phba);
2828}
2829
2830/**
2831 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2832 * @phba: pointer to lpfc hba data structure.
2833 *
2834 * This routine is invoked to remove the driver default fcf record from
2835 * the port. This routine currently acts on FCF Index 0.
2836 *
2837 **/
2838void
2839lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2840{
2841 int rc = 0;
2842 LPFC_MBOXQ_t *mboxq;
2843 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2844 uint32_t mbox_tmo, req_len;
2845 uint32_t shdr_status, shdr_add_status;
2846
2847 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2848 if (!mboxq) {
2849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2850 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2851 return;
2852 }
2853
2854 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2855 sizeof(struct lpfc_sli4_cfg_mhdr);
2856 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2857 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2858 req_len, LPFC_SLI4_MBX_EMBED);
2859 /*
2860 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2861 * supports multiple FCF indices.
2862 */
2863 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2864 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2865 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
James Smartecfd03c2010-02-12 14:41:27 -05002866 phba->fcf.current_rec.fcf_indx);
James Smartda0436e2009-05-22 14:51:39 -04002867
2868 if (!phba->sli4_hba.intr_enable)
2869 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2870 else {
2871 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2872 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2873 }
2874 /* The IOCTL status is embedded in the mailbox subheader. */
2875 shdr_status = bf_get(lpfc_mbox_hdr_status,
2876 &del_fcf_record->header.cfg_shdr.response);
2877 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2878 &del_fcf_record->header.cfg_shdr.response);
2879 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2881 "2516 DEL FCF of default FCF Index failed "
2882 "mbx status x%x, status x%x add_status x%x\n",
2883 rc, shdr_status, shdr_add_status);
2884 }
2885 if (rc != MBX_TIMEOUT)
2886 mempool_free(mboxq, phba->mbox_mem_pool);
2887}
2888
2889/**
James Smartecfd03c2010-02-12 14:41:27 -05002890 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2891 * @phba: Pointer to hba for which this call is being executed.
2892 *
2893 * This routine starts the timer waiting for the FCF rediscovery to complete.
2894 **/
2895void
2896lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2897{
2898 unsigned long fcf_redisc_wait_tmo =
2899 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2900 /* Start fcf rediscovery wait period timer */
2901 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2902 spin_lock_irq(&phba->hbalock);
2903 /* Allow action to new fcf asynchronous event */
2904 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2905 /* Mark the FCF rediscovery pending state */
2906 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2907 spin_unlock_irq(&phba->hbalock);
2908}
2909
2910/**
2911 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2912 * @ptr: Map to lpfc_hba data structure pointer.
2913 *
2914 * This routine is invoked when waiting for FCF table rediscover has been
2915 * timed out. If new FCF record(s) has (have) been discovered during the
2916 * wait period, a new FCF event shall be added to the FCOE async event
2917 * list, and then worker thread shall be waked up for processing from the
2918 * worker thread context.
2919 **/
2920void
2921lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2922{
2923 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2924
2925 /* Don't send FCF rediscovery event if timer cancelled */
2926 spin_lock_irq(&phba->hbalock);
2927 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2928 spin_unlock_irq(&phba->hbalock);
2929 return;
2930 }
2931 /* Clear FCF rediscovery timer pending flag */
2932 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2933 /* FCF rediscovery event to worker thread */
2934 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2935 spin_unlock_irq(&phba->hbalock);
2936 /* wake up worker thread */
2937 lpfc_worker_wake_up(phba);
2938}
2939
2940/**
James Smart6669f9b2009-10-02 15:16:45 -04002941 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2942 * @phba: pointer to lpfc hba data structure.
2943 *
2944 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2945 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2946 * was successful and the firmware supports FCoE. Any other return indicates
2947 * a error. It is assumed that this function will be called before interrupts
2948 * are enabled.
2949 **/
2950static int
2951lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2952{
2953 int rc = 0;
2954 LPFC_MBOXQ_t *mboxq;
2955 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2956 uint32_t length;
2957 uint32_t shdr_status, shdr_add_status;
2958
2959 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2960 if (!mboxq) {
2961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2962 "2621 Failed to allocate mbox for "
2963 "query firmware config cmd\n");
2964 return -ENOMEM;
2965 }
2966 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2967 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2968 sizeof(struct lpfc_sli4_cfg_mhdr));
2969 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2970 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2971 length, LPFC_SLI4_MBX_EMBED);
2972 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2973 /* The IOCTL status is embedded in the mailbox subheader. */
2974 shdr_status = bf_get(lpfc_mbox_hdr_status,
2975 &query_fw_cfg->header.cfg_shdr.response);
2976 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2977 &query_fw_cfg->header.cfg_shdr.response);
2978 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2979 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2980 "2622 Query Firmware Config failed "
2981 "mbx status x%x, status x%x add_status x%x\n",
2982 rc, shdr_status, shdr_add_status);
2983 return -EINVAL;
2984 }
2985 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2986 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2987 "2623 FCoE Function not supported by firmware. "
2988 "Function mode = %08x\n",
2989 query_fw_cfg->function_mode);
2990 return -EINVAL;
2991 }
2992 if (rc != MBX_TIMEOUT)
2993 mempool_free(mboxq, phba->mbox_mem_pool);
2994 return 0;
2995}
2996
2997/**
James Smartda0436e2009-05-22 14:51:39 -04002998 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2999 * @phba: pointer to lpfc hba data structure.
3000 * @acqe_link: pointer to the async link completion queue entry.
3001 *
3002 * This routine is to parse the SLI4 link-attention link fault code and
3003 * translate it into the base driver's read link attention mailbox command
3004 * status.
3005 *
3006 * Return: Link-attention status in terms of base driver's coding.
3007 **/
3008static uint16_t
3009lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3010 struct lpfc_acqe_link *acqe_link)
3011{
3012 uint16_t latt_fault;
3013
3014 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3015 case LPFC_ASYNC_LINK_FAULT_NONE:
3016 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3017 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3018 latt_fault = 0;
3019 break;
3020 default:
3021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3022 "0398 Invalid link fault code: x%x\n",
3023 bf_get(lpfc_acqe_link_fault, acqe_link));
3024 latt_fault = MBXERR_ERROR;
3025 break;
3026 }
3027 return latt_fault;
3028}
3029
3030/**
3031 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3032 * @phba: pointer to lpfc hba data structure.
3033 * @acqe_link: pointer to the async link completion queue entry.
3034 *
3035 * This routine is to parse the SLI4 link attention type and translate it
3036 * into the base driver's link attention type coding.
3037 *
3038 * Return: Link attention type in terms of base driver's coding.
3039 **/
3040static uint8_t
3041lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3042 struct lpfc_acqe_link *acqe_link)
3043{
3044 uint8_t att_type;
3045
3046 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3047 case LPFC_ASYNC_LINK_STATUS_DOWN:
3048 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3049 att_type = AT_LINK_DOWN;
3050 break;
3051 case LPFC_ASYNC_LINK_STATUS_UP:
3052 /* Ignore physical link up events - wait for logical link up */
3053 att_type = AT_RESERVED;
3054 break;
3055 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3056 att_type = AT_LINK_UP;
3057 break;
3058 default:
3059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3060 "0399 Invalid link attention type: x%x\n",
3061 bf_get(lpfc_acqe_link_status, acqe_link));
3062 att_type = AT_RESERVED;
3063 break;
3064 }
3065 return att_type;
3066}
3067
3068/**
3069 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3070 * @phba: pointer to lpfc hba data structure.
3071 * @acqe_link: pointer to the async link completion queue entry.
3072 *
3073 * This routine is to parse the SLI4 link-attention link speed and translate
3074 * it into the base driver's link-attention link speed coding.
3075 *
3076 * Return: Link-attention link speed in terms of base driver's coding.
3077 **/
3078static uint8_t
3079lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3080 struct lpfc_acqe_link *acqe_link)
3081{
3082 uint8_t link_speed;
3083
3084 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3085 case LPFC_ASYNC_LINK_SPEED_ZERO:
3086 link_speed = LA_UNKNW_LINK;
3087 break;
3088 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3089 link_speed = LA_UNKNW_LINK;
3090 break;
3091 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3092 link_speed = LA_UNKNW_LINK;
3093 break;
3094 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3095 link_speed = LA_1GHZ_LINK;
3096 break;
3097 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3098 link_speed = LA_10GHZ_LINK;
3099 break;
3100 default:
3101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3102 "0483 Invalid link-attention link speed: x%x\n",
3103 bf_get(lpfc_acqe_link_speed, acqe_link));
3104 link_speed = LA_UNKNW_LINK;
3105 break;
3106 }
3107 return link_speed;
3108}
3109
3110/**
3111 * lpfc_sli4_async_link_evt - Process the asynchronous link event
3112 * @phba: pointer to lpfc hba data structure.
3113 * @acqe_link: pointer to the async link completion queue entry.
3114 *
3115 * This routine is to handle the SLI4 asynchronous link event.
3116 **/
3117static void
3118lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3119 struct lpfc_acqe_link *acqe_link)
3120{
3121 struct lpfc_dmabuf *mp;
3122 LPFC_MBOXQ_t *pmb;
3123 MAILBOX_t *mb;
3124 READ_LA_VAR *la;
3125 uint8_t att_type;
3126
3127 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3128 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3129 return;
James Smart32b97932009-07-19 10:01:21 -04003130 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003131 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3132 if (!pmb) {
3133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3134 "0395 The mboxq allocation failed\n");
3135 return;
3136 }
3137 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3138 if (!mp) {
3139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3140 "0396 The lpfc_dmabuf allocation failed\n");
3141 goto out_free_pmb;
3142 }
3143 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3144 if (!mp->virt) {
3145 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3146 "0397 The mbuf allocation failed\n");
3147 goto out_free_dmabuf;
3148 }
3149
3150 /* Cleanup any outstanding ELS commands */
3151 lpfc_els_flush_all_cmd(phba);
3152
3153 /* Block ELS IOCBs until we have done process link event */
3154 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3155
3156 /* Update link event statistics */
3157 phba->sli.slistat.link_event++;
3158
3159 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3160 lpfc_read_la(phba, pmb, mp);
3161 pmb->vport = phba->pport;
3162
3163 /* Parse and translate status field */
3164 mb = &pmb->u.mb;
3165 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3166
3167 /* Parse and translate link attention fields */
3168 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3169 la->eventTag = acqe_link->event_tag;
3170 la->attType = att_type;
3171 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3172
3173 /* Fake the the following irrelvant fields */
3174 la->topology = TOPOLOGY_PT_PT;
3175 la->granted_AL_PA = 0;
3176 la->il = 0;
3177 la->pb = 0;
3178 la->fa = 0;
3179 la->mm = 0;
3180
3181 /* Keep the link status for extra SLI4 state machine reference */
3182 phba->sli4_hba.link_state.speed =
3183 bf_get(lpfc_acqe_link_speed, acqe_link);
3184 phba->sli4_hba.link_state.duplex =
3185 bf_get(lpfc_acqe_link_duplex, acqe_link);
3186 phba->sli4_hba.link_state.status =
3187 bf_get(lpfc_acqe_link_status, acqe_link);
3188 phba->sli4_hba.link_state.physical =
3189 bf_get(lpfc_acqe_link_physical, acqe_link);
3190 phba->sli4_hba.link_state.fault =
3191 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003192 phba->sli4_hba.link_state.logical_speed =
3193 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003194
3195 /* Invoke the lpfc_handle_latt mailbox command callback function */
3196 lpfc_mbx_cmpl_read_la(phba, pmb);
3197
3198 return;
3199
3200out_free_dmabuf:
3201 kfree(mp);
3202out_free_pmb:
3203 mempool_free(pmb, phba->mbox_mem_pool);
3204}
3205
3206/**
3207 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3208 * @phba: pointer to lpfc hba data structure.
3209 * @acqe_link: pointer to the async fcoe completion queue entry.
3210 *
3211 * This routine is to handle the SLI4 asynchronous fcoe event.
3212 **/
3213static void
3214lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3215 struct lpfc_acqe_fcoe *acqe_fcoe)
3216{
3217 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3218 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003219 struct lpfc_vport *vport;
3220 struct lpfc_nodelist *ndlp;
3221 struct Scsi_Host *shost;
James Smart97957242009-12-21 17:03:15 -05003222 uint32_t link_state;
James Smart695a8142010-01-26 23:08:03 -05003223 int active_vlink_present;
3224 struct lpfc_vport **vports;
3225 int i;
James Smartda0436e2009-05-22 14:51:39 -04003226
James Smart4d9ab992009-10-02 15:16:39 -04003227 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003228 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003229 switch (event_type) {
3230 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
James Smartecfd03c2010-02-12 14:41:27 -05003231 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
James Smartda0436e2009-05-22 14:51:39 -04003232 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smarte4e74272009-07-19 10:01:38 -04003233 "2546 New FCF found index 0x%x tag 0x%x\n",
James Smart6669f9b2009-10-02 15:16:45 -04003234 acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003235 acqe_fcoe->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003236 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003237 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3238 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3239 /*
3240 * If the current FCF is in discovered state or
3241 * FCF discovery is in progress, do nothing.
3242 */
3243 spin_unlock_irq(&phba->hbalock);
3244 break;
3245 }
3246 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3247 /*
3248 * If fast FCF failover rescan event is pending,
3249 * do nothing.
3250 */
James Smartda0436e2009-05-22 14:51:39 -04003251 spin_unlock_irq(&phba->hbalock);
3252 break;
3253 }
3254 spin_unlock_irq(&phba->hbalock);
3255
3256 /* Read the FCF table and re-discover SAN. */
James Smart6a9c52c2009-10-02 15:16:51 -04003257 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003258 if (rc)
3259 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003260 "2547 Read FCF record failed 0x%x\n",
3261 rc);
James Smartda0436e2009-05-22 14:51:39 -04003262 break;
3263
3264 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003266 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003267 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3268 acqe_fcoe->event_tag);
3269 break;
3270
3271 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3272 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003273 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003274 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003275 acqe_fcoe->event_tag);
3276 /* If the event is not for currently used fcf do nothing */
James Smartecfd03c2010-02-12 14:41:27 -05003277 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003278 break;
3279 /*
3280 * Currently, driver support only one FCF - so treat this as
James Smart97957242009-12-21 17:03:15 -05003281 * a link down, but save the link state because we don't want
3282 * it to be changed to Link Down unless it is already down.
James Smartda0436e2009-05-22 14:51:39 -04003283 */
James Smart97957242009-12-21 17:03:15 -05003284 link_state = phba->link_state;
James Smartda0436e2009-05-22 14:51:39 -04003285 lpfc_linkdown(phba);
James Smart97957242009-12-21 17:03:15 -05003286 phba->link_state = link_state;
James Smartda0436e2009-05-22 14:51:39 -04003287 /* Unregister FCF if no devices connected to it */
3288 lpfc_unregister_unused_fcf(phba);
3289 break;
James Smart6669f9b2009-10-02 15:16:45 -04003290 case LPFC_FCOE_EVENT_TYPE_CVL:
3291 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3292 "2718 Clear Virtual Link Received for VPI 0x%x"
3293 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3294 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003295 acqe_fcoe->index - phba->vpi_base);
James Smart6669f9b2009-10-02 15:16:45 -04003296 if (!vport)
3297 break;
3298 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3299 if (!ndlp)
3300 break;
3301 shost = lpfc_shost_from_vport(vport);
James Smart695a8142010-01-26 23:08:03 -05003302 if (phba->pport->port_state <= LPFC_FLOGI)
3303 break;
3304 /* If virtual link is not yet instantiated ignore CVL */
3305 if (vport->port_state <= LPFC_FDISC)
3306 break;
3307
James Smart6669f9b2009-10-02 15:16:45 -04003308 lpfc_linkdown_port(vport);
James Smart695a8142010-01-26 23:08:03 -05003309 lpfc_cleanup_pending_mbox(vport);
3310 spin_lock_irq(shost->host_lock);
3311 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3312 spin_unlock_irq(shost->host_lock);
3313 active_vlink_present = 0;
3314
3315 vports = lpfc_create_vport_work_array(phba);
3316 if (vports) {
3317 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3318 i++) {
3319 if ((!(vports[i]->fc_flag &
3320 FC_VPORT_CVL_RCVD)) &&
3321 (vports[i]->port_state > LPFC_FDISC)) {
3322 active_vlink_present = 1;
3323 break;
3324 }
3325 }
3326 lpfc_destroy_vport_work_array(phba, vports);
3327 }
3328
3329 if (active_vlink_present) {
3330 /*
3331 * If there are other active VLinks present,
3332 * re-instantiate the Vlink using FDISC.
3333 */
James Smart6669f9b2009-10-02 15:16:45 -04003334 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3335 spin_lock_irq(shost->host_lock);
3336 ndlp->nlp_flag |= NLP_DELAY_TMO;
3337 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003338 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3339 vport->port_state = LPFC_FDISC;
3340 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003341 /*
3342 * Otherwise, we request port to rediscover
3343 * the entire FCF table for a fast recovery
3344 * from possible case that the current FCF
3345 * is no longer valid.
3346 */
3347 rc = lpfc_sli4_redisc_fcf_table(phba);
3348 if (rc)
3349 /*
3350 * Last resort will be re-try on the
3351 * the current registered FCF entry.
3352 */
3353 lpfc_retry_pport_discovery(phba);
James Smart6669f9b2009-10-02 15:16:45 -04003354 }
3355 break;
James Smartda0436e2009-05-22 14:51:39 -04003356 default:
3357 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3358 "0288 Unknown FCoE event type 0x%x event tag "
3359 "0x%x\n", event_type, acqe_fcoe->event_tag);
3360 break;
3361 }
3362}
3363
3364/**
3365 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3366 * @phba: pointer to lpfc hba data structure.
3367 * @acqe_link: pointer to the async dcbx completion queue entry.
3368 *
3369 * This routine is to handle the SLI4 asynchronous dcbx event.
3370 **/
3371static void
3372lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3373 struct lpfc_acqe_dcbx *acqe_dcbx)
3374{
James Smart4d9ab992009-10-02 15:16:39 -04003375 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3377 "0290 The SLI4 DCBX asynchronous event is not "
3378 "handled yet\n");
3379}
3380
3381/**
3382 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3383 * @phba: pointer to lpfc hba data structure.
3384 *
3385 * This routine is invoked by the worker thread to process all the pending
3386 * SLI4 asynchronous events.
3387 **/
3388void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3389{
3390 struct lpfc_cq_event *cq_event;
3391
3392 /* First, declare the async event has been handled */
3393 spin_lock_irq(&phba->hbalock);
3394 phba->hba_flag &= ~ASYNC_EVENT;
3395 spin_unlock_irq(&phba->hbalock);
3396 /* Now, handle all the async events */
3397 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3398 /* Get the first event from the head of the event queue */
3399 spin_lock_irq(&phba->hbalock);
3400 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3401 cq_event, struct lpfc_cq_event, list);
3402 spin_unlock_irq(&phba->hbalock);
3403 /* Process the asynchronous event */
3404 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3405 case LPFC_TRAILER_CODE_LINK:
3406 lpfc_sli4_async_link_evt(phba,
3407 &cq_event->cqe.acqe_link);
3408 break;
3409 case LPFC_TRAILER_CODE_FCOE:
3410 lpfc_sli4_async_fcoe_evt(phba,
3411 &cq_event->cqe.acqe_fcoe);
3412 break;
3413 case LPFC_TRAILER_CODE_DCBX:
3414 lpfc_sli4_async_dcbx_evt(phba,
3415 &cq_event->cqe.acqe_dcbx);
3416 break;
3417 default:
3418 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3419 "1804 Invalid asynchrous event code: "
3420 "x%x\n", bf_get(lpfc_trailer_code,
3421 &cq_event->cqe.mcqe_cmpl));
3422 break;
3423 }
3424 /* Free the completion event processed to the free pool */
3425 lpfc_sli4_cq_event_release(phba, cq_event);
3426 }
3427}
3428
3429/**
James Smartecfd03c2010-02-12 14:41:27 -05003430 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3431 * @phba: pointer to lpfc hba data structure.
3432 *
3433 * This routine is invoked by the worker thread to process FCF table
3434 * rediscovery pending completion event.
3435 **/
3436void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3437{
3438 int rc;
3439
3440 spin_lock_irq(&phba->hbalock);
3441 /* Clear FCF rediscovery timeout event */
3442 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3443 /* Clear driver fast failover FCF record flag */
3444 phba->fcf.failover_rec.flag = 0;
3445 /* Set state for FCF fast failover */
3446 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3447 spin_unlock_irq(&phba->hbalock);
3448
3449 /* Scan FCF table from the first entry to re-discover SAN */
3450 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
3451 if (rc)
3452 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3453 "2747 Post FCF rediscovery read FCF record "
3454 "failed 0x%x\n", rc);
3455}
3456
3457/**
James Smartda0436e2009-05-22 14:51:39 -04003458 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3459 * @phba: pointer to lpfc hba data structure.
3460 * @dev_grp: The HBA PCI-Device group number.
3461 *
3462 * This routine is invoked to set up the per HBA PCI-Device group function
3463 * API jump table entries.
3464 *
3465 * Return: 0 if success, otherwise -ENODEV
3466 **/
3467int
3468lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003469{
3470 int rc;
3471
James Smartda0436e2009-05-22 14:51:39 -04003472 /* Set up lpfc PCI-device group */
3473 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003474
James Smartda0436e2009-05-22 14:51:39 -04003475 /* The LPFC_PCI_DEV_OC uses SLI4 */
3476 if (dev_grp == LPFC_PCI_DEV_OC)
3477 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003478
James Smartda0436e2009-05-22 14:51:39 -04003479 /* Set up device INIT API function jump table */
3480 rc = lpfc_init_api_table_setup(phba, dev_grp);
3481 if (rc)
3482 return -ENODEV;
3483 /* Set up SCSI API function jump table */
3484 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3485 if (rc)
3486 return -ENODEV;
3487 /* Set up SLI API function jump table */
3488 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3489 if (rc)
3490 return -ENODEV;
3491 /* Set up MBOX API function jump table */
3492 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3493 if (rc)
3494 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003495
James Smartda0436e2009-05-22 14:51:39 -04003496 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003497}
3498
3499/**
James Smart3621a712009-04-06 18:47:14 -04003500 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003501 * @phba: pointer to lpfc hba data structure.
3502 * @intr_mode: active interrupt mode adopted.
3503 *
3504 * This routine it invoked to log the currently used active interrupt mode
3505 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003506 **/
3507static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003508{
3509 switch (intr_mode) {
3510 case 0:
3511 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3512 "0470 Enable INTx interrupt mode.\n");
3513 break;
3514 case 1:
3515 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3516 "0481 Enabled MSI interrupt mode.\n");
3517 break;
3518 case 2:
3519 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3520 "0480 Enabled MSI-X interrupt mode.\n");
3521 break;
3522 default:
3523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3524 "0482 Illegal interrupt mode.\n");
3525 break;
3526 }
3527 return;
3528}
3529
James Smart5b75da22008-12-04 22:39:35 -05003530/**
James Smart3772a992009-05-22 14:50:54 -04003531 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003532 * @phba: pointer to lpfc hba data structure.
3533 *
James Smart3772a992009-05-22 14:50:54 -04003534 * This routine is invoked to enable the PCI device that is common to all
3535 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003536 *
3537 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003538 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003539 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003540 **/
James Smart3772a992009-05-22 14:50:54 -04003541static int
3542lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003543{
James Smart3772a992009-05-22 14:50:54 -04003544 struct pci_dev *pdev;
3545 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003546
James Smart3772a992009-05-22 14:50:54 -04003547 /* Obtain PCI device reference */
3548 if (!phba->pcidev)
3549 goto out_error;
3550 else
3551 pdev = phba->pcidev;
3552 /* Select PCI BARs */
3553 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3554 /* Enable PCI device */
3555 if (pci_enable_device_mem(pdev))
3556 goto out_error;
3557 /* Request PCI resource for the device */
3558 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3559 goto out_disable_device;
3560 /* Set up device as PCI master and save state for EEH */
3561 pci_set_master(pdev);
3562 pci_try_set_mwi(pdev);
3563 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003564
James Smart3772a992009-05-22 14:50:54 -04003565 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003566
James Smart3772a992009-05-22 14:50:54 -04003567out_disable_device:
3568 pci_disable_device(pdev);
3569out_error:
3570 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003571}
3572
3573/**
James Smart3772a992009-05-22 14:50:54 -04003574 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003575 * @phba: pointer to lpfc hba data structure.
3576 *
James Smart3772a992009-05-22 14:50:54 -04003577 * This routine is invoked to disable the PCI device that is common to all
3578 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003579 **/
3580static void
James Smart3772a992009-05-22 14:50:54 -04003581lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003582{
James Smart3772a992009-05-22 14:50:54 -04003583 struct pci_dev *pdev;
3584 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003585
James Smart3772a992009-05-22 14:50:54 -04003586 /* Obtain PCI device reference */
3587 if (!phba->pcidev)
3588 return;
3589 else
3590 pdev = phba->pcidev;
3591 /* Select PCI BARs */
3592 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3593 /* Release PCI resource and disable PCI device */
3594 pci_release_selected_regions(pdev, bars);
3595 pci_disable_device(pdev);
3596 /* Null out PCI private reference to driver */
3597 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003598
3599 return;
3600}
3601
3602/**
James Smart3772a992009-05-22 14:50:54 -04003603 * lpfc_reset_hba - Reset a hba
3604 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003605 *
James Smart3772a992009-05-22 14:50:54 -04003606 * This routine is invoked to reset a hba device. It brings the HBA
3607 * offline, performs a board restart, and then brings the board back
3608 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3609 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003610 **/
James Smart3772a992009-05-22 14:50:54 -04003611void
3612lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003613{
James Smart3772a992009-05-22 14:50:54 -04003614 /* If resets are disabled then set error state and return. */
3615 if (!phba->cfg_enable_hba_reset) {
3616 phba->link_state = LPFC_HBA_ERROR;
3617 return;
3618 }
3619 lpfc_offline_prep(phba);
3620 lpfc_offline(phba);
3621 lpfc_sli_brdrestart(phba);
3622 lpfc_online(phba);
3623 lpfc_unblock_mgmt_io(phba);
3624}
dea31012005-04-17 16:05:31 -05003625
James Smart3772a992009-05-22 14:50:54 -04003626/**
3627 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3628 * @phba: pointer to lpfc hba data structure.
3629 *
3630 * This routine is invoked to set up the driver internal resources specific to
3631 * support the SLI-3 HBA device it attached to.
3632 *
3633 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003634 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003635 * other values - error
3636 **/
3637static int
3638lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3639{
3640 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003641
James Smart3772a992009-05-22 14:50:54 -04003642 /*
3643 * Initialize timers used by driver
3644 */
dea31012005-04-17 16:05:31 -05003645
James Smart3772a992009-05-22 14:50:54 -04003646 /* Heartbeat timer */
3647 init_timer(&phba->hb_tmofunc);
3648 phba->hb_tmofunc.function = lpfc_hb_timeout;
3649 phba->hb_tmofunc.data = (unsigned long)phba;
3650
3651 psli = &phba->sli;
3652 /* MBOX heartbeat timer */
3653 init_timer(&psli->mbox_tmo);
3654 psli->mbox_tmo.function = lpfc_mbox_timeout;
3655 psli->mbox_tmo.data = (unsigned long) phba;
3656 /* FCP polling mode timer */
3657 init_timer(&phba->fcp_poll_timer);
3658 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3659 phba->fcp_poll_timer.data = (unsigned long) phba;
3660 /* Fabric block timer */
3661 init_timer(&phba->fabric_block_timer);
3662 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3663 phba->fabric_block_timer.data = (unsigned long) phba;
3664 /* EA polling mode timer */
3665 init_timer(&phba->eratt_poll);
3666 phba->eratt_poll.function = lpfc_poll_eratt;
3667 phba->eratt_poll.data = (unsigned long) phba;
3668
3669 /* Host attention work mask setup */
3670 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3671 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3672
3673 /* Get all the module params for configuring this host */
3674 lpfc_get_cfgparam(phba);
3675 /*
3676 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3677 * used to create the sg_dma_buf_pool must be dynamically calculated.
3678 * 2 segments are added since the IOCB needs a command and response bde.
3679 */
3680 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3681 sizeof(struct fcp_rsp) +
3682 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3683
3684 if (phba->cfg_enable_bg) {
3685 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3686 phba->cfg_sg_dma_buf_size +=
3687 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3688 }
3689
3690 /* Also reinitialize the host templates with new values. */
3691 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3692 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3693
3694 phba->max_vpi = LPFC_MAX_VPI;
3695 /* This will be set to correct value after config_port mbox */
3696 phba->max_vports = 0;
3697
3698 /*
3699 * Initialize the SLI Layer to run with lpfc HBAs.
3700 */
3701 lpfc_sli_setup(phba);
3702 lpfc_sli_queue_setup(phba);
3703
3704 /* Allocate device driver memory */
3705 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3706 return -ENOMEM;
3707
3708 return 0;
3709}
3710
3711/**
3712 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3713 * @phba: pointer to lpfc hba data structure.
3714 *
3715 * This routine is invoked to unset the driver internal resources set up
3716 * specific for supporting the SLI-3 HBA device it attached to.
3717 **/
3718static void
3719lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3720{
3721 /* Free device driver memory allocated */
3722 lpfc_mem_free_all(phba);
3723
3724 return;
3725}
3726
3727/**
James Smartda0436e2009-05-22 14:51:39 -04003728 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3729 * @phba: pointer to lpfc hba data structure.
3730 *
3731 * This routine is invoked to set up the driver internal resources specific to
3732 * support the SLI-4 HBA device it attached to.
3733 *
3734 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003735 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04003736 * other values - error
3737 **/
3738static int
3739lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3740{
3741 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05003742 LPFC_MBOXQ_t *mboxq;
3743 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3744 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3745 struct lpfc_mqe *mqe;
James Smartda0436e2009-05-22 14:51:39 -04003746
3747 /* Before proceed, wait for POST done and device ready */
3748 rc = lpfc_sli4_post_status_check(phba);
3749 if (rc)
3750 return -ENODEV;
3751
3752 /*
3753 * Initialize timers used by driver
3754 */
3755
3756 /* Heartbeat timer */
3757 init_timer(&phba->hb_tmofunc);
3758 phba->hb_tmofunc.function = lpfc_hb_timeout;
3759 phba->hb_tmofunc.data = (unsigned long)phba;
3760
3761 psli = &phba->sli;
3762 /* MBOX heartbeat timer */
3763 init_timer(&psli->mbox_tmo);
3764 psli->mbox_tmo.function = lpfc_mbox_timeout;
3765 psli->mbox_tmo.data = (unsigned long) phba;
3766 /* Fabric block timer */
3767 init_timer(&phba->fabric_block_timer);
3768 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3769 phba->fabric_block_timer.data = (unsigned long) phba;
3770 /* EA polling mode timer */
3771 init_timer(&phba->eratt_poll);
3772 phba->eratt_poll.function = lpfc_poll_eratt;
3773 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05003774 /* FCF rediscover timer */
3775 init_timer(&phba->fcf.redisc_wait);
3776 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3777 phba->fcf.redisc_wait.data = (unsigned long)phba;
3778
James Smartda0436e2009-05-22 14:51:39 -04003779 /*
3780 * We need to do a READ_CONFIG mailbox command here before
3781 * calling lpfc_get_cfgparam. For VFs this will report the
3782 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3783 * All of the resources allocated
3784 * for this Port are tied to these values.
3785 */
3786 /* Get all the module params for configuring this host */
3787 lpfc_get_cfgparam(phba);
3788 phba->max_vpi = LPFC_MAX_VPI;
3789 /* This will be set to correct value after the read_config mbox */
3790 phba->max_vports = 0;
3791
3792 /* Program the default value of vlan_id and fc_map */
3793 phba->valid_vlan = 0;
3794 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3795 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3796 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3797
3798 /*
3799 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3800 * used to create the sg_dma_buf_pool must be dynamically calculated.
3801 * 2 segments are added since the IOCB needs a command and response bde.
3802 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05003803 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04003804 */
James Smart28baac72010-02-12 14:42:03 -05003805 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
3806 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
3807 /* Feature Level 1 hardware is limited to 2 pages */
3808 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
3809 LPFC_SLI_INTF_FEATURELEVEL1_1))
3810 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04003811 else
James Smart28baac72010-02-12 14:42:03 -05003812 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
3813 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
3814 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
3815 dma_buf_size = dma_buf_size << 1)
3816 ;
3817 if (dma_buf_size == max_buf_size)
3818 phba->cfg_sg_seg_cnt = (dma_buf_size -
3819 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
3820 (2 * sizeof(struct sli4_sge))) /
3821 sizeof(struct sli4_sge);
3822 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04003823
3824 /* Initialize buffer queue management fields */
3825 hbq_count = lpfc_sli_hbq_count();
3826 for (i = 0; i < hbq_count; ++i)
3827 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3828 INIT_LIST_HEAD(&phba->rb_pend_list);
3829 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3830 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3831
3832 /*
3833 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3834 */
3835 /* Initialize the Abort scsi buffer list used by driver */
3836 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3837 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3838 /* This abort list used by worker thread */
3839 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3840
3841 /*
3842 * Initialize dirver internal slow-path work queues
3843 */
3844
3845 /* Driver internel slow-path CQ Event pool */
3846 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3847 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04003848 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04003849 /* Asynchronous event CQ Event work queue list */
3850 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3851 /* Fast-path XRI aborted CQ Event work queue list */
3852 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3853 /* Slow-path XRI aborted CQ Event work queue list */
3854 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3855 /* Receive queue CQ Event work queue list */
3856 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3857
3858 /* Initialize the driver internal SLI layer lists. */
3859 lpfc_sli_setup(phba);
3860 lpfc_sli_queue_setup(phba);
3861
3862 /* Allocate device driver memory */
3863 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3864 if (rc)
3865 return -ENOMEM;
3866
3867 /* Create the bootstrap mailbox command */
3868 rc = lpfc_create_bootstrap_mbox(phba);
3869 if (unlikely(rc))
3870 goto out_free_mem;
3871
3872 /* Set up the host's endian order with the device. */
3873 rc = lpfc_setup_endian_order(phba);
3874 if (unlikely(rc))
3875 goto out_free_bsmbx;
3876
James Smart6669f9b2009-10-02 15:16:45 -04003877 rc = lpfc_sli4_fw_cfg_check(phba);
3878 if (unlikely(rc))
3879 goto out_free_bsmbx;
3880
James Smartda0436e2009-05-22 14:51:39 -04003881 /* Set up the hba's configuration parameters. */
3882 rc = lpfc_sli4_read_config(phba);
3883 if (unlikely(rc))
3884 goto out_free_bsmbx;
3885
3886 /* Perform a function reset */
3887 rc = lpfc_pci_function_reset(phba);
3888 if (unlikely(rc))
3889 goto out_free_bsmbx;
3890
3891 /* Create all the SLI4 queues */
3892 rc = lpfc_sli4_queue_create(phba);
3893 if (rc)
3894 goto out_free_bsmbx;
3895
3896 /* Create driver internal CQE event pool */
3897 rc = lpfc_sli4_cq_event_pool_create(phba);
3898 if (rc)
3899 goto out_destroy_queue;
3900
3901 /* Initialize and populate the iocb list per host */
3902 rc = lpfc_init_sgl_list(phba);
3903 if (rc) {
3904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3905 "1400 Failed to initialize sgl list.\n");
3906 goto out_destroy_cq_event_pool;
3907 }
3908 rc = lpfc_init_active_sgl_array(phba);
3909 if (rc) {
3910 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3911 "1430 Failed to initialize sgl list.\n");
3912 goto out_free_sgl_list;
3913 }
3914
3915 rc = lpfc_sli4_init_rpi_hdrs(phba);
3916 if (rc) {
3917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3918 "1432 Failed to initialize rpi headers.\n");
3919 goto out_free_active_sgl;
3920 }
3921
3922 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3923 phba->cfg_fcp_eq_count), GFP_KERNEL);
3924 if (!phba->sli4_hba.fcp_eq_hdl) {
3925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3926 "2572 Failed allocate memory for fast-path "
3927 "per-EQ handle array\n");
3928 goto out_remove_rpi_hdrs;
3929 }
3930
3931 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3932 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3933 if (!phba->sli4_hba.msix_entries) {
3934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3935 "2573 Failed allocate memory for msi-x "
3936 "interrupt vector entries\n");
3937 goto out_free_fcp_eq_hdl;
3938 }
3939
James Smart28baac72010-02-12 14:42:03 -05003940 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3941 GFP_KERNEL);
3942 if (!mboxq) {
3943 rc = -ENOMEM;
3944 goto out_free_fcp_eq_hdl;
3945 }
3946
3947 /* Get the Supported Pages. It is always available. */
3948 lpfc_supported_pages(mboxq);
3949 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3950 if (unlikely(rc)) {
3951 rc = -EIO;
3952 mempool_free(mboxq, phba->mbox_mem_pool);
3953 goto out_free_fcp_eq_hdl;
3954 }
3955
3956 mqe = &mboxq->u.mqe;
3957 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
3958 LPFC_MAX_SUPPORTED_PAGES);
3959 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
3960 switch (pn_page[i]) {
3961 case LPFC_SLI4_PARAMETERS:
3962 phba->sli4_hba.pc_sli4_params.supported = 1;
3963 break;
3964 default:
3965 break;
3966 }
3967 }
3968
3969 /* Read the port's SLI4 Parameters capabilities if supported. */
3970 if (phba->sli4_hba.pc_sli4_params.supported)
3971 rc = lpfc_pc_sli4_params_get(phba, mboxq);
3972 mempool_free(mboxq, phba->mbox_mem_pool);
3973 if (rc) {
3974 rc = -EIO;
3975 goto out_free_fcp_eq_hdl;
3976 }
James Smartda0436e2009-05-22 14:51:39 -04003977 return rc;
3978
3979out_free_fcp_eq_hdl:
3980 kfree(phba->sli4_hba.fcp_eq_hdl);
3981out_remove_rpi_hdrs:
3982 lpfc_sli4_remove_rpi_hdrs(phba);
3983out_free_active_sgl:
3984 lpfc_free_active_sgl(phba);
3985out_free_sgl_list:
3986 lpfc_free_sgl_list(phba);
3987out_destroy_cq_event_pool:
3988 lpfc_sli4_cq_event_pool_destroy(phba);
3989out_destroy_queue:
3990 lpfc_sli4_queue_destroy(phba);
3991out_free_bsmbx:
3992 lpfc_destroy_bootstrap_mbox(phba);
3993out_free_mem:
3994 lpfc_mem_free(phba);
3995 return rc;
3996}
3997
3998/**
3999 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4000 * @phba: pointer to lpfc hba data structure.
4001 *
4002 * This routine is invoked to unset the driver internal resources set up
4003 * specific for supporting the SLI-4 HBA device it attached to.
4004 **/
4005static void
4006lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4007{
4008 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4009
4010 /* unregister default FCFI from the HBA */
4011 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4012
4013 /* Free the default FCR table */
4014 lpfc_sli_remove_dflt_fcf(phba);
4015
4016 /* Free memory allocated for msi-x interrupt vector entries */
4017 kfree(phba->sli4_hba.msix_entries);
4018
4019 /* Free memory allocated for fast-path work queue handles */
4020 kfree(phba->sli4_hba.fcp_eq_hdl);
4021
4022 /* Free the allocated rpi headers. */
4023 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004024 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004025
4026 /* Free the ELS sgl list */
4027 lpfc_free_active_sgl(phba);
4028 lpfc_free_sgl_list(phba);
4029
4030 /* Free the SCSI sgl management array */
4031 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4032
4033 /* Free the SLI4 queues */
4034 lpfc_sli4_queue_destroy(phba);
4035
4036 /* Free the completion queue EQ event pool */
4037 lpfc_sli4_cq_event_release_all(phba);
4038 lpfc_sli4_cq_event_pool_destroy(phba);
4039
4040 /* Reset SLI4 HBA FCoE function */
4041 lpfc_pci_function_reset(phba);
4042
4043 /* Free the bsmbx region. */
4044 lpfc_destroy_bootstrap_mbox(phba);
4045
4046 /* Free the SLI Layer memory with SLI4 HBAs */
4047 lpfc_mem_free_all(phba);
4048
4049 /* Free the current connect table */
4050 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004051 &phba->fcf_conn_rec_list, list) {
4052 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004053 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004054 }
James Smartda0436e2009-05-22 14:51:39 -04004055
4056 return;
4057}
4058
4059/**
James Smart3772a992009-05-22 14:50:54 -04004060 * lpfc_init_api_table_setup - Set up init api fucntion jump table
4061 * @phba: The hba struct for which this call is being executed.
4062 * @dev_grp: The HBA PCI-Device group number.
4063 *
4064 * This routine sets up the device INIT interface API function jump table
4065 * in @phba struct.
4066 *
4067 * Returns: 0 - success, -ENODEV - failure.
4068 **/
4069int
4070lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4071{
James Smart84d1b002010-02-12 14:42:33 -05004072 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4073 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart3772a992009-05-22 14:50:54 -04004074 switch (dev_grp) {
4075 case LPFC_PCI_DEV_LP:
4076 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4077 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4078 phba->lpfc_stop_port = lpfc_stop_port_s3;
4079 break;
James Smartda0436e2009-05-22 14:51:39 -04004080 case LPFC_PCI_DEV_OC:
4081 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4082 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4083 phba->lpfc_stop_port = lpfc_stop_port_s4;
4084 break;
James Smart3772a992009-05-22 14:50:54 -04004085 default:
4086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4087 "1431 Invalid HBA PCI-device group: 0x%x\n",
4088 dev_grp);
4089 return -ENODEV;
4090 break;
4091 }
4092 return 0;
4093}
4094
4095/**
4096 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4097 * @phba: pointer to lpfc hba data structure.
4098 *
4099 * This routine is invoked to set up the driver internal resources before the
4100 * device specific resource setup to support the HBA device it attached to.
4101 *
4102 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004103 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004104 * other values - error
4105 **/
4106static int
4107lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4108{
4109 /*
4110 * Driver resources common to all SLI revisions
4111 */
James Smartea2151b2008-09-07 11:52:10 -04004112 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004113 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004114
James Smarte47c9092008-02-08 18:49:26 -05004115 /* Initialize ndlp management spinlock */
4116 spin_lock_init(&phba->ndlp_lock);
4117
James Smart2e0fef82007-06-17 19:56:36 -05004118 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004119 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004120 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004121
James Smart3772a992009-05-22 14:50:54 -04004122 /* Initialize the wait queue head for the kernel thread */
4123 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004124
James Smart3772a992009-05-22 14:50:54 -04004125 /* Initialize the scsi buffer list used by driver for scsi IO */
4126 spin_lock_init(&phba->scsi_buf_list_lock);
4127 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004128
James Smart3772a992009-05-22 14:50:54 -04004129 /* Initialize the fabric iocb list */
4130 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004131
James Smart3772a992009-05-22 14:50:54 -04004132 /* Initialize list to save ELS buffers */
4133 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004134
James Smart3772a992009-05-22 14:50:54 -04004135 /* Initialize FCF connection rec list */
4136 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004137
James Smart3772a992009-05-22 14:50:54 -04004138 return 0;
4139}
dea31012005-04-17 16:05:31 -05004140
James Smart3772a992009-05-22 14:50:54 -04004141/**
4142 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4143 * @phba: pointer to lpfc hba data structure.
4144 *
4145 * This routine is invoked to set up the driver internal resources after the
4146 * device specific resource setup to support the HBA device it attached to.
4147 *
4148 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004149 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004150 * other values - error
4151 **/
4152static int
4153lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4154{
4155 int error;
4156
4157 /* Startup the kernel thread for this host adapter. */
4158 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4159 "lpfc_worker_%d", phba->brd_no);
4160 if (IS_ERR(phba->worker_thread)) {
4161 error = PTR_ERR(phba->worker_thread);
4162 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004163 }
4164
James Smart3772a992009-05-22 14:50:54 -04004165 return 0;
4166}
4167
4168/**
4169 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4170 * @phba: pointer to lpfc hba data structure.
4171 *
4172 * This routine is invoked to unset the driver internal resources set up after
4173 * the device specific resource setup for supporting the HBA device it
4174 * attached to.
4175 **/
4176static void
4177lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4178{
4179 /* Stop kernel worker thread */
4180 kthread_stop(phba->worker_thread);
4181}
4182
4183/**
4184 * lpfc_free_iocb_list - Free iocb list.
4185 * @phba: pointer to lpfc hba data structure.
4186 *
4187 * This routine is invoked to free the driver's IOCB list and memory.
4188 **/
4189static void
4190lpfc_free_iocb_list(struct lpfc_hba *phba)
4191{
4192 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4193
4194 spin_lock_irq(&phba->hbalock);
4195 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4196 &phba->lpfc_iocb_list, list) {
4197 list_del(&iocbq_entry->list);
4198 kfree(iocbq_entry);
4199 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004200 }
James Smart3772a992009-05-22 14:50:54 -04004201 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004202
James Smart3772a992009-05-22 14:50:54 -04004203 return;
4204}
dea31012005-04-17 16:05:31 -05004205
James Smart3772a992009-05-22 14:50:54 -04004206/**
4207 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4208 * @phba: pointer to lpfc hba data structure.
4209 *
4210 * This routine is invoked to allocate and initizlize the driver's IOCB
4211 * list and set up the IOCB tag array accordingly.
4212 *
4213 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004214 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004215 * other values - error
4216 **/
4217static int
4218lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4219{
4220 struct lpfc_iocbq *iocbq_entry = NULL;
4221 uint16_t iotag;
4222 int i;
dea31012005-04-17 16:05:31 -05004223
4224 /* Initialize and populate the iocb list per host. */
4225 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004226 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004227 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004228 if (iocbq_entry == NULL) {
4229 printk(KERN_ERR "%s: only allocated %d iocbs of "
4230 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004231 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004232 goto out_free_iocbq;
4233 }
4234
James Bottomley604a3e32005-10-29 10:28:33 -05004235 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4236 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004237 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004238 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004239 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004240 goto out_free_iocbq;
4241 }
James Smart3772a992009-05-22 14:50:54 -04004242 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004243
4244 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004245 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4246 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004247 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004248 }
4249
James Smart3772a992009-05-22 14:50:54 -04004250 return 0;
4251
4252out_free_iocbq:
4253 lpfc_free_iocb_list(phba);
4254
4255 return -ENOMEM;
4256}
4257
4258/**
James Smartda0436e2009-05-22 14:51:39 -04004259 * lpfc_free_sgl_list - Free sgl list.
4260 * @phba: pointer to lpfc hba data structure.
4261 *
4262 * This routine is invoked to free the driver's sgl list and memory.
4263 **/
4264static void
4265lpfc_free_sgl_list(struct lpfc_hba *phba)
4266{
4267 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4268 LIST_HEAD(sglq_list);
4269 int rc = 0;
4270
4271 spin_lock_irq(&phba->hbalock);
4272 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4273 spin_unlock_irq(&phba->hbalock);
4274
4275 list_for_each_entry_safe(sglq_entry, sglq_next,
4276 &sglq_list, list) {
4277 list_del(&sglq_entry->list);
4278 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4279 kfree(sglq_entry);
4280 phba->sli4_hba.total_sglq_bufs--;
4281 }
4282 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4283 if (rc) {
4284 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004285 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004286 }
4287 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4288}
4289
4290/**
4291 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4292 * @phba: pointer to lpfc hba data structure.
4293 *
4294 * This routine is invoked to allocate the driver's active sgl memory.
4295 * This array will hold the sglq_entry's for active IOs.
4296 **/
4297static int
4298lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4299{
4300 int size;
4301 size = sizeof(struct lpfc_sglq *);
4302 size *= phba->sli4_hba.max_cfg_param.max_xri;
4303
4304 phba->sli4_hba.lpfc_sglq_active_list =
4305 kzalloc(size, GFP_KERNEL);
4306 if (!phba->sli4_hba.lpfc_sglq_active_list)
4307 return -ENOMEM;
4308 return 0;
4309}
4310
4311/**
4312 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4313 * @phba: pointer to lpfc hba data structure.
4314 *
4315 * This routine is invoked to walk through the array of active sglq entries
4316 * and free all of the resources.
4317 * This is just a place holder for now.
4318 **/
4319static void
4320lpfc_free_active_sgl(struct lpfc_hba *phba)
4321{
4322 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4323}
4324
4325/**
4326 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4327 * @phba: pointer to lpfc hba data structure.
4328 *
4329 * This routine is invoked to allocate and initizlize the driver's sgl
4330 * list and set up the sgl xritag tag array accordingly.
4331 *
4332 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004333 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004334 * other values - error
4335 **/
4336static int
4337lpfc_init_sgl_list(struct lpfc_hba *phba)
4338{
4339 struct lpfc_sglq *sglq_entry = NULL;
4340 int i;
4341 int els_xri_cnt;
4342
4343 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4344 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4345 "2400 lpfc_init_sgl_list els %d.\n",
4346 els_xri_cnt);
4347 /* Initialize and populate the sglq list per host/VF. */
4348 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4349 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4350
4351 /* Sanity check on XRI management */
4352 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4353 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4354 "2562 No room left for SCSI XRI allocation: "
4355 "max_xri=%d, els_xri=%d\n",
4356 phba->sli4_hba.max_cfg_param.max_xri,
4357 els_xri_cnt);
4358 return -ENOMEM;
4359 }
4360
4361 /* Allocate memory for the ELS XRI management array */
4362 phba->sli4_hba.lpfc_els_sgl_array =
4363 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4364 GFP_KERNEL);
4365
4366 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4367 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4368 "2401 Failed to allocate memory for ELS "
4369 "XRI management array of size %d.\n",
4370 els_xri_cnt);
4371 return -ENOMEM;
4372 }
4373
4374 /* Keep the SCSI XRI into the XRI management array */
4375 phba->sli4_hba.scsi_xri_max =
4376 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4377 phba->sli4_hba.scsi_xri_cnt = 0;
4378
4379 phba->sli4_hba.lpfc_scsi_psb_array =
4380 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4381 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4382
4383 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4384 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4385 "2563 Failed to allocate memory for SCSI "
4386 "XRI management array of size %d.\n",
4387 phba->sli4_hba.scsi_xri_max);
4388 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4389 return -ENOMEM;
4390 }
4391
4392 for (i = 0; i < els_xri_cnt; i++) {
4393 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4394 if (sglq_entry == NULL) {
4395 printk(KERN_ERR "%s: only allocated %d sgls of "
4396 "expected %d count. Unloading driver.\n",
4397 __func__, i, els_xri_cnt);
4398 goto out_free_mem;
4399 }
4400
4401 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4402 if (sglq_entry->sli4_xritag == NO_XRI) {
4403 kfree(sglq_entry);
4404 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4405 "Unloading driver.\n", __func__);
4406 goto out_free_mem;
4407 }
4408 sglq_entry->buff_type = GEN_BUFF_TYPE;
4409 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4410 if (sglq_entry->virt == NULL) {
4411 kfree(sglq_entry);
4412 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4413 "Unloading driver.\n", __func__);
4414 goto out_free_mem;
4415 }
4416 sglq_entry->sgl = sglq_entry->virt;
4417 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4418
4419 /* The list order is used by later block SGL registraton */
4420 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05004421 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04004422 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4423 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4424 phba->sli4_hba.total_sglq_bufs++;
4425 spin_unlock_irq(&phba->hbalock);
4426 }
4427 return 0;
4428
4429out_free_mem:
4430 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4431 lpfc_free_sgl_list(phba);
4432 return -ENOMEM;
4433}
4434
4435/**
4436 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4437 * @phba: pointer to lpfc hba data structure.
4438 *
4439 * This routine is invoked to post rpi header templates to the
4440 * HBA consistent with the SLI-4 interface spec. This routine
4441 * posts a PAGE_SIZE memory region to the port to hold up to
4442 * PAGE_SIZE modulo 64 rpi context headers.
4443 * No locks are held here because this is an initialization routine
4444 * called only from probe or lpfc_online when interrupts are not
4445 * enabled and the driver is reinitializing the device.
4446 *
4447 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004448 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004449 * ENOMEM - No availble memory
4450 * EIO - The mailbox failed to complete successfully.
4451 **/
4452int
4453lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4454{
4455 int rc = 0;
4456 int longs;
4457 uint16_t rpi_count;
4458 struct lpfc_rpi_hdr *rpi_hdr;
4459
4460 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4461
4462 /*
4463 * Provision an rpi bitmask range for discovery. The total count
4464 * is the difference between max and base + 1.
4465 */
4466 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4467 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4468
4469 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4470 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4471 GFP_KERNEL);
4472 if (!phba->sli4_hba.rpi_bmask)
4473 return -ENOMEM;
4474
4475 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4476 if (!rpi_hdr) {
4477 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4478 "0391 Error during rpi post operation\n");
4479 lpfc_sli4_remove_rpis(phba);
4480 rc = -ENODEV;
4481 }
4482
4483 return rc;
4484}
4485
4486/**
4487 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4488 * @phba: pointer to lpfc hba data structure.
4489 *
4490 * This routine is invoked to allocate a single 4KB memory region to
4491 * support rpis and stores them in the phba. This single region
4492 * provides support for up to 64 rpis. The region is used globally
4493 * by the device.
4494 *
4495 * Returns:
4496 * A valid rpi hdr on success.
4497 * A NULL pointer on any failure.
4498 **/
4499struct lpfc_rpi_hdr *
4500lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4501{
4502 uint16_t rpi_limit, curr_rpi_range;
4503 struct lpfc_dmabuf *dmabuf;
4504 struct lpfc_rpi_hdr *rpi_hdr;
4505
4506 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4507 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4508
4509 spin_lock_irq(&phba->hbalock);
4510 curr_rpi_range = phba->sli4_hba.next_rpi;
4511 spin_unlock_irq(&phba->hbalock);
4512
4513 /*
4514 * The port has a limited number of rpis. The increment here
4515 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4516 * and to allow the full max_rpi range per port.
4517 */
4518 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4519 return NULL;
4520
4521 /*
4522 * First allocate the protocol header region for the port. The
4523 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4524 */
4525 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4526 if (!dmabuf)
4527 return NULL;
4528
4529 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4530 LPFC_HDR_TEMPLATE_SIZE,
4531 &dmabuf->phys,
4532 GFP_KERNEL);
4533 if (!dmabuf->virt) {
4534 rpi_hdr = NULL;
4535 goto err_free_dmabuf;
4536 }
4537
4538 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4539 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4540 rpi_hdr = NULL;
4541 goto err_free_coherent;
4542 }
4543
4544 /* Save the rpi header data for cleanup later. */
4545 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4546 if (!rpi_hdr)
4547 goto err_free_coherent;
4548
4549 rpi_hdr->dmabuf = dmabuf;
4550 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4551 rpi_hdr->page_count = 1;
4552 spin_lock_irq(&phba->hbalock);
4553 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4554 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4555
4556 /*
4557 * The next_rpi stores the next module-64 rpi value to post
4558 * in any subsequent rpi memory region postings.
4559 */
4560 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4561 spin_unlock_irq(&phba->hbalock);
4562 return rpi_hdr;
4563
4564 err_free_coherent:
4565 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4566 dmabuf->virt, dmabuf->phys);
4567 err_free_dmabuf:
4568 kfree(dmabuf);
4569 return NULL;
4570}
4571
4572/**
4573 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4574 * @phba: pointer to lpfc hba data structure.
4575 *
4576 * This routine is invoked to remove all memory resources allocated
4577 * to support rpis. This routine presumes the caller has released all
4578 * rpis consumed by fabric or port logins and is prepared to have
4579 * the header pages removed.
4580 **/
4581void
4582lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4583{
4584 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4585
4586 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4587 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4588 list_del(&rpi_hdr->list);
4589 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4590 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4591 kfree(rpi_hdr->dmabuf);
4592 kfree(rpi_hdr);
4593 }
4594
4595 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4596 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4597}
4598
4599/**
James Smart3772a992009-05-22 14:50:54 -04004600 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4601 * @pdev: pointer to pci device data structure.
4602 *
4603 * This routine is invoked to allocate the driver hba data structure for an
4604 * HBA device. If the allocation is successful, the phba reference to the
4605 * PCI device data structure is set.
4606 *
4607 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004608 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004609 * NULL - error
4610 **/
4611static struct lpfc_hba *
4612lpfc_hba_alloc(struct pci_dev *pdev)
4613{
4614 struct lpfc_hba *phba;
4615
4616 /* Allocate memory for HBA structure */
4617 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4618 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004619 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004620 return NULL;
4621 }
4622
4623 /* Set reference to PCI device in HBA structure */
4624 phba->pcidev = pdev;
4625
4626 /* Assign an unused board number */
4627 phba->brd_no = lpfc_get_instance();
4628 if (phba->brd_no < 0) {
4629 kfree(phba);
4630 return NULL;
4631 }
4632
James Smart4fede782010-01-26 23:08:55 -05004633 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04004634 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4635
James Smart3772a992009-05-22 14:50:54 -04004636 return phba;
4637}
4638
4639/**
4640 * lpfc_hba_free - Free driver hba data structure with a device.
4641 * @phba: pointer to lpfc hba data structure.
4642 *
4643 * This routine is invoked to free the driver hba data structure with an
4644 * HBA device.
4645 **/
4646static void
4647lpfc_hba_free(struct lpfc_hba *phba)
4648{
4649 /* Release the driver assigned board number */
4650 idr_remove(&lpfc_hba_index, phba->brd_no);
4651
4652 kfree(phba);
4653 return;
4654}
4655
4656/**
4657 * lpfc_create_shost - Create hba physical port with associated scsi host.
4658 * @phba: pointer to lpfc hba data structure.
4659 *
4660 * This routine is invoked to create HBA physical port and associate a SCSI
4661 * host with it.
4662 *
4663 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004664 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004665 * other values - error
4666 **/
4667static int
4668lpfc_create_shost(struct lpfc_hba *phba)
4669{
4670 struct lpfc_vport *vport;
4671 struct Scsi_Host *shost;
4672
4673 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004674 phba->fc_edtov = FF_DEF_EDTOV;
4675 phba->fc_ratov = FF_DEF_RATOV;
4676 phba->fc_altov = FF_DEF_ALTOV;
4677 phba->fc_arbtov = FF_DEF_ARBTOV;
4678
James Smart3de2a652007-08-02 11:09:59 -04004679 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004680 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004681 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004682
4683 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004684 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004685 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004686 /* Put reference to SCSI host to driver's device private data */
4687 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004688
James Smart3772a992009-05-22 14:50:54 -04004689 return 0;
4690}
dea31012005-04-17 16:05:31 -05004691
James Smart3772a992009-05-22 14:50:54 -04004692/**
4693 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4694 * @phba: pointer to lpfc hba data structure.
4695 *
4696 * This routine is invoked to destroy HBA physical port and the associated
4697 * SCSI host.
4698 **/
4699static void
4700lpfc_destroy_shost(struct lpfc_hba *phba)
4701{
4702 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004703
James Smart3772a992009-05-22 14:50:54 -04004704 /* Destroy physical port that associated with the SCSI host */
4705 destroy_port(vport);
4706
4707 return;
4708}
4709
4710/**
4711 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4712 * @phba: pointer to lpfc hba data structure.
4713 * @shost: the shost to be used to detect Block guard settings.
4714 *
4715 * This routine sets up the local Block guard protocol settings for @shost.
4716 * This routine also allocates memory for debugging bg buffers.
4717 **/
4718static void
4719lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4720{
4721 int pagecnt = 10;
4722 if (lpfc_prot_mask && lpfc_prot_guard) {
4723 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4724 "1478 Registering BlockGuard with the "
4725 "SCSI layer\n");
4726 scsi_host_set_prot(shost, lpfc_prot_mask);
4727 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004728 }
James Smart81301a92008-12-04 22:39:46 -05004729 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004730 while (pagecnt) {
4731 spin_lock_init(&_dump_buf_lock);
4732 _dump_buf_data =
4733 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4734 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004735 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4736 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004737 "_dump_buf_data at 0x%p\n",
4738 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004739 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004740 memset(_dump_buf_data, 0,
4741 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004742 break;
James Smart3772a992009-05-22 14:50:54 -04004743 } else
James Smart81301a92008-12-04 22:39:46 -05004744 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004745 }
James Smart81301a92008-12-04 22:39:46 -05004746 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004747 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4748 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004749 "memory for hexdump\n");
4750 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004751 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4752 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004753 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004754 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004755 while (pagecnt) {
4756 _dump_buf_dif =
4757 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4758 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004759 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4760 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004761 "_dump_buf_dif at 0x%p\n",
4762 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004763 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004764 memset(_dump_buf_dif, 0,
4765 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004766 break;
James Smart3772a992009-05-22 14:50:54 -04004767 } else
James Smart81301a92008-12-04 22:39:46 -05004768 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004769 }
James Smart81301a92008-12-04 22:39:46 -05004770 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004771 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4772 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004773 "memory for hexdump\n");
4774 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004775 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4776 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004777 _dump_buf_dif);
4778}
4779
4780/**
4781 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4782 * @phba: pointer to lpfc hba data structure.
4783 *
4784 * This routine is invoked to perform all the necessary post initialization
4785 * setup for the device.
4786 **/
4787static void
4788lpfc_post_init_setup(struct lpfc_hba *phba)
4789{
4790 struct Scsi_Host *shost;
4791 struct lpfc_adapter_event_header adapter_event;
4792
4793 /* Get the default values for Model Name and Description */
4794 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4795
4796 /*
4797 * hba setup may have changed the hba_queue_depth so we need to
4798 * adjust the value of can_queue.
4799 */
4800 shost = pci_get_drvdata(phba->pcidev);
4801 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4802 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4803 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004804
4805 lpfc_host_attrib_init(shost);
4806
James Smart2e0fef82007-06-17 19:56:36 -05004807 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4808 spin_lock_irq(shost->host_lock);
4809 lpfc_poll_start_timer(phba);
4810 spin_unlock_irq(shost->host_lock);
4811 }
James Smart8f6d98d2006-08-01 07:34:00 -04004812
James Smart93996272008-08-24 21:50:30 -04004813 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4814 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004815 /* Send board arrival event to upper layer */
4816 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4817 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4818 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004819 sizeof(adapter_event),
4820 (char *) &adapter_event,
4821 LPFC_NL_VENDOR_ID);
4822 return;
4823}
4824
4825/**
4826 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4827 * @phba: pointer to lpfc hba data structure.
4828 *
4829 * This routine is invoked to set up the PCI device memory space for device
4830 * with SLI-3 interface spec.
4831 *
4832 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004833 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004834 * other values - error
4835 **/
4836static int
4837lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4838{
4839 struct pci_dev *pdev;
4840 unsigned long bar0map_len, bar2map_len;
4841 int i, hbq_count;
4842 void *ptr;
4843 int error = -ENODEV;
4844
4845 /* Obtain PCI device reference */
4846 if (!phba->pcidev)
4847 return error;
4848 else
4849 pdev = phba->pcidev;
4850
4851 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05004852 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
4853 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
4854 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
4855 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04004856 return error;
Michael Reed8e685972009-09-18 12:02:05 -05004857 }
4858 }
James Smart3772a992009-05-22 14:50:54 -04004859
4860 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4861 * required by each mapping.
4862 */
4863 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4864 bar0map_len = pci_resource_len(pdev, 0);
4865
4866 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4867 bar2map_len = pci_resource_len(pdev, 2);
4868
4869 /* Map HBA SLIM to a kernel virtual address. */
4870 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4871 if (!phba->slim_memmap_p) {
4872 dev_printk(KERN_ERR, &pdev->dev,
4873 "ioremap failed for SLIM memory.\n");
4874 goto out;
4875 }
4876
4877 /* Map HBA Control Registers to a kernel virtual address. */
4878 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4879 if (!phba->ctrl_regs_memmap_p) {
4880 dev_printk(KERN_ERR, &pdev->dev,
4881 "ioremap failed for HBA control registers.\n");
4882 goto out_iounmap_slim;
4883 }
4884
4885 /* Allocate memory for SLI-2 structures */
4886 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4887 SLI2_SLIM_SIZE,
4888 &phba->slim2p.phys,
4889 GFP_KERNEL);
4890 if (!phba->slim2p.virt)
4891 goto out_iounmap;
4892
4893 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4894 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4895 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4896 phba->IOCBs = (phba->slim2p.virt +
4897 offsetof(struct lpfc_sli2_slim, IOCBs));
4898
4899 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4900 lpfc_sli_hbq_size(),
4901 &phba->hbqslimp.phys,
4902 GFP_KERNEL);
4903 if (!phba->hbqslimp.virt)
4904 goto out_free_slim;
4905
4906 hbq_count = lpfc_sli_hbq_count();
4907 ptr = phba->hbqslimp.virt;
4908 for (i = 0; i < hbq_count; ++i) {
4909 phba->hbqs[i].hbq_virt = ptr;
4910 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4911 ptr += (lpfc_hbq_defs[i]->entry_count *
4912 sizeof(struct lpfc_hbq_entry));
4913 }
4914 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4915 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4916
4917 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4918
4919 INIT_LIST_HEAD(&phba->rb_pend_list);
4920
4921 phba->MBslimaddr = phba->slim_memmap_p;
4922 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4923 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4924 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4925 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004926
dea31012005-04-17 16:05:31 -05004927 return 0;
4928
dea31012005-04-17 16:05:31 -05004929out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004930 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4931 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004932out_iounmap:
4933 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004934out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004935 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004936out:
4937 return error;
4938}
4939
James Smarte59058c2008-08-24 21:49:00 -04004940/**
James Smart3772a992009-05-22 14:50:54 -04004941 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4942 * @phba: pointer to lpfc hba data structure.
4943 *
4944 * This routine is invoked to unset the PCI device memory space for device
4945 * with SLI-3 interface spec.
4946 **/
4947static void
4948lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4949{
4950 struct pci_dev *pdev;
4951
4952 /* Obtain PCI device reference */
4953 if (!phba->pcidev)
4954 return;
4955 else
4956 pdev = phba->pcidev;
4957
4958 /* Free coherent DMA memory allocated */
4959 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4960 phba->hbqslimp.virt, phba->hbqslimp.phys);
4961 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4962 phba->slim2p.virt, phba->slim2p.phys);
4963
4964 /* I/O memory unmap */
4965 iounmap(phba->ctrl_regs_memmap_p);
4966 iounmap(phba->slim_memmap_p);
4967
4968 return;
4969}
4970
4971/**
James Smartda0436e2009-05-22 14:51:39 -04004972 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4973 * @phba: pointer to lpfc hba data structure.
4974 *
4975 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4976 * done and check status.
4977 *
4978 * Return 0 if successful, otherwise -ENODEV.
4979 **/
4980int
4981lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4982{
James Smart28baac72010-02-12 14:42:03 -05004983 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
James Smartda0436e2009-05-22 14:51:39 -04004984 int i, port_error = -ENODEV;
4985
4986 if (!phba->sli4_hba.STAregaddr)
4987 return -ENODEV;
4988
James Smartda0436e2009-05-22 14:51:39 -04004989 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4990 for (i = 0; i < 3000; i++) {
4991 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4992 /* Encounter fatal POST error, break out */
4993 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4994 port_error = -ENODEV;
4995 break;
4996 }
4997 if (LPFC_POST_STAGE_ARMFW_READY ==
4998 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4999 port_error = 0;
5000 break;
5001 }
5002 msleep(10);
5003 }
5004
5005 if (port_error)
5006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5007 "1408 Failure HBA POST Status: sta_reg=0x%x, "
5008 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5009 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5010 bf_get(lpfc_hst_state_perr, &sta_reg),
5011 bf_get(lpfc_hst_state_sfi, &sta_reg),
5012 bf_get(lpfc_hst_state_nip, &sta_reg),
5013 bf_get(lpfc_hst_state_ipc, &sta_reg),
5014 bf_get(lpfc_hst_state_xrom, &sta_reg),
5015 bf_get(lpfc_hst_state_dl, &sta_reg),
5016 bf_get(lpfc_hst_state_port_status, &sta_reg));
5017
5018 /* Log device information */
James Smart28baac72010-02-12 14:42:03 -05005019 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5020 if (bf_get(lpfc_sli_intf_valid,
5021 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5022 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5023 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5024 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5025 bf_get(lpfc_sli_intf_sli_family,
5026 &phba->sli4_hba.sli_intf),
5027 bf_get(lpfc_sli_intf_slirev,
5028 &phba->sli4_hba.sli_intf),
5029 bf_get(lpfc_sli_intf_featurelevel1,
5030 &phba->sli4_hba.sli_intf),
5031 bf_get(lpfc_sli_intf_featurelevel2,
5032 &phba->sli4_hba.sli_intf));
5033 }
James Smarta747c9c2009-11-18 15:41:10 -05005034 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5035 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04005036 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05005037 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5038 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5039 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5040 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5042 "1422 HBA Unrecoverable error: "
5043 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5044 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5045 uerrlo_reg.word0, uerrhi_reg.word0,
5046 phba->sli4_hba.ue_mask_lo,
5047 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04005048 return -ENODEV;
5049 }
5050
James Smartda0436e2009-05-22 14:51:39 -04005051 return port_error;
5052}
5053
5054/**
5055 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5056 * @phba: pointer to lpfc hba data structure.
5057 *
5058 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5059 * memory map.
5060 **/
5061static void
5062lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5063{
5064 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5065 LPFC_UERR_STATUS_LO;
5066 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5067 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05005068 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5069 LPFC_UE_MASK_LO;
5070 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5071 LPFC_UE_MASK_HI;
James Smart28baac72010-02-12 14:42:03 -05005072 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5073 LPFC_SLI_INTF;
James Smartda0436e2009-05-22 14:51:39 -04005074}
5075
5076/**
5077 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5078 * @phba: pointer to lpfc hba data structure.
5079 *
5080 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5081 * memory map.
5082 **/
5083static void
5084lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5085{
5086
5087 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5088 LPFC_HST_STATE;
5089 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5090 LPFC_HST_ISR0;
5091 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5092 LPFC_HST_IMR0;
5093 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5094 LPFC_HST_ISCR0;
5095 return;
5096}
5097
5098/**
5099 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5100 * @phba: pointer to lpfc hba data structure.
5101 * @vf: virtual function number
5102 *
5103 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5104 * based on the given viftual function number, @vf.
5105 *
5106 * Return 0 if successful, otherwise -ENODEV.
5107 **/
5108static int
5109lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5110{
5111 if (vf > LPFC_VIR_FUNC_MAX)
5112 return -ENODEV;
5113
5114 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5115 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5116 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5117 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5118 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5119 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5120 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5121 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5122 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5123 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5124 return 0;
5125}
5126
5127/**
5128 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5129 * @phba: pointer to lpfc hba data structure.
5130 *
5131 * This routine is invoked to create the bootstrap mailbox
5132 * region consistent with the SLI-4 interface spec. This
5133 * routine allocates all memory necessary to communicate
5134 * mailbox commands to the port and sets up all alignment
5135 * needs. No locks are expected to be held when calling
5136 * this routine.
5137 *
5138 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005139 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005140 * ENOMEM - could not allocated memory.
5141 **/
5142static int
5143lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5144{
5145 uint32_t bmbx_size;
5146 struct lpfc_dmabuf *dmabuf;
5147 struct dma_address *dma_address;
5148 uint32_t pa_addr;
5149 uint64_t phys_addr;
5150
5151 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5152 if (!dmabuf)
5153 return -ENOMEM;
5154
5155 /*
5156 * The bootstrap mailbox region is comprised of 2 parts
5157 * plus an alignment restriction of 16 bytes.
5158 */
5159 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5160 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5161 bmbx_size,
5162 &dmabuf->phys,
5163 GFP_KERNEL);
5164 if (!dmabuf->virt) {
5165 kfree(dmabuf);
5166 return -ENOMEM;
5167 }
5168 memset(dmabuf->virt, 0, bmbx_size);
5169
5170 /*
5171 * Initialize the bootstrap mailbox pointers now so that the register
5172 * operations are simple later. The mailbox dma address is required
5173 * to be 16-byte aligned. Also align the virtual memory as each
5174 * maibox is copied into the bmbx mailbox region before issuing the
5175 * command to the port.
5176 */
5177 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5178 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5179
5180 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5181 LPFC_ALIGN_16_BYTE);
5182 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5183 LPFC_ALIGN_16_BYTE);
5184
5185 /*
5186 * Set the high and low physical addresses now. The SLI4 alignment
5187 * requirement is 16 bytes and the mailbox is posted to the port
5188 * as two 30-bit addresses. The other data is a bit marking whether
5189 * the 30-bit address is the high or low address.
5190 * Upcast bmbx aphys to 64bits so shift instruction compiles
5191 * clean on 32 bit machines.
5192 */
5193 dma_address = &phba->sli4_hba.bmbx.dma_address;
5194 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5195 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5196 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5197 LPFC_BMBX_BIT1_ADDR_HI);
5198
5199 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5200 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5201 LPFC_BMBX_BIT1_ADDR_LO);
5202 return 0;
5203}
5204
5205/**
5206 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5207 * @phba: pointer to lpfc hba data structure.
5208 *
5209 * This routine is invoked to teardown the bootstrap mailbox
5210 * region and release all host resources. This routine requires
5211 * the caller to ensure all mailbox commands recovered, no
5212 * additional mailbox comands are sent, and interrupts are disabled
5213 * before calling this routine.
5214 *
5215 **/
5216static void
5217lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5218{
5219 dma_free_coherent(&phba->pcidev->dev,
5220 phba->sli4_hba.bmbx.bmbx_size,
5221 phba->sli4_hba.bmbx.dmabuf->virt,
5222 phba->sli4_hba.bmbx.dmabuf->phys);
5223
5224 kfree(phba->sli4_hba.bmbx.dmabuf);
5225 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5226}
5227
5228/**
5229 * lpfc_sli4_read_config - Get the config parameters.
5230 * @phba: pointer to lpfc hba data structure.
5231 *
5232 * This routine is invoked to read the configuration parameters from the HBA.
5233 * The configuration parameters are used to set the base and maximum values
5234 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5235 * allocation for the port.
5236 *
5237 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005238 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005239 * ENOMEM - No availble memory
5240 * EIO - The mailbox failed to complete successfully.
5241 **/
5242static int
5243lpfc_sli4_read_config(struct lpfc_hba *phba)
5244{
5245 LPFC_MBOXQ_t *pmb;
5246 struct lpfc_mbx_read_config *rd_config;
5247 uint32_t rc = 0;
5248
5249 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5250 if (!pmb) {
5251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5252 "2011 Unable to allocate memory for issuing "
5253 "SLI_CONFIG_SPECIAL mailbox command\n");
5254 return -ENOMEM;
5255 }
5256
5257 lpfc_read_config(phba, pmb);
5258
5259 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5260 if (rc != MBX_SUCCESS) {
5261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5262 "2012 Mailbox failed , mbxCmd x%x "
5263 "READ_CONFIG, mbxStatus x%x\n",
5264 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5265 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5266 rc = -EIO;
5267 } else {
5268 rd_config = &pmb->u.mqe.un.rd_config;
5269 phba->sli4_hba.max_cfg_param.max_xri =
5270 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5271 phba->sli4_hba.max_cfg_param.xri_base =
5272 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5273 phba->sli4_hba.max_cfg_param.max_vpi =
5274 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5275 phba->sli4_hba.max_cfg_param.vpi_base =
5276 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5277 phba->sli4_hba.max_cfg_param.max_rpi =
5278 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5279 phba->sli4_hba.max_cfg_param.rpi_base =
5280 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5281 phba->sli4_hba.max_cfg_param.max_vfi =
5282 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5283 phba->sli4_hba.max_cfg_param.vfi_base =
5284 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5285 phba->sli4_hba.max_cfg_param.max_fcfi =
5286 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5287 phba->sli4_hba.max_cfg_param.fcfi_base =
5288 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5289 phba->sli4_hba.max_cfg_param.max_eq =
5290 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5291 phba->sli4_hba.max_cfg_param.max_rq =
5292 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5293 phba->sli4_hba.max_cfg_param.max_wq =
5294 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5295 phba->sli4_hba.max_cfg_param.max_cq =
5296 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5297 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5298 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5299 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5300 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5301 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005302 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5303 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005304 phba->max_vports = phba->max_vpi;
5305 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5306 "2003 cfg params XRI(B:%d M:%d), "
5307 "VPI(B:%d M:%d) "
5308 "VFI(B:%d M:%d) "
5309 "RPI(B:%d M:%d) "
5310 "FCFI(B:%d M:%d)\n",
5311 phba->sli4_hba.max_cfg_param.xri_base,
5312 phba->sli4_hba.max_cfg_param.max_xri,
5313 phba->sli4_hba.max_cfg_param.vpi_base,
5314 phba->sli4_hba.max_cfg_param.max_vpi,
5315 phba->sli4_hba.max_cfg_param.vfi_base,
5316 phba->sli4_hba.max_cfg_param.max_vfi,
5317 phba->sli4_hba.max_cfg_param.rpi_base,
5318 phba->sli4_hba.max_cfg_param.max_rpi,
5319 phba->sli4_hba.max_cfg_param.fcfi_base,
5320 phba->sli4_hba.max_cfg_param.max_fcfi);
5321 }
5322 mempool_free(pmb, phba->mbox_mem_pool);
5323
5324 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5325 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5326 phba->cfg_hba_queue_depth =
5327 phba->sli4_hba.max_cfg_param.max_xri;
5328 return rc;
5329}
5330
5331/**
5332 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5333 * @phba: pointer to lpfc hba data structure.
5334 *
5335 * This routine is invoked to setup the host-side endian order to the
5336 * HBA consistent with the SLI-4 interface spec.
5337 *
5338 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005339 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005340 * ENOMEM - No availble memory
5341 * EIO - The mailbox failed to complete successfully.
5342 **/
5343static int
5344lpfc_setup_endian_order(struct lpfc_hba *phba)
5345{
5346 LPFC_MBOXQ_t *mboxq;
5347 uint32_t rc = 0;
5348 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5349 HOST_ENDIAN_HIGH_WORD1};
5350
5351 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5352 if (!mboxq) {
5353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5354 "0492 Unable to allocate memory for issuing "
5355 "SLI_CONFIG_SPECIAL mailbox command\n");
5356 return -ENOMEM;
5357 }
5358
5359 /*
5360 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5361 * words to contain special data values and no other data.
5362 */
5363 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5364 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5365 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5366 if (rc != MBX_SUCCESS) {
5367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5368 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5369 "status x%x\n",
5370 rc);
5371 rc = -EIO;
5372 }
5373
5374 mempool_free(mboxq, phba->mbox_mem_pool);
5375 return rc;
5376}
5377
5378/**
5379 * lpfc_sli4_queue_create - Create all the SLI4 queues
5380 * @phba: pointer to lpfc hba data structure.
5381 *
5382 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5383 * operation. For each SLI4 queue type, the parameters such as queue entry
5384 * count (queue depth) shall be taken from the module parameter. For now,
5385 * we just use some constant number as place holder.
5386 *
5387 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005388 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005389 * ENOMEM - No availble memory
5390 * EIO - The mailbox failed to complete successfully.
5391 **/
5392static int
5393lpfc_sli4_queue_create(struct lpfc_hba *phba)
5394{
5395 struct lpfc_queue *qdesc;
5396 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5397 int cfg_fcp_wq_count;
5398 int cfg_fcp_eq_count;
5399
5400 /*
5401 * Sanity check for confiugred queue parameters against the run-time
5402 * device parameters
5403 */
5404
5405 /* Sanity check on FCP fast-path WQ parameters */
5406 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5407 if (cfg_fcp_wq_count >
5408 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5409 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5410 LPFC_SP_WQN_DEF;
5411 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5413 "2581 Not enough WQs (%d) from "
5414 "the pci function for supporting "
5415 "FCP WQs (%d)\n",
5416 phba->sli4_hba.max_cfg_param.max_wq,
5417 phba->cfg_fcp_wq_count);
5418 goto out_error;
5419 }
5420 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5421 "2582 Not enough WQs (%d) from the pci "
5422 "function for supporting the requested "
5423 "FCP WQs (%d), the actual FCP WQs can "
5424 "be supported: %d\n",
5425 phba->sli4_hba.max_cfg_param.max_wq,
5426 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5427 }
5428 /* The actual number of FCP work queues adopted */
5429 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5430
5431 /* Sanity check on FCP fast-path EQ parameters */
5432 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5433 if (cfg_fcp_eq_count >
5434 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5435 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5436 LPFC_SP_EQN_DEF;
5437 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5439 "2574 Not enough EQs (%d) from the "
5440 "pci function for supporting FCP "
5441 "EQs (%d)\n",
5442 phba->sli4_hba.max_cfg_param.max_eq,
5443 phba->cfg_fcp_eq_count);
5444 goto out_error;
5445 }
5446 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5447 "2575 Not enough EQs (%d) from the pci "
5448 "function for supporting the requested "
5449 "FCP EQs (%d), the actual FCP EQs can "
5450 "be supported: %d\n",
5451 phba->sli4_hba.max_cfg_param.max_eq,
5452 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5453 }
5454 /* It does not make sense to have more EQs than WQs */
5455 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5456 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005457 "2593 The FCP EQ count(%d) cannot be greater "
5458 "than the FCP WQ count(%d), limiting the "
5459 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005460 phba->cfg_fcp_wq_count,
5461 phba->cfg_fcp_wq_count);
5462 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5463 }
5464 /* The actual number of FCP event queues adopted */
5465 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5466 /* The overall number of event queues used */
5467 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5468
5469 /*
5470 * Create Event Queues (EQs)
5471 */
5472
5473 /* Get EQ depth from module parameter, fake the default for now */
5474 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5475 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5476
5477 /* Create slow path event queue */
5478 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5479 phba->sli4_hba.eq_ecount);
5480 if (!qdesc) {
5481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5482 "0496 Failed allocate slow-path EQ\n");
5483 goto out_error;
5484 }
5485 phba->sli4_hba.sp_eq = qdesc;
5486
5487 /* Create fast-path FCP Event Queue(s) */
5488 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5489 phba->cfg_fcp_eq_count), GFP_KERNEL);
5490 if (!phba->sli4_hba.fp_eq) {
5491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5492 "2576 Failed allocate memory for fast-path "
5493 "EQ record array\n");
5494 goto out_free_sp_eq;
5495 }
5496 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5497 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5498 phba->sli4_hba.eq_ecount);
5499 if (!qdesc) {
5500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5501 "0497 Failed allocate fast-path EQ\n");
5502 goto out_free_fp_eq;
5503 }
5504 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5505 }
5506
5507 /*
5508 * Create Complete Queues (CQs)
5509 */
5510
5511 /* Get CQ depth from module parameter, fake the default for now */
5512 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5513 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5514
5515 /* Create slow-path Mailbox Command Complete Queue */
5516 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5517 phba->sli4_hba.cq_ecount);
5518 if (!qdesc) {
5519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5520 "0500 Failed allocate slow-path mailbox CQ\n");
5521 goto out_free_fp_eq;
5522 }
5523 phba->sli4_hba.mbx_cq = qdesc;
5524
5525 /* Create slow-path ELS Complete Queue */
5526 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5527 phba->sli4_hba.cq_ecount);
5528 if (!qdesc) {
5529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5530 "0501 Failed allocate slow-path ELS CQ\n");
5531 goto out_free_mbx_cq;
5532 }
5533 phba->sli4_hba.els_cq = qdesc;
5534
James Smartda0436e2009-05-22 14:51:39 -04005535
5536 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5537 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5538 phba->cfg_fcp_eq_count), GFP_KERNEL);
5539 if (!phba->sli4_hba.fcp_cq) {
5540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5541 "2577 Failed allocate memory for fast-path "
5542 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005543 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005544 }
5545 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5546 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5547 phba->sli4_hba.cq_ecount);
5548 if (!qdesc) {
5549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5550 "0499 Failed allocate fast-path FCP "
5551 "CQ (%d)\n", fcp_cqidx);
5552 goto out_free_fcp_cq;
5553 }
5554 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5555 }
5556
5557 /* Create Mailbox Command Queue */
5558 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5559 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5560
5561 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5562 phba->sli4_hba.mq_ecount);
5563 if (!qdesc) {
5564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5565 "0505 Failed allocate slow-path MQ\n");
5566 goto out_free_fcp_cq;
5567 }
5568 phba->sli4_hba.mbx_wq = qdesc;
5569
5570 /*
5571 * Create all the Work Queues (WQs)
5572 */
5573 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5574 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5575
5576 /* Create slow-path ELS Work Queue */
5577 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5578 phba->sli4_hba.wq_ecount);
5579 if (!qdesc) {
5580 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5581 "0504 Failed allocate slow-path ELS WQ\n");
5582 goto out_free_mbx_wq;
5583 }
5584 phba->sli4_hba.els_wq = qdesc;
5585
5586 /* Create fast-path FCP Work Queue(s) */
5587 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5588 phba->cfg_fcp_wq_count), GFP_KERNEL);
5589 if (!phba->sli4_hba.fcp_wq) {
5590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5591 "2578 Failed allocate memory for fast-path "
5592 "WQ record array\n");
5593 goto out_free_els_wq;
5594 }
5595 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5596 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5597 phba->sli4_hba.wq_ecount);
5598 if (!qdesc) {
5599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5600 "0503 Failed allocate fast-path FCP "
5601 "WQ (%d)\n", fcp_wqidx);
5602 goto out_free_fcp_wq;
5603 }
5604 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5605 }
5606
5607 /*
5608 * Create Receive Queue (RQ)
5609 */
5610 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5611 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5612
5613 /* Create Receive Queue for header */
5614 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5615 phba->sli4_hba.rq_ecount);
5616 if (!qdesc) {
5617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5618 "0506 Failed allocate receive HRQ\n");
5619 goto out_free_fcp_wq;
5620 }
5621 phba->sli4_hba.hdr_rq = qdesc;
5622
5623 /* Create Receive Queue for data */
5624 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5625 phba->sli4_hba.rq_ecount);
5626 if (!qdesc) {
5627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5628 "0507 Failed allocate receive DRQ\n");
5629 goto out_free_hdr_rq;
5630 }
5631 phba->sli4_hba.dat_rq = qdesc;
5632
5633 return 0;
5634
5635out_free_hdr_rq:
5636 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5637 phba->sli4_hba.hdr_rq = NULL;
5638out_free_fcp_wq:
5639 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5640 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5641 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5642 }
5643 kfree(phba->sli4_hba.fcp_wq);
5644out_free_els_wq:
5645 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5646 phba->sli4_hba.els_wq = NULL;
5647out_free_mbx_wq:
5648 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5649 phba->sli4_hba.mbx_wq = NULL;
5650out_free_fcp_cq:
5651 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5652 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5653 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5654 }
5655 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005656out_free_els_cq:
5657 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5658 phba->sli4_hba.els_cq = NULL;
5659out_free_mbx_cq:
5660 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5661 phba->sli4_hba.mbx_cq = NULL;
5662out_free_fp_eq:
5663 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5664 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5665 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5666 }
5667 kfree(phba->sli4_hba.fp_eq);
5668out_free_sp_eq:
5669 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5670 phba->sli4_hba.sp_eq = NULL;
5671out_error:
5672 return -ENOMEM;
5673}
5674
5675/**
5676 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5677 * @phba: pointer to lpfc hba data structure.
5678 *
5679 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5680 * operation.
5681 *
5682 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005683 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005684 * ENOMEM - No availble memory
5685 * EIO - The mailbox failed to complete successfully.
5686 **/
5687static void
5688lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5689{
5690 int fcp_qidx;
5691
5692 /* Release mailbox command work queue */
5693 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5694 phba->sli4_hba.mbx_wq = NULL;
5695
5696 /* Release ELS work queue */
5697 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5698 phba->sli4_hba.els_wq = NULL;
5699
5700 /* Release FCP work queue */
5701 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5702 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5703 kfree(phba->sli4_hba.fcp_wq);
5704 phba->sli4_hba.fcp_wq = NULL;
5705
5706 /* Release unsolicited receive queue */
5707 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5708 phba->sli4_hba.hdr_rq = NULL;
5709 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5710 phba->sli4_hba.dat_rq = NULL;
5711
James Smartda0436e2009-05-22 14:51:39 -04005712 /* Release ELS complete queue */
5713 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5714 phba->sli4_hba.els_cq = NULL;
5715
5716 /* Release mailbox command complete queue */
5717 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5718 phba->sli4_hba.mbx_cq = NULL;
5719
5720 /* Release FCP response complete queue */
5721 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5722 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5723 kfree(phba->sli4_hba.fcp_cq);
5724 phba->sli4_hba.fcp_cq = NULL;
5725
5726 /* Release fast-path event queue */
5727 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5728 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5729 kfree(phba->sli4_hba.fp_eq);
5730 phba->sli4_hba.fp_eq = NULL;
5731
5732 /* Release slow-path event queue */
5733 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5734 phba->sli4_hba.sp_eq = NULL;
5735
5736 return;
5737}
5738
5739/**
5740 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5741 * @phba: pointer to lpfc hba data structure.
5742 *
5743 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5744 * operation.
5745 *
5746 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005747 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005748 * ENOMEM - No availble memory
5749 * EIO - The mailbox failed to complete successfully.
5750 **/
5751int
5752lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5753{
5754 int rc = -ENOMEM;
5755 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5756 int fcp_cq_index = 0;
5757
5758 /*
5759 * Set up Event Queues (EQs)
5760 */
5761
5762 /* Set up slow-path event queue */
5763 if (!phba->sli4_hba.sp_eq) {
5764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5765 "0520 Slow-path EQ not allocated\n");
5766 goto out_error;
5767 }
5768 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5769 LPFC_SP_DEF_IMAX);
5770 if (rc) {
5771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5772 "0521 Failed setup of slow-path EQ: "
5773 "rc = 0x%x\n", rc);
5774 goto out_error;
5775 }
5776 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5777 "2583 Slow-path EQ setup: queue-id=%d\n",
5778 phba->sli4_hba.sp_eq->queue_id);
5779
5780 /* Set up fast-path event queue */
5781 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5782 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5784 "0522 Fast-path EQ (%d) not "
5785 "allocated\n", fcp_eqidx);
5786 goto out_destroy_fp_eq;
5787 }
5788 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5789 phba->cfg_fcp_imax);
5790 if (rc) {
5791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5792 "0523 Failed setup of fast-path EQ "
5793 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5794 goto out_destroy_fp_eq;
5795 }
5796 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5797 "2584 Fast-path EQ setup: "
5798 "queue[%d]-id=%d\n", fcp_eqidx,
5799 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5800 }
5801
5802 /*
5803 * Set up Complete Queues (CQs)
5804 */
5805
5806 /* Set up slow-path MBOX Complete Queue as the first CQ */
5807 if (!phba->sli4_hba.mbx_cq) {
5808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5809 "0528 Mailbox CQ not allocated\n");
5810 goto out_destroy_fp_eq;
5811 }
5812 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5813 LPFC_MCQ, LPFC_MBOX);
5814 if (rc) {
5815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5816 "0529 Failed setup of slow-path mailbox CQ: "
5817 "rc = 0x%x\n", rc);
5818 goto out_destroy_fp_eq;
5819 }
5820 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5821 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5822 phba->sli4_hba.mbx_cq->queue_id,
5823 phba->sli4_hba.sp_eq->queue_id);
5824
5825 /* Set up slow-path ELS Complete Queue */
5826 if (!phba->sli4_hba.els_cq) {
5827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5828 "0530 ELS CQ not allocated\n");
5829 goto out_destroy_mbx_cq;
5830 }
5831 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5832 LPFC_WCQ, LPFC_ELS);
5833 if (rc) {
5834 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5835 "0531 Failed setup of slow-path ELS CQ: "
5836 "rc = 0x%x\n", rc);
5837 goto out_destroy_mbx_cq;
5838 }
5839 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5840 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5841 phba->sli4_hba.els_cq->queue_id,
5842 phba->sli4_hba.sp_eq->queue_id);
5843
James Smartda0436e2009-05-22 14:51:39 -04005844 /* Set up fast-path FCP Response Complete Queue */
5845 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5846 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5848 "0526 Fast-path FCP CQ (%d) not "
5849 "allocated\n", fcp_cqidx);
5850 goto out_destroy_fcp_cq;
5851 }
5852 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5853 phba->sli4_hba.fp_eq[fcp_cqidx],
5854 LPFC_WCQ, LPFC_FCP);
5855 if (rc) {
5856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5857 "0527 Failed setup of fast-path FCP "
5858 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5859 goto out_destroy_fcp_cq;
5860 }
5861 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5862 "2588 FCP CQ setup: cq[%d]-id=%d, "
5863 "parent eq[%d]-id=%d\n",
5864 fcp_cqidx,
5865 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5866 fcp_cqidx,
5867 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5868 }
5869
5870 /*
5871 * Set up all the Work Queues (WQs)
5872 */
5873
5874 /* Set up Mailbox Command Queue */
5875 if (!phba->sli4_hba.mbx_wq) {
5876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5877 "0538 Slow-path MQ not allocated\n");
5878 goto out_destroy_fcp_cq;
5879 }
5880 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5881 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5882 if (rc) {
5883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5884 "0539 Failed setup of slow-path MQ: "
5885 "rc = 0x%x\n", rc);
5886 goto out_destroy_fcp_cq;
5887 }
5888 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5889 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5890 phba->sli4_hba.mbx_wq->queue_id,
5891 phba->sli4_hba.mbx_cq->queue_id);
5892
5893 /* Set up slow-path ELS Work Queue */
5894 if (!phba->sli4_hba.els_wq) {
5895 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5896 "0536 Slow-path ELS WQ not allocated\n");
5897 goto out_destroy_mbx_wq;
5898 }
5899 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5900 phba->sli4_hba.els_cq, LPFC_ELS);
5901 if (rc) {
5902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5903 "0537 Failed setup of slow-path ELS WQ: "
5904 "rc = 0x%x\n", rc);
5905 goto out_destroy_mbx_wq;
5906 }
5907 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5908 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5909 phba->sli4_hba.els_wq->queue_id,
5910 phba->sli4_hba.els_cq->queue_id);
5911
5912 /* Set up fast-path FCP Work Queue */
5913 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5914 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5916 "0534 Fast-path FCP WQ (%d) not "
5917 "allocated\n", fcp_wqidx);
5918 goto out_destroy_fcp_wq;
5919 }
5920 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5921 phba->sli4_hba.fcp_cq[fcp_cq_index],
5922 LPFC_FCP);
5923 if (rc) {
5924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5925 "0535 Failed setup of fast-path FCP "
5926 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5927 goto out_destroy_fcp_wq;
5928 }
5929 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5930 "2591 FCP WQ setup: wq[%d]-id=%d, "
5931 "parent cq[%d]-id=%d\n",
5932 fcp_wqidx,
5933 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5934 fcp_cq_index,
5935 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5936 /* Round robin FCP Work Queue's Completion Queue assignment */
5937 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5938 }
5939
5940 /*
5941 * Create Receive Queue (RQ)
5942 */
5943 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5945 "0540 Receive Queue not allocated\n");
5946 goto out_destroy_fcp_wq;
5947 }
5948 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04005949 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04005950 if (rc) {
5951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5952 "0541 Failed setup of Receive Queue: "
5953 "rc = 0x%x\n", rc);
5954 goto out_destroy_fcp_wq;
5955 }
5956 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5957 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5958 "parent cq-id=%d\n",
5959 phba->sli4_hba.hdr_rq->queue_id,
5960 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04005961 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04005962 return 0;
5963
5964out_destroy_fcp_wq:
5965 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5966 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5967 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5968out_destroy_mbx_wq:
5969 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5970out_destroy_fcp_cq:
5971 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5972 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04005973 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5974out_destroy_mbx_cq:
5975 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5976out_destroy_fp_eq:
5977 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5978 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5979 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5980out_error:
5981 return rc;
5982}
5983
5984/**
5985 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5986 * @phba: pointer to lpfc hba data structure.
5987 *
5988 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5989 * operation.
5990 *
5991 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005992 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005993 * ENOMEM - No availble memory
5994 * EIO - The mailbox failed to complete successfully.
5995 **/
5996void
5997lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5998{
5999 int fcp_qidx;
6000
6001 /* Unset mailbox command work queue */
6002 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6003 /* Unset ELS work queue */
6004 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6005 /* Unset unsolicited receive queue */
6006 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6007 /* Unset FCP work queue */
6008 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6009 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6010 /* Unset mailbox command complete queue */
6011 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6012 /* Unset ELS complete queue */
6013 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006014 /* Unset FCP response complete queue */
6015 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6016 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6017 /* Unset fast-path event queue */
6018 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6019 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6020 /* Unset slow-path event queue */
6021 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6022}
6023
6024/**
6025 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6026 * @phba: pointer to lpfc hba data structure.
6027 *
6028 * This routine is invoked to allocate and set up a pool of completion queue
6029 * events. The body of the completion queue event is a completion queue entry
6030 * CQE. For now, this pool is used for the interrupt service routine to queue
6031 * the following HBA completion queue events for the worker thread to process:
6032 * - Mailbox asynchronous events
6033 * - Receive queue completion unsolicited events
6034 * Later, this can be used for all the slow-path events.
6035 *
6036 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006037 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006038 * -ENOMEM - No availble memory
6039 **/
6040static int
6041lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6042{
6043 struct lpfc_cq_event *cq_event;
6044 int i;
6045
6046 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6047 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6048 if (!cq_event)
6049 goto out_pool_create_fail;
6050 list_add_tail(&cq_event->list,
6051 &phba->sli4_hba.sp_cqe_event_pool);
6052 }
6053 return 0;
6054
6055out_pool_create_fail:
6056 lpfc_sli4_cq_event_pool_destroy(phba);
6057 return -ENOMEM;
6058}
6059
6060/**
6061 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6062 * @phba: pointer to lpfc hba data structure.
6063 *
6064 * This routine is invoked to free the pool of completion queue events at
6065 * driver unload time. Note that, it is the responsibility of the driver
6066 * cleanup routine to free all the outstanding completion-queue events
6067 * allocated from this pool back into the pool before invoking this routine
6068 * to destroy the pool.
6069 **/
6070static void
6071lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6072{
6073 struct lpfc_cq_event *cq_event, *next_cq_event;
6074
6075 list_for_each_entry_safe(cq_event, next_cq_event,
6076 &phba->sli4_hba.sp_cqe_event_pool, list) {
6077 list_del(&cq_event->list);
6078 kfree(cq_event);
6079 }
6080}
6081
6082/**
6083 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6084 * @phba: pointer to lpfc hba data structure.
6085 *
6086 * This routine is the lock free version of the API invoked to allocate a
6087 * completion-queue event from the free pool.
6088 *
6089 * Return: Pointer to the newly allocated completion-queue event if successful
6090 * NULL otherwise.
6091 **/
6092struct lpfc_cq_event *
6093__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6094{
6095 struct lpfc_cq_event *cq_event = NULL;
6096
6097 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6098 struct lpfc_cq_event, list);
6099 return cq_event;
6100}
6101
6102/**
6103 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6104 * @phba: pointer to lpfc hba data structure.
6105 *
6106 * This routine is the lock version of the API invoked to allocate a
6107 * completion-queue event from the free pool.
6108 *
6109 * Return: Pointer to the newly allocated completion-queue event if successful
6110 * NULL otherwise.
6111 **/
6112struct lpfc_cq_event *
6113lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6114{
6115 struct lpfc_cq_event *cq_event;
6116 unsigned long iflags;
6117
6118 spin_lock_irqsave(&phba->hbalock, iflags);
6119 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6120 spin_unlock_irqrestore(&phba->hbalock, iflags);
6121 return cq_event;
6122}
6123
6124/**
6125 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6126 * @phba: pointer to lpfc hba data structure.
6127 * @cq_event: pointer to the completion queue event to be freed.
6128 *
6129 * This routine is the lock free version of the API invoked to release a
6130 * completion-queue event back into the free pool.
6131 **/
6132void
6133__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6134 struct lpfc_cq_event *cq_event)
6135{
6136 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6137}
6138
6139/**
6140 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6141 * @phba: pointer to lpfc hba data structure.
6142 * @cq_event: pointer to the completion queue event to be freed.
6143 *
6144 * This routine is the lock version of the API invoked to release a
6145 * completion-queue event back into the free pool.
6146 **/
6147void
6148lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6149 struct lpfc_cq_event *cq_event)
6150{
6151 unsigned long iflags;
6152 spin_lock_irqsave(&phba->hbalock, iflags);
6153 __lpfc_sli4_cq_event_release(phba, cq_event);
6154 spin_unlock_irqrestore(&phba->hbalock, iflags);
6155}
6156
6157/**
6158 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6159 * @phba: pointer to lpfc hba data structure.
6160 *
6161 * This routine is to free all the pending completion-queue events to the
6162 * back into the free pool for device reset.
6163 **/
6164static void
6165lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6166{
6167 LIST_HEAD(cqelist);
6168 struct lpfc_cq_event *cqe;
6169 unsigned long iflags;
6170
6171 /* Retrieve all the pending WCQEs from pending WCQE lists */
6172 spin_lock_irqsave(&phba->hbalock, iflags);
6173 /* Pending FCP XRI abort events */
6174 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6175 &cqelist);
6176 /* Pending ELS XRI abort events */
6177 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6178 &cqelist);
6179 /* Pending asynnc events */
6180 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6181 &cqelist);
6182 spin_unlock_irqrestore(&phba->hbalock, iflags);
6183
6184 while (!list_empty(&cqelist)) {
6185 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6186 lpfc_sli4_cq_event_release(phba, cqe);
6187 }
6188}
6189
6190/**
6191 * lpfc_pci_function_reset - Reset pci function.
6192 * @phba: pointer to lpfc hba data structure.
6193 *
6194 * This routine is invoked to request a PCI function reset. It will destroys
6195 * all resources assigned to the PCI function which originates this request.
6196 *
6197 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006198 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006199 * ENOMEM - No availble memory
6200 * EIO - The mailbox failed to complete successfully.
6201 **/
6202int
6203lpfc_pci_function_reset(struct lpfc_hba *phba)
6204{
6205 LPFC_MBOXQ_t *mboxq;
6206 uint32_t rc = 0;
6207 uint32_t shdr_status, shdr_add_status;
6208 union lpfc_sli4_cfg_shdr *shdr;
6209
6210 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6211 if (!mboxq) {
6212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6213 "0494 Unable to allocate memory for issuing "
6214 "SLI_FUNCTION_RESET mailbox command\n");
6215 return -ENOMEM;
6216 }
6217
6218 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6219 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6220 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6221 LPFC_SLI4_MBX_EMBED);
6222 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6223 shdr = (union lpfc_sli4_cfg_shdr *)
6224 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6225 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6226 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6227 if (rc != MBX_TIMEOUT)
6228 mempool_free(mboxq, phba->mbox_mem_pool);
6229 if (shdr_status || shdr_add_status || rc) {
6230 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6231 "0495 SLI_FUNCTION_RESET mailbox failed with "
6232 "status x%x add_status x%x, mbx status x%x\n",
6233 shdr_status, shdr_add_status, rc);
6234 rc = -ENXIO;
6235 }
6236 return rc;
6237}
6238
6239/**
6240 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6241 * @phba: pointer to lpfc hba data structure.
6242 * @cnt: number of nop mailbox commands to send.
6243 *
6244 * This routine is invoked to send a number @cnt of NOP mailbox command and
6245 * wait for each command to complete.
6246 *
6247 * Return: the number of NOP mailbox command completed.
6248 **/
6249static int
6250lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6251{
6252 LPFC_MBOXQ_t *mboxq;
6253 int length, cmdsent;
6254 uint32_t mbox_tmo;
6255 uint32_t rc = 0;
6256 uint32_t shdr_status, shdr_add_status;
6257 union lpfc_sli4_cfg_shdr *shdr;
6258
6259 if (cnt == 0) {
6260 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6261 "2518 Requested to send 0 NOP mailbox cmd\n");
6262 return cnt;
6263 }
6264
6265 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6266 if (!mboxq) {
6267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6268 "2519 Unable to allocate memory for issuing "
6269 "NOP mailbox command\n");
6270 return 0;
6271 }
6272
6273 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6274 length = (sizeof(struct lpfc_mbx_nop) -
6275 sizeof(struct lpfc_sli4_cfg_mhdr));
6276 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6277 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6278
6279 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6280 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6281 if (!phba->sli4_hba.intr_enable)
6282 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6283 else
6284 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6285 if (rc == MBX_TIMEOUT)
6286 break;
6287 /* Check return status */
6288 shdr = (union lpfc_sli4_cfg_shdr *)
6289 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6290 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6291 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6292 &shdr->response);
6293 if (shdr_status || shdr_add_status || rc) {
6294 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6295 "2520 NOP mailbox command failed "
6296 "status x%x add_status x%x mbx "
6297 "status x%x\n", shdr_status,
6298 shdr_add_status, rc);
6299 break;
6300 }
6301 }
6302
6303 if (rc != MBX_TIMEOUT)
6304 mempool_free(mboxq, phba->mbox_mem_pool);
6305
6306 return cmdsent;
6307}
6308
6309/**
6310 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6311 * @phba: pointer to lpfc hba data structure.
6312 * @fcfi: fcf index.
6313 *
6314 * This routine is invoked to unregister a FCFI from device.
6315 **/
6316void
6317lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6318{
6319 LPFC_MBOXQ_t *mbox;
6320 uint32_t mbox_tmo;
6321 int rc;
6322 unsigned long flags;
6323
6324 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6325
6326 if (!mbox)
6327 return;
6328
6329 lpfc_unreg_fcfi(mbox, fcfi);
6330
6331 if (!phba->sli4_hba.intr_enable)
6332 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6333 else {
6334 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6335 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6336 }
6337 if (rc != MBX_TIMEOUT)
6338 mempool_free(mbox, phba->mbox_mem_pool);
6339 if (rc != MBX_SUCCESS)
6340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6341 "2517 Unregister FCFI command failed "
6342 "status %d, mbxStatus x%x\n", rc,
6343 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6344 else {
6345 spin_lock_irqsave(&phba->hbalock, flags);
6346 /* Mark the FCFI is no longer registered */
6347 phba->fcf.fcf_flag &=
James Smartecfd03c2010-02-12 14:41:27 -05006348 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
James Smartda0436e2009-05-22 14:51:39 -04006349 spin_unlock_irqrestore(&phba->hbalock, flags);
6350 }
6351}
6352
6353/**
6354 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6355 * @phba: pointer to lpfc hba data structure.
6356 *
6357 * This routine is invoked to set up the PCI device memory space for device
6358 * with SLI-4 interface spec.
6359 *
6360 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006361 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006362 * other values - error
6363 **/
6364static int
6365lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6366{
6367 struct pci_dev *pdev;
6368 unsigned long bar0map_len, bar1map_len, bar2map_len;
6369 int error = -ENODEV;
6370
6371 /* Obtain PCI device reference */
6372 if (!phba->pcidev)
6373 return error;
6374 else
6375 pdev = phba->pcidev;
6376
6377 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006378 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6379 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6380 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6381 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006382 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006383 }
6384 }
James Smartda0436e2009-05-22 14:51:39 -04006385
6386 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6387 * number of bytes required by each mapping. They are actually
James Smart1dfb5a42010-02-12 14:40:50 -05006388 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006389 */
James Smart1dfb5a42010-02-12 14:40:50 -05006390 if (pci_resource_start(pdev, 0)) {
6391 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6392 bar0map_len = pci_resource_len(pdev, 0);
6393 } else {
6394 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6395 bar0map_len = pci_resource_len(pdev, 1);
6396 }
6397 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6398 bar1map_len = pci_resource_len(pdev, 2);
James Smartda0436e2009-05-22 14:51:39 -04006399
James Smart1dfb5a42010-02-12 14:40:50 -05006400 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6401 bar2map_len = pci_resource_len(pdev, 4);
James Smartda0436e2009-05-22 14:51:39 -04006402
6403 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6404 phba->sli4_hba.conf_regs_memmap_p =
6405 ioremap(phba->pci_bar0_map, bar0map_len);
6406 if (!phba->sli4_hba.conf_regs_memmap_p) {
6407 dev_printk(KERN_ERR, &pdev->dev,
6408 "ioremap failed for SLI4 PCI config registers.\n");
6409 goto out;
6410 }
6411
6412 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6413 phba->sli4_hba.ctrl_regs_memmap_p =
6414 ioremap(phba->pci_bar1_map, bar1map_len);
6415 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6416 dev_printk(KERN_ERR, &pdev->dev,
6417 "ioremap failed for SLI4 HBA control registers.\n");
6418 goto out_iounmap_conf;
6419 }
6420
6421 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6422 phba->sli4_hba.drbl_regs_memmap_p =
6423 ioremap(phba->pci_bar2_map, bar2map_len);
6424 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6425 dev_printk(KERN_ERR, &pdev->dev,
6426 "ioremap failed for SLI4 HBA doorbell registers.\n");
6427 goto out_iounmap_ctrl;
6428 }
6429
6430 /* Set up BAR0 PCI config space register memory map */
6431 lpfc_sli4_bar0_register_memmap(phba);
6432
6433 /* Set up BAR1 register memory map */
6434 lpfc_sli4_bar1_register_memmap(phba);
6435
6436 /* Set up BAR2 register memory map */
6437 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6438 if (error)
6439 goto out_iounmap_all;
6440
6441 return 0;
6442
6443out_iounmap_all:
6444 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6445out_iounmap_ctrl:
6446 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6447out_iounmap_conf:
6448 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6449out:
6450 return error;
6451}
6452
6453/**
6454 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6455 * @phba: pointer to lpfc hba data structure.
6456 *
6457 * This routine is invoked to unset the PCI device memory space for device
6458 * with SLI-4 interface spec.
6459 **/
6460static void
6461lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6462{
6463 struct pci_dev *pdev;
6464
6465 /* Obtain PCI device reference */
6466 if (!phba->pcidev)
6467 return;
6468 else
6469 pdev = phba->pcidev;
6470
6471 /* Free coherent DMA memory allocated */
6472
6473 /* Unmap I/O memory space */
6474 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6475 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6476 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6477
6478 return;
6479}
6480
6481/**
James Smart3772a992009-05-22 14:50:54 -04006482 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6483 * @phba: pointer to lpfc hba data structure.
6484 *
6485 * This routine is invoked to enable the MSI-X interrupt vectors to device
6486 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6487 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6488 * invoked, enables either all or nothing, depending on the current
6489 * availability of PCI vector resources. The device driver is responsible
6490 * for calling the individual request_irq() to register each MSI-X vector
6491 * with a interrupt handler, which is done in this function. Note that
6492 * later when device is unloading, the driver should always call free_irq()
6493 * on all MSI-X vectors it has done request_irq() on before calling
6494 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6495 * will be left with MSI-X enabled and leaks its vectors.
6496 *
6497 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006498 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006499 * other values - error
6500 **/
6501static int
6502lpfc_sli_enable_msix(struct lpfc_hba *phba)
6503{
6504 int rc, i;
6505 LPFC_MBOXQ_t *pmb;
6506
6507 /* Set up MSI-X multi-message vectors */
6508 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6509 phba->msix_entries[i].entry = i;
6510
6511 /* Configure MSI-X capability structure */
6512 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6513 ARRAY_SIZE(phba->msix_entries));
6514 if (rc) {
6515 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6516 "0420 PCI enable MSI-X failed (%d)\n", rc);
6517 goto msi_fail_out;
6518 }
6519 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6521 "0477 MSI-X entry[%d]: vector=x%x "
6522 "message=%d\n", i,
6523 phba->msix_entries[i].vector,
6524 phba->msix_entries[i].entry);
6525 /*
6526 * Assign MSI-X vectors to interrupt handlers
6527 */
6528
6529 /* vector-0 is associated to slow-path handler */
6530 rc = request_irq(phba->msix_entries[0].vector,
6531 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6532 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6533 if (rc) {
6534 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6535 "0421 MSI-X slow-path request_irq failed "
6536 "(%d)\n", rc);
6537 goto msi_fail_out;
6538 }
6539
6540 /* vector-1 is associated to fast-path handler */
6541 rc = request_irq(phba->msix_entries[1].vector,
6542 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6543 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6544
6545 if (rc) {
6546 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6547 "0429 MSI-X fast-path request_irq failed "
6548 "(%d)\n", rc);
6549 goto irq_fail_out;
6550 }
6551
6552 /*
6553 * Configure HBA MSI-X attention conditions to messages
6554 */
6555 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6556
6557 if (!pmb) {
6558 rc = -ENOMEM;
6559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6560 "0474 Unable to allocate memory for issuing "
6561 "MBOX_CONFIG_MSI command\n");
6562 goto mem_fail_out;
6563 }
6564 rc = lpfc_config_msi(phba, pmb);
6565 if (rc)
6566 goto mbx_fail_out;
6567 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6568 if (rc != MBX_SUCCESS) {
6569 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6570 "0351 Config MSI mailbox command failed, "
6571 "mbxCmd x%x, mbxStatus x%x\n",
6572 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6573 goto mbx_fail_out;
6574 }
6575
6576 /* Free memory allocated for mailbox command */
6577 mempool_free(pmb, phba->mbox_mem_pool);
6578 return rc;
6579
6580mbx_fail_out:
6581 /* Free memory allocated for mailbox command */
6582 mempool_free(pmb, phba->mbox_mem_pool);
6583
6584mem_fail_out:
6585 /* free the irq already requested */
6586 free_irq(phba->msix_entries[1].vector, phba);
6587
6588irq_fail_out:
6589 /* free the irq already requested */
6590 free_irq(phba->msix_entries[0].vector, phba);
6591
6592msi_fail_out:
6593 /* Unconfigure MSI-X capability structure */
6594 pci_disable_msix(phba->pcidev);
6595 return rc;
6596}
6597
6598/**
6599 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6600 * @phba: pointer to lpfc hba data structure.
6601 *
6602 * This routine is invoked to release the MSI-X vectors and then disable the
6603 * MSI-X interrupt mode to device with SLI-3 interface spec.
6604 **/
6605static void
6606lpfc_sli_disable_msix(struct lpfc_hba *phba)
6607{
6608 int i;
6609
6610 /* Free up MSI-X multi-message vectors */
6611 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6612 free_irq(phba->msix_entries[i].vector, phba);
6613 /* Disable MSI-X */
6614 pci_disable_msix(phba->pcidev);
6615
6616 return;
6617}
6618
6619/**
6620 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6621 * @phba: pointer to lpfc hba data structure.
6622 *
6623 * This routine is invoked to enable the MSI interrupt mode to device with
6624 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6625 * enable the MSI vector. The device driver is responsible for calling the
6626 * request_irq() to register MSI vector with a interrupt the handler, which
6627 * is done in this function.
6628 *
6629 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006630 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006631 * other values - error
6632 */
6633static int
6634lpfc_sli_enable_msi(struct lpfc_hba *phba)
6635{
6636 int rc;
6637
6638 rc = pci_enable_msi(phba->pcidev);
6639 if (!rc)
6640 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6641 "0462 PCI enable MSI mode success.\n");
6642 else {
6643 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6644 "0471 PCI enable MSI mode failed (%d)\n", rc);
6645 return rc;
6646 }
6647
6648 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6649 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6650 if (rc) {
6651 pci_disable_msi(phba->pcidev);
6652 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6653 "0478 MSI request_irq failed (%d)\n", rc);
6654 }
6655 return rc;
6656}
6657
6658/**
6659 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6660 * @phba: pointer to lpfc hba data structure.
6661 *
6662 * This routine is invoked to disable the MSI interrupt mode to device with
6663 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6664 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6665 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6666 * its vector.
6667 */
6668static void
6669lpfc_sli_disable_msi(struct lpfc_hba *phba)
6670{
6671 free_irq(phba->pcidev->irq, phba);
6672 pci_disable_msi(phba->pcidev);
6673 return;
6674}
6675
6676/**
6677 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6678 * @phba: pointer to lpfc hba data structure.
6679 *
6680 * This routine is invoked to enable device interrupt and associate driver's
6681 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6682 * spec. Depends on the interrupt mode configured to the driver, the driver
6683 * will try to fallback from the configured interrupt mode to an interrupt
6684 * mode which is supported by the platform, kernel, and device in the order
6685 * of:
6686 * MSI-X -> MSI -> IRQ.
6687 *
6688 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006689 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006690 * other values - error
6691 **/
6692static uint32_t
6693lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6694{
6695 uint32_t intr_mode = LPFC_INTR_ERROR;
6696 int retval;
6697
6698 if (cfg_mode == 2) {
6699 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6700 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6701 if (!retval) {
6702 /* Now, try to enable MSI-X interrupt mode */
6703 retval = lpfc_sli_enable_msix(phba);
6704 if (!retval) {
6705 /* Indicate initialization to MSI-X mode */
6706 phba->intr_type = MSIX;
6707 intr_mode = 2;
6708 }
6709 }
6710 }
6711
6712 /* Fallback to MSI if MSI-X initialization failed */
6713 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6714 retval = lpfc_sli_enable_msi(phba);
6715 if (!retval) {
6716 /* Indicate initialization to MSI mode */
6717 phba->intr_type = MSI;
6718 intr_mode = 1;
6719 }
6720 }
6721
6722 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6723 if (phba->intr_type == NONE) {
6724 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6725 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6726 if (!retval) {
6727 /* Indicate initialization to INTx mode */
6728 phba->intr_type = INTx;
6729 intr_mode = 0;
6730 }
6731 }
6732 return intr_mode;
6733}
6734
6735/**
6736 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6737 * @phba: pointer to lpfc hba data structure.
6738 *
6739 * This routine is invoked to disable device interrupt and disassociate the
6740 * driver's interrupt handler(s) from interrupt vector(s) to device with
6741 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6742 * release the interrupt vector(s) for the message signaled interrupt.
6743 **/
6744static void
6745lpfc_sli_disable_intr(struct lpfc_hba *phba)
6746{
6747 /* Disable the currently initialized interrupt mode */
6748 if (phba->intr_type == MSIX)
6749 lpfc_sli_disable_msix(phba);
6750 else if (phba->intr_type == MSI)
6751 lpfc_sli_disable_msi(phba);
6752 else if (phba->intr_type == INTx)
6753 free_irq(phba->pcidev->irq, phba);
6754
6755 /* Reset interrupt management states */
6756 phba->intr_type = NONE;
6757 phba->sli.slistat.sli_intr = 0;
6758
6759 return;
6760}
6761
6762/**
James Smartda0436e2009-05-22 14:51:39 -04006763 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6764 * @phba: pointer to lpfc hba data structure.
6765 *
6766 * This routine is invoked to enable the MSI-X interrupt vectors to device
6767 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6768 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6769 * enables either all or nothing, depending on the current availability of
6770 * PCI vector resources. The device driver is responsible for calling the
6771 * individual request_irq() to register each MSI-X vector with a interrupt
6772 * handler, which is done in this function. Note that later when device is
6773 * unloading, the driver should always call free_irq() on all MSI-X vectors
6774 * it has done request_irq() on before calling pci_disable_msix(). Failure
6775 * to do so results in a BUG_ON() and a device will be left with MSI-X
6776 * enabled and leaks its vectors.
6777 *
6778 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006779 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006780 * other values - error
6781 **/
6782static int
6783lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6784{
6785 int rc, index;
6786
6787 /* Set up MSI-X multi-message vectors */
6788 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6789 phba->sli4_hba.msix_entries[index].entry = index;
6790
6791 /* Configure MSI-X capability structure */
6792 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6793 phba->sli4_hba.cfg_eqn);
6794 if (rc) {
6795 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6796 "0484 PCI enable MSI-X failed (%d)\n", rc);
6797 goto msi_fail_out;
6798 }
6799 /* Log MSI-X vector assignment */
6800 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6801 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6802 "0489 MSI-X entry[%d]: vector=x%x "
6803 "message=%d\n", index,
6804 phba->sli4_hba.msix_entries[index].vector,
6805 phba->sli4_hba.msix_entries[index].entry);
6806 /*
6807 * Assign MSI-X vectors to interrupt handlers
6808 */
6809
6810 /* The first vector must associated to slow-path handler for MQ */
6811 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6812 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6813 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6814 if (rc) {
6815 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6816 "0485 MSI-X slow-path request_irq failed "
6817 "(%d)\n", rc);
6818 goto msi_fail_out;
6819 }
6820
6821 /* The rest of the vector(s) are associated to fast-path handler(s) */
6822 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6823 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6824 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6825 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6826 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6827 LPFC_FP_DRIVER_HANDLER_NAME,
6828 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6829 if (rc) {
6830 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6831 "0486 MSI-X fast-path (%d) "
6832 "request_irq failed (%d)\n", index, rc);
6833 goto cfg_fail_out;
6834 }
6835 }
6836
6837 return rc;
6838
6839cfg_fail_out:
6840 /* free the irq already requested */
6841 for (--index; index >= 1; index--)
6842 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6843 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6844
6845 /* free the irq already requested */
6846 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6847
6848msi_fail_out:
6849 /* Unconfigure MSI-X capability structure */
6850 pci_disable_msix(phba->pcidev);
6851 return rc;
6852}
6853
6854/**
6855 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6856 * @phba: pointer to lpfc hba data structure.
6857 *
6858 * This routine is invoked to release the MSI-X vectors and then disable the
6859 * MSI-X interrupt mode to device with SLI-4 interface spec.
6860 **/
6861static void
6862lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6863{
6864 int index;
6865
6866 /* Free up MSI-X multi-message vectors */
6867 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6868
6869 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6870 free_irq(phba->sli4_hba.msix_entries[index].vector,
6871 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6872 /* Disable MSI-X */
6873 pci_disable_msix(phba->pcidev);
6874
6875 return;
6876}
6877
6878/**
6879 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6880 * @phba: pointer to lpfc hba data structure.
6881 *
6882 * This routine is invoked to enable the MSI interrupt mode to device with
6883 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6884 * to enable the MSI vector. The device driver is responsible for calling
6885 * the request_irq() to register MSI vector with a interrupt the handler,
6886 * which is done in this function.
6887 *
6888 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006889 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006890 * other values - error
6891 **/
6892static int
6893lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6894{
6895 int rc, index;
6896
6897 rc = pci_enable_msi(phba->pcidev);
6898 if (!rc)
6899 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6900 "0487 PCI enable MSI mode success.\n");
6901 else {
6902 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6903 "0488 PCI enable MSI mode failed (%d)\n", rc);
6904 return rc;
6905 }
6906
6907 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6908 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6909 if (rc) {
6910 pci_disable_msi(phba->pcidev);
6911 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6912 "0490 MSI request_irq failed (%d)\n", rc);
6913 }
6914
6915 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6916 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6917 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6918 }
6919
6920 return rc;
6921}
6922
6923/**
6924 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6925 * @phba: pointer to lpfc hba data structure.
6926 *
6927 * This routine is invoked to disable the MSI interrupt mode to device with
6928 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6929 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6930 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6931 * its vector.
6932 **/
6933static void
6934lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6935{
6936 free_irq(phba->pcidev->irq, phba);
6937 pci_disable_msi(phba->pcidev);
6938 return;
6939}
6940
6941/**
6942 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6943 * @phba: pointer to lpfc hba data structure.
6944 *
6945 * This routine is invoked to enable device interrupt and associate driver's
6946 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6947 * interface spec. Depends on the interrupt mode configured to the driver,
6948 * the driver will try to fallback from the configured interrupt mode to an
6949 * interrupt mode which is supported by the platform, kernel, and device in
6950 * the order of:
6951 * MSI-X -> MSI -> IRQ.
6952 *
6953 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006954 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006955 * other values - error
6956 **/
6957static uint32_t
6958lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6959{
6960 uint32_t intr_mode = LPFC_INTR_ERROR;
6961 int retval, index;
6962
6963 if (cfg_mode == 2) {
6964 /* Preparation before conf_msi mbox cmd */
6965 retval = 0;
6966 if (!retval) {
6967 /* Now, try to enable MSI-X interrupt mode */
6968 retval = lpfc_sli4_enable_msix(phba);
6969 if (!retval) {
6970 /* Indicate initialization to MSI-X mode */
6971 phba->intr_type = MSIX;
6972 intr_mode = 2;
6973 }
6974 }
6975 }
6976
6977 /* Fallback to MSI if MSI-X initialization failed */
6978 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6979 retval = lpfc_sli4_enable_msi(phba);
6980 if (!retval) {
6981 /* Indicate initialization to MSI mode */
6982 phba->intr_type = MSI;
6983 intr_mode = 1;
6984 }
6985 }
6986
6987 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6988 if (phba->intr_type == NONE) {
6989 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6990 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6991 if (!retval) {
6992 /* Indicate initialization to INTx mode */
6993 phba->intr_type = INTx;
6994 intr_mode = 0;
6995 for (index = 0; index < phba->cfg_fcp_eq_count;
6996 index++) {
6997 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6998 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6999 }
7000 }
7001 }
7002 return intr_mode;
7003}
7004
7005/**
7006 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7007 * @phba: pointer to lpfc hba data structure.
7008 *
7009 * This routine is invoked to disable device interrupt and disassociate
7010 * the driver's interrupt handler(s) from interrupt vector(s) to device
7011 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7012 * will release the interrupt vector(s) for the message signaled interrupt.
7013 **/
7014static void
7015lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7016{
7017 /* Disable the currently initialized interrupt mode */
7018 if (phba->intr_type == MSIX)
7019 lpfc_sli4_disable_msix(phba);
7020 else if (phba->intr_type == MSI)
7021 lpfc_sli4_disable_msi(phba);
7022 else if (phba->intr_type == INTx)
7023 free_irq(phba->pcidev->irq, phba);
7024
7025 /* Reset interrupt management states */
7026 phba->intr_type = NONE;
7027 phba->sli.slistat.sli_intr = 0;
7028
7029 return;
7030}
7031
7032/**
James Smart3772a992009-05-22 14:50:54 -04007033 * lpfc_unset_hba - Unset SLI3 hba device initialization
7034 * @phba: pointer to lpfc hba data structure.
7035 *
7036 * This routine is invoked to unset the HBA device initialization steps to
7037 * a device with SLI-3 interface spec.
7038 **/
7039static void
7040lpfc_unset_hba(struct lpfc_hba *phba)
7041{
7042 struct lpfc_vport *vport = phba->pport;
7043 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7044
7045 spin_lock_irq(shost->host_lock);
7046 vport->load_flag |= FC_UNLOADING;
7047 spin_unlock_irq(shost->host_lock);
7048
7049 lpfc_stop_hba_timers(phba);
7050
7051 phba->pport->work_port_events = 0;
7052
7053 lpfc_sli_hba_down(phba);
7054
7055 lpfc_sli_brdrestart(phba);
7056
7057 lpfc_sli_disable_intr(phba);
7058
7059 return;
7060}
7061
7062/**
James Smartda0436e2009-05-22 14:51:39 -04007063 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7064 * @phba: pointer to lpfc hba data structure.
7065 *
7066 * This routine is invoked to unset the HBA device initialization steps to
7067 * a device with SLI-4 interface spec.
7068 **/
7069static void
7070lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7071{
7072 struct lpfc_vport *vport = phba->pport;
7073 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7074
7075 spin_lock_irq(shost->host_lock);
7076 vport->load_flag |= FC_UNLOADING;
7077 spin_unlock_irq(shost->host_lock);
7078
7079 phba->pport->work_port_events = 0;
7080
7081 lpfc_sli4_hba_down(phba);
7082
7083 lpfc_sli4_disable_intr(phba);
7084
7085 return;
7086}
7087
7088/**
7089 * lpfc_sli4_hba_unset - Unset the fcoe hba
7090 * @phba: Pointer to HBA context object.
7091 *
7092 * This function is called in the SLI4 code path to reset the HBA's FCoE
7093 * function. The caller is not required to hold any lock. This routine
7094 * issues PCI function reset mailbox command to reset the FCoE function.
7095 * At the end of the function, it calls lpfc_hba_down_post function to
7096 * free any pending commands.
7097 **/
7098static void
7099lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7100{
7101 int wait_cnt = 0;
7102 LPFC_MBOXQ_t *mboxq;
7103
7104 lpfc_stop_hba_timers(phba);
7105 phba->sli4_hba.intr_enable = 0;
7106
7107 /*
7108 * Gracefully wait out the potential current outstanding asynchronous
7109 * mailbox command.
7110 */
7111
7112 /* First, block any pending async mailbox command from posted */
7113 spin_lock_irq(&phba->hbalock);
7114 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7115 spin_unlock_irq(&phba->hbalock);
7116 /* Now, trying to wait it out if we can */
7117 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7118 msleep(10);
7119 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7120 break;
7121 }
7122 /* Forcefully release the outstanding mailbox command if timed out */
7123 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7124 spin_lock_irq(&phba->hbalock);
7125 mboxq = phba->sli.mbox_active;
7126 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7127 __lpfc_mbox_cmpl_put(phba, mboxq);
7128 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7129 phba->sli.mbox_active = NULL;
7130 spin_unlock_irq(&phba->hbalock);
7131 }
7132
7133 /* Tear down the queues in the HBA */
7134 lpfc_sli4_queue_unset(phba);
7135
7136 /* Disable PCI subsystem interrupt */
7137 lpfc_sli4_disable_intr(phba);
7138
7139 /* Stop kthread signal shall trigger work_done one more time */
7140 kthread_stop(phba->worker_thread);
7141
7142 /* Stop the SLI4 device port */
7143 phba->pport->work_port_events = 0;
7144}
7145
James Smart28baac72010-02-12 14:42:03 -05007146 /**
7147 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7148 * @phba: Pointer to HBA context object.
7149 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7150 *
7151 * This function is called in the SLI4 code path to read the port's
7152 * sli4 capabilities.
7153 *
7154 * This function may be be called from any context that can block-wait
7155 * for the completion. The expectation is that this routine is called
7156 * typically from probe_one or from the online routine.
7157 **/
7158int
7159lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7160{
7161 int rc;
7162 struct lpfc_mqe *mqe;
7163 struct lpfc_pc_sli4_params *sli4_params;
7164 uint32_t mbox_tmo;
7165
7166 rc = 0;
7167 mqe = &mboxq->u.mqe;
7168
7169 /* Read the port's SLI4 Parameters port capabilities */
7170 lpfc_sli4_params(mboxq);
7171 if (!phba->sli4_hba.intr_enable)
7172 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7173 else {
7174 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7175 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7176 }
7177
7178 if (unlikely(rc))
7179 return 1;
7180
7181 sli4_params = &phba->sli4_hba.pc_sli4_params;
7182 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7183 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7184 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7185 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7186 &mqe->un.sli4_params);
7187 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7188 &mqe->un.sli4_params);
7189 sli4_params->proto_types = mqe->un.sli4_params.word3;
7190 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7191 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7192 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7193 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7194 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7195 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7196 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7197 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7198 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7199 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7200 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7201 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7202 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7203 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7204 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7205 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7206 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7207 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7208 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7209 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7210 return rc;
7211}
7212
James Smartda0436e2009-05-22 14:51:39 -04007213/**
James Smart3772a992009-05-22 14:50:54 -04007214 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7215 * @pdev: pointer to PCI device
7216 * @pid: pointer to PCI device identifier
7217 *
7218 * This routine is to be called to attach a device with SLI-3 interface spec
7219 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7220 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7221 * information of the device and driver to see if the driver state that it can
7222 * support this kind of device. If the match is successful, the driver core
7223 * invokes this routine. If this routine determines it can claim the HBA, it
7224 * does all the initialization that it needs to do to handle the HBA properly.
7225 *
7226 * Return code
7227 * 0 - driver can claim the device
7228 * negative value - driver can not claim the device
7229 **/
7230static int __devinit
7231lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7232{
7233 struct lpfc_hba *phba;
7234 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007235 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007236 int error;
7237 uint32_t cfg_mode, intr_mode;
7238
7239 /* Allocate memory for HBA structure */
7240 phba = lpfc_hba_alloc(pdev);
7241 if (!phba)
7242 return -ENOMEM;
7243
7244 /* Perform generic PCI device enabling operation */
7245 error = lpfc_enable_pci_dev(phba);
7246 if (error) {
7247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7248 "1401 Failed to enable pci device.\n");
7249 goto out_free_phba;
7250 }
7251
7252 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7253 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7254 if (error)
7255 goto out_disable_pci_dev;
7256
7257 /* Set up SLI-3 specific device PCI memory space */
7258 error = lpfc_sli_pci_mem_setup(phba);
7259 if (error) {
7260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7261 "1402 Failed to set up pci memory space.\n");
7262 goto out_disable_pci_dev;
7263 }
7264
7265 /* Set up phase-1 common device driver resources */
7266 error = lpfc_setup_driver_resource_phase1(phba);
7267 if (error) {
7268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7269 "1403 Failed to set up driver resource.\n");
7270 goto out_unset_pci_mem_s3;
7271 }
7272
7273 /* Set up SLI-3 specific device driver resources */
7274 error = lpfc_sli_driver_resource_setup(phba);
7275 if (error) {
7276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7277 "1404 Failed to set up driver resource.\n");
7278 goto out_unset_pci_mem_s3;
7279 }
7280
7281 /* Initialize and populate the iocb list per host */
7282 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7283 if (error) {
7284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7285 "1405 Failed to initialize iocb list.\n");
7286 goto out_unset_driver_resource_s3;
7287 }
7288
7289 /* Set up common device driver resources */
7290 error = lpfc_setup_driver_resource_phase2(phba);
7291 if (error) {
7292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7293 "1406 Failed to set up driver resource.\n");
7294 goto out_free_iocb_list;
7295 }
7296
7297 /* Create SCSI host to the physical port */
7298 error = lpfc_create_shost(phba);
7299 if (error) {
7300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7301 "1407 Failed to create scsi host.\n");
7302 goto out_unset_driver_resource;
7303 }
7304
7305 /* Configure sysfs attributes */
7306 vport = phba->pport;
7307 error = lpfc_alloc_sysfs_attr(vport);
7308 if (error) {
7309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7310 "1476 Failed to allocate sysfs attr\n");
7311 goto out_destroy_shost;
7312 }
7313
James Smart6669f9b2009-10-02 15:16:45 -04007314 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04007315 /* Now, trying to enable interrupt and bring up the device */
7316 cfg_mode = phba->cfg_use_msi;
7317 while (true) {
7318 /* Put device to a known state before enabling interrupt */
7319 lpfc_stop_port(phba);
7320 /* Configure and enable interrupt */
7321 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7322 if (intr_mode == LPFC_INTR_ERROR) {
7323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7324 "0431 Failed to enable interrupt.\n");
7325 error = -ENODEV;
7326 goto out_free_sysfs_attr;
7327 }
7328 /* SLI-3 HBA setup */
7329 if (lpfc_sli_hba_setup(phba)) {
7330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7331 "1477 Failed to set up hba\n");
7332 error = -ENODEV;
7333 goto out_remove_device;
7334 }
7335
7336 /* Wait 50ms for the interrupts of previous mailbox commands */
7337 msleep(50);
7338 /* Check active interrupts on message signaled interrupts */
7339 if (intr_mode == 0 ||
7340 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7341 /* Log the current active interrupt mode */
7342 phba->intr_mode = intr_mode;
7343 lpfc_log_intr_mode(phba, intr_mode);
7344 break;
7345 } else {
7346 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7347 "0447 Configure interrupt mode (%d) "
7348 "failed active interrupt test.\n",
7349 intr_mode);
7350 /* Disable the current interrupt mode */
7351 lpfc_sli_disable_intr(phba);
7352 /* Try next level of interrupt mode */
7353 cfg_mode = --intr_mode;
7354 }
7355 }
7356
7357 /* Perform post initialization setup */
7358 lpfc_post_init_setup(phba);
7359
7360 /* Check if there are static vports to be created. */
7361 lpfc_create_static_vport(phba);
7362
7363 return 0;
7364
7365out_remove_device:
7366 lpfc_unset_hba(phba);
7367out_free_sysfs_attr:
7368 lpfc_free_sysfs_attr(vport);
7369out_destroy_shost:
7370 lpfc_destroy_shost(phba);
7371out_unset_driver_resource:
7372 lpfc_unset_driver_resource_phase2(phba);
7373out_free_iocb_list:
7374 lpfc_free_iocb_list(phba);
7375out_unset_driver_resource_s3:
7376 lpfc_sli_driver_resource_unset(phba);
7377out_unset_pci_mem_s3:
7378 lpfc_sli_pci_mem_unset(phba);
7379out_disable_pci_dev:
7380 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007381 if (shost)
7382 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007383out_free_phba:
7384 lpfc_hba_free(phba);
7385 return error;
7386}
7387
7388/**
7389 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007390 * @pdev: pointer to PCI device
7391 *
James Smart3772a992009-05-22 14:50:54 -04007392 * This routine is to be called to disattach a device with SLI-3 interface
7393 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7394 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7395 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007396 **/
dea31012005-04-17 16:05:31 -05007397static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007398lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007399{
James Smart2e0fef82007-06-17 19:56:36 -05007400 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7401 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007402 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007403 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007404 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007405 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7406
James Smart549e55c2007-08-02 11:09:51 -04007407 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007408 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007409 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007410
James Smart858c9f62007-06-17 19:56:39 -05007411 lpfc_free_sysfs_attr(vport);
7412
James Smarteada2722008-12-04 22:39:13 -05007413 /* Release all the vports against this physical port */
7414 vports = lpfc_create_vport_work_array(phba);
7415 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007416 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007417 fc_vport_terminate(vports[i]->fc_vport);
7418 lpfc_destroy_vport_work_array(phba, vports);
7419
7420 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007421 fc_remove_host(shost);
7422 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007423 lpfc_cleanup(vport);
7424
James Smart2e0fef82007-06-17 19:56:36 -05007425 /*
7426 * Bring down the SLI Layer. This step disable all interrupts,
7427 * clears the rings, discards all mailbox commands, and resets
7428 * the HBA.
7429 */
James Smarta257bf92009-04-06 18:48:10 -04007430
7431 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007432 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007433 /* Stop kthread signal shall trigger work_done one more time */
7434 kthread_stop(phba->worker_thread);
7435 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007436 lpfc_sli_brdrestart(phba);
7437
James Smart3772a992009-05-22 14:50:54 -04007438 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007439 spin_lock_irq(&phba->hbalock);
7440 list_del_init(&vport->listentry);
7441 spin_unlock_irq(&phba->hbalock);
7442
James Smart858c9f62007-06-17 19:56:39 -05007443 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007444
James Smart5b75da22008-12-04 22:39:35 -05007445 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007446 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007447
7448 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007449 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007450
7451 /*
7452 * Call scsi_free before mem_free since scsi bufs are released to their
7453 * corresponding pools here.
7454 */
7455 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007456 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007457
James Smart34b02dc2008-08-24 21:49:55 -04007458 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7459 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007460
James Smart2e0fef82007-06-17 19:56:36 -05007461 /* Free resources associated with SLI2 interface */
7462 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007463 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007464
7465 /* unmap adapter SLIM and Control Registers */
7466 iounmap(phba->ctrl_regs_memmap_p);
7467 iounmap(phba->slim_memmap_p);
7468
James Smart3772a992009-05-22 14:50:54 -04007469 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007470
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007471 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007472 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007473}
7474
Linas Vepstas8d63f372007-02-14 14:28:36 -06007475/**
James Smart3772a992009-05-22 14:50:54 -04007476 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007477 * @pdev: pointer to PCI device
7478 * @msg: power management message
7479 *
James Smart3772a992009-05-22 14:50:54 -04007480 * This routine is to be called from the kernel's PCI subsystem to support
7481 * system Power Management (PM) to device with SLI-3 interface spec. When
7482 * PM invokes this method, it quiesces the device by stopping the driver's
7483 * worker thread for the device, turning off device's interrupt and DMA,
7484 * and bring the device offline. Note that as the driver implements the
7485 * minimum PM requirements to a power-aware driver's PM support for the
7486 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7487 * to the suspend() method call will be treated as SUSPEND and the driver will
7488 * fully reinitialize its device during resume() method call, the driver will
7489 * set device to PCI_D3hot state in PCI config space instead of setting it
7490 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007491 *
7492 * Return code
James Smart3772a992009-05-22 14:50:54 -04007493 * 0 - driver suspended the device
7494 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007495 **/
7496static int
James Smart3772a992009-05-22 14:50:54 -04007497lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007498{
7499 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7500 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7501
7502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7503 "0473 PCI device Power Management suspend.\n");
7504
7505 /* Bring down the device */
7506 lpfc_offline_prep(phba);
7507 lpfc_offline(phba);
7508 kthread_stop(phba->worker_thread);
7509
7510 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007511 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007512
7513 /* Save device state to PCI config space */
7514 pci_save_state(pdev);
7515 pci_set_power_state(pdev, PCI_D3hot);
7516
7517 return 0;
7518}
7519
7520/**
James Smart3772a992009-05-22 14:50:54 -04007521 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007522 * @pdev: pointer to PCI device
7523 *
James Smart3772a992009-05-22 14:50:54 -04007524 * This routine is to be called from the kernel's PCI subsystem to support
7525 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7526 * invokes this method, it restores the device's PCI config space state and
7527 * fully reinitializes the device and brings it online. Note that as the
7528 * driver implements the minimum PM requirements to a power-aware driver's
7529 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7530 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7531 * driver will fully reinitialize its device during resume() method call,
7532 * the device will be set to PCI_D0 directly in PCI config space before
7533 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007534 *
7535 * Return code
James Smart3772a992009-05-22 14:50:54 -04007536 * 0 - driver suspended the device
7537 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007538 **/
7539static int
James Smart3772a992009-05-22 14:50:54 -04007540lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007541{
7542 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7543 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007544 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007545 int error;
7546
7547 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7548 "0452 PCI device Power Management resume.\n");
7549
7550 /* Restore device state from PCI config space */
7551 pci_set_power_state(pdev, PCI_D0);
7552 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007553
James Smart1dfb5a42010-02-12 14:40:50 -05007554 /*
7555 * As the new kernel behavior of pci_restore_state() API call clears
7556 * device saved_state flag, need to save the restored state again.
7557 */
7558 pci_save_state(pdev);
7559
James Smart3a55b532008-12-04 22:38:54 -05007560 if (pdev->is_busmaster)
7561 pci_set_master(pdev);
7562
7563 /* Startup the kernel thread for this host adapter. */
7564 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7565 "lpfc_worker_%d", phba->brd_no);
7566 if (IS_ERR(phba->worker_thread)) {
7567 error = PTR_ERR(phba->worker_thread);
7568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7569 "0434 PM resume failed to start worker "
7570 "thread: error=x%x.\n", error);
7571 return error;
7572 }
7573
James Smart5b75da22008-12-04 22:39:35 -05007574 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007575 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007576 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007578 "0430 PM resume Failed to enable interrupt\n");
7579 return -EIO;
7580 } else
7581 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007582
7583 /* Restart HBA and bring it online */
7584 lpfc_sli_brdrestart(phba);
7585 lpfc_online(phba);
7586
James Smart5b75da22008-12-04 22:39:35 -05007587 /* Log the current active interrupt mode */
7588 lpfc_log_intr_mode(phba, phba->intr_mode);
7589
James Smart3a55b532008-12-04 22:38:54 -05007590 return 0;
7591}
7592
7593/**
James Smart891478a2009-11-18 15:40:23 -05007594 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7595 * @phba: pointer to lpfc hba data structure.
7596 *
7597 * This routine is called to prepare the SLI3 device for PCI slot recover. It
7598 * aborts and stops all the on-going I/Os on the pci device.
7599 **/
7600static void
7601lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7602{
7603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7604 "2723 PCI channel I/O abort preparing for recovery\n");
7605 /* Prepare for bringing HBA offline */
7606 lpfc_offline_prep(phba);
7607 /* Clear sli active flag to prevent sysfs access to HBA */
7608 spin_lock_irq(&phba->hbalock);
7609 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7610 spin_unlock_irq(&phba->hbalock);
7611 /* Stop and flush all I/Os and bring HBA offline */
7612 lpfc_offline(phba);
7613}
7614
7615/**
James Smart0d878412009-10-02 15:16:56 -04007616 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7617 * @phba: pointer to lpfc hba data structure.
7618 *
7619 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7620 * disables the device interrupt and pci device, and aborts the internal FCP
7621 * pending I/Os.
7622 **/
7623static void
7624lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7625{
7626 struct lpfc_sli *psli = &phba->sli;
7627 struct lpfc_sli_ring *pring;
7628
7629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007630 "2710 PCI channel disable preparing for reset\n");
James Smart0d878412009-10-02 15:16:56 -04007631 /* Disable interrupt and pci device */
7632 lpfc_sli_disable_intr(phba);
7633 pci_disable_device(phba->pcidev);
7634 /*
7635 * There may be I/Os dropped by the firmware.
7636 * Error iocb (I/O) on txcmplq and let the SCSI layer
7637 * retry it after re-establishing link.
7638 */
7639 pring = &psli->ring[psli->fcp_ring];
7640 lpfc_sli_abort_iocb_ring(phba, pring);
7641}
7642
7643/**
7644 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7645 * @phba: pointer to lpfc hba data structure.
7646 *
7647 * This routine is called to prepare the SLI3 device for PCI slot permanently
7648 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7649 * pending I/Os.
7650 **/
7651static void
7652lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7653{
7654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007655 "2711 PCI channel permanent disable for failure\n");
James Smart0d878412009-10-02 15:16:56 -04007656 /* Clean up all driver's outstanding SCSI I/Os */
7657 lpfc_sli_flush_fcp_rings(phba);
7658}
7659
7660/**
James Smart3772a992009-05-22 14:50:54 -04007661 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007662 * @pdev: pointer to PCI device.
7663 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007664 *
James Smart3772a992009-05-22 14:50:54 -04007665 * This routine is called from the PCI subsystem for I/O error handling to
7666 * device with SLI-3 interface spec. This function is called by the PCI
7667 * subsystem after a PCI bus error affecting this device has been detected.
7668 * When this function is invoked, it will need to stop all the I/Os and
7669 * interrupt(s) to the device. Once that is done, it will return
7670 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7671 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007672 *
7673 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007674 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007675 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7676 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007677 **/
James Smart3772a992009-05-22 14:50:54 -04007678static pci_ers_result_t
7679lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007680{
James Smart51ef4c22007-08-02 11:10:31 -04007681 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7682 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007683
James Smartaacc20e2009-12-21 17:02:51 -05007684 /* Block all SCSI devices' I/Os on the host */
7685 lpfc_scsi_dev_block(phba);
7686
James Smart0d878412009-10-02 15:16:56 -04007687 switch (state) {
7688 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007689 /* Non-fatal error, prepare for recovery */
7690 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007691 return PCI_ERS_RESULT_CAN_RECOVER;
7692 case pci_channel_io_frozen:
7693 /* Fatal error, prepare for slot reset */
7694 lpfc_sli_prep_dev_for_reset(phba);
7695 return PCI_ERS_RESULT_NEED_RESET;
7696 case pci_channel_io_perm_failure:
7697 /* Permanent failure, prepare for device down */
7698 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007699 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007700 default:
7701 /* Unknown state, prepare and request slot reset */
7702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7703 "0472 Unknown PCI error state: x%x\n", state);
7704 lpfc_sli_prep_dev_for_reset(phba);
7705 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007706 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007707}
7708
7709/**
James Smart3772a992009-05-22 14:50:54 -04007710 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007711 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007712 *
James Smart3772a992009-05-22 14:50:54 -04007713 * This routine is called from the PCI subsystem for error handling to
7714 * device with SLI-3 interface spec. This is called after PCI bus has been
7715 * reset to restart the PCI card from scratch, as if from a cold-boot.
7716 * During the PCI subsystem error recovery, after driver returns
7717 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7718 * recovery and then call this routine before calling the .resume method
7719 * to recover the device. This function will initialize the HBA device,
7720 * enable the interrupt, but it will just put the HBA to offline state
7721 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007722 *
7723 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007724 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7725 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007726 */
James Smart3772a992009-05-22 14:50:54 -04007727static pci_ers_result_t
7728lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007729{
James Smart51ef4c22007-08-02 11:10:31 -04007730 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7731 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007732 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007733 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007734
7735 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007736 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007737 printk(KERN_ERR "lpfc: Cannot re-enable "
7738 "PCI device after reset.\n");
7739 return PCI_ERS_RESULT_DISCONNECT;
7740 }
7741
James Smart97207482008-12-04 22:39:19 -05007742 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05007743
7744 /*
7745 * As the new kernel behavior of pci_restore_state() API call clears
7746 * device saved_state flag, need to save the restored state again.
7747 */
7748 pci_save_state(pdev);
7749
James Smart97207482008-12-04 22:39:19 -05007750 if (pdev->is_busmaster)
7751 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007752
James Smart92d7f7b2007-06-17 19:56:38 -05007753 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007754 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007755 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007756
James Smart5b75da22008-12-04 22:39:35 -05007757 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007758 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007759 if (intr_mode == LPFC_INTR_ERROR) {
7760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7761 "0427 Cannot re-enable interrupt after "
7762 "slot reset.\n");
7763 return PCI_ERS_RESULT_DISCONNECT;
7764 } else
7765 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007766
7767 /* Take device offline; this will perform cleanup */
7768 lpfc_offline(phba);
7769 lpfc_sli_brdrestart(phba);
7770
James Smart5b75da22008-12-04 22:39:35 -05007771 /* Log the current active interrupt mode */
7772 lpfc_log_intr_mode(phba, phba->intr_mode);
7773
Linas Vepstas8d63f372007-02-14 14:28:36 -06007774 return PCI_ERS_RESULT_RECOVERED;
7775}
7776
7777/**
James Smart3772a992009-05-22 14:50:54 -04007778 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007779 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007780 *
James Smart3772a992009-05-22 14:50:54 -04007781 * This routine is called from the PCI subsystem for error handling to device
7782 * with SLI-3 interface spec. It is called when kernel error recovery tells
7783 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7784 * error recovery. After this call, traffic can start to flow from this device
7785 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007786 */
James Smart3772a992009-05-22 14:50:54 -04007787static void
7788lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007789{
James Smart51ef4c22007-08-02 11:10:31 -04007790 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7791 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007792
James Smart0d878412009-10-02 15:16:56 -04007793 /* Bring the device online */
James Smart58da1ff2008-04-07 10:15:56 -04007794 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04007795
7796 /* Clean up Advanced Error Reporting (AER) if needed */
7797 if (phba->hba_flag & HBA_AER_ENABLED)
7798 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007799}
7800
James Smart3772a992009-05-22 14:50:54 -04007801/**
James Smartda0436e2009-05-22 14:51:39 -04007802 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7803 * @phba: pointer to lpfc hba data structure.
7804 *
7805 * returns the number of ELS/CT IOCBs to reserve
7806 **/
7807int
7808lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7809{
7810 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7811
James Smartf1126682009-06-10 17:22:44 -04007812 if (phba->sli_rev == LPFC_SLI_REV4) {
7813 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04007814 return 10;
James Smartf1126682009-06-10 17:22:44 -04007815 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04007816 return 25;
James Smartf1126682009-06-10 17:22:44 -04007817 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04007818 return 50;
James Smartf1126682009-06-10 17:22:44 -04007819 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04007820 return 100;
James Smartf1126682009-06-10 17:22:44 -04007821 else
James Smart6a9c52c2009-10-02 15:16:51 -04007822 return 150;
James Smartf1126682009-06-10 17:22:44 -04007823 } else
7824 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007825}
7826
7827/**
7828 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7829 * @pdev: pointer to PCI device
7830 * @pid: pointer to PCI device identifier
7831 *
7832 * This routine is called from the kernel's PCI subsystem to device with
7833 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7834 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7835 * information of the device and driver to see if the driver state that it
7836 * can support this kind of device. If the match is successful, the driver
7837 * core invokes this routine. If this routine determines it can claim the HBA,
7838 * it does all the initialization that it needs to do to handle the HBA
7839 * properly.
7840 *
7841 * Return code
7842 * 0 - driver can claim the device
7843 * negative value - driver can not claim the device
7844 **/
7845static int __devinit
7846lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7847{
7848 struct lpfc_hba *phba;
7849 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007850 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007851 int error;
7852 uint32_t cfg_mode, intr_mode;
7853 int mcnt;
7854
7855 /* Allocate memory for HBA structure */
7856 phba = lpfc_hba_alloc(pdev);
7857 if (!phba)
7858 return -ENOMEM;
7859
7860 /* Perform generic PCI device enabling operation */
7861 error = lpfc_enable_pci_dev(phba);
7862 if (error) {
7863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7864 "1409 Failed to enable pci device.\n");
7865 goto out_free_phba;
7866 }
7867
7868 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7869 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7870 if (error)
7871 goto out_disable_pci_dev;
7872
7873 /* Set up SLI-4 specific device PCI memory space */
7874 error = lpfc_sli4_pci_mem_setup(phba);
7875 if (error) {
7876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7877 "1410 Failed to set up pci memory space.\n");
7878 goto out_disable_pci_dev;
7879 }
7880
7881 /* Set up phase-1 common device driver resources */
7882 error = lpfc_setup_driver_resource_phase1(phba);
7883 if (error) {
7884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7885 "1411 Failed to set up driver resource.\n");
7886 goto out_unset_pci_mem_s4;
7887 }
7888
7889 /* Set up SLI-4 Specific device driver resources */
7890 error = lpfc_sli4_driver_resource_setup(phba);
7891 if (error) {
7892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7893 "1412 Failed to set up driver resource.\n");
7894 goto out_unset_pci_mem_s4;
7895 }
7896
7897 /* Initialize and populate the iocb list per host */
7898 error = lpfc_init_iocb_list(phba,
7899 phba->sli4_hba.max_cfg_param.max_xri);
7900 if (error) {
7901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7902 "1413 Failed to initialize iocb list.\n");
7903 goto out_unset_driver_resource_s4;
7904 }
7905
7906 /* Set up common device driver resources */
7907 error = lpfc_setup_driver_resource_phase2(phba);
7908 if (error) {
7909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7910 "1414 Failed to set up driver resource.\n");
7911 goto out_free_iocb_list;
7912 }
7913
7914 /* Create SCSI host to the physical port */
7915 error = lpfc_create_shost(phba);
7916 if (error) {
7917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7918 "1415 Failed to create scsi host.\n");
7919 goto out_unset_driver_resource;
7920 }
7921
7922 /* Configure sysfs attributes */
7923 vport = phba->pport;
7924 error = lpfc_alloc_sysfs_attr(vport);
7925 if (error) {
7926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7927 "1416 Failed to allocate sysfs attr\n");
7928 goto out_destroy_shost;
7929 }
7930
James Smart6669f9b2009-10-02 15:16:45 -04007931 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04007932 /* Now, trying to enable interrupt and bring up the device */
7933 cfg_mode = phba->cfg_use_msi;
7934 while (true) {
7935 /* Put device to a known state before enabling interrupt */
7936 lpfc_stop_port(phba);
7937 /* Configure and enable interrupt */
7938 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7939 if (intr_mode == LPFC_INTR_ERROR) {
7940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7941 "0426 Failed to enable interrupt.\n");
7942 error = -ENODEV;
7943 goto out_free_sysfs_attr;
7944 }
James Smartdef9c7a2009-12-21 17:02:28 -05007945 /* Default to single FCP EQ for non-MSI-X */
7946 if (phba->intr_type != MSIX)
7947 phba->cfg_fcp_eq_count = 1;
James Smartda0436e2009-05-22 14:51:39 -04007948 /* Set up SLI-4 HBA */
7949 if (lpfc_sli4_hba_setup(phba)) {
7950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7951 "1421 Failed to set up hba\n");
7952 error = -ENODEV;
7953 goto out_disable_intr;
7954 }
7955
7956 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7957 if (intr_mode != 0)
7958 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7959 LPFC_ACT_INTR_CNT);
7960
7961 /* Check active interrupts received only for MSI/MSI-X */
7962 if (intr_mode == 0 ||
7963 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7964 /* Log the current active interrupt mode */
7965 phba->intr_mode = intr_mode;
7966 lpfc_log_intr_mode(phba, intr_mode);
7967 break;
7968 }
7969 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7970 "0451 Configure interrupt mode (%d) "
7971 "failed active interrupt test.\n",
7972 intr_mode);
7973 /* Unset the preivous SLI-4 HBA setup */
7974 lpfc_sli4_unset_hba(phba);
7975 /* Try next level of interrupt mode */
7976 cfg_mode = --intr_mode;
7977 }
7978
7979 /* Perform post initialization setup */
7980 lpfc_post_init_setup(phba);
7981
James Smart1c6834a2009-07-19 10:01:26 -04007982 /* Check if there are static vports to be created. */
7983 lpfc_create_static_vport(phba);
7984
James Smartda0436e2009-05-22 14:51:39 -04007985 return 0;
7986
7987out_disable_intr:
7988 lpfc_sli4_disable_intr(phba);
7989out_free_sysfs_attr:
7990 lpfc_free_sysfs_attr(vport);
7991out_destroy_shost:
7992 lpfc_destroy_shost(phba);
7993out_unset_driver_resource:
7994 lpfc_unset_driver_resource_phase2(phba);
7995out_free_iocb_list:
7996 lpfc_free_iocb_list(phba);
7997out_unset_driver_resource_s4:
7998 lpfc_sli4_driver_resource_unset(phba);
7999out_unset_pci_mem_s4:
8000 lpfc_sli4_pci_mem_unset(phba);
8001out_disable_pci_dev:
8002 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008003 if (shost)
8004 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04008005out_free_phba:
8006 lpfc_hba_free(phba);
8007 return error;
8008}
8009
8010/**
8011 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8012 * @pdev: pointer to PCI device
8013 *
8014 * This routine is called from the kernel's PCI subsystem to device with
8015 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8016 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8017 * device to be removed from the PCI subsystem properly.
8018 **/
8019static void __devexit
8020lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8021{
8022 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8023 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8024 struct lpfc_vport **vports;
8025 struct lpfc_hba *phba = vport->phba;
8026 int i;
8027
8028 /* Mark the device unloading flag */
8029 spin_lock_irq(&phba->hbalock);
8030 vport->load_flag |= FC_UNLOADING;
8031 spin_unlock_irq(&phba->hbalock);
8032
8033 /* Free the HBA sysfs attributes */
8034 lpfc_free_sysfs_attr(vport);
8035
8036 /* Release all the vports against this physical port */
8037 vports = lpfc_create_vport_work_array(phba);
8038 if (vports != NULL)
8039 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8040 fc_vport_terminate(vports[i]->fc_vport);
8041 lpfc_destroy_vport_work_array(phba, vports);
8042
8043 /* Remove FC host and then SCSI host with the physical port */
8044 fc_remove_host(shost);
8045 scsi_remove_host(shost);
8046
8047 /* Perform cleanup on the physical port */
8048 lpfc_cleanup(vport);
8049
8050 /*
8051 * Bring down the SLI Layer. This step disables all interrupts,
8052 * clears the rings, discards all mailbox commands, and resets
8053 * the HBA FCoE function.
8054 */
8055 lpfc_debugfs_terminate(vport);
8056 lpfc_sli4_hba_unset(phba);
8057
8058 spin_lock_irq(&phba->hbalock);
8059 list_del_init(&vport->listentry);
8060 spin_unlock_irq(&phba->hbalock);
8061
8062 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
8063 * buffers are released to their corresponding pools here.
8064 */
8065 lpfc_scsi_free(phba);
8066 lpfc_sli4_driver_resource_unset(phba);
8067
8068 /* Unmap adapter Control and Doorbell registers */
8069 lpfc_sli4_pci_mem_unset(phba);
8070
8071 /* Release PCI resources and disable device's PCI function */
8072 scsi_host_put(shost);
8073 lpfc_disable_pci_dev(phba);
8074
8075 /* Finally, free the driver's device data structure */
8076 lpfc_hba_free(phba);
8077
8078 return;
8079}
8080
8081/**
8082 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
8083 * @pdev: pointer to PCI device
8084 * @msg: power management message
8085 *
8086 * This routine is called from the kernel's PCI subsystem to support system
8087 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8088 * this method, it quiesces the device by stopping the driver's worker
8089 * thread for the device, turning off device's interrupt and DMA, and bring
8090 * the device offline. Note that as the driver implements the minimum PM
8091 * requirements to a power-aware driver's PM support for suspend/resume -- all
8092 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8093 * method call will be treated as SUSPEND and the driver will fully
8094 * reinitialize its device during resume() method call, the driver will set
8095 * device to PCI_D3hot state in PCI config space instead of setting it
8096 * according to the @msg provided by the PM.
8097 *
8098 * Return code
8099 * 0 - driver suspended the device
8100 * Error otherwise
8101 **/
8102static int
8103lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
8104{
8105 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8106 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8107
8108 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8109 "0298 PCI device Power Management suspend.\n");
8110
8111 /* Bring down the device */
8112 lpfc_offline_prep(phba);
8113 lpfc_offline(phba);
8114 kthread_stop(phba->worker_thread);
8115
8116 /* Disable interrupt from device */
8117 lpfc_sli4_disable_intr(phba);
8118
8119 /* Save device state to PCI config space */
8120 pci_save_state(pdev);
8121 pci_set_power_state(pdev, PCI_D3hot);
8122
8123 return 0;
8124}
8125
8126/**
8127 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8128 * @pdev: pointer to PCI device
8129 *
8130 * This routine is called from the kernel's PCI subsystem to support system
8131 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8132 * this method, it restores the device's PCI config space state and fully
8133 * reinitializes the device and brings it online. Note that as the driver
8134 * implements the minimum PM requirements to a power-aware driver's PM for
8135 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8136 * to the suspend() method call will be treated as SUSPEND and the driver
8137 * will fully reinitialize its device during resume() method call, the device
8138 * will be set to PCI_D0 directly in PCI config space before restoring the
8139 * state.
8140 *
8141 * Return code
8142 * 0 - driver suspended the device
8143 * Error otherwise
8144 **/
8145static int
8146lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8147{
8148 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8149 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8150 uint32_t intr_mode;
8151 int error;
8152
8153 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8154 "0292 PCI device Power Management resume.\n");
8155
8156 /* Restore device state from PCI config space */
8157 pci_set_power_state(pdev, PCI_D0);
8158 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008159
8160 /*
8161 * As the new kernel behavior of pci_restore_state() API call clears
8162 * device saved_state flag, need to save the restored state again.
8163 */
8164 pci_save_state(pdev);
8165
James Smartda0436e2009-05-22 14:51:39 -04008166 if (pdev->is_busmaster)
8167 pci_set_master(pdev);
8168
8169 /* Startup the kernel thread for this host adapter. */
8170 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8171 "lpfc_worker_%d", phba->brd_no);
8172 if (IS_ERR(phba->worker_thread)) {
8173 error = PTR_ERR(phba->worker_thread);
8174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8175 "0293 PM resume failed to start worker "
8176 "thread: error=x%x.\n", error);
8177 return error;
8178 }
8179
8180 /* Configure and enable interrupt */
8181 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8182 if (intr_mode == LPFC_INTR_ERROR) {
8183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8184 "0294 PM resume Failed to enable interrupt\n");
8185 return -EIO;
8186 } else
8187 phba->intr_mode = intr_mode;
8188
8189 /* Restart HBA and bring it online */
8190 lpfc_sli_brdrestart(phba);
8191 lpfc_online(phba);
8192
8193 /* Log the current active interrupt mode */
8194 lpfc_log_intr_mode(phba, phba->intr_mode);
8195
8196 return 0;
8197}
8198
8199/**
8200 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
8201 * @pdev: pointer to PCI device.
8202 * @state: the current PCI connection state.
8203 *
8204 * This routine is called from the PCI subsystem for error handling to device
8205 * with SLI-4 interface spec. This function is called by the PCI subsystem
8206 * after a PCI bus error affecting this device has been detected. When this
8207 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8208 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8209 * for the PCI subsystem to perform proper recovery as desired.
8210 *
8211 * Return codes
8212 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8213 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8214 **/
8215static pci_ers_result_t
8216lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8217{
8218 return PCI_ERS_RESULT_NEED_RESET;
8219}
8220
8221/**
8222 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
8223 * @pdev: pointer to PCI device.
8224 *
8225 * This routine is called from the PCI subsystem for error handling to device
8226 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8227 * restart the PCI card from scratch, as if from a cold-boot. During the
8228 * PCI subsystem error recovery, after the driver returns
8229 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8230 * recovery and then call this routine before calling the .resume method to
8231 * recover the device. This function will initialize the HBA device, enable
8232 * the interrupt, but it will just put the HBA to offline state without
8233 * passing any I/O traffic.
8234 *
8235 * Return codes
8236 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8237 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8238 */
8239static pci_ers_result_t
8240lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8241{
8242 return PCI_ERS_RESULT_RECOVERED;
8243}
8244
8245/**
8246 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8247 * @pdev: pointer to PCI device
8248 *
8249 * This routine is called from the PCI subsystem for error handling to device
8250 * with SLI-4 interface spec. It is called when kernel error recovery tells
8251 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8252 * error recovery. After this call, traffic can start to flow from this device
8253 * again.
8254 **/
8255static void
8256lpfc_io_resume_s4(struct pci_dev *pdev)
8257{
8258 return;
8259}
8260
8261/**
James Smart3772a992009-05-22 14:50:54 -04008262 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8263 * @pdev: pointer to PCI device
8264 * @pid: pointer to PCI device identifier
8265 *
8266 * This routine is to be registered to the kernel's PCI subsystem. When an
8267 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8268 * at PCI device-specific information of the device and driver to see if the
8269 * driver state that it can support this kind of device. If the match is
8270 * successful, the driver core invokes this routine. This routine dispatches
8271 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8272 * do all the initialization that it needs to do to handle the HBA device
8273 * properly.
8274 *
8275 * Return code
8276 * 0 - driver can claim the device
8277 * negative value - driver can not claim the device
8278 **/
8279static int __devinit
8280lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8281{
8282 int rc;
James Smart8fa38512009-07-19 10:01:03 -04008283 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04008284
James Smart28baac72010-02-12 14:42:03 -05008285 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04008286 return -ENODEV;
8287
James Smart8fa38512009-07-19 10:01:03 -04008288 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05008289 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04008290 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008291 else
James Smart3772a992009-05-22 14:50:54 -04008292 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008293
James Smart3772a992009-05-22 14:50:54 -04008294 return rc;
8295}
8296
8297/**
8298 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8299 * @pdev: pointer to PCI device
8300 *
8301 * This routine is to be registered to the kernel's PCI subsystem. When an
8302 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8303 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8304 * remove routine, which will perform all the necessary cleanup for the
8305 * device to be removed from the PCI subsystem properly.
8306 **/
8307static void __devexit
8308lpfc_pci_remove_one(struct pci_dev *pdev)
8309{
8310 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8311 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8312
8313 switch (phba->pci_dev_grp) {
8314 case LPFC_PCI_DEV_LP:
8315 lpfc_pci_remove_one_s3(pdev);
8316 break;
James Smartda0436e2009-05-22 14:51:39 -04008317 case LPFC_PCI_DEV_OC:
8318 lpfc_pci_remove_one_s4(pdev);
8319 break;
James Smart3772a992009-05-22 14:50:54 -04008320 default:
8321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8322 "1424 Invalid PCI device group: 0x%x\n",
8323 phba->pci_dev_grp);
8324 break;
8325 }
8326 return;
8327}
8328
8329/**
8330 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8331 * @pdev: pointer to PCI device
8332 * @msg: power management message
8333 *
8334 * This routine is to be registered to the kernel's PCI subsystem to support
8335 * system Power Management (PM). When PM invokes this method, it dispatches
8336 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8337 * suspend the device.
8338 *
8339 * Return code
8340 * 0 - driver suspended the device
8341 * Error otherwise
8342 **/
8343static int
8344lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8345{
8346 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8347 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8348 int rc = -ENODEV;
8349
8350 switch (phba->pci_dev_grp) {
8351 case LPFC_PCI_DEV_LP:
8352 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8353 break;
James Smartda0436e2009-05-22 14:51:39 -04008354 case LPFC_PCI_DEV_OC:
8355 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8356 break;
James Smart3772a992009-05-22 14:50:54 -04008357 default:
8358 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8359 "1425 Invalid PCI device group: 0x%x\n",
8360 phba->pci_dev_grp);
8361 break;
8362 }
8363 return rc;
8364}
8365
8366/**
8367 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8368 * @pdev: pointer to PCI device
8369 *
8370 * This routine is to be registered to the kernel's PCI subsystem to support
8371 * system Power Management (PM). When PM invokes this method, it dispatches
8372 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8373 * resume the device.
8374 *
8375 * Return code
8376 * 0 - driver suspended the device
8377 * Error otherwise
8378 **/
8379static int
8380lpfc_pci_resume_one(struct pci_dev *pdev)
8381{
8382 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8383 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8384 int rc = -ENODEV;
8385
8386 switch (phba->pci_dev_grp) {
8387 case LPFC_PCI_DEV_LP:
8388 rc = lpfc_pci_resume_one_s3(pdev);
8389 break;
James Smartda0436e2009-05-22 14:51:39 -04008390 case LPFC_PCI_DEV_OC:
8391 rc = lpfc_pci_resume_one_s4(pdev);
8392 break;
James Smart3772a992009-05-22 14:50:54 -04008393 default:
8394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8395 "1426 Invalid PCI device group: 0x%x\n",
8396 phba->pci_dev_grp);
8397 break;
8398 }
8399 return rc;
8400}
8401
8402/**
8403 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8404 * @pdev: pointer to PCI device.
8405 * @state: the current PCI connection state.
8406 *
8407 * This routine is registered to the PCI subsystem for error handling. This
8408 * function is called by the PCI subsystem after a PCI bus error affecting
8409 * this device has been detected. When this routine is invoked, it dispatches
8410 * the action to the proper SLI-3 or SLI-4 device error detected handling
8411 * routine, which will perform the proper error detected operation.
8412 *
8413 * Return codes
8414 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8415 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8416 **/
8417static pci_ers_result_t
8418lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8419{
8420 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8421 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8422 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8423
8424 switch (phba->pci_dev_grp) {
8425 case LPFC_PCI_DEV_LP:
8426 rc = lpfc_io_error_detected_s3(pdev, state);
8427 break;
James Smartda0436e2009-05-22 14:51:39 -04008428 case LPFC_PCI_DEV_OC:
8429 rc = lpfc_io_error_detected_s4(pdev, state);
8430 break;
James Smart3772a992009-05-22 14:50:54 -04008431 default:
8432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8433 "1427 Invalid PCI device group: 0x%x\n",
8434 phba->pci_dev_grp);
8435 break;
8436 }
8437 return rc;
8438}
8439
8440/**
8441 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8442 * @pdev: pointer to PCI device.
8443 *
8444 * This routine is registered to the PCI subsystem for error handling. This
8445 * function is called after PCI bus has been reset to restart the PCI card
8446 * from scratch, as if from a cold-boot. When this routine is invoked, it
8447 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8448 * routine, which will perform the proper device reset.
8449 *
8450 * Return codes
8451 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8452 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8453 **/
8454static pci_ers_result_t
8455lpfc_io_slot_reset(struct pci_dev *pdev)
8456{
8457 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8458 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8459 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8460
8461 switch (phba->pci_dev_grp) {
8462 case LPFC_PCI_DEV_LP:
8463 rc = lpfc_io_slot_reset_s3(pdev);
8464 break;
James Smartda0436e2009-05-22 14:51:39 -04008465 case LPFC_PCI_DEV_OC:
8466 rc = lpfc_io_slot_reset_s4(pdev);
8467 break;
James Smart3772a992009-05-22 14:50:54 -04008468 default:
8469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8470 "1428 Invalid PCI device group: 0x%x\n",
8471 phba->pci_dev_grp);
8472 break;
8473 }
8474 return rc;
8475}
8476
8477/**
8478 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8479 * @pdev: pointer to PCI device
8480 *
8481 * This routine is registered to the PCI subsystem for error handling. It
8482 * is called when kernel error recovery tells the lpfc driver that it is
8483 * OK to resume normal PCI operation after PCI bus error recovery. When
8484 * this routine is invoked, it dispatches the action to the proper SLI-3
8485 * or SLI-4 device io_resume routine, which will resume the device operation.
8486 **/
8487static void
8488lpfc_io_resume(struct pci_dev *pdev)
8489{
8490 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8491 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8492
8493 switch (phba->pci_dev_grp) {
8494 case LPFC_PCI_DEV_LP:
8495 lpfc_io_resume_s3(pdev);
8496 break;
James Smartda0436e2009-05-22 14:51:39 -04008497 case LPFC_PCI_DEV_OC:
8498 lpfc_io_resume_s4(pdev);
8499 break;
James Smart3772a992009-05-22 14:50:54 -04008500 default:
8501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8502 "1429 Invalid PCI device group: 0x%x\n",
8503 phba->pci_dev_grp);
8504 break;
8505 }
8506 return;
8507}
8508
dea31012005-04-17 16:05:31 -05008509static struct pci_device_id lpfc_id_table[] = {
8510 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8511 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008512 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8513 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008514 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8515 PCI_ANY_ID, PCI_ANY_ID, },
8516 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8517 PCI_ANY_ID, PCI_ANY_ID, },
8518 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8519 PCI_ANY_ID, PCI_ANY_ID, },
8520 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8521 PCI_ANY_ID, PCI_ANY_ID, },
8522 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8523 PCI_ANY_ID, PCI_ANY_ID, },
8524 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8525 PCI_ANY_ID, PCI_ANY_ID, },
8526 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8527 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008528 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8529 PCI_ANY_ID, PCI_ANY_ID, },
8530 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8531 PCI_ANY_ID, PCI_ANY_ID, },
8532 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8533 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008534 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8535 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008536 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8537 PCI_ANY_ID, PCI_ANY_ID, },
8538 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8539 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008540 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8541 PCI_ANY_ID, PCI_ANY_ID, },
8542 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8543 PCI_ANY_ID, PCI_ANY_ID, },
8544 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8545 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008546 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8547 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008548 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8549 PCI_ANY_ID, PCI_ANY_ID, },
8550 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8551 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008552 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8553 PCI_ANY_ID, PCI_ANY_ID, },
8554 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8555 PCI_ANY_ID, PCI_ANY_ID, },
8556 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8557 PCI_ANY_ID, PCI_ANY_ID, },
8558 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8559 PCI_ANY_ID, PCI_ANY_ID, },
8560 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8561 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008562 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8563 PCI_ANY_ID, PCI_ANY_ID, },
8564 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8565 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008566 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8567 PCI_ANY_ID, PCI_ANY_ID, },
8568 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8569 PCI_ANY_ID, PCI_ANY_ID, },
8570 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8571 PCI_ANY_ID, PCI_ANY_ID, },
8572 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8573 PCI_ANY_ID, PCI_ANY_ID, },
8574 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8575 PCI_ANY_ID, PCI_ANY_ID, },
8576 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8577 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008578 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8579 PCI_ANY_ID, PCI_ANY_ID, },
8580 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8581 PCI_ANY_ID, PCI_ANY_ID, },
8582 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8583 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008584 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8585 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05008586 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8587 PCI_ANY_ID, PCI_ANY_ID, },
8588 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04008589 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008590 { 0 }
8591};
8592
8593MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8594
Linas Vepstas8d63f372007-02-14 14:28:36 -06008595static struct pci_error_handlers lpfc_err_handler = {
8596 .error_detected = lpfc_io_error_detected,
8597 .slot_reset = lpfc_io_slot_reset,
8598 .resume = lpfc_io_resume,
8599};
8600
dea31012005-04-17 16:05:31 -05008601static struct pci_driver lpfc_driver = {
8602 .name = LPFC_DRIVER_NAME,
8603 .id_table = lpfc_id_table,
8604 .probe = lpfc_pci_probe_one,
8605 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008606 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008607 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008608 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008609};
8610
James Smarte59058c2008-08-24 21:49:00 -04008611/**
James Smart3621a712009-04-06 18:47:14 -04008612 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008613 *
8614 * This routine is to be invoked when the lpfc module is loaded into the
8615 * kernel. The special kernel macro module_init() is used to indicate the
8616 * role of this routine to the kernel as lpfc module entry point.
8617 *
8618 * Return codes
8619 * 0 - successful
8620 * -ENOMEM - FC attach transport failed
8621 * all others - failed
8622 */
dea31012005-04-17 16:05:31 -05008623static int __init
8624lpfc_init(void)
8625{
8626 int error = 0;
8627
8628 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008629 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008630
James Smart7ee5d432007-10-27 13:37:17 -04008631 if (lpfc_enable_npiv) {
8632 lpfc_transport_functions.vport_create = lpfc_vport_create;
8633 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8634 }
dea31012005-04-17 16:05:31 -05008635 lpfc_transport_template =
8636 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008637 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008638 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008639 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008640 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008641 fc_attach_transport(&lpfc_vport_transport_functions);
8642 if (lpfc_vport_transport_template == NULL) {
8643 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008644 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008645 }
James Smart7ee5d432007-10-27 13:37:17 -04008646 }
dea31012005-04-17 16:05:31 -05008647 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008648 if (error) {
dea31012005-04-17 16:05:31 -05008649 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008650 if (lpfc_enable_npiv)
8651 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008652 }
dea31012005-04-17 16:05:31 -05008653
8654 return error;
8655}
8656
James Smarte59058c2008-08-24 21:49:00 -04008657/**
James Smart3621a712009-04-06 18:47:14 -04008658 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008659 *
8660 * This routine is invoked when the lpfc module is removed from the kernel.
8661 * The special kernel macro module_exit() is used to indicate the role of
8662 * this routine to the kernel as lpfc module exit point.
8663 */
dea31012005-04-17 16:05:31 -05008664static void __exit
8665lpfc_exit(void)
8666{
8667 pci_unregister_driver(&lpfc_driver);
8668 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008669 if (lpfc_enable_npiv)
8670 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008671 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008672 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8673 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008674 (1L << _dump_buf_data_order), _dump_buf_data);
8675 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8676 }
8677
8678 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008679 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8680 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008681 (1L << _dump_buf_dif_order), _dump_buf_dif);
8682 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8683 }
dea31012005-04-17 16:05:31 -05008684}
8685
8686module_init(lpfc_init);
8687module_exit(lpfc_exit);
8688MODULE_LICENSE("GPL");
8689MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8690MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8691MODULE_VERSION("0:" LPFC_DRIVER_VERSION);