blob: 93679f30a5af0fcd2531ce7369783950b011dc7b [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040031#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050032
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040033#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050034#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_transport_fc.h>
37
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
40#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
44#include "lpfc_scsi.h"
45#include "lpfc.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_version.h"
50
James Smart81301a92008-12-04 22:39:46 -050051char *_dump_buf_data;
52unsigned long _dump_buf_data_order;
53char *_dump_buf_dif;
54unsigned long _dump_buf_dif_order;
55spinlock_t _dump_buf_lock;
56
dea31012005-04-17 16:05:31 -050057static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
58static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040059static int lpfc_sli4_queue_create(struct lpfc_hba *);
60static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
61static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
62static int lpfc_setup_endian_order(struct lpfc_hba *);
63static int lpfc_sli4_read_config(struct lpfc_hba *);
64static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
65static void lpfc_free_sgl_list(struct lpfc_hba *);
66static int lpfc_init_sgl_list(struct lpfc_hba *);
67static int lpfc_init_active_sgl_array(struct lpfc_hba *);
68static void lpfc_free_active_sgl(struct lpfc_hba *);
69static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
70static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
71static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
72static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
73static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050074
75static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050076static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050077static DEFINE_IDR(lpfc_hba_index);
78
James Smarte59058c2008-08-24 21:49:00 -040079/**
James Smart3621a712009-04-06 18:47:14 -040080 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040081 * @phba: pointer to lpfc hba data structure.
82 *
83 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
84 * mailbox command. It retrieves the revision information from the HBA and
85 * collects the Vital Product Data (VPD) about the HBA for preparing the
86 * configuration of the HBA.
87 *
88 * Return codes:
89 * 0 - success.
90 * -ERESTART - requests the SLI layer to reset the HBA and try again.
91 * Any other value - indicates an error.
92 **/
dea31012005-04-17 16:05:31 -050093int
James Smart2e0fef82007-06-17 19:56:36 -050094lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050095{
96 lpfc_vpd_t *vp = &phba->vpd;
97 int i = 0, rc;
98 LPFC_MBOXQ_t *pmb;
99 MAILBOX_t *mb;
100 char *lpfc_vpd_data = NULL;
101 uint16_t offset = 0;
102 static char licensed[56] =
103 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400104 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500105
106 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
107 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500108 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500109 return -ENOMEM;
110 }
111
James Smart04c68492009-05-22 14:52:52 -0400112 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500113 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500114
115 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400116 if (init_key) {
117 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500118
James Smart65a29c12006-07-06 15:50:50 -0400119 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
120 *ptext = cpu_to_be32(*ptext);
121 init_key = 0;
122 }
dea31012005-04-17 16:05:31 -0500123
124 lpfc_read_nv(phba, pmb);
125 memset((char*)mb->un.varRDnvp.rsvd3, 0,
126 sizeof (mb->un.varRDnvp.rsvd3));
127 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
128 sizeof (licensed));
129
130 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
131
132 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500133 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400134 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500135 "error, mbxCmd x%x READ_NVPARM, "
136 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500137 mb->mbxCommand, mb->mbxStatus);
138 mempool_free(pmb, phba->mbox_mem_pool);
139 return -ERESTART;
140 }
141 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500142 sizeof(phba->wwnn));
143 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
144 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500145 }
146
James Smart92d7f7b2007-06-17 19:56:38 -0500147 phba->sli3_options = 0x0;
148
dea31012005-04-17 16:05:31 -0500149 /* Setup and issue mailbox READ REV command */
150 lpfc_read_rev(phba, pmb);
151 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
152 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400154 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500155 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500156 mb->mbxCommand, mb->mbxStatus);
157 mempool_free( pmb, phba->mbox_mem_pool);
158 return -ERESTART;
159 }
160
James Smart92d7f7b2007-06-17 19:56:38 -0500161
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500162 /*
163 * The value of rr must be 1 since the driver set the cv field to 1.
164 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500165 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500166 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500167 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400169 "0440 Adapter failed to init, READ_REV has "
170 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500171 mempool_free(pmb, phba->mbox_mem_pool);
172 return -ERESTART;
dea31012005-04-17 16:05:31 -0500173 }
174
James Smart495a7142008-06-14 22:52:59 -0400175 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
176 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500177 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400178 }
James Smarted957682007-06-17 19:56:37 -0500179
dea31012005-04-17 16:05:31 -0500180 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500181 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500182 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500183 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
184 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
185 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
186 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500187 vp->rev.biuRev = mb->un.varRdRev.biuRev;
188 vp->rev.smRev = mb->un.varRdRev.smRev;
189 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
190 vp->rev.endecRev = mb->un.varRdRev.endecRev;
191 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
192 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
193 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
194 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
195 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
196 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
197
James Smart92d7f7b2007-06-17 19:56:38 -0500198 /* If the sli feature level is less then 9, we must
199 * tear down all RPIs and VPIs on link down if NPIV
200 * is enabled.
201 */
202 if (vp->rev.feaLevelHigh < 9)
203 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
204
dea31012005-04-17 16:05:31 -0500205 if (lpfc_is_LC_HBA(phba->pcidev->device))
206 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
207 sizeof (phba->RandomData));
208
dea31012005-04-17 16:05:31 -0500209 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500210 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
211 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400212 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500213
214 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400215 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500216 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
217
218 if (rc != MBX_SUCCESS) {
219 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400220 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500221 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500222 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500223 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500224 }
James Smart04c68492009-05-22 14:52:52 -0400225 /* dump mem may return a zero when finished or we got a
226 * mailbox error, either way we are done.
227 */
228 if (mb->un.varDmp.word_cnt == 0)
229 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500230 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
231 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400232 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
233 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500234 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500235 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500236 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
237 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500238
239 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500240out_free_mbox:
241 mempool_free(pmb, phba->mbox_mem_pool);
242 return 0;
243}
244
James Smarte59058c2008-08-24 21:49:00 -0400245/**
James Smart3621a712009-04-06 18:47:14 -0400246 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400247 * @phba: pointer to lpfc hba data structure.
248 * @pmboxq: pointer to the driver internal queue element for mailbox command.
249 *
250 * This is the completion handler for driver's configuring asynchronous event
251 * mailbox command to the device. If the mailbox command returns successfully,
252 * it will set internal async event support flag to 1; otherwise, it will
253 * set internal async event support flag to 0.
254 **/
James Smart57127f12007-10-27 13:37:05 -0400255static void
256lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
257{
James Smart04c68492009-05-22 14:52:52 -0400258 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400259 phba->temp_sensor_support = 1;
260 else
261 phba->temp_sensor_support = 0;
262 mempool_free(pmboxq, phba->mbox_mem_pool);
263 return;
264}
265
James Smarte59058c2008-08-24 21:49:00 -0400266/**
James Smart3621a712009-04-06 18:47:14 -0400267 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500268 * @phba: pointer to lpfc hba data structure.
269 * @pmboxq: pointer to the driver internal queue element for mailbox command.
270 *
271 * This is the completion handler for dump mailbox command for getting
272 * wake up parameters. When this command complete, the response contain
273 * Option rom version of the HBA. This function translate the version number
274 * into a human readable string and store it in OptionROMVersion.
275 **/
276static void
277lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
278{
279 struct prog_id *prg;
280 uint32_t prog_id_word;
281 char dist = ' ';
282 /* character array used for decoding dist type. */
283 char dist_char[] = "nabx";
284
James Smart04c68492009-05-22 14:52:52 -0400285 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500286 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500287 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500288 }
James Smart97207482008-12-04 22:39:19 -0500289
290 prg = (struct prog_id *) &prog_id_word;
291
292 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400293 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500294
295 /* Decode the Option rom version word to a readable string */
296 if (prg->dist < 4)
297 dist = dist_char[prg->dist];
298
299 if ((prg->dist == 3) && (prg->num == 0))
300 sprintf(phba->OptionROMVersion, "%d.%d%d",
301 prg->ver, prg->rev, prg->lev);
302 else
303 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
304 prg->ver, prg->rev, prg->lev,
305 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500306 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500307 return;
308}
309
310/**
James Smart3621a712009-04-06 18:47:14 -0400311 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400312 * @phba: pointer to lpfc hba data structure.
313 *
314 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
315 * command call. It performs all internal resource and state setups on the
316 * port: post IOCB buffers, enable appropriate host interrupt attentions,
317 * ELS ring timers, etc.
318 *
319 * Return codes
320 * 0 - success.
321 * Any other value - error.
322 **/
dea31012005-04-17 16:05:31 -0500323int
James Smart2e0fef82007-06-17 19:56:36 -0500324lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500325{
James Smart2e0fef82007-06-17 19:56:36 -0500326 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400327 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500328 LPFC_MBOXQ_t *pmb;
329 MAILBOX_t *mb;
330 struct lpfc_dmabuf *mp;
331 struct lpfc_sli *psli = &phba->sli;
332 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500333 int i, j;
334 int rc;
dea31012005-04-17 16:05:31 -0500335
James Smart7af67052007-10-27 13:38:11 -0400336 spin_lock_irq(&phba->hbalock);
337 /*
338 * If the Config port completed correctly the HBA is not
339 * over heated any more.
340 */
341 if (phba->over_temp_state == HBA_OVER_TEMP)
342 phba->over_temp_state = HBA_NORMAL_TEMP;
343 spin_unlock_irq(&phba->hbalock);
344
dea31012005-04-17 16:05:31 -0500345 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
346 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500347 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500348 return -ENOMEM;
349 }
James Smart04c68492009-05-22 14:52:52 -0400350 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500351
dea31012005-04-17 16:05:31 -0500352 /* Get login parameters for NID. */
James Smart92d7f7b2007-06-17 19:56:38 -0500353 lpfc_read_sparam(phba, pmb, 0);
James Smarted957682007-06-17 19:56:37 -0500354 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500355 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400357 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500358 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500359 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500360 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500361 mp = (struct lpfc_dmabuf *) pmb->context1;
362 mempool_free( pmb, phba->mbox_mem_pool);
363 lpfc_mbuf_free(phba, mp->virt, mp->phys);
364 kfree(mp);
365 return -EIO;
366 }
367
368 mp = (struct lpfc_dmabuf *) pmb->context1;
369
James Smart2e0fef82007-06-17 19:56:36 -0500370 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500371 lpfc_mbuf_free(phba, mp->virt, mp->phys);
372 kfree(mp);
373 pmb->context1 = NULL;
374
James Smarta12e07b2006-12-02 13:35:30 -0500375 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500376 u64_to_wwn(phba->cfg_soft_wwnn,
377 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400378 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500379 u64_to_wwn(phba->cfg_soft_wwpn,
380 vport->fc_sparam.portName.u.wwn);
381 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500382 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500383 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500384 sizeof (struct lpfc_name));
James Smarta257bf92009-04-06 18:48:10 -0400385
386 /* Update the fc_host data structures with new wwn. */
387 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
388 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400389 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400390
dea31012005-04-17 16:05:31 -0500391 /* If no serial number in VPD data, use low 6 bytes of WWNN */
392 /* This should be consolidated into parse_vpd ? - mr */
393 if (phba->SerialNumber[0] == 0) {
394 uint8_t *outptr;
395
James Smart2e0fef82007-06-17 19:56:36 -0500396 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500397 for (i = 0; i < 12; i++) {
398 status = *outptr++;
399 j = ((status & 0xf0) >> 4);
400 if (j <= 9)
401 phba->SerialNumber[i] =
402 (char)((uint8_t) 0x30 + (uint8_t) j);
403 else
404 phba->SerialNumber[i] =
405 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
406 i++;
407 j = (status & 0xf);
408 if (j <= 9)
409 phba->SerialNumber[i] =
410 (char)((uint8_t) 0x30 + (uint8_t) j);
411 else
412 phba->SerialNumber[i] =
413 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
414 }
415 }
416
dea31012005-04-17 16:05:31 -0500417 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500418 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500419 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400421 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500422 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500423 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500424 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500425 mempool_free( pmb, phba->mbox_mem_pool);
426 return -EIO;
427 }
428
James Smarta0c87cb2009-07-19 10:01:10 -0400429 /* Check if the port is disabled */
430 lpfc_sli_read_link_ste(phba);
431
dea31012005-04-17 16:05:31 -0500432 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
433 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
434 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400435 (mb->un.varRdConfig.max_xri + 1) -
436 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500437
438 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500439
440 /* Get the default values for Model Name and Description */
441 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
442
443 if ((phba->cfg_link_speed > LINK_SPEED_10G)
444 || ((phba->cfg_link_speed == LINK_SPEED_1G)
445 && !(phba->lmt & LMT_1Gb))
446 || ((phba->cfg_link_speed == LINK_SPEED_2G)
447 && !(phba->lmt & LMT_2Gb))
448 || ((phba->cfg_link_speed == LINK_SPEED_4G)
449 && !(phba->lmt & LMT_4Gb))
450 || ((phba->cfg_link_speed == LINK_SPEED_8G)
451 && !(phba->lmt & LMT_8Gb))
452 || ((phba->cfg_link_speed == LINK_SPEED_10G)
453 && !(phba->lmt & LMT_10Gb))) {
454 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500455 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400456 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500457 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500458 phba->cfg_link_speed);
459 phba->cfg_link_speed = LINK_SPEED_AUTO;
460 }
461
James Smart2e0fef82007-06-17 19:56:36 -0500462 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500463
James Smart0b727fe2007-10-27 13:37:25 -0400464 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500465 if (psli->ring[psli->extra_ring].cmdringaddr)
466 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500467 if (psli->ring[psli->fcp_ring].cmdringaddr)
468 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
469 if (psli->ring[psli->next_ring].cmdringaddr)
470 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
471
472 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500473 if (phba->sli_rev != 3)
474 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500475
James Smart93996272008-08-24 21:50:30 -0400476 /*
477 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
478 */
479 if (phba->intr_type == MSIX) {
480 rc = lpfc_config_msi(phba, pmb);
481 if (rc) {
482 mempool_free(pmb, phba->mbox_mem_pool);
483 return -EIO;
484 }
485 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
486 if (rc != MBX_SUCCESS) {
487 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
488 "0352 Config MSI mailbox command "
489 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400490 pmb->u.mb.mbxCommand,
491 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400492 mempool_free(pmb, phba->mbox_mem_pool);
493 return -EIO;
494 }
495 }
496
James Smart04c68492009-05-22 14:52:52 -0400497 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400498 /* Initialize ERATT handling flag */
499 phba->hba_flag &= ~HBA_ERATT_HANDLED;
500
dea31012005-04-17 16:05:31 -0500501 /* Enable appropriate host interrupts */
dea31012005-04-17 16:05:31 -0500502 status = readl(phba->HCregaddr);
503 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
504 if (psli->num_rings > 0)
505 status |= HC_R0INT_ENA;
506 if (psli->num_rings > 1)
507 status |= HC_R1INT_ENA;
508 if (psli->num_rings > 2)
509 status |= HC_R2INT_ENA;
510 if (psli->num_rings > 3)
511 status |= HC_R3INT_ENA;
512
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500513 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
514 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400515 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500516
dea31012005-04-17 16:05:31 -0500517 writel(status, phba->HCregaddr);
518 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500519 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500520
James Smart93996272008-08-24 21:50:30 -0400521 /* Set up ring-0 (ELS) timer */
522 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500523 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400524 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500525 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
526 phba->hb_outstanding = 0;
527 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400528 /* Set up error attention (ERATT) polling timer */
529 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500530
James Smarta0c87cb2009-07-19 10:01:10 -0400531 if (phba->hba_flag & LINK_DISABLED) {
532 lpfc_printf_log(phba,
533 KERN_ERR, LOG_INIT,
534 "2598 Adapter Link is disabled.\n");
535 lpfc_down_link(phba, pmb);
536 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
537 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
538 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
539 lpfc_printf_log(phba,
540 KERN_ERR, LOG_INIT,
541 "2599 Adapter failed to issue DOWN_LINK"
542 " mbox command rc 0x%x\n", rc);
543
544 mempool_free(pmb, phba->mbox_mem_pool);
545 return -EIO;
546 }
547 } else {
548 lpfc_init_link(phba, pmb, phba->cfg_topology,
549 phba->cfg_link_speed);
550 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
551 lpfc_set_loopback_flag(phba);
552 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
553 if (rc != MBX_SUCCESS) {
554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400555 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500556 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500557 mb->mbxCommand, mb->mbxStatus);
558
James Smarta0c87cb2009-07-19 10:01:10 -0400559 /* Clear all interrupt enable conditions */
560 writel(0, phba->HCregaddr);
561 readl(phba->HCregaddr); /* flush */
562 /* Clear all pending interrupts */
563 writel(0xffffffff, phba->HAregaddr);
564 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500565
James Smarta0c87cb2009-07-19 10:01:10 -0400566 phba->link_state = LPFC_HBA_ERROR;
567 if (rc != MBX_BUSY)
568 mempool_free(pmb, phba->mbox_mem_pool);
569 return -EIO;
570 }
dea31012005-04-17 16:05:31 -0500571 }
572 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400573 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
574 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
575 pmb->mbox_cmpl = lpfc_config_async_cmpl;
576 pmb->vport = phba->pport;
577 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500578
James Smart57127f12007-10-27 13:37:05 -0400579 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
580 lpfc_printf_log(phba,
581 KERN_ERR,
582 LOG_INIT,
583 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400584 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400585 rc);
586 mempool_free(pmb, phba->mbox_mem_pool);
587 }
James Smart97207482008-12-04 22:39:19 -0500588
589 /* Get Option rom version */
590 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
591 lpfc_dump_wakeup_param(phba, pmb);
592 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
593 pmb->vport = phba->pport;
594 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
595
596 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400598 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500599 mempool_free(pmb, phba->mbox_mem_pool);
600 }
601
James Smartd7c255b2008-08-24 21:50:00 -0400602 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400603}
604
James Smarte59058c2008-08-24 21:49:00 -0400605/**
James Smart3621a712009-04-06 18:47:14 -0400606 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400607 * @phba: pointer to lpfc HBA data structure.
608 *
609 * This routine will do LPFC uninitialization before the HBA is reset when
610 * bringing down the SLI Layer.
611 *
612 * Return codes
613 * 0 - success.
614 * Any other value - error.
615 **/
dea31012005-04-17 16:05:31 -0500616int
James Smart2e0fef82007-06-17 19:56:36 -0500617lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500618{
James Smart1b32f6a2008-02-08 18:49:39 -0500619 struct lpfc_vport **vports;
620 int i;
James Smart3772a992009-05-22 14:50:54 -0400621
622 if (phba->sli_rev <= LPFC_SLI_REV3) {
623 /* Disable interrupts */
624 writel(0, phba->HCregaddr);
625 readl(phba->HCregaddr); /* flush */
626 }
dea31012005-04-17 16:05:31 -0500627
James Smart1b32f6a2008-02-08 18:49:39 -0500628 if (phba->pport->load_flag & FC_UNLOADING)
629 lpfc_cleanup_discovery_resources(phba->pport);
630 else {
631 vports = lpfc_create_vport_work_array(phba);
632 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400633 for (i = 0; i <= phba->max_vports &&
634 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500635 lpfc_cleanup_discovery_resources(vports[i]);
636 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500637 }
638 return 0;
dea31012005-04-17 16:05:31 -0500639}
640
James Smarte59058c2008-08-24 21:49:00 -0400641/**
James Smart3772a992009-05-22 14:50:54 -0400642 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400643 * @phba: pointer to lpfc HBA data structure.
644 *
645 * This routine will do uninitialization after the HBA is reset when bring
646 * down the SLI Layer.
647 *
648 * Return codes
649 * 0 - sucess.
650 * Any other value - error.
651 **/
James Smart3772a992009-05-22 14:50:54 -0400652static int
653lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500654{
655 struct lpfc_sli *psli = &phba->sli;
656 struct lpfc_sli_ring *pring;
657 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500658 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500659 int i;
660
James Smart92d7f7b2007-06-17 19:56:38 -0500661 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
662 lpfc_sli_hbqbuf_free_all(phba);
663 else {
664 /* Cleanup preposted buffers on the ELS ring */
665 pring = &psli->ring[LPFC_ELS_RING];
666 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
667 list_del(&mp->list);
668 pring->postbufq_cnt--;
669 lpfc_mbuf_free(phba, mp->virt, mp->phys);
670 kfree(mp);
671 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500672 }
673
James Smart09372822008-01-11 01:52:54 -0500674 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500675 for (i = 0; i < psli->num_rings; i++) {
676 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500677
678 /* At this point in time the HBA is either reset or DOA. Either
679 * way, nothing should be on txcmplq as it will NEVER complete.
680 */
681 list_splice_init(&pring->txcmplq, &completions);
682 pring->txcmplq_cnt = 0;
683 spin_unlock_irq(&phba->hbalock);
684
James Smarta257bf92009-04-06 18:48:10 -0400685 /* Cancel all the IOCBs from the completions list */
686 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
687 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500688
Jamie Wellnitz41415862006-02-28 19:25:27 -0500689 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500690 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500691 }
James Smart09372822008-01-11 01:52:54 -0500692 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500693
694 return 0;
695}
James Smartda0436e2009-05-22 14:51:39 -0400696/**
697 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
698 * @phba: pointer to lpfc HBA data structure.
699 *
700 * This routine will do uninitialization after the HBA is reset when bring
701 * down the SLI Layer.
702 *
703 * Return codes
704 * 0 - sucess.
705 * Any other value - error.
706 **/
707static int
708lpfc_hba_down_post_s4(struct lpfc_hba *phba)
709{
710 struct lpfc_scsi_buf *psb, *psb_next;
711 LIST_HEAD(aborts);
712 int ret;
713 unsigned long iflag = 0;
714 ret = lpfc_hba_down_post_s3(phba);
715 if (ret)
716 return ret;
717 /* At this point in time the HBA is either reset or DOA. Either
718 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
719 * on the lpfc_sgl_list so that it can either be freed if the
720 * driver is unloading or reposted if the driver is restarting
721 * the port.
722 */
723 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
724 /* scsl_buf_list */
725 /* abts_sgl_list_lock required because worker thread uses this
726 * list.
727 */
728 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
729 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
730 &phba->sli4_hba.lpfc_sgl_list);
731 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
732 /* abts_scsi_buf_list_lock required because worker thread uses this
733 * list.
734 */
735 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
736 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
737 &aborts);
738 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
739 spin_unlock_irq(&phba->hbalock);
740
741 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
742 psb->pCmd = NULL;
743 psb->status = IOSTAT_SUCCESS;
744 }
745 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
746 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
747 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
748 return 0;
749}
750
751/**
752 * lpfc_hba_down_post - Wrapper func for hba down post routine
753 * @phba: pointer to lpfc HBA data structure.
754 *
755 * This routine wraps the actual SLI3 or SLI4 routine for performing
756 * uninitialization after the HBA is reset when bring down the SLI Layer.
757 *
758 * Return codes
759 * 0 - sucess.
760 * Any other value - error.
761 **/
762int
763lpfc_hba_down_post(struct lpfc_hba *phba)
764{
765 return (*phba->lpfc_hba_down_post)(phba);
766}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500767
James Smarte59058c2008-08-24 21:49:00 -0400768/**
James Smart3621a712009-04-06 18:47:14 -0400769 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400770 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
771 *
772 * This is the HBA-timer timeout handler registered to the lpfc driver. When
773 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
774 * work-port-events bitmap and the worker thread is notified. This timeout
775 * event will be used by the worker thread to invoke the actual timeout
776 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
777 * be performed in the timeout handler and the HBA timeout event bit shall
778 * be cleared by the worker thread after it has taken the event bitmap out.
779 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100780static void
James Smart858c9f62007-06-17 19:56:39 -0500781lpfc_hb_timeout(unsigned long ptr)
782{
783 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400784 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500785 unsigned long iflag;
786
787 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400788
789 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500790 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400791 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
792 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500793 phba->pport->work_port_events |= WORKER_HB_TMO;
794 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
795
James Smart93996272008-08-24 21:50:30 -0400796 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400797 if (!tmo_posted)
798 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500799 return;
800}
801
James Smarte59058c2008-08-24 21:49:00 -0400802/**
James Smart3621a712009-04-06 18:47:14 -0400803 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400804 * @phba: pointer to lpfc hba data structure.
805 * @pmboxq: pointer to the driver internal queue element for mailbox command.
806 *
807 * This is the callback function to the lpfc heart-beat mailbox command.
808 * If configured, the lpfc driver issues the heart-beat mailbox command to
809 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
810 * heart-beat mailbox command is issued, the driver shall set up heart-beat
811 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
812 * heart-beat outstanding state. Once the mailbox command comes back and
813 * no error conditions detected, the heart-beat mailbox command timer is
814 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
815 * state is cleared for the next heart-beat. If the timer expired with the
816 * heart-beat outstanding state set, the driver will put the HBA offline.
817 **/
James Smart858c9f62007-06-17 19:56:39 -0500818static void
819lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
820{
821 unsigned long drvr_flag;
822
823 spin_lock_irqsave(&phba->hbalock, drvr_flag);
824 phba->hb_outstanding = 0;
825 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
826
James Smart93996272008-08-24 21:50:30 -0400827 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500828 mempool_free(pmboxq, phba->mbox_mem_pool);
829 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
830 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400831 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500832 mod_timer(&phba->hb_tmofunc,
833 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
834 return;
835}
836
James Smarte59058c2008-08-24 21:49:00 -0400837/**
James Smart3621a712009-04-06 18:47:14 -0400838 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400839 * @phba: pointer to lpfc hba data structure.
840 *
841 * This is the actual HBA-timer timeout handler to be invoked by the worker
842 * thread whenever the HBA timer fired and HBA-timeout event posted. This
843 * handler performs any periodic operations needed for the device. If such
844 * periodic event has already been attended to either in the interrupt handler
845 * or by processing slow-ring or fast-ring events within the HBA-timer
846 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
847 * the timer for the next timeout period. If lpfc heart-beat mailbox command
848 * is configured and there is no heart-beat mailbox command outstanding, a
849 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
850 * has been a heart-beat mailbox command outstanding, the HBA shall be put
851 * to offline.
852 **/
James Smart858c9f62007-06-17 19:56:39 -0500853void
854lpfc_hb_timeout_handler(struct lpfc_hba *phba)
855{
James Smart45ed1192009-10-02 15:17:02 -0400856 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -0500857 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500858 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -0400859 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -0500860 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500861 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500862
James Smart45ed1192009-10-02 15:17:02 -0400863 vports = lpfc_create_vport_work_array(phba);
864 if (vports != NULL)
865 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
866 lpfc_rcv_seq_check_edtov(vports[i]);
867 lpfc_destroy_vport_work_array(phba, vports);
868
James Smart858c9f62007-06-17 19:56:39 -0500869 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400870 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500871 (phba->pport->fc_flag & FC_OFFLINE_MODE))
872 return;
873
874 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -0500875
876 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
877 jiffies)) {
878 spin_unlock_irq(&phba->pport->work_port_lock);
879 if (!phba->hb_outstanding)
880 mod_timer(&phba->hb_tmofunc,
881 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
882 else
883 mod_timer(&phba->hb_tmofunc,
884 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
885 return;
886 }
887 spin_unlock_irq(&phba->pport->work_port_lock);
888
James Smart0ff10d42008-01-11 01:52:36 -0500889 if (phba->elsbuf_cnt &&
890 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
891 spin_lock_irq(&phba->hbalock);
892 list_splice_init(&phba->elsbuf, &completions);
893 phba->elsbuf_cnt = 0;
894 phba->elsbuf_prev_cnt = 0;
895 spin_unlock_irq(&phba->hbalock);
896
897 while (!list_empty(&completions)) {
898 list_remove_head(&completions, buf_ptr,
899 struct lpfc_dmabuf, list);
900 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
901 kfree(buf_ptr);
902 }
903 }
904 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
905
James Smart858c9f62007-06-17 19:56:39 -0500906 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -0500907 if (phba->cfg_enable_hba_heartbeat) {
908 if (!phba->hb_outstanding) {
909 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
910 if (!pmboxq) {
911 mod_timer(&phba->hb_tmofunc,
912 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
913 return;
914 }
915
916 lpfc_heart_beat(phba, pmboxq);
917 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
918 pmboxq->vport = phba->pport;
919 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
920
921 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
922 mempool_free(pmboxq, phba->mbox_mem_pool);
923 mod_timer(&phba->hb_tmofunc,
924 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
925 return;
926 }
James Smart858c9f62007-06-17 19:56:39 -0500927 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -0500928 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
929 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -0500930 return;
James Smart13815c82008-01-11 01:52:48 -0500931 } else {
932 /*
933 * If heart beat timeout called with hb_outstanding set
934 * we need to take the HBA offline.
935 */
936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
937 "0459 Adapter heartbeat failure, "
938 "taking this port offline.\n");
939
940 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400941 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart13815c82008-01-11 01:52:48 -0500942 spin_unlock_irq(&phba->hbalock);
943
944 lpfc_offline_prep(phba);
945 lpfc_offline(phba);
946 lpfc_unblock_mgmt_io(phba);
947 phba->link_state = LPFC_HBA_ERROR;
948 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -0500949 }
James Smart858c9f62007-06-17 19:56:39 -0500950 }
951}
952
James Smarte59058c2008-08-24 21:49:00 -0400953/**
James Smart3621a712009-04-06 18:47:14 -0400954 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -0400955 * @phba: pointer to lpfc hba data structure.
956 *
957 * This routine is called to bring the HBA offline when HBA hardware error
958 * other than Port Error 6 has been detected.
959 **/
James Smart09372822008-01-11 01:52:54 -0500960static void
961lpfc_offline_eratt(struct lpfc_hba *phba)
962{
963 struct lpfc_sli *psli = &phba->sli;
964
965 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -0400966 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -0500967 spin_unlock_irq(&phba->hbalock);
968 lpfc_offline_prep(phba);
969
970 lpfc_offline(phba);
971 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400972 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500973 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -0400974 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500975 lpfc_hba_down_post(phba);
976 lpfc_sli_brdready(phba, HS_MBRDY);
977 lpfc_unblock_mgmt_io(phba);
978 phba->link_state = LPFC_HBA_ERROR;
979 return;
980}
981
James Smarte59058c2008-08-24 21:49:00 -0400982/**
James Smartda0436e2009-05-22 14:51:39 -0400983 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
984 * @phba: pointer to lpfc hba data structure.
985 *
986 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
987 * other than Port Error 6 has been detected.
988 **/
989static void
990lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
991{
992 lpfc_offline_prep(phba);
993 lpfc_offline(phba);
994 lpfc_sli4_brdreset(phba);
995 lpfc_hba_down_post(phba);
996 lpfc_sli4_post_status_check(phba);
997 lpfc_unblock_mgmt_io(phba);
998 phba->link_state = LPFC_HBA_ERROR;
999}
1000
1001/**
James Smarta257bf92009-04-06 18:48:10 -04001002 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1003 * @phba: pointer to lpfc hba data structure.
1004 *
1005 * This routine is invoked to handle the deferred HBA hardware error
1006 * conditions. This type of error is indicated by HBA by setting ER1
1007 * and another ER bit in the host status register. The driver will
1008 * wait until the ER1 bit clears before handling the error condition.
1009 **/
1010static void
1011lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1012{
1013 uint32_t old_host_status = phba->work_hs;
1014 struct lpfc_sli_ring *pring;
1015 struct lpfc_sli *psli = &phba->sli;
1016
James Smartf4b4c682009-05-22 14:53:12 -04001017 /* If the pci channel is offline, ignore possible errors,
1018 * since we cannot communicate with the pci card anyway.
1019 */
1020 if (pci_channel_offline(phba->pcidev)) {
1021 spin_lock_irq(&phba->hbalock);
1022 phba->hba_flag &= ~DEFER_ERATT;
1023 spin_unlock_irq(&phba->hbalock);
1024 return;
1025 }
1026
James Smarta257bf92009-04-06 18:48:10 -04001027 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1028 "0479 Deferred Adapter Hardware Error "
1029 "Data: x%x x%x x%x\n",
1030 phba->work_hs,
1031 phba->work_status[0], phba->work_status[1]);
1032
1033 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001034 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001035 spin_unlock_irq(&phba->hbalock);
1036
1037
1038 /*
1039 * Firmware stops when it triggred erratt. That could cause the I/Os
1040 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1041 * SCSI layer retry it after re-establishing link.
1042 */
1043 pring = &psli->ring[psli->fcp_ring];
1044 lpfc_sli_abort_iocb_ring(phba, pring);
1045
1046 /*
1047 * There was a firmware error. Take the hba offline and then
1048 * attempt to restart it.
1049 */
1050 lpfc_offline_prep(phba);
1051 lpfc_offline(phba);
1052
1053 /* Wait for the ER1 bit to clear.*/
1054 while (phba->work_hs & HS_FFER1) {
1055 msleep(100);
1056 phba->work_hs = readl(phba->HSregaddr);
1057 /* If driver is unloading let the worker thread continue */
1058 if (phba->pport->load_flag & FC_UNLOADING) {
1059 phba->work_hs = 0;
1060 break;
1061 }
1062 }
1063
1064 /*
1065 * This is to ptrotect against a race condition in which
1066 * first write to the host attention register clear the
1067 * host status register.
1068 */
1069 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1070 phba->work_hs = old_host_status & ~HS_FFER1;
1071
James Smart3772a992009-05-22 14:50:54 -04001072 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001073 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001074 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001075 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1076 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1077}
1078
James Smart3772a992009-05-22 14:50:54 -04001079static void
1080lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1081{
1082 struct lpfc_board_event_header board_event;
1083 struct Scsi_Host *shost;
1084
1085 board_event.event_type = FC_REG_BOARD_EVENT;
1086 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1087 shost = lpfc_shost_from_vport(phba->pport);
1088 fc_host_post_vendor_event(shost, fc_get_event_number(),
1089 sizeof(board_event),
1090 (char *) &board_event,
1091 LPFC_NL_VENDOR_ID);
1092}
1093
James Smarta257bf92009-04-06 18:48:10 -04001094/**
James Smart3772a992009-05-22 14:50:54 -04001095 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001096 * @phba: pointer to lpfc hba data structure.
1097 *
1098 * This routine is invoked to handle the following HBA hardware error
1099 * conditions:
1100 * 1 - HBA error attention interrupt
1101 * 2 - DMA ring index out of range
1102 * 3 - Mailbox command came back as unknown
1103 **/
James Smart3772a992009-05-22 14:50:54 -04001104static void
1105lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001106{
James Smart2e0fef82007-06-17 19:56:36 -05001107 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001108 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001109 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001110 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001111 unsigned long temperature;
1112 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001113 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001114
Linas Vepstas8d63f372007-02-14 14:28:36 -06001115 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001116 * since we cannot communicate with the pci card anyway.
1117 */
1118 if (pci_channel_offline(phba->pcidev)) {
1119 spin_lock_irq(&phba->hbalock);
1120 phba->hba_flag &= ~DEFER_ERATT;
1121 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001122 return;
James Smart3772a992009-05-22 14:50:54 -04001123 }
1124
James Smart13815c82008-01-11 01:52:48 -05001125 /* If resets are disabled then leave the HBA alone and return */
1126 if (!phba->cfg_enable_hba_reset)
1127 return;
dea31012005-04-17 16:05:31 -05001128
James Smartea2151b2008-09-07 11:52:10 -04001129 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001130 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001131
James Smarta257bf92009-04-06 18:48:10 -04001132 if (phba->hba_flag & DEFER_ERATT)
1133 lpfc_handle_deferred_eratt(phba);
1134
James Smart97eab632008-04-07 10:16:05 -04001135 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -05001136 /* Re-establishing Link */
1137 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001138 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -05001139 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001140 phba->work_hs,
dea31012005-04-17 16:05:31 -05001141 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001142
James Smart92d7f7b2007-06-17 19:56:38 -05001143 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001144 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001145 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001146
1147 /*
1148 * Firmware stops when it triggled erratt with HS_FFER6.
1149 * That could cause the I/Os dropped by the firmware.
1150 * Error iocb (I/O) on txcmplq and let the SCSI layer
1151 * retry it after re-establishing link.
1152 */
1153 pring = &psli->ring[psli->fcp_ring];
1154 lpfc_sli_abort_iocb_ring(phba, pring);
1155
dea31012005-04-17 16:05:31 -05001156 /*
1157 * There was a firmware error. Take the hba offline and then
1158 * attempt to restart it.
1159 */
James Smart46fa3112007-04-25 09:51:45 -04001160 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001161 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001162 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001163 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001164 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001165 return;
1166 }
James Smart46fa3112007-04-25 09:51:45 -04001167 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001168 } else if (phba->work_hs & HS_CRIT_TEMP) {
1169 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1170 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1171 temp_event_data.event_code = LPFC_CRIT_TEMP;
1172 temp_event_data.data = (uint32_t)temperature;
1173
1174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001175 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001176 "(%ld), taking this port offline "
1177 "Data: x%x x%x x%x\n",
1178 temperature, phba->work_hs,
1179 phba->work_status[0], phba->work_status[1]);
1180
1181 shost = lpfc_shost_from_vport(phba->pport);
1182 fc_host_post_vendor_event(shost, fc_get_event_number(),
1183 sizeof(temp_event_data),
1184 (char *) &temp_event_data,
1185 SCSI_NL_VID_TYPE_PCI
1186 | PCI_VENDOR_ID_EMULEX);
1187
James Smart7af67052007-10-27 13:38:11 -04001188 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001189 phba->over_temp_state = HBA_OVER_TEMP;
1190 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001191 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001192
dea31012005-04-17 16:05:31 -05001193 } else {
1194 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001195 * failure is a value other than FFER6. Do not call the offline
1196 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001197 */
1198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001199 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001200 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001201 phba->work_hs,
dea31012005-04-17 16:05:31 -05001202 phba->work_status[0], phba->work_status[1]);
1203
James Smartd2873e42006-08-18 17:46:43 -04001204 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001205 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001206 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001207 sizeof(event_data), (char *) &event_data,
1208 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1209
James Smart09372822008-01-11 01:52:54 -05001210 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001211 }
James Smart93996272008-08-24 21:50:30 -04001212 return;
dea31012005-04-17 16:05:31 -05001213}
1214
James Smarte59058c2008-08-24 21:49:00 -04001215/**
James Smartda0436e2009-05-22 14:51:39 -04001216 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1217 * @phba: pointer to lpfc hba data structure.
1218 *
1219 * This routine is invoked to handle the SLI4 HBA hardware error attention
1220 * conditions.
1221 **/
1222static void
1223lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1224{
1225 struct lpfc_vport *vport = phba->pport;
1226 uint32_t event_data;
1227 struct Scsi_Host *shost;
1228
1229 /* If the pci channel is offline, ignore possible errors, since
1230 * we cannot communicate with the pci card anyway.
1231 */
1232 if (pci_channel_offline(phba->pcidev))
1233 return;
1234 /* If resets are disabled then leave the HBA alone and return */
1235 if (!phba->cfg_enable_hba_reset)
1236 return;
1237
1238 /* Send an internal error event to mgmt application */
1239 lpfc_board_errevt_to_mgmt(phba);
1240
1241 /* For now, the actual action for SLI4 device handling is not
1242 * specified yet, just treated it as adaptor hardware failure
1243 */
1244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1245 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1246 phba->work_status[0], phba->work_status[1]);
1247
1248 event_data = FC_REG_DUMP_EVENT;
1249 shost = lpfc_shost_from_vport(vport);
1250 fc_host_post_vendor_event(shost, fc_get_event_number(),
1251 sizeof(event_data), (char *) &event_data,
1252 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1253
1254 lpfc_sli4_offline_eratt(phba);
1255}
1256
1257/**
1258 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1259 * @phba: pointer to lpfc HBA data structure.
1260 *
1261 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1262 * routine from the API jump table function pointer from the lpfc_hba struct.
1263 *
1264 * Return codes
1265 * 0 - sucess.
1266 * Any other value - error.
1267 **/
1268void
1269lpfc_handle_eratt(struct lpfc_hba *phba)
1270{
1271 (*phba->lpfc_handle_eratt)(phba);
1272}
1273
1274/**
James Smart3621a712009-04-06 18:47:14 -04001275 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001276 * @phba: pointer to lpfc hba data structure.
1277 *
1278 * This routine is invoked from the worker thread to handle a HBA host
1279 * attention link event.
1280 **/
dea31012005-04-17 16:05:31 -05001281void
James Smart2e0fef82007-06-17 19:56:36 -05001282lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001283{
James Smart2e0fef82007-06-17 19:56:36 -05001284 struct lpfc_vport *vport = phba->pport;
1285 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001286 LPFC_MBOXQ_t *pmb;
1287 volatile uint32_t control;
1288 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001289 int rc = 0;
dea31012005-04-17 16:05:31 -05001290
1291 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001292 if (!pmb) {
1293 rc = 1;
dea31012005-04-17 16:05:31 -05001294 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001295 }
dea31012005-04-17 16:05:31 -05001296
1297 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001298 if (!mp) {
1299 rc = 2;
dea31012005-04-17 16:05:31 -05001300 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001301 }
dea31012005-04-17 16:05:31 -05001302
1303 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001304 if (!mp->virt) {
1305 rc = 3;
dea31012005-04-17 16:05:31 -05001306 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001307 }
dea31012005-04-17 16:05:31 -05001308
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001309 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001310 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001311
1312 psli->slistat.link_event++;
1313 lpfc_read_la(phba, pmb, mp);
1314 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001315 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001316 /* Block ELS IOCBs until we have processed this mbox command */
1317 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001318 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001319 if (rc == MBX_NOT_FINISHED) {
1320 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001321 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001322 }
dea31012005-04-17 16:05:31 -05001323
1324 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001325 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001326 writel(HA_LATT, phba->HAregaddr);
1327 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001328 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001329
1330 return;
1331
James Smart14691152006-12-02 13:34:28 -05001332lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001333 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001334 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001335lpfc_handle_latt_free_mp:
1336 kfree(mp);
1337lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001338 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001339lpfc_handle_latt_err_exit:
1340 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001341 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001342 psli->sli_flag |= LPFC_PROCESS_LA;
1343 control = readl(phba->HCregaddr);
1344 control |= HC_LAINT_ENA;
1345 writel(control, phba->HCregaddr);
1346 readl(phba->HCregaddr); /* flush */
1347
1348 /* Clear Link Attention in HA REG */
1349 writel(HA_LATT, phba->HAregaddr);
1350 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001351 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001352 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001353 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001354
James Smart09372822008-01-11 01:52:54 -05001355 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1356 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001357
1358 return;
1359}
1360
James Smarte59058c2008-08-24 21:49:00 -04001361/**
James Smart3621a712009-04-06 18:47:14 -04001362 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001363 * @phba: pointer to lpfc hba data structure.
1364 * @vpd: pointer to the vital product data.
1365 * @len: length of the vital product data in bytes.
1366 *
1367 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1368 * an array of characters. In this routine, the ModelName, ProgramType, and
1369 * ModelDesc, etc. fields of the phba data structure will be populated.
1370 *
1371 * Return codes
1372 * 0 - pointer to the VPD passed in is NULL
1373 * 1 - success
1374 **/
James Smart3772a992009-05-22 14:50:54 -04001375int
James Smart2e0fef82007-06-17 19:56:36 -05001376lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001377{
1378 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001379 int Length;
dea31012005-04-17 16:05:31 -05001380 int i, j;
1381 int finished = 0;
1382 int index = 0;
1383
1384 if (!vpd)
1385 return 0;
1386
1387 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001388 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001389 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001390 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1391 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001392 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001393 switch (vpd[index]) {
1394 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001395 case 0x91:
dea31012005-04-17 16:05:31 -05001396 index += 1;
1397 lenlo = vpd[index];
1398 index += 1;
1399 lenhi = vpd[index];
1400 index += 1;
1401 i = ((((unsigned short)lenhi) << 8) + lenlo);
1402 index += i;
1403 break;
1404 case 0x90:
1405 index += 1;
1406 lenlo = vpd[index];
1407 index += 1;
1408 lenhi = vpd[index];
1409 index += 1;
1410 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001411 if (Length > len - index)
1412 Length = len - index;
dea31012005-04-17 16:05:31 -05001413 while (Length > 0) {
1414 /* Look for Serial Number */
1415 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1416 index += 2;
1417 i = vpd[index];
1418 index += 1;
1419 j = 0;
1420 Length -= (3+i);
1421 while(i--) {
1422 phba->SerialNumber[j++] = vpd[index++];
1423 if (j == 31)
1424 break;
1425 }
1426 phba->SerialNumber[j] = 0;
1427 continue;
1428 }
1429 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1430 phba->vpd_flag |= VPD_MODEL_DESC;
1431 index += 2;
1432 i = vpd[index];
1433 index += 1;
1434 j = 0;
1435 Length -= (3+i);
1436 while(i--) {
1437 phba->ModelDesc[j++] = vpd[index++];
1438 if (j == 255)
1439 break;
1440 }
1441 phba->ModelDesc[j] = 0;
1442 continue;
1443 }
1444 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1445 phba->vpd_flag |= VPD_MODEL_NAME;
1446 index += 2;
1447 i = vpd[index];
1448 index += 1;
1449 j = 0;
1450 Length -= (3+i);
1451 while(i--) {
1452 phba->ModelName[j++] = vpd[index++];
1453 if (j == 79)
1454 break;
1455 }
1456 phba->ModelName[j] = 0;
1457 continue;
1458 }
1459 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1460 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1461 index += 2;
1462 i = vpd[index];
1463 index += 1;
1464 j = 0;
1465 Length -= (3+i);
1466 while(i--) {
1467 phba->ProgramType[j++] = vpd[index++];
1468 if (j == 255)
1469 break;
1470 }
1471 phba->ProgramType[j] = 0;
1472 continue;
1473 }
1474 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1475 phba->vpd_flag |= VPD_PORT;
1476 index += 2;
1477 i = vpd[index];
1478 index += 1;
1479 j = 0;
1480 Length -= (3+i);
1481 while(i--) {
1482 phba->Port[j++] = vpd[index++];
1483 if (j == 19)
1484 break;
1485 }
1486 phba->Port[j] = 0;
1487 continue;
1488 }
1489 else {
1490 index += 2;
1491 i = vpd[index];
1492 index += 1;
1493 index += i;
1494 Length -= (3 + i);
1495 }
1496 }
1497 finished = 0;
1498 break;
1499 case 0x78:
1500 finished = 1;
1501 break;
1502 default:
1503 index ++;
1504 break;
1505 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001506 }
dea31012005-04-17 16:05:31 -05001507
1508 return(1);
1509}
1510
James Smarte59058c2008-08-24 21:49:00 -04001511/**
James Smart3621a712009-04-06 18:47:14 -04001512 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001513 * @phba: pointer to lpfc hba data structure.
1514 * @mdp: pointer to the data structure to hold the derived model name.
1515 * @descp: pointer to the data structure to hold the derived description.
1516 *
1517 * This routine retrieves HBA's description based on its registered PCI device
1518 * ID. The @descp passed into this function points to an array of 256 chars. It
1519 * shall be returned with the model name, maximum speed, and the host bus type.
1520 * The @mdp passed into this function points to an array of 80 chars. When the
1521 * function returns, the @mdp will be filled with the model name.
1522 **/
dea31012005-04-17 16:05:31 -05001523static void
James Smart2e0fef82007-06-17 19:56:36 -05001524lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001525{
1526 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001527 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001528 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001529 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001530 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001531 struct {
1532 char * name;
1533 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001534 char * bus;
James Smart18a3b592006-12-02 13:35:08 -05001535 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001536
1537 if (mdp && mdp[0] != '\0'
1538 && descp && descp[0] != '\0')
1539 return;
1540
1541 if (phba->lmt & LMT_10Gb)
1542 max_speed = 10;
1543 else if (phba->lmt & LMT_8Gb)
1544 max_speed = 8;
1545 else if (phba->lmt & LMT_4Gb)
1546 max_speed = 4;
1547 else if (phba->lmt & LMT_2Gb)
1548 max_speed = 2;
1549 else
1550 max_speed = 1;
dea31012005-04-17 16:05:31 -05001551
1552 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001553
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001554 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001555 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -05001556 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001557 break;
dea31012005-04-17 16:05:31 -05001558 case PCI_DEVICE_ID_SUPERFLY:
1559 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -05001560 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001561 else
James Smart18a3b592006-12-02 13:35:08 -05001562 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001563 break;
1564 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -05001565 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001566 break;
1567 case PCI_DEVICE_ID_CENTAUR:
1568 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -05001569 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001570 else
James Smart18a3b592006-12-02 13:35:08 -05001571 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001572 break;
1573 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -05001574 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001575 break;
1576 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -05001577 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001578 break;
1579 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -05001580 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001581 break;
1582 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -05001583 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001584 break;
1585 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -05001586 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001587 break;
1588 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -05001589 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001590 break;
1591 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -05001592 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001593 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001594 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001595 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001596 break;
1597 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001598 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001599 break;
1600 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -05001601 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001602 break;
1603 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001604 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001605 break;
1606 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001607 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001608 break;
dea31012005-04-17 16:05:31 -05001609 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -05001610 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001611 break;
1612 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -05001613 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001614 break;
1615 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -05001616 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001617 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001618 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001619 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001620 break;
1621 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta257bf92009-04-06 18:48:10 -04001622 m = (typeof(m)){"LP2105", max_speed, "PCIe"};
1623 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001624 break;
dea31012005-04-17 16:05:31 -05001625 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -05001626 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001627 break;
1628 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -05001629 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001630 break;
1631 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -05001632 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001633 break;
1634 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -05001635 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001636 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001637 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -05001638 m = (typeof(m)){"LP11000-S", max_speed,
1639 "PCI-X2"};
1640 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001641 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -05001642 m = (typeof(m)){"LPe11000-S", max_speed,
1643 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001644 break;
James Smartb87eab32007-04-25 09:53:28 -04001645 case PCI_DEVICE_ID_SAT:
1646 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1647 break;
1648 case PCI_DEVICE_ID_SAT_MID:
1649 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1650 break;
1651 case PCI_DEVICE_ID_SAT_SMB:
1652 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1653 break;
1654 case PCI_DEVICE_ID_SAT_DCSP:
1655 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1656 break;
1657 case PCI_DEVICE_ID_SAT_SCSP:
1658 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1659 break;
1660 case PCI_DEVICE_ID_SAT_S:
1661 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1662 break;
James Smart84774a42008-08-24 21:50:06 -04001663 case PCI_DEVICE_ID_HORNET:
1664 m = (typeof(m)){"LP21000", max_speed, "PCIe"};
1665 GE = 1;
1666 break;
1667 case PCI_DEVICE_ID_PROTEUS_VF:
1668 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1669 break;
1670 case PCI_DEVICE_ID_PROTEUS_PF:
1671 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1672 break;
1673 case PCI_DEVICE_ID_PROTEUS_S:
1674 m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
1675 break;
James Smartda0436e2009-05-22 14:51:39 -04001676 case PCI_DEVICE_ID_TIGERSHARK:
1677 oneConnect = 1;
1678 m = (typeof(m)) {"OCe10100-F", max_speed, "PCIe"};
1679 break;
James Smart6669f9b2009-10-02 15:16:45 -04001680 case PCI_DEVICE_ID_TS_BE3:
1681 oneConnect = 1;
1682 m = (typeof(m)) {"OCeXXXXX-F", max_speed, "PCIe"};
1683 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001684 default:
Randy Dunlap041976f2006-06-25 01:58:51 -07001685 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001686 break;
dea31012005-04-17 16:05:31 -05001687 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001688
1689 if (mdp && mdp[0] == '\0')
1690 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001691 /* oneConnect hba requires special processing, they are all initiators
1692 * and we put the port number on the end
1693 */
1694 if (descp && descp[0] == '\0') {
1695 if (oneConnect)
1696 snprintf(descp, 255,
1697 "Emulex OneConnect %s, FCoE Initiator, Port %s",
1698 m.name,
1699 phba->Port);
1700 else
1701 snprintf(descp, 255,
1702 "Emulex %s %d%s %s %s",
1703 m.name, m.max_speed,
1704 (GE) ? "GE" : "Gb",
1705 m.bus,
1706 (GE) ? "FCoE Adapter" :
1707 "Fibre Channel Adapter");
1708 }
dea31012005-04-17 16:05:31 -05001709}
1710
James Smarte59058c2008-08-24 21:49:00 -04001711/**
James Smart3621a712009-04-06 18:47:14 -04001712 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001713 * @phba: pointer to lpfc hba data structure.
1714 * @pring: pointer to a IOCB ring.
1715 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1716 *
1717 * This routine posts a given number of IOCBs with the associated DMA buffer
1718 * descriptors specified by the cnt argument to the given IOCB ring.
1719 *
1720 * Return codes
1721 * The number of IOCBs NOT able to be posted to the IOCB ring.
1722 **/
dea31012005-04-17 16:05:31 -05001723int
James Smart495a7142008-06-14 22:52:59 -04001724lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001725{
1726 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001727 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001728 struct lpfc_dmabuf *mp1, *mp2;
1729
1730 cnt += pring->missbufcnt;
1731
1732 /* While there are buffers to post */
1733 while (cnt > 0) {
1734 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001735 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001736 if (iocb == NULL) {
1737 pring->missbufcnt = cnt;
1738 return cnt;
1739 }
dea31012005-04-17 16:05:31 -05001740 icmd = &iocb->iocb;
1741
1742 /* 2 buffers can be posted per command */
1743 /* Allocate buffer to post */
1744 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1745 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001746 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1747 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001748 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001749 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001750 pring->missbufcnt = cnt;
1751 return cnt;
1752 }
1753
1754 INIT_LIST_HEAD(&mp1->list);
1755 /* Allocate buffer to post */
1756 if (cnt > 1) {
1757 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1758 if (mp2)
1759 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1760 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001761 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001762 kfree(mp2);
dea31012005-04-17 16:05:31 -05001763 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1764 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001765 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001766 pring->missbufcnt = cnt;
1767 return cnt;
1768 }
1769
1770 INIT_LIST_HEAD(&mp2->list);
1771 } else {
1772 mp2 = NULL;
1773 }
1774
1775 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1776 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1777 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1778 icmd->ulpBdeCount = 1;
1779 cnt--;
1780 if (mp2) {
1781 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1782 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1783 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1784 cnt--;
1785 icmd->ulpBdeCount = 2;
1786 }
1787
1788 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1789 icmd->ulpLe = 1;
1790
James Smart3772a992009-05-22 14:50:54 -04001791 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1792 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001793 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1794 kfree(mp1);
1795 cnt++;
1796 if (mp2) {
1797 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1798 kfree(mp2);
1799 cnt++;
1800 }
James Bottomley604a3e32005-10-29 10:28:33 -05001801 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001802 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001803 return cnt;
1804 }
dea31012005-04-17 16:05:31 -05001805 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001806 if (mp2)
dea31012005-04-17 16:05:31 -05001807 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001808 }
1809 pring->missbufcnt = 0;
1810 return 0;
1811}
1812
James Smarte59058c2008-08-24 21:49:00 -04001813/**
James Smart3621a712009-04-06 18:47:14 -04001814 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001815 * @phba: pointer to lpfc hba data structure.
1816 *
1817 * This routine posts initial receive IOCB buffers to the ELS ring. The
1818 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1819 * set to 64 IOCBs.
1820 *
1821 * Return codes
1822 * 0 - success (currently always success)
1823 **/
dea31012005-04-17 16:05:31 -05001824static int
James Smart2e0fef82007-06-17 19:56:36 -05001825lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001826{
1827 struct lpfc_sli *psli = &phba->sli;
1828
1829 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001830 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001831 /* Ring 2 - FCP no buffers needed */
1832
1833 return 0;
1834}
1835
1836#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1837
James Smarte59058c2008-08-24 21:49:00 -04001838/**
James Smart3621a712009-04-06 18:47:14 -04001839 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001840 * @HashResultPointer: pointer to an array as hash table.
1841 *
1842 * This routine sets up the initial values to the array of hash table entries
1843 * for the LC HBAs.
1844 **/
dea31012005-04-17 16:05:31 -05001845static void
1846lpfc_sha_init(uint32_t * HashResultPointer)
1847{
1848 HashResultPointer[0] = 0x67452301;
1849 HashResultPointer[1] = 0xEFCDAB89;
1850 HashResultPointer[2] = 0x98BADCFE;
1851 HashResultPointer[3] = 0x10325476;
1852 HashResultPointer[4] = 0xC3D2E1F0;
1853}
1854
James Smarte59058c2008-08-24 21:49:00 -04001855/**
James Smart3621a712009-04-06 18:47:14 -04001856 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001857 * @HashResultPointer: pointer to an initial/result hash table.
1858 * @HashWorkingPointer: pointer to an working hash table.
1859 *
1860 * This routine iterates an initial hash table pointed by @HashResultPointer
1861 * with the values from the working hash table pointeed by @HashWorkingPointer.
1862 * The results are putting back to the initial hash table, returned through
1863 * the @HashResultPointer as the result hash table.
1864 **/
dea31012005-04-17 16:05:31 -05001865static void
1866lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1867{
1868 int t;
1869 uint32_t TEMP;
1870 uint32_t A, B, C, D, E;
1871 t = 16;
1872 do {
1873 HashWorkingPointer[t] =
1874 S(1,
1875 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1876 8] ^
1877 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1878 } while (++t <= 79);
1879 t = 0;
1880 A = HashResultPointer[0];
1881 B = HashResultPointer[1];
1882 C = HashResultPointer[2];
1883 D = HashResultPointer[3];
1884 E = HashResultPointer[4];
1885
1886 do {
1887 if (t < 20) {
1888 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1889 } else if (t < 40) {
1890 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1891 } else if (t < 60) {
1892 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1893 } else {
1894 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1895 }
1896 TEMP += S(5, A) + E + HashWorkingPointer[t];
1897 E = D;
1898 D = C;
1899 C = S(30, B);
1900 B = A;
1901 A = TEMP;
1902 } while (++t <= 79);
1903
1904 HashResultPointer[0] += A;
1905 HashResultPointer[1] += B;
1906 HashResultPointer[2] += C;
1907 HashResultPointer[3] += D;
1908 HashResultPointer[4] += E;
1909
1910}
1911
James Smarte59058c2008-08-24 21:49:00 -04001912/**
James Smart3621a712009-04-06 18:47:14 -04001913 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04001914 * @RandomChallenge: pointer to the entry of host challenge random number array.
1915 * @HashWorking: pointer to the entry of the working hash array.
1916 *
1917 * This routine calculates the working hash array referred by @HashWorking
1918 * from the challenge random numbers associated with the host, referred by
1919 * @RandomChallenge. The result is put into the entry of the working hash
1920 * array and returned by reference through @HashWorking.
1921 **/
dea31012005-04-17 16:05:31 -05001922static void
1923lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1924{
1925 *HashWorking = (*RandomChallenge ^ *HashWorking);
1926}
1927
James Smarte59058c2008-08-24 21:49:00 -04001928/**
James Smart3621a712009-04-06 18:47:14 -04001929 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04001930 * @phba: pointer to lpfc hba data structure.
1931 * @hbainit: pointer to an array of unsigned 32-bit integers.
1932 *
1933 * This routine performs the special handling for LC HBA initialization.
1934 **/
dea31012005-04-17 16:05:31 -05001935void
1936lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1937{
1938 int t;
1939 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001940 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001941
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001942 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001943 if (!HashWorking)
1944 return;
1945
dea31012005-04-17 16:05:31 -05001946 HashWorking[0] = HashWorking[78] = *pwwnn++;
1947 HashWorking[1] = HashWorking[79] = *pwwnn;
1948
1949 for (t = 0; t < 7; t++)
1950 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1951
1952 lpfc_sha_init(hbainit);
1953 lpfc_sha_iterate(hbainit, HashWorking);
1954 kfree(HashWorking);
1955}
1956
James Smarte59058c2008-08-24 21:49:00 -04001957/**
James Smart3621a712009-04-06 18:47:14 -04001958 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04001959 * @vport: pointer to a virtual N_Port data structure.
1960 *
1961 * This routine performs the necessary cleanups before deleting the @vport.
1962 * It invokes the discovery state machine to perform necessary state
1963 * transitions and to release the ndlps associated with the @vport. Note,
1964 * the physical port is treated as @vport 0.
1965 **/
James Smart87af33f2007-10-27 13:37:43 -04001966void
James Smart2e0fef82007-06-17 19:56:36 -05001967lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001968{
James Smart87af33f2007-10-27 13:37:43 -04001969 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001970 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04001971 int i = 0;
dea31012005-04-17 16:05:31 -05001972
James Smart87af33f2007-10-27 13:37:43 -04001973 if (phba->link_state > LPFC_LINK_DOWN)
1974 lpfc_port_link_failure(vport);
1975
1976 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001977 if (!NLP_CHK_NODE_ACT(ndlp)) {
1978 ndlp = lpfc_enable_node(vport, ndlp,
1979 NLP_STE_UNUSED_NODE);
1980 if (!ndlp)
1981 continue;
1982 spin_lock_irq(&phba->ndlp_lock);
1983 NLP_SET_FREE_REQ(ndlp);
1984 spin_unlock_irq(&phba->ndlp_lock);
1985 /* Trigger the release of the ndlp memory */
1986 lpfc_nlp_put(ndlp);
1987 continue;
1988 }
1989 spin_lock_irq(&phba->ndlp_lock);
1990 if (NLP_CHK_FREE_REQ(ndlp)) {
1991 /* The ndlp should not be in memory free mode already */
1992 spin_unlock_irq(&phba->ndlp_lock);
1993 continue;
1994 } else
1995 /* Indicate request for freeing ndlp memory */
1996 NLP_SET_FREE_REQ(ndlp);
1997 spin_unlock_irq(&phba->ndlp_lock);
1998
James Smart58da1ff2008-04-07 10:15:56 -04001999 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2000 ndlp->nlp_DID == Fabric_DID) {
2001 /* Just free up ndlp with Fabric_DID for vports */
2002 lpfc_nlp_put(ndlp);
2003 continue;
2004 }
2005
James Smart87af33f2007-10-27 13:37:43 -04002006 if (ndlp->nlp_type & NLP_FABRIC)
2007 lpfc_disc_state_machine(vport, ndlp, NULL,
2008 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002009
James Smart87af33f2007-10-27 13:37:43 -04002010 lpfc_disc_state_machine(vport, ndlp, NULL,
2011 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002012
James Smart87af33f2007-10-27 13:37:43 -04002013 }
2014
James Smarta8adb832007-10-27 13:37:53 -04002015 /* At this point, ALL ndlp's should be gone
2016 * because of the previous NLP_EVT_DEVICE_RM.
2017 * Lets wait for this to happen, if needed.
2018 */
James Smart87af33f2007-10-27 13:37:43 -04002019 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002020 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002021 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002022 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002023 list_for_each_entry_safe(ndlp, next_ndlp,
2024 &vport->fc_nodes, nlp_listp) {
2025 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2026 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002027 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002028 "usgmap:x%x refcnt:%d\n",
2029 ndlp->nlp_DID, (void *)ndlp,
2030 ndlp->nlp_usg_map,
2031 atomic_read(
2032 &ndlp->kref.refcount));
2033 }
James Smarta8adb832007-10-27 13:37:53 -04002034 break;
James Smart87af33f2007-10-27 13:37:43 -04002035 }
James Smarta8adb832007-10-27 13:37:53 -04002036
2037 /* Wait for any activity on ndlps to settle */
2038 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002039 }
dea31012005-04-17 16:05:31 -05002040}
2041
James Smarte59058c2008-08-24 21:49:00 -04002042/**
James Smart3621a712009-04-06 18:47:14 -04002043 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002044 * @vport: pointer to a virtual N_Port data structure.
2045 *
2046 * This routine stops all the timers associated with a @vport. This function
2047 * is invoked before disabling or deleting a @vport. Note that the physical
2048 * port is treated as @vport 0.
2049 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002050void
2051lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002052{
James Smart92d7f7b2007-06-17 19:56:38 -05002053 del_timer_sync(&vport->els_tmofunc);
2054 del_timer_sync(&vport->fc_fdmitmo);
2055 lpfc_can_disctmo(vport);
2056 return;
dea31012005-04-17 16:05:31 -05002057}
2058
James Smarte59058c2008-08-24 21:49:00 -04002059/**
James Smart3772a992009-05-22 14:50:54 -04002060 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002061 * @phba: pointer to lpfc hba data structure.
2062 *
2063 * This routine stops all the timers associated with a HBA. This function is
2064 * invoked before either putting a HBA offline or unloading the driver.
2065 **/
James Smart3772a992009-05-22 14:50:54 -04002066void
2067lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002068{
James Smart51ef4c22007-08-02 11:10:31 -04002069 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002070 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002071 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002072 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002073 del_timer_sync(&phba->hb_tmofunc);
2074 phba->hb_outstanding = 0;
2075
2076 switch (phba->pci_dev_grp) {
2077 case LPFC_PCI_DEV_LP:
2078 /* Stop any LightPulse device specific driver timers */
2079 del_timer_sync(&phba->fcp_poll_timer);
2080 break;
2081 case LPFC_PCI_DEV_OC:
2082 /* Stop any OneConnect device sepcific driver timers */
2083 break;
2084 default:
2085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2086 "0297 Invalid device group (x%x)\n",
2087 phba->pci_dev_grp);
2088 break;
2089 }
James Smart2e0fef82007-06-17 19:56:36 -05002090 return;
dea31012005-04-17 16:05:31 -05002091}
2092
James Smarte59058c2008-08-24 21:49:00 -04002093/**
James Smart3621a712009-04-06 18:47:14 -04002094 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002095 * @phba: pointer to lpfc hba data structure.
2096 *
2097 * This routine marks a HBA's management interface as blocked. Once the HBA's
2098 * management interface is marked as blocked, all the user space access to
2099 * the HBA, whether they are from sysfs interface or libdfc interface will
2100 * all be blocked. The HBA is set to block the management interface when the
2101 * driver prepares the HBA interface for online or offline.
2102 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002103static void
2104lpfc_block_mgmt_io(struct lpfc_hba * phba)
2105{
2106 unsigned long iflag;
2107
2108 spin_lock_irqsave(&phba->hbalock, iflag);
2109 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2110 spin_unlock_irqrestore(&phba->hbalock, iflag);
2111}
2112
James Smarte59058c2008-08-24 21:49:00 -04002113/**
James Smart3621a712009-04-06 18:47:14 -04002114 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002115 * @phba: pointer to lpfc hba data structure.
2116 *
2117 * This routine initializes the HBA and brings a HBA online. During this
2118 * process, the management interface is blocked to prevent user space access
2119 * to the HBA interfering with the driver initialization.
2120 *
2121 * Return codes
2122 * 0 - successful
2123 * 1 - failed
2124 **/
dea31012005-04-17 16:05:31 -05002125int
James Smart2e0fef82007-06-17 19:56:36 -05002126lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002127{
Julia Lawall372bd282008-12-16 16:15:08 +01002128 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002129 struct lpfc_vport **vports;
2130 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002131
dea31012005-04-17 16:05:31 -05002132 if (!phba)
2133 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002134 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002135
James Smart2e0fef82007-06-17 19:56:36 -05002136 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002137 return 0;
2138
James Smarted957682007-06-17 19:56:37 -05002139 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002140 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002141
James Smart46fa3112007-04-25 09:51:45 -04002142 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002143
James Smart46fa3112007-04-25 09:51:45 -04002144 if (!lpfc_sli_queue_setup(phba)) {
2145 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002146 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002147 }
2148
James Smartda0436e2009-05-22 14:51:39 -04002149 if (phba->sli_rev == LPFC_SLI_REV4) {
2150 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2151 lpfc_unblock_mgmt_io(phba);
2152 return 1;
2153 }
2154 } else {
2155 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2156 lpfc_unblock_mgmt_io(phba);
2157 return 1;
2158 }
James Smart46fa3112007-04-25 09:51:45 -04002159 }
dea31012005-04-17 16:05:31 -05002160
James Smart549e55c2007-08-02 11:09:51 -04002161 vports = lpfc_create_vport_work_array(phba);
2162 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002163 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002164 struct Scsi_Host *shost;
2165 shost = lpfc_shost_from_vport(vports[i]);
2166 spin_lock_irq(shost->host_lock);
2167 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2168 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2169 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002170 if (phba->sli_rev == LPFC_SLI_REV4)
2171 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002172 spin_unlock_irq(shost->host_lock);
2173 }
James Smart09372822008-01-11 01:52:54 -05002174 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002175
James Smart46fa3112007-04-25 09:51:45 -04002176 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002177 return 0;
2178}
2179
James Smarte59058c2008-08-24 21:49:00 -04002180/**
James Smart3621a712009-04-06 18:47:14 -04002181 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002182 * @phba: pointer to lpfc hba data structure.
2183 *
2184 * This routine marks a HBA's management interface as not blocked. Once the
2185 * HBA's management interface is marked as not blocked, all the user space
2186 * access to the HBA, whether they are from sysfs interface or libdfc
2187 * interface will be allowed. The HBA is set to block the management interface
2188 * when the driver prepares the HBA interface for online or offline and then
2189 * set to unblock the management interface afterwards.
2190 **/
James Smart46fa3112007-04-25 09:51:45 -04002191void
James Smart46fa3112007-04-25 09:51:45 -04002192lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2193{
2194 unsigned long iflag;
2195
James Smart2e0fef82007-06-17 19:56:36 -05002196 spin_lock_irqsave(&phba->hbalock, iflag);
2197 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2198 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002199}
2200
James Smarte59058c2008-08-24 21:49:00 -04002201/**
James Smart3621a712009-04-06 18:47:14 -04002202 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002203 * @phba: pointer to lpfc hba data structure.
2204 *
2205 * This routine is invoked to prepare a HBA to be brought offline. It performs
2206 * unregistration login to all the nodes on all vports and flushes the mailbox
2207 * queue to make it ready to be brought offline.
2208 **/
James Smart46fa3112007-04-25 09:51:45 -04002209void
2210lpfc_offline_prep(struct lpfc_hba * phba)
2211{
James Smart2e0fef82007-06-17 19:56:36 -05002212 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002213 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002214 struct lpfc_vport **vports;
2215 int i;
dea31012005-04-17 16:05:31 -05002216
James Smart2e0fef82007-06-17 19:56:36 -05002217 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002218 return;
dea31012005-04-17 16:05:31 -05002219
James Smart46fa3112007-04-25 09:51:45 -04002220 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002221
2222 lpfc_linkdown(phba);
2223
James Smart87af33f2007-10-27 13:37:43 -04002224 /* Issue an unreg_login to all nodes on all vports */
2225 vports = lpfc_create_vport_work_array(phba);
2226 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002227 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart87af33f2007-10-27 13:37:43 -04002228 struct Scsi_Host *shost;
2229
James Smarta8adb832007-10-27 13:37:53 -04002230 if (vports[i]->load_flag & FC_UNLOADING)
2231 continue;
James Smartc8685952009-11-18 15:39:16 -05002232 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart87af33f2007-10-27 13:37:43 -04002233 shost = lpfc_shost_from_vport(vports[i]);
2234 list_for_each_entry_safe(ndlp, next_ndlp,
2235 &vports[i]->fc_nodes,
2236 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002237 if (!NLP_CHK_NODE_ACT(ndlp))
2238 continue;
James Smart87af33f2007-10-27 13:37:43 -04002239 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2240 continue;
2241 if (ndlp->nlp_type & NLP_FABRIC) {
2242 lpfc_disc_state_machine(vports[i], ndlp,
2243 NULL, NLP_EVT_DEVICE_RECOVERY);
2244 lpfc_disc_state_machine(vports[i], ndlp,
2245 NULL, NLP_EVT_DEVICE_RM);
2246 }
2247 spin_lock_irq(shost->host_lock);
2248 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2249 spin_unlock_irq(shost->host_lock);
2250 lpfc_unreg_rpi(vports[i], ndlp);
2251 }
2252 }
2253 }
James Smart09372822008-01-11 01:52:54 -05002254 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002255
James Smart04c68492009-05-22 14:52:52 -04002256 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002257}
2258
James Smarte59058c2008-08-24 21:49:00 -04002259/**
James Smart3621a712009-04-06 18:47:14 -04002260 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002261 * @phba: pointer to lpfc hba data structure.
2262 *
2263 * This routine actually brings a HBA offline. It stops all the timers
2264 * associated with the HBA, brings down the SLI layer, and eventually
2265 * marks the HBA as in offline state for the upper layer protocol.
2266 **/
James Smart46fa3112007-04-25 09:51:45 -04002267void
James Smart2e0fef82007-06-17 19:56:36 -05002268lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002269{
James Smart549e55c2007-08-02 11:09:51 -04002270 struct Scsi_Host *shost;
2271 struct lpfc_vport **vports;
2272 int i;
James Smart46fa3112007-04-25 09:51:45 -04002273
James Smart549e55c2007-08-02 11:09:51 -04002274 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002275 return;
James Smart688a8862006-07-06 15:49:56 -04002276
James Smartda0436e2009-05-22 14:51:39 -04002277 /* stop port and all timers associated with this hba */
2278 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002279 vports = lpfc_create_vport_work_array(phba);
2280 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002281 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002282 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002283 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002284 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002285 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002286 /* Bring down the SLI Layer and cleanup. The HBA is offline
2287 now. */
2288 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002289 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002290 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002291 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002292 vports = lpfc_create_vport_work_array(phba);
2293 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002294 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002295 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002296 spin_lock_irq(shost->host_lock);
2297 vports[i]->work_port_events = 0;
2298 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2299 spin_unlock_irq(shost->host_lock);
2300 }
James Smart09372822008-01-11 01:52:54 -05002301 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002302}
2303
James Smarte59058c2008-08-24 21:49:00 -04002304/**
James Smart3621a712009-04-06 18:47:14 -04002305 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002306 * @phba: pointer to lpfc hba data structure.
2307 *
2308 * This routine is to free all the SCSI buffers and IOCBs from the driver
2309 * list back to kernel. It is called from lpfc_pci_remove_one to free
2310 * the internal resources before the device is removed from the system.
2311 *
2312 * Return codes
2313 * 0 - successful (for now, it always returns 0)
2314 **/
dea31012005-04-17 16:05:31 -05002315static int
James Smart2e0fef82007-06-17 19:56:36 -05002316lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002317{
2318 struct lpfc_scsi_buf *sb, *sb_next;
2319 struct lpfc_iocbq *io, *io_next;
2320
James Smart2e0fef82007-06-17 19:56:36 -05002321 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002322 /* Release all the lpfc_scsi_bufs maintained by this host. */
2323 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2324 list_del(&sb->list);
2325 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002326 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002327 kfree(sb);
2328 phba->total_scsi_bufs--;
2329 }
2330
2331 /* Release all the lpfc_iocbq entries maintained by this host. */
2332 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2333 list_del(&io->list);
2334 kfree(io);
2335 phba->total_iocbq_bufs--;
2336 }
2337
James Smart2e0fef82007-06-17 19:56:36 -05002338 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002339
2340 return 0;
2341}
2342
James Smarte59058c2008-08-24 21:49:00 -04002343/**
James Smart3621a712009-04-06 18:47:14 -04002344 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002345 * @phba: pointer to lpfc hba data structure.
2346 * @instance: a unique integer ID to this FC port.
2347 * @dev: pointer to the device data structure.
2348 *
2349 * This routine creates a FC port for the upper layer protocol. The FC port
2350 * can be created on top of either a physical port or a virtual port provided
2351 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2352 * and associates the FC port created before adding the shost into the SCSI
2353 * layer.
2354 *
2355 * Return codes
2356 * @vport - pointer to the virtual N_Port data structure.
2357 * NULL - port create failed.
2358 **/
James Smart2e0fef82007-06-17 19:56:36 -05002359struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002360lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002361{
James Smart2e0fef82007-06-17 19:56:36 -05002362 struct lpfc_vport *vport;
2363 struct Scsi_Host *shost;
2364 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002365
James Smart3de2a652007-08-02 11:09:59 -04002366 if (dev != &phba->pcidev->dev)
2367 shost = scsi_host_alloc(&lpfc_vport_template,
2368 sizeof(struct lpfc_vport));
2369 else
2370 shost = scsi_host_alloc(&lpfc_template,
2371 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002372 if (!shost)
2373 goto out;
James Smart47a86172007-04-25 09:53:22 -04002374
James Smart2e0fef82007-06-17 19:56:36 -05002375 vport = (struct lpfc_vport *) shost->hostdata;
2376 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002377 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002378 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002379 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002380
James Smart3de2a652007-08-02 11:09:59 -04002381 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002382 shost->unique_id = instance;
2383 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002384 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002385 shost->this_id = -1;
2386 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002387 if (phba->sli_rev == LPFC_SLI_REV4) {
2388 shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2389 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2390 }
James Smart81301a92008-12-04 22:39:46 -05002391
James Smart47a86172007-04-25 09:53:22 -04002392 /*
James Smart2e0fef82007-06-17 19:56:36 -05002393 * Set initial can_queue value since 0 is no longer supported and
2394 * scsi_add_host will fail. This will be adjusted later based on the
2395 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002396 */
James Smart2e0fef82007-06-17 19:56:36 -05002397 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002398 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002399 shost->transportt = lpfc_vport_transport_template;
2400 vport->port_type = LPFC_NPIV_PORT;
2401 } else {
2402 shost->transportt = lpfc_transport_template;
2403 vport->port_type = LPFC_PHYSICAL_PORT;
2404 }
James Smart47a86172007-04-25 09:53:22 -04002405
James Smart2e0fef82007-06-17 19:56:36 -05002406 /* Initialize all internally managed lists. */
2407 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002408 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002409 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002410
James Smart2e0fef82007-06-17 19:56:36 -05002411 init_timer(&vport->fc_disctmo);
2412 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002413 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002414
James Smart2e0fef82007-06-17 19:56:36 -05002415 init_timer(&vport->fc_fdmitmo);
2416 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002417 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002418
James Smart2e0fef82007-06-17 19:56:36 -05002419 init_timer(&vport->els_tmofunc);
2420 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002421 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002422
James Bottomleyd139b9b2009-11-05 13:33:12 -06002423 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002424 if (error)
2425 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002426
James Smart549e55c2007-08-02 11:09:51 -04002427 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002428 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002429 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002430 return vport;
James Smart47a86172007-04-25 09:53:22 -04002431
James Smart2e0fef82007-06-17 19:56:36 -05002432out_put_shost:
2433 scsi_host_put(shost);
2434out:
2435 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002436}
2437
James Smarte59058c2008-08-24 21:49:00 -04002438/**
James Smart3621a712009-04-06 18:47:14 -04002439 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002440 * @vport: pointer to an lpfc virtual N_Port data structure.
2441 *
2442 * This routine destroys a FC port from the upper layer protocol. All the
2443 * resources associated with the port are released.
2444 **/
James Smart2e0fef82007-06-17 19:56:36 -05002445void
2446destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002447{
James Smart92d7f7b2007-06-17 19:56:38 -05002448 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2449 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002450
James Smart858c9f62007-06-17 19:56:39 -05002451 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002452 fc_remove_host(shost);
2453 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002454
James Smart92d7f7b2007-06-17 19:56:38 -05002455 spin_lock_irq(&phba->hbalock);
2456 list_del_init(&vport->listentry);
2457 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002458
James Smart92d7f7b2007-06-17 19:56:38 -05002459 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002460 return;
James Smart47a86172007-04-25 09:53:22 -04002461}
2462
James Smarte59058c2008-08-24 21:49:00 -04002463/**
James Smart3621a712009-04-06 18:47:14 -04002464 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002465 *
2466 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2467 * uses the kernel idr facility to perform the task.
2468 *
2469 * Return codes:
2470 * instance - a unique integer ID allocated as the new instance.
2471 * -1 - lpfc get instance failed.
2472 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002473int
2474lpfc_get_instance(void)
2475{
2476 int instance = 0;
2477
2478 /* Assign an unused number */
2479 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2480 return -1;
2481 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2482 return -1;
2483 return instance;
2484}
2485
James Smarte59058c2008-08-24 21:49:00 -04002486/**
James Smart3621a712009-04-06 18:47:14 -04002487 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002488 * @shost: pointer to SCSI host data structure.
2489 * @time: elapsed time of the scan in jiffies.
2490 *
2491 * This routine is called by the SCSI layer with a SCSI host to determine
2492 * whether the scan host is finished.
2493 *
2494 * Note: there is no scan_start function as adapter initialization will have
2495 * asynchronously kicked off the link initialization.
2496 *
2497 * Return codes
2498 * 0 - SCSI host scan is not over yet.
2499 * 1 - SCSI host scan is over.
2500 **/
James Smart47a86172007-04-25 09:53:22 -04002501int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2502{
James Smart2e0fef82007-06-17 19:56:36 -05002503 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2504 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002505 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002506
James Smart858c9f62007-06-17 19:56:39 -05002507 spin_lock_irq(shost->host_lock);
2508
James Smart51ef4c22007-08-02 11:10:31 -04002509 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002510 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002511 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002512 }
James Smart2e0fef82007-06-17 19:56:36 -05002513 if (time >= 30 * HZ) {
2514 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002515 "0461 Scanning longer than 30 "
2516 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002517 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002518 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002519 }
2520 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2521 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002522 "0465 Link down longer than 15 "
2523 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002524 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002525 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002526 }
2527
James Smart2e0fef82007-06-17 19:56:36 -05002528 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002529 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002530 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002531 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002532 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002533 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002534 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002535 goto finished;
2536
2537 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002538
2539finished:
James Smart858c9f62007-06-17 19:56:39 -05002540 spin_unlock_irq(shost->host_lock);
2541 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002542}
2543
James Smarte59058c2008-08-24 21:49:00 -04002544/**
James Smart3621a712009-04-06 18:47:14 -04002545 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002546 * @shost: pointer to SCSI host data structure.
2547 *
2548 * This routine initializes a given SCSI host attributes on a FC port. The
2549 * SCSI host can be either on top of a physical port or a virtual port.
2550 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002551void lpfc_host_attrib_init(struct Scsi_Host *shost)
2552{
2553 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2554 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002555 /*
James Smart2e0fef82007-06-17 19:56:36 -05002556 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002557 */
2558
James Smart2e0fef82007-06-17 19:56:36 -05002559 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2560 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002561 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2562
2563 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002564 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002565 fc_host_supported_fc4s(shost)[2] = 1;
2566 fc_host_supported_fc4s(shost)[7] = 1;
2567
James Smart92d7f7b2007-06-17 19:56:38 -05002568 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2569 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002570
2571 fc_host_supported_speeds(shost) = 0;
2572 if (phba->lmt & LMT_10Gb)
2573 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002574 if (phba->lmt & LMT_8Gb)
2575 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002576 if (phba->lmt & LMT_4Gb)
2577 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2578 if (phba->lmt & LMT_2Gb)
2579 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2580 if (phba->lmt & LMT_1Gb)
2581 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2582
2583 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002584 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2585 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002586
2587 /* This value is also unchanging */
2588 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002589 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002590 fc_host_active_fc4s(shost)[2] = 1;
2591 fc_host_active_fc4s(shost)[7] = 1;
2592
James Smart92d7f7b2007-06-17 19:56:38 -05002593 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002594 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002595 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002596 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002597}
dea31012005-04-17 16:05:31 -05002598
James Smarte59058c2008-08-24 21:49:00 -04002599/**
James Smartda0436e2009-05-22 14:51:39 -04002600 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002601 * @phba: pointer to lpfc hba data structure.
2602 *
James Smartda0436e2009-05-22 14:51:39 -04002603 * This routine is invoked to stop an SLI3 device port, it stops the device
2604 * from generating interrupts and stops the device driver's timers for the
2605 * device.
James Smarte59058c2008-08-24 21:49:00 -04002606 **/
James Smartdb2378e2008-02-08 18:49:51 -05002607static void
James Smartda0436e2009-05-22 14:51:39 -04002608lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002609{
James Smartda0436e2009-05-22 14:51:39 -04002610 /* Clear all interrupt enable conditions */
2611 writel(0, phba->HCregaddr);
2612 readl(phba->HCregaddr); /* flush */
2613 /* Clear all pending interrupts */
2614 writel(0xffffffff, phba->HAregaddr);
2615 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002616
James Smartda0436e2009-05-22 14:51:39 -04002617 /* Reset some HBA SLI setup states */
2618 lpfc_stop_hba_timers(phba);
2619 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002620}
2621
James Smarte59058c2008-08-24 21:49:00 -04002622/**
James Smartda0436e2009-05-22 14:51:39 -04002623 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002624 * @phba: pointer to lpfc hba data structure.
2625 *
James Smartda0436e2009-05-22 14:51:39 -04002626 * This routine is invoked to stop an SLI4 device port, it stops the device
2627 * from generating interrupts and stops the device driver's timers for the
2628 * device.
2629 **/
2630static void
2631lpfc_stop_port_s4(struct lpfc_hba *phba)
2632{
2633 /* Reset some HBA SLI4 setup states */
2634 lpfc_stop_hba_timers(phba);
2635 phba->pport->work_port_events = 0;
2636 phba->sli4_hba.intr_enable = 0;
2637 /* Hard clear it for now, shall have more graceful way to wait later */
2638 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2639}
2640
2641/**
2642 * lpfc_stop_port - Wrapper function for stopping hba port
2643 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002644 *
James Smartda0436e2009-05-22 14:51:39 -04002645 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2646 * the API jump table function pointer from the lpfc_hba struct.
2647 **/
2648void
2649lpfc_stop_port(struct lpfc_hba *phba)
2650{
2651 phba->lpfc_stop_port(phba);
2652}
2653
2654/**
2655 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2656 * @phba: pointer to lpfc hba data structure.
2657 *
2658 * This routine is invoked to remove the driver default fcf record from
2659 * the port. This routine currently acts on FCF Index 0.
2660 *
2661 **/
2662void
2663lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2664{
2665 int rc = 0;
2666 LPFC_MBOXQ_t *mboxq;
2667 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2668 uint32_t mbox_tmo, req_len;
2669 uint32_t shdr_status, shdr_add_status;
2670
2671 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2672 if (!mboxq) {
2673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2674 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2675 return;
2676 }
2677
2678 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2679 sizeof(struct lpfc_sli4_cfg_mhdr);
2680 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2681 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2682 req_len, LPFC_SLI4_MBX_EMBED);
2683 /*
2684 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2685 * supports multiple FCF indices.
2686 */
2687 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2688 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2689 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2690 phba->fcf.fcf_indx);
2691
2692 if (!phba->sli4_hba.intr_enable)
2693 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2694 else {
2695 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2696 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2697 }
2698 /* The IOCTL status is embedded in the mailbox subheader. */
2699 shdr_status = bf_get(lpfc_mbox_hdr_status,
2700 &del_fcf_record->header.cfg_shdr.response);
2701 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2702 &del_fcf_record->header.cfg_shdr.response);
2703 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2704 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2705 "2516 DEL FCF of default FCF Index failed "
2706 "mbx status x%x, status x%x add_status x%x\n",
2707 rc, shdr_status, shdr_add_status);
2708 }
2709 if (rc != MBX_TIMEOUT)
2710 mempool_free(mboxq, phba->mbox_mem_pool);
2711}
2712
2713/**
James Smart6669f9b2009-10-02 15:16:45 -04002714 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2715 * @phba: pointer to lpfc hba data structure.
2716 *
2717 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2718 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2719 * was successful and the firmware supports FCoE. Any other return indicates
2720 * a error. It is assumed that this function will be called before interrupts
2721 * are enabled.
2722 **/
2723static int
2724lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2725{
2726 int rc = 0;
2727 LPFC_MBOXQ_t *mboxq;
2728 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2729 uint32_t length;
2730 uint32_t shdr_status, shdr_add_status;
2731
2732 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2733 if (!mboxq) {
2734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2735 "2621 Failed to allocate mbox for "
2736 "query firmware config cmd\n");
2737 return -ENOMEM;
2738 }
2739 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2740 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2741 sizeof(struct lpfc_sli4_cfg_mhdr));
2742 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2743 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2744 length, LPFC_SLI4_MBX_EMBED);
2745 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2746 /* The IOCTL status is embedded in the mailbox subheader. */
2747 shdr_status = bf_get(lpfc_mbox_hdr_status,
2748 &query_fw_cfg->header.cfg_shdr.response);
2749 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2750 &query_fw_cfg->header.cfg_shdr.response);
2751 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2752 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2753 "2622 Query Firmware Config failed "
2754 "mbx status x%x, status x%x add_status x%x\n",
2755 rc, shdr_status, shdr_add_status);
2756 return -EINVAL;
2757 }
2758 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2759 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2760 "2623 FCoE Function not supported by firmware. "
2761 "Function mode = %08x\n",
2762 query_fw_cfg->function_mode);
2763 return -EINVAL;
2764 }
2765 if (rc != MBX_TIMEOUT)
2766 mempool_free(mboxq, phba->mbox_mem_pool);
2767 return 0;
2768}
2769
2770/**
James Smartda0436e2009-05-22 14:51:39 -04002771 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2772 * @phba: pointer to lpfc hba data structure.
2773 * @acqe_link: pointer to the async link completion queue entry.
2774 *
2775 * This routine is to parse the SLI4 link-attention link fault code and
2776 * translate it into the base driver's read link attention mailbox command
2777 * status.
2778 *
2779 * Return: Link-attention status in terms of base driver's coding.
2780 **/
2781static uint16_t
2782lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2783 struct lpfc_acqe_link *acqe_link)
2784{
2785 uint16_t latt_fault;
2786
2787 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2788 case LPFC_ASYNC_LINK_FAULT_NONE:
2789 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2790 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2791 latt_fault = 0;
2792 break;
2793 default:
2794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2795 "0398 Invalid link fault code: x%x\n",
2796 bf_get(lpfc_acqe_link_fault, acqe_link));
2797 latt_fault = MBXERR_ERROR;
2798 break;
2799 }
2800 return latt_fault;
2801}
2802
2803/**
2804 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2805 * @phba: pointer to lpfc hba data structure.
2806 * @acqe_link: pointer to the async link completion queue entry.
2807 *
2808 * This routine is to parse the SLI4 link attention type and translate it
2809 * into the base driver's link attention type coding.
2810 *
2811 * Return: Link attention type in terms of base driver's coding.
2812 **/
2813static uint8_t
2814lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2815 struct lpfc_acqe_link *acqe_link)
2816{
2817 uint8_t att_type;
2818
2819 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2820 case LPFC_ASYNC_LINK_STATUS_DOWN:
2821 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2822 att_type = AT_LINK_DOWN;
2823 break;
2824 case LPFC_ASYNC_LINK_STATUS_UP:
2825 /* Ignore physical link up events - wait for logical link up */
2826 att_type = AT_RESERVED;
2827 break;
2828 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2829 att_type = AT_LINK_UP;
2830 break;
2831 default:
2832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2833 "0399 Invalid link attention type: x%x\n",
2834 bf_get(lpfc_acqe_link_status, acqe_link));
2835 att_type = AT_RESERVED;
2836 break;
2837 }
2838 return att_type;
2839}
2840
2841/**
2842 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2843 * @phba: pointer to lpfc hba data structure.
2844 * @acqe_link: pointer to the async link completion queue entry.
2845 *
2846 * This routine is to parse the SLI4 link-attention link speed and translate
2847 * it into the base driver's link-attention link speed coding.
2848 *
2849 * Return: Link-attention link speed in terms of base driver's coding.
2850 **/
2851static uint8_t
2852lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2853 struct lpfc_acqe_link *acqe_link)
2854{
2855 uint8_t link_speed;
2856
2857 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2858 case LPFC_ASYNC_LINK_SPEED_ZERO:
2859 link_speed = LA_UNKNW_LINK;
2860 break;
2861 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2862 link_speed = LA_UNKNW_LINK;
2863 break;
2864 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2865 link_speed = LA_UNKNW_LINK;
2866 break;
2867 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2868 link_speed = LA_1GHZ_LINK;
2869 break;
2870 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2871 link_speed = LA_10GHZ_LINK;
2872 break;
2873 default:
2874 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2875 "0483 Invalid link-attention link speed: x%x\n",
2876 bf_get(lpfc_acqe_link_speed, acqe_link));
2877 link_speed = LA_UNKNW_LINK;
2878 break;
2879 }
2880 return link_speed;
2881}
2882
2883/**
2884 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2885 * @phba: pointer to lpfc hba data structure.
2886 * @acqe_link: pointer to the async link completion queue entry.
2887 *
2888 * This routine is to handle the SLI4 asynchronous link event.
2889 **/
2890static void
2891lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
2892 struct lpfc_acqe_link *acqe_link)
2893{
2894 struct lpfc_dmabuf *mp;
2895 LPFC_MBOXQ_t *pmb;
2896 MAILBOX_t *mb;
2897 READ_LA_VAR *la;
2898 uint8_t att_type;
2899
2900 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
2901 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
2902 return;
James Smart32b97932009-07-19 10:01:21 -04002903 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04002904 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2905 if (!pmb) {
2906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2907 "0395 The mboxq allocation failed\n");
2908 return;
2909 }
2910 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2911 if (!mp) {
2912 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2913 "0396 The lpfc_dmabuf allocation failed\n");
2914 goto out_free_pmb;
2915 }
2916 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2917 if (!mp->virt) {
2918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2919 "0397 The mbuf allocation failed\n");
2920 goto out_free_dmabuf;
2921 }
2922
2923 /* Cleanup any outstanding ELS commands */
2924 lpfc_els_flush_all_cmd(phba);
2925
2926 /* Block ELS IOCBs until we have done process link event */
2927 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2928
2929 /* Update link event statistics */
2930 phba->sli.slistat.link_event++;
2931
2932 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
2933 lpfc_read_la(phba, pmb, mp);
2934 pmb->vport = phba->pport;
2935
2936 /* Parse and translate status field */
2937 mb = &pmb->u.mb;
2938 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
2939
2940 /* Parse and translate link attention fields */
2941 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2942 la->eventTag = acqe_link->event_tag;
2943 la->attType = att_type;
2944 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
2945
2946 /* Fake the the following irrelvant fields */
2947 la->topology = TOPOLOGY_PT_PT;
2948 la->granted_AL_PA = 0;
2949 la->il = 0;
2950 la->pb = 0;
2951 la->fa = 0;
2952 la->mm = 0;
2953
2954 /* Keep the link status for extra SLI4 state machine reference */
2955 phba->sli4_hba.link_state.speed =
2956 bf_get(lpfc_acqe_link_speed, acqe_link);
2957 phba->sli4_hba.link_state.duplex =
2958 bf_get(lpfc_acqe_link_duplex, acqe_link);
2959 phba->sli4_hba.link_state.status =
2960 bf_get(lpfc_acqe_link_status, acqe_link);
2961 phba->sli4_hba.link_state.physical =
2962 bf_get(lpfc_acqe_link_physical, acqe_link);
2963 phba->sli4_hba.link_state.fault =
2964 bf_get(lpfc_acqe_link_fault, acqe_link);
2965
2966 /* Invoke the lpfc_handle_latt mailbox command callback function */
2967 lpfc_mbx_cmpl_read_la(phba, pmb);
2968
2969 return;
2970
2971out_free_dmabuf:
2972 kfree(mp);
2973out_free_pmb:
2974 mempool_free(pmb, phba->mbox_mem_pool);
2975}
2976
2977/**
2978 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
2979 * @phba: pointer to lpfc hba data structure.
2980 * @acqe_link: pointer to the async fcoe completion queue entry.
2981 *
2982 * This routine is to handle the SLI4 asynchronous fcoe event.
2983 **/
2984static void
2985lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
2986 struct lpfc_acqe_fcoe *acqe_fcoe)
2987{
2988 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
2989 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04002990 struct lpfc_vport *vport;
2991 struct lpfc_nodelist *ndlp;
2992 struct Scsi_Host *shost;
James Smartda0436e2009-05-22 14:51:39 -04002993
James Smart4d9ab992009-10-02 15:16:39 -04002994 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04002995 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04002996 switch (event_type) {
2997 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
2998 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smarte4e74272009-07-19 10:01:38 -04002999 "2546 New FCF found index 0x%x tag 0x%x\n",
James Smart6669f9b2009-10-02 15:16:45 -04003000 acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003001 acqe_fcoe->event_tag);
3002 /*
James Smart32b97932009-07-19 10:01:21 -04003003 * If the current FCF is in discovered state, or
3004 * FCF discovery is in progress do nothing.
James Smartda0436e2009-05-22 14:51:39 -04003005 */
3006 spin_lock_irq(&phba->hbalock);
James Smart32b97932009-07-19 10:01:21 -04003007 if ((phba->fcf.fcf_flag & FCF_DISCOVERED) ||
3008 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
James Smartda0436e2009-05-22 14:51:39 -04003009 spin_unlock_irq(&phba->hbalock);
3010 break;
3011 }
3012 spin_unlock_irq(&phba->hbalock);
3013
3014 /* Read the FCF table and re-discover SAN. */
James Smart6a9c52c2009-10-02 15:16:51 -04003015 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003016 if (rc)
3017 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003018 "2547 Read FCF record failed 0x%x\n",
3019 rc);
James Smartda0436e2009-05-22 14:51:39 -04003020 break;
3021
3022 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003024 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003025 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3026 acqe_fcoe->event_tag);
3027 break;
3028
3029 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3030 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003031 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003032 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003033 acqe_fcoe->event_tag);
3034 /* If the event is not for currently used fcf do nothing */
James Smart6669f9b2009-10-02 15:16:45 -04003035 if (phba->fcf.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003036 break;
3037 /*
3038 * Currently, driver support only one FCF - so treat this as
3039 * a link down.
3040 */
3041 lpfc_linkdown(phba);
3042 /* Unregister FCF if no devices connected to it */
3043 lpfc_unregister_unused_fcf(phba);
3044 break;
James Smart6669f9b2009-10-02 15:16:45 -04003045 case LPFC_FCOE_EVENT_TYPE_CVL:
3046 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3047 "2718 Clear Virtual Link Received for VPI 0x%x"
3048 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3049 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003050 acqe_fcoe->index - phba->vpi_base);
James Smart6669f9b2009-10-02 15:16:45 -04003051 if (!vport)
3052 break;
3053 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3054 if (!ndlp)
3055 break;
3056 shost = lpfc_shost_from_vport(vport);
3057 lpfc_linkdown_port(vport);
3058 if (vport->port_type != LPFC_NPIV_PORT) {
3059 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3060 spin_lock_irq(shost->host_lock);
3061 ndlp->nlp_flag |= NLP_DELAY_TMO;
3062 spin_unlock_irq(shost->host_lock);
3063 ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
3064 vport->port_state = LPFC_FLOGI;
3065 }
3066 break;
James Smartda0436e2009-05-22 14:51:39 -04003067 default:
3068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3069 "0288 Unknown FCoE event type 0x%x event tag "
3070 "0x%x\n", event_type, acqe_fcoe->event_tag);
3071 break;
3072 }
3073}
3074
3075/**
3076 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3077 * @phba: pointer to lpfc hba data structure.
3078 * @acqe_link: pointer to the async dcbx completion queue entry.
3079 *
3080 * This routine is to handle the SLI4 asynchronous dcbx event.
3081 **/
3082static void
3083lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3084 struct lpfc_acqe_dcbx *acqe_dcbx)
3085{
James Smart4d9ab992009-10-02 15:16:39 -04003086 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3088 "0290 The SLI4 DCBX asynchronous event is not "
3089 "handled yet\n");
3090}
3091
3092/**
3093 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3094 * @phba: pointer to lpfc hba data structure.
3095 *
3096 * This routine is invoked by the worker thread to process all the pending
3097 * SLI4 asynchronous events.
3098 **/
3099void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3100{
3101 struct lpfc_cq_event *cq_event;
3102
3103 /* First, declare the async event has been handled */
3104 spin_lock_irq(&phba->hbalock);
3105 phba->hba_flag &= ~ASYNC_EVENT;
3106 spin_unlock_irq(&phba->hbalock);
3107 /* Now, handle all the async events */
3108 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3109 /* Get the first event from the head of the event queue */
3110 spin_lock_irq(&phba->hbalock);
3111 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3112 cq_event, struct lpfc_cq_event, list);
3113 spin_unlock_irq(&phba->hbalock);
3114 /* Process the asynchronous event */
3115 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3116 case LPFC_TRAILER_CODE_LINK:
3117 lpfc_sli4_async_link_evt(phba,
3118 &cq_event->cqe.acqe_link);
3119 break;
3120 case LPFC_TRAILER_CODE_FCOE:
3121 lpfc_sli4_async_fcoe_evt(phba,
3122 &cq_event->cqe.acqe_fcoe);
3123 break;
3124 case LPFC_TRAILER_CODE_DCBX:
3125 lpfc_sli4_async_dcbx_evt(phba,
3126 &cq_event->cqe.acqe_dcbx);
3127 break;
3128 default:
3129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3130 "1804 Invalid asynchrous event code: "
3131 "x%x\n", bf_get(lpfc_trailer_code,
3132 &cq_event->cqe.mcqe_cmpl));
3133 break;
3134 }
3135 /* Free the completion event processed to the free pool */
3136 lpfc_sli4_cq_event_release(phba, cq_event);
3137 }
3138}
3139
3140/**
3141 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3142 * @phba: pointer to lpfc hba data structure.
3143 * @dev_grp: The HBA PCI-Device group number.
3144 *
3145 * This routine is invoked to set up the per HBA PCI-Device group function
3146 * API jump table entries.
3147 *
3148 * Return: 0 if success, otherwise -ENODEV
3149 **/
3150int
3151lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003152{
3153 int rc;
3154
James Smartda0436e2009-05-22 14:51:39 -04003155 /* Set up lpfc PCI-device group */
3156 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003157
James Smartda0436e2009-05-22 14:51:39 -04003158 /* The LPFC_PCI_DEV_OC uses SLI4 */
3159 if (dev_grp == LPFC_PCI_DEV_OC)
3160 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003161
James Smartda0436e2009-05-22 14:51:39 -04003162 /* Set up device INIT API function jump table */
3163 rc = lpfc_init_api_table_setup(phba, dev_grp);
3164 if (rc)
3165 return -ENODEV;
3166 /* Set up SCSI API function jump table */
3167 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3168 if (rc)
3169 return -ENODEV;
3170 /* Set up SLI API function jump table */
3171 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3172 if (rc)
3173 return -ENODEV;
3174 /* Set up MBOX API function jump table */
3175 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3176 if (rc)
3177 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003178
James Smartda0436e2009-05-22 14:51:39 -04003179 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003180}
3181
3182/**
James Smart3621a712009-04-06 18:47:14 -04003183 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003184 * @phba: pointer to lpfc hba data structure.
3185 * @intr_mode: active interrupt mode adopted.
3186 *
3187 * This routine it invoked to log the currently used active interrupt mode
3188 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003189 **/
3190static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003191{
3192 switch (intr_mode) {
3193 case 0:
3194 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3195 "0470 Enable INTx interrupt mode.\n");
3196 break;
3197 case 1:
3198 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3199 "0481 Enabled MSI interrupt mode.\n");
3200 break;
3201 case 2:
3202 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3203 "0480 Enabled MSI-X interrupt mode.\n");
3204 break;
3205 default:
3206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3207 "0482 Illegal interrupt mode.\n");
3208 break;
3209 }
3210 return;
3211}
3212
James Smart5b75da22008-12-04 22:39:35 -05003213/**
James Smart3772a992009-05-22 14:50:54 -04003214 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003215 * @phba: pointer to lpfc hba data structure.
3216 *
James Smart3772a992009-05-22 14:50:54 -04003217 * This routine is invoked to enable the PCI device that is common to all
3218 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003219 *
3220 * Return codes
James Smart3772a992009-05-22 14:50:54 -04003221 * 0 - sucessful
3222 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003223 **/
James Smart3772a992009-05-22 14:50:54 -04003224static int
3225lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003226{
James Smart3772a992009-05-22 14:50:54 -04003227 struct pci_dev *pdev;
3228 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003229
James Smart3772a992009-05-22 14:50:54 -04003230 /* Obtain PCI device reference */
3231 if (!phba->pcidev)
3232 goto out_error;
3233 else
3234 pdev = phba->pcidev;
3235 /* Select PCI BARs */
3236 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3237 /* Enable PCI device */
3238 if (pci_enable_device_mem(pdev))
3239 goto out_error;
3240 /* Request PCI resource for the device */
3241 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3242 goto out_disable_device;
3243 /* Set up device as PCI master and save state for EEH */
3244 pci_set_master(pdev);
3245 pci_try_set_mwi(pdev);
3246 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003247
James Smart3772a992009-05-22 14:50:54 -04003248 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003249
James Smart3772a992009-05-22 14:50:54 -04003250out_disable_device:
3251 pci_disable_device(pdev);
3252out_error:
3253 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003254}
3255
3256/**
James Smart3772a992009-05-22 14:50:54 -04003257 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003258 * @phba: pointer to lpfc hba data structure.
3259 *
James Smart3772a992009-05-22 14:50:54 -04003260 * This routine is invoked to disable the PCI device that is common to all
3261 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003262 **/
3263static void
James Smart3772a992009-05-22 14:50:54 -04003264lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003265{
James Smart3772a992009-05-22 14:50:54 -04003266 struct pci_dev *pdev;
3267 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003268
James Smart3772a992009-05-22 14:50:54 -04003269 /* Obtain PCI device reference */
3270 if (!phba->pcidev)
3271 return;
3272 else
3273 pdev = phba->pcidev;
3274 /* Select PCI BARs */
3275 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3276 /* Release PCI resource and disable PCI device */
3277 pci_release_selected_regions(pdev, bars);
3278 pci_disable_device(pdev);
3279 /* Null out PCI private reference to driver */
3280 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003281
3282 return;
3283}
3284
3285/**
James Smart3772a992009-05-22 14:50:54 -04003286 * lpfc_reset_hba - Reset a hba
3287 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003288 *
James Smart3772a992009-05-22 14:50:54 -04003289 * This routine is invoked to reset a hba device. It brings the HBA
3290 * offline, performs a board restart, and then brings the board back
3291 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3292 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003293 **/
James Smart3772a992009-05-22 14:50:54 -04003294void
3295lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003296{
James Smart3772a992009-05-22 14:50:54 -04003297 /* If resets are disabled then set error state and return. */
3298 if (!phba->cfg_enable_hba_reset) {
3299 phba->link_state = LPFC_HBA_ERROR;
3300 return;
3301 }
3302 lpfc_offline_prep(phba);
3303 lpfc_offline(phba);
3304 lpfc_sli_brdrestart(phba);
3305 lpfc_online(phba);
3306 lpfc_unblock_mgmt_io(phba);
3307}
dea31012005-04-17 16:05:31 -05003308
James Smart3772a992009-05-22 14:50:54 -04003309/**
3310 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3311 * @phba: pointer to lpfc hba data structure.
3312 *
3313 * This routine is invoked to set up the driver internal resources specific to
3314 * support the SLI-3 HBA device it attached to.
3315 *
3316 * Return codes
3317 * 0 - sucessful
3318 * other values - error
3319 **/
3320static int
3321lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3322{
3323 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003324
James Smart3772a992009-05-22 14:50:54 -04003325 /*
3326 * Initialize timers used by driver
3327 */
dea31012005-04-17 16:05:31 -05003328
James Smart3772a992009-05-22 14:50:54 -04003329 /* Heartbeat timer */
3330 init_timer(&phba->hb_tmofunc);
3331 phba->hb_tmofunc.function = lpfc_hb_timeout;
3332 phba->hb_tmofunc.data = (unsigned long)phba;
3333
3334 psli = &phba->sli;
3335 /* MBOX heartbeat timer */
3336 init_timer(&psli->mbox_tmo);
3337 psli->mbox_tmo.function = lpfc_mbox_timeout;
3338 psli->mbox_tmo.data = (unsigned long) phba;
3339 /* FCP polling mode timer */
3340 init_timer(&phba->fcp_poll_timer);
3341 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3342 phba->fcp_poll_timer.data = (unsigned long) phba;
3343 /* Fabric block timer */
3344 init_timer(&phba->fabric_block_timer);
3345 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3346 phba->fabric_block_timer.data = (unsigned long) phba;
3347 /* EA polling mode timer */
3348 init_timer(&phba->eratt_poll);
3349 phba->eratt_poll.function = lpfc_poll_eratt;
3350 phba->eratt_poll.data = (unsigned long) phba;
3351
3352 /* Host attention work mask setup */
3353 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3354 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3355
3356 /* Get all the module params for configuring this host */
3357 lpfc_get_cfgparam(phba);
3358 /*
3359 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3360 * used to create the sg_dma_buf_pool must be dynamically calculated.
3361 * 2 segments are added since the IOCB needs a command and response bde.
3362 */
3363 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3364 sizeof(struct fcp_rsp) +
3365 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3366
3367 if (phba->cfg_enable_bg) {
3368 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3369 phba->cfg_sg_dma_buf_size +=
3370 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3371 }
3372
3373 /* Also reinitialize the host templates with new values. */
3374 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3375 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3376
3377 phba->max_vpi = LPFC_MAX_VPI;
3378 /* This will be set to correct value after config_port mbox */
3379 phba->max_vports = 0;
3380
3381 /*
3382 * Initialize the SLI Layer to run with lpfc HBAs.
3383 */
3384 lpfc_sli_setup(phba);
3385 lpfc_sli_queue_setup(phba);
3386
3387 /* Allocate device driver memory */
3388 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3389 return -ENOMEM;
3390
3391 return 0;
3392}
3393
3394/**
3395 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3396 * @phba: pointer to lpfc hba data structure.
3397 *
3398 * This routine is invoked to unset the driver internal resources set up
3399 * specific for supporting the SLI-3 HBA device it attached to.
3400 **/
3401static void
3402lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3403{
3404 /* Free device driver memory allocated */
3405 lpfc_mem_free_all(phba);
3406
3407 return;
3408}
3409
3410/**
James Smartda0436e2009-05-22 14:51:39 -04003411 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3412 * @phba: pointer to lpfc hba data structure.
3413 *
3414 * This routine is invoked to set up the driver internal resources specific to
3415 * support the SLI-4 HBA device it attached to.
3416 *
3417 * Return codes
3418 * 0 - sucessful
3419 * other values - error
3420 **/
3421static int
3422lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3423{
3424 struct lpfc_sli *psli;
3425 int rc;
3426 int i, hbq_count;
3427
3428 /* Before proceed, wait for POST done and device ready */
3429 rc = lpfc_sli4_post_status_check(phba);
3430 if (rc)
3431 return -ENODEV;
3432
3433 /*
3434 * Initialize timers used by driver
3435 */
3436
3437 /* Heartbeat timer */
3438 init_timer(&phba->hb_tmofunc);
3439 phba->hb_tmofunc.function = lpfc_hb_timeout;
3440 phba->hb_tmofunc.data = (unsigned long)phba;
3441
3442 psli = &phba->sli;
3443 /* MBOX heartbeat timer */
3444 init_timer(&psli->mbox_tmo);
3445 psli->mbox_tmo.function = lpfc_mbox_timeout;
3446 psli->mbox_tmo.data = (unsigned long) phba;
3447 /* Fabric block timer */
3448 init_timer(&phba->fabric_block_timer);
3449 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3450 phba->fabric_block_timer.data = (unsigned long) phba;
3451 /* EA polling mode timer */
3452 init_timer(&phba->eratt_poll);
3453 phba->eratt_poll.function = lpfc_poll_eratt;
3454 phba->eratt_poll.data = (unsigned long) phba;
3455 /*
3456 * We need to do a READ_CONFIG mailbox command here before
3457 * calling lpfc_get_cfgparam. For VFs this will report the
3458 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3459 * All of the resources allocated
3460 * for this Port are tied to these values.
3461 */
3462 /* Get all the module params for configuring this host */
3463 lpfc_get_cfgparam(phba);
3464 phba->max_vpi = LPFC_MAX_VPI;
3465 /* This will be set to correct value after the read_config mbox */
3466 phba->max_vports = 0;
3467
3468 /* Program the default value of vlan_id and fc_map */
3469 phba->valid_vlan = 0;
3470 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3471 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3472 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3473
3474 /*
3475 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3476 * used to create the sg_dma_buf_pool must be dynamically calculated.
3477 * 2 segments are added since the IOCB needs a command and response bde.
3478 * To insure that the scsi sgl does not cross a 4k page boundary only
3479 * sgl sizes of 1k, 2k, 4k, and 8k are supported.
3480 * Table of sgl sizes and seg_cnt:
3481 * sgl size, sg_seg_cnt total seg
3482 * 1k 50 52
3483 * 2k 114 116
3484 * 4k 242 244
3485 * 8k 498 500
3486 * cmd(32) + rsp(160) + (52 * sizeof(sli4_sge)) = 1024
3487 * cmd(32) + rsp(160) + (116 * sizeof(sli4_sge)) = 2048
3488 * cmd(32) + rsp(160) + (244 * sizeof(sli4_sge)) = 4096
3489 * cmd(32) + rsp(160) + (500 * sizeof(sli4_sge)) = 8192
3490 */
3491 if (phba->cfg_sg_seg_cnt <= LPFC_DEFAULT_SG_SEG_CNT)
3492 phba->cfg_sg_seg_cnt = 50;
3493 else if (phba->cfg_sg_seg_cnt <= 114)
3494 phba->cfg_sg_seg_cnt = 114;
3495 else if (phba->cfg_sg_seg_cnt <= 242)
3496 phba->cfg_sg_seg_cnt = 242;
3497 else
3498 phba->cfg_sg_seg_cnt = 498;
3499
3500 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd)
3501 + sizeof(struct fcp_rsp);
3502 phba->cfg_sg_dma_buf_size +=
3503 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
3504
3505 /* Initialize buffer queue management fields */
3506 hbq_count = lpfc_sli_hbq_count();
3507 for (i = 0; i < hbq_count; ++i)
3508 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3509 INIT_LIST_HEAD(&phba->rb_pend_list);
3510 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3511 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3512
3513 /*
3514 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3515 */
3516 /* Initialize the Abort scsi buffer list used by driver */
3517 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3518 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3519 /* This abort list used by worker thread */
3520 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3521
3522 /*
3523 * Initialize dirver internal slow-path work queues
3524 */
3525
3526 /* Driver internel slow-path CQ Event pool */
3527 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3528 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04003529 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04003530 /* Asynchronous event CQ Event work queue list */
3531 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3532 /* Fast-path XRI aborted CQ Event work queue list */
3533 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3534 /* Slow-path XRI aborted CQ Event work queue list */
3535 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3536 /* Receive queue CQ Event work queue list */
3537 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3538
3539 /* Initialize the driver internal SLI layer lists. */
3540 lpfc_sli_setup(phba);
3541 lpfc_sli_queue_setup(phba);
3542
3543 /* Allocate device driver memory */
3544 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3545 if (rc)
3546 return -ENOMEM;
3547
3548 /* Create the bootstrap mailbox command */
3549 rc = lpfc_create_bootstrap_mbox(phba);
3550 if (unlikely(rc))
3551 goto out_free_mem;
3552
3553 /* Set up the host's endian order with the device. */
3554 rc = lpfc_setup_endian_order(phba);
3555 if (unlikely(rc))
3556 goto out_free_bsmbx;
3557
James Smart6669f9b2009-10-02 15:16:45 -04003558 rc = lpfc_sli4_fw_cfg_check(phba);
3559 if (unlikely(rc))
3560 goto out_free_bsmbx;
3561
James Smartda0436e2009-05-22 14:51:39 -04003562 /* Set up the hba's configuration parameters. */
3563 rc = lpfc_sli4_read_config(phba);
3564 if (unlikely(rc))
3565 goto out_free_bsmbx;
3566
3567 /* Perform a function reset */
3568 rc = lpfc_pci_function_reset(phba);
3569 if (unlikely(rc))
3570 goto out_free_bsmbx;
3571
3572 /* Create all the SLI4 queues */
3573 rc = lpfc_sli4_queue_create(phba);
3574 if (rc)
3575 goto out_free_bsmbx;
3576
3577 /* Create driver internal CQE event pool */
3578 rc = lpfc_sli4_cq_event_pool_create(phba);
3579 if (rc)
3580 goto out_destroy_queue;
3581
3582 /* Initialize and populate the iocb list per host */
3583 rc = lpfc_init_sgl_list(phba);
3584 if (rc) {
3585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3586 "1400 Failed to initialize sgl list.\n");
3587 goto out_destroy_cq_event_pool;
3588 }
3589 rc = lpfc_init_active_sgl_array(phba);
3590 if (rc) {
3591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3592 "1430 Failed to initialize sgl list.\n");
3593 goto out_free_sgl_list;
3594 }
3595
3596 rc = lpfc_sli4_init_rpi_hdrs(phba);
3597 if (rc) {
3598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3599 "1432 Failed to initialize rpi headers.\n");
3600 goto out_free_active_sgl;
3601 }
3602
3603 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3604 phba->cfg_fcp_eq_count), GFP_KERNEL);
3605 if (!phba->sli4_hba.fcp_eq_hdl) {
3606 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3607 "2572 Failed allocate memory for fast-path "
3608 "per-EQ handle array\n");
3609 goto out_remove_rpi_hdrs;
3610 }
3611
3612 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3613 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3614 if (!phba->sli4_hba.msix_entries) {
3615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3616 "2573 Failed allocate memory for msi-x "
3617 "interrupt vector entries\n");
3618 goto out_free_fcp_eq_hdl;
3619 }
3620
3621 return rc;
3622
3623out_free_fcp_eq_hdl:
3624 kfree(phba->sli4_hba.fcp_eq_hdl);
3625out_remove_rpi_hdrs:
3626 lpfc_sli4_remove_rpi_hdrs(phba);
3627out_free_active_sgl:
3628 lpfc_free_active_sgl(phba);
3629out_free_sgl_list:
3630 lpfc_free_sgl_list(phba);
3631out_destroy_cq_event_pool:
3632 lpfc_sli4_cq_event_pool_destroy(phba);
3633out_destroy_queue:
3634 lpfc_sli4_queue_destroy(phba);
3635out_free_bsmbx:
3636 lpfc_destroy_bootstrap_mbox(phba);
3637out_free_mem:
3638 lpfc_mem_free(phba);
3639 return rc;
3640}
3641
3642/**
3643 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3644 * @phba: pointer to lpfc hba data structure.
3645 *
3646 * This routine is invoked to unset the driver internal resources set up
3647 * specific for supporting the SLI-4 HBA device it attached to.
3648 **/
3649static void
3650lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3651{
3652 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3653
3654 /* unregister default FCFI from the HBA */
3655 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3656
3657 /* Free the default FCR table */
3658 lpfc_sli_remove_dflt_fcf(phba);
3659
3660 /* Free memory allocated for msi-x interrupt vector entries */
3661 kfree(phba->sli4_hba.msix_entries);
3662
3663 /* Free memory allocated for fast-path work queue handles */
3664 kfree(phba->sli4_hba.fcp_eq_hdl);
3665
3666 /* Free the allocated rpi headers. */
3667 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04003668 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04003669
3670 /* Free the ELS sgl list */
3671 lpfc_free_active_sgl(phba);
3672 lpfc_free_sgl_list(phba);
3673
3674 /* Free the SCSI sgl management array */
3675 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3676
3677 /* Free the SLI4 queues */
3678 lpfc_sli4_queue_destroy(phba);
3679
3680 /* Free the completion queue EQ event pool */
3681 lpfc_sli4_cq_event_release_all(phba);
3682 lpfc_sli4_cq_event_pool_destroy(phba);
3683
3684 /* Reset SLI4 HBA FCoE function */
3685 lpfc_pci_function_reset(phba);
3686
3687 /* Free the bsmbx region. */
3688 lpfc_destroy_bootstrap_mbox(phba);
3689
3690 /* Free the SLI Layer memory with SLI4 HBAs */
3691 lpfc_mem_free_all(phba);
3692
3693 /* Free the current connect table */
3694 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04003695 &phba->fcf_conn_rec_list, list) {
3696 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04003697 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04003698 }
James Smartda0436e2009-05-22 14:51:39 -04003699
3700 return;
3701}
3702
3703/**
James Smart3772a992009-05-22 14:50:54 -04003704 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3705 * @phba: The hba struct for which this call is being executed.
3706 * @dev_grp: The HBA PCI-Device group number.
3707 *
3708 * This routine sets up the device INIT interface API function jump table
3709 * in @phba struct.
3710 *
3711 * Returns: 0 - success, -ENODEV - failure.
3712 **/
3713int
3714lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3715{
3716 switch (dev_grp) {
3717 case LPFC_PCI_DEV_LP:
3718 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3719 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3720 phba->lpfc_stop_port = lpfc_stop_port_s3;
3721 break;
James Smartda0436e2009-05-22 14:51:39 -04003722 case LPFC_PCI_DEV_OC:
3723 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3724 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3725 phba->lpfc_stop_port = lpfc_stop_port_s4;
3726 break;
James Smart3772a992009-05-22 14:50:54 -04003727 default:
3728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3729 "1431 Invalid HBA PCI-device group: 0x%x\n",
3730 dev_grp);
3731 return -ENODEV;
3732 break;
3733 }
3734 return 0;
3735}
3736
3737/**
3738 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3739 * @phba: pointer to lpfc hba data structure.
3740 *
3741 * This routine is invoked to set up the driver internal resources before the
3742 * device specific resource setup to support the HBA device it attached to.
3743 *
3744 * Return codes
3745 * 0 - sucessful
3746 * other values - error
3747 **/
3748static int
3749lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3750{
3751 /*
3752 * Driver resources common to all SLI revisions
3753 */
James Smartea2151b2008-09-07 11:52:10 -04003754 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003755 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003756
James Smarte47c9092008-02-08 18:49:26 -05003757 /* Initialize ndlp management spinlock */
3758 spin_lock_init(&phba->ndlp_lock);
3759
James Smart2e0fef82007-06-17 19:56:36 -05003760 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04003761 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04003762 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05003763
James Smart3772a992009-05-22 14:50:54 -04003764 /* Initialize the wait queue head for the kernel thread */
3765 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05003766
James Smart3772a992009-05-22 14:50:54 -04003767 /* Initialize the scsi buffer list used by driver for scsi IO */
3768 spin_lock_init(&phba->scsi_buf_list_lock);
3769 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05003770
James Smart3772a992009-05-22 14:50:54 -04003771 /* Initialize the fabric iocb list */
3772 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05003773
James Smart3772a992009-05-22 14:50:54 -04003774 /* Initialize list to save ELS buffers */
3775 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05003776
James Smart3772a992009-05-22 14:50:54 -04003777 /* Initialize FCF connection rec list */
3778 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05003779
James Smart3772a992009-05-22 14:50:54 -04003780 return 0;
3781}
dea31012005-04-17 16:05:31 -05003782
James Smart3772a992009-05-22 14:50:54 -04003783/**
3784 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
3785 * @phba: pointer to lpfc hba data structure.
3786 *
3787 * This routine is invoked to set up the driver internal resources after the
3788 * device specific resource setup to support the HBA device it attached to.
3789 *
3790 * Return codes
3791 * 0 - sucessful
3792 * other values - error
3793 **/
3794static int
3795lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
3796{
3797 int error;
3798
3799 /* Startup the kernel thread for this host adapter. */
3800 phba->worker_thread = kthread_run(lpfc_do_work, phba,
3801 "lpfc_worker_%d", phba->brd_no);
3802 if (IS_ERR(phba->worker_thread)) {
3803 error = PTR_ERR(phba->worker_thread);
3804 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003805 }
3806
James Smart3772a992009-05-22 14:50:54 -04003807 return 0;
3808}
3809
3810/**
3811 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
3812 * @phba: pointer to lpfc hba data structure.
3813 *
3814 * This routine is invoked to unset the driver internal resources set up after
3815 * the device specific resource setup for supporting the HBA device it
3816 * attached to.
3817 **/
3818static void
3819lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
3820{
3821 /* Stop kernel worker thread */
3822 kthread_stop(phba->worker_thread);
3823}
3824
3825/**
3826 * lpfc_free_iocb_list - Free iocb list.
3827 * @phba: pointer to lpfc hba data structure.
3828 *
3829 * This routine is invoked to free the driver's IOCB list and memory.
3830 **/
3831static void
3832lpfc_free_iocb_list(struct lpfc_hba *phba)
3833{
3834 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
3835
3836 spin_lock_irq(&phba->hbalock);
3837 list_for_each_entry_safe(iocbq_entry, iocbq_next,
3838 &phba->lpfc_iocb_list, list) {
3839 list_del(&iocbq_entry->list);
3840 kfree(iocbq_entry);
3841 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05003842 }
James Smart3772a992009-05-22 14:50:54 -04003843 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003844
James Smart3772a992009-05-22 14:50:54 -04003845 return;
3846}
dea31012005-04-17 16:05:31 -05003847
James Smart3772a992009-05-22 14:50:54 -04003848/**
3849 * lpfc_init_iocb_list - Allocate and initialize iocb list.
3850 * @phba: pointer to lpfc hba data structure.
3851 *
3852 * This routine is invoked to allocate and initizlize the driver's IOCB
3853 * list and set up the IOCB tag array accordingly.
3854 *
3855 * Return codes
3856 * 0 - sucessful
3857 * other values - error
3858 **/
3859static int
3860lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
3861{
3862 struct lpfc_iocbq *iocbq_entry = NULL;
3863 uint16_t iotag;
3864 int i;
dea31012005-04-17 16:05:31 -05003865
3866 /* Initialize and populate the iocb list per host. */
3867 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04003868 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003869 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05003870 if (iocbq_entry == NULL) {
3871 printk(KERN_ERR "%s: only allocated %d iocbs of "
3872 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003873 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05003874 goto out_free_iocbq;
3875 }
3876
James Bottomley604a3e32005-10-29 10:28:33 -05003877 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
3878 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04003879 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05003880 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04003881 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05003882 goto out_free_iocbq;
3883 }
James Smart3772a992009-05-22 14:50:54 -04003884 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05003885
3886 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003887 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
3888 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05003889 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003890 }
3891
James Smart3772a992009-05-22 14:50:54 -04003892 return 0;
3893
3894out_free_iocbq:
3895 lpfc_free_iocb_list(phba);
3896
3897 return -ENOMEM;
3898}
3899
3900/**
James Smartda0436e2009-05-22 14:51:39 -04003901 * lpfc_free_sgl_list - Free sgl list.
3902 * @phba: pointer to lpfc hba data structure.
3903 *
3904 * This routine is invoked to free the driver's sgl list and memory.
3905 **/
3906static void
3907lpfc_free_sgl_list(struct lpfc_hba *phba)
3908{
3909 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
3910 LIST_HEAD(sglq_list);
3911 int rc = 0;
3912
3913 spin_lock_irq(&phba->hbalock);
3914 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
3915 spin_unlock_irq(&phba->hbalock);
3916
3917 list_for_each_entry_safe(sglq_entry, sglq_next,
3918 &sglq_list, list) {
3919 list_del(&sglq_entry->list);
3920 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
3921 kfree(sglq_entry);
3922 phba->sli4_hba.total_sglq_bufs--;
3923 }
3924 rc = lpfc_sli4_remove_all_sgl_pages(phba);
3925 if (rc) {
3926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04003927 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003928 }
3929 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3930}
3931
3932/**
3933 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
3934 * @phba: pointer to lpfc hba data structure.
3935 *
3936 * This routine is invoked to allocate the driver's active sgl memory.
3937 * This array will hold the sglq_entry's for active IOs.
3938 **/
3939static int
3940lpfc_init_active_sgl_array(struct lpfc_hba *phba)
3941{
3942 int size;
3943 size = sizeof(struct lpfc_sglq *);
3944 size *= phba->sli4_hba.max_cfg_param.max_xri;
3945
3946 phba->sli4_hba.lpfc_sglq_active_list =
3947 kzalloc(size, GFP_KERNEL);
3948 if (!phba->sli4_hba.lpfc_sglq_active_list)
3949 return -ENOMEM;
3950 return 0;
3951}
3952
3953/**
3954 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3955 * @phba: pointer to lpfc hba data structure.
3956 *
3957 * This routine is invoked to walk through the array of active sglq entries
3958 * and free all of the resources.
3959 * This is just a place holder for now.
3960 **/
3961static void
3962lpfc_free_active_sgl(struct lpfc_hba *phba)
3963{
3964 kfree(phba->sli4_hba.lpfc_sglq_active_list);
3965}
3966
3967/**
3968 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3969 * @phba: pointer to lpfc hba data structure.
3970 *
3971 * This routine is invoked to allocate and initizlize the driver's sgl
3972 * list and set up the sgl xritag tag array accordingly.
3973 *
3974 * Return codes
3975 * 0 - sucessful
3976 * other values - error
3977 **/
3978static int
3979lpfc_init_sgl_list(struct lpfc_hba *phba)
3980{
3981 struct lpfc_sglq *sglq_entry = NULL;
3982 int i;
3983 int els_xri_cnt;
3984
3985 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3986 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3987 "2400 lpfc_init_sgl_list els %d.\n",
3988 els_xri_cnt);
3989 /* Initialize and populate the sglq list per host/VF. */
3990 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
3991 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
3992
3993 /* Sanity check on XRI management */
3994 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
3995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3996 "2562 No room left for SCSI XRI allocation: "
3997 "max_xri=%d, els_xri=%d\n",
3998 phba->sli4_hba.max_cfg_param.max_xri,
3999 els_xri_cnt);
4000 return -ENOMEM;
4001 }
4002
4003 /* Allocate memory for the ELS XRI management array */
4004 phba->sli4_hba.lpfc_els_sgl_array =
4005 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4006 GFP_KERNEL);
4007
4008 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4010 "2401 Failed to allocate memory for ELS "
4011 "XRI management array of size %d.\n",
4012 els_xri_cnt);
4013 return -ENOMEM;
4014 }
4015
4016 /* Keep the SCSI XRI into the XRI management array */
4017 phba->sli4_hba.scsi_xri_max =
4018 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4019 phba->sli4_hba.scsi_xri_cnt = 0;
4020
4021 phba->sli4_hba.lpfc_scsi_psb_array =
4022 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4023 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4024
4025 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4027 "2563 Failed to allocate memory for SCSI "
4028 "XRI management array of size %d.\n",
4029 phba->sli4_hba.scsi_xri_max);
4030 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4031 return -ENOMEM;
4032 }
4033
4034 for (i = 0; i < els_xri_cnt; i++) {
4035 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4036 if (sglq_entry == NULL) {
4037 printk(KERN_ERR "%s: only allocated %d sgls of "
4038 "expected %d count. Unloading driver.\n",
4039 __func__, i, els_xri_cnt);
4040 goto out_free_mem;
4041 }
4042
4043 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4044 if (sglq_entry->sli4_xritag == NO_XRI) {
4045 kfree(sglq_entry);
4046 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4047 "Unloading driver.\n", __func__);
4048 goto out_free_mem;
4049 }
4050 sglq_entry->buff_type = GEN_BUFF_TYPE;
4051 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4052 if (sglq_entry->virt == NULL) {
4053 kfree(sglq_entry);
4054 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4055 "Unloading driver.\n", __func__);
4056 goto out_free_mem;
4057 }
4058 sglq_entry->sgl = sglq_entry->virt;
4059 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4060
4061 /* The list order is used by later block SGL registraton */
4062 spin_lock_irq(&phba->hbalock);
4063 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4064 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4065 phba->sli4_hba.total_sglq_bufs++;
4066 spin_unlock_irq(&phba->hbalock);
4067 }
4068 return 0;
4069
4070out_free_mem:
4071 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4072 lpfc_free_sgl_list(phba);
4073 return -ENOMEM;
4074}
4075
4076/**
4077 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4078 * @phba: pointer to lpfc hba data structure.
4079 *
4080 * This routine is invoked to post rpi header templates to the
4081 * HBA consistent with the SLI-4 interface spec. This routine
4082 * posts a PAGE_SIZE memory region to the port to hold up to
4083 * PAGE_SIZE modulo 64 rpi context headers.
4084 * No locks are held here because this is an initialization routine
4085 * called only from probe or lpfc_online when interrupts are not
4086 * enabled and the driver is reinitializing the device.
4087 *
4088 * Return codes
4089 * 0 - sucessful
4090 * ENOMEM - No availble memory
4091 * EIO - The mailbox failed to complete successfully.
4092 **/
4093int
4094lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4095{
4096 int rc = 0;
4097 int longs;
4098 uint16_t rpi_count;
4099 struct lpfc_rpi_hdr *rpi_hdr;
4100
4101 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4102
4103 /*
4104 * Provision an rpi bitmask range for discovery. The total count
4105 * is the difference between max and base + 1.
4106 */
4107 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4108 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4109
4110 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4111 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4112 GFP_KERNEL);
4113 if (!phba->sli4_hba.rpi_bmask)
4114 return -ENOMEM;
4115
4116 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4117 if (!rpi_hdr) {
4118 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4119 "0391 Error during rpi post operation\n");
4120 lpfc_sli4_remove_rpis(phba);
4121 rc = -ENODEV;
4122 }
4123
4124 return rc;
4125}
4126
4127/**
4128 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4129 * @phba: pointer to lpfc hba data structure.
4130 *
4131 * This routine is invoked to allocate a single 4KB memory region to
4132 * support rpis and stores them in the phba. This single region
4133 * provides support for up to 64 rpis. The region is used globally
4134 * by the device.
4135 *
4136 * Returns:
4137 * A valid rpi hdr on success.
4138 * A NULL pointer on any failure.
4139 **/
4140struct lpfc_rpi_hdr *
4141lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4142{
4143 uint16_t rpi_limit, curr_rpi_range;
4144 struct lpfc_dmabuf *dmabuf;
4145 struct lpfc_rpi_hdr *rpi_hdr;
4146
4147 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4148 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4149
4150 spin_lock_irq(&phba->hbalock);
4151 curr_rpi_range = phba->sli4_hba.next_rpi;
4152 spin_unlock_irq(&phba->hbalock);
4153
4154 /*
4155 * The port has a limited number of rpis. The increment here
4156 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4157 * and to allow the full max_rpi range per port.
4158 */
4159 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4160 return NULL;
4161
4162 /*
4163 * First allocate the protocol header region for the port. The
4164 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4165 */
4166 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4167 if (!dmabuf)
4168 return NULL;
4169
4170 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4171 LPFC_HDR_TEMPLATE_SIZE,
4172 &dmabuf->phys,
4173 GFP_KERNEL);
4174 if (!dmabuf->virt) {
4175 rpi_hdr = NULL;
4176 goto err_free_dmabuf;
4177 }
4178
4179 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4180 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4181 rpi_hdr = NULL;
4182 goto err_free_coherent;
4183 }
4184
4185 /* Save the rpi header data for cleanup later. */
4186 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4187 if (!rpi_hdr)
4188 goto err_free_coherent;
4189
4190 rpi_hdr->dmabuf = dmabuf;
4191 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4192 rpi_hdr->page_count = 1;
4193 spin_lock_irq(&phba->hbalock);
4194 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4195 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4196
4197 /*
4198 * The next_rpi stores the next module-64 rpi value to post
4199 * in any subsequent rpi memory region postings.
4200 */
4201 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4202 spin_unlock_irq(&phba->hbalock);
4203 return rpi_hdr;
4204
4205 err_free_coherent:
4206 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4207 dmabuf->virt, dmabuf->phys);
4208 err_free_dmabuf:
4209 kfree(dmabuf);
4210 return NULL;
4211}
4212
4213/**
4214 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4215 * @phba: pointer to lpfc hba data structure.
4216 *
4217 * This routine is invoked to remove all memory resources allocated
4218 * to support rpis. This routine presumes the caller has released all
4219 * rpis consumed by fabric or port logins and is prepared to have
4220 * the header pages removed.
4221 **/
4222void
4223lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4224{
4225 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4226
4227 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4228 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4229 list_del(&rpi_hdr->list);
4230 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4231 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4232 kfree(rpi_hdr->dmabuf);
4233 kfree(rpi_hdr);
4234 }
4235
4236 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4237 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4238}
4239
4240/**
James Smart3772a992009-05-22 14:50:54 -04004241 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4242 * @pdev: pointer to pci device data structure.
4243 *
4244 * This routine is invoked to allocate the driver hba data structure for an
4245 * HBA device. If the allocation is successful, the phba reference to the
4246 * PCI device data structure is set.
4247 *
4248 * Return codes
4249 * pointer to @phba - sucessful
4250 * NULL - error
4251 **/
4252static struct lpfc_hba *
4253lpfc_hba_alloc(struct pci_dev *pdev)
4254{
4255 struct lpfc_hba *phba;
4256
4257 /* Allocate memory for HBA structure */
4258 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4259 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004260 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004261 return NULL;
4262 }
4263
4264 /* Set reference to PCI device in HBA structure */
4265 phba->pcidev = pdev;
4266
4267 /* Assign an unused board number */
4268 phba->brd_no = lpfc_get_instance();
4269 if (phba->brd_no < 0) {
4270 kfree(phba);
4271 return NULL;
4272 }
4273
James Smartf1c3b0f2009-07-19 10:01:32 -04004274 mutex_init(&phba->ct_event_mutex);
4275 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4276
James Smart3772a992009-05-22 14:50:54 -04004277 return phba;
4278}
4279
4280/**
4281 * lpfc_hba_free - Free driver hba data structure with a device.
4282 * @phba: pointer to lpfc hba data structure.
4283 *
4284 * This routine is invoked to free the driver hba data structure with an
4285 * HBA device.
4286 **/
4287static void
4288lpfc_hba_free(struct lpfc_hba *phba)
4289{
4290 /* Release the driver assigned board number */
4291 idr_remove(&lpfc_hba_index, phba->brd_no);
4292
4293 kfree(phba);
4294 return;
4295}
4296
4297/**
4298 * lpfc_create_shost - Create hba physical port with associated scsi host.
4299 * @phba: pointer to lpfc hba data structure.
4300 *
4301 * This routine is invoked to create HBA physical port and associate a SCSI
4302 * host with it.
4303 *
4304 * Return codes
4305 * 0 - sucessful
4306 * other values - error
4307 **/
4308static int
4309lpfc_create_shost(struct lpfc_hba *phba)
4310{
4311 struct lpfc_vport *vport;
4312 struct Scsi_Host *shost;
4313
4314 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004315 phba->fc_edtov = FF_DEF_EDTOV;
4316 phba->fc_ratov = FF_DEF_RATOV;
4317 phba->fc_altov = FF_DEF_ALTOV;
4318 phba->fc_arbtov = FF_DEF_ARBTOV;
4319
James Smart3de2a652007-08-02 11:09:59 -04004320 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004321 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004322 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004323
4324 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004325 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004326 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004327 /* Put reference to SCSI host to driver's device private data */
4328 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004329
James Smart3772a992009-05-22 14:50:54 -04004330 return 0;
4331}
dea31012005-04-17 16:05:31 -05004332
James Smart3772a992009-05-22 14:50:54 -04004333/**
4334 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4335 * @phba: pointer to lpfc hba data structure.
4336 *
4337 * This routine is invoked to destroy HBA physical port and the associated
4338 * SCSI host.
4339 **/
4340static void
4341lpfc_destroy_shost(struct lpfc_hba *phba)
4342{
4343 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004344
James Smart3772a992009-05-22 14:50:54 -04004345 /* Destroy physical port that associated with the SCSI host */
4346 destroy_port(vport);
4347
4348 return;
4349}
4350
4351/**
4352 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4353 * @phba: pointer to lpfc hba data structure.
4354 * @shost: the shost to be used to detect Block guard settings.
4355 *
4356 * This routine sets up the local Block guard protocol settings for @shost.
4357 * This routine also allocates memory for debugging bg buffers.
4358 **/
4359static void
4360lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4361{
4362 int pagecnt = 10;
4363 if (lpfc_prot_mask && lpfc_prot_guard) {
4364 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4365 "1478 Registering BlockGuard with the "
4366 "SCSI layer\n");
4367 scsi_host_set_prot(shost, lpfc_prot_mask);
4368 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004369 }
James Smart81301a92008-12-04 22:39:46 -05004370 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004371 while (pagecnt) {
4372 spin_lock_init(&_dump_buf_lock);
4373 _dump_buf_data =
4374 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4375 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004376 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4377 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004378 "_dump_buf_data at 0x%p\n",
4379 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004380 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004381 memset(_dump_buf_data, 0,
4382 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004383 break;
James Smart3772a992009-05-22 14:50:54 -04004384 } else
James Smart81301a92008-12-04 22:39:46 -05004385 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004386 }
James Smart81301a92008-12-04 22:39:46 -05004387 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004388 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4389 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004390 "memory for hexdump\n");
4391 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004392 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4393 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004394 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004395 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004396 while (pagecnt) {
4397 _dump_buf_dif =
4398 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4399 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004400 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4401 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004402 "_dump_buf_dif at 0x%p\n",
4403 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05004404 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004405 memset(_dump_buf_dif, 0,
4406 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004407 break;
James Smart3772a992009-05-22 14:50:54 -04004408 } else
James Smart81301a92008-12-04 22:39:46 -05004409 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004410 }
James Smart81301a92008-12-04 22:39:46 -05004411 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004412 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4413 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004414 "memory for hexdump\n");
4415 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004416 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4417 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04004418 _dump_buf_dif);
4419}
4420
4421/**
4422 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4423 * @phba: pointer to lpfc hba data structure.
4424 *
4425 * This routine is invoked to perform all the necessary post initialization
4426 * setup for the device.
4427 **/
4428static void
4429lpfc_post_init_setup(struct lpfc_hba *phba)
4430{
4431 struct Scsi_Host *shost;
4432 struct lpfc_adapter_event_header adapter_event;
4433
4434 /* Get the default values for Model Name and Description */
4435 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4436
4437 /*
4438 * hba setup may have changed the hba_queue_depth so we need to
4439 * adjust the value of can_queue.
4440 */
4441 shost = pci_get_drvdata(phba->pcidev);
4442 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4443 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4444 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05004445
4446 lpfc_host_attrib_init(shost);
4447
James Smart2e0fef82007-06-17 19:56:36 -05004448 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4449 spin_lock_irq(shost->host_lock);
4450 lpfc_poll_start_timer(phba);
4451 spin_unlock_irq(shost->host_lock);
4452 }
James Smart8f6d98d2006-08-01 07:34:00 -04004453
James Smart93996272008-08-24 21:50:30 -04004454 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4455 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04004456 /* Send board arrival event to upper layer */
4457 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4458 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4459 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04004460 sizeof(adapter_event),
4461 (char *) &adapter_event,
4462 LPFC_NL_VENDOR_ID);
4463 return;
4464}
4465
4466/**
4467 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4468 * @phba: pointer to lpfc hba data structure.
4469 *
4470 * This routine is invoked to set up the PCI device memory space for device
4471 * with SLI-3 interface spec.
4472 *
4473 * Return codes
4474 * 0 - sucessful
4475 * other values - error
4476 **/
4477static int
4478lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4479{
4480 struct pci_dev *pdev;
4481 unsigned long bar0map_len, bar2map_len;
4482 int i, hbq_count;
4483 void *ptr;
4484 int error = -ENODEV;
4485
4486 /* Obtain PCI device reference */
4487 if (!phba->pcidev)
4488 return error;
4489 else
4490 pdev = phba->pcidev;
4491
4492 /* Set the device DMA mask size */
4493 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
4494 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
4495 return error;
4496
4497 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4498 * required by each mapping.
4499 */
4500 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4501 bar0map_len = pci_resource_len(pdev, 0);
4502
4503 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4504 bar2map_len = pci_resource_len(pdev, 2);
4505
4506 /* Map HBA SLIM to a kernel virtual address. */
4507 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4508 if (!phba->slim_memmap_p) {
4509 dev_printk(KERN_ERR, &pdev->dev,
4510 "ioremap failed for SLIM memory.\n");
4511 goto out;
4512 }
4513
4514 /* Map HBA Control Registers to a kernel virtual address. */
4515 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4516 if (!phba->ctrl_regs_memmap_p) {
4517 dev_printk(KERN_ERR, &pdev->dev,
4518 "ioremap failed for HBA control registers.\n");
4519 goto out_iounmap_slim;
4520 }
4521
4522 /* Allocate memory for SLI-2 structures */
4523 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4524 SLI2_SLIM_SIZE,
4525 &phba->slim2p.phys,
4526 GFP_KERNEL);
4527 if (!phba->slim2p.virt)
4528 goto out_iounmap;
4529
4530 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4531 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4532 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4533 phba->IOCBs = (phba->slim2p.virt +
4534 offsetof(struct lpfc_sli2_slim, IOCBs));
4535
4536 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4537 lpfc_sli_hbq_size(),
4538 &phba->hbqslimp.phys,
4539 GFP_KERNEL);
4540 if (!phba->hbqslimp.virt)
4541 goto out_free_slim;
4542
4543 hbq_count = lpfc_sli_hbq_count();
4544 ptr = phba->hbqslimp.virt;
4545 for (i = 0; i < hbq_count; ++i) {
4546 phba->hbqs[i].hbq_virt = ptr;
4547 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4548 ptr += (lpfc_hbq_defs[i]->entry_count *
4549 sizeof(struct lpfc_hbq_entry));
4550 }
4551 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4552 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4553
4554 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4555
4556 INIT_LIST_HEAD(&phba->rb_pend_list);
4557
4558 phba->MBslimaddr = phba->slim_memmap_p;
4559 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4560 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4561 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4562 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04004563
dea31012005-04-17 16:05:31 -05004564 return 0;
4565
dea31012005-04-17 16:05:31 -05004566out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04004567 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4568 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05004569out_iounmap:
4570 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004571out_iounmap_slim:
dea31012005-04-17 16:05:31 -05004572 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05004573out:
4574 return error;
4575}
4576
James Smarte59058c2008-08-24 21:49:00 -04004577/**
James Smart3772a992009-05-22 14:50:54 -04004578 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4579 * @phba: pointer to lpfc hba data structure.
4580 *
4581 * This routine is invoked to unset the PCI device memory space for device
4582 * with SLI-3 interface spec.
4583 **/
4584static void
4585lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4586{
4587 struct pci_dev *pdev;
4588
4589 /* Obtain PCI device reference */
4590 if (!phba->pcidev)
4591 return;
4592 else
4593 pdev = phba->pcidev;
4594
4595 /* Free coherent DMA memory allocated */
4596 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4597 phba->hbqslimp.virt, phba->hbqslimp.phys);
4598 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4599 phba->slim2p.virt, phba->slim2p.phys);
4600
4601 /* I/O memory unmap */
4602 iounmap(phba->ctrl_regs_memmap_p);
4603 iounmap(phba->slim_memmap_p);
4604
4605 return;
4606}
4607
4608/**
James Smartda0436e2009-05-22 14:51:39 -04004609 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4610 * @phba: pointer to lpfc hba data structure.
4611 *
4612 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4613 * done and check status.
4614 *
4615 * Return 0 if successful, otherwise -ENODEV.
4616 **/
4617int
4618lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4619{
4620 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg, scratchpad;
4621 uint32_t onlnreg0, onlnreg1;
4622 int i, port_error = -ENODEV;
4623
4624 if (!phba->sli4_hba.STAregaddr)
4625 return -ENODEV;
4626
James Smartda0436e2009-05-22 14:51:39 -04004627 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4628 for (i = 0; i < 3000; i++) {
4629 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4630 /* Encounter fatal POST error, break out */
4631 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4632 port_error = -ENODEV;
4633 break;
4634 }
4635 if (LPFC_POST_STAGE_ARMFW_READY ==
4636 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4637 port_error = 0;
4638 break;
4639 }
4640 msleep(10);
4641 }
4642
4643 if (port_error)
4644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4645 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4646 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4647 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4648 bf_get(lpfc_hst_state_perr, &sta_reg),
4649 bf_get(lpfc_hst_state_sfi, &sta_reg),
4650 bf_get(lpfc_hst_state_nip, &sta_reg),
4651 bf_get(lpfc_hst_state_ipc, &sta_reg),
4652 bf_get(lpfc_hst_state_xrom, &sta_reg),
4653 bf_get(lpfc_hst_state_dl, &sta_reg),
4654 bf_get(lpfc_hst_state_port_status, &sta_reg));
4655
4656 /* Log device information */
4657 scratchpad.word0 = readl(phba->sli4_hba.SCRATCHPADregaddr);
4658 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4659 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4660 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4661 bf_get(lpfc_scratchpad_chiptype, &scratchpad),
4662 bf_get(lpfc_scratchpad_slirev, &scratchpad),
4663 bf_get(lpfc_scratchpad_featurelevel1, &scratchpad),
4664 bf_get(lpfc_scratchpad_featurelevel2, &scratchpad));
4665
James Smart8fa38512009-07-19 10:01:03 -04004666 /* With uncoverable error, log the error message and return error */
4667 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
4668 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
4669 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
4670 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4671 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4672 if (uerrlo_reg.word0 || uerrhi_reg.word0) {
4673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4674 "1422 HBA Unrecoverable error: "
4675 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4676 "online0_reg=0x%x, online1_reg=0x%x\n",
4677 uerrlo_reg.word0, uerrhi_reg.word0,
4678 onlnreg0, onlnreg1);
4679 }
4680 return -ENODEV;
4681 }
4682
James Smartda0436e2009-05-22 14:51:39 -04004683 return port_error;
4684}
4685
4686/**
4687 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4688 * @phba: pointer to lpfc hba data structure.
4689 *
4690 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4691 * memory map.
4692 **/
4693static void
4694lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4695{
4696 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4697 LPFC_UERR_STATUS_LO;
4698 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4699 LPFC_UERR_STATUS_HI;
4700 phba->sli4_hba.ONLINE0regaddr = phba->sli4_hba.conf_regs_memmap_p +
4701 LPFC_ONLINE0;
4702 phba->sli4_hba.ONLINE1regaddr = phba->sli4_hba.conf_regs_memmap_p +
4703 LPFC_ONLINE1;
4704 phba->sli4_hba.SCRATCHPADregaddr = phba->sli4_hba.conf_regs_memmap_p +
4705 LPFC_SCRATCHPAD;
4706}
4707
4708/**
4709 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4710 * @phba: pointer to lpfc hba data structure.
4711 *
4712 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4713 * memory map.
4714 **/
4715static void
4716lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4717{
4718
4719 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4720 LPFC_HST_STATE;
4721 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4722 LPFC_HST_ISR0;
4723 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4724 LPFC_HST_IMR0;
4725 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4726 LPFC_HST_ISCR0;
4727 return;
4728}
4729
4730/**
4731 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
4732 * @phba: pointer to lpfc hba data structure.
4733 * @vf: virtual function number
4734 *
4735 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4736 * based on the given viftual function number, @vf.
4737 *
4738 * Return 0 if successful, otherwise -ENODEV.
4739 **/
4740static int
4741lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
4742{
4743 if (vf > LPFC_VIR_FUNC_MAX)
4744 return -ENODEV;
4745
4746 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4747 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4748 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4749 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4750 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4751 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
4752 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4753 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
4754 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4755 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
4756 return 0;
4757}
4758
4759/**
4760 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
4761 * @phba: pointer to lpfc hba data structure.
4762 *
4763 * This routine is invoked to create the bootstrap mailbox
4764 * region consistent with the SLI-4 interface spec. This
4765 * routine allocates all memory necessary to communicate
4766 * mailbox commands to the port and sets up all alignment
4767 * needs. No locks are expected to be held when calling
4768 * this routine.
4769 *
4770 * Return codes
4771 * 0 - sucessful
4772 * ENOMEM - could not allocated memory.
4773 **/
4774static int
4775lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
4776{
4777 uint32_t bmbx_size;
4778 struct lpfc_dmabuf *dmabuf;
4779 struct dma_address *dma_address;
4780 uint32_t pa_addr;
4781 uint64_t phys_addr;
4782
4783 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4784 if (!dmabuf)
4785 return -ENOMEM;
4786
4787 /*
4788 * The bootstrap mailbox region is comprised of 2 parts
4789 * plus an alignment restriction of 16 bytes.
4790 */
4791 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
4792 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4793 bmbx_size,
4794 &dmabuf->phys,
4795 GFP_KERNEL);
4796 if (!dmabuf->virt) {
4797 kfree(dmabuf);
4798 return -ENOMEM;
4799 }
4800 memset(dmabuf->virt, 0, bmbx_size);
4801
4802 /*
4803 * Initialize the bootstrap mailbox pointers now so that the register
4804 * operations are simple later. The mailbox dma address is required
4805 * to be 16-byte aligned. Also align the virtual memory as each
4806 * maibox is copied into the bmbx mailbox region before issuing the
4807 * command to the port.
4808 */
4809 phba->sli4_hba.bmbx.dmabuf = dmabuf;
4810 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
4811
4812 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
4813 LPFC_ALIGN_16_BYTE);
4814 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
4815 LPFC_ALIGN_16_BYTE);
4816
4817 /*
4818 * Set the high and low physical addresses now. The SLI4 alignment
4819 * requirement is 16 bytes and the mailbox is posted to the port
4820 * as two 30-bit addresses. The other data is a bit marking whether
4821 * the 30-bit address is the high or low address.
4822 * Upcast bmbx aphys to 64bits so shift instruction compiles
4823 * clean on 32 bit machines.
4824 */
4825 dma_address = &phba->sli4_hba.bmbx.dma_address;
4826 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
4827 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
4828 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
4829 LPFC_BMBX_BIT1_ADDR_HI);
4830
4831 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
4832 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
4833 LPFC_BMBX_BIT1_ADDR_LO);
4834 return 0;
4835}
4836
4837/**
4838 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
4839 * @phba: pointer to lpfc hba data structure.
4840 *
4841 * This routine is invoked to teardown the bootstrap mailbox
4842 * region and release all host resources. This routine requires
4843 * the caller to ensure all mailbox commands recovered, no
4844 * additional mailbox comands are sent, and interrupts are disabled
4845 * before calling this routine.
4846 *
4847 **/
4848static void
4849lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
4850{
4851 dma_free_coherent(&phba->pcidev->dev,
4852 phba->sli4_hba.bmbx.bmbx_size,
4853 phba->sli4_hba.bmbx.dmabuf->virt,
4854 phba->sli4_hba.bmbx.dmabuf->phys);
4855
4856 kfree(phba->sli4_hba.bmbx.dmabuf);
4857 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
4858}
4859
4860/**
4861 * lpfc_sli4_read_config - Get the config parameters.
4862 * @phba: pointer to lpfc hba data structure.
4863 *
4864 * This routine is invoked to read the configuration parameters from the HBA.
4865 * The configuration parameters are used to set the base and maximum values
4866 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
4867 * allocation for the port.
4868 *
4869 * Return codes
4870 * 0 - sucessful
4871 * ENOMEM - No availble memory
4872 * EIO - The mailbox failed to complete successfully.
4873 **/
4874static int
4875lpfc_sli4_read_config(struct lpfc_hba *phba)
4876{
4877 LPFC_MBOXQ_t *pmb;
4878 struct lpfc_mbx_read_config *rd_config;
4879 uint32_t rc = 0;
4880
4881 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4882 if (!pmb) {
4883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4884 "2011 Unable to allocate memory for issuing "
4885 "SLI_CONFIG_SPECIAL mailbox command\n");
4886 return -ENOMEM;
4887 }
4888
4889 lpfc_read_config(phba, pmb);
4890
4891 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4892 if (rc != MBX_SUCCESS) {
4893 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4894 "2012 Mailbox failed , mbxCmd x%x "
4895 "READ_CONFIG, mbxStatus x%x\n",
4896 bf_get(lpfc_mqe_command, &pmb->u.mqe),
4897 bf_get(lpfc_mqe_status, &pmb->u.mqe));
4898 rc = -EIO;
4899 } else {
4900 rd_config = &pmb->u.mqe.un.rd_config;
4901 phba->sli4_hba.max_cfg_param.max_xri =
4902 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
4903 phba->sli4_hba.max_cfg_param.xri_base =
4904 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
4905 phba->sli4_hba.max_cfg_param.max_vpi =
4906 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
4907 phba->sli4_hba.max_cfg_param.vpi_base =
4908 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
4909 phba->sli4_hba.max_cfg_param.max_rpi =
4910 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
4911 phba->sli4_hba.max_cfg_param.rpi_base =
4912 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
4913 phba->sli4_hba.max_cfg_param.max_vfi =
4914 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
4915 phba->sli4_hba.max_cfg_param.vfi_base =
4916 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
4917 phba->sli4_hba.max_cfg_param.max_fcfi =
4918 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
4919 phba->sli4_hba.max_cfg_param.fcfi_base =
4920 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
4921 phba->sli4_hba.max_cfg_param.max_eq =
4922 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
4923 phba->sli4_hba.max_cfg_param.max_rq =
4924 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
4925 phba->sli4_hba.max_cfg_param.max_wq =
4926 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
4927 phba->sli4_hba.max_cfg_param.max_cq =
4928 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
4929 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
4930 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
4931 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
4932 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
4933 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05004934 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
4935 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04004936 phba->max_vports = phba->max_vpi;
4937 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4938 "2003 cfg params XRI(B:%d M:%d), "
4939 "VPI(B:%d M:%d) "
4940 "VFI(B:%d M:%d) "
4941 "RPI(B:%d M:%d) "
4942 "FCFI(B:%d M:%d)\n",
4943 phba->sli4_hba.max_cfg_param.xri_base,
4944 phba->sli4_hba.max_cfg_param.max_xri,
4945 phba->sli4_hba.max_cfg_param.vpi_base,
4946 phba->sli4_hba.max_cfg_param.max_vpi,
4947 phba->sli4_hba.max_cfg_param.vfi_base,
4948 phba->sli4_hba.max_cfg_param.max_vfi,
4949 phba->sli4_hba.max_cfg_param.rpi_base,
4950 phba->sli4_hba.max_cfg_param.max_rpi,
4951 phba->sli4_hba.max_cfg_param.fcfi_base,
4952 phba->sli4_hba.max_cfg_param.max_fcfi);
4953 }
4954 mempool_free(pmb, phba->mbox_mem_pool);
4955
4956 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
4957 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
4958 phba->cfg_hba_queue_depth =
4959 phba->sli4_hba.max_cfg_param.max_xri;
4960 return rc;
4961}
4962
4963/**
4964 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
4965 * @phba: pointer to lpfc hba data structure.
4966 *
4967 * This routine is invoked to setup the host-side endian order to the
4968 * HBA consistent with the SLI-4 interface spec.
4969 *
4970 * Return codes
4971 * 0 - sucessful
4972 * ENOMEM - No availble memory
4973 * EIO - The mailbox failed to complete successfully.
4974 **/
4975static int
4976lpfc_setup_endian_order(struct lpfc_hba *phba)
4977{
4978 LPFC_MBOXQ_t *mboxq;
4979 uint32_t rc = 0;
4980 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
4981 HOST_ENDIAN_HIGH_WORD1};
4982
4983 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4984 if (!mboxq) {
4985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4986 "0492 Unable to allocate memory for issuing "
4987 "SLI_CONFIG_SPECIAL mailbox command\n");
4988 return -ENOMEM;
4989 }
4990
4991 /*
4992 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
4993 * words to contain special data values and no other data.
4994 */
4995 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
4996 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
4997 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4998 if (rc != MBX_SUCCESS) {
4999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5000 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5001 "status x%x\n",
5002 rc);
5003 rc = -EIO;
5004 }
5005
5006 mempool_free(mboxq, phba->mbox_mem_pool);
5007 return rc;
5008}
5009
5010/**
5011 * lpfc_sli4_queue_create - Create all the SLI4 queues
5012 * @phba: pointer to lpfc hba data structure.
5013 *
5014 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5015 * operation. For each SLI4 queue type, the parameters such as queue entry
5016 * count (queue depth) shall be taken from the module parameter. For now,
5017 * we just use some constant number as place holder.
5018 *
5019 * Return codes
5020 * 0 - sucessful
5021 * ENOMEM - No availble memory
5022 * EIO - The mailbox failed to complete successfully.
5023 **/
5024static int
5025lpfc_sli4_queue_create(struct lpfc_hba *phba)
5026{
5027 struct lpfc_queue *qdesc;
5028 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5029 int cfg_fcp_wq_count;
5030 int cfg_fcp_eq_count;
5031
5032 /*
5033 * Sanity check for confiugred queue parameters against the run-time
5034 * device parameters
5035 */
5036
5037 /* Sanity check on FCP fast-path WQ parameters */
5038 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5039 if (cfg_fcp_wq_count >
5040 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5041 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5042 LPFC_SP_WQN_DEF;
5043 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5044 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5045 "2581 Not enough WQs (%d) from "
5046 "the pci function for supporting "
5047 "FCP WQs (%d)\n",
5048 phba->sli4_hba.max_cfg_param.max_wq,
5049 phba->cfg_fcp_wq_count);
5050 goto out_error;
5051 }
5052 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5053 "2582 Not enough WQs (%d) from the pci "
5054 "function for supporting the requested "
5055 "FCP WQs (%d), the actual FCP WQs can "
5056 "be supported: %d\n",
5057 phba->sli4_hba.max_cfg_param.max_wq,
5058 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5059 }
5060 /* The actual number of FCP work queues adopted */
5061 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5062
5063 /* Sanity check on FCP fast-path EQ parameters */
5064 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5065 if (cfg_fcp_eq_count >
5066 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5067 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5068 LPFC_SP_EQN_DEF;
5069 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5071 "2574 Not enough EQs (%d) from the "
5072 "pci function for supporting FCP "
5073 "EQs (%d)\n",
5074 phba->sli4_hba.max_cfg_param.max_eq,
5075 phba->cfg_fcp_eq_count);
5076 goto out_error;
5077 }
5078 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5079 "2575 Not enough EQs (%d) from the pci "
5080 "function for supporting the requested "
5081 "FCP EQs (%d), the actual FCP EQs can "
5082 "be supported: %d\n",
5083 phba->sli4_hba.max_cfg_param.max_eq,
5084 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5085 }
5086 /* It does not make sense to have more EQs than WQs */
5087 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5088 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005089 "2593 The FCP EQ count(%d) cannot be greater "
5090 "than the FCP WQ count(%d), limiting the "
5091 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005092 phba->cfg_fcp_wq_count,
5093 phba->cfg_fcp_wq_count);
5094 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5095 }
5096 /* The actual number of FCP event queues adopted */
5097 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5098 /* The overall number of event queues used */
5099 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5100
5101 /*
5102 * Create Event Queues (EQs)
5103 */
5104
5105 /* Get EQ depth from module parameter, fake the default for now */
5106 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5107 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5108
5109 /* Create slow path event queue */
5110 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5111 phba->sli4_hba.eq_ecount);
5112 if (!qdesc) {
5113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5114 "0496 Failed allocate slow-path EQ\n");
5115 goto out_error;
5116 }
5117 phba->sli4_hba.sp_eq = qdesc;
5118
5119 /* Create fast-path FCP Event Queue(s) */
5120 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5121 phba->cfg_fcp_eq_count), GFP_KERNEL);
5122 if (!phba->sli4_hba.fp_eq) {
5123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5124 "2576 Failed allocate memory for fast-path "
5125 "EQ record array\n");
5126 goto out_free_sp_eq;
5127 }
5128 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5129 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5130 phba->sli4_hba.eq_ecount);
5131 if (!qdesc) {
5132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5133 "0497 Failed allocate fast-path EQ\n");
5134 goto out_free_fp_eq;
5135 }
5136 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5137 }
5138
5139 /*
5140 * Create Complete Queues (CQs)
5141 */
5142
5143 /* Get CQ depth from module parameter, fake the default for now */
5144 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5145 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5146
5147 /* Create slow-path Mailbox Command Complete Queue */
5148 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5149 phba->sli4_hba.cq_ecount);
5150 if (!qdesc) {
5151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5152 "0500 Failed allocate slow-path mailbox CQ\n");
5153 goto out_free_fp_eq;
5154 }
5155 phba->sli4_hba.mbx_cq = qdesc;
5156
5157 /* Create slow-path ELS Complete Queue */
5158 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5159 phba->sli4_hba.cq_ecount);
5160 if (!qdesc) {
5161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5162 "0501 Failed allocate slow-path ELS CQ\n");
5163 goto out_free_mbx_cq;
5164 }
5165 phba->sli4_hba.els_cq = qdesc;
5166
James Smartda0436e2009-05-22 14:51:39 -04005167
5168 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5169 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5170 phba->cfg_fcp_eq_count), GFP_KERNEL);
5171 if (!phba->sli4_hba.fcp_cq) {
5172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5173 "2577 Failed allocate memory for fast-path "
5174 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005175 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005176 }
5177 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5178 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5179 phba->sli4_hba.cq_ecount);
5180 if (!qdesc) {
5181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5182 "0499 Failed allocate fast-path FCP "
5183 "CQ (%d)\n", fcp_cqidx);
5184 goto out_free_fcp_cq;
5185 }
5186 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5187 }
5188
5189 /* Create Mailbox Command Queue */
5190 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5191 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5192
5193 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5194 phba->sli4_hba.mq_ecount);
5195 if (!qdesc) {
5196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5197 "0505 Failed allocate slow-path MQ\n");
5198 goto out_free_fcp_cq;
5199 }
5200 phba->sli4_hba.mbx_wq = qdesc;
5201
5202 /*
5203 * Create all the Work Queues (WQs)
5204 */
5205 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5206 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5207
5208 /* Create slow-path ELS Work Queue */
5209 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5210 phba->sli4_hba.wq_ecount);
5211 if (!qdesc) {
5212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5213 "0504 Failed allocate slow-path ELS WQ\n");
5214 goto out_free_mbx_wq;
5215 }
5216 phba->sli4_hba.els_wq = qdesc;
5217
5218 /* Create fast-path FCP Work Queue(s) */
5219 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5220 phba->cfg_fcp_wq_count), GFP_KERNEL);
5221 if (!phba->sli4_hba.fcp_wq) {
5222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5223 "2578 Failed allocate memory for fast-path "
5224 "WQ record array\n");
5225 goto out_free_els_wq;
5226 }
5227 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5228 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5229 phba->sli4_hba.wq_ecount);
5230 if (!qdesc) {
5231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5232 "0503 Failed allocate fast-path FCP "
5233 "WQ (%d)\n", fcp_wqidx);
5234 goto out_free_fcp_wq;
5235 }
5236 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5237 }
5238
5239 /*
5240 * Create Receive Queue (RQ)
5241 */
5242 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5243 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5244
5245 /* Create Receive Queue for header */
5246 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5247 phba->sli4_hba.rq_ecount);
5248 if (!qdesc) {
5249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5250 "0506 Failed allocate receive HRQ\n");
5251 goto out_free_fcp_wq;
5252 }
5253 phba->sli4_hba.hdr_rq = qdesc;
5254
5255 /* Create Receive Queue for data */
5256 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5257 phba->sli4_hba.rq_ecount);
5258 if (!qdesc) {
5259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5260 "0507 Failed allocate receive DRQ\n");
5261 goto out_free_hdr_rq;
5262 }
5263 phba->sli4_hba.dat_rq = qdesc;
5264
5265 return 0;
5266
5267out_free_hdr_rq:
5268 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5269 phba->sli4_hba.hdr_rq = NULL;
5270out_free_fcp_wq:
5271 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5272 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5273 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5274 }
5275 kfree(phba->sli4_hba.fcp_wq);
5276out_free_els_wq:
5277 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5278 phba->sli4_hba.els_wq = NULL;
5279out_free_mbx_wq:
5280 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5281 phba->sli4_hba.mbx_wq = NULL;
5282out_free_fcp_cq:
5283 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5284 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5285 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5286 }
5287 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005288out_free_els_cq:
5289 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5290 phba->sli4_hba.els_cq = NULL;
5291out_free_mbx_cq:
5292 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5293 phba->sli4_hba.mbx_cq = NULL;
5294out_free_fp_eq:
5295 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5296 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5297 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5298 }
5299 kfree(phba->sli4_hba.fp_eq);
5300out_free_sp_eq:
5301 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5302 phba->sli4_hba.sp_eq = NULL;
5303out_error:
5304 return -ENOMEM;
5305}
5306
5307/**
5308 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5309 * @phba: pointer to lpfc hba data structure.
5310 *
5311 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5312 * operation.
5313 *
5314 * Return codes
5315 * 0 - sucessful
5316 * ENOMEM - No availble memory
5317 * EIO - The mailbox failed to complete successfully.
5318 **/
5319static void
5320lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5321{
5322 int fcp_qidx;
5323
5324 /* Release mailbox command work queue */
5325 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5326 phba->sli4_hba.mbx_wq = NULL;
5327
5328 /* Release ELS work queue */
5329 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5330 phba->sli4_hba.els_wq = NULL;
5331
5332 /* Release FCP work queue */
5333 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5334 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5335 kfree(phba->sli4_hba.fcp_wq);
5336 phba->sli4_hba.fcp_wq = NULL;
5337
5338 /* Release unsolicited receive queue */
5339 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5340 phba->sli4_hba.hdr_rq = NULL;
5341 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5342 phba->sli4_hba.dat_rq = NULL;
5343
James Smartda0436e2009-05-22 14:51:39 -04005344 /* Release ELS complete queue */
5345 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5346 phba->sli4_hba.els_cq = NULL;
5347
5348 /* Release mailbox command complete queue */
5349 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5350 phba->sli4_hba.mbx_cq = NULL;
5351
5352 /* Release FCP response complete queue */
5353 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5354 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5355 kfree(phba->sli4_hba.fcp_cq);
5356 phba->sli4_hba.fcp_cq = NULL;
5357
5358 /* Release fast-path event queue */
5359 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5360 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5361 kfree(phba->sli4_hba.fp_eq);
5362 phba->sli4_hba.fp_eq = NULL;
5363
5364 /* Release slow-path event queue */
5365 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5366 phba->sli4_hba.sp_eq = NULL;
5367
5368 return;
5369}
5370
5371/**
5372 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5373 * @phba: pointer to lpfc hba data structure.
5374 *
5375 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5376 * operation.
5377 *
5378 * Return codes
5379 * 0 - sucessful
5380 * ENOMEM - No availble memory
5381 * EIO - The mailbox failed to complete successfully.
5382 **/
5383int
5384lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5385{
5386 int rc = -ENOMEM;
5387 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5388 int fcp_cq_index = 0;
5389
5390 /*
5391 * Set up Event Queues (EQs)
5392 */
5393
5394 /* Set up slow-path event queue */
5395 if (!phba->sli4_hba.sp_eq) {
5396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5397 "0520 Slow-path EQ not allocated\n");
5398 goto out_error;
5399 }
5400 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5401 LPFC_SP_DEF_IMAX);
5402 if (rc) {
5403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5404 "0521 Failed setup of slow-path EQ: "
5405 "rc = 0x%x\n", rc);
5406 goto out_error;
5407 }
5408 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5409 "2583 Slow-path EQ setup: queue-id=%d\n",
5410 phba->sli4_hba.sp_eq->queue_id);
5411
5412 /* Set up fast-path event queue */
5413 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5414 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5416 "0522 Fast-path EQ (%d) not "
5417 "allocated\n", fcp_eqidx);
5418 goto out_destroy_fp_eq;
5419 }
5420 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5421 phba->cfg_fcp_imax);
5422 if (rc) {
5423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5424 "0523 Failed setup of fast-path EQ "
5425 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5426 goto out_destroy_fp_eq;
5427 }
5428 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5429 "2584 Fast-path EQ setup: "
5430 "queue[%d]-id=%d\n", fcp_eqidx,
5431 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5432 }
5433
5434 /*
5435 * Set up Complete Queues (CQs)
5436 */
5437
5438 /* Set up slow-path MBOX Complete Queue as the first CQ */
5439 if (!phba->sli4_hba.mbx_cq) {
5440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5441 "0528 Mailbox CQ not allocated\n");
5442 goto out_destroy_fp_eq;
5443 }
5444 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5445 LPFC_MCQ, LPFC_MBOX);
5446 if (rc) {
5447 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5448 "0529 Failed setup of slow-path mailbox CQ: "
5449 "rc = 0x%x\n", rc);
5450 goto out_destroy_fp_eq;
5451 }
5452 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5453 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5454 phba->sli4_hba.mbx_cq->queue_id,
5455 phba->sli4_hba.sp_eq->queue_id);
5456
5457 /* Set up slow-path ELS Complete Queue */
5458 if (!phba->sli4_hba.els_cq) {
5459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5460 "0530 ELS CQ not allocated\n");
5461 goto out_destroy_mbx_cq;
5462 }
5463 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5464 LPFC_WCQ, LPFC_ELS);
5465 if (rc) {
5466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5467 "0531 Failed setup of slow-path ELS CQ: "
5468 "rc = 0x%x\n", rc);
5469 goto out_destroy_mbx_cq;
5470 }
5471 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5472 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5473 phba->sli4_hba.els_cq->queue_id,
5474 phba->sli4_hba.sp_eq->queue_id);
5475
James Smartda0436e2009-05-22 14:51:39 -04005476 /* Set up fast-path FCP Response Complete Queue */
5477 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5478 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5480 "0526 Fast-path FCP CQ (%d) not "
5481 "allocated\n", fcp_cqidx);
5482 goto out_destroy_fcp_cq;
5483 }
5484 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5485 phba->sli4_hba.fp_eq[fcp_cqidx],
5486 LPFC_WCQ, LPFC_FCP);
5487 if (rc) {
5488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5489 "0527 Failed setup of fast-path FCP "
5490 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5491 goto out_destroy_fcp_cq;
5492 }
5493 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5494 "2588 FCP CQ setup: cq[%d]-id=%d, "
5495 "parent eq[%d]-id=%d\n",
5496 fcp_cqidx,
5497 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5498 fcp_cqidx,
5499 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5500 }
5501
5502 /*
5503 * Set up all the Work Queues (WQs)
5504 */
5505
5506 /* Set up Mailbox Command Queue */
5507 if (!phba->sli4_hba.mbx_wq) {
5508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5509 "0538 Slow-path MQ not allocated\n");
5510 goto out_destroy_fcp_cq;
5511 }
5512 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5513 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5514 if (rc) {
5515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5516 "0539 Failed setup of slow-path MQ: "
5517 "rc = 0x%x\n", rc);
5518 goto out_destroy_fcp_cq;
5519 }
5520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5521 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5522 phba->sli4_hba.mbx_wq->queue_id,
5523 phba->sli4_hba.mbx_cq->queue_id);
5524
5525 /* Set up slow-path ELS Work Queue */
5526 if (!phba->sli4_hba.els_wq) {
5527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5528 "0536 Slow-path ELS WQ not allocated\n");
5529 goto out_destroy_mbx_wq;
5530 }
5531 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5532 phba->sli4_hba.els_cq, LPFC_ELS);
5533 if (rc) {
5534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5535 "0537 Failed setup of slow-path ELS WQ: "
5536 "rc = 0x%x\n", rc);
5537 goto out_destroy_mbx_wq;
5538 }
5539 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5540 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5541 phba->sli4_hba.els_wq->queue_id,
5542 phba->sli4_hba.els_cq->queue_id);
5543
5544 /* Set up fast-path FCP Work Queue */
5545 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5546 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5548 "0534 Fast-path FCP WQ (%d) not "
5549 "allocated\n", fcp_wqidx);
5550 goto out_destroy_fcp_wq;
5551 }
5552 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5553 phba->sli4_hba.fcp_cq[fcp_cq_index],
5554 LPFC_FCP);
5555 if (rc) {
5556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5557 "0535 Failed setup of fast-path FCP "
5558 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5559 goto out_destroy_fcp_wq;
5560 }
5561 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5562 "2591 FCP WQ setup: wq[%d]-id=%d, "
5563 "parent cq[%d]-id=%d\n",
5564 fcp_wqidx,
5565 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5566 fcp_cq_index,
5567 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5568 /* Round robin FCP Work Queue's Completion Queue assignment */
5569 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5570 }
5571
5572 /*
5573 * Create Receive Queue (RQ)
5574 */
5575 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5577 "0540 Receive Queue not allocated\n");
5578 goto out_destroy_fcp_wq;
5579 }
5580 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04005581 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04005582 if (rc) {
5583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5584 "0541 Failed setup of Receive Queue: "
5585 "rc = 0x%x\n", rc);
5586 goto out_destroy_fcp_wq;
5587 }
5588 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5589 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5590 "parent cq-id=%d\n",
5591 phba->sli4_hba.hdr_rq->queue_id,
5592 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04005593 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04005594 return 0;
5595
5596out_destroy_fcp_wq:
5597 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5598 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5599 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5600out_destroy_mbx_wq:
5601 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5602out_destroy_fcp_cq:
5603 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5604 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04005605 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5606out_destroy_mbx_cq:
5607 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5608out_destroy_fp_eq:
5609 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5610 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5611 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5612out_error:
5613 return rc;
5614}
5615
5616/**
5617 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5618 * @phba: pointer to lpfc hba data structure.
5619 *
5620 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5621 * operation.
5622 *
5623 * Return codes
5624 * 0 - sucessful
5625 * ENOMEM - No availble memory
5626 * EIO - The mailbox failed to complete successfully.
5627 **/
5628void
5629lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5630{
5631 int fcp_qidx;
5632
5633 /* Unset mailbox command work queue */
5634 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5635 /* Unset ELS work queue */
5636 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5637 /* Unset unsolicited receive queue */
5638 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5639 /* Unset FCP work queue */
5640 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5641 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5642 /* Unset mailbox command complete queue */
5643 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5644 /* Unset ELS complete queue */
5645 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04005646 /* Unset FCP response complete queue */
5647 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5648 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5649 /* Unset fast-path event queue */
5650 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5651 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5652 /* Unset slow-path event queue */
5653 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5654}
5655
5656/**
5657 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5658 * @phba: pointer to lpfc hba data structure.
5659 *
5660 * This routine is invoked to allocate and set up a pool of completion queue
5661 * events. The body of the completion queue event is a completion queue entry
5662 * CQE. For now, this pool is used for the interrupt service routine to queue
5663 * the following HBA completion queue events for the worker thread to process:
5664 * - Mailbox asynchronous events
5665 * - Receive queue completion unsolicited events
5666 * Later, this can be used for all the slow-path events.
5667 *
5668 * Return codes
5669 * 0 - sucessful
5670 * -ENOMEM - No availble memory
5671 **/
5672static int
5673lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5674{
5675 struct lpfc_cq_event *cq_event;
5676 int i;
5677
5678 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5679 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5680 if (!cq_event)
5681 goto out_pool_create_fail;
5682 list_add_tail(&cq_event->list,
5683 &phba->sli4_hba.sp_cqe_event_pool);
5684 }
5685 return 0;
5686
5687out_pool_create_fail:
5688 lpfc_sli4_cq_event_pool_destroy(phba);
5689 return -ENOMEM;
5690}
5691
5692/**
5693 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5694 * @phba: pointer to lpfc hba data structure.
5695 *
5696 * This routine is invoked to free the pool of completion queue events at
5697 * driver unload time. Note that, it is the responsibility of the driver
5698 * cleanup routine to free all the outstanding completion-queue events
5699 * allocated from this pool back into the pool before invoking this routine
5700 * to destroy the pool.
5701 **/
5702static void
5703lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5704{
5705 struct lpfc_cq_event *cq_event, *next_cq_event;
5706
5707 list_for_each_entry_safe(cq_event, next_cq_event,
5708 &phba->sli4_hba.sp_cqe_event_pool, list) {
5709 list_del(&cq_event->list);
5710 kfree(cq_event);
5711 }
5712}
5713
5714/**
5715 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5716 * @phba: pointer to lpfc hba data structure.
5717 *
5718 * This routine is the lock free version of the API invoked to allocate a
5719 * completion-queue event from the free pool.
5720 *
5721 * Return: Pointer to the newly allocated completion-queue event if successful
5722 * NULL otherwise.
5723 **/
5724struct lpfc_cq_event *
5725__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5726{
5727 struct lpfc_cq_event *cq_event = NULL;
5728
5729 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5730 struct lpfc_cq_event, list);
5731 return cq_event;
5732}
5733
5734/**
5735 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5736 * @phba: pointer to lpfc hba data structure.
5737 *
5738 * This routine is the lock version of the API invoked to allocate a
5739 * completion-queue event from the free pool.
5740 *
5741 * Return: Pointer to the newly allocated completion-queue event if successful
5742 * NULL otherwise.
5743 **/
5744struct lpfc_cq_event *
5745lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5746{
5747 struct lpfc_cq_event *cq_event;
5748 unsigned long iflags;
5749
5750 spin_lock_irqsave(&phba->hbalock, iflags);
5751 cq_event = __lpfc_sli4_cq_event_alloc(phba);
5752 spin_unlock_irqrestore(&phba->hbalock, iflags);
5753 return cq_event;
5754}
5755
5756/**
5757 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5758 * @phba: pointer to lpfc hba data structure.
5759 * @cq_event: pointer to the completion queue event to be freed.
5760 *
5761 * This routine is the lock free version of the API invoked to release a
5762 * completion-queue event back into the free pool.
5763 **/
5764void
5765__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5766 struct lpfc_cq_event *cq_event)
5767{
5768 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
5769}
5770
5771/**
5772 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5773 * @phba: pointer to lpfc hba data structure.
5774 * @cq_event: pointer to the completion queue event to be freed.
5775 *
5776 * This routine is the lock version of the API invoked to release a
5777 * completion-queue event back into the free pool.
5778 **/
5779void
5780lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5781 struct lpfc_cq_event *cq_event)
5782{
5783 unsigned long iflags;
5784 spin_lock_irqsave(&phba->hbalock, iflags);
5785 __lpfc_sli4_cq_event_release(phba, cq_event);
5786 spin_unlock_irqrestore(&phba->hbalock, iflags);
5787}
5788
5789/**
5790 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
5791 * @phba: pointer to lpfc hba data structure.
5792 *
5793 * This routine is to free all the pending completion-queue events to the
5794 * back into the free pool for device reset.
5795 **/
5796static void
5797lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
5798{
5799 LIST_HEAD(cqelist);
5800 struct lpfc_cq_event *cqe;
5801 unsigned long iflags;
5802
5803 /* Retrieve all the pending WCQEs from pending WCQE lists */
5804 spin_lock_irqsave(&phba->hbalock, iflags);
5805 /* Pending FCP XRI abort events */
5806 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
5807 &cqelist);
5808 /* Pending ELS XRI abort events */
5809 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
5810 &cqelist);
5811 /* Pending asynnc events */
5812 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
5813 &cqelist);
5814 spin_unlock_irqrestore(&phba->hbalock, iflags);
5815
5816 while (!list_empty(&cqelist)) {
5817 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
5818 lpfc_sli4_cq_event_release(phba, cqe);
5819 }
5820}
5821
5822/**
5823 * lpfc_pci_function_reset - Reset pci function.
5824 * @phba: pointer to lpfc hba data structure.
5825 *
5826 * This routine is invoked to request a PCI function reset. It will destroys
5827 * all resources assigned to the PCI function which originates this request.
5828 *
5829 * Return codes
5830 * 0 - sucessful
5831 * ENOMEM - No availble memory
5832 * EIO - The mailbox failed to complete successfully.
5833 **/
5834int
5835lpfc_pci_function_reset(struct lpfc_hba *phba)
5836{
5837 LPFC_MBOXQ_t *mboxq;
5838 uint32_t rc = 0;
5839 uint32_t shdr_status, shdr_add_status;
5840 union lpfc_sli4_cfg_shdr *shdr;
5841
5842 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5843 if (!mboxq) {
5844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5845 "0494 Unable to allocate memory for issuing "
5846 "SLI_FUNCTION_RESET mailbox command\n");
5847 return -ENOMEM;
5848 }
5849
5850 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
5851 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5852 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
5853 LPFC_SLI4_MBX_EMBED);
5854 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5855 shdr = (union lpfc_sli4_cfg_shdr *)
5856 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5857 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5858 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5859 if (rc != MBX_TIMEOUT)
5860 mempool_free(mboxq, phba->mbox_mem_pool);
5861 if (shdr_status || shdr_add_status || rc) {
5862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5863 "0495 SLI_FUNCTION_RESET mailbox failed with "
5864 "status x%x add_status x%x, mbx status x%x\n",
5865 shdr_status, shdr_add_status, rc);
5866 rc = -ENXIO;
5867 }
5868 return rc;
5869}
5870
5871/**
5872 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
5873 * @phba: pointer to lpfc hba data structure.
5874 * @cnt: number of nop mailbox commands to send.
5875 *
5876 * This routine is invoked to send a number @cnt of NOP mailbox command and
5877 * wait for each command to complete.
5878 *
5879 * Return: the number of NOP mailbox command completed.
5880 **/
5881static int
5882lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
5883{
5884 LPFC_MBOXQ_t *mboxq;
5885 int length, cmdsent;
5886 uint32_t mbox_tmo;
5887 uint32_t rc = 0;
5888 uint32_t shdr_status, shdr_add_status;
5889 union lpfc_sli4_cfg_shdr *shdr;
5890
5891 if (cnt == 0) {
5892 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5893 "2518 Requested to send 0 NOP mailbox cmd\n");
5894 return cnt;
5895 }
5896
5897 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5898 if (!mboxq) {
5899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5900 "2519 Unable to allocate memory for issuing "
5901 "NOP mailbox command\n");
5902 return 0;
5903 }
5904
5905 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
5906 length = (sizeof(struct lpfc_mbx_nop) -
5907 sizeof(struct lpfc_sli4_cfg_mhdr));
5908 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5909 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
5910
5911 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5912 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
5913 if (!phba->sli4_hba.intr_enable)
5914 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5915 else
5916 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
5917 if (rc == MBX_TIMEOUT)
5918 break;
5919 /* Check return status */
5920 shdr = (union lpfc_sli4_cfg_shdr *)
5921 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5922 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5923 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
5924 &shdr->response);
5925 if (shdr_status || shdr_add_status || rc) {
5926 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5927 "2520 NOP mailbox command failed "
5928 "status x%x add_status x%x mbx "
5929 "status x%x\n", shdr_status,
5930 shdr_add_status, rc);
5931 break;
5932 }
5933 }
5934
5935 if (rc != MBX_TIMEOUT)
5936 mempool_free(mboxq, phba->mbox_mem_pool);
5937
5938 return cmdsent;
5939}
5940
5941/**
5942 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
5943 * @phba: pointer to lpfc hba data structure.
5944 * @fcfi: fcf index.
5945 *
5946 * This routine is invoked to unregister a FCFI from device.
5947 **/
5948void
5949lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
5950{
5951 LPFC_MBOXQ_t *mbox;
5952 uint32_t mbox_tmo;
5953 int rc;
5954 unsigned long flags;
5955
5956 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5957
5958 if (!mbox)
5959 return;
5960
5961 lpfc_unreg_fcfi(mbox, fcfi);
5962
5963 if (!phba->sli4_hba.intr_enable)
5964 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5965 else {
5966 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5967 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5968 }
5969 if (rc != MBX_TIMEOUT)
5970 mempool_free(mbox, phba->mbox_mem_pool);
5971 if (rc != MBX_SUCCESS)
5972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5973 "2517 Unregister FCFI command failed "
5974 "status %d, mbxStatus x%x\n", rc,
5975 bf_get(lpfc_mqe_status, &mbox->u.mqe));
5976 else {
5977 spin_lock_irqsave(&phba->hbalock, flags);
5978 /* Mark the FCFI is no longer registered */
5979 phba->fcf.fcf_flag &=
5980 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_DISCOVERED);
5981 spin_unlock_irqrestore(&phba->hbalock, flags);
5982 }
5983}
5984
5985/**
5986 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
5987 * @phba: pointer to lpfc hba data structure.
5988 *
5989 * This routine is invoked to set up the PCI device memory space for device
5990 * with SLI-4 interface spec.
5991 *
5992 * Return codes
5993 * 0 - sucessful
5994 * other values - error
5995 **/
5996static int
5997lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
5998{
5999 struct pci_dev *pdev;
6000 unsigned long bar0map_len, bar1map_len, bar2map_len;
6001 int error = -ENODEV;
6002
6003 /* Obtain PCI device reference */
6004 if (!phba->pcidev)
6005 return error;
6006 else
6007 pdev = phba->pcidev;
6008
6009 /* Set the device DMA mask size */
6010 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
6011 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
6012 return error;
6013
6014 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6015 * number of bytes required by each mapping. They are actually
6016 * mapping to the PCI BAR regions 1, 2, and 4 by the SLI4 device.
6017 */
6018 phba->pci_bar0_map = pci_resource_start(pdev, LPFC_SLI4_BAR0);
6019 bar0map_len = pci_resource_len(pdev, LPFC_SLI4_BAR0);
6020
6021 phba->pci_bar1_map = pci_resource_start(pdev, LPFC_SLI4_BAR1);
6022 bar1map_len = pci_resource_len(pdev, LPFC_SLI4_BAR1);
6023
6024 phba->pci_bar2_map = pci_resource_start(pdev, LPFC_SLI4_BAR2);
6025 bar2map_len = pci_resource_len(pdev, LPFC_SLI4_BAR2);
6026
6027 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6028 phba->sli4_hba.conf_regs_memmap_p =
6029 ioremap(phba->pci_bar0_map, bar0map_len);
6030 if (!phba->sli4_hba.conf_regs_memmap_p) {
6031 dev_printk(KERN_ERR, &pdev->dev,
6032 "ioremap failed for SLI4 PCI config registers.\n");
6033 goto out;
6034 }
6035
6036 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6037 phba->sli4_hba.ctrl_regs_memmap_p =
6038 ioremap(phba->pci_bar1_map, bar1map_len);
6039 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6040 dev_printk(KERN_ERR, &pdev->dev,
6041 "ioremap failed for SLI4 HBA control registers.\n");
6042 goto out_iounmap_conf;
6043 }
6044
6045 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6046 phba->sli4_hba.drbl_regs_memmap_p =
6047 ioremap(phba->pci_bar2_map, bar2map_len);
6048 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6049 dev_printk(KERN_ERR, &pdev->dev,
6050 "ioremap failed for SLI4 HBA doorbell registers.\n");
6051 goto out_iounmap_ctrl;
6052 }
6053
6054 /* Set up BAR0 PCI config space register memory map */
6055 lpfc_sli4_bar0_register_memmap(phba);
6056
6057 /* Set up BAR1 register memory map */
6058 lpfc_sli4_bar1_register_memmap(phba);
6059
6060 /* Set up BAR2 register memory map */
6061 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6062 if (error)
6063 goto out_iounmap_all;
6064
6065 return 0;
6066
6067out_iounmap_all:
6068 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6069out_iounmap_ctrl:
6070 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6071out_iounmap_conf:
6072 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6073out:
6074 return error;
6075}
6076
6077/**
6078 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6079 * @phba: pointer to lpfc hba data structure.
6080 *
6081 * This routine is invoked to unset the PCI device memory space for device
6082 * with SLI-4 interface spec.
6083 **/
6084static void
6085lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6086{
6087 struct pci_dev *pdev;
6088
6089 /* Obtain PCI device reference */
6090 if (!phba->pcidev)
6091 return;
6092 else
6093 pdev = phba->pcidev;
6094
6095 /* Free coherent DMA memory allocated */
6096
6097 /* Unmap I/O memory space */
6098 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6099 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6100 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6101
6102 return;
6103}
6104
6105/**
James Smart3772a992009-05-22 14:50:54 -04006106 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6107 * @phba: pointer to lpfc hba data structure.
6108 *
6109 * This routine is invoked to enable the MSI-X interrupt vectors to device
6110 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6111 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6112 * invoked, enables either all or nothing, depending on the current
6113 * availability of PCI vector resources. The device driver is responsible
6114 * for calling the individual request_irq() to register each MSI-X vector
6115 * with a interrupt handler, which is done in this function. Note that
6116 * later when device is unloading, the driver should always call free_irq()
6117 * on all MSI-X vectors it has done request_irq() on before calling
6118 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6119 * will be left with MSI-X enabled and leaks its vectors.
6120 *
6121 * Return codes
6122 * 0 - sucessful
6123 * other values - error
6124 **/
6125static int
6126lpfc_sli_enable_msix(struct lpfc_hba *phba)
6127{
6128 int rc, i;
6129 LPFC_MBOXQ_t *pmb;
6130
6131 /* Set up MSI-X multi-message vectors */
6132 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6133 phba->msix_entries[i].entry = i;
6134
6135 /* Configure MSI-X capability structure */
6136 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6137 ARRAY_SIZE(phba->msix_entries));
6138 if (rc) {
6139 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6140 "0420 PCI enable MSI-X failed (%d)\n", rc);
6141 goto msi_fail_out;
6142 }
6143 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6144 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6145 "0477 MSI-X entry[%d]: vector=x%x "
6146 "message=%d\n", i,
6147 phba->msix_entries[i].vector,
6148 phba->msix_entries[i].entry);
6149 /*
6150 * Assign MSI-X vectors to interrupt handlers
6151 */
6152
6153 /* vector-0 is associated to slow-path handler */
6154 rc = request_irq(phba->msix_entries[0].vector,
6155 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6156 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6157 if (rc) {
6158 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6159 "0421 MSI-X slow-path request_irq failed "
6160 "(%d)\n", rc);
6161 goto msi_fail_out;
6162 }
6163
6164 /* vector-1 is associated to fast-path handler */
6165 rc = request_irq(phba->msix_entries[1].vector,
6166 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6167 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6168
6169 if (rc) {
6170 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6171 "0429 MSI-X fast-path request_irq failed "
6172 "(%d)\n", rc);
6173 goto irq_fail_out;
6174 }
6175
6176 /*
6177 * Configure HBA MSI-X attention conditions to messages
6178 */
6179 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6180
6181 if (!pmb) {
6182 rc = -ENOMEM;
6183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6184 "0474 Unable to allocate memory for issuing "
6185 "MBOX_CONFIG_MSI command\n");
6186 goto mem_fail_out;
6187 }
6188 rc = lpfc_config_msi(phba, pmb);
6189 if (rc)
6190 goto mbx_fail_out;
6191 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6192 if (rc != MBX_SUCCESS) {
6193 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6194 "0351 Config MSI mailbox command failed, "
6195 "mbxCmd x%x, mbxStatus x%x\n",
6196 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6197 goto mbx_fail_out;
6198 }
6199
6200 /* Free memory allocated for mailbox command */
6201 mempool_free(pmb, phba->mbox_mem_pool);
6202 return rc;
6203
6204mbx_fail_out:
6205 /* Free memory allocated for mailbox command */
6206 mempool_free(pmb, phba->mbox_mem_pool);
6207
6208mem_fail_out:
6209 /* free the irq already requested */
6210 free_irq(phba->msix_entries[1].vector, phba);
6211
6212irq_fail_out:
6213 /* free the irq already requested */
6214 free_irq(phba->msix_entries[0].vector, phba);
6215
6216msi_fail_out:
6217 /* Unconfigure MSI-X capability structure */
6218 pci_disable_msix(phba->pcidev);
6219 return rc;
6220}
6221
6222/**
6223 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6224 * @phba: pointer to lpfc hba data structure.
6225 *
6226 * This routine is invoked to release the MSI-X vectors and then disable the
6227 * MSI-X interrupt mode to device with SLI-3 interface spec.
6228 **/
6229static void
6230lpfc_sli_disable_msix(struct lpfc_hba *phba)
6231{
6232 int i;
6233
6234 /* Free up MSI-X multi-message vectors */
6235 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6236 free_irq(phba->msix_entries[i].vector, phba);
6237 /* Disable MSI-X */
6238 pci_disable_msix(phba->pcidev);
6239
6240 return;
6241}
6242
6243/**
6244 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6245 * @phba: pointer to lpfc hba data structure.
6246 *
6247 * This routine is invoked to enable the MSI interrupt mode to device with
6248 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6249 * enable the MSI vector. The device driver is responsible for calling the
6250 * request_irq() to register MSI vector with a interrupt the handler, which
6251 * is done in this function.
6252 *
6253 * Return codes
6254 * 0 - sucessful
6255 * other values - error
6256 */
6257static int
6258lpfc_sli_enable_msi(struct lpfc_hba *phba)
6259{
6260 int rc;
6261
6262 rc = pci_enable_msi(phba->pcidev);
6263 if (!rc)
6264 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6265 "0462 PCI enable MSI mode success.\n");
6266 else {
6267 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6268 "0471 PCI enable MSI mode failed (%d)\n", rc);
6269 return rc;
6270 }
6271
6272 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6273 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6274 if (rc) {
6275 pci_disable_msi(phba->pcidev);
6276 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6277 "0478 MSI request_irq failed (%d)\n", rc);
6278 }
6279 return rc;
6280}
6281
6282/**
6283 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6284 * @phba: pointer to lpfc hba data structure.
6285 *
6286 * This routine is invoked to disable the MSI interrupt mode to device with
6287 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6288 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6289 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6290 * its vector.
6291 */
6292static void
6293lpfc_sli_disable_msi(struct lpfc_hba *phba)
6294{
6295 free_irq(phba->pcidev->irq, phba);
6296 pci_disable_msi(phba->pcidev);
6297 return;
6298}
6299
6300/**
6301 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6302 * @phba: pointer to lpfc hba data structure.
6303 *
6304 * This routine is invoked to enable device interrupt and associate driver's
6305 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6306 * spec. Depends on the interrupt mode configured to the driver, the driver
6307 * will try to fallback from the configured interrupt mode to an interrupt
6308 * mode which is supported by the platform, kernel, and device in the order
6309 * of:
6310 * MSI-X -> MSI -> IRQ.
6311 *
6312 * Return codes
6313 * 0 - sucessful
6314 * other values - error
6315 **/
6316static uint32_t
6317lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6318{
6319 uint32_t intr_mode = LPFC_INTR_ERROR;
6320 int retval;
6321
6322 if (cfg_mode == 2) {
6323 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6324 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6325 if (!retval) {
6326 /* Now, try to enable MSI-X interrupt mode */
6327 retval = lpfc_sli_enable_msix(phba);
6328 if (!retval) {
6329 /* Indicate initialization to MSI-X mode */
6330 phba->intr_type = MSIX;
6331 intr_mode = 2;
6332 }
6333 }
6334 }
6335
6336 /* Fallback to MSI if MSI-X initialization failed */
6337 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6338 retval = lpfc_sli_enable_msi(phba);
6339 if (!retval) {
6340 /* Indicate initialization to MSI mode */
6341 phba->intr_type = MSI;
6342 intr_mode = 1;
6343 }
6344 }
6345
6346 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6347 if (phba->intr_type == NONE) {
6348 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6349 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6350 if (!retval) {
6351 /* Indicate initialization to INTx mode */
6352 phba->intr_type = INTx;
6353 intr_mode = 0;
6354 }
6355 }
6356 return intr_mode;
6357}
6358
6359/**
6360 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6361 * @phba: pointer to lpfc hba data structure.
6362 *
6363 * This routine is invoked to disable device interrupt and disassociate the
6364 * driver's interrupt handler(s) from interrupt vector(s) to device with
6365 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6366 * release the interrupt vector(s) for the message signaled interrupt.
6367 **/
6368static void
6369lpfc_sli_disable_intr(struct lpfc_hba *phba)
6370{
6371 /* Disable the currently initialized interrupt mode */
6372 if (phba->intr_type == MSIX)
6373 lpfc_sli_disable_msix(phba);
6374 else if (phba->intr_type == MSI)
6375 lpfc_sli_disable_msi(phba);
6376 else if (phba->intr_type == INTx)
6377 free_irq(phba->pcidev->irq, phba);
6378
6379 /* Reset interrupt management states */
6380 phba->intr_type = NONE;
6381 phba->sli.slistat.sli_intr = 0;
6382
6383 return;
6384}
6385
6386/**
James Smartda0436e2009-05-22 14:51:39 -04006387 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6388 * @phba: pointer to lpfc hba data structure.
6389 *
6390 * This routine is invoked to enable the MSI-X interrupt vectors to device
6391 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6392 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6393 * enables either all or nothing, depending on the current availability of
6394 * PCI vector resources. The device driver is responsible for calling the
6395 * individual request_irq() to register each MSI-X vector with a interrupt
6396 * handler, which is done in this function. Note that later when device is
6397 * unloading, the driver should always call free_irq() on all MSI-X vectors
6398 * it has done request_irq() on before calling pci_disable_msix(). Failure
6399 * to do so results in a BUG_ON() and a device will be left with MSI-X
6400 * enabled and leaks its vectors.
6401 *
6402 * Return codes
6403 * 0 - sucessful
6404 * other values - error
6405 **/
6406static int
6407lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6408{
6409 int rc, index;
6410
6411 /* Set up MSI-X multi-message vectors */
6412 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6413 phba->sli4_hba.msix_entries[index].entry = index;
6414
6415 /* Configure MSI-X capability structure */
6416 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6417 phba->sli4_hba.cfg_eqn);
6418 if (rc) {
6419 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6420 "0484 PCI enable MSI-X failed (%d)\n", rc);
6421 goto msi_fail_out;
6422 }
6423 /* Log MSI-X vector assignment */
6424 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6425 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6426 "0489 MSI-X entry[%d]: vector=x%x "
6427 "message=%d\n", index,
6428 phba->sli4_hba.msix_entries[index].vector,
6429 phba->sli4_hba.msix_entries[index].entry);
6430 /*
6431 * Assign MSI-X vectors to interrupt handlers
6432 */
6433
6434 /* The first vector must associated to slow-path handler for MQ */
6435 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6436 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6437 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6438 if (rc) {
6439 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6440 "0485 MSI-X slow-path request_irq failed "
6441 "(%d)\n", rc);
6442 goto msi_fail_out;
6443 }
6444
6445 /* The rest of the vector(s) are associated to fast-path handler(s) */
6446 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6447 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6448 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6449 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6450 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6451 LPFC_FP_DRIVER_HANDLER_NAME,
6452 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6453 if (rc) {
6454 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6455 "0486 MSI-X fast-path (%d) "
6456 "request_irq failed (%d)\n", index, rc);
6457 goto cfg_fail_out;
6458 }
6459 }
6460
6461 return rc;
6462
6463cfg_fail_out:
6464 /* free the irq already requested */
6465 for (--index; index >= 1; index--)
6466 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6467 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6468
6469 /* free the irq already requested */
6470 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6471
6472msi_fail_out:
6473 /* Unconfigure MSI-X capability structure */
6474 pci_disable_msix(phba->pcidev);
6475 return rc;
6476}
6477
6478/**
6479 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6480 * @phba: pointer to lpfc hba data structure.
6481 *
6482 * This routine is invoked to release the MSI-X vectors and then disable the
6483 * MSI-X interrupt mode to device with SLI-4 interface spec.
6484 **/
6485static void
6486lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6487{
6488 int index;
6489
6490 /* Free up MSI-X multi-message vectors */
6491 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6492
6493 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6494 free_irq(phba->sli4_hba.msix_entries[index].vector,
6495 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6496 /* Disable MSI-X */
6497 pci_disable_msix(phba->pcidev);
6498
6499 return;
6500}
6501
6502/**
6503 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6504 * @phba: pointer to lpfc hba data structure.
6505 *
6506 * This routine is invoked to enable the MSI interrupt mode to device with
6507 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6508 * to enable the MSI vector. The device driver is responsible for calling
6509 * the request_irq() to register MSI vector with a interrupt the handler,
6510 * which is done in this function.
6511 *
6512 * Return codes
6513 * 0 - sucessful
6514 * other values - error
6515 **/
6516static int
6517lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6518{
6519 int rc, index;
6520
6521 rc = pci_enable_msi(phba->pcidev);
6522 if (!rc)
6523 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6524 "0487 PCI enable MSI mode success.\n");
6525 else {
6526 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6527 "0488 PCI enable MSI mode failed (%d)\n", rc);
6528 return rc;
6529 }
6530
6531 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6532 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6533 if (rc) {
6534 pci_disable_msi(phba->pcidev);
6535 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6536 "0490 MSI request_irq failed (%d)\n", rc);
6537 }
6538
6539 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6540 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6541 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6542 }
6543
6544 return rc;
6545}
6546
6547/**
6548 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6549 * @phba: pointer to lpfc hba data structure.
6550 *
6551 * This routine is invoked to disable the MSI interrupt mode to device with
6552 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6553 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6554 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6555 * its vector.
6556 **/
6557static void
6558lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6559{
6560 free_irq(phba->pcidev->irq, phba);
6561 pci_disable_msi(phba->pcidev);
6562 return;
6563}
6564
6565/**
6566 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6567 * @phba: pointer to lpfc hba data structure.
6568 *
6569 * This routine is invoked to enable device interrupt and associate driver's
6570 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6571 * interface spec. Depends on the interrupt mode configured to the driver,
6572 * the driver will try to fallback from the configured interrupt mode to an
6573 * interrupt mode which is supported by the platform, kernel, and device in
6574 * the order of:
6575 * MSI-X -> MSI -> IRQ.
6576 *
6577 * Return codes
6578 * 0 - sucessful
6579 * other values - error
6580 **/
6581static uint32_t
6582lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6583{
6584 uint32_t intr_mode = LPFC_INTR_ERROR;
6585 int retval, index;
6586
6587 if (cfg_mode == 2) {
6588 /* Preparation before conf_msi mbox cmd */
6589 retval = 0;
6590 if (!retval) {
6591 /* Now, try to enable MSI-X interrupt mode */
6592 retval = lpfc_sli4_enable_msix(phba);
6593 if (!retval) {
6594 /* Indicate initialization to MSI-X mode */
6595 phba->intr_type = MSIX;
6596 intr_mode = 2;
6597 }
6598 }
6599 }
6600
6601 /* Fallback to MSI if MSI-X initialization failed */
6602 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6603 retval = lpfc_sli4_enable_msi(phba);
6604 if (!retval) {
6605 /* Indicate initialization to MSI mode */
6606 phba->intr_type = MSI;
6607 intr_mode = 1;
6608 }
6609 }
6610
6611 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6612 if (phba->intr_type == NONE) {
6613 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6614 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6615 if (!retval) {
6616 /* Indicate initialization to INTx mode */
6617 phba->intr_type = INTx;
6618 intr_mode = 0;
6619 for (index = 0; index < phba->cfg_fcp_eq_count;
6620 index++) {
6621 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6622 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6623 }
6624 }
6625 }
6626 return intr_mode;
6627}
6628
6629/**
6630 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6631 * @phba: pointer to lpfc hba data structure.
6632 *
6633 * This routine is invoked to disable device interrupt and disassociate
6634 * the driver's interrupt handler(s) from interrupt vector(s) to device
6635 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6636 * will release the interrupt vector(s) for the message signaled interrupt.
6637 **/
6638static void
6639lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6640{
6641 /* Disable the currently initialized interrupt mode */
6642 if (phba->intr_type == MSIX)
6643 lpfc_sli4_disable_msix(phba);
6644 else if (phba->intr_type == MSI)
6645 lpfc_sli4_disable_msi(phba);
6646 else if (phba->intr_type == INTx)
6647 free_irq(phba->pcidev->irq, phba);
6648
6649 /* Reset interrupt management states */
6650 phba->intr_type = NONE;
6651 phba->sli.slistat.sli_intr = 0;
6652
6653 return;
6654}
6655
6656/**
James Smart3772a992009-05-22 14:50:54 -04006657 * lpfc_unset_hba - Unset SLI3 hba device initialization
6658 * @phba: pointer to lpfc hba data structure.
6659 *
6660 * This routine is invoked to unset the HBA device initialization steps to
6661 * a device with SLI-3 interface spec.
6662 **/
6663static void
6664lpfc_unset_hba(struct lpfc_hba *phba)
6665{
6666 struct lpfc_vport *vport = phba->pport;
6667 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6668
6669 spin_lock_irq(shost->host_lock);
6670 vport->load_flag |= FC_UNLOADING;
6671 spin_unlock_irq(shost->host_lock);
6672
6673 lpfc_stop_hba_timers(phba);
6674
6675 phba->pport->work_port_events = 0;
6676
6677 lpfc_sli_hba_down(phba);
6678
6679 lpfc_sli_brdrestart(phba);
6680
6681 lpfc_sli_disable_intr(phba);
6682
6683 return;
6684}
6685
6686/**
James Smartda0436e2009-05-22 14:51:39 -04006687 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6688 * @phba: pointer to lpfc hba data structure.
6689 *
6690 * This routine is invoked to unset the HBA device initialization steps to
6691 * a device with SLI-4 interface spec.
6692 **/
6693static void
6694lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6695{
6696 struct lpfc_vport *vport = phba->pport;
6697 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6698
6699 spin_lock_irq(shost->host_lock);
6700 vport->load_flag |= FC_UNLOADING;
6701 spin_unlock_irq(shost->host_lock);
6702
6703 phba->pport->work_port_events = 0;
6704
6705 lpfc_sli4_hba_down(phba);
6706
6707 lpfc_sli4_disable_intr(phba);
6708
6709 return;
6710}
6711
6712/**
6713 * lpfc_sli4_hba_unset - Unset the fcoe hba
6714 * @phba: Pointer to HBA context object.
6715 *
6716 * This function is called in the SLI4 code path to reset the HBA's FCoE
6717 * function. The caller is not required to hold any lock. This routine
6718 * issues PCI function reset mailbox command to reset the FCoE function.
6719 * At the end of the function, it calls lpfc_hba_down_post function to
6720 * free any pending commands.
6721 **/
6722static void
6723lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6724{
6725 int wait_cnt = 0;
6726 LPFC_MBOXQ_t *mboxq;
6727
6728 lpfc_stop_hba_timers(phba);
6729 phba->sli4_hba.intr_enable = 0;
6730
6731 /*
6732 * Gracefully wait out the potential current outstanding asynchronous
6733 * mailbox command.
6734 */
6735
6736 /* First, block any pending async mailbox command from posted */
6737 spin_lock_irq(&phba->hbalock);
6738 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6739 spin_unlock_irq(&phba->hbalock);
6740 /* Now, trying to wait it out if we can */
6741 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6742 msleep(10);
6743 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
6744 break;
6745 }
6746 /* Forcefully release the outstanding mailbox command if timed out */
6747 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6748 spin_lock_irq(&phba->hbalock);
6749 mboxq = phba->sli.mbox_active;
6750 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6751 __lpfc_mbox_cmpl_put(phba, mboxq);
6752 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6753 phba->sli.mbox_active = NULL;
6754 spin_unlock_irq(&phba->hbalock);
6755 }
6756
6757 /* Tear down the queues in the HBA */
6758 lpfc_sli4_queue_unset(phba);
6759
6760 /* Disable PCI subsystem interrupt */
6761 lpfc_sli4_disable_intr(phba);
6762
6763 /* Stop kthread signal shall trigger work_done one more time */
6764 kthread_stop(phba->worker_thread);
6765
6766 /* Stop the SLI4 device port */
6767 phba->pport->work_port_events = 0;
6768}
6769
6770/**
James Smart3772a992009-05-22 14:50:54 -04006771 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
6772 * @pdev: pointer to PCI device
6773 * @pid: pointer to PCI device identifier
6774 *
6775 * This routine is to be called to attach a device with SLI-3 interface spec
6776 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6777 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
6778 * information of the device and driver to see if the driver state that it can
6779 * support this kind of device. If the match is successful, the driver core
6780 * invokes this routine. If this routine determines it can claim the HBA, it
6781 * does all the initialization that it needs to do to handle the HBA properly.
6782 *
6783 * Return code
6784 * 0 - driver can claim the device
6785 * negative value - driver can not claim the device
6786 **/
6787static int __devinit
6788lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
6789{
6790 struct lpfc_hba *phba;
6791 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04006792 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04006793 int error;
6794 uint32_t cfg_mode, intr_mode;
6795
6796 /* Allocate memory for HBA structure */
6797 phba = lpfc_hba_alloc(pdev);
6798 if (!phba)
6799 return -ENOMEM;
6800
6801 /* Perform generic PCI device enabling operation */
6802 error = lpfc_enable_pci_dev(phba);
6803 if (error) {
6804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6805 "1401 Failed to enable pci device.\n");
6806 goto out_free_phba;
6807 }
6808
6809 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
6810 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
6811 if (error)
6812 goto out_disable_pci_dev;
6813
6814 /* Set up SLI-3 specific device PCI memory space */
6815 error = lpfc_sli_pci_mem_setup(phba);
6816 if (error) {
6817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6818 "1402 Failed to set up pci memory space.\n");
6819 goto out_disable_pci_dev;
6820 }
6821
6822 /* Set up phase-1 common device driver resources */
6823 error = lpfc_setup_driver_resource_phase1(phba);
6824 if (error) {
6825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6826 "1403 Failed to set up driver resource.\n");
6827 goto out_unset_pci_mem_s3;
6828 }
6829
6830 /* Set up SLI-3 specific device driver resources */
6831 error = lpfc_sli_driver_resource_setup(phba);
6832 if (error) {
6833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6834 "1404 Failed to set up driver resource.\n");
6835 goto out_unset_pci_mem_s3;
6836 }
6837
6838 /* Initialize and populate the iocb list per host */
6839 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
6840 if (error) {
6841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6842 "1405 Failed to initialize iocb list.\n");
6843 goto out_unset_driver_resource_s3;
6844 }
6845
6846 /* Set up common device driver resources */
6847 error = lpfc_setup_driver_resource_phase2(phba);
6848 if (error) {
6849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6850 "1406 Failed to set up driver resource.\n");
6851 goto out_free_iocb_list;
6852 }
6853
6854 /* Create SCSI host to the physical port */
6855 error = lpfc_create_shost(phba);
6856 if (error) {
6857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6858 "1407 Failed to create scsi host.\n");
6859 goto out_unset_driver_resource;
6860 }
6861
6862 /* Configure sysfs attributes */
6863 vport = phba->pport;
6864 error = lpfc_alloc_sysfs_attr(vport);
6865 if (error) {
6866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6867 "1476 Failed to allocate sysfs attr\n");
6868 goto out_destroy_shost;
6869 }
6870
James Smart6669f9b2009-10-02 15:16:45 -04006871 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04006872 /* Now, trying to enable interrupt and bring up the device */
6873 cfg_mode = phba->cfg_use_msi;
6874 while (true) {
6875 /* Put device to a known state before enabling interrupt */
6876 lpfc_stop_port(phba);
6877 /* Configure and enable interrupt */
6878 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
6879 if (intr_mode == LPFC_INTR_ERROR) {
6880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6881 "0431 Failed to enable interrupt.\n");
6882 error = -ENODEV;
6883 goto out_free_sysfs_attr;
6884 }
6885 /* SLI-3 HBA setup */
6886 if (lpfc_sli_hba_setup(phba)) {
6887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6888 "1477 Failed to set up hba\n");
6889 error = -ENODEV;
6890 goto out_remove_device;
6891 }
6892
6893 /* Wait 50ms for the interrupts of previous mailbox commands */
6894 msleep(50);
6895 /* Check active interrupts on message signaled interrupts */
6896 if (intr_mode == 0 ||
6897 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
6898 /* Log the current active interrupt mode */
6899 phba->intr_mode = intr_mode;
6900 lpfc_log_intr_mode(phba, intr_mode);
6901 break;
6902 } else {
6903 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6904 "0447 Configure interrupt mode (%d) "
6905 "failed active interrupt test.\n",
6906 intr_mode);
6907 /* Disable the current interrupt mode */
6908 lpfc_sli_disable_intr(phba);
6909 /* Try next level of interrupt mode */
6910 cfg_mode = --intr_mode;
6911 }
6912 }
6913
6914 /* Perform post initialization setup */
6915 lpfc_post_init_setup(phba);
6916
6917 /* Check if there are static vports to be created. */
6918 lpfc_create_static_vport(phba);
6919
6920 return 0;
6921
6922out_remove_device:
6923 lpfc_unset_hba(phba);
6924out_free_sysfs_attr:
6925 lpfc_free_sysfs_attr(vport);
6926out_destroy_shost:
6927 lpfc_destroy_shost(phba);
6928out_unset_driver_resource:
6929 lpfc_unset_driver_resource_phase2(phba);
6930out_free_iocb_list:
6931 lpfc_free_iocb_list(phba);
6932out_unset_driver_resource_s3:
6933 lpfc_sli_driver_resource_unset(phba);
6934out_unset_pci_mem_s3:
6935 lpfc_sli_pci_mem_unset(phba);
6936out_disable_pci_dev:
6937 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04006938 if (shost)
6939 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04006940out_free_phba:
6941 lpfc_hba_free(phba);
6942 return error;
6943}
6944
6945/**
6946 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04006947 * @pdev: pointer to PCI device
6948 *
James Smart3772a992009-05-22 14:50:54 -04006949 * This routine is to be called to disattach a device with SLI-3 interface
6950 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6951 * removed from PCI bus, it performs all the necessary cleanup for the HBA
6952 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04006953 **/
dea31012005-04-17 16:05:31 -05006954static void __devexit
James Smart3772a992009-05-22 14:50:54 -04006955lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05006956{
James Smart2e0fef82007-06-17 19:56:36 -05006957 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6958 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05006959 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05006960 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05006961 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05006962 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
6963
James Smart549e55c2007-08-02 11:09:51 -04006964 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04006965 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04006966 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006967
James Smart858c9f62007-06-17 19:56:39 -05006968 lpfc_free_sysfs_attr(vport);
6969
James Smarteada2722008-12-04 22:39:13 -05006970 /* Release all the vports against this physical port */
6971 vports = lpfc_create_vport_work_array(phba);
6972 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04006973 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05006974 fc_vport_terminate(vports[i]->fc_vport);
6975 lpfc_destroy_vport_work_array(phba, vports);
6976
6977 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05006978 fc_remove_host(shost);
6979 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04006980 lpfc_cleanup(vport);
6981
James Smart2e0fef82007-06-17 19:56:36 -05006982 /*
6983 * Bring down the SLI Layer. This step disable all interrupts,
6984 * clears the rings, discards all mailbox commands, and resets
6985 * the HBA.
6986 */
James Smarta257bf92009-04-06 18:48:10 -04006987
6988 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05006989 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04006990 /* Stop kthread signal shall trigger work_done one more time */
6991 kthread_stop(phba->worker_thread);
6992 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05006993 lpfc_sli_brdrestart(phba);
6994
James Smart3772a992009-05-22 14:50:54 -04006995 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05006996 spin_lock_irq(&phba->hbalock);
6997 list_del_init(&vport->listentry);
6998 spin_unlock_irq(&phba->hbalock);
6999
James Smart858c9f62007-06-17 19:56:39 -05007000 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007001
James Smart5b75da22008-12-04 22:39:35 -05007002 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007003 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007004
7005 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007006 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007007
7008 /*
7009 * Call scsi_free before mem_free since scsi bufs are released to their
7010 * corresponding pools here.
7011 */
7012 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007013 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007014
James Smart34b02dc2008-08-24 21:49:55 -04007015 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7016 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007017
James Smart2e0fef82007-06-17 19:56:36 -05007018 /* Free resources associated with SLI2 interface */
7019 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007020 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007021
7022 /* unmap adapter SLIM and Control Registers */
7023 iounmap(phba->ctrl_regs_memmap_p);
7024 iounmap(phba->slim_memmap_p);
7025
James Smart3772a992009-05-22 14:50:54 -04007026 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007027
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007028 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007029 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007030}
7031
Linas Vepstas8d63f372007-02-14 14:28:36 -06007032/**
James Smart3772a992009-05-22 14:50:54 -04007033 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007034 * @pdev: pointer to PCI device
7035 * @msg: power management message
7036 *
James Smart3772a992009-05-22 14:50:54 -04007037 * This routine is to be called from the kernel's PCI subsystem to support
7038 * system Power Management (PM) to device with SLI-3 interface spec. When
7039 * PM invokes this method, it quiesces the device by stopping the driver's
7040 * worker thread for the device, turning off device's interrupt and DMA,
7041 * and bring the device offline. Note that as the driver implements the
7042 * minimum PM requirements to a power-aware driver's PM support for the
7043 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7044 * to the suspend() method call will be treated as SUSPEND and the driver will
7045 * fully reinitialize its device during resume() method call, the driver will
7046 * set device to PCI_D3hot state in PCI config space instead of setting it
7047 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007048 *
7049 * Return code
James Smart3772a992009-05-22 14:50:54 -04007050 * 0 - driver suspended the device
7051 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007052 **/
7053static int
James Smart3772a992009-05-22 14:50:54 -04007054lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007055{
7056 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7057 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7058
7059 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7060 "0473 PCI device Power Management suspend.\n");
7061
7062 /* Bring down the device */
7063 lpfc_offline_prep(phba);
7064 lpfc_offline(phba);
7065 kthread_stop(phba->worker_thread);
7066
7067 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007068 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007069
7070 /* Save device state to PCI config space */
7071 pci_save_state(pdev);
7072 pci_set_power_state(pdev, PCI_D3hot);
7073
7074 return 0;
7075}
7076
7077/**
James Smart3772a992009-05-22 14:50:54 -04007078 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007079 * @pdev: pointer to PCI device
7080 *
James Smart3772a992009-05-22 14:50:54 -04007081 * This routine is to be called from the kernel's PCI subsystem to support
7082 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7083 * invokes this method, it restores the device's PCI config space state and
7084 * fully reinitializes the device and brings it online. Note that as the
7085 * driver implements the minimum PM requirements to a power-aware driver's
7086 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7087 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7088 * driver will fully reinitialize its device during resume() method call,
7089 * the device will be set to PCI_D0 directly in PCI config space before
7090 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007091 *
7092 * Return code
James Smart3772a992009-05-22 14:50:54 -04007093 * 0 - driver suspended the device
7094 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007095 **/
7096static int
James Smart3772a992009-05-22 14:50:54 -04007097lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007098{
7099 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7100 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007101 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007102 int error;
7103
7104 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7105 "0452 PCI device Power Management resume.\n");
7106
7107 /* Restore device state from PCI config space */
7108 pci_set_power_state(pdev, PCI_D0);
7109 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007110
James Smart3a55b532008-12-04 22:38:54 -05007111 if (pdev->is_busmaster)
7112 pci_set_master(pdev);
7113
7114 /* Startup the kernel thread for this host adapter. */
7115 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7116 "lpfc_worker_%d", phba->brd_no);
7117 if (IS_ERR(phba->worker_thread)) {
7118 error = PTR_ERR(phba->worker_thread);
7119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7120 "0434 PM resume failed to start worker "
7121 "thread: error=x%x.\n", error);
7122 return error;
7123 }
7124
James Smart5b75da22008-12-04 22:39:35 -05007125 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007126 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007127 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007129 "0430 PM resume Failed to enable interrupt\n");
7130 return -EIO;
7131 } else
7132 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007133
7134 /* Restart HBA and bring it online */
7135 lpfc_sli_brdrestart(phba);
7136 lpfc_online(phba);
7137
James Smart5b75da22008-12-04 22:39:35 -05007138 /* Log the current active interrupt mode */
7139 lpfc_log_intr_mode(phba, phba->intr_mode);
7140
James Smart3a55b532008-12-04 22:38:54 -05007141 return 0;
7142}
7143
7144/**
James Smart891478a2009-11-18 15:40:23 -05007145 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7146 * @phba: pointer to lpfc hba data structure.
7147 *
7148 * This routine is called to prepare the SLI3 device for PCI slot recover. It
7149 * aborts and stops all the on-going I/Os on the pci device.
7150 **/
7151static void
7152lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7153{
7154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7155 "2723 PCI channel I/O abort preparing for recovery\n");
7156 /* Prepare for bringing HBA offline */
7157 lpfc_offline_prep(phba);
7158 /* Clear sli active flag to prevent sysfs access to HBA */
7159 spin_lock_irq(&phba->hbalock);
7160 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7161 spin_unlock_irq(&phba->hbalock);
7162 /* Stop and flush all I/Os and bring HBA offline */
7163 lpfc_offline(phba);
7164}
7165
7166/**
James Smart0d878412009-10-02 15:16:56 -04007167 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7168 * @phba: pointer to lpfc hba data structure.
7169 *
7170 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7171 * disables the device interrupt and pci device, and aborts the internal FCP
7172 * pending I/Os.
7173 **/
7174static void
7175lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7176{
7177 struct lpfc_sli *psli = &phba->sli;
7178 struct lpfc_sli_ring *pring;
7179
7180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007181 "2710 PCI channel disable preparing for reset\n");
James Smart0d878412009-10-02 15:16:56 -04007182 /* Disable interrupt and pci device */
7183 lpfc_sli_disable_intr(phba);
7184 pci_disable_device(phba->pcidev);
7185 /*
7186 * There may be I/Os dropped by the firmware.
7187 * Error iocb (I/O) on txcmplq and let the SCSI layer
7188 * retry it after re-establishing link.
7189 */
7190 pring = &psli->ring[psli->fcp_ring];
7191 lpfc_sli_abort_iocb_ring(phba, pring);
7192}
7193
7194/**
7195 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7196 * @phba: pointer to lpfc hba data structure.
7197 *
7198 * This routine is called to prepare the SLI3 device for PCI slot permanently
7199 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7200 * pending I/Os.
7201 **/
7202static void
7203lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7204{
7205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007206 "2711 PCI channel permanent disable for failure\n");
James Smart0d878412009-10-02 15:16:56 -04007207 /* Block all SCSI devices' I/Os on the host */
7208 lpfc_scsi_dev_block(phba);
7209 /* Clean up all driver's outstanding SCSI I/Os */
7210 lpfc_sli_flush_fcp_rings(phba);
7211}
7212
7213/**
James Smart3772a992009-05-22 14:50:54 -04007214 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007215 * @pdev: pointer to PCI device.
7216 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007217 *
James Smart3772a992009-05-22 14:50:54 -04007218 * This routine is called from the PCI subsystem for I/O error handling to
7219 * device with SLI-3 interface spec. This function is called by the PCI
7220 * subsystem after a PCI bus error affecting this device has been detected.
7221 * When this function is invoked, it will need to stop all the I/Os and
7222 * interrupt(s) to the device. Once that is done, it will return
7223 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7224 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007225 *
7226 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007227 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007228 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7229 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007230 **/
James Smart3772a992009-05-22 14:50:54 -04007231static pci_ers_result_t
7232lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007233{
James Smart51ef4c22007-08-02 11:10:31 -04007234 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7235 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007236
James Smart0d878412009-10-02 15:16:56 -04007237 switch (state) {
7238 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007239 /* Non-fatal error, prepare for recovery */
7240 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007241 return PCI_ERS_RESULT_CAN_RECOVER;
7242 case pci_channel_io_frozen:
7243 /* Fatal error, prepare for slot reset */
7244 lpfc_sli_prep_dev_for_reset(phba);
7245 return PCI_ERS_RESULT_NEED_RESET;
7246 case pci_channel_io_perm_failure:
7247 /* Permanent failure, prepare for device down */
7248 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007249 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007250 default:
7251 /* Unknown state, prepare and request slot reset */
7252 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7253 "0472 Unknown PCI error state: x%x\n", state);
7254 lpfc_sli_prep_dev_for_reset(phba);
7255 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007256 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007257}
7258
7259/**
James Smart3772a992009-05-22 14:50:54 -04007260 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007261 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007262 *
James Smart3772a992009-05-22 14:50:54 -04007263 * This routine is called from the PCI subsystem for error handling to
7264 * device with SLI-3 interface spec. This is called after PCI bus has been
7265 * reset to restart the PCI card from scratch, as if from a cold-boot.
7266 * During the PCI subsystem error recovery, after driver returns
7267 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7268 * recovery and then call this routine before calling the .resume method
7269 * to recover the device. This function will initialize the HBA device,
7270 * enable the interrupt, but it will just put the HBA to offline state
7271 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007272 *
7273 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007274 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7275 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007276 */
James Smart3772a992009-05-22 14:50:54 -04007277static pci_ers_result_t
7278lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007279{
James Smart51ef4c22007-08-02 11:10:31 -04007280 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7281 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007282 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007283 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007284
7285 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007286 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007287 printk(KERN_ERR "lpfc: Cannot re-enable "
7288 "PCI device after reset.\n");
7289 return PCI_ERS_RESULT_DISCONNECT;
7290 }
7291
James Smart97207482008-12-04 22:39:19 -05007292 pci_restore_state(pdev);
7293 if (pdev->is_busmaster)
7294 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007295
James Smart92d7f7b2007-06-17 19:56:38 -05007296 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007297 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05007298 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007299
James Smart5b75da22008-12-04 22:39:35 -05007300 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007301 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007302 if (intr_mode == LPFC_INTR_ERROR) {
7303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7304 "0427 Cannot re-enable interrupt after "
7305 "slot reset.\n");
7306 return PCI_ERS_RESULT_DISCONNECT;
7307 } else
7308 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007309
7310 /* Take device offline; this will perform cleanup */
7311 lpfc_offline(phba);
7312 lpfc_sli_brdrestart(phba);
7313
James Smart5b75da22008-12-04 22:39:35 -05007314 /* Log the current active interrupt mode */
7315 lpfc_log_intr_mode(phba, phba->intr_mode);
7316
Linas Vepstas8d63f372007-02-14 14:28:36 -06007317 return PCI_ERS_RESULT_RECOVERED;
7318}
7319
7320/**
James Smart3772a992009-05-22 14:50:54 -04007321 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04007322 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06007323 *
James Smart3772a992009-05-22 14:50:54 -04007324 * This routine is called from the PCI subsystem for error handling to device
7325 * with SLI-3 interface spec. It is called when kernel error recovery tells
7326 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7327 * error recovery. After this call, traffic can start to flow from this device
7328 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007329 */
James Smart3772a992009-05-22 14:50:54 -04007330static void
7331lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007332{
James Smart51ef4c22007-08-02 11:10:31 -04007333 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7334 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007335
James Smart0d878412009-10-02 15:16:56 -04007336 /* Bring the device online */
James Smart58da1ff2008-04-07 10:15:56 -04007337 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04007338
7339 /* Clean up Advanced Error Reporting (AER) if needed */
7340 if (phba->hba_flag & HBA_AER_ENABLED)
7341 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007342}
7343
James Smart3772a992009-05-22 14:50:54 -04007344/**
James Smartda0436e2009-05-22 14:51:39 -04007345 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7346 * @phba: pointer to lpfc hba data structure.
7347 *
7348 * returns the number of ELS/CT IOCBs to reserve
7349 **/
7350int
7351lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7352{
7353 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7354
James Smartf1126682009-06-10 17:22:44 -04007355 if (phba->sli_rev == LPFC_SLI_REV4) {
7356 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04007357 return 10;
James Smartf1126682009-06-10 17:22:44 -04007358 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04007359 return 25;
James Smartf1126682009-06-10 17:22:44 -04007360 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04007361 return 50;
James Smartf1126682009-06-10 17:22:44 -04007362 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04007363 return 100;
James Smartf1126682009-06-10 17:22:44 -04007364 else
James Smart6a9c52c2009-10-02 15:16:51 -04007365 return 150;
James Smartf1126682009-06-10 17:22:44 -04007366 } else
7367 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007368}
7369
7370/**
7371 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7372 * @pdev: pointer to PCI device
7373 * @pid: pointer to PCI device identifier
7374 *
7375 * This routine is called from the kernel's PCI subsystem to device with
7376 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7377 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7378 * information of the device and driver to see if the driver state that it
7379 * can support this kind of device. If the match is successful, the driver
7380 * core invokes this routine. If this routine determines it can claim the HBA,
7381 * it does all the initialization that it needs to do to handle the HBA
7382 * properly.
7383 *
7384 * Return code
7385 * 0 - driver can claim the device
7386 * negative value - driver can not claim the device
7387 **/
7388static int __devinit
7389lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7390{
7391 struct lpfc_hba *phba;
7392 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007393 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007394 int error;
7395 uint32_t cfg_mode, intr_mode;
7396 int mcnt;
7397
7398 /* Allocate memory for HBA structure */
7399 phba = lpfc_hba_alloc(pdev);
7400 if (!phba)
7401 return -ENOMEM;
7402
7403 /* Perform generic PCI device enabling operation */
7404 error = lpfc_enable_pci_dev(phba);
7405 if (error) {
7406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7407 "1409 Failed to enable pci device.\n");
7408 goto out_free_phba;
7409 }
7410
7411 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7412 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7413 if (error)
7414 goto out_disable_pci_dev;
7415
7416 /* Set up SLI-4 specific device PCI memory space */
7417 error = lpfc_sli4_pci_mem_setup(phba);
7418 if (error) {
7419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7420 "1410 Failed to set up pci memory space.\n");
7421 goto out_disable_pci_dev;
7422 }
7423
7424 /* Set up phase-1 common device driver resources */
7425 error = lpfc_setup_driver_resource_phase1(phba);
7426 if (error) {
7427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7428 "1411 Failed to set up driver resource.\n");
7429 goto out_unset_pci_mem_s4;
7430 }
7431
7432 /* Set up SLI-4 Specific device driver resources */
7433 error = lpfc_sli4_driver_resource_setup(phba);
7434 if (error) {
7435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7436 "1412 Failed to set up driver resource.\n");
7437 goto out_unset_pci_mem_s4;
7438 }
7439
7440 /* Initialize and populate the iocb list per host */
7441 error = lpfc_init_iocb_list(phba,
7442 phba->sli4_hba.max_cfg_param.max_xri);
7443 if (error) {
7444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7445 "1413 Failed to initialize iocb list.\n");
7446 goto out_unset_driver_resource_s4;
7447 }
7448
7449 /* Set up common device driver resources */
7450 error = lpfc_setup_driver_resource_phase2(phba);
7451 if (error) {
7452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7453 "1414 Failed to set up driver resource.\n");
7454 goto out_free_iocb_list;
7455 }
7456
7457 /* Create SCSI host to the physical port */
7458 error = lpfc_create_shost(phba);
7459 if (error) {
7460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7461 "1415 Failed to create scsi host.\n");
7462 goto out_unset_driver_resource;
7463 }
7464
7465 /* Configure sysfs attributes */
7466 vport = phba->pport;
7467 error = lpfc_alloc_sysfs_attr(vport);
7468 if (error) {
7469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7470 "1416 Failed to allocate sysfs attr\n");
7471 goto out_destroy_shost;
7472 }
7473
James Smart6669f9b2009-10-02 15:16:45 -04007474 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04007475 /* Now, trying to enable interrupt and bring up the device */
7476 cfg_mode = phba->cfg_use_msi;
7477 while (true) {
7478 /* Put device to a known state before enabling interrupt */
7479 lpfc_stop_port(phba);
7480 /* Configure and enable interrupt */
7481 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7482 if (intr_mode == LPFC_INTR_ERROR) {
7483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7484 "0426 Failed to enable interrupt.\n");
7485 error = -ENODEV;
7486 goto out_free_sysfs_attr;
7487 }
7488 /* Set up SLI-4 HBA */
7489 if (lpfc_sli4_hba_setup(phba)) {
7490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7491 "1421 Failed to set up hba\n");
7492 error = -ENODEV;
7493 goto out_disable_intr;
7494 }
7495
7496 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7497 if (intr_mode != 0)
7498 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7499 LPFC_ACT_INTR_CNT);
7500
7501 /* Check active interrupts received only for MSI/MSI-X */
7502 if (intr_mode == 0 ||
7503 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7504 /* Log the current active interrupt mode */
7505 phba->intr_mode = intr_mode;
7506 lpfc_log_intr_mode(phba, intr_mode);
7507 break;
7508 }
7509 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7510 "0451 Configure interrupt mode (%d) "
7511 "failed active interrupt test.\n",
7512 intr_mode);
7513 /* Unset the preivous SLI-4 HBA setup */
7514 lpfc_sli4_unset_hba(phba);
7515 /* Try next level of interrupt mode */
7516 cfg_mode = --intr_mode;
7517 }
7518
7519 /* Perform post initialization setup */
7520 lpfc_post_init_setup(phba);
7521
James Smart1c6834a2009-07-19 10:01:26 -04007522 /* Check if there are static vports to be created. */
7523 lpfc_create_static_vport(phba);
7524
James Smartda0436e2009-05-22 14:51:39 -04007525 return 0;
7526
7527out_disable_intr:
7528 lpfc_sli4_disable_intr(phba);
7529out_free_sysfs_attr:
7530 lpfc_free_sysfs_attr(vport);
7531out_destroy_shost:
7532 lpfc_destroy_shost(phba);
7533out_unset_driver_resource:
7534 lpfc_unset_driver_resource_phase2(phba);
7535out_free_iocb_list:
7536 lpfc_free_iocb_list(phba);
7537out_unset_driver_resource_s4:
7538 lpfc_sli4_driver_resource_unset(phba);
7539out_unset_pci_mem_s4:
7540 lpfc_sli4_pci_mem_unset(phba);
7541out_disable_pci_dev:
7542 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007543 if (shost)
7544 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04007545out_free_phba:
7546 lpfc_hba_free(phba);
7547 return error;
7548}
7549
7550/**
7551 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
7552 * @pdev: pointer to PCI device
7553 *
7554 * This routine is called from the kernel's PCI subsystem to device with
7555 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7556 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7557 * device to be removed from the PCI subsystem properly.
7558 **/
7559static void __devexit
7560lpfc_pci_remove_one_s4(struct pci_dev *pdev)
7561{
7562 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7563 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7564 struct lpfc_vport **vports;
7565 struct lpfc_hba *phba = vport->phba;
7566 int i;
7567
7568 /* Mark the device unloading flag */
7569 spin_lock_irq(&phba->hbalock);
7570 vport->load_flag |= FC_UNLOADING;
7571 spin_unlock_irq(&phba->hbalock);
7572
7573 /* Free the HBA sysfs attributes */
7574 lpfc_free_sysfs_attr(vport);
7575
7576 /* Release all the vports against this physical port */
7577 vports = lpfc_create_vport_work_array(phba);
7578 if (vports != NULL)
7579 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7580 fc_vport_terminate(vports[i]->fc_vport);
7581 lpfc_destroy_vport_work_array(phba, vports);
7582
7583 /* Remove FC host and then SCSI host with the physical port */
7584 fc_remove_host(shost);
7585 scsi_remove_host(shost);
7586
7587 /* Perform cleanup on the physical port */
7588 lpfc_cleanup(vport);
7589
7590 /*
7591 * Bring down the SLI Layer. This step disables all interrupts,
7592 * clears the rings, discards all mailbox commands, and resets
7593 * the HBA FCoE function.
7594 */
7595 lpfc_debugfs_terminate(vport);
7596 lpfc_sli4_hba_unset(phba);
7597
7598 spin_lock_irq(&phba->hbalock);
7599 list_del_init(&vport->listentry);
7600 spin_unlock_irq(&phba->hbalock);
7601
7602 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7603 * buffers are released to their corresponding pools here.
7604 */
7605 lpfc_scsi_free(phba);
7606 lpfc_sli4_driver_resource_unset(phba);
7607
7608 /* Unmap adapter Control and Doorbell registers */
7609 lpfc_sli4_pci_mem_unset(phba);
7610
7611 /* Release PCI resources and disable device's PCI function */
7612 scsi_host_put(shost);
7613 lpfc_disable_pci_dev(phba);
7614
7615 /* Finally, free the driver's device data structure */
7616 lpfc_hba_free(phba);
7617
7618 return;
7619}
7620
7621/**
7622 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
7623 * @pdev: pointer to PCI device
7624 * @msg: power management message
7625 *
7626 * This routine is called from the kernel's PCI subsystem to support system
7627 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7628 * this method, it quiesces the device by stopping the driver's worker
7629 * thread for the device, turning off device's interrupt and DMA, and bring
7630 * the device offline. Note that as the driver implements the minimum PM
7631 * requirements to a power-aware driver's PM support for suspend/resume -- all
7632 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7633 * method call will be treated as SUSPEND and the driver will fully
7634 * reinitialize its device during resume() method call, the driver will set
7635 * device to PCI_D3hot state in PCI config space instead of setting it
7636 * according to the @msg provided by the PM.
7637 *
7638 * Return code
7639 * 0 - driver suspended the device
7640 * Error otherwise
7641 **/
7642static int
7643lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
7644{
7645 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7646 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7647
7648 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7649 "0298 PCI device Power Management suspend.\n");
7650
7651 /* Bring down the device */
7652 lpfc_offline_prep(phba);
7653 lpfc_offline(phba);
7654 kthread_stop(phba->worker_thread);
7655
7656 /* Disable interrupt from device */
7657 lpfc_sli4_disable_intr(phba);
7658
7659 /* Save device state to PCI config space */
7660 pci_save_state(pdev);
7661 pci_set_power_state(pdev, PCI_D3hot);
7662
7663 return 0;
7664}
7665
7666/**
7667 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
7668 * @pdev: pointer to PCI device
7669 *
7670 * This routine is called from the kernel's PCI subsystem to support system
7671 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
7672 * this method, it restores the device's PCI config space state and fully
7673 * reinitializes the device and brings it online. Note that as the driver
7674 * implements the minimum PM requirements to a power-aware driver's PM for
7675 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7676 * to the suspend() method call will be treated as SUSPEND and the driver
7677 * will fully reinitialize its device during resume() method call, the device
7678 * will be set to PCI_D0 directly in PCI config space before restoring the
7679 * state.
7680 *
7681 * Return code
7682 * 0 - driver suspended the device
7683 * Error otherwise
7684 **/
7685static int
7686lpfc_pci_resume_one_s4(struct pci_dev *pdev)
7687{
7688 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7689 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7690 uint32_t intr_mode;
7691 int error;
7692
7693 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7694 "0292 PCI device Power Management resume.\n");
7695
7696 /* Restore device state from PCI config space */
7697 pci_set_power_state(pdev, PCI_D0);
7698 pci_restore_state(pdev);
7699 if (pdev->is_busmaster)
7700 pci_set_master(pdev);
7701
7702 /* Startup the kernel thread for this host adapter. */
7703 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7704 "lpfc_worker_%d", phba->brd_no);
7705 if (IS_ERR(phba->worker_thread)) {
7706 error = PTR_ERR(phba->worker_thread);
7707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7708 "0293 PM resume failed to start worker "
7709 "thread: error=x%x.\n", error);
7710 return error;
7711 }
7712
7713 /* Configure and enable interrupt */
7714 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
7715 if (intr_mode == LPFC_INTR_ERROR) {
7716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7717 "0294 PM resume Failed to enable interrupt\n");
7718 return -EIO;
7719 } else
7720 phba->intr_mode = intr_mode;
7721
7722 /* Restart HBA and bring it online */
7723 lpfc_sli_brdrestart(phba);
7724 lpfc_online(phba);
7725
7726 /* Log the current active interrupt mode */
7727 lpfc_log_intr_mode(phba, phba->intr_mode);
7728
7729 return 0;
7730}
7731
7732/**
7733 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
7734 * @pdev: pointer to PCI device.
7735 * @state: the current PCI connection state.
7736 *
7737 * This routine is called from the PCI subsystem for error handling to device
7738 * with SLI-4 interface spec. This function is called by the PCI subsystem
7739 * after a PCI bus error affecting this device has been detected. When this
7740 * function is invoked, it will need to stop all the I/Os and interrupt(s)
7741 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
7742 * for the PCI subsystem to perform proper recovery as desired.
7743 *
7744 * Return codes
7745 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7746 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7747 **/
7748static pci_ers_result_t
7749lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
7750{
7751 return PCI_ERS_RESULT_NEED_RESET;
7752}
7753
7754/**
7755 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
7756 * @pdev: pointer to PCI device.
7757 *
7758 * This routine is called from the PCI subsystem for error handling to device
7759 * with SLI-4 interface spec. It is called after PCI bus has been reset to
7760 * restart the PCI card from scratch, as if from a cold-boot. During the
7761 * PCI subsystem error recovery, after the driver returns
7762 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7763 * recovery and then call this routine before calling the .resume method to
7764 * recover the device. This function will initialize the HBA device, enable
7765 * the interrupt, but it will just put the HBA to offline state without
7766 * passing any I/O traffic.
7767 *
7768 * Return codes
7769 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7770 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7771 */
7772static pci_ers_result_t
7773lpfc_io_slot_reset_s4(struct pci_dev *pdev)
7774{
7775 return PCI_ERS_RESULT_RECOVERED;
7776}
7777
7778/**
7779 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
7780 * @pdev: pointer to PCI device
7781 *
7782 * This routine is called from the PCI subsystem for error handling to device
7783 * with SLI-4 interface spec. It is called when kernel error recovery tells
7784 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7785 * error recovery. After this call, traffic can start to flow from this device
7786 * again.
7787 **/
7788static void
7789lpfc_io_resume_s4(struct pci_dev *pdev)
7790{
7791 return;
7792}
7793
7794/**
James Smart3772a992009-05-22 14:50:54 -04007795 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
7796 * @pdev: pointer to PCI device
7797 * @pid: pointer to PCI device identifier
7798 *
7799 * This routine is to be registered to the kernel's PCI subsystem. When an
7800 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
7801 * at PCI device-specific information of the device and driver to see if the
7802 * driver state that it can support this kind of device. If the match is
7803 * successful, the driver core invokes this routine. This routine dispatches
7804 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
7805 * do all the initialization that it needs to do to handle the HBA device
7806 * properly.
7807 *
7808 * Return code
7809 * 0 - driver can claim the device
7810 * negative value - driver can not claim the device
7811 **/
7812static int __devinit
7813lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
7814{
7815 int rc;
James Smart8fa38512009-07-19 10:01:03 -04007816 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04007817
James Smart8fa38512009-07-19 10:01:03 -04007818 if (pci_read_config_dword(pdev, LPFC_SLIREV_CONF_WORD, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04007819 return -ENODEV;
7820
James Smart8fa38512009-07-19 10:01:03 -04007821 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
7822 (bf_get(lpfc_sli_intf_rev, &intf) == LPFC_SLIREV_CONF_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04007823 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007824 else
James Smart3772a992009-05-22 14:50:54 -04007825 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04007826
James Smart3772a992009-05-22 14:50:54 -04007827 return rc;
7828}
7829
7830/**
7831 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
7832 * @pdev: pointer to PCI device
7833 *
7834 * This routine is to be registered to the kernel's PCI subsystem. When an
7835 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
7836 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
7837 * remove routine, which will perform all the necessary cleanup for the
7838 * device to be removed from the PCI subsystem properly.
7839 **/
7840static void __devexit
7841lpfc_pci_remove_one(struct pci_dev *pdev)
7842{
7843 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7844 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7845
7846 switch (phba->pci_dev_grp) {
7847 case LPFC_PCI_DEV_LP:
7848 lpfc_pci_remove_one_s3(pdev);
7849 break;
James Smartda0436e2009-05-22 14:51:39 -04007850 case LPFC_PCI_DEV_OC:
7851 lpfc_pci_remove_one_s4(pdev);
7852 break;
James Smart3772a992009-05-22 14:50:54 -04007853 default:
7854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7855 "1424 Invalid PCI device group: 0x%x\n",
7856 phba->pci_dev_grp);
7857 break;
7858 }
7859 return;
7860}
7861
7862/**
7863 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
7864 * @pdev: pointer to PCI device
7865 * @msg: power management message
7866 *
7867 * This routine is to be registered to the kernel's PCI subsystem to support
7868 * system Power Management (PM). When PM invokes this method, it dispatches
7869 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
7870 * suspend the device.
7871 *
7872 * Return code
7873 * 0 - driver suspended the device
7874 * Error otherwise
7875 **/
7876static int
7877lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
7878{
7879 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7880 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7881 int rc = -ENODEV;
7882
7883 switch (phba->pci_dev_grp) {
7884 case LPFC_PCI_DEV_LP:
7885 rc = lpfc_pci_suspend_one_s3(pdev, msg);
7886 break;
James Smartda0436e2009-05-22 14:51:39 -04007887 case LPFC_PCI_DEV_OC:
7888 rc = lpfc_pci_suspend_one_s4(pdev, msg);
7889 break;
James Smart3772a992009-05-22 14:50:54 -04007890 default:
7891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7892 "1425 Invalid PCI device group: 0x%x\n",
7893 phba->pci_dev_grp);
7894 break;
7895 }
7896 return rc;
7897}
7898
7899/**
7900 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
7901 * @pdev: pointer to PCI device
7902 *
7903 * This routine is to be registered to the kernel's PCI subsystem to support
7904 * system Power Management (PM). When PM invokes this method, it dispatches
7905 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
7906 * resume the device.
7907 *
7908 * Return code
7909 * 0 - driver suspended the device
7910 * Error otherwise
7911 **/
7912static int
7913lpfc_pci_resume_one(struct pci_dev *pdev)
7914{
7915 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7916 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7917 int rc = -ENODEV;
7918
7919 switch (phba->pci_dev_grp) {
7920 case LPFC_PCI_DEV_LP:
7921 rc = lpfc_pci_resume_one_s3(pdev);
7922 break;
James Smartda0436e2009-05-22 14:51:39 -04007923 case LPFC_PCI_DEV_OC:
7924 rc = lpfc_pci_resume_one_s4(pdev);
7925 break;
James Smart3772a992009-05-22 14:50:54 -04007926 default:
7927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7928 "1426 Invalid PCI device group: 0x%x\n",
7929 phba->pci_dev_grp);
7930 break;
7931 }
7932 return rc;
7933}
7934
7935/**
7936 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
7937 * @pdev: pointer to PCI device.
7938 * @state: the current PCI connection state.
7939 *
7940 * This routine is registered to the PCI subsystem for error handling. This
7941 * function is called by the PCI subsystem after a PCI bus error affecting
7942 * this device has been detected. When this routine is invoked, it dispatches
7943 * the action to the proper SLI-3 or SLI-4 device error detected handling
7944 * routine, which will perform the proper error detected operation.
7945 *
7946 * Return codes
7947 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7948 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7949 **/
7950static pci_ers_result_t
7951lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7952{
7953 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7954 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7955 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7956
7957 switch (phba->pci_dev_grp) {
7958 case LPFC_PCI_DEV_LP:
7959 rc = lpfc_io_error_detected_s3(pdev, state);
7960 break;
James Smartda0436e2009-05-22 14:51:39 -04007961 case LPFC_PCI_DEV_OC:
7962 rc = lpfc_io_error_detected_s4(pdev, state);
7963 break;
James Smart3772a992009-05-22 14:50:54 -04007964 default:
7965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7966 "1427 Invalid PCI device group: 0x%x\n",
7967 phba->pci_dev_grp);
7968 break;
7969 }
7970 return rc;
7971}
7972
7973/**
7974 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
7975 * @pdev: pointer to PCI device.
7976 *
7977 * This routine is registered to the PCI subsystem for error handling. This
7978 * function is called after PCI bus has been reset to restart the PCI card
7979 * from scratch, as if from a cold-boot. When this routine is invoked, it
7980 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
7981 * routine, which will perform the proper device reset.
7982 *
7983 * Return codes
7984 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7985 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7986 **/
7987static pci_ers_result_t
7988lpfc_io_slot_reset(struct pci_dev *pdev)
7989{
7990 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7991 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7992 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7993
7994 switch (phba->pci_dev_grp) {
7995 case LPFC_PCI_DEV_LP:
7996 rc = lpfc_io_slot_reset_s3(pdev);
7997 break;
James Smartda0436e2009-05-22 14:51:39 -04007998 case LPFC_PCI_DEV_OC:
7999 rc = lpfc_io_slot_reset_s4(pdev);
8000 break;
James Smart3772a992009-05-22 14:50:54 -04008001 default:
8002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8003 "1428 Invalid PCI device group: 0x%x\n",
8004 phba->pci_dev_grp);
8005 break;
8006 }
8007 return rc;
8008}
8009
8010/**
8011 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8012 * @pdev: pointer to PCI device
8013 *
8014 * This routine is registered to the PCI subsystem for error handling. It
8015 * is called when kernel error recovery tells the lpfc driver that it is
8016 * OK to resume normal PCI operation after PCI bus error recovery. When
8017 * this routine is invoked, it dispatches the action to the proper SLI-3
8018 * or SLI-4 device io_resume routine, which will resume the device operation.
8019 **/
8020static void
8021lpfc_io_resume(struct pci_dev *pdev)
8022{
8023 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8024 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8025
8026 switch (phba->pci_dev_grp) {
8027 case LPFC_PCI_DEV_LP:
8028 lpfc_io_resume_s3(pdev);
8029 break;
James Smartda0436e2009-05-22 14:51:39 -04008030 case LPFC_PCI_DEV_OC:
8031 lpfc_io_resume_s4(pdev);
8032 break;
James Smart3772a992009-05-22 14:50:54 -04008033 default:
8034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8035 "1429 Invalid PCI device group: 0x%x\n",
8036 phba->pci_dev_grp);
8037 break;
8038 }
8039 return;
8040}
8041
dea31012005-04-17 16:05:31 -05008042static struct pci_device_id lpfc_id_table[] = {
8043 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8044 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008045 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8046 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008047 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8048 PCI_ANY_ID, PCI_ANY_ID, },
8049 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8050 PCI_ANY_ID, PCI_ANY_ID, },
8051 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8052 PCI_ANY_ID, PCI_ANY_ID, },
8053 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8054 PCI_ANY_ID, PCI_ANY_ID, },
8055 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8056 PCI_ANY_ID, PCI_ANY_ID, },
8057 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8058 PCI_ANY_ID, PCI_ANY_ID, },
8059 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8060 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008061 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8062 PCI_ANY_ID, PCI_ANY_ID, },
8063 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8064 PCI_ANY_ID, PCI_ANY_ID, },
8065 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8066 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008067 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8068 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008069 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8070 PCI_ANY_ID, PCI_ANY_ID, },
8071 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8072 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008073 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8074 PCI_ANY_ID, PCI_ANY_ID, },
8075 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8076 PCI_ANY_ID, PCI_ANY_ID, },
8077 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8078 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008079 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8080 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008081 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8082 PCI_ANY_ID, PCI_ANY_ID, },
8083 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8084 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008085 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8086 PCI_ANY_ID, PCI_ANY_ID, },
8087 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8088 PCI_ANY_ID, PCI_ANY_ID, },
8089 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8090 PCI_ANY_ID, PCI_ANY_ID, },
8091 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8092 PCI_ANY_ID, PCI_ANY_ID, },
8093 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8094 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008095 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8096 PCI_ANY_ID, PCI_ANY_ID, },
8097 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8098 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008099 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8100 PCI_ANY_ID, PCI_ANY_ID, },
8101 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8102 PCI_ANY_ID, PCI_ANY_ID, },
8103 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8104 PCI_ANY_ID, PCI_ANY_ID, },
8105 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8106 PCI_ANY_ID, PCI_ANY_ID, },
8107 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8108 PCI_ANY_ID, PCI_ANY_ID, },
8109 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8110 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008111 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8112 PCI_ANY_ID, PCI_ANY_ID, },
8113 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8114 PCI_ANY_ID, PCI_ANY_ID, },
8115 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8116 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008117 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8118 PCI_ANY_ID, PCI_ANY_ID, },
James Smart6669f9b2009-10-02 15:16:45 -04008119 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TS_BE3,
8120 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008121 { 0 }
8122};
8123
8124MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8125
Linas Vepstas8d63f372007-02-14 14:28:36 -06008126static struct pci_error_handlers lpfc_err_handler = {
8127 .error_detected = lpfc_io_error_detected,
8128 .slot_reset = lpfc_io_slot_reset,
8129 .resume = lpfc_io_resume,
8130};
8131
dea31012005-04-17 16:05:31 -05008132static struct pci_driver lpfc_driver = {
8133 .name = LPFC_DRIVER_NAME,
8134 .id_table = lpfc_id_table,
8135 .probe = lpfc_pci_probe_one,
8136 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008137 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008138 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008139 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008140};
8141
James Smarte59058c2008-08-24 21:49:00 -04008142/**
James Smart3621a712009-04-06 18:47:14 -04008143 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008144 *
8145 * This routine is to be invoked when the lpfc module is loaded into the
8146 * kernel. The special kernel macro module_init() is used to indicate the
8147 * role of this routine to the kernel as lpfc module entry point.
8148 *
8149 * Return codes
8150 * 0 - successful
8151 * -ENOMEM - FC attach transport failed
8152 * all others - failed
8153 */
dea31012005-04-17 16:05:31 -05008154static int __init
8155lpfc_init(void)
8156{
8157 int error = 0;
8158
8159 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008160 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008161
James Smart7ee5d432007-10-27 13:37:17 -04008162 if (lpfc_enable_npiv) {
8163 lpfc_transport_functions.vport_create = lpfc_vport_create;
8164 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8165 }
dea31012005-04-17 16:05:31 -05008166 lpfc_transport_template =
8167 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008168 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008169 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008170 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008171 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008172 fc_attach_transport(&lpfc_vport_transport_functions);
8173 if (lpfc_vport_transport_template == NULL) {
8174 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008175 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008176 }
James Smart7ee5d432007-10-27 13:37:17 -04008177 }
dea31012005-04-17 16:05:31 -05008178 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008179 if (error) {
dea31012005-04-17 16:05:31 -05008180 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008181 if (lpfc_enable_npiv)
8182 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008183 }
dea31012005-04-17 16:05:31 -05008184
8185 return error;
8186}
8187
James Smarte59058c2008-08-24 21:49:00 -04008188/**
James Smart3621a712009-04-06 18:47:14 -04008189 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008190 *
8191 * This routine is invoked when the lpfc module is removed from the kernel.
8192 * The special kernel macro module_exit() is used to indicate the role of
8193 * this routine to the kernel as lpfc module exit point.
8194 */
dea31012005-04-17 16:05:31 -05008195static void __exit
8196lpfc_exit(void)
8197{
8198 pci_unregister_driver(&lpfc_driver);
8199 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008200 if (lpfc_enable_npiv)
8201 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008202 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008203 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8204 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008205 (1L << _dump_buf_data_order), _dump_buf_data);
8206 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8207 }
8208
8209 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008210 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8211 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008212 (1L << _dump_buf_dif_order), _dump_buf_dif);
8213 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8214 }
dea31012005-04-17 16:05:31 -05008215}
8216
8217module_init(lpfc_init);
8218module_exit(lpfc_exit);
8219MODULE_LICENSE("GPL");
8220MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8221MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8222MODULE_VERSION("0:" LPFC_DRIVER_VERSION);