]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/lpfc/lpfc_init.c
[SCSI] lpfc 8.3.4: NPIV vport fixes
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / lpfc / lpfc_init.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
d8e93df1 4 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * 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. *
dea3101e
JB
20 *******************************************************************/
21
dea3101e
JB
22#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>
92d7f7b0 30#include <linux/ctype.h>
dea3101e 31
91886523 32#include <scsi/scsi.h>
dea3101e
JB
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport_fc.h>
36
da0436e9 37#include "lpfc_hw4.h"
dea3101e
JB
38#include "lpfc_hw.h"
39#include "lpfc_sli.h"
da0436e9 40#include "lpfc_sli4.h"
ea2151b4 41#include "lpfc_nl.h"
dea3101e
JB
42#include "lpfc_disc.h"
43#include "lpfc_scsi.h"
44#include "lpfc.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_crtn.h"
92d7f7b0 47#include "lpfc_vport.h"
dea3101e
JB
48#include "lpfc_version.h"
49
81301a9b
JS
50char *_dump_buf_data;
51unsigned long _dump_buf_data_order;
52char *_dump_buf_dif;
53unsigned long _dump_buf_dif_order;
54spinlock_t _dump_buf_lock;
55
dea3101e
JB
56static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
57static int lpfc_post_rcv_buf(struct lpfc_hba *);
da0436e9
JS
58static int lpfc_sli4_queue_create(struct lpfc_hba *);
59static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
60static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
61static int lpfc_setup_endian_order(struct lpfc_hba *);
62static int lpfc_sli4_read_config(struct lpfc_hba *);
63static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
64static void lpfc_free_sgl_list(struct lpfc_hba *);
65static int lpfc_init_sgl_list(struct lpfc_hba *);
66static int lpfc_init_active_sgl_array(struct lpfc_hba *);
67static void lpfc_free_active_sgl(struct lpfc_hba *);
68static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
69static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
70static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
71static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
72static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea3101e
JB
73
74static struct scsi_transport_template *lpfc_transport_template = NULL;
92d7f7b0 75static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea3101e
JB
76static DEFINE_IDR(lpfc_hba_index);
77
e59058c4 78/**
3621a710 79 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
e59058c4
JS
80 * @phba: pointer to lpfc hba data structure.
81 *
82 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
83 * mailbox command. It retrieves the revision information from the HBA and
84 * collects the Vital Product Data (VPD) about the HBA for preparing the
85 * configuration of the HBA.
86 *
87 * Return codes:
88 * 0 - success.
89 * -ERESTART - requests the SLI layer to reset the HBA and try again.
90 * Any other value - indicates an error.
91 **/
dea3101e 92int
2e0fef85 93lpfc_config_port_prep(struct lpfc_hba *phba)
dea3101e
JB
94{
95 lpfc_vpd_t *vp = &phba->vpd;
96 int i = 0, rc;
97 LPFC_MBOXQ_t *pmb;
98 MAILBOX_t *mb;
99 char *lpfc_vpd_data = NULL;
100 uint16_t offset = 0;
101 static char licensed[56] =
102 "key unlock for use with gnu public licensed code only\0";
65a29c16 103 static int init_key = 1;
dea3101e
JB
104
105 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
106 if (!pmb) {
2e0fef85 107 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
108 return -ENOMEM;
109 }
110
04c68496 111 mb = &pmb->u.mb;
2e0fef85 112 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
113
114 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
65a29c16
JS
115 if (init_key) {
116 uint32_t *ptext = (uint32_t *) licensed;
dea3101e 117
65a29c16
JS
118 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
119 *ptext = cpu_to_be32(*ptext);
120 init_key = 0;
121 }
dea3101e
JB
122
123 lpfc_read_nv(phba, pmb);
124 memset((char*)mb->un.varRDnvp.rsvd3, 0,
125 sizeof (mb->un.varRDnvp.rsvd3));
126 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
127 sizeof (licensed));
128
129 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
130
131 if (rc != MBX_SUCCESS) {
ed957684 132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
e8b62011 133 "0324 Config Port initialization "
dea3101e
JB
134 "error, mbxCmd x%x READ_NVPARM, "
135 "mbxStatus x%x\n",
dea3101e
JB
136 mb->mbxCommand, mb->mbxStatus);
137 mempool_free(pmb, phba->mbox_mem_pool);
138 return -ERESTART;
139 }
140 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
2e0fef85
JS
141 sizeof(phba->wwnn));
142 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
143 sizeof(phba->wwpn));
dea3101e
JB
144 }
145
92d7f7b0
JS
146 phba->sli3_options = 0x0;
147
dea3101e
JB
148 /* Setup and issue mailbox READ REV command */
149 lpfc_read_rev(phba, pmb);
150 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
151 if (rc != MBX_SUCCESS) {
ed957684 152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 153 "0439 Adapter failed to init, mbxCmd x%x "
dea3101e 154 "READ_REV, mbxStatus x%x\n",
dea3101e
JB
155 mb->mbxCommand, mb->mbxStatus);
156 mempool_free( pmb, phba->mbox_mem_pool);
157 return -ERESTART;
158 }
159
92d7f7b0 160
1de933f3
JSEC
161 /*
162 * The value of rr must be 1 since the driver set the cv field to 1.
163 * This setting requires the FW to set all revision fields.
dea3101e 164 */
1de933f3 165 if (mb->un.varRdRev.rr == 0) {
dea3101e 166 vp->rev.rBit = 0;
1de933f3 167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011
JS
168 "0440 Adapter failed to init, READ_REV has "
169 "missing revision information.\n");
dea3101e
JB
170 mempool_free(pmb, phba->mbox_mem_pool);
171 return -ERESTART;
dea3101e
JB
172 }
173
495a714c
JS
174 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
175 mempool_free(pmb, phba->mbox_mem_pool);
ed957684 176 return -EINVAL;
495a714c 177 }
ed957684 178
dea3101e 179 /* Save information as VPD data */
1de933f3 180 vp->rev.rBit = 1;
92d7f7b0 181 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
1de933f3
JSEC
182 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
183 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
184 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
185 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea3101e
JB
186 vp->rev.biuRev = mb->un.varRdRev.biuRev;
187 vp->rev.smRev = mb->un.varRdRev.smRev;
188 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
189 vp->rev.endecRev = mb->un.varRdRev.endecRev;
190 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
191 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
192 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
193 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
194 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
195 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
196
92d7f7b0
JS
197 /* If the sli feature level is less then 9, we must
198 * tear down all RPIs and VPIs on link down if NPIV
199 * is enabled.
200 */
201 if (vp->rev.feaLevelHigh < 9)
202 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
203
dea3101e
JB
204 if (lpfc_is_LC_HBA(phba->pcidev->device))
205 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
206 sizeof (phba->RandomData));
207
dea3101e 208 /* Get adapter VPD information */
dea3101e
JB
209 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
210 if (!lpfc_vpd_data)
d7c255b2 211 goto out_free_mbox;
dea3101e
JB
212
213 do {
a0c87cbd 214 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea3101e
JB
215 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
216
217 if (rc != MBX_SUCCESS) {
218 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 219 "0441 VPD not present on adapter, "
dea3101e 220 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea3101e 221 mb->mbxCommand, mb->mbxStatus);
74b72a59 222 mb->un.varDmp.word_cnt = 0;
dea3101e 223 }
04c68496
JS
224 /* dump mem may return a zero when finished or we got a
225 * mailbox error, either way we are done.
226 */
227 if (mb->un.varDmp.word_cnt == 0)
228 break;
74b72a59
JW
229 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
230 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
d7c255b2
JS
231 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
232 lpfc_vpd_data + offset,
92d7f7b0 233 mb->un.varDmp.word_cnt);
dea3101e 234 offset += mb->un.varDmp.word_cnt;
74b72a59
JW
235 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
236 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea3101e
JB
237
238 kfree(lpfc_vpd_data);
dea3101e
JB
239out_free_mbox:
240 mempool_free(pmb, phba->mbox_mem_pool);
241 return 0;
242}
243
e59058c4 244/**
3621a710 245 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
e59058c4
JS
246 * @phba: pointer to lpfc hba data structure.
247 * @pmboxq: pointer to the driver internal queue element for mailbox command.
248 *
249 * This is the completion handler for driver's configuring asynchronous event
250 * mailbox command to the device. If the mailbox command returns successfully,
251 * it will set internal async event support flag to 1; otherwise, it will
252 * set internal async event support flag to 0.
253 **/
57127f15
JS
254static void
255lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
256{
04c68496 257 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
57127f15
JS
258 phba->temp_sensor_support = 1;
259 else
260 phba->temp_sensor_support = 0;
261 mempool_free(pmboxq, phba->mbox_mem_pool);
262 return;
263}
264
97207482 265/**
3621a710 266 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
97207482
JS
267 * @phba: pointer to lpfc hba data structure.
268 * @pmboxq: pointer to the driver internal queue element for mailbox command.
269 *
270 * This is the completion handler for dump mailbox command for getting
271 * wake up parameters. When this command complete, the response contain
272 * Option rom version of the HBA. This function translate the version number
273 * into a human readable string and store it in OptionROMVersion.
274 **/
275static void
276lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
277{
278 struct prog_id *prg;
279 uint32_t prog_id_word;
280 char dist = ' ';
281 /* character array used for decoding dist type. */
282 char dist_char[] = "nabx";
283
04c68496 284 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
9f1e1b50 285 mempool_free(pmboxq, phba->mbox_mem_pool);
97207482 286 return;
9f1e1b50 287 }
97207482
JS
288
289 prg = (struct prog_id *) &prog_id_word;
290
291 /* word 7 contain option rom version */
04c68496 292 prog_id_word = pmboxq->u.mb.un.varWords[7];
97207482
JS
293
294 /* Decode the Option rom version word to a readable string */
295 if (prg->dist < 4)
296 dist = dist_char[prg->dist];
297
298 if ((prg->dist == 3) && (prg->num == 0))
299 sprintf(phba->OptionROMVersion, "%d.%d%d",
300 prg->ver, prg->rev, prg->lev);
301 else
302 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
303 prg->ver, prg->rev, prg->lev,
304 dist, prg->num);
9f1e1b50 305 mempool_free(pmboxq, phba->mbox_mem_pool);
97207482
JS
306 return;
307}
308
e59058c4 309/**
3621a710 310 * lpfc_config_port_post - Perform lpfc initialization after config port
e59058c4
JS
311 * @phba: pointer to lpfc hba data structure.
312 *
313 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
314 * command call. It performs all internal resource and state setups on the
315 * port: post IOCB buffers, enable appropriate host interrupt attentions,
316 * ELS ring timers, etc.
317 *
318 * Return codes
319 * 0 - success.
320 * Any other value - error.
321 **/
dea3101e 322int
2e0fef85 323lpfc_config_port_post(struct lpfc_hba *phba)
dea3101e 324{
2e0fef85 325 struct lpfc_vport *vport = phba->pport;
a257bf90 326 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e
JB
327 LPFC_MBOXQ_t *pmb;
328 MAILBOX_t *mb;
329 struct lpfc_dmabuf *mp;
330 struct lpfc_sli *psli = &phba->sli;
331 uint32_t status, timeout;
2e0fef85
JS
332 int i, j;
333 int rc;
dea3101e 334
7af67051
JS
335 spin_lock_irq(&phba->hbalock);
336 /*
337 * If the Config port completed correctly the HBA is not
338 * over heated any more.
339 */
340 if (phba->over_temp_state == HBA_OVER_TEMP)
341 phba->over_temp_state = HBA_NORMAL_TEMP;
342 spin_unlock_irq(&phba->hbalock);
343
dea3101e
JB
344 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
345 if (!pmb) {
2e0fef85 346 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
347 return -ENOMEM;
348 }
04c68496 349 mb = &pmb->u.mb;
dea3101e 350
dea3101e 351 /* Get login parameters for NID. */
92d7f7b0 352 lpfc_read_sparam(phba, pmb, 0);
ed957684 353 pmb->vport = vport;
dea3101e 354 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
ed957684 355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 356 "0448 Adapter failed init, mbxCmd x%x "
dea3101e 357 "READ_SPARM mbxStatus x%x\n",
dea3101e 358 mb->mbxCommand, mb->mbxStatus);
2e0fef85 359 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
360 mp = (struct lpfc_dmabuf *) pmb->context1;
361 mempool_free( pmb, phba->mbox_mem_pool);
362 lpfc_mbuf_free(phba, mp->virt, mp->phys);
363 kfree(mp);
364 return -EIO;
365 }
366
367 mp = (struct lpfc_dmabuf *) pmb->context1;
368
2e0fef85 369 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea3101e
JB
370 lpfc_mbuf_free(phba, mp->virt, mp->phys);
371 kfree(mp);
372 pmb->context1 = NULL;
373
a12e07bc 374 if (phba->cfg_soft_wwnn)
2e0fef85
JS
375 u64_to_wwn(phba->cfg_soft_wwnn,
376 vport->fc_sparam.nodeName.u.wwn);
c3f28afa 377 if (phba->cfg_soft_wwpn)
2e0fef85
JS
378 u64_to_wwn(phba->cfg_soft_wwpn,
379 vport->fc_sparam.portName.u.wwn);
380 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea3101e 381 sizeof (struct lpfc_name));
2e0fef85 382 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea3101e 383 sizeof (struct lpfc_name));
a257bf90
JS
384
385 /* Update the fc_host data structures with new wwn. */
386 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
387 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
21e9a0a5 388 fc_host_max_npiv_vports(shost) = phba->max_vpi;
a257bf90 389
dea3101e
JB
390 /* If no serial number in VPD data, use low 6 bytes of WWNN */
391 /* This should be consolidated into parse_vpd ? - mr */
392 if (phba->SerialNumber[0] == 0) {
393 uint8_t *outptr;
394
2e0fef85 395 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea3101e
JB
396 for (i = 0; i < 12; i++) {
397 status = *outptr++;
398 j = ((status & 0xf0) >> 4);
399 if (j <= 9)
400 phba->SerialNumber[i] =
401 (char)((uint8_t) 0x30 + (uint8_t) j);
402 else
403 phba->SerialNumber[i] =
404 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
405 i++;
406 j = (status & 0xf);
407 if (j <= 9)
408 phba->SerialNumber[i] =
409 (char)((uint8_t) 0x30 + (uint8_t) j);
410 else
411 phba->SerialNumber[i] =
412 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
413 }
414 }
415
dea3101e 416 lpfc_read_config(phba, pmb);
ed957684 417 pmb->vport = vport;
dea3101e 418 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
ed957684 419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 420 "0453 Adapter failed to init, mbxCmd x%x "
dea3101e 421 "READ_CONFIG, mbxStatus x%x\n",
dea3101e 422 mb->mbxCommand, mb->mbxStatus);
2e0fef85 423 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
424 mempool_free( pmb, phba->mbox_mem_pool);
425 return -EIO;
426 }
427
a0c87cbd
JS
428 /* Check if the port is disabled */
429 lpfc_sli_read_link_ste(phba);
430
dea3101e
JB
431 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
432 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
433 phba->cfg_hba_queue_depth =
f1126688
JS
434 (mb->un.varRdConfig.max_xri + 1) -
435 lpfc_sli4_get_els_iocb_cnt(phba);
dea3101e
JB
436
437 phba->lmt = mb->un.varRdConfig.lmt;
74b72a59
JW
438
439 /* Get the default values for Model Name and Description */
440 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
441
442 if ((phba->cfg_link_speed > LINK_SPEED_10G)
443 || ((phba->cfg_link_speed == LINK_SPEED_1G)
444 && !(phba->lmt & LMT_1Gb))
445 || ((phba->cfg_link_speed == LINK_SPEED_2G)
446 && !(phba->lmt & LMT_2Gb))
447 || ((phba->cfg_link_speed == LINK_SPEED_4G)
448 && !(phba->lmt & LMT_4Gb))
449 || ((phba->cfg_link_speed == LINK_SPEED_8G)
450 && !(phba->lmt & LMT_8Gb))
451 || ((phba->cfg_link_speed == LINK_SPEED_10G)
452 && !(phba->lmt & LMT_10Gb))) {
453 /* Reset link speed to auto */
ed957684 454 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
e8b62011 455 "1302 Invalid speed for this board: "
dea3101e 456 "Reset link speed to auto: x%x\n",
dea3101e
JB
457 phba->cfg_link_speed);
458 phba->cfg_link_speed = LINK_SPEED_AUTO;
459 }
460
2e0fef85 461 phba->link_state = LPFC_LINK_DOWN;
dea3101e 462
0b727fea 463 /* Only process IOCBs on ELS ring till hba_state is READY */
a4bc3379
JS
464 if (psli->ring[psli->extra_ring].cmdringaddr)
465 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea3101e
JB
466 if (psli->ring[psli->fcp_ring].cmdringaddr)
467 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
468 if (psli->ring[psli->next_ring].cmdringaddr)
469 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
470
471 /* Post receive buffers for desired rings */
ed957684
JS
472 if (phba->sli_rev != 3)
473 lpfc_post_rcv_buf(phba);
dea3101e 474
9399627f
JS
475 /*
476 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
477 */
478 if (phba->intr_type == MSIX) {
479 rc = lpfc_config_msi(phba, pmb);
480 if (rc) {
481 mempool_free(pmb, phba->mbox_mem_pool);
482 return -EIO;
483 }
484 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
485 if (rc != MBX_SUCCESS) {
486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
487 "0352 Config MSI mailbox command "
488 "failed, mbxCmd x%x, mbxStatus x%x\n",
04c68496
JS
489 pmb->u.mb.mbxCommand,
490 pmb->u.mb.mbxStatus);
9399627f
JS
491 mempool_free(pmb, phba->mbox_mem_pool);
492 return -EIO;
493 }
494 }
495
04c68496 496 spin_lock_irq(&phba->hbalock);
9399627f
JS
497 /* Initialize ERATT handling flag */
498 phba->hba_flag &= ~HBA_ERATT_HANDLED;
499
dea3101e 500 /* Enable appropriate host interrupts */
dea3101e
JB
501 status = readl(phba->HCregaddr);
502 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
503 if (psli->num_rings > 0)
504 status |= HC_R0INT_ENA;
505 if (psli->num_rings > 1)
506 status |= HC_R1INT_ENA;
507 if (psli->num_rings > 2)
508 status |= HC_R2INT_ENA;
509 if (psli->num_rings > 3)
510 status |= HC_R3INT_ENA;
511
875fbdfe
JSEC
512 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
513 (phba->cfg_poll & DISABLE_FCP_RING_INT))
9399627f 514 status &= ~(HC_R0INT_ENA);
875fbdfe 515
dea3101e
JB
516 writel(status, phba->HCregaddr);
517 readl(phba->HCregaddr); /* flush */
2e0fef85 518 spin_unlock_irq(&phba->hbalock);
dea3101e 519
9399627f
JS
520 /* Set up ring-0 (ELS) timer */
521 timeout = phba->fc_ratov * 2;
2e0fef85 522 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
9399627f 523 /* Set up heart beat (HB) timer */
858c9f6c
JS
524 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
525 phba->hb_outstanding = 0;
526 phba->last_completion_time = jiffies;
9399627f
JS
527 /* Set up error attention (ERATT) polling timer */
528 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea3101e 529
a0c87cbd
JS
530 if (phba->hba_flag & LINK_DISABLED) {
531 lpfc_printf_log(phba,
532 KERN_ERR, LOG_INIT,
533 "2598 Adapter Link is disabled.\n");
534 lpfc_down_link(phba, pmb);
535 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
536 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
537 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
538 lpfc_printf_log(phba,
539 KERN_ERR, LOG_INIT,
540 "2599 Adapter failed to issue DOWN_LINK"
541 " mbox command rc 0x%x\n", rc);
542
543 mempool_free(pmb, phba->mbox_mem_pool);
544 return -EIO;
545 }
546 } else {
547 lpfc_init_link(phba, pmb, phba->cfg_topology,
548 phba->cfg_link_speed);
549 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
550 lpfc_set_loopback_flag(phba);
551 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
552 if (rc != MBX_SUCCESS) {
553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 554 "0454 Adapter failed to init, mbxCmd x%x "
dea3101e 555 "INIT_LINK, mbxStatus x%x\n",
dea3101e
JB
556 mb->mbxCommand, mb->mbxStatus);
557
a0c87cbd
JS
558 /* Clear all interrupt enable conditions */
559 writel(0, phba->HCregaddr);
560 readl(phba->HCregaddr); /* flush */
561 /* Clear all pending interrupts */
562 writel(0xffffffff, phba->HAregaddr);
563 readl(phba->HAregaddr); /* flush */
dea3101e 564
a0c87cbd
JS
565 phba->link_state = LPFC_HBA_ERROR;
566 if (rc != MBX_BUSY)
567 mempool_free(pmb, phba->mbox_mem_pool);
568 return -EIO;
569 }
dea3101e
JB
570 }
571 /* MBOX buffer will be freed in mbox compl */
57127f15
JS
572 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
573 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
574 pmb->mbox_cmpl = lpfc_config_async_cmpl;
575 pmb->vport = phba->pport;
576 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea3101e 577
57127f15
JS
578 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
579 lpfc_printf_log(phba,
580 KERN_ERR,
581 LOG_INIT,
582 "0456 Adapter failed to issue "
583 "ASYNCEVT_ENABLE mbox status x%x \n.",
584 rc);
585 mempool_free(pmb, phba->mbox_mem_pool);
586 }
97207482
JS
587
588 /* Get Option rom version */
589 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
590 lpfc_dump_wakeup_param(phba, pmb);
591 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
592 pmb->vport = phba->pport;
593 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
594
595 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
597 "to get Option ROM version status x%x\n.", rc);
598 mempool_free(pmb, phba->mbox_mem_pool);
599 }
600
d7c255b2 601 return 0;
ce8b3ce5
JS
602}
603
e59058c4 604/**
3621a710 605 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
e59058c4
JS
606 * @phba: pointer to lpfc HBA data structure.
607 *
608 * This routine will do LPFC uninitialization before the HBA is reset when
609 * bringing down the SLI Layer.
610 *
611 * Return codes
612 * 0 - success.
613 * Any other value - error.
614 **/
dea3101e 615int
2e0fef85 616lpfc_hba_down_prep(struct lpfc_hba *phba)
dea3101e 617{
1b32f6aa
JS
618 struct lpfc_vport **vports;
619 int i;
3772a991
JS
620
621 if (phba->sli_rev <= LPFC_SLI_REV3) {
622 /* Disable interrupts */
623 writel(0, phba->HCregaddr);
624 readl(phba->HCregaddr); /* flush */
625 }
dea3101e 626
1b32f6aa
JS
627 if (phba->pport->load_flag & FC_UNLOADING)
628 lpfc_cleanup_discovery_resources(phba->pport);
629 else {
630 vports = lpfc_create_vport_work_array(phba);
631 if (vports != NULL)
3772a991
JS
632 for (i = 0; i <= phba->max_vports &&
633 vports[i] != NULL; i++)
1b32f6aa
JS
634 lpfc_cleanup_discovery_resources(vports[i]);
635 lpfc_destroy_vport_work_array(phba, vports);
7f5f3d0d
JS
636 }
637 return 0;
dea3101e
JB
638}
639
e59058c4 640/**
3772a991 641 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
e59058c4
JS
642 * @phba: pointer to lpfc HBA data structure.
643 *
644 * This routine will do uninitialization after the HBA is reset when bring
645 * down the SLI Layer.
646 *
647 * Return codes
648 * 0 - sucess.
649 * Any other value - error.
650 **/
3772a991
JS
651static int
652lpfc_hba_down_post_s3(struct lpfc_hba *phba)
41415862
JW
653{
654 struct lpfc_sli *psli = &phba->sli;
655 struct lpfc_sli_ring *pring;
656 struct lpfc_dmabuf *mp, *next_mp;
09372820 657 LIST_HEAD(completions);
41415862
JW
658 int i;
659
92d7f7b0
JS
660 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
661 lpfc_sli_hbqbuf_free_all(phba);
662 else {
663 /* Cleanup preposted buffers on the ELS ring */
664 pring = &psli->ring[LPFC_ELS_RING];
665 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
666 list_del(&mp->list);
667 pring->postbufq_cnt--;
668 lpfc_mbuf_free(phba, mp->virt, mp->phys);
669 kfree(mp);
670 }
41415862
JW
671 }
672
09372820 673 spin_lock_irq(&phba->hbalock);
41415862
JW
674 for (i = 0; i < psli->num_rings; i++) {
675 pring = &psli->ring[i];
09372820
JS
676
677 /* At this point in time the HBA is either reset or DOA. Either
678 * way, nothing should be on txcmplq as it will NEVER complete.
679 */
680 list_splice_init(&pring->txcmplq, &completions);
681 pring->txcmplq_cnt = 0;
682 spin_unlock_irq(&phba->hbalock);
683
a257bf90
JS
684 /* Cancel all the IOCBs from the completions list */
685 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
686 IOERR_SLI_ABORTED);
09372820 687
41415862 688 lpfc_sli_abort_iocb_ring(phba, pring);
09372820 689 spin_lock_irq(&phba->hbalock);
41415862 690 }
09372820 691 spin_unlock_irq(&phba->hbalock);
41415862
JW
692
693 return 0;
694}
da0436e9
JS
695/**
696 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
697 * @phba: pointer to lpfc HBA data structure.
698 *
699 * This routine will do uninitialization after the HBA is reset when bring
700 * down the SLI Layer.
701 *
702 * Return codes
703 * 0 - sucess.
704 * Any other value - error.
705 **/
706static int
707lpfc_hba_down_post_s4(struct lpfc_hba *phba)
708{
709 struct lpfc_scsi_buf *psb, *psb_next;
710 LIST_HEAD(aborts);
711 int ret;
712 unsigned long iflag = 0;
713 ret = lpfc_hba_down_post_s3(phba);
714 if (ret)
715 return ret;
716 /* At this point in time the HBA is either reset or DOA. Either
717 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
718 * on the lpfc_sgl_list so that it can either be freed if the
719 * driver is unloading or reposted if the driver is restarting
720 * the port.
721 */
722 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
723 /* scsl_buf_list */
724 /* abts_sgl_list_lock required because worker thread uses this
725 * list.
726 */
727 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
728 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
729 &phba->sli4_hba.lpfc_sgl_list);
730 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
731 /* abts_scsi_buf_list_lock required because worker thread uses this
732 * list.
733 */
734 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
735 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
736 &aborts);
737 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
738 spin_unlock_irq(&phba->hbalock);
739
740 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
741 psb->pCmd = NULL;
742 psb->status = IOSTAT_SUCCESS;
743 }
744 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
745 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
746 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
747 return 0;
748}
749
750/**
751 * lpfc_hba_down_post - Wrapper func for hba down post routine
752 * @phba: pointer to lpfc HBA data structure.
753 *
754 * This routine wraps the actual SLI3 or SLI4 routine for performing
755 * uninitialization after the HBA is reset when bring down the SLI Layer.
756 *
757 * Return codes
758 * 0 - sucess.
759 * Any other value - error.
760 **/
761int
762lpfc_hba_down_post(struct lpfc_hba *phba)
763{
764 return (*phba->lpfc_hba_down_post)(phba);
765}
41415862 766
e59058c4 767/**
3621a710 768 * lpfc_hb_timeout - The HBA-timer timeout handler
e59058c4
JS
769 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
770 *
771 * This is the HBA-timer timeout handler registered to the lpfc driver. When
772 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
773 * work-port-events bitmap and the worker thread is notified. This timeout
774 * event will be used by the worker thread to invoke the actual timeout
775 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
776 * be performed in the timeout handler and the HBA timeout event bit shall
777 * be cleared by the worker thread after it has taken the event bitmap out.
778 **/
a6ababd2 779static void
858c9f6c
JS
780lpfc_hb_timeout(unsigned long ptr)
781{
782 struct lpfc_hba *phba;
5e9d9b82 783 uint32_t tmo_posted;
858c9f6c
JS
784 unsigned long iflag;
785
786 phba = (struct lpfc_hba *)ptr;
9399627f
JS
787
788 /* Check for heart beat timeout conditions */
858c9f6c 789 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5e9d9b82
JS
790 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
791 if (!tmo_posted)
858c9f6c
JS
792 phba->pport->work_port_events |= WORKER_HB_TMO;
793 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
794
9399627f 795 /* Tell the worker thread there is work to do */
5e9d9b82
JS
796 if (!tmo_posted)
797 lpfc_worker_wake_up(phba);
858c9f6c
JS
798 return;
799}
800
e59058c4 801/**
3621a710 802 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
e59058c4
JS
803 * @phba: pointer to lpfc hba data structure.
804 * @pmboxq: pointer to the driver internal queue element for mailbox command.
805 *
806 * This is the callback function to the lpfc heart-beat mailbox command.
807 * If configured, the lpfc driver issues the heart-beat mailbox command to
808 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
809 * heart-beat mailbox command is issued, the driver shall set up heart-beat
810 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
811 * heart-beat outstanding state. Once the mailbox command comes back and
812 * no error conditions detected, the heart-beat mailbox command timer is
813 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
814 * state is cleared for the next heart-beat. If the timer expired with the
815 * heart-beat outstanding state set, the driver will put the HBA offline.
816 **/
858c9f6c
JS
817static void
818lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
819{
820 unsigned long drvr_flag;
821
822 spin_lock_irqsave(&phba->hbalock, drvr_flag);
823 phba->hb_outstanding = 0;
824 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
825
9399627f 826 /* Check and reset heart-beat timer is necessary */
858c9f6c
JS
827 mempool_free(pmboxq, phba->mbox_mem_pool);
828 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
829 !(phba->link_state == LPFC_HBA_ERROR) &&
51ef4c26 830 !(phba->pport->load_flag & FC_UNLOADING))
858c9f6c
JS
831 mod_timer(&phba->hb_tmofunc,
832 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
833 return;
834}
835
e59058c4 836/**
3621a710 837 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
e59058c4
JS
838 * @phba: pointer to lpfc hba data structure.
839 *
840 * This is the actual HBA-timer timeout handler to be invoked by the worker
841 * thread whenever the HBA timer fired and HBA-timeout event posted. This
842 * handler performs any periodic operations needed for the device. If such
843 * periodic event has already been attended to either in the interrupt handler
844 * or by processing slow-ring or fast-ring events within the HBA-timer
845 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
846 * the timer for the next timeout period. If lpfc heart-beat mailbox command
847 * is configured and there is no heart-beat mailbox command outstanding, a
848 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
849 * has been a heart-beat mailbox command outstanding, the HBA shall be put
850 * to offline.
851 **/
858c9f6c
JS
852void
853lpfc_hb_timeout_handler(struct lpfc_hba *phba)
854{
855 LPFC_MBOXQ_t *pmboxq;
0ff10d46 856 struct lpfc_dmabuf *buf_ptr;
858c9f6c
JS
857 int retval;
858 struct lpfc_sli *psli = &phba->sli;
0ff10d46 859 LIST_HEAD(completions);
858c9f6c
JS
860
861 if ((phba->link_state == LPFC_HBA_ERROR) ||
51ef4c26 862 (phba->pport->load_flag & FC_UNLOADING) ||
858c9f6c
JS
863 (phba->pport->fc_flag & FC_OFFLINE_MODE))
864 return;
865
866 spin_lock_irq(&phba->pport->work_port_lock);
858c9f6c
JS
867
868 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
869 jiffies)) {
870 spin_unlock_irq(&phba->pport->work_port_lock);
871 if (!phba->hb_outstanding)
872 mod_timer(&phba->hb_tmofunc,
873 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
874 else
875 mod_timer(&phba->hb_tmofunc,
876 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
877 return;
878 }
879 spin_unlock_irq(&phba->pport->work_port_lock);
880
0ff10d46
JS
881 if (phba->elsbuf_cnt &&
882 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
883 spin_lock_irq(&phba->hbalock);
884 list_splice_init(&phba->elsbuf, &completions);
885 phba->elsbuf_cnt = 0;
886 phba->elsbuf_prev_cnt = 0;
887 spin_unlock_irq(&phba->hbalock);
888
889 while (!list_empty(&completions)) {
890 list_remove_head(&completions, buf_ptr,
891 struct lpfc_dmabuf, list);
892 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
893 kfree(buf_ptr);
894 }
895 }
896 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
897
858c9f6c 898 /* If there is no heart beat outstanding, issue a heartbeat command */
13815c83
JS
899 if (phba->cfg_enable_hba_heartbeat) {
900 if (!phba->hb_outstanding) {
901 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
902 if (!pmboxq) {
903 mod_timer(&phba->hb_tmofunc,
904 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
905 return;
906 }
858c9f6c 907
13815c83
JS
908 lpfc_heart_beat(phba, pmboxq);
909 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
910 pmboxq->vport = phba->pport;
911 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
858c9f6c 912
13815c83
JS
913 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
914 mempool_free(pmboxq, phba->mbox_mem_pool);
915 mod_timer(&phba->hb_tmofunc,
916 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
917 return;
918 }
858c9f6c 919 mod_timer(&phba->hb_tmofunc,
13815c83
JS
920 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
921 phba->hb_outstanding = 1;
858c9f6c 922 return;
13815c83
JS
923 } else {
924 /*
925 * If heart beat timeout called with hb_outstanding set
926 * we need to take the HBA offline.
927 */
928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
929 "0459 Adapter heartbeat failure, "
930 "taking this port offline.\n");
931
932 spin_lock_irq(&phba->hbalock);
f4b4c68f 933 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
13815c83
JS
934 spin_unlock_irq(&phba->hbalock);
935
936 lpfc_offline_prep(phba);
937 lpfc_offline(phba);
938 lpfc_unblock_mgmt_io(phba);
939 phba->link_state = LPFC_HBA_ERROR;
940 lpfc_hba_down_post(phba);
858c9f6c 941 }
858c9f6c
JS
942 }
943}
944
e59058c4 945/**
3621a710 946 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
e59058c4
JS
947 * @phba: pointer to lpfc hba data structure.
948 *
949 * This routine is called to bring the HBA offline when HBA hardware error
950 * other than Port Error 6 has been detected.
951 **/
09372820
JS
952static void
953lpfc_offline_eratt(struct lpfc_hba *phba)
954{
955 struct lpfc_sli *psli = &phba->sli;
956
957 spin_lock_irq(&phba->hbalock);
f4b4c68f 958 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
09372820
JS
959 spin_unlock_irq(&phba->hbalock);
960 lpfc_offline_prep(phba);
961
962 lpfc_offline(phba);
963 lpfc_reset_barrier(phba);
f4b4c68f 964 spin_lock_irq(&phba->hbalock);
09372820 965 lpfc_sli_brdreset(phba);
f4b4c68f 966 spin_unlock_irq(&phba->hbalock);
09372820
JS
967 lpfc_hba_down_post(phba);
968 lpfc_sli_brdready(phba, HS_MBRDY);
969 lpfc_unblock_mgmt_io(phba);
970 phba->link_state = LPFC_HBA_ERROR;
971 return;
972}
973
da0436e9
JS
974/**
975 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
976 * @phba: pointer to lpfc hba data structure.
977 *
978 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
979 * other than Port Error 6 has been detected.
980 **/
981static void
982lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
983{
984 lpfc_offline_prep(phba);
985 lpfc_offline(phba);
986 lpfc_sli4_brdreset(phba);
987 lpfc_hba_down_post(phba);
988 lpfc_sli4_post_status_check(phba);
989 lpfc_unblock_mgmt_io(phba);
990 phba->link_state = LPFC_HBA_ERROR;
991}
992
a257bf90
JS
993/**
994 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
995 * @phba: pointer to lpfc hba data structure.
996 *
997 * This routine is invoked to handle the deferred HBA hardware error
998 * conditions. This type of error is indicated by HBA by setting ER1
999 * and another ER bit in the host status register. The driver will
1000 * wait until the ER1 bit clears before handling the error condition.
1001 **/
1002static void
1003lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1004{
1005 uint32_t old_host_status = phba->work_hs;
1006 struct lpfc_sli_ring *pring;
1007 struct lpfc_sli *psli = &phba->sli;
1008
f4b4c68f
JS
1009 /* If the pci channel is offline, ignore possible errors,
1010 * since we cannot communicate with the pci card anyway.
1011 */
1012 if (pci_channel_offline(phba->pcidev)) {
1013 spin_lock_irq(&phba->hbalock);
1014 phba->hba_flag &= ~DEFER_ERATT;
1015 spin_unlock_irq(&phba->hbalock);
1016 return;
1017 }
1018
a257bf90
JS
1019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1020 "0479 Deferred Adapter Hardware Error "
1021 "Data: x%x x%x x%x\n",
1022 phba->work_hs,
1023 phba->work_status[0], phba->work_status[1]);
1024
1025 spin_lock_irq(&phba->hbalock);
f4b4c68f 1026 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
a257bf90
JS
1027 spin_unlock_irq(&phba->hbalock);
1028
1029
1030 /*
1031 * Firmware stops when it triggred erratt. That could cause the I/Os
1032 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1033 * SCSI layer retry it after re-establishing link.
1034 */
1035 pring = &psli->ring[psli->fcp_ring];
1036 lpfc_sli_abort_iocb_ring(phba, pring);
1037
1038 /*
1039 * There was a firmware error. Take the hba offline and then
1040 * attempt to restart it.
1041 */
1042 lpfc_offline_prep(phba);
1043 lpfc_offline(phba);
1044
1045 /* Wait for the ER1 bit to clear.*/
1046 while (phba->work_hs & HS_FFER1) {
1047 msleep(100);
1048 phba->work_hs = readl(phba->HSregaddr);
1049 /* If driver is unloading let the worker thread continue */
1050 if (phba->pport->load_flag & FC_UNLOADING) {
1051 phba->work_hs = 0;
1052 break;
1053 }
1054 }
1055
1056 /*
1057 * This is to ptrotect against a race condition in which
1058 * first write to the host attention register clear the
1059 * host status register.
1060 */
1061 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1062 phba->work_hs = old_host_status & ~HS_FFER1;
1063
3772a991 1064 spin_lock_irq(&phba->hbalock);
a257bf90 1065 phba->hba_flag &= ~DEFER_ERATT;
3772a991 1066 spin_unlock_irq(&phba->hbalock);
a257bf90
JS
1067 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1068 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1069}
1070
3772a991
JS
1071static void
1072lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1073{
1074 struct lpfc_board_event_header board_event;
1075 struct Scsi_Host *shost;
1076
1077 board_event.event_type = FC_REG_BOARD_EVENT;
1078 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1079 shost = lpfc_shost_from_vport(phba->pport);
1080 fc_host_post_vendor_event(shost, fc_get_event_number(),
1081 sizeof(board_event),
1082 (char *) &board_event,
1083 LPFC_NL_VENDOR_ID);
1084}
1085
e59058c4 1086/**
3772a991 1087 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
e59058c4
JS
1088 * @phba: pointer to lpfc hba data structure.
1089 *
1090 * This routine is invoked to handle the following HBA hardware error
1091 * conditions:
1092 * 1 - HBA error attention interrupt
1093 * 2 - DMA ring index out of range
1094 * 3 - Mailbox command came back as unknown
1095 **/
3772a991
JS
1096static void
1097lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea3101e 1098{
2e0fef85 1099 struct lpfc_vport *vport = phba->pport;
2e0fef85 1100 struct lpfc_sli *psli = &phba->sli;
dea3101e 1101 struct lpfc_sli_ring *pring;
d2873e4c 1102 uint32_t event_data;
57127f15
JS
1103 unsigned long temperature;
1104 struct temp_event temp_event_data;
92d7f7b0 1105 struct Scsi_Host *shost;
2e0fef85 1106
8d63f375 1107 /* If the pci channel is offline, ignore possible errors,
3772a991
JS
1108 * since we cannot communicate with the pci card anyway.
1109 */
1110 if (pci_channel_offline(phba->pcidev)) {
1111 spin_lock_irq(&phba->hbalock);
1112 phba->hba_flag &= ~DEFER_ERATT;
1113 spin_unlock_irq(&phba->hbalock);
8d63f375 1114 return;
3772a991
JS
1115 }
1116
13815c83
JS
1117 /* If resets are disabled then leave the HBA alone and return */
1118 if (!phba->cfg_enable_hba_reset)
1119 return;
dea3101e 1120
ea2151b4 1121 /* Send an internal error event to mgmt application */
3772a991 1122 lpfc_board_errevt_to_mgmt(phba);
ea2151b4 1123
a257bf90
JS
1124 if (phba->hba_flag & DEFER_ERATT)
1125 lpfc_handle_deferred_eratt(phba);
1126
97eab634 1127 if (phba->work_hs & HS_FFER6) {
dea3101e
JB
1128 /* Re-establishing Link */
1129 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
e8b62011 1130 "1301 Re-establishing Link "
dea3101e 1131 "Data: x%x x%x x%x\n",
e8b62011 1132 phba->work_hs,
dea3101e 1133 phba->work_status[0], phba->work_status[1]);
58da1ffb 1134
92d7f7b0 1135 spin_lock_irq(&phba->hbalock);
f4b4c68f 1136 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
92d7f7b0 1137 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
1138
1139 /*
1140 * Firmware stops when it triggled erratt with HS_FFER6.
1141 * That could cause the I/Os dropped by the firmware.
1142 * Error iocb (I/O) on txcmplq and let the SCSI layer
1143 * retry it after re-establishing link.
1144 */
1145 pring = &psli->ring[psli->fcp_ring];
1146 lpfc_sli_abort_iocb_ring(phba, pring);
1147
dea3101e
JB
1148 /*
1149 * There was a firmware error. Take the hba offline and then
1150 * attempt to restart it.
1151 */
46fa311e 1152 lpfc_offline_prep(phba);
dea3101e 1153 lpfc_offline(phba);
41415862 1154 lpfc_sli_brdrestart(phba);
dea3101e 1155 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
46fa311e 1156 lpfc_unblock_mgmt_io(phba);
dea3101e
JB
1157 return;
1158 }
46fa311e 1159 lpfc_unblock_mgmt_io(phba);
57127f15
JS
1160 } else if (phba->work_hs & HS_CRIT_TEMP) {
1161 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1162 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1163 temp_event_data.event_code = LPFC_CRIT_TEMP;
1164 temp_event_data.data = (uint32_t)temperature;
1165
1166 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 1167 "0406 Adapter maximum temperature exceeded "
57127f15
JS
1168 "(%ld), taking this port offline "
1169 "Data: x%x x%x x%x\n",
1170 temperature, phba->work_hs,
1171 phba->work_status[0], phba->work_status[1]);
1172
1173 shost = lpfc_shost_from_vport(phba->pport);
1174 fc_host_post_vendor_event(shost, fc_get_event_number(),
1175 sizeof(temp_event_data),
1176 (char *) &temp_event_data,
1177 SCSI_NL_VID_TYPE_PCI
1178 | PCI_VENDOR_ID_EMULEX);
1179
7af67051 1180 spin_lock_irq(&phba->hbalock);
7af67051
JS
1181 phba->over_temp_state = HBA_OVER_TEMP;
1182 spin_unlock_irq(&phba->hbalock);
09372820 1183 lpfc_offline_eratt(phba);
57127f15 1184
dea3101e
JB
1185 } else {
1186 /* The if clause above forces this code path when the status
9399627f
JS
1187 * failure is a value other than FFER6. Do not call the offline
1188 * twice. This is the adapter hardware error path.
dea3101e
JB
1189 */
1190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1191 "0457 Adapter Hardware Error "
dea3101e 1192 "Data: x%x x%x x%x\n",
e8b62011 1193 phba->work_hs,
dea3101e
JB
1194 phba->work_status[0], phba->work_status[1]);
1195
d2873e4c 1196 event_data = FC_REG_DUMP_EVENT;
92d7f7b0 1197 shost = lpfc_shost_from_vport(vport);
2e0fef85 1198 fc_host_post_vendor_event(shost, fc_get_event_number(),
d2873e4c
JS
1199 sizeof(event_data), (char *) &event_data,
1200 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1201
09372820 1202 lpfc_offline_eratt(phba);
dea3101e 1203 }
9399627f 1204 return;
dea3101e
JB
1205}
1206
da0436e9
JS
1207/**
1208 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1209 * @phba: pointer to lpfc hba data structure.
1210 *
1211 * This routine is invoked to handle the SLI4 HBA hardware error attention
1212 * conditions.
1213 **/
1214static void
1215lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1216{
1217 struct lpfc_vport *vport = phba->pport;
1218 uint32_t event_data;
1219 struct Scsi_Host *shost;
1220
1221 /* If the pci channel is offline, ignore possible errors, since
1222 * we cannot communicate with the pci card anyway.
1223 */
1224 if (pci_channel_offline(phba->pcidev))
1225 return;
1226 /* If resets are disabled then leave the HBA alone and return */
1227 if (!phba->cfg_enable_hba_reset)
1228 return;
1229
1230 /* Send an internal error event to mgmt application */
1231 lpfc_board_errevt_to_mgmt(phba);
1232
1233 /* For now, the actual action for SLI4 device handling is not
1234 * specified yet, just treated it as adaptor hardware failure
1235 */
1236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1237 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1238 phba->work_status[0], phba->work_status[1]);
1239
1240 event_data = FC_REG_DUMP_EVENT;
1241 shost = lpfc_shost_from_vport(vport);
1242 fc_host_post_vendor_event(shost, fc_get_event_number(),
1243 sizeof(event_data), (char *) &event_data,
1244 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1245
1246 lpfc_sli4_offline_eratt(phba);
1247}
1248
1249/**
1250 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1251 * @phba: pointer to lpfc HBA data structure.
1252 *
1253 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1254 * routine from the API jump table function pointer from the lpfc_hba struct.
1255 *
1256 * Return codes
1257 * 0 - sucess.
1258 * Any other value - error.
1259 **/
1260void
1261lpfc_handle_eratt(struct lpfc_hba *phba)
1262{
1263 (*phba->lpfc_handle_eratt)(phba);
1264}
1265
e59058c4 1266/**
3621a710 1267 * lpfc_handle_latt - The HBA link event handler
e59058c4
JS
1268 * @phba: pointer to lpfc hba data structure.
1269 *
1270 * This routine is invoked from the worker thread to handle a HBA host
1271 * attention link event.
1272 **/
dea3101e 1273void
2e0fef85 1274lpfc_handle_latt(struct lpfc_hba *phba)
dea3101e 1275{
2e0fef85
JS
1276 struct lpfc_vport *vport = phba->pport;
1277 struct lpfc_sli *psli = &phba->sli;
dea3101e
JB
1278 LPFC_MBOXQ_t *pmb;
1279 volatile uint32_t control;
1280 struct lpfc_dmabuf *mp;
09372820 1281 int rc = 0;
dea3101e
JB
1282
1283 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
09372820
JS
1284 if (!pmb) {
1285 rc = 1;
dea3101e 1286 goto lpfc_handle_latt_err_exit;
09372820 1287 }
dea3101e
JB
1288
1289 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
09372820
JS
1290 if (!mp) {
1291 rc = 2;
dea3101e 1292 goto lpfc_handle_latt_free_pmb;
09372820 1293 }
dea3101e
JB
1294
1295 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
09372820
JS
1296 if (!mp->virt) {
1297 rc = 3;
dea3101e 1298 goto lpfc_handle_latt_free_mp;
09372820 1299 }
dea3101e 1300
6281bfe0 1301 /* Cleanup any outstanding ELS commands */
549e55cd 1302 lpfc_els_flush_all_cmd(phba);
dea3101e
JB
1303
1304 psli->slistat.link_event++;
1305 lpfc_read_la(phba, pmb, mp);
1306 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
2e0fef85 1307 pmb->vport = vport;
0d2b6b83
JS
1308 /* Block ELS IOCBs until we have processed this mbox command */
1309 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
0b727fea 1310 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
09372820
JS
1311 if (rc == MBX_NOT_FINISHED) {
1312 rc = 4;
14691150 1313 goto lpfc_handle_latt_free_mbuf;
09372820 1314 }
dea3101e
JB
1315
1316 /* Clear Link Attention in HA REG */
2e0fef85 1317 spin_lock_irq(&phba->hbalock);
dea3101e
JB
1318 writel(HA_LATT, phba->HAregaddr);
1319 readl(phba->HAregaddr); /* flush */
2e0fef85 1320 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
1321
1322 return;
1323
14691150 1324lpfc_handle_latt_free_mbuf:
0d2b6b83 1325 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
14691150 1326 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea3101e
JB
1327lpfc_handle_latt_free_mp:
1328 kfree(mp);
1329lpfc_handle_latt_free_pmb:
1dcb58e5 1330 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e
JB
1331lpfc_handle_latt_err_exit:
1332 /* Enable Link attention interrupts */
2e0fef85 1333 spin_lock_irq(&phba->hbalock);
dea3101e
JB
1334 psli->sli_flag |= LPFC_PROCESS_LA;
1335 control = readl(phba->HCregaddr);
1336 control |= HC_LAINT_ENA;
1337 writel(control, phba->HCregaddr);
1338 readl(phba->HCregaddr); /* flush */
1339
1340 /* Clear Link Attention in HA REG */
1341 writel(HA_LATT, phba->HAregaddr);
1342 readl(phba->HAregaddr); /* flush */
2e0fef85 1343 spin_unlock_irq(&phba->hbalock);
dea3101e 1344 lpfc_linkdown(phba);
2e0fef85 1345 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1346
09372820
JS
1347 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1348 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea3101e
JB
1349
1350 return;
1351}
1352
e59058c4 1353/**
3621a710 1354 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
e59058c4
JS
1355 * @phba: pointer to lpfc hba data structure.
1356 * @vpd: pointer to the vital product data.
1357 * @len: length of the vital product data in bytes.
1358 *
1359 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1360 * an array of characters. In this routine, the ModelName, ProgramType, and
1361 * ModelDesc, etc. fields of the phba data structure will be populated.
1362 *
1363 * Return codes
1364 * 0 - pointer to the VPD passed in is NULL
1365 * 1 - success
1366 **/
3772a991 1367int
2e0fef85 1368lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea3101e
JB
1369{
1370 uint8_t lenlo, lenhi;
07da60c1 1371 int Length;
dea3101e
JB
1372 int i, j;
1373 int finished = 0;
1374 int index = 0;
1375
1376 if (!vpd)
1377 return 0;
1378
1379 /* Vital Product */
ed957684 1380 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 1381 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea3101e
JB
1382 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1383 (uint32_t) vpd[3]);
74b72a59 1384 while (!finished && (index < (len - 4))) {
dea3101e
JB
1385 switch (vpd[index]) {
1386 case 0x82:
74b72a59 1387 case 0x91:
dea3101e
JB
1388 index += 1;
1389 lenlo = vpd[index];
1390 index += 1;
1391 lenhi = vpd[index];
1392 index += 1;
1393 i = ((((unsigned short)lenhi) << 8) + lenlo);
1394 index += i;
1395 break;
1396 case 0x90:
1397 index += 1;
1398 lenlo = vpd[index];
1399 index += 1;
1400 lenhi = vpd[index];
1401 index += 1;
1402 Length = ((((unsigned short)lenhi) << 8) + lenlo);
74b72a59
JW
1403 if (Length > len - index)
1404 Length = len - index;
dea3101e
JB
1405 while (Length > 0) {
1406 /* Look for Serial Number */
1407 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1408 index += 2;
1409 i = vpd[index];
1410 index += 1;
1411 j = 0;
1412 Length -= (3+i);
1413 while(i--) {
1414 phba->SerialNumber[j++] = vpd[index++];
1415 if (j == 31)
1416 break;
1417 }
1418 phba->SerialNumber[j] = 0;
1419 continue;
1420 }
1421 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1422 phba->vpd_flag |= VPD_MODEL_DESC;
1423 index += 2;
1424 i = vpd[index];
1425 index += 1;
1426 j = 0;
1427 Length -= (3+i);
1428 while(i--) {
1429 phba->ModelDesc[j++] = vpd[index++];
1430 if (j == 255)
1431 break;
1432 }
1433 phba->ModelDesc[j] = 0;
1434 continue;
1435 }
1436 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1437 phba->vpd_flag |= VPD_MODEL_NAME;
1438 index += 2;
1439 i = vpd[index];
1440 index += 1;
1441 j = 0;
1442 Length -= (3+i);
1443 while(i--) {
1444 phba->ModelName[j++] = vpd[index++];
1445 if (j == 79)
1446 break;
1447 }
1448 phba->ModelName[j] = 0;
1449 continue;
1450 }
1451 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1452 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1453 index += 2;
1454 i = vpd[index];
1455 index += 1;
1456 j = 0;
1457 Length -= (3+i);
1458 while(i--) {
1459 phba->ProgramType[j++] = vpd[index++];
1460 if (j == 255)
1461 break;
1462 }
1463 phba->ProgramType[j] = 0;
1464 continue;
1465 }
1466 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1467 phba->vpd_flag |= VPD_PORT;
1468 index += 2;
1469 i = vpd[index];
1470 index += 1;
1471 j = 0;
1472 Length -= (3+i);
1473 while(i--) {
1474 phba->Port[j++] = vpd[index++];
1475 if (j == 19)
1476 break;
1477 }
1478 phba->Port[j] = 0;
1479 continue;
1480 }
1481 else {
1482 index += 2;
1483 i = vpd[index];
1484 index += 1;
1485 index += i;
1486 Length -= (3 + i);
1487 }
1488 }
1489 finished = 0;
1490 break;
1491 case 0x78:
1492 finished = 1;
1493 break;
1494 default:
1495 index ++;
1496 break;
1497 }
74b72a59 1498 }
dea3101e
JB
1499
1500 return(1);
1501}
1502
e59058c4 1503/**
3621a710 1504 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
e59058c4
JS
1505 * @phba: pointer to lpfc hba data structure.
1506 * @mdp: pointer to the data structure to hold the derived model name.
1507 * @descp: pointer to the data structure to hold the derived description.
1508 *
1509 * This routine retrieves HBA's description based on its registered PCI device
1510 * ID. The @descp passed into this function points to an array of 256 chars. It
1511 * shall be returned with the model name, maximum speed, and the host bus type.
1512 * The @mdp passed into this function points to an array of 80 chars. When the
1513 * function returns, the @mdp will be filled with the model name.
1514 **/
dea3101e 1515static void
2e0fef85 1516lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea3101e
JB
1517{
1518 lpfc_vpd_t *vp;
fefcb2b6 1519 uint16_t dev_id = phba->pcidev->device;
74b72a59 1520 int max_speed;
84774a4d 1521 int GE = 0;
da0436e9 1522 int oneConnect = 0; /* default is not a oneConnect */
74b72a59
JW
1523 struct {
1524 char * name;
1525 int max_speed;
74b72a59 1526 char * bus;
18a3b596 1527 } m = {"<Unknown>", 0, ""};
74b72a59
JW
1528
1529 if (mdp && mdp[0] != '\0'
1530 && descp && descp[0] != '\0')
1531 return;
1532
1533 if (phba->lmt & LMT_10Gb)
1534 max_speed = 10;
1535 else if (phba->lmt & LMT_8Gb)
1536 max_speed = 8;
1537 else if (phba->lmt & LMT_4Gb)
1538 max_speed = 4;
1539 else if (phba->lmt & LMT_2Gb)
1540 max_speed = 2;
1541 else
1542 max_speed = 1;
dea3101e
JB
1543
1544 vp = &phba->vpd;
dea3101e 1545
e4adb204 1546 switch (dev_id) {
06325e74 1547 case PCI_DEVICE_ID_FIREFLY:
18a3b596 1548 m = (typeof(m)){"LP6000", max_speed, "PCI"};
06325e74 1549 break;
dea3101e
JB
1550 case PCI_DEVICE_ID_SUPERFLY:
1551 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
18a3b596 1552 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea3101e 1553 else
18a3b596 1554 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea3101e
JB
1555 break;
1556 case PCI_DEVICE_ID_DRAGONFLY:
18a3b596 1557 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea3101e
JB
1558 break;
1559 case PCI_DEVICE_ID_CENTAUR:
1560 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
18a3b596 1561 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea3101e 1562 else
18a3b596 1563 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea3101e
JB
1564 break;
1565 case PCI_DEVICE_ID_RFLY:
18a3b596 1566 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea3101e
JB
1567 break;
1568 case PCI_DEVICE_ID_PEGASUS:
18a3b596 1569 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea3101e
JB
1570 break;
1571 case PCI_DEVICE_ID_THOR:
18a3b596 1572 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea3101e
JB
1573 break;
1574 case PCI_DEVICE_ID_VIPER:
18a3b596 1575 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea3101e
JB
1576 break;
1577 case PCI_DEVICE_ID_PFLY:
18a3b596 1578 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea3101e
JB
1579 break;
1580 case PCI_DEVICE_ID_TFLY:
18a3b596 1581 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea3101e
JB
1582 break;
1583 case PCI_DEVICE_ID_HELIOS:
18a3b596 1584 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea3101e 1585 break;
e4adb204 1586 case PCI_DEVICE_ID_HELIOS_SCSP:
18a3b596 1587 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
e4adb204
JSEC
1588 break;
1589 case PCI_DEVICE_ID_HELIOS_DCSP:
18a3b596 1590 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
e4adb204
JSEC
1591 break;
1592 case PCI_DEVICE_ID_NEPTUNE:
18a3b596 1593 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
e4adb204
JSEC
1594 break;
1595 case PCI_DEVICE_ID_NEPTUNE_SCSP:
18a3b596 1596 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
e4adb204
JSEC
1597 break;
1598 case PCI_DEVICE_ID_NEPTUNE_DCSP:
18a3b596 1599 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
e4adb204 1600 break;
dea3101e 1601 case PCI_DEVICE_ID_BMID:
18a3b596 1602 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea3101e
JB
1603 break;
1604 case PCI_DEVICE_ID_BSMB:
18a3b596 1605 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea3101e
JB
1606 break;
1607 case PCI_DEVICE_ID_ZEPHYR:
18a3b596 1608 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea3101e 1609 break;
e4adb204 1610 case PCI_DEVICE_ID_ZEPHYR_SCSP:
18a3b596 1611 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
e4adb204
JSEC
1612 break;
1613 case PCI_DEVICE_ID_ZEPHYR_DCSP:
a257bf90
JS
1614 m = (typeof(m)){"LP2105", max_speed, "PCIe"};
1615 GE = 1;
e4adb204 1616 break;
dea3101e 1617 case PCI_DEVICE_ID_ZMID:
18a3b596 1618 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea3101e
JB
1619 break;
1620 case PCI_DEVICE_ID_ZSMB:
18a3b596 1621 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea3101e
JB
1622 break;
1623 case PCI_DEVICE_ID_LP101:
18a3b596 1624 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea3101e
JB
1625 break;
1626 case PCI_DEVICE_ID_LP10000S:
18a3b596 1627 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
06325e74 1628 break;
e4adb204 1629 case PCI_DEVICE_ID_LP11000S:
18a3b596
JS
1630 m = (typeof(m)){"LP11000-S", max_speed,
1631 "PCI-X2"};
1632 break;
e4adb204 1633 case PCI_DEVICE_ID_LPE11000S:
18a3b596
JS
1634 m = (typeof(m)){"LPe11000-S", max_speed,
1635 "PCIe"};
5cc36b3c 1636 break;
b87eab38
JS
1637 case PCI_DEVICE_ID_SAT:
1638 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1639 break;
1640 case PCI_DEVICE_ID_SAT_MID:
1641 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1642 break;
1643 case PCI_DEVICE_ID_SAT_SMB:
1644 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1645 break;
1646 case PCI_DEVICE_ID_SAT_DCSP:
1647 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1648 break;
1649 case PCI_DEVICE_ID_SAT_SCSP:
1650 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1651 break;
1652 case PCI_DEVICE_ID_SAT_S:
1653 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1654 break;
84774a4d
JS
1655 case PCI_DEVICE_ID_HORNET:
1656 m = (typeof(m)){"LP21000", max_speed, "PCIe"};
1657 GE = 1;
1658 break;
1659 case PCI_DEVICE_ID_PROTEUS_VF:
1660 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1661 break;
1662 case PCI_DEVICE_ID_PROTEUS_PF:
1663 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1664 break;
1665 case PCI_DEVICE_ID_PROTEUS_S:
1666 m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
1667 break;
da0436e9
JS
1668 case PCI_DEVICE_ID_TIGERSHARK:
1669 oneConnect = 1;
1670 m = (typeof(m)) {"OCe10100-F", max_speed, "PCIe"};
1671 break;
5cc36b3c 1672 default:
041976fb 1673 m = (typeof(m)){ NULL };
e4adb204 1674 break;
dea3101e 1675 }
74b72a59
JW
1676
1677 if (mdp && mdp[0] == '\0')
1678 snprintf(mdp, 79,"%s", m.name);
da0436e9
JS
1679 /* oneConnect hba requires special processing, they are all initiators
1680 * and we put the port number on the end
1681 */
1682 if (descp && descp[0] == '\0') {
1683 if (oneConnect)
1684 snprintf(descp, 255,
1685 "Emulex OneConnect %s, FCoE Initiator, Port %s",
1686 m.name,
1687 phba->Port);
1688 else
1689 snprintf(descp, 255,
1690 "Emulex %s %d%s %s %s",
1691 m.name, m.max_speed,
1692 (GE) ? "GE" : "Gb",
1693 m.bus,
1694 (GE) ? "FCoE Adapter" :
1695 "Fibre Channel Adapter");
1696 }
dea3101e
JB
1697}
1698
e59058c4 1699/**
3621a710 1700 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
e59058c4
JS
1701 * @phba: pointer to lpfc hba data structure.
1702 * @pring: pointer to a IOCB ring.
1703 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1704 *
1705 * This routine posts a given number of IOCBs with the associated DMA buffer
1706 * descriptors specified by the cnt argument to the given IOCB ring.
1707 *
1708 * Return codes
1709 * The number of IOCBs NOT able to be posted to the IOCB ring.
1710 **/
dea3101e 1711int
495a714c 1712lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea3101e
JB
1713{
1714 IOCB_t *icmd;
0bd4ca25 1715 struct lpfc_iocbq *iocb;
dea3101e
JB
1716 struct lpfc_dmabuf *mp1, *mp2;
1717
1718 cnt += pring->missbufcnt;
1719
1720 /* While there are buffers to post */
1721 while (cnt > 0) {
1722 /* Allocate buffer for command iocb */
0bd4ca25 1723 iocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
1724 if (iocb == NULL) {
1725 pring->missbufcnt = cnt;
1726 return cnt;
1727 }
dea3101e
JB
1728 icmd = &iocb->iocb;
1729
1730 /* 2 buffers can be posted per command */
1731 /* Allocate buffer to post */
1732 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1733 if (mp1)
98c9ea5c
JS
1734 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1735 if (!mp1 || !mp1->virt) {
c9475cb0 1736 kfree(mp1);
604a3e30 1737 lpfc_sli_release_iocbq(phba, iocb);
dea3101e
JB
1738 pring->missbufcnt = cnt;
1739 return cnt;
1740 }
1741
1742 INIT_LIST_HEAD(&mp1->list);
1743 /* Allocate buffer to post */
1744 if (cnt > 1) {
1745 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1746 if (mp2)
1747 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1748 &mp2->phys);
98c9ea5c 1749 if (!mp2 || !mp2->virt) {
c9475cb0 1750 kfree(mp2);
dea3101e
JB
1751 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1752 kfree(mp1);
604a3e30 1753 lpfc_sli_release_iocbq(phba, iocb);
dea3101e
JB
1754 pring->missbufcnt = cnt;
1755 return cnt;
1756 }
1757
1758 INIT_LIST_HEAD(&mp2->list);
1759 } else {
1760 mp2 = NULL;
1761 }
1762
1763 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1764 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1765 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1766 icmd->ulpBdeCount = 1;
1767 cnt--;
1768 if (mp2) {
1769 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1770 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1771 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1772 cnt--;
1773 icmd->ulpBdeCount = 2;
1774 }
1775
1776 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1777 icmd->ulpLe = 1;
1778
3772a991
JS
1779 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1780 IOCB_ERROR) {
dea3101e
JB
1781 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1782 kfree(mp1);
1783 cnt++;
1784 if (mp2) {
1785 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1786 kfree(mp2);
1787 cnt++;
1788 }
604a3e30 1789 lpfc_sli_release_iocbq(phba, iocb);
dea3101e 1790 pring->missbufcnt = cnt;
dea3101e
JB
1791 return cnt;
1792 }
dea3101e 1793 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
92d7f7b0 1794 if (mp2)
dea3101e 1795 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea3101e
JB
1796 }
1797 pring->missbufcnt = 0;
1798 return 0;
1799}
1800
e59058c4 1801/**
3621a710 1802 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
e59058c4
JS
1803 * @phba: pointer to lpfc hba data structure.
1804 *
1805 * This routine posts initial receive IOCB buffers to the ELS ring. The
1806 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1807 * set to 64 IOCBs.
1808 *
1809 * Return codes
1810 * 0 - success (currently always success)
1811 **/
dea3101e 1812static int
2e0fef85 1813lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea3101e
JB
1814{
1815 struct lpfc_sli *psli = &phba->sli;
1816
1817 /* Ring 0, ELS / CT buffers */
495a714c 1818 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea3101e
JB
1819 /* Ring 2 - FCP no buffers needed */
1820
1821 return 0;
1822}
1823
1824#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1825
e59058c4 1826/**
3621a710 1827 * lpfc_sha_init - Set up initial array of hash table entries
e59058c4
JS
1828 * @HashResultPointer: pointer to an array as hash table.
1829 *
1830 * This routine sets up the initial values to the array of hash table entries
1831 * for the LC HBAs.
1832 **/
dea3101e
JB
1833static void
1834lpfc_sha_init(uint32_t * HashResultPointer)
1835{
1836 HashResultPointer[0] = 0x67452301;
1837 HashResultPointer[1] = 0xEFCDAB89;
1838 HashResultPointer[2] = 0x98BADCFE;
1839 HashResultPointer[3] = 0x10325476;
1840 HashResultPointer[4] = 0xC3D2E1F0;
1841}
1842
e59058c4 1843/**
3621a710 1844 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
e59058c4
JS
1845 * @HashResultPointer: pointer to an initial/result hash table.
1846 * @HashWorkingPointer: pointer to an working hash table.
1847 *
1848 * This routine iterates an initial hash table pointed by @HashResultPointer
1849 * with the values from the working hash table pointeed by @HashWorkingPointer.
1850 * The results are putting back to the initial hash table, returned through
1851 * the @HashResultPointer as the result hash table.
1852 **/
dea3101e
JB
1853static void
1854lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1855{
1856 int t;
1857 uint32_t TEMP;
1858 uint32_t A, B, C, D, E;
1859 t = 16;
1860 do {
1861 HashWorkingPointer[t] =
1862 S(1,
1863 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1864 8] ^
1865 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1866 } while (++t <= 79);
1867 t = 0;
1868 A = HashResultPointer[0];
1869 B = HashResultPointer[1];
1870 C = HashResultPointer[2];
1871 D = HashResultPointer[3];
1872 E = HashResultPointer[4];
1873
1874 do {
1875 if (t < 20) {
1876 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1877 } else if (t < 40) {
1878 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1879 } else if (t < 60) {
1880 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1881 } else {
1882 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1883 }
1884 TEMP += S(5, A) + E + HashWorkingPointer[t];
1885 E = D;
1886 D = C;
1887 C = S(30, B);
1888 B = A;
1889 A = TEMP;
1890 } while (++t <= 79);
1891
1892 HashResultPointer[0] += A;
1893 HashResultPointer[1] += B;
1894 HashResultPointer[2] += C;
1895 HashResultPointer[3] += D;
1896 HashResultPointer[4] += E;
1897
1898}
1899
e59058c4 1900/**
3621a710 1901 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
e59058c4
JS
1902 * @RandomChallenge: pointer to the entry of host challenge random number array.
1903 * @HashWorking: pointer to the entry of the working hash array.
1904 *
1905 * This routine calculates the working hash array referred by @HashWorking
1906 * from the challenge random numbers associated with the host, referred by
1907 * @RandomChallenge. The result is put into the entry of the working hash
1908 * array and returned by reference through @HashWorking.
1909 **/
dea3101e
JB
1910static void
1911lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1912{
1913 *HashWorking = (*RandomChallenge ^ *HashWorking);
1914}
1915
e59058c4 1916/**
3621a710 1917 * lpfc_hba_init - Perform special handling for LC HBA initialization
e59058c4
JS
1918 * @phba: pointer to lpfc hba data structure.
1919 * @hbainit: pointer to an array of unsigned 32-bit integers.
1920 *
1921 * This routine performs the special handling for LC HBA initialization.
1922 **/
dea3101e
JB
1923void
1924lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1925{
1926 int t;
1927 uint32_t *HashWorking;
2e0fef85 1928 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea3101e 1929
bbfbbbc1 1930 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea3101e
JB
1931 if (!HashWorking)
1932 return;
1933
dea3101e
JB
1934 HashWorking[0] = HashWorking[78] = *pwwnn++;
1935 HashWorking[1] = HashWorking[79] = *pwwnn;
1936
1937 for (t = 0; t < 7; t++)
1938 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1939
1940 lpfc_sha_init(hbainit);
1941 lpfc_sha_iterate(hbainit, HashWorking);
1942 kfree(HashWorking);
1943}
1944
e59058c4 1945/**
3621a710 1946 * lpfc_cleanup - Performs vport cleanups before deleting a vport
e59058c4
JS
1947 * @vport: pointer to a virtual N_Port data structure.
1948 *
1949 * This routine performs the necessary cleanups before deleting the @vport.
1950 * It invokes the discovery state machine to perform necessary state
1951 * transitions and to release the ndlps associated with the @vport. Note,
1952 * the physical port is treated as @vport 0.
1953 **/
87af33fe 1954void
2e0fef85 1955lpfc_cleanup(struct lpfc_vport *vport)
dea3101e 1956{
87af33fe 1957 struct lpfc_hba *phba = vport->phba;
dea3101e 1958 struct lpfc_nodelist *ndlp, *next_ndlp;
a8adb832 1959 int i = 0;
dea3101e 1960
87af33fe
JS
1961 if (phba->link_state > LPFC_LINK_DOWN)
1962 lpfc_port_link_failure(vport);
1963
1964 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
1965 if (!NLP_CHK_NODE_ACT(ndlp)) {
1966 ndlp = lpfc_enable_node(vport, ndlp,
1967 NLP_STE_UNUSED_NODE);
1968 if (!ndlp)
1969 continue;
1970 spin_lock_irq(&phba->ndlp_lock);
1971 NLP_SET_FREE_REQ(ndlp);
1972 spin_unlock_irq(&phba->ndlp_lock);
1973 /* Trigger the release of the ndlp memory */
1974 lpfc_nlp_put(ndlp);
1975 continue;
1976 }
1977 spin_lock_irq(&phba->ndlp_lock);
1978 if (NLP_CHK_FREE_REQ(ndlp)) {
1979 /* The ndlp should not be in memory free mode already */
1980 spin_unlock_irq(&phba->ndlp_lock);
1981 continue;
1982 } else
1983 /* Indicate request for freeing ndlp memory */
1984 NLP_SET_FREE_REQ(ndlp);
1985 spin_unlock_irq(&phba->ndlp_lock);
1986
58da1ffb
JS
1987 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1988 ndlp->nlp_DID == Fabric_DID) {
1989 /* Just free up ndlp with Fabric_DID for vports */
1990 lpfc_nlp_put(ndlp);
1991 continue;
1992 }
1993
87af33fe
JS
1994 if (ndlp->nlp_type & NLP_FABRIC)
1995 lpfc_disc_state_machine(vport, ndlp, NULL,
1996 NLP_EVT_DEVICE_RECOVERY);
e47c9093 1997
87af33fe
JS
1998 lpfc_disc_state_machine(vport, ndlp, NULL,
1999 NLP_EVT_DEVICE_RM);
495a714c 2000
87af33fe
JS
2001 }
2002
a8adb832
JS
2003 /* At this point, ALL ndlp's should be gone
2004 * because of the previous NLP_EVT_DEVICE_RM.
2005 * Lets wait for this to happen, if needed.
2006 */
87af33fe 2007 while (!list_empty(&vport->fc_nodes)) {
a8adb832 2008 if (i++ > 3000) {
87af33fe 2009 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
a8adb832 2010 "0233 Nodelist not empty\n");
e47c9093
JS
2011 list_for_each_entry_safe(ndlp, next_ndlp,
2012 &vport->fc_nodes, nlp_listp) {
2013 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2014 LOG_NODE,
d7c255b2 2015 "0282 did:x%x ndlp:x%p "
e47c9093
JS
2016 "usgmap:x%x refcnt:%d\n",
2017 ndlp->nlp_DID, (void *)ndlp,
2018 ndlp->nlp_usg_map,
2019 atomic_read(
2020 &ndlp->kref.refcount));
2021 }
a8adb832 2022 break;
87af33fe 2023 }
a8adb832
JS
2024
2025 /* Wait for any activity on ndlps to settle */
2026 msleep(10);
87af33fe 2027 }
dea3101e
JB
2028}
2029
e59058c4 2030/**
3621a710 2031 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
e59058c4
JS
2032 * @vport: pointer to a virtual N_Port data structure.
2033 *
2034 * This routine stops all the timers associated with a @vport. This function
2035 * is invoked before disabling or deleting a @vport. Note that the physical
2036 * port is treated as @vport 0.
2037 **/
92d7f7b0
JS
2038void
2039lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea3101e 2040{
92d7f7b0
JS
2041 del_timer_sync(&vport->els_tmofunc);
2042 del_timer_sync(&vport->fc_fdmitmo);
2043 lpfc_can_disctmo(vport);
2044 return;
dea3101e
JB
2045}
2046
e59058c4 2047/**
3772a991 2048 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
e59058c4
JS
2049 * @phba: pointer to lpfc hba data structure.
2050 *
2051 * This routine stops all the timers associated with a HBA. This function is
2052 * invoked before either putting a HBA offline or unloading the driver.
2053 **/
3772a991
JS
2054void
2055lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea3101e 2056{
51ef4c26 2057 lpfc_stop_vport_timers(phba->pport);
2e0fef85 2058 del_timer_sync(&phba->sli.mbox_tmo);
92d7f7b0 2059 del_timer_sync(&phba->fabric_block_timer);
9399627f 2060 del_timer_sync(&phba->eratt_poll);
3772a991
JS
2061 del_timer_sync(&phba->hb_tmofunc);
2062 phba->hb_outstanding = 0;
2063
2064 switch (phba->pci_dev_grp) {
2065 case LPFC_PCI_DEV_LP:
2066 /* Stop any LightPulse device specific driver timers */
2067 del_timer_sync(&phba->fcp_poll_timer);
2068 break;
2069 case LPFC_PCI_DEV_OC:
2070 /* Stop any OneConnect device sepcific driver timers */
2071 break;
2072 default:
2073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2074 "0297 Invalid device group (x%x)\n",
2075 phba->pci_dev_grp);
2076 break;
2077 }
2e0fef85 2078 return;
dea3101e
JB
2079}
2080
e59058c4 2081/**
3621a710 2082 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
e59058c4
JS
2083 * @phba: pointer to lpfc hba data structure.
2084 *
2085 * This routine marks a HBA's management interface as blocked. Once the HBA's
2086 * management interface is marked as blocked, all the user space access to
2087 * the HBA, whether they are from sysfs interface or libdfc interface will
2088 * all be blocked. The HBA is set to block the management interface when the
2089 * driver prepares the HBA interface for online or offline.
2090 **/
a6ababd2
AB
2091static void
2092lpfc_block_mgmt_io(struct lpfc_hba * phba)
2093{
2094 unsigned long iflag;
2095
2096 spin_lock_irqsave(&phba->hbalock, iflag);
2097 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2098 spin_unlock_irqrestore(&phba->hbalock, iflag);
2099}
2100
e59058c4 2101/**
3621a710 2102 * lpfc_online - Initialize and bring a HBA online
e59058c4
JS
2103 * @phba: pointer to lpfc hba data structure.
2104 *
2105 * This routine initializes the HBA and brings a HBA online. During this
2106 * process, the management interface is blocked to prevent user space access
2107 * to the HBA interfering with the driver initialization.
2108 *
2109 * Return codes
2110 * 0 - successful
2111 * 1 - failed
2112 **/
dea3101e 2113int
2e0fef85 2114lpfc_online(struct lpfc_hba *phba)
dea3101e 2115{
372bd282 2116 struct lpfc_vport *vport;
549e55cd
JS
2117 struct lpfc_vport **vports;
2118 int i;
2e0fef85 2119
dea3101e
JB
2120 if (!phba)
2121 return 0;
372bd282 2122 vport = phba->pport;
dea3101e 2123
2e0fef85 2124 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea3101e
JB
2125 return 0;
2126
ed957684 2127 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
e8b62011 2128 "0458 Bring Adapter online\n");
dea3101e 2129
46fa311e
JS
2130 lpfc_block_mgmt_io(phba);
2131
2132 if (!lpfc_sli_queue_setup(phba)) {
2133 lpfc_unblock_mgmt_io(phba);
dea3101e 2134 return 1;
46fa311e 2135 }
dea3101e 2136
da0436e9
JS
2137 if (phba->sli_rev == LPFC_SLI_REV4) {
2138 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2139 lpfc_unblock_mgmt_io(phba);
2140 return 1;
2141 }
2142 } else {
2143 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2144 lpfc_unblock_mgmt_io(phba);
2145 return 1;
2146 }
46fa311e 2147 }
dea3101e 2148
549e55cd
JS
2149 vports = lpfc_create_vport_work_array(phba);
2150 if (vports != NULL)
da0436e9 2151 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
2152 struct Scsi_Host *shost;
2153 shost = lpfc_shost_from_vport(vports[i]);
2154 spin_lock_irq(shost->host_lock);
2155 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2156 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2157 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1c6834a7
JS
2158 if (phba->sli_rev == LPFC_SLI_REV4)
2159 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
549e55cd
JS
2160 spin_unlock_irq(shost->host_lock);
2161 }
09372820 2162 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 2163
46fa311e 2164 lpfc_unblock_mgmt_io(phba);
dea3101e
JB
2165 return 0;
2166}
2167
e59058c4 2168/**
3621a710 2169 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
e59058c4
JS
2170 * @phba: pointer to lpfc hba data structure.
2171 *
2172 * This routine marks a HBA's management interface as not blocked. Once the
2173 * HBA's management interface is marked as not blocked, all the user space
2174 * access to the HBA, whether they are from sysfs interface or libdfc
2175 * interface will be allowed. The HBA is set to block the management interface
2176 * when the driver prepares the HBA interface for online or offline and then
2177 * set to unblock the management interface afterwards.
2178 **/
46fa311e
JS
2179void
2180lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2181{
2182 unsigned long iflag;
2183
2e0fef85
JS
2184 spin_lock_irqsave(&phba->hbalock, iflag);
2185 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2186 spin_unlock_irqrestore(&phba->hbalock, iflag);
46fa311e
JS
2187}
2188
e59058c4 2189/**
3621a710 2190 * lpfc_offline_prep - Prepare a HBA to be brought offline
e59058c4
JS
2191 * @phba: pointer to lpfc hba data structure.
2192 *
2193 * This routine is invoked to prepare a HBA to be brought offline. It performs
2194 * unregistration login to all the nodes on all vports and flushes the mailbox
2195 * queue to make it ready to be brought offline.
2196 **/
46fa311e
JS
2197void
2198lpfc_offline_prep(struct lpfc_hba * phba)
2199{
2e0fef85 2200 struct lpfc_vport *vport = phba->pport;
46fa311e 2201 struct lpfc_nodelist *ndlp, *next_ndlp;
87af33fe
JS
2202 struct lpfc_vport **vports;
2203 int i;
dea3101e 2204
2e0fef85 2205 if (vport->fc_flag & FC_OFFLINE_MODE)
46fa311e 2206 return;
dea3101e 2207
46fa311e 2208 lpfc_block_mgmt_io(phba);
dea3101e
JB
2209
2210 lpfc_linkdown(phba);
2211
87af33fe
JS
2212 /* Issue an unreg_login to all nodes on all vports */
2213 vports = lpfc_create_vport_work_array(phba);
2214 if (vports != NULL) {
da0436e9 2215 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
87af33fe
JS
2216 struct Scsi_Host *shost;
2217
a8adb832
JS
2218 if (vports[i]->load_flag & FC_UNLOADING)
2219 continue;
da0436e9 2220 vports[i]->vfi_state &= ~LPFC_VFI_REGISTERED;
87af33fe
JS
2221 shost = lpfc_shost_from_vport(vports[i]);
2222 list_for_each_entry_safe(ndlp, next_ndlp,
2223 &vports[i]->fc_nodes,
2224 nlp_listp) {
e47c9093
JS
2225 if (!NLP_CHK_NODE_ACT(ndlp))
2226 continue;
87af33fe
JS
2227 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2228 continue;
2229 if (ndlp->nlp_type & NLP_FABRIC) {
2230 lpfc_disc_state_machine(vports[i], ndlp,
2231 NULL, NLP_EVT_DEVICE_RECOVERY);
2232 lpfc_disc_state_machine(vports[i], ndlp,
2233 NULL, NLP_EVT_DEVICE_RM);
2234 }
2235 spin_lock_irq(shost->host_lock);
2236 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2237 spin_unlock_irq(shost->host_lock);
2238 lpfc_unreg_rpi(vports[i], ndlp);
2239 }
2240 }
2241 }
09372820 2242 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 2243
04c68496 2244 lpfc_sli_mbox_sys_shutdown(phba);
46fa311e
JS
2245}
2246
e59058c4 2247/**
3621a710 2248 * lpfc_offline - Bring a HBA offline
e59058c4
JS
2249 * @phba: pointer to lpfc hba data structure.
2250 *
2251 * This routine actually brings a HBA offline. It stops all the timers
2252 * associated with the HBA, brings down the SLI layer, and eventually
2253 * marks the HBA as in offline state for the upper layer protocol.
2254 **/
46fa311e 2255void
2e0fef85 2256lpfc_offline(struct lpfc_hba *phba)
46fa311e 2257{
549e55cd
JS
2258 struct Scsi_Host *shost;
2259 struct lpfc_vport **vports;
2260 int i;
46fa311e 2261
549e55cd 2262 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
46fa311e 2263 return;
688a8863 2264
da0436e9
JS
2265 /* stop port and all timers associated with this hba */
2266 lpfc_stop_port(phba);
51ef4c26
JS
2267 vports = lpfc_create_vport_work_array(phba);
2268 if (vports != NULL)
da0436e9 2269 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
51ef4c26 2270 lpfc_stop_vport_timers(vports[i]);
09372820 2271 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0 2272 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
e8b62011 2273 "0460 Bring Adapter offline\n");
dea3101e
JB
2274 /* Bring down the SLI Layer and cleanup. The HBA is offline
2275 now. */
2276 lpfc_sli_hba_down(phba);
92d7f7b0 2277 spin_lock_irq(&phba->hbalock);
7054a606 2278 phba->work_ha = 0;
92d7f7b0 2279 spin_unlock_irq(&phba->hbalock);
549e55cd
JS
2280 vports = lpfc_create_vport_work_array(phba);
2281 if (vports != NULL)
da0436e9 2282 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd 2283 shost = lpfc_shost_from_vport(vports[i]);
549e55cd
JS
2284 spin_lock_irq(shost->host_lock);
2285 vports[i]->work_port_events = 0;
2286 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2287 spin_unlock_irq(shost->host_lock);
2288 }
09372820 2289 lpfc_destroy_vport_work_array(phba, vports);
dea3101e
JB
2290}
2291
e59058c4 2292/**
3621a710 2293 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
e59058c4
JS
2294 * @phba: pointer to lpfc hba data structure.
2295 *
2296 * This routine is to free all the SCSI buffers and IOCBs from the driver
2297 * list back to kernel. It is called from lpfc_pci_remove_one to free
2298 * the internal resources before the device is removed from the system.
2299 *
2300 * Return codes
2301 * 0 - successful (for now, it always returns 0)
2302 **/
dea3101e 2303static int
2e0fef85 2304lpfc_scsi_free(struct lpfc_hba *phba)
dea3101e
JB
2305{
2306 struct lpfc_scsi_buf *sb, *sb_next;
2307 struct lpfc_iocbq *io, *io_next;
2308
2e0fef85 2309 spin_lock_irq(&phba->hbalock);
dea3101e
JB
2310 /* Release all the lpfc_scsi_bufs maintained by this host. */
2311 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2312 list_del(&sb->list);
2313 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
92d7f7b0 2314 sb->dma_handle);
dea3101e
JB
2315 kfree(sb);
2316 phba->total_scsi_bufs--;
2317 }
2318
2319 /* Release all the lpfc_iocbq entries maintained by this host. */
2320 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2321 list_del(&io->list);
2322 kfree(io);
2323 phba->total_iocbq_bufs--;
2324 }
2325
2e0fef85 2326 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
2327
2328 return 0;
2329}
2330
e59058c4 2331/**
3621a710 2332 * lpfc_create_port - Create an FC port
e59058c4
JS
2333 * @phba: pointer to lpfc hba data structure.
2334 * @instance: a unique integer ID to this FC port.
2335 * @dev: pointer to the device data structure.
2336 *
2337 * This routine creates a FC port for the upper layer protocol. The FC port
2338 * can be created on top of either a physical port or a virtual port provided
2339 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2340 * and associates the FC port created before adding the shost into the SCSI
2341 * layer.
2342 *
2343 * Return codes
2344 * @vport - pointer to the virtual N_Port data structure.
2345 * NULL - port create failed.
2346 **/
2e0fef85 2347struct lpfc_vport *
3de2a653 2348lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
47a8617c 2349{
2e0fef85
JS
2350 struct lpfc_vport *vport;
2351 struct Scsi_Host *shost;
2352 int error = 0;
47a8617c 2353
3de2a653
JS
2354 if (dev != &phba->pcidev->dev)
2355 shost = scsi_host_alloc(&lpfc_vport_template,
2356 sizeof(struct lpfc_vport));
2357 else
2358 shost = scsi_host_alloc(&lpfc_template,
2359 sizeof(struct lpfc_vport));
2e0fef85
JS
2360 if (!shost)
2361 goto out;
47a8617c 2362
2e0fef85
JS
2363 vport = (struct lpfc_vport *) shost->hostdata;
2364 vport->phba = phba;
2e0fef85 2365 vport->load_flag |= FC_LOADING;
92d7f7b0 2366 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7f5f3d0d 2367 vport->fc_rscn_flush = 0;
47a8617c 2368
3de2a653 2369 lpfc_get_vport_cfgparam(vport);
2e0fef85
JS
2370 shost->unique_id = instance;
2371 shost->max_id = LPFC_MAX_TARGET;
3de2a653 2372 shost->max_lun = vport->cfg_max_luns;
2e0fef85
JS
2373 shost->this_id = -1;
2374 shost->max_cmd_len = 16;
da0436e9
JS
2375 if (phba->sli_rev == LPFC_SLI_REV4) {
2376 shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2377 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2378 }
81301a9b 2379
47a8617c 2380 /*
2e0fef85
JS
2381 * Set initial can_queue value since 0 is no longer supported and
2382 * scsi_add_host will fail. This will be adjusted later based on the
2383 * max xri value determined in hba setup.
47a8617c 2384 */
2e0fef85 2385 shost->can_queue = phba->cfg_hba_queue_depth - 10;
3de2a653 2386 if (dev != &phba->pcidev->dev) {
92d7f7b0
JS
2387 shost->transportt = lpfc_vport_transport_template;
2388 vport->port_type = LPFC_NPIV_PORT;
2389 } else {
2390 shost->transportt = lpfc_transport_template;
2391 vport->port_type = LPFC_PHYSICAL_PORT;
2392 }
47a8617c 2393
2e0fef85
JS
2394 /* Initialize all internally managed lists. */
2395 INIT_LIST_HEAD(&vport->fc_nodes);
da0436e9 2396 INIT_LIST_HEAD(&vport->rcv_buffer_list);
2e0fef85 2397 spin_lock_init(&vport->work_port_lock);
47a8617c 2398
2e0fef85
JS
2399 init_timer(&vport->fc_disctmo);
2400 vport->fc_disctmo.function = lpfc_disc_timeout;
92d7f7b0 2401 vport->fc_disctmo.data = (unsigned long)vport;
47a8617c 2402
2e0fef85
JS
2403 init_timer(&vport->fc_fdmitmo);
2404 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
92d7f7b0 2405 vport->fc_fdmitmo.data = (unsigned long)vport;
47a8617c 2406
2e0fef85
JS
2407 init_timer(&vport->els_tmofunc);
2408 vport->els_tmofunc.function = lpfc_els_timeout;
92d7f7b0 2409 vport->els_tmofunc.data = (unsigned long)vport;
47a8617c 2410
3de2a653 2411 error = scsi_add_host(shost, dev);
2e0fef85
JS
2412 if (error)
2413 goto out_put_shost;
47a8617c 2414
549e55cd 2415 spin_lock_irq(&phba->hbalock);
2e0fef85 2416 list_add_tail(&vport->listentry, &phba->port_list);
549e55cd 2417 spin_unlock_irq(&phba->hbalock);
2e0fef85 2418 return vport;
47a8617c 2419
2e0fef85
JS
2420out_put_shost:
2421 scsi_host_put(shost);
2422out:
2423 return NULL;
47a8617c
JS
2424}
2425
e59058c4 2426/**
3621a710 2427 * destroy_port - destroy an FC port
e59058c4
JS
2428 * @vport: pointer to an lpfc virtual N_Port data structure.
2429 *
2430 * This routine destroys a FC port from the upper layer protocol. All the
2431 * resources associated with the port are released.
2432 **/
2e0fef85
JS
2433void
2434destroy_port(struct lpfc_vport *vport)
47a8617c 2435{
92d7f7b0
JS
2436 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2437 struct lpfc_hba *phba = vport->phba;
47a8617c 2438
858c9f6c 2439 lpfc_debugfs_terminate(vport);
92d7f7b0
JS
2440 fc_remove_host(shost);
2441 scsi_remove_host(shost);
47a8617c 2442
92d7f7b0
JS
2443 spin_lock_irq(&phba->hbalock);
2444 list_del_init(&vport->listentry);
2445 spin_unlock_irq(&phba->hbalock);
47a8617c 2446
92d7f7b0 2447 lpfc_cleanup(vport);
47a8617c 2448 return;
47a8617c
JS
2449}
2450
e59058c4 2451/**
3621a710 2452 * lpfc_get_instance - Get a unique integer ID
e59058c4
JS
2453 *
2454 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2455 * uses the kernel idr facility to perform the task.
2456 *
2457 * Return codes:
2458 * instance - a unique integer ID allocated as the new instance.
2459 * -1 - lpfc get instance failed.
2460 **/
92d7f7b0
JS
2461int
2462lpfc_get_instance(void)
2463{
2464 int instance = 0;
47a8617c 2465
92d7f7b0
JS
2466 /* Assign an unused number */
2467 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2468 return -1;
2469 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2470 return -1;
2471 return instance;
47a8617c
JS
2472}
2473
e59058c4 2474/**
3621a710 2475 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
e59058c4
JS
2476 * @shost: pointer to SCSI host data structure.
2477 * @time: elapsed time of the scan in jiffies.
2478 *
2479 * This routine is called by the SCSI layer with a SCSI host to determine
2480 * whether the scan host is finished.
2481 *
2482 * Note: there is no scan_start function as adapter initialization will have
2483 * asynchronously kicked off the link initialization.
2484 *
2485 * Return codes
2486 * 0 - SCSI host scan is not over yet.
2487 * 1 - SCSI host scan is over.
2488 **/
47a8617c
JS
2489int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2490{
2e0fef85
JS
2491 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2492 struct lpfc_hba *phba = vport->phba;
858c9f6c 2493 int stat = 0;
47a8617c 2494
858c9f6c
JS
2495 spin_lock_irq(shost->host_lock);
2496
51ef4c26 2497 if (vport->load_flag & FC_UNLOADING) {
858c9f6c
JS
2498 stat = 1;
2499 goto finished;
2500 }
2e0fef85
JS
2501 if (time >= 30 * HZ) {
2502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
2503 "0461 Scanning longer than 30 "
2504 "seconds. Continuing initialization\n");
858c9f6c 2505 stat = 1;
47a8617c 2506 goto finished;
2e0fef85
JS
2507 }
2508 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2509 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
2510 "0465 Link down longer than 15 "
2511 "seconds. Continuing initialization\n");
858c9f6c 2512 stat = 1;
47a8617c 2513 goto finished;
2e0fef85 2514 }
47a8617c 2515
2e0fef85 2516 if (vport->port_state != LPFC_VPORT_READY)
858c9f6c 2517 goto finished;
2e0fef85 2518 if (vport->num_disc_nodes || vport->fc_prli_sent)
858c9f6c 2519 goto finished;
2e0fef85 2520 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
858c9f6c 2521 goto finished;
2e0fef85 2522 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
858c9f6c
JS
2523 goto finished;
2524
2525 stat = 1;
47a8617c
JS
2526
2527finished:
858c9f6c
JS
2528 spin_unlock_irq(shost->host_lock);
2529 return stat;
92d7f7b0 2530}
47a8617c 2531
e59058c4 2532/**
3621a710 2533 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
e59058c4
JS
2534 * @shost: pointer to SCSI host data structure.
2535 *
2536 * This routine initializes a given SCSI host attributes on a FC port. The
2537 * SCSI host can be either on top of a physical port or a virtual port.
2538 **/
92d7f7b0
JS
2539void lpfc_host_attrib_init(struct Scsi_Host *shost)
2540{
2541 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2542 struct lpfc_hba *phba = vport->phba;
47a8617c 2543 /*
2e0fef85 2544 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
47a8617c
JS
2545 */
2546
2e0fef85
JS
2547 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2548 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
47a8617c
JS
2549 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2550
2551 memset(fc_host_supported_fc4s(shost), 0,
2e0fef85 2552 sizeof(fc_host_supported_fc4s(shost)));
47a8617c
JS
2553 fc_host_supported_fc4s(shost)[2] = 1;
2554 fc_host_supported_fc4s(shost)[7] = 1;
2555
92d7f7b0
JS
2556 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2557 sizeof fc_host_symbolic_name(shost));
47a8617c
JS
2558
2559 fc_host_supported_speeds(shost) = 0;
2560 if (phba->lmt & LMT_10Gb)
2561 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
a8adb832
JS
2562 if (phba->lmt & LMT_8Gb)
2563 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
47a8617c
JS
2564 if (phba->lmt & LMT_4Gb)
2565 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2566 if (phba->lmt & LMT_2Gb)
2567 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2568 if (phba->lmt & LMT_1Gb)
2569 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2570
2571 fc_host_maxframe_size(shost) =
2e0fef85
JS
2572 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2573 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
47a8617c
JS
2574
2575 /* This value is also unchanging */
2576 memset(fc_host_active_fc4s(shost), 0,
2e0fef85 2577 sizeof(fc_host_active_fc4s(shost)));
47a8617c
JS
2578 fc_host_active_fc4s(shost)[2] = 1;
2579 fc_host_active_fc4s(shost)[7] = 1;
2580
92d7f7b0 2581 fc_host_max_npiv_vports(shost) = phba->max_vpi;
47a8617c 2582 spin_lock_irq(shost->host_lock);
51ef4c26 2583 vport->load_flag &= ~FC_LOADING;
47a8617c 2584 spin_unlock_irq(shost->host_lock);
47a8617c 2585}
dea3101e 2586
e59058c4 2587/**
da0436e9 2588 * lpfc_stop_port_s3 - Stop SLI3 device port
e59058c4
JS
2589 * @phba: pointer to lpfc hba data structure.
2590 *
da0436e9
JS
2591 * This routine is invoked to stop an SLI3 device port, it stops the device
2592 * from generating interrupts and stops the device driver's timers for the
2593 * device.
e59058c4 2594 **/
da0436e9
JS
2595static void
2596lpfc_stop_port_s3(struct lpfc_hba *phba)
db2378e0 2597{
da0436e9
JS
2598 /* Clear all interrupt enable conditions */
2599 writel(0, phba->HCregaddr);
2600 readl(phba->HCregaddr); /* flush */
2601 /* Clear all pending interrupts */
2602 writel(0xffffffff, phba->HAregaddr);
2603 readl(phba->HAregaddr); /* flush */
db2378e0 2604
da0436e9
JS
2605 /* Reset some HBA SLI setup states */
2606 lpfc_stop_hba_timers(phba);
2607 phba->pport->work_port_events = 0;
2608}
db2378e0 2609
da0436e9
JS
2610/**
2611 * lpfc_stop_port_s4 - Stop SLI4 device port
2612 * @phba: pointer to lpfc hba data structure.
2613 *
2614 * This routine is invoked to stop an SLI4 device port, it stops the device
2615 * from generating interrupts and stops the device driver's timers for the
2616 * device.
2617 **/
2618static void
2619lpfc_stop_port_s4(struct lpfc_hba *phba)
2620{
2621 /* Reset some HBA SLI4 setup states */
2622 lpfc_stop_hba_timers(phba);
2623 phba->pport->work_port_events = 0;
2624 phba->sli4_hba.intr_enable = 0;
2625 /* Hard clear it for now, shall have more graceful way to wait later */
2626 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2627}
9399627f 2628
da0436e9
JS
2629/**
2630 * lpfc_stop_port - Wrapper function for stopping hba port
2631 * @phba: Pointer to HBA context object.
2632 *
2633 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2634 * the API jump table function pointer from the lpfc_hba struct.
2635 **/
2636void
2637lpfc_stop_port(struct lpfc_hba *phba)
2638{
2639 phba->lpfc_stop_port(phba);
2640}
db2378e0 2641
da0436e9
JS
2642/**
2643 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2644 * @phba: pointer to lpfc hba data structure.
2645 *
2646 * This routine is invoked to remove the driver default fcf record from
2647 * the port. This routine currently acts on FCF Index 0.
2648 *
2649 **/
2650void
2651lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2652{
2653 int rc = 0;
2654 LPFC_MBOXQ_t *mboxq;
2655 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2656 uint32_t mbox_tmo, req_len;
2657 uint32_t shdr_status, shdr_add_status;
9399627f 2658
da0436e9
JS
2659 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2660 if (!mboxq) {
2661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2662 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2663 return;
db2378e0 2664 }
9399627f 2665
da0436e9
JS
2666 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2667 sizeof(struct lpfc_sli4_cfg_mhdr);
2668 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2669 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2670 req_len, LPFC_SLI4_MBX_EMBED);
9399627f 2671 /*
da0436e9
JS
2672 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2673 * supports multiple FCF indices.
9399627f 2674 */
da0436e9
JS
2675 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2676 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2677 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2678 phba->fcf.fcf_indx);
9399627f 2679
da0436e9
JS
2680 if (!phba->sli4_hba.intr_enable)
2681 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2682 else {
2683 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2684 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9399627f 2685 }
da0436e9
JS
2686 /* The IOCTL status is embedded in the mailbox subheader. */
2687 shdr_status = bf_get(lpfc_mbox_hdr_status,
2688 &del_fcf_record->header.cfg_shdr.response);
2689 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2690 &del_fcf_record->header.cfg_shdr.response);
2691 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2693 "2516 DEL FCF of default FCF Index failed "
2694 "mbx status x%x, status x%x add_status x%x\n",
2695 rc, shdr_status, shdr_add_status);
9399627f 2696 }
da0436e9
JS
2697 if (rc != MBX_TIMEOUT)
2698 mempool_free(mboxq, phba->mbox_mem_pool);
db2378e0
JS
2699}
2700
e59058c4 2701/**
da0436e9 2702 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
e59058c4 2703 * @phba: pointer to lpfc hba data structure.
da0436e9 2704 * @acqe_link: pointer to the async link completion queue entry.
e59058c4 2705 *
da0436e9
JS
2706 * This routine is to parse the SLI4 link-attention link fault code and
2707 * translate it into the base driver's read link attention mailbox command
2708 * status.
2709 *
2710 * Return: Link-attention status in terms of base driver's coding.
e59058c4 2711 **/
da0436e9
JS
2712static uint16_t
2713lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2714 struct lpfc_acqe_link *acqe_link)
db2378e0 2715{
da0436e9 2716 uint16_t latt_fault;
9399627f 2717
da0436e9
JS
2718 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2719 case LPFC_ASYNC_LINK_FAULT_NONE:
2720 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2721 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2722 latt_fault = 0;
2723 break;
2724 default:
2725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2726 "0398 Invalid link fault code: x%x\n",
2727 bf_get(lpfc_acqe_link_fault, acqe_link));
2728 latt_fault = MBXERR_ERROR;
2729 break;
2730 }
2731 return latt_fault;
db2378e0
JS
2732}
2733
5b75da2f 2734/**
da0436e9 2735 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
5b75da2f 2736 * @phba: pointer to lpfc hba data structure.
da0436e9 2737 * @acqe_link: pointer to the async link completion queue entry.
5b75da2f 2738 *
da0436e9
JS
2739 * This routine is to parse the SLI4 link attention type and translate it
2740 * into the base driver's link attention type coding.
5b75da2f 2741 *
da0436e9
JS
2742 * Return: Link attention type in terms of base driver's coding.
2743 **/
2744static uint8_t
2745lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2746 struct lpfc_acqe_link *acqe_link)
5b75da2f 2747{
da0436e9 2748 uint8_t att_type;
5b75da2f 2749
da0436e9
JS
2750 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2751 case LPFC_ASYNC_LINK_STATUS_DOWN:
2752 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2753 att_type = AT_LINK_DOWN;
2754 break;
2755 case LPFC_ASYNC_LINK_STATUS_UP:
2756 /* Ignore physical link up events - wait for logical link up */
2757 att_type = AT_RESERVED;
2758 break;
2759 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2760 att_type = AT_LINK_UP;
2761 break;
2762 default:
2763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2764 "0399 Invalid link attention type: x%x\n",
2765 bf_get(lpfc_acqe_link_status, acqe_link));
2766 att_type = AT_RESERVED;
2767 break;
5b75da2f 2768 }
da0436e9 2769 return att_type;
5b75da2f
JS
2770}
2771
2772/**
da0436e9 2773 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
5b75da2f 2774 * @phba: pointer to lpfc hba data structure.
da0436e9 2775 * @acqe_link: pointer to the async link completion queue entry.
5b75da2f 2776 *
da0436e9
JS
2777 * This routine is to parse the SLI4 link-attention link speed and translate
2778 * it into the base driver's link-attention link speed coding.
2779 *
2780 * Return: Link-attention link speed in terms of base driver's coding.
2781 **/
2782static uint8_t
2783lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2784 struct lpfc_acqe_link *acqe_link)
5b75da2f 2785{
da0436e9
JS
2786 uint8_t link_speed;
2787
2788 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2789 case LPFC_ASYNC_LINK_SPEED_ZERO:
2790 link_speed = LA_UNKNW_LINK;
2791 break;
2792 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2793 link_speed = LA_UNKNW_LINK;
2794 break;
2795 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2796 link_speed = LA_UNKNW_LINK;
2797 break;
2798 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2799 link_speed = LA_1GHZ_LINK;
2800 break;
2801 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2802 link_speed = LA_10GHZ_LINK;
2803 break;
2804 default:
2805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2806 "0483 Invalid link-attention link speed: x%x\n",
2807 bf_get(lpfc_acqe_link_speed, acqe_link));
2808 link_speed = LA_UNKNW_LINK;
2809 break;
2810 }
2811 return link_speed;
2812}
2813
2814/**
2815 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2816 * @phba: pointer to lpfc hba data structure.
2817 * @acqe_link: pointer to the async link completion queue entry.
2818 *
2819 * This routine is to handle the SLI4 asynchronous link event.
2820 **/
2821static void
2822lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
2823 struct lpfc_acqe_link *acqe_link)
2824{
2825 struct lpfc_dmabuf *mp;
2826 LPFC_MBOXQ_t *pmb;
2827 MAILBOX_t *mb;
2828 READ_LA_VAR *la;
2829 uint8_t att_type;
2830
2831 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
2832 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
2833 return;
32b9793f 2834 phba->fcoe_eventtag = acqe_link->event_tag;
da0436e9
JS
2835 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2836 if (!pmb) {
2837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2838 "0395 The mboxq allocation failed\n");
2839 return;
2840 }
2841 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2842 if (!mp) {
2843 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2844 "0396 The lpfc_dmabuf allocation failed\n");
2845 goto out_free_pmb;
2846 }
2847 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2848 if (!mp->virt) {
2849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2850 "0397 The mbuf allocation failed\n");
2851 goto out_free_dmabuf;
2852 }
2853
2854 /* Cleanup any outstanding ELS commands */
2855 lpfc_els_flush_all_cmd(phba);
2856
2857 /* Block ELS IOCBs until we have done process link event */
2858 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2859
2860 /* Update link event statistics */
2861 phba->sli.slistat.link_event++;
2862
2863 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
2864 lpfc_read_la(phba, pmb, mp);
2865 pmb->vport = phba->pport;
2866
2867 /* Parse and translate status field */
2868 mb = &pmb->u.mb;
2869 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
2870
2871 /* Parse and translate link attention fields */
2872 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2873 la->eventTag = acqe_link->event_tag;
2874 la->attType = att_type;
2875 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
2876
2877 /* Fake the the following irrelvant fields */
2878 la->topology = TOPOLOGY_PT_PT;
2879 la->granted_AL_PA = 0;
2880 la->il = 0;
2881 la->pb = 0;
2882 la->fa = 0;
2883 la->mm = 0;
2884
2885 /* Keep the link status for extra SLI4 state machine reference */
2886 phba->sli4_hba.link_state.speed =
2887 bf_get(lpfc_acqe_link_speed, acqe_link);
2888 phba->sli4_hba.link_state.duplex =
2889 bf_get(lpfc_acqe_link_duplex, acqe_link);
2890 phba->sli4_hba.link_state.status =
2891 bf_get(lpfc_acqe_link_status, acqe_link);
2892 phba->sli4_hba.link_state.physical =
2893 bf_get(lpfc_acqe_link_physical, acqe_link);
2894 phba->sli4_hba.link_state.fault =
2895 bf_get(lpfc_acqe_link_fault, acqe_link);
2896
2897 /* Invoke the lpfc_handle_latt mailbox command callback function */
2898 lpfc_mbx_cmpl_read_la(phba, pmb);
2899
5b75da2f 2900 return;
da0436e9
JS
2901
2902out_free_dmabuf:
2903 kfree(mp);
2904out_free_pmb:
2905 mempool_free(pmb, phba->mbox_mem_pool);
2906}
2907
2908/**
2909 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
2910 * @phba: pointer to lpfc hba data structure.
2911 * @acqe_link: pointer to the async fcoe completion queue entry.
2912 *
2913 * This routine is to handle the SLI4 asynchronous fcoe event.
2914 **/
2915static void
2916lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
2917 struct lpfc_acqe_fcoe *acqe_fcoe)
2918{
2919 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
2920 int rc;
2921
32b9793f 2922 phba->fcoe_eventtag = acqe_fcoe->event_tag;
da0436e9
JS
2923 switch (event_type) {
2924 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
2925 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2926 "2546 New FCF found index 0x%x tag 0x%x \n",
2927 acqe_fcoe->fcf_index,
2928 acqe_fcoe->event_tag);
2929 /*
32b9793f
JS
2930 * If the current FCF is in discovered state, or
2931 * FCF discovery is in progress do nothing.
da0436e9
JS
2932 */
2933 spin_lock_irq(&phba->hbalock);
32b9793f
JS
2934 if ((phba->fcf.fcf_flag & FCF_DISCOVERED) ||
2935 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
da0436e9
JS
2936 spin_unlock_irq(&phba->hbalock);
2937 break;
2938 }
2939 spin_unlock_irq(&phba->hbalock);
2940
2941 /* Read the FCF table and re-discover SAN. */
2942 rc = lpfc_sli4_read_fcf_record(phba,
2943 LPFC_FCOE_FCF_GET_FIRST);
2944 if (rc)
2945 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2946 "2547 Read FCF record failed 0x%x\n",
2947 rc);
2948 break;
2949
2950 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
2951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2952 "2548 FCF Table full count 0x%x tag 0x%x \n",
2953 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
2954 acqe_fcoe->event_tag);
2955 break;
2956
2957 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
2958 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2959 "2549 FCF disconnected fron network index 0x%x"
2960 " tag 0x%x \n", acqe_fcoe->fcf_index,
2961 acqe_fcoe->event_tag);
2962 /* If the event is not for currently used fcf do nothing */
2963 if (phba->fcf.fcf_indx != acqe_fcoe->fcf_index)
2964 break;
2965 /*
2966 * Currently, driver support only one FCF - so treat this as
2967 * a link down.
2968 */
2969 lpfc_linkdown(phba);
2970 /* Unregister FCF if no devices connected to it */
2971 lpfc_unregister_unused_fcf(phba);
2972 break;
2973
2974 default:
2975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2976 "0288 Unknown FCoE event type 0x%x event tag "
2977 "0x%x\n", event_type, acqe_fcoe->event_tag);
2978 break;
2979 }
2980}
2981
2982/**
2983 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
2984 * @phba: pointer to lpfc hba data structure.
2985 * @acqe_link: pointer to the async dcbx completion queue entry.
2986 *
2987 * This routine is to handle the SLI4 asynchronous dcbx event.
2988 **/
2989static void
2990lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
2991 struct lpfc_acqe_dcbx *acqe_dcbx)
2992{
2993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2994 "0290 The SLI4 DCBX asynchronous event is not "
2995 "handled yet\n");
2996}
2997
2998/**
2999 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3000 * @phba: pointer to lpfc hba data structure.
3001 *
3002 * This routine is invoked by the worker thread to process all the pending
3003 * SLI4 asynchronous events.
3004 **/
3005void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3006{
3007 struct lpfc_cq_event *cq_event;
3008
3009 /* First, declare the async event has been handled */
3010 spin_lock_irq(&phba->hbalock);
3011 phba->hba_flag &= ~ASYNC_EVENT;
3012 spin_unlock_irq(&phba->hbalock);
3013 /* Now, handle all the async events */
3014 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3015 /* Get the first event from the head of the event queue */
3016 spin_lock_irq(&phba->hbalock);
3017 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3018 cq_event, struct lpfc_cq_event, list);
3019 spin_unlock_irq(&phba->hbalock);
3020 /* Process the asynchronous event */
3021 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3022 case LPFC_TRAILER_CODE_LINK:
3023 lpfc_sli4_async_link_evt(phba,
3024 &cq_event->cqe.acqe_link);
3025 break;
3026 case LPFC_TRAILER_CODE_FCOE:
3027 lpfc_sli4_async_fcoe_evt(phba,
3028 &cq_event->cqe.acqe_fcoe);
3029 break;
3030 case LPFC_TRAILER_CODE_DCBX:
3031 lpfc_sli4_async_dcbx_evt(phba,
3032 &cq_event->cqe.acqe_dcbx);
3033 break;
3034 default:
3035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3036 "1804 Invalid asynchrous event code: "
3037 "x%x\n", bf_get(lpfc_trailer_code,
3038 &cq_event->cqe.mcqe_cmpl));
3039 break;
3040 }
3041 /* Free the completion event processed to the free pool */
3042 lpfc_sli4_cq_event_release(phba, cq_event);
3043 }
3044}
3045
3046/**
3047 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3048 * @phba: pointer to lpfc hba data structure.
3049 * @dev_grp: The HBA PCI-Device group number.
3050 *
3051 * This routine is invoked to set up the per HBA PCI-Device group function
3052 * API jump table entries.
3053 *
3054 * Return: 0 if success, otherwise -ENODEV
3055 **/
3056int
3057lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3058{
3059 int rc;
3060
3061 /* Set up lpfc PCI-device group */
3062 phba->pci_dev_grp = dev_grp;
3063
3064 /* The LPFC_PCI_DEV_OC uses SLI4 */
3065 if (dev_grp == LPFC_PCI_DEV_OC)
3066 phba->sli_rev = LPFC_SLI_REV4;
3067
3068 /* Set up device INIT API function jump table */
3069 rc = lpfc_init_api_table_setup(phba, dev_grp);
3070 if (rc)
3071 return -ENODEV;
3072 /* Set up SCSI API function jump table */
3073 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3074 if (rc)
3075 return -ENODEV;
3076 /* Set up SLI API function jump table */
3077 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3078 if (rc)
3079 return -ENODEV;
3080 /* Set up MBOX API function jump table */
3081 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3082 if (rc)
3083 return -ENODEV;
3084
3085 return 0;
5b75da2f
JS
3086}
3087
3088/**
3621a710 3089 * lpfc_log_intr_mode - Log the active interrupt mode
5b75da2f
JS
3090 * @phba: pointer to lpfc hba data structure.
3091 * @intr_mode: active interrupt mode adopted.
3092 *
3093 * This routine it invoked to log the currently used active interrupt mode
3094 * to the device.
3772a991
JS
3095 **/
3096static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5b75da2f
JS
3097{
3098 switch (intr_mode) {
3099 case 0:
3100 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3101 "0470 Enable INTx interrupt mode.\n");
3102 break;
3103 case 1:
3104 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3105 "0481 Enabled MSI interrupt mode.\n");
3106 break;
3107 case 2:
3108 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3109 "0480 Enabled MSI-X interrupt mode.\n");
3110 break;
3111 default:
3112 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3113 "0482 Illegal interrupt mode.\n");
3114 break;
3115 }
3116 return;
3117}
3118
5b75da2f 3119/**
3772a991 3120 * lpfc_enable_pci_dev - Enable a generic PCI device.
5b75da2f
JS
3121 * @phba: pointer to lpfc hba data structure.
3122 *
3772a991
JS
3123 * This routine is invoked to enable the PCI device that is common to all
3124 * PCI devices.
5b75da2f
JS
3125 *
3126 * Return codes
3772a991
JS
3127 * 0 - sucessful
3128 * other values - error
5b75da2f 3129 **/
3772a991
JS
3130static int
3131lpfc_enable_pci_dev(struct lpfc_hba *phba)
5b75da2f 3132{
3772a991
JS
3133 struct pci_dev *pdev;
3134 int bars;
5b75da2f 3135
3772a991
JS
3136 /* Obtain PCI device reference */
3137 if (!phba->pcidev)
3138 goto out_error;
3139 else
3140 pdev = phba->pcidev;
3141 /* Select PCI BARs */
3142 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3143 /* Enable PCI device */
3144 if (pci_enable_device_mem(pdev))
3145 goto out_error;
3146 /* Request PCI resource for the device */
3147 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3148 goto out_disable_device;
3149 /* Set up device as PCI master and save state for EEH */
3150 pci_set_master(pdev);
3151 pci_try_set_mwi(pdev);
3152 pci_save_state(pdev);
5b75da2f 3153
3772a991 3154 return 0;
5b75da2f 3155
3772a991
JS
3156out_disable_device:
3157 pci_disable_device(pdev);
3158out_error:
3159 return -ENODEV;
5b75da2f
JS
3160}
3161
3162/**
3772a991 3163 * lpfc_disable_pci_dev - Disable a generic PCI device.
5b75da2f
JS
3164 * @phba: pointer to lpfc hba data structure.
3165 *
3772a991
JS
3166 * This routine is invoked to disable the PCI device that is common to all
3167 * PCI devices.
5b75da2f
JS
3168 **/
3169static void
3772a991 3170lpfc_disable_pci_dev(struct lpfc_hba *phba)
5b75da2f 3171{
3772a991
JS
3172 struct pci_dev *pdev;
3173 int bars;
5b75da2f 3174
3772a991
JS
3175 /* Obtain PCI device reference */
3176 if (!phba->pcidev)
3177 return;
3178 else
3179 pdev = phba->pcidev;
3180 /* Select PCI BARs */
3181 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3182 /* Release PCI resource and disable PCI device */
3183 pci_release_selected_regions(pdev, bars);
3184 pci_disable_device(pdev);
3185 /* Null out PCI private reference to driver */
3186 pci_set_drvdata(pdev, NULL);
5b75da2f
JS
3187
3188 return;
3189}
3190
e59058c4 3191/**
3772a991
JS
3192 * lpfc_reset_hba - Reset a hba
3193 * @phba: pointer to lpfc hba data structure.
e59058c4 3194 *
3772a991
JS
3195 * This routine is invoked to reset a hba device. It brings the HBA
3196 * offline, performs a board restart, and then brings the board back
3197 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3198 * on outstanding mailbox commands.
e59058c4 3199 **/
3772a991
JS
3200void
3201lpfc_reset_hba(struct lpfc_hba *phba)
dea3101e 3202{
3772a991
JS
3203 /* If resets are disabled then set error state and return. */
3204 if (!phba->cfg_enable_hba_reset) {
3205 phba->link_state = LPFC_HBA_ERROR;
3206 return;
3207 }
3208 lpfc_offline_prep(phba);
3209 lpfc_offline(phba);
3210 lpfc_sli_brdrestart(phba);
3211 lpfc_online(phba);
3212 lpfc_unblock_mgmt_io(phba);
3213}
dea3101e 3214
3772a991
JS
3215/**
3216 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3217 * @phba: pointer to lpfc hba data structure.
3218 *
3219 * This routine is invoked to set up the driver internal resources specific to
3220 * support the SLI-3 HBA device it attached to.
3221 *
3222 * Return codes
3223 * 0 - sucessful
3224 * other values - error
3225 **/
3226static int
3227lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3228{
3229 struct lpfc_sli *psli;
dea3101e 3230
2e0fef85 3231 /*
3772a991 3232 * Initialize timers used by driver
2e0fef85 3233 */
dea3101e 3234
3772a991 3235 /* Heartbeat timer */
858c9f6c
JS
3236 init_timer(&phba->hb_tmofunc);
3237 phba->hb_tmofunc.function = lpfc_hb_timeout;
3238 phba->hb_tmofunc.data = (unsigned long)phba;
3239
dea3101e 3240 psli = &phba->sli;
3772a991 3241 /* MBOX heartbeat timer */
dea3101e
JB
3242 init_timer(&psli->mbox_tmo);
3243 psli->mbox_tmo.function = lpfc_mbox_timeout;
2e0fef85 3244 psli->mbox_tmo.data = (unsigned long) phba;
3772a991 3245 /* FCP polling mode timer */
875fbdfe
JSEC
3246 init_timer(&phba->fcp_poll_timer);
3247 phba->fcp_poll_timer.function = lpfc_poll_timeout;
2e0fef85 3248 phba->fcp_poll_timer.data = (unsigned long) phba;
3772a991 3249 /* Fabric block timer */
92d7f7b0
JS
3250 init_timer(&phba->fabric_block_timer);
3251 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3252 phba->fabric_block_timer.data = (unsigned long) phba;
3772a991 3253 /* EA polling mode timer */
9399627f
JS
3254 init_timer(&phba->eratt_poll);
3255 phba->eratt_poll.function = lpfc_poll_eratt;
3256 phba->eratt_poll.data = (unsigned long) phba;
dea3101e 3257
3772a991
JS
3258 /* Host attention work mask setup */
3259 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3260 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
dea3101e 3261
3772a991
JS
3262 /* Get all the module params for configuring this host */
3263 lpfc_get_cfgparam(phba);
dea3101e 3264 /*
3772a991
JS
3265 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3266 * used to create the sg_dma_buf_pool must be dynamically calculated.
3267 * 2 segments are added since the IOCB needs a command and response bde.
dea3101e 3268 */
3772a991
JS
3269 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3270 sizeof(struct fcp_rsp) +
3271 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3272
3273 if (phba->cfg_enable_bg) {
3274 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3275 phba->cfg_sg_dma_buf_size +=
3276 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
901a920f 3277 }
dea3101e 3278
3772a991
JS
3279 /* Also reinitialize the host templates with new values. */
3280 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3281 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
dea3101e 3282
3772a991
JS
3283 phba->max_vpi = LPFC_MAX_VPI;
3284 /* This will be set to correct value after config_port mbox */
3285 phba->max_vports = 0;
dea3101e 3286
3772a991
JS
3287 /*
3288 * Initialize the SLI Layer to run with lpfc HBAs.
3289 */
3290 lpfc_sli_setup(phba);
3291 lpfc_sli_queue_setup(phba);
ed957684 3292
3772a991
JS
3293 /* Allocate device driver memory */
3294 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3295 return -ENOMEM;
51ef4c26 3296
3772a991
JS
3297 return 0;
3298}
ed957684 3299
3772a991
JS
3300/**
3301 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3302 * @phba: pointer to lpfc hba data structure.
3303 *
3304 * This routine is invoked to unset the driver internal resources set up
3305 * specific for supporting the SLI-3 HBA device it attached to.
3306 **/
3307static void
3308lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3309{
3310 /* Free device driver memory allocated */
3311 lpfc_mem_free_all(phba);
3163f725 3312
3772a991
JS
3313 return;
3314}
dea3101e 3315
3772a991 3316/**
da0436e9 3317 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3772a991
JS
3318 * @phba: pointer to lpfc hba data structure.
3319 *
da0436e9
JS
3320 * This routine is invoked to set up the driver internal resources specific to
3321 * support the SLI-4 HBA device it attached to.
3772a991
JS
3322 *
3323 * Return codes
da0436e9
JS
3324 * 0 - sucessful
3325 * other values - error
3772a991
JS
3326 **/
3327static int
da0436e9 3328lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3772a991 3329{
da0436e9
JS
3330 struct lpfc_sli *psli;
3331 int rc;
3332 int i, hbq_count;
3333
3334 /* Before proceed, wait for POST done and device ready */
3335 rc = lpfc_sli4_post_status_check(phba);
3336 if (rc)
3337 return -ENODEV;
3338
3772a991 3339 /*
da0436e9 3340 * Initialize timers used by driver
3772a991 3341 */
3772a991 3342
da0436e9
JS
3343 /* Heartbeat timer */
3344 init_timer(&phba->hb_tmofunc);
3345 phba->hb_tmofunc.function = lpfc_hb_timeout;
3346 phba->hb_tmofunc.data = (unsigned long)phba;
3772a991 3347
da0436e9
JS
3348 psli = &phba->sli;
3349 /* MBOX heartbeat timer */
3350 init_timer(&psli->mbox_tmo);
3351 psli->mbox_tmo.function = lpfc_mbox_timeout;
3352 psli->mbox_tmo.data = (unsigned long) phba;
3353 /* Fabric block timer */
3354 init_timer(&phba->fabric_block_timer);
3355 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3356 phba->fabric_block_timer.data = (unsigned long) phba;
3357 /* EA polling mode timer */
3358 init_timer(&phba->eratt_poll);
3359 phba->eratt_poll.function = lpfc_poll_eratt;
3360 phba->eratt_poll.data = (unsigned long) phba;
3361 /*
3362 * We need to do a READ_CONFIG mailbox command here before
3363 * calling lpfc_get_cfgparam. For VFs this will report the
3364 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3365 * All of the resources allocated
3366 * for this Port are tied to these values.
3367 */
3368 /* Get all the module params for configuring this host */
3369 lpfc_get_cfgparam(phba);
3370 phba->max_vpi = LPFC_MAX_VPI;
3371 /* This will be set to correct value after the read_config mbox */
3372 phba->max_vports = 0;
3772a991 3373
da0436e9
JS
3374 /* Program the default value of vlan_id and fc_map */
3375 phba->valid_vlan = 0;
3376 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3377 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3378 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3772a991 3379
da0436e9
JS
3380 /*
3381 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3382 * used to create the sg_dma_buf_pool must be dynamically calculated.
3383 * 2 segments are added since the IOCB needs a command and response bde.
3384 * To insure that the scsi sgl does not cross a 4k page boundary only
3385 * sgl sizes of 1k, 2k, 4k, and 8k are supported.
3386 * Table of sgl sizes and seg_cnt:
3387 * sgl size, sg_seg_cnt total seg
3388 * 1k 50 52
3389 * 2k 114 116
3390 * 4k 242 244
3391 * 8k 498 500
3392 * cmd(32) + rsp(160) + (52 * sizeof(sli4_sge)) = 1024
3393 * cmd(32) + rsp(160) + (116 * sizeof(sli4_sge)) = 2048
3394 * cmd(32) + rsp(160) + (244 * sizeof(sli4_sge)) = 4096
3395 * cmd(32) + rsp(160) + (500 * sizeof(sli4_sge)) = 8192
3396 */
3397 if (phba->cfg_sg_seg_cnt <= LPFC_DEFAULT_SG_SEG_CNT)
3398 phba->cfg_sg_seg_cnt = 50;
3399 else if (phba->cfg_sg_seg_cnt <= 114)
3400 phba->cfg_sg_seg_cnt = 114;
3401 else if (phba->cfg_sg_seg_cnt <= 242)
3402 phba->cfg_sg_seg_cnt = 242;
3403 else
3404 phba->cfg_sg_seg_cnt = 498;
3772a991 3405
da0436e9
JS
3406 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd)
3407 + sizeof(struct fcp_rsp);
3408 phba->cfg_sg_dma_buf_size +=
3409 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
3772a991 3410
da0436e9
JS
3411 /* Initialize buffer queue management fields */
3412 hbq_count = lpfc_sli_hbq_count();
3413 for (i = 0; i < hbq_count; ++i)
3414 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3415 INIT_LIST_HEAD(&phba->rb_pend_list);
3416 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3417 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3772a991 3418
da0436e9
JS
3419 /*
3420 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3421 */
3422 /* Initialize the Abort scsi buffer list used by driver */
3423 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3424 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3425 /* This abort list used by worker thread */
3426 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3772a991 3427
da0436e9
JS
3428 /*
3429 * Initialize dirver internal slow-path work queues
3430 */
3772a991 3431
da0436e9
JS
3432 /* Driver internel slow-path CQ Event pool */
3433 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3434 /* Response IOCB work queue list */
3435 INIT_LIST_HEAD(&phba->sli4_hba.sp_rspiocb_work_queue);
3436 /* Asynchronous event CQ Event work queue list */
3437 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3438 /* Fast-path XRI aborted CQ Event work queue list */
3439 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3440 /* Slow-path XRI aborted CQ Event work queue list */
3441 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3442 /* Receive queue CQ Event work queue list */
3443 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3444
3445 /* Initialize the driver internal SLI layer lists. */
3446 lpfc_sli_setup(phba);
3447 lpfc_sli_queue_setup(phba);
3772a991 3448
da0436e9
JS
3449 /* Allocate device driver memory */
3450 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3451 if (rc)
3452 return -ENOMEM;
3453
3454 /* Create the bootstrap mailbox command */
3455 rc = lpfc_create_bootstrap_mbox(phba);
3456 if (unlikely(rc))
3457 goto out_free_mem;
3458
3459 /* Set up the host's endian order with the device. */
3460 rc = lpfc_setup_endian_order(phba);
3461 if (unlikely(rc))
3462 goto out_free_bsmbx;
3463
3464 /* Set up the hba's configuration parameters. */
3465 rc = lpfc_sli4_read_config(phba);
3466 if (unlikely(rc))
3467 goto out_free_bsmbx;
3468
3469 /* Perform a function reset */
3470 rc = lpfc_pci_function_reset(phba);
3471 if (unlikely(rc))
3472 goto out_free_bsmbx;
3473
3474 /* Create all the SLI4 queues */
3475 rc = lpfc_sli4_queue_create(phba);
3476 if (rc)
3477 goto out_free_bsmbx;
3478
3479 /* Create driver internal CQE event pool */
3480 rc = lpfc_sli4_cq_event_pool_create(phba);
3481 if (rc)
3482 goto out_destroy_queue;
3483
3484 /* Initialize and populate the iocb list per host */
3485 rc = lpfc_init_sgl_list(phba);
3486 if (rc) {
3487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3488 "1400 Failed to initialize sgl list.\n");
3489 goto out_destroy_cq_event_pool;
3490 }
3491 rc = lpfc_init_active_sgl_array(phba);
3492 if (rc) {
3493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3494 "1430 Failed to initialize sgl list.\n");
3495 goto out_free_sgl_list;
3496 }
3497
3498 rc = lpfc_sli4_init_rpi_hdrs(phba);
3499 if (rc) {
3500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3501 "1432 Failed to initialize rpi headers.\n");
3502 goto out_free_active_sgl;
3503 }
3504
3505 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3506 phba->cfg_fcp_eq_count), GFP_KERNEL);
3507 if (!phba->sli4_hba.fcp_eq_hdl) {
3508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3509 "2572 Failed allocate memory for fast-path "
3510 "per-EQ handle array\n");
3511 goto out_remove_rpi_hdrs;
3512 }
3513
3514 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3515 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3516 if (!phba->sli4_hba.msix_entries) {
3517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3518 "2573 Failed allocate memory for msi-x "
3519 "interrupt vector entries\n");
3520 goto out_free_fcp_eq_hdl;
3521 }
3522
3523 return rc;
3524
3525out_free_fcp_eq_hdl:
3526 kfree(phba->sli4_hba.fcp_eq_hdl);
3527out_remove_rpi_hdrs:
3528 lpfc_sli4_remove_rpi_hdrs(phba);
3529out_free_active_sgl:
3530 lpfc_free_active_sgl(phba);
3531out_free_sgl_list:
3532 lpfc_free_sgl_list(phba);
3533out_destroy_cq_event_pool:
3534 lpfc_sli4_cq_event_pool_destroy(phba);
3535out_destroy_queue:
3536 lpfc_sli4_queue_destroy(phba);
3537out_free_bsmbx:
3538 lpfc_destroy_bootstrap_mbox(phba);
3539out_free_mem:
3540 lpfc_mem_free(phba);
3541 return rc;
3542}
3543
3544/**
3545 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3546 * @phba: pointer to lpfc hba data structure.
3547 *
3548 * This routine is invoked to unset the driver internal resources set up
3549 * specific for supporting the SLI-4 HBA device it attached to.
3550 **/
3551static void
3552lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3553{
3554 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3555
3556 /* unregister default FCFI from the HBA */
3557 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3558
3559 /* Free the default FCR table */
3560 lpfc_sli_remove_dflt_fcf(phba);
3561
3562 /* Free memory allocated for msi-x interrupt vector entries */
3563 kfree(phba->sli4_hba.msix_entries);
3564
3565 /* Free memory allocated for fast-path work queue handles */
3566 kfree(phba->sli4_hba.fcp_eq_hdl);
3567
3568 /* Free the allocated rpi headers. */
3569 lpfc_sli4_remove_rpi_hdrs(phba);
d11e31dd 3570 lpfc_sli4_remove_rpis(phba);
da0436e9
JS
3571
3572 /* Free the ELS sgl list */
3573 lpfc_free_active_sgl(phba);
3574 lpfc_free_sgl_list(phba);
3575
3576 /* Free the SCSI sgl management array */
3577 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3578
3579 /* Free the SLI4 queues */
3580 lpfc_sli4_queue_destroy(phba);
3581
3582 /* Free the completion queue EQ event pool */
3583 lpfc_sli4_cq_event_release_all(phba);
3584 lpfc_sli4_cq_event_pool_destroy(phba);
3585
3586 /* Reset SLI4 HBA FCoE function */
3587 lpfc_pci_function_reset(phba);
3588
3589 /* Free the bsmbx region. */
3590 lpfc_destroy_bootstrap_mbox(phba);
3591
3592 /* Free the SLI Layer memory with SLI4 HBAs */
3593 lpfc_mem_free_all(phba);
3594
3595 /* Free the current connect table */
3596 list_for_each_entry_safe(conn_entry, next_conn_entry,
3597 &phba->fcf_conn_rec_list, list)
3598 kfree(conn_entry);
3599
3600 return;
3601}
3602
3603/**
3604 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3605 * @phba: The hba struct for which this call is being executed.
3606 * @dev_grp: The HBA PCI-Device group number.
3607 *
3608 * This routine sets up the device INIT interface API function jump table
3609 * in @phba struct.
3610 *
3611 * Returns: 0 - success, -ENODEV - failure.
3612 **/
3613int
3614lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3615{
3616 switch (dev_grp) {
3617 case LPFC_PCI_DEV_LP:
3618 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3619 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3620 phba->lpfc_stop_port = lpfc_stop_port_s3;
3621 break;
3622 case LPFC_PCI_DEV_OC:
3623 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3624 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3625 phba->lpfc_stop_port = lpfc_stop_port_s4;
3626 break;
3627 default:
3628 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3629 "1431 Invalid HBA PCI-device group: 0x%x\n",
3630 dev_grp);
3631 return -ENODEV;
3632 break;
3633 }
3634 return 0;
3635}
3636
3637/**
3638 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3639 * @phba: pointer to lpfc hba data structure.
3640 *
3641 * This routine is invoked to set up the driver internal resources before the
3642 * device specific resource setup to support the HBA device it attached to.
3643 *
3644 * Return codes
3645 * 0 - sucessful
3646 * other values - error
3647 **/
3648static int
3649lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3650{
3651 /*
3652 * Driver resources common to all SLI revisions
3653 */
3654 atomic_set(&phba->fast_event_count, 0);
3655 spin_lock_init(&phba->hbalock);
3656
3657 /* Initialize ndlp management spinlock */
3658 spin_lock_init(&phba->ndlp_lock);
3659
3660 INIT_LIST_HEAD(&phba->port_list);
3661 INIT_LIST_HEAD(&phba->work_list);
3662 init_waitqueue_head(&phba->wait_4_mlo_m_q);
3663
3664 /* Initialize the wait queue head for the kernel thread */
3665 init_waitqueue_head(&phba->work_waitq);
3666
3667 /* Initialize the scsi buffer list used by driver for scsi IO */
3668 spin_lock_init(&phba->scsi_buf_list_lock);
3669 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
3670
3671 /* Initialize the fabric iocb list */
3672 INIT_LIST_HEAD(&phba->fabric_iocb_list);
3673
3674 /* Initialize list to save ELS buffers */
3675 INIT_LIST_HEAD(&phba->elsbuf);
3676
3677 /* Initialize FCF connection rec list */
3678 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
3679
3680 return 0;
3681}
3682
3683/**
3684 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
3685 * @phba: pointer to lpfc hba data structure.
3686 *
3687 * This routine is invoked to set up the driver internal resources after the
3688 * device specific resource setup to support the HBA device it attached to.
3689 *
3690 * Return codes
3691 * 0 - sucessful
3692 * other values - error
3693 **/
3694static int
3695lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
3696{
3697 int error;
3698
3699 /* Startup the kernel thread for this host adapter. */
3700 phba->worker_thread = kthread_run(lpfc_do_work, phba,
3701 "lpfc_worker_%d", phba->brd_no);
3702 if (IS_ERR(phba->worker_thread)) {
3703 error = PTR_ERR(phba->worker_thread);
3704 return error;
3772a991
JS
3705 }
3706
3707 return 0;
3708}
3709
3710/**
3711 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
3712 * @phba: pointer to lpfc hba data structure.
3713 *
3714 * This routine is invoked to unset the driver internal resources set up after
3715 * the device specific resource setup for supporting the HBA device it
3716 * attached to.
3717 **/
3718static void
3719lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
3720{
3721 /* Stop kernel worker thread */
3722 kthread_stop(phba->worker_thread);
3723}
3724
3725/**
3726 * lpfc_free_iocb_list - Free iocb list.
3727 * @phba: pointer to lpfc hba data structure.
3728 *
3729 * This routine is invoked to free the driver's IOCB list and memory.
3730 **/
3731static void
3732lpfc_free_iocb_list(struct lpfc_hba *phba)
3733{
3734 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
3735
3736 spin_lock_irq(&phba->hbalock);
3737 list_for_each_entry_safe(iocbq_entry, iocbq_next,
3738 &phba->lpfc_iocb_list, list) {
3739 list_del(&iocbq_entry->list);
3740 kfree(iocbq_entry);
3741 phba->total_iocbq_bufs--;
98c9ea5c 3742 }
3772a991
JS
3743 spin_unlock_irq(&phba->hbalock);
3744
3745 return;
3746}
3747
3748/**
3749 * lpfc_init_iocb_list - Allocate and initialize iocb list.
3750 * @phba: pointer to lpfc hba data structure.
3751 *
3752 * This routine is invoked to allocate and initizlize the driver's IOCB
3753 * list and set up the IOCB tag array accordingly.
3754 *
3755 * Return codes
3756 * 0 - sucessful
3757 * other values - error
3758 **/
3759static int
3760lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
3761{
3762 struct lpfc_iocbq *iocbq_entry = NULL;
3763 uint16_t iotag;
3764 int i;
dea3101e
JB
3765
3766 /* Initialize and populate the iocb list per host. */
3767 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
3772a991 3768 for (i = 0; i < iocb_count; i++) {
dd00cc48 3769 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea3101e
JB
3770 if (iocbq_entry == NULL) {
3771 printk(KERN_ERR "%s: only allocated %d iocbs of "
3772 "expected %d count. Unloading driver.\n",
cadbd4a5 3773 __func__, i, LPFC_IOCB_LIST_CNT);
dea3101e
JB
3774 goto out_free_iocbq;
3775 }
3776
604a3e30
JB
3777 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
3778 if (iotag == 0) {
3772a991 3779 kfree(iocbq_entry);
604a3e30 3780 printk(KERN_ERR "%s: failed to allocate IOTAG. "
3772a991 3781 "Unloading driver.\n", __func__);
604a3e30
JB
3782 goto out_free_iocbq;
3783 }
3772a991 3784 iocbq_entry->sli4_xritag = NO_XRI;
2e0fef85
JS
3785
3786 spin_lock_irq(&phba->hbalock);
dea3101e
JB
3787 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
3788 phba->total_iocbq_bufs++;
2e0fef85 3789 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
3790 }
3791
3772a991 3792 return 0;
dea3101e 3793
3772a991
JS
3794out_free_iocbq:
3795 lpfc_free_iocb_list(phba);
dea3101e 3796
3772a991
JS
3797 return -ENOMEM;
3798}
5e9d9b82 3799
3772a991 3800/**
da0436e9
JS
3801 * lpfc_free_sgl_list - Free sgl list.
3802 * @phba: pointer to lpfc hba data structure.
3772a991 3803 *
da0436e9 3804 * This routine is invoked to free the driver's sgl list and memory.
3772a991 3805 **/
da0436e9
JS
3806static void
3807lpfc_free_sgl_list(struct lpfc_hba *phba)
3772a991 3808{
da0436e9
JS
3809 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
3810 LIST_HEAD(sglq_list);
3811 int rc = 0;
dea3101e 3812
da0436e9
JS
3813 spin_lock_irq(&phba->hbalock);
3814 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
3815 spin_unlock_irq(&phba->hbalock);
dea3101e 3816
da0436e9
JS
3817 list_for_each_entry_safe(sglq_entry, sglq_next,
3818 &sglq_list, list) {
3819 list_del(&sglq_entry->list);
3820 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
3821 kfree(sglq_entry);
3822 phba->sli4_hba.total_sglq_bufs--;
3823 }
3824 rc = lpfc_sli4_remove_all_sgl_pages(phba);
3825 if (rc) {
3826 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3827 "2005 Unable to deregister pages from HBA: %x", rc);
3772a991 3828 }
da0436e9
JS
3829 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3830}
92d7f7b0 3831
da0436e9
JS
3832/**
3833 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
3834 * @phba: pointer to lpfc hba data structure.
3835 *
3836 * This routine is invoked to allocate the driver's active sgl memory.
3837 * This array will hold the sglq_entry's for active IOs.
3838 **/
3839static int
3840lpfc_init_active_sgl_array(struct lpfc_hba *phba)
3841{
3842 int size;
3843 size = sizeof(struct lpfc_sglq *);
3844 size *= phba->sli4_hba.max_cfg_param.max_xri;
3845
3846 phba->sli4_hba.lpfc_sglq_active_list =
3847 kzalloc(size, GFP_KERNEL);
3848 if (!phba->sli4_hba.lpfc_sglq_active_list)
3849 return -ENOMEM;
3850 return 0;
3772a991
JS
3851}
3852
3853/**
da0436e9 3854 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3772a991
JS
3855 * @phba: pointer to lpfc hba data structure.
3856 *
da0436e9
JS
3857 * This routine is invoked to walk through the array of active sglq entries
3858 * and free all of the resources.
3859 * This is just a place holder for now.
3772a991
JS
3860 **/
3861static void
da0436e9 3862lpfc_free_active_sgl(struct lpfc_hba *phba)
3772a991 3863{
da0436e9 3864 kfree(phba->sli4_hba.lpfc_sglq_active_list);
3772a991
JS
3865}
3866
3867/**
da0436e9 3868 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3772a991
JS
3869 * @phba: pointer to lpfc hba data structure.
3870 *
da0436e9
JS
3871 * This routine is invoked to allocate and initizlize the driver's sgl
3872 * list and set up the sgl xritag tag array accordingly.
3772a991
JS
3873 *
3874 * Return codes
da0436e9
JS
3875 * 0 - sucessful
3876 * other values - error
3772a991
JS
3877 **/
3878static int
da0436e9 3879lpfc_init_sgl_list(struct lpfc_hba *phba)
3772a991 3880{
da0436e9
JS
3881 struct lpfc_sglq *sglq_entry = NULL;
3882 int i;
3883 int els_xri_cnt;
3884
3885 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3886 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3887 "2400 lpfc_init_sgl_list els %d.\n",
3888 els_xri_cnt);
3889 /* Initialize and populate the sglq list per host/VF. */
3890 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
3891 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
3892
3893 /* Sanity check on XRI management */
3894 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
3895 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3896 "2562 No room left for SCSI XRI allocation: "
3897 "max_xri=%d, els_xri=%d\n",
3898 phba->sli4_hba.max_cfg_param.max_xri,
3899 els_xri_cnt);
3900 return -ENOMEM;
3901 }
3772a991 3902
da0436e9
JS
3903 /* Allocate memory for the ELS XRI management array */
3904 phba->sli4_hba.lpfc_els_sgl_array =
3905 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
3906 GFP_KERNEL);
0ff10d46 3907
da0436e9
JS
3908 if (!phba->sli4_hba.lpfc_els_sgl_array) {
3909 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3910 "2401 Failed to allocate memory for ELS "
3911 "XRI management array of size %d.\n",
3912 els_xri_cnt);
3913 return -ENOMEM;
3914 }
2e0fef85 3915
da0436e9
JS
3916 /* Keep the SCSI XRI into the XRI management array */
3917 phba->sli4_hba.scsi_xri_max =
3918 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3919 phba->sli4_hba.scsi_xri_cnt = 0;
3920
3921 phba->sli4_hba.lpfc_scsi_psb_array =
3922 kzalloc((sizeof(struct lpfc_scsi_buf *) *
3923 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
3924
3925 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
3926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3927 "2563 Failed to allocate memory for SCSI "
3928 "XRI management array of size %d.\n",
3929 phba->sli4_hba.scsi_xri_max);
3930 kfree(phba->sli4_hba.lpfc_els_sgl_array);
3931 return -ENOMEM;
3932 }
3933
3934 for (i = 0; i < els_xri_cnt; i++) {
3935 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
3936 if (sglq_entry == NULL) {
3937 printk(KERN_ERR "%s: only allocated %d sgls of "
3938 "expected %d count. Unloading driver.\n",
3939 __func__, i, els_xri_cnt);
3940 goto out_free_mem;
3941 }
3942
3943 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
3944 if (sglq_entry->sli4_xritag == NO_XRI) {
3945 kfree(sglq_entry);
3946 printk(KERN_ERR "%s: failed to allocate XRI.\n"
3947 "Unloading driver.\n", __func__);
3948 goto out_free_mem;
3949 }
3950 sglq_entry->buff_type = GEN_BUFF_TYPE;
3951 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
3952 if (sglq_entry->virt == NULL) {
3953 kfree(sglq_entry);
3954 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
3955 "Unloading driver.\n", __func__);
3956 goto out_free_mem;
3957 }
3958 sglq_entry->sgl = sglq_entry->virt;
3959 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3960
3961 /* The list order is used by later block SGL registraton */
3962 spin_lock_irq(&phba->hbalock);
3963 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
3964 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
3965 phba->sli4_hba.total_sglq_bufs++;
3966 spin_unlock_irq(&phba->hbalock);
3967 }
3968 return 0;
3969
3970out_free_mem:
3971 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3972 lpfc_free_sgl_list(phba);
3973 return -ENOMEM;
3974}
3975
3976/**
3977 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
3978 * @phba: pointer to lpfc hba data structure.
3979 *
3980 * This routine is invoked to post rpi header templates to the
3981 * HBA consistent with the SLI-4 interface spec. This routine
3982 * posts a PAGE_SIZE memory region to the port to hold up to
3983 * PAGE_SIZE modulo 64 rpi context headers.
3984 * No locks are held here because this is an initialization routine
3985 * called only from probe or lpfc_online when interrupts are not
3986 * enabled and the driver is reinitializing the device.
3987 *
3988 * Return codes
3989 * 0 - sucessful
3990 * ENOMEM - No availble memory
3991 * EIO - The mailbox failed to complete successfully.
3992 **/
3993int
3994lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
3995{
3996 int rc = 0;
3997 int longs;
3998 uint16_t rpi_count;
3999 struct lpfc_rpi_hdr *rpi_hdr;
4000
4001 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4002
4003 /*
4004 * Provision an rpi bitmask range for discovery. The total count
4005 * is the difference between max and base + 1.
4006 */
4007 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4008 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4009
4010 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4011 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4012 GFP_KERNEL);
4013 if (!phba->sli4_hba.rpi_bmask)
4014 return -ENOMEM;
4015
4016 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4017 if (!rpi_hdr) {
4018 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4019 "0391 Error during rpi post operation\n");
4020 lpfc_sli4_remove_rpis(phba);
4021 rc = -ENODEV;
4022 }
4023
4024 return rc;
4025}
4026
4027/**
4028 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4029 * @phba: pointer to lpfc hba data structure.
4030 *
4031 * This routine is invoked to allocate a single 4KB memory region to
4032 * support rpis and stores them in the phba. This single region
4033 * provides support for up to 64 rpis. The region is used globally
4034 * by the device.
4035 *
4036 * Returns:
4037 * A valid rpi hdr on success.
4038 * A NULL pointer on any failure.
4039 **/
4040struct lpfc_rpi_hdr *
4041lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4042{
4043 uint16_t rpi_limit, curr_rpi_range;
4044 struct lpfc_dmabuf *dmabuf;
4045 struct lpfc_rpi_hdr *rpi_hdr;
4046
4047 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4048 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4049
4050 spin_lock_irq(&phba->hbalock);
4051 curr_rpi_range = phba->sli4_hba.next_rpi;
4052 spin_unlock_irq(&phba->hbalock);
4053
4054 /*
4055 * The port has a limited number of rpis. The increment here
4056 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4057 * and to allow the full max_rpi range per port.
4058 */
4059 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4060 return NULL;
4061
4062 /*
4063 * First allocate the protocol header region for the port. The
4064 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4065 */
4066 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4067 if (!dmabuf)
4068 return NULL;
4069
4070 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4071 LPFC_HDR_TEMPLATE_SIZE,
4072 &dmabuf->phys,
4073 GFP_KERNEL);
4074 if (!dmabuf->virt) {
4075 rpi_hdr = NULL;
4076 goto err_free_dmabuf;
4077 }
4078
4079 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4080 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4081 rpi_hdr = NULL;
4082 goto err_free_coherent;
4083 }
4084
4085 /* Save the rpi header data for cleanup later. */
4086 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4087 if (!rpi_hdr)
4088 goto err_free_coherent;
4089
4090 rpi_hdr->dmabuf = dmabuf;
4091 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4092 rpi_hdr->page_count = 1;
4093 spin_lock_irq(&phba->hbalock);
4094 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4095 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4096
4097 /*
4098 * The next_rpi stores the next module-64 rpi value to post
4099 * in any subsequent rpi memory region postings.
4100 */
4101 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4102 spin_unlock_irq(&phba->hbalock);
4103 return rpi_hdr;
4104
4105 err_free_coherent:
4106 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4107 dmabuf->virt, dmabuf->phys);
4108 err_free_dmabuf:
4109 kfree(dmabuf);
4110 return NULL;
4111}
4112
4113/**
4114 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4115 * @phba: pointer to lpfc hba data structure.
4116 *
4117 * This routine is invoked to remove all memory resources allocated
4118 * to support rpis. This routine presumes the caller has released all
4119 * rpis consumed by fabric or port logins and is prepared to have
4120 * the header pages removed.
4121 **/
4122void
4123lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4124{
4125 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4126
4127 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4128 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4129 list_del(&rpi_hdr->list);
4130 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4131 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4132 kfree(rpi_hdr->dmabuf);
4133 kfree(rpi_hdr);
4134 }
4135
4136 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4137 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4138}
4139
4140/**
4141 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4142 * @pdev: pointer to pci device data structure.
4143 *
4144 * This routine is invoked to allocate the driver hba data structure for an
4145 * HBA device. If the allocation is successful, the phba reference to the
4146 * PCI device data structure is set.
4147 *
4148 * Return codes
4149 * pointer to @phba - sucessful
4150 * NULL - error
4151 **/
4152static struct lpfc_hba *
4153lpfc_hba_alloc(struct pci_dev *pdev)
4154{
4155 struct lpfc_hba *phba;
4156
4157 /* Allocate memory for HBA structure */
4158 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4159 if (!phba) {
e34ccdfe 4160 dev_err(&pdev->dev, "failed to allocate hba struct\n");
da0436e9
JS
4161 return NULL;
4162 }
4163
4164 /* Set reference to PCI device in HBA structure */
4165 phba->pcidev = pdev;
4166
4167 /* Assign an unused board number */
4168 phba->brd_no = lpfc_get_instance();
4169 if (phba->brd_no < 0) {
4170 kfree(phba);
4171 return NULL;
4172 }
4173
4174 return phba;
4175}
4176
4177/**
4178 * lpfc_hba_free - Free driver hba data structure with a device.
4179 * @phba: pointer to lpfc hba data structure.
4180 *
4181 * This routine is invoked to free the driver hba data structure with an
4182 * HBA device.
4183 **/
4184static void
4185lpfc_hba_free(struct lpfc_hba *phba)
4186{
4187 /* Release the driver assigned board number */
4188 idr_remove(&lpfc_hba_index, phba->brd_no);
4189
4190 kfree(phba);
4191 return;
4192}
4193
4194/**
4195 * lpfc_create_shost - Create hba physical port with associated scsi host.
4196 * @phba: pointer to lpfc hba data structure.
4197 *
4198 * This routine is invoked to create HBA physical port and associate a SCSI
4199 * host with it.
4200 *
4201 * Return codes
4202 * 0 - sucessful
4203 * other values - error
4204 **/
4205static int
4206lpfc_create_shost(struct lpfc_hba *phba)
4207{
4208 struct lpfc_vport *vport;
4209 struct Scsi_Host *shost;
4210
4211 /* Initialize HBA FC structure */
4212 phba->fc_edtov = FF_DEF_EDTOV;
4213 phba->fc_ratov = FF_DEF_RATOV;
4214 phba->fc_altov = FF_DEF_ALTOV;
4215 phba->fc_arbtov = FF_DEF_ARBTOV;
4216
4217 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
4218 if (!vport)
4219 return -ENODEV;
4220
4221 shost = lpfc_shost_from_vport(vport);
4222 phba->pport = vport;
4223 lpfc_debugfs_initialize(vport);
4224 /* Put reference to SCSI host to driver's device private data */
4225 pci_set_drvdata(phba->pcidev, shost);
2e0fef85 4226
3772a991
JS
4227 return 0;
4228}
db2378e0 4229
3772a991
JS
4230/**
4231 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4232 * @phba: pointer to lpfc hba data structure.
4233 *
4234 * This routine is invoked to destroy HBA physical port and the associated
4235 * SCSI host.
4236 **/
4237static void
4238lpfc_destroy_shost(struct lpfc_hba *phba)
4239{
4240 struct lpfc_vport *vport = phba->pport;
4241
4242 /* Destroy physical port that associated with the SCSI host */
4243 destroy_port(vport);
4244
4245 return;
4246}
4247
4248/**
4249 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4250 * @phba: pointer to lpfc hba data structure.
4251 * @shost: the shost to be used to detect Block guard settings.
4252 *
4253 * This routine sets up the local Block guard protocol settings for @shost.
4254 * This routine also allocates memory for debugging bg buffers.
4255 **/
4256static void
4257lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4258{
4259 int pagecnt = 10;
4260 if (lpfc_prot_mask && lpfc_prot_guard) {
4261 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4262 "1478 Registering BlockGuard with the "
4263 "SCSI layer\n");
4264 scsi_host_set_prot(shost, lpfc_prot_mask);
4265 scsi_host_set_guard(shost, lpfc_prot_guard);
4266 }
4267 if (!_dump_buf_data) {
4268 while (pagecnt) {
4269 spin_lock_init(&_dump_buf_lock);
4270 _dump_buf_data =
4271 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4272 if (_dump_buf_data) {
4273 printk(KERN_ERR "BLKGRD allocated %d pages for "
4274 "_dump_buf_data at 0x%p\n",
4275 (1 << pagecnt), _dump_buf_data);
4276 _dump_buf_data_order = pagecnt;
4277 memset(_dump_buf_data, 0,
4278 ((1 << PAGE_SHIFT) << pagecnt));
4279 break;
4280 } else
4281 --pagecnt;
4282 }
4283 if (!_dump_buf_data_order)
4284 printk(KERN_ERR "BLKGRD ERROR unable to allocate "
4285 "memory for hexdump\n");
4286 } else
4287 printk(KERN_ERR "BLKGRD already allocated _dump_buf_data=0x%p"
4288 "\n", _dump_buf_data);
4289 if (!_dump_buf_dif) {
4290 while (pagecnt) {
4291 _dump_buf_dif =
4292 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4293 if (_dump_buf_dif) {
4294 printk(KERN_ERR "BLKGRD allocated %d pages for "
4295 "_dump_buf_dif at 0x%p\n",
4296 (1 << pagecnt), _dump_buf_dif);
4297 _dump_buf_dif_order = pagecnt;
4298 memset(_dump_buf_dif, 0,
4299 ((1 << PAGE_SHIFT) << pagecnt));
4300 break;
4301 } else
4302 --pagecnt;
4303 }
4304 if (!_dump_buf_dif_order)
4305 printk(KERN_ERR "BLKGRD ERROR unable to allocate "
4306 "memory for hexdump\n");
4307 } else
4308 printk(KERN_ERR "BLKGRD already allocated _dump_buf_dif=0x%p\n",
4309 _dump_buf_dif);
4310}
4311
4312/**
4313 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4314 * @phba: pointer to lpfc hba data structure.
4315 *
4316 * This routine is invoked to perform all the necessary post initialization
4317 * setup for the device.
4318 **/
4319static void
4320lpfc_post_init_setup(struct lpfc_hba *phba)
4321{
4322 struct Scsi_Host *shost;
4323 struct lpfc_adapter_event_header adapter_event;
4324
4325 /* Get the default values for Model Name and Description */
4326 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4327
4328 /*
4329 * hba setup may have changed the hba_queue_depth so we need to
4330 * adjust the value of can_queue.
4331 */
4332 shost = pci_get_drvdata(phba->pcidev);
4333 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4334 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4335 lpfc_setup_bg(phba, shost);
4336
4337 lpfc_host_attrib_init(shost);
4338
4339 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4340 spin_lock_irq(shost->host_lock);
4341 lpfc_poll_start_timer(phba);
4342 spin_unlock_irq(shost->host_lock);
4343 }
4344
4345 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4346 "0428 Perform SCSI scan\n");
4347 /* Send board arrival event to upper layer */
4348 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4349 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4350 fc_host_post_vendor_event(shost, fc_get_event_number(),
4351 sizeof(adapter_event),
4352 (char *) &adapter_event,
4353 LPFC_NL_VENDOR_ID);
4354 return;
4355}
4356
4357/**
4358 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4359 * @phba: pointer to lpfc hba data structure.
4360 *
4361 * This routine is invoked to set up the PCI device memory space for device
4362 * with SLI-3 interface spec.
4363 *
4364 * Return codes
4365 * 0 - sucessful
4366 * other values - error
4367 **/
4368static int
4369lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4370{
4371 struct pci_dev *pdev;
4372 unsigned long bar0map_len, bar2map_len;
4373 int i, hbq_count;
4374 void *ptr;
4375 int error = -ENODEV;
4376
4377 /* Obtain PCI device reference */
4378 if (!phba->pcidev)
4379 return error;
4380 else
4381 pdev = phba->pcidev;
4382
4383 /* Set the device DMA mask size */
4384 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
4385 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
4386 return error;
4387
4388 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4389 * required by each mapping.
4390 */
4391 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4392 bar0map_len = pci_resource_len(pdev, 0);
4393
4394 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4395 bar2map_len = pci_resource_len(pdev, 2);
4396
4397 /* Map HBA SLIM to a kernel virtual address. */
4398 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4399 if (!phba->slim_memmap_p) {
4400 dev_printk(KERN_ERR, &pdev->dev,
4401 "ioremap failed for SLIM memory.\n");
4402 goto out;
4403 }
4404
4405 /* Map HBA Control Registers to a kernel virtual address. */
4406 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4407 if (!phba->ctrl_regs_memmap_p) {
4408 dev_printk(KERN_ERR, &pdev->dev,
4409 "ioremap failed for HBA control registers.\n");
4410 goto out_iounmap_slim;
4411 }
4412
4413 /* Allocate memory for SLI-2 structures */
4414 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4415 SLI2_SLIM_SIZE,
4416 &phba->slim2p.phys,
4417 GFP_KERNEL);
4418 if (!phba->slim2p.virt)
4419 goto out_iounmap;
4420
4421 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4422 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4423 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4424 phba->IOCBs = (phba->slim2p.virt +
4425 offsetof(struct lpfc_sli2_slim, IOCBs));
4426
4427 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4428 lpfc_sli_hbq_size(),
4429 &phba->hbqslimp.phys,
4430 GFP_KERNEL);
4431 if (!phba->hbqslimp.virt)
4432 goto out_free_slim;
4433
4434 hbq_count = lpfc_sli_hbq_count();
4435 ptr = phba->hbqslimp.virt;
4436 for (i = 0; i < hbq_count; ++i) {
4437 phba->hbqs[i].hbq_virt = ptr;
4438 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4439 ptr += (lpfc_hbq_defs[i]->entry_count *
4440 sizeof(struct lpfc_hbq_entry));
4441 }
4442 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4443 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4444
4445 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4446
4447 INIT_LIST_HEAD(&phba->rb_pend_list);
4448
4449 phba->MBslimaddr = phba->slim_memmap_p;
4450 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4451 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4452 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4453 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
4454
4455 return 0;
4456
4457out_free_slim:
4458 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4459 phba->slim2p.virt, phba->slim2p.phys);
4460out_iounmap:
4461 iounmap(phba->ctrl_regs_memmap_p);
4462out_iounmap_slim:
4463 iounmap(phba->slim_memmap_p);
4464out:
4465 return error;
4466}
4467
4468/**
4469 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4470 * @phba: pointer to lpfc hba data structure.
4471 *
4472 * This routine is invoked to unset the PCI device memory space for device
4473 * with SLI-3 interface spec.
4474 **/
4475static void
4476lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4477{
4478 struct pci_dev *pdev;
4479
4480 /* Obtain PCI device reference */
4481 if (!phba->pcidev)
4482 return;
4483 else
4484 pdev = phba->pcidev;
4485
4486 /* Free coherent DMA memory allocated */
4487 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4488 phba->hbqslimp.virt, phba->hbqslimp.phys);
4489 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4490 phba->slim2p.virt, phba->slim2p.phys);
4491
4492 /* I/O memory unmap */
4493 iounmap(phba->ctrl_regs_memmap_p);
4494 iounmap(phba->slim_memmap_p);
4495
4496 return;
4497}
4498
4499/**
da0436e9 4500 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
3772a991
JS
4501 * @phba: pointer to lpfc hba data structure.
4502 *
da0436e9
JS
4503 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4504 * done and check status.
3772a991 4505 *
da0436e9 4506 * Return 0 if successful, otherwise -ENODEV.
3772a991 4507 **/
da0436e9
JS
4508int
4509lpfc_sli4_post_status_check(struct lpfc_hba *phba)
3772a991 4510{
da0436e9
JS
4511 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg, scratchpad;
4512 uint32_t onlnreg0, onlnreg1;
4513 int i, port_error = -ENODEV;
3772a991 4514
da0436e9
JS
4515 if (!phba->sli4_hba.STAregaddr)
4516 return -ENODEV;
3772a991 4517
da0436e9
JS
4518 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4519 for (i = 0; i < 3000; i++) {
4520 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4521 /* Encounter fatal POST error, break out */
4522 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4523 port_error = -ENODEV;
4524 break;
4525 }
4526 if (LPFC_POST_STAGE_ARMFW_READY ==
4527 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4528 port_error = 0;
4529 break;
4530 }
4531 msleep(10);
3772a991
JS
4532 }
4533
da0436e9
JS
4534 if (port_error)
4535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4536 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4537 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4538 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4539 bf_get(lpfc_hst_state_perr, &sta_reg),
4540 bf_get(lpfc_hst_state_sfi, &sta_reg),
4541 bf_get(lpfc_hst_state_nip, &sta_reg),
4542 bf_get(lpfc_hst_state_ipc, &sta_reg),
4543 bf_get(lpfc_hst_state_xrom, &sta_reg),
4544 bf_get(lpfc_hst_state_dl, &sta_reg),
4545 bf_get(lpfc_hst_state_port_status, &sta_reg));
4546
4547 /* Log device information */
4548 scratchpad.word0 = readl(phba->sli4_hba.SCRATCHPADregaddr);
4549 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4550 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4551 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4552 bf_get(lpfc_scratchpad_chiptype, &scratchpad),
4553 bf_get(lpfc_scratchpad_slirev, &scratchpad),
4554 bf_get(lpfc_scratchpad_featurelevel1, &scratchpad),
4555 bf_get(lpfc_scratchpad_featurelevel2, &scratchpad));
4556
8fa38513
JS
4557 /* With uncoverable error, log the error message and return error */
4558 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
4559 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
4560 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
4561 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4562 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4563 if (uerrlo_reg.word0 || uerrhi_reg.word0) {
4564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4565 "1422 HBA Unrecoverable error: "
4566 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4567 "online0_reg=0x%x, online1_reg=0x%x\n",
4568 uerrlo_reg.word0, uerrhi_reg.word0,
4569 onlnreg0, onlnreg1);
4570 }
4571 return -ENODEV;
4572 }
4573
da0436e9
JS
4574 return port_error;
4575}
3772a991 4576
da0436e9
JS
4577/**
4578 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4579 * @phba: pointer to lpfc hba data structure.
4580 *
4581 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4582 * memory map.
4583 **/
4584static void
4585lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4586{
4587 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4588 LPFC_UERR_STATUS_LO;
4589 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4590 LPFC_UERR_STATUS_HI;
4591 phba->sli4_hba.ONLINE0regaddr = phba->sli4_hba.conf_regs_memmap_p +
4592 LPFC_ONLINE0;
4593 phba->sli4_hba.ONLINE1regaddr = phba->sli4_hba.conf_regs_memmap_p +
4594 LPFC_ONLINE1;
4595 phba->sli4_hba.SCRATCHPADregaddr = phba->sli4_hba.conf_regs_memmap_p +
4596 LPFC_SCRATCHPAD;
4597}
3772a991 4598
da0436e9
JS
4599/**
4600 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4601 * @phba: pointer to lpfc hba data structure.
4602 *
4603 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4604 * memory map.
4605 **/
4606static void
4607lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4608{
3772a991 4609
da0436e9
JS
4610 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4611 LPFC_HST_STATE;
4612 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4613 LPFC_HST_ISR0;
4614 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4615 LPFC_HST_IMR0;
4616 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4617 LPFC_HST_ISCR0;
4618 return;
3772a991
JS
4619}
4620
4621/**
da0436e9 4622 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
3772a991 4623 * @phba: pointer to lpfc hba data structure.
da0436e9 4624 * @vf: virtual function number
3772a991 4625 *
da0436e9
JS
4626 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4627 * based on the given viftual function number, @vf.
4628 *
4629 * Return 0 if successful, otherwise -ENODEV.
3772a991 4630 **/
da0436e9
JS
4631static int
4632lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
3772a991 4633{
da0436e9
JS
4634 if (vf > LPFC_VIR_FUNC_MAX)
4635 return -ENODEV;
3772a991 4636
da0436e9
JS
4637 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4638 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4639 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4640 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4641 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4642 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
4643 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4644 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
4645 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4646 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
4647 return 0;
3772a991
JS
4648}
4649
4650/**
da0436e9 4651 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
3772a991
JS
4652 * @phba: pointer to lpfc hba data structure.
4653 *
da0436e9
JS
4654 * This routine is invoked to create the bootstrap mailbox
4655 * region consistent with the SLI-4 interface spec. This
4656 * routine allocates all memory necessary to communicate
4657 * mailbox commands to the port and sets up all alignment
4658 * needs. No locks are expected to be held when calling
4659 * this routine.
3772a991
JS
4660 *
4661 * Return codes
4662 * 0 - sucessful
da0436e9
JS
4663 * ENOMEM - could not allocated memory.
4664 **/
3772a991 4665static int
da0436e9 4666lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
3772a991 4667{
da0436e9
JS
4668 uint32_t bmbx_size;
4669 struct lpfc_dmabuf *dmabuf;
4670 struct dma_address *dma_address;
4671 uint32_t pa_addr;
4672 uint64_t phys_addr;
4673
4674 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4675 if (!dmabuf)
4676 return -ENOMEM;
3772a991 4677
da0436e9
JS
4678 /*
4679 * The bootstrap mailbox region is comprised of 2 parts
4680 * plus an alignment restriction of 16 bytes.
4681 */
4682 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
4683 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4684 bmbx_size,
4685 &dmabuf->phys,
4686 GFP_KERNEL);
4687 if (!dmabuf->virt) {
4688 kfree(dmabuf);
4689 return -ENOMEM;
3772a991 4690 }
da0436e9 4691 memset(dmabuf->virt, 0, bmbx_size);
3772a991 4692
da0436e9
JS
4693 /*
4694 * Initialize the bootstrap mailbox pointers now so that the register
4695 * operations are simple later. The mailbox dma address is required
4696 * to be 16-byte aligned. Also align the virtual memory as each
4697 * maibox is copied into the bmbx mailbox region before issuing the
4698 * command to the port.
4699 */
4700 phba->sli4_hba.bmbx.dmabuf = dmabuf;
4701 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
4702
4703 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
4704 LPFC_ALIGN_16_BYTE);
4705 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
4706 LPFC_ALIGN_16_BYTE);
4707
4708 /*
4709 * Set the high and low physical addresses now. The SLI4 alignment
4710 * requirement is 16 bytes and the mailbox is posted to the port
4711 * as two 30-bit addresses. The other data is a bit marking whether
4712 * the 30-bit address is the high or low address.
4713 * Upcast bmbx aphys to 64bits so shift instruction compiles
4714 * clean on 32 bit machines.
4715 */
4716 dma_address = &phba->sli4_hba.bmbx.dma_address;
4717 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
4718 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
4719 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
4720 LPFC_BMBX_BIT1_ADDR_HI);
4721
4722 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
4723 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
4724 LPFC_BMBX_BIT1_ADDR_LO);
4725 return 0;
3772a991
JS
4726}
4727
4728/**
da0436e9 4729 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
3772a991
JS
4730 * @phba: pointer to lpfc hba data structure.
4731 *
da0436e9
JS
4732 * This routine is invoked to teardown the bootstrap mailbox
4733 * region and release all host resources. This routine requires
4734 * the caller to ensure all mailbox commands recovered, no
4735 * additional mailbox comands are sent, and interrupts are disabled
4736 * before calling this routine.
4737 *
4738 **/
3772a991 4739static void
da0436e9 4740lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
3772a991 4741{
da0436e9
JS
4742 dma_free_coherent(&phba->pcidev->dev,
4743 phba->sli4_hba.bmbx.bmbx_size,
4744 phba->sli4_hba.bmbx.dmabuf->virt,
4745 phba->sli4_hba.bmbx.dmabuf->phys);
4746
4747 kfree(phba->sli4_hba.bmbx.dmabuf);
4748 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
3772a991
JS
4749}
4750
4751/**
da0436e9 4752 * lpfc_sli4_read_config - Get the config parameters.
3772a991
JS
4753 * @phba: pointer to lpfc hba data structure.
4754 *
da0436e9
JS
4755 * This routine is invoked to read the configuration parameters from the HBA.
4756 * The configuration parameters are used to set the base and maximum values
4757 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
4758 * allocation for the port.
3772a991
JS
4759 *
4760 * Return codes
da0436e9
JS
4761 * 0 - sucessful
4762 * ENOMEM - No availble memory
4763 * EIO - The mailbox failed to complete successfully.
3772a991 4764 **/
da0436e9
JS
4765static int
4766lpfc_sli4_read_config(struct lpfc_hba *phba)
3772a991 4767{
da0436e9
JS
4768 LPFC_MBOXQ_t *pmb;
4769 struct lpfc_mbx_read_config *rd_config;
4770 uint32_t rc = 0;
3772a991 4771
da0436e9
JS
4772 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4773 if (!pmb) {
4774 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4775 "2011 Unable to allocate memory for issuing "
4776 "SLI_CONFIG_SPECIAL mailbox command\n");
4777 return -ENOMEM;
3772a991
JS
4778 }
4779
da0436e9 4780 lpfc_read_config(phba, pmb);
3772a991 4781
da0436e9
JS
4782 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4783 if (rc != MBX_SUCCESS) {
4784 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4785 "2012 Mailbox failed , mbxCmd x%x "
4786 "READ_CONFIG, mbxStatus x%x\n",
4787 bf_get(lpfc_mqe_command, &pmb->u.mqe),
4788 bf_get(lpfc_mqe_status, &pmb->u.mqe));
4789 rc = -EIO;
4790 } else {
4791 rd_config = &pmb->u.mqe.un.rd_config;
4792 phba->sli4_hba.max_cfg_param.max_xri =
4793 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
4794 phba->sli4_hba.max_cfg_param.xri_base =
4795 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
4796 phba->sli4_hba.max_cfg_param.max_vpi =
4797 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
4798 phba->sli4_hba.max_cfg_param.vpi_base =
4799 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
4800 phba->sli4_hba.max_cfg_param.max_rpi =
4801 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
4802 phba->sli4_hba.max_cfg_param.rpi_base =
4803 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
4804 phba->sli4_hba.max_cfg_param.max_vfi =
4805 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
4806 phba->sli4_hba.max_cfg_param.vfi_base =
4807 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
4808 phba->sli4_hba.max_cfg_param.max_fcfi =
4809 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
4810 phba->sli4_hba.max_cfg_param.fcfi_base =
4811 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
4812 phba->sli4_hba.max_cfg_param.max_eq =
4813 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
4814 phba->sli4_hba.max_cfg_param.max_rq =
4815 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
4816 phba->sli4_hba.max_cfg_param.max_wq =
4817 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
4818 phba->sli4_hba.max_cfg_param.max_cq =
4819 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
4820 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
4821 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
4822 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
4823 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
4824 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4825 phba->max_vpi = phba->sli4_hba.max_cfg_param.max_vpi;
4826 phba->max_vports = phba->max_vpi;
4827 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4828 "2003 cfg params XRI(B:%d M:%d), "
4829 "VPI(B:%d M:%d) "
4830 "VFI(B:%d M:%d) "
4831 "RPI(B:%d M:%d) "
4832 "FCFI(B:%d M:%d)\n",
4833 phba->sli4_hba.max_cfg_param.xri_base,
4834 phba->sli4_hba.max_cfg_param.max_xri,
4835 phba->sli4_hba.max_cfg_param.vpi_base,
4836 phba->sli4_hba.max_cfg_param.max_vpi,
4837 phba->sli4_hba.max_cfg_param.vfi_base,
4838 phba->sli4_hba.max_cfg_param.max_vfi,
4839 phba->sli4_hba.max_cfg_param.rpi_base,
4840 phba->sli4_hba.max_cfg_param.max_rpi,
4841 phba->sli4_hba.max_cfg_param.fcfi_base,
4842 phba->sli4_hba.max_cfg_param.max_fcfi);
3772a991 4843 }
da0436e9
JS
4844 mempool_free(pmb, phba->mbox_mem_pool);
4845
4846 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
4847 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
4848 phba->cfg_hba_queue_depth =
4849 phba->sli4_hba.max_cfg_param.max_xri;
4850 return rc;
3772a991
JS
4851}
4852
4853/**
da0436e9 4854 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
3772a991
JS
4855 * @phba: pointer to lpfc hba data structure.
4856 *
da0436e9
JS
4857 * This routine is invoked to setup the host-side endian order to the
4858 * HBA consistent with the SLI-4 interface spec.
4859 *
4860 * Return codes
4861 * 0 - sucessful
4862 * ENOMEM - No availble memory
4863 * EIO - The mailbox failed to complete successfully.
3772a991 4864 **/
da0436e9
JS
4865static int
4866lpfc_setup_endian_order(struct lpfc_hba *phba)
3772a991 4867{
da0436e9
JS
4868 LPFC_MBOXQ_t *mboxq;
4869 uint32_t rc = 0;
4870 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
4871 HOST_ENDIAN_HIGH_WORD1};
3772a991 4872
da0436e9
JS
4873 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4874 if (!mboxq) {
4875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4876 "0492 Unable to allocate memory for issuing "
4877 "SLI_CONFIG_SPECIAL mailbox command\n");
4878 return -ENOMEM;
4879 }
3772a991 4880
da0436e9
JS
4881 /*
4882 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
4883 * words to contain special data values and no other data.
4884 */
4885 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
4886 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
4887 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4888 if (rc != MBX_SUCCESS) {
4889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4890 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
4891 "status x%x\n",
4892 rc);
4893 rc = -EIO;
4894 }
4895
4896 mempool_free(mboxq, phba->mbox_mem_pool);
4897 return rc;
3772a991
JS
4898}
4899
4900/**
da0436e9 4901 * lpfc_sli4_queue_create - Create all the SLI4 queues
3772a991
JS
4902 * @phba: pointer to lpfc hba data structure.
4903 *
da0436e9
JS
4904 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
4905 * operation. For each SLI4 queue type, the parameters such as queue entry
4906 * count (queue depth) shall be taken from the module parameter. For now,
4907 * we just use some constant number as place holder.
4908 *
4909 * Return codes
4910 * 0 - sucessful
4911 * ENOMEM - No availble memory
4912 * EIO - The mailbox failed to complete successfully.
3772a991 4913 **/
da0436e9
JS
4914static int
4915lpfc_sli4_queue_create(struct lpfc_hba *phba)
3772a991 4916{
da0436e9
JS
4917 struct lpfc_queue *qdesc;
4918 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
4919 int cfg_fcp_wq_count;
4920 int cfg_fcp_eq_count;
3772a991 4921
da0436e9
JS
4922 /*
4923 * Sanity check for confiugred queue parameters against the run-time
4924 * device parameters
4925 */
3772a991 4926
da0436e9
JS
4927 /* Sanity check on FCP fast-path WQ parameters */
4928 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
4929 if (cfg_fcp_wq_count >
4930 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
4931 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
4932 LPFC_SP_WQN_DEF;
4933 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
4934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4935 "2581 Not enough WQs (%d) from "
4936 "the pci function for supporting "
4937 "FCP WQs (%d)\n",
4938 phba->sli4_hba.max_cfg_param.max_wq,
4939 phba->cfg_fcp_wq_count);
4940 goto out_error;
4941 }
4942 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4943 "2582 Not enough WQs (%d) from the pci "
4944 "function for supporting the requested "
4945 "FCP WQs (%d), the actual FCP WQs can "
4946 "be supported: %d\n",
4947 phba->sli4_hba.max_cfg_param.max_wq,
4948 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
4949 }
4950 /* The actual number of FCP work queues adopted */
4951 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
4952
4953 /* Sanity check on FCP fast-path EQ parameters */
4954 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
4955 if (cfg_fcp_eq_count >
4956 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
4957 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
4958 LPFC_SP_EQN_DEF;
4959 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
4960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4961 "2574 Not enough EQs (%d) from the "
4962 "pci function for supporting FCP "
4963 "EQs (%d)\n",
4964 phba->sli4_hba.max_cfg_param.max_eq,
4965 phba->cfg_fcp_eq_count);
4966 goto out_error;
4967 }
4968 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4969 "2575 Not enough EQs (%d) from the pci "
4970 "function for supporting the requested "
4971 "FCP EQs (%d), the actual FCP EQs can "
4972 "be supported: %d\n",
4973 phba->sli4_hba.max_cfg_param.max_eq,
4974 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
4975 }
4976 /* It does not make sense to have more EQs than WQs */
4977 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
4978 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4979 "2593 The number of FCP EQs (%d) is more "
4980 "than the number of FCP WQs (%d), take "
4981 "the number of FCP EQs same as than of "
4982 "WQs (%d)\n", cfg_fcp_eq_count,
4983 phba->cfg_fcp_wq_count,
4984 phba->cfg_fcp_wq_count);
4985 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
4986 }
4987 /* The actual number of FCP event queues adopted */
4988 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
4989 /* The overall number of event queues used */
4990 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
3772a991 4991
da0436e9
JS
4992 /*
4993 * Create Event Queues (EQs)
4994 */
3772a991 4995
da0436e9
JS
4996 /* Get EQ depth from module parameter, fake the default for now */
4997 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
4998 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
3772a991 4999
da0436e9
JS
5000 /* Create slow path event queue */
5001 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5002 phba->sli4_hba.eq_ecount);
5003 if (!qdesc) {
5004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5005 "0496 Failed allocate slow-path EQ\n");
5006 goto out_error;
5007 }
5008 phba->sli4_hba.sp_eq = qdesc;
5009
5010 /* Create fast-path FCP Event Queue(s) */
5011 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5012 phba->cfg_fcp_eq_count), GFP_KERNEL);
5013 if (!phba->sli4_hba.fp_eq) {
5014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5015 "2576 Failed allocate memory for fast-path "
5016 "EQ record array\n");
5017 goto out_free_sp_eq;
5018 }
5019 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5020 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5021 phba->sli4_hba.eq_ecount);
5022 if (!qdesc) {
5023 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5024 "0497 Failed allocate fast-path EQ\n");
5025 goto out_free_fp_eq;
5026 }
5027 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5028 }
5029
5030 /*
5031 * Create Complete Queues (CQs)
5032 */
5033
5034 /* Get CQ depth from module parameter, fake the default for now */
5035 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5036 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5037
5038 /* Create slow-path Mailbox Command Complete Queue */
5039 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5040 phba->sli4_hba.cq_ecount);
5041 if (!qdesc) {
5042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5043 "0500 Failed allocate slow-path mailbox CQ\n");
5044 goto out_free_fp_eq;
5045 }
5046 phba->sli4_hba.mbx_cq = qdesc;
5047
5048 /* Create slow-path ELS Complete Queue */
5049 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5050 phba->sli4_hba.cq_ecount);
5051 if (!qdesc) {
5052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5053 "0501 Failed allocate slow-path ELS CQ\n");
5054 goto out_free_mbx_cq;
5055 }
5056 phba->sli4_hba.els_cq = qdesc;
5057
5058 /* Create slow-path Unsolicited Receive Complete Queue */
5059 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5060 phba->sli4_hba.cq_ecount);
5061 if (!qdesc) {
5062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5063 "0502 Failed allocate slow-path USOL RX CQ\n");
5064 goto out_free_els_cq;
5065 }
5066 phba->sli4_hba.rxq_cq = qdesc;
5067
5068 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5069 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5070 phba->cfg_fcp_eq_count), GFP_KERNEL);
5071 if (!phba->sli4_hba.fcp_cq) {
5072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5073 "2577 Failed allocate memory for fast-path "
5074 "CQ record array\n");
5075 goto out_free_rxq_cq;
5076 }
5077 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5078 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5079 phba->sli4_hba.cq_ecount);
5080 if (!qdesc) {
5081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5082 "0499 Failed allocate fast-path FCP "
5083 "CQ (%d)\n", fcp_cqidx);
5084 goto out_free_fcp_cq;
5085 }
5086 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5087 }
5088
5089 /* Create Mailbox Command Queue */
5090 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5091 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5092
5093 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5094 phba->sli4_hba.mq_ecount);
5095 if (!qdesc) {
5096 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5097 "0505 Failed allocate slow-path MQ\n");
5098 goto out_free_fcp_cq;
5099 }
5100 phba->sli4_hba.mbx_wq = qdesc;
5101
5102 /*
5103 * Create all the Work Queues (WQs)
5104 */
5105 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5106 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5107
5108 /* Create slow-path ELS Work Queue */
5109 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5110 phba->sli4_hba.wq_ecount);
5111 if (!qdesc) {
5112 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5113 "0504 Failed allocate slow-path ELS WQ\n");
5114 goto out_free_mbx_wq;
5115 }
5116 phba->sli4_hba.els_wq = qdesc;
5117
5118 /* Create fast-path FCP Work Queue(s) */
5119 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5120 phba->cfg_fcp_wq_count), GFP_KERNEL);
5121 if (!phba->sli4_hba.fcp_wq) {
5122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5123 "2578 Failed allocate memory for fast-path "
5124 "WQ record array\n");
5125 goto out_free_els_wq;
5126 }
5127 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5128 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5129 phba->sli4_hba.wq_ecount);
5130 if (!qdesc) {
5131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5132 "0503 Failed allocate fast-path FCP "
5133 "WQ (%d)\n", fcp_wqidx);
5134 goto out_free_fcp_wq;
5135 }
5136 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5137 }
5138
5139 /*
5140 * Create Receive Queue (RQ)
5141 */
5142 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5143 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5144
5145 /* Create Receive Queue for header */
5146 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5147 phba->sli4_hba.rq_ecount);
5148 if (!qdesc) {
5149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5150 "0506 Failed allocate receive HRQ\n");
5151 goto out_free_fcp_wq;
5152 }
5153 phba->sli4_hba.hdr_rq = qdesc;
5154
5155 /* Create Receive Queue for data */
5156 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5157 phba->sli4_hba.rq_ecount);
5158 if (!qdesc) {
5159 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5160 "0507 Failed allocate receive DRQ\n");
5161 goto out_free_hdr_rq;
5162 }
5163 phba->sli4_hba.dat_rq = qdesc;
5164
5165 return 0;
5166
5167out_free_hdr_rq:
5168 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5169 phba->sli4_hba.hdr_rq = NULL;
5170out_free_fcp_wq:
5171 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5172 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5173 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5174 }
5175 kfree(phba->sli4_hba.fcp_wq);
5176out_free_els_wq:
5177 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5178 phba->sli4_hba.els_wq = NULL;
5179out_free_mbx_wq:
5180 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5181 phba->sli4_hba.mbx_wq = NULL;
5182out_free_fcp_cq:
5183 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5184 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5185 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5186 }
5187 kfree(phba->sli4_hba.fcp_cq);
5188out_free_rxq_cq:
5189 lpfc_sli4_queue_free(phba->sli4_hba.rxq_cq);
5190 phba->sli4_hba.rxq_cq = NULL;
5191out_free_els_cq:
5192 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5193 phba->sli4_hba.els_cq = NULL;
5194out_free_mbx_cq:
5195 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5196 phba->sli4_hba.mbx_cq = NULL;
5197out_free_fp_eq:
5198 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5199 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5200 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5201 }
5202 kfree(phba->sli4_hba.fp_eq);
5203out_free_sp_eq:
5204 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5205 phba->sli4_hba.sp_eq = NULL;
5206out_error:
5207 return -ENOMEM;
5208}
5209
5210/**
5211 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5212 * @phba: pointer to lpfc hba data structure.
5213 *
5214 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5215 * operation.
5216 *
5217 * Return codes
5218 * 0 - sucessful
5219 * ENOMEM - No availble memory
5220 * EIO - The mailbox failed to complete successfully.
5221 **/
5222static void
5223lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5224{
5225 int fcp_qidx;
5226
5227 /* Release mailbox command work queue */
5228 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5229 phba->sli4_hba.mbx_wq = NULL;
5230
5231 /* Release ELS work queue */
5232 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5233 phba->sli4_hba.els_wq = NULL;
5234
5235 /* Release FCP work queue */
5236 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5237 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5238 kfree(phba->sli4_hba.fcp_wq);
5239 phba->sli4_hba.fcp_wq = NULL;
5240
5241 /* Release unsolicited receive queue */
5242 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5243 phba->sli4_hba.hdr_rq = NULL;
5244 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5245 phba->sli4_hba.dat_rq = NULL;
5246
5247 /* Release unsolicited receive complete queue */
5248 lpfc_sli4_queue_free(phba->sli4_hba.rxq_cq);
5249 phba->sli4_hba.rxq_cq = NULL;
5250
5251 /* Release ELS complete queue */
5252 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5253 phba->sli4_hba.els_cq = NULL;
5254
5255 /* Release mailbox command complete queue */
5256 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5257 phba->sli4_hba.mbx_cq = NULL;
5258
5259 /* Release FCP response complete queue */
5260 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5261 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5262 kfree(phba->sli4_hba.fcp_cq);
5263 phba->sli4_hba.fcp_cq = NULL;
5264
5265 /* Release fast-path event queue */
5266 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5267 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5268 kfree(phba->sli4_hba.fp_eq);
5269 phba->sli4_hba.fp_eq = NULL;
5270
5271 /* Release slow-path event queue */
5272 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5273 phba->sli4_hba.sp_eq = NULL;
5274
5275 return;
5276}
5277
5278/**
5279 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5280 * @phba: pointer to lpfc hba data structure.
5281 *
5282 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5283 * operation.
5284 *
5285 * Return codes
5286 * 0 - sucessful
5287 * ENOMEM - No availble memory
5288 * EIO - The mailbox failed to complete successfully.
5289 **/
5290int
5291lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5292{
5293 int rc = -ENOMEM;
5294 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5295 int fcp_cq_index = 0;
5296
5297 /*
5298 * Set up Event Queues (EQs)
5299 */
5300
5301 /* Set up slow-path event queue */
5302 if (!phba->sli4_hba.sp_eq) {
5303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5304 "0520 Slow-path EQ not allocated\n");
5305 goto out_error;
5306 }
5307 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5308 LPFC_SP_DEF_IMAX);
5309 if (rc) {
5310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5311 "0521 Failed setup of slow-path EQ: "
5312 "rc = 0x%x\n", rc);
5313 goto out_error;
5314 }
5315 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5316 "2583 Slow-path EQ setup: queue-id=%d\n",
5317 phba->sli4_hba.sp_eq->queue_id);
5318
5319 /* Set up fast-path event queue */
5320 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5321 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5323 "0522 Fast-path EQ (%d) not "
5324 "allocated\n", fcp_eqidx);
5325 goto out_destroy_fp_eq;
5326 }
5327 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5328 phba->cfg_fcp_imax);
5329 if (rc) {
5330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5331 "0523 Failed setup of fast-path EQ "
5332 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5333 goto out_destroy_fp_eq;
5334 }
5335 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5336 "2584 Fast-path EQ setup: "
5337 "queue[%d]-id=%d\n", fcp_eqidx,
5338 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5339 }
5340
5341 /*
5342 * Set up Complete Queues (CQs)
5343 */
5344
5345 /* Set up slow-path MBOX Complete Queue as the first CQ */
5346 if (!phba->sli4_hba.mbx_cq) {
5347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5348 "0528 Mailbox CQ not allocated\n");
5349 goto out_destroy_fp_eq;
5350 }
5351 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5352 LPFC_MCQ, LPFC_MBOX);
5353 if (rc) {
5354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5355 "0529 Failed setup of slow-path mailbox CQ: "
5356 "rc = 0x%x\n", rc);
5357 goto out_destroy_fp_eq;
5358 }
5359 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5360 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5361 phba->sli4_hba.mbx_cq->queue_id,
5362 phba->sli4_hba.sp_eq->queue_id);
5363
5364 /* Set up slow-path ELS Complete Queue */
5365 if (!phba->sli4_hba.els_cq) {
5366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5367 "0530 ELS CQ not allocated\n");
5368 goto out_destroy_mbx_cq;
5369 }
5370 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5371 LPFC_WCQ, LPFC_ELS);
5372 if (rc) {
5373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5374 "0531 Failed setup of slow-path ELS CQ: "
5375 "rc = 0x%x\n", rc);
5376 goto out_destroy_mbx_cq;
5377 }
5378 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5379 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5380 phba->sli4_hba.els_cq->queue_id,
5381 phba->sli4_hba.sp_eq->queue_id);
5382
5383 /* Set up slow-path Unsolicited Receive Complete Queue */
5384 if (!phba->sli4_hba.rxq_cq) {
5385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5386 "0532 USOL RX CQ not allocated\n");
5387 goto out_destroy_els_cq;
5388 }
5389 rc = lpfc_cq_create(phba, phba->sli4_hba.rxq_cq, phba->sli4_hba.sp_eq,
5390 LPFC_RCQ, LPFC_USOL);
5391 if (rc) {
5392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5393 "0533 Failed setup of slow-path USOL RX CQ: "
5394 "rc = 0x%x\n", rc);
5395 goto out_destroy_els_cq;
5396 }
5397 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5398 "2587 USL CQ setup: cq-id=%d, parent eq-id=%d\n",
5399 phba->sli4_hba.rxq_cq->queue_id,
5400 phba->sli4_hba.sp_eq->queue_id);
5401
5402 /* Set up fast-path FCP Response Complete Queue */
5403 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5404 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5406 "0526 Fast-path FCP CQ (%d) not "
5407 "allocated\n", fcp_cqidx);
5408 goto out_destroy_fcp_cq;
5409 }
5410 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5411 phba->sli4_hba.fp_eq[fcp_cqidx],
5412 LPFC_WCQ, LPFC_FCP);
5413 if (rc) {
5414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5415 "0527 Failed setup of fast-path FCP "
5416 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5417 goto out_destroy_fcp_cq;
5418 }
5419 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5420 "2588 FCP CQ setup: cq[%d]-id=%d, "
5421 "parent eq[%d]-id=%d\n",
5422 fcp_cqidx,
5423 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5424 fcp_cqidx,
5425 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5426 }
5427
5428 /*
5429 * Set up all the Work Queues (WQs)
5430 */
5431
5432 /* Set up Mailbox Command Queue */
5433 if (!phba->sli4_hba.mbx_wq) {
5434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5435 "0538 Slow-path MQ not allocated\n");
5436 goto out_destroy_fcp_cq;
5437 }
5438 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5439 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5440 if (rc) {
5441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5442 "0539 Failed setup of slow-path MQ: "
5443 "rc = 0x%x\n", rc);
5444 goto out_destroy_fcp_cq;
5445 }
5446 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5447 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5448 phba->sli4_hba.mbx_wq->queue_id,
5449 phba->sli4_hba.mbx_cq->queue_id);
5450
5451 /* Set up slow-path ELS Work Queue */
5452 if (!phba->sli4_hba.els_wq) {
5453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5454 "0536 Slow-path ELS WQ not allocated\n");
5455 goto out_destroy_mbx_wq;
5456 }
5457 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5458 phba->sli4_hba.els_cq, LPFC_ELS);
5459 if (rc) {
5460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5461 "0537 Failed setup of slow-path ELS WQ: "
5462 "rc = 0x%x\n", rc);
5463 goto out_destroy_mbx_wq;
5464 }
5465 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5466 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5467 phba->sli4_hba.els_wq->queue_id,
5468 phba->sli4_hba.els_cq->queue_id);
5469
5470 /* Set up fast-path FCP Work Queue */
5471 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5472 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5474 "0534 Fast-path FCP WQ (%d) not "
5475 "allocated\n", fcp_wqidx);
5476 goto out_destroy_fcp_wq;
5477 }
5478 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5479 phba->sli4_hba.fcp_cq[fcp_cq_index],
5480 LPFC_FCP);
5481 if (rc) {
5482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5483 "0535 Failed setup of fast-path FCP "
5484 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5485 goto out_destroy_fcp_wq;
5486 }
5487 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5488 "2591 FCP WQ setup: wq[%d]-id=%d, "
5489 "parent cq[%d]-id=%d\n",
5490 fcp_wqidx,
5491 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5492 fcp_cq_index,
5493 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5494 /* Round robin FCP Work Queue's Completion Queue assignment */
5495 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5496 }
5497
5498 /*
5499 * Create Receive Queue (RQ)
5500 */
5501 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5503 "0540 Receive Queue not allocated\n");
5504 goto out_destroy_fcp_wq;
5505 }
5506 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
5507 phba->sli4_hba.rxq_cq, LPFC_USOL);
5508 if (rc) {
5509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5510 "0541 Failed setup of Receive Queue: "
5511 "rc = 0x%x\n", rc);
5512 goto out_destroy_fcp_wq;
5513 }
5514 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5515 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5516 "parent cq-id=%d\n",
5517 phba->sli4_hba.hdr_rq->queue_id,
5518 phba->sli4_hba.dat_rq->queue_id,
5519 phba->sli4_hba.rxq_cq->queue_id);
5520 return 0;
5521
5522out_destroy_fcp_wq:
5523 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5524 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5525 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5526out_destroy_mbx_wq:
5527 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5528out_destroy_fcp_cq:
5529 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5530 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
5531 lpfc_cq_destroy(phba, phba->sli4_hba.rxq_cq);
5532out_destroy_els_cq:
5533 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5534out_destroy_mbx_cq:
5535 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5536out_destroy_fp_eq:
5537 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5538 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5539 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5540out_error:
5541 return rc;
5542}
5543
5544/**
5545 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5546 * @phba: pointer to lpfc hba data structure.
5547 *
5548 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5549 * operation.
5550 *
5551 * Return codes
5552 * 0 - sucessful
5553 * ENOMEM - No availble memory
5554 * EIO - The mailbox failed to complete successfully.
5555 **/
5556void
5557lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5558{
5559 int fcp_qidx;
5560
5561 /* Unset mailbox command work queue */
5562 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5563 /* Unset ELS work queue */
5564 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5565 /* Unset unsolicited receive queue */
5566 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5567 /* Unset FCP work queue */
5568 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5569 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5570 /* Unset mailbox command complete queue */
5571 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5572 /* Unset ELS complete queue */
5573 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5574 /* Unset unsolicited receive complete queue */
5575 lpfc_cq_destroy(phba, phba->sli4_hba.rxq_cq);
5576 /* Unset FCP response complete queue */
5577 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5578 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5579 /* Unset fast-path event queue */
5580 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5581 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5582 /* Unset slow-path event queue */
5583 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5584}
5585
5586/**
5587 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5588 * @phba: pointer to lpfc hba data structure.
5589 *
5590 * This routine is invoked to allocate and set up a pool of completion queue
5591 * events. The body of the completion queue event is a completion queue entry
5592 * CQE. For now, this pool is used for the interrupt service routine to queue
5593 * the following HBA completion queue events for the worker thread to process:
5594 * - Mailbox asynchronous events
5595 * - Receive queue completion unsolicited events
5596 * Later, this can be used for all the slow-path events.
5597 *
5598 * Return codes
5599 * 0 - sucessful
5600 * -ENOMEM - No availble memory
5601 **/
5602static int
5603lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5604{
5605 struct lpfc_cq_event *cq_event;
5606 int i;
5607
5608 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5609 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5610 if (!cq_event)
5611 goto out_pool_create_fail;
5612 list_add_tail(&cq_event->list,
5613 &phba->sli4_hba.sp_cqe_event_pool);
5614 }
5615 return 0;
5616
5617out_pool_create_fail:
5618 lpfc_sli4_cq_event_pool_destroy(phba);
5619 return -ENOMEM;
5620}
5621
5622/**
5623 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5624 * @phba: pointer to lpfc hba data structure.
5625 *
5626 * This routine is invoked to free the pool of completion queue events at
5627 * driver unload time. Note that, it is the responsibility of the driver
5628 * cleanup routine to free all the outstanding completion-queue events
5629 * allocated from this pool back into the pool before invoking this routine
5630 * to destroy the pool.
5631 **/
5632static void
5633lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5634{
5635 struct lpfc_cq_event *cq_event, *next_cq_event;
5636
5637 list_for_each_entry_safe(cq_event, next_cq_event,
5638 &phba->sli4_hba.sp_cqe_event_pool, list) {
5639 list_del(&cq_event->list);
5640 kfree(cq_event);
5641 }
5642}
5643
5644/**
5645 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5646 * @phba: pointer to lpfc hba data structure.
5647 *
5648 * This routine is the lock free version of the API invoked to allocate a
5649 * completion-queue event from the free pool.
5650 *
5651 * Return: Pointer to the newly allocated completion-queue event if successful
5652 * NULL otherwise.
5653 **/
5654struct lpfc_cq_event *
5655__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5656{
5657 struct lpfc_cq_event *cq_event = NULL;
5658
5659 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5660 struct lpfc_cq_event, list);
5661 return cq_event;
5662}
5663
5664/**
5665 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5666 * @phba: pointer to lpfc hba data structure.
5667 *
5668 * This routine is the lock version of the API invoked to allocate a
5669 * completion-queue event from the free pool.
5670 *
5671 * Return: Pointer to the newly allocated completion-queue event if successful
5672 * NULL otherwise.
5673 **/
5674struct lpfc_cq_event *
5675lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5676{
5677 struct lpfc_cq_event *cq_event;
5678 unsigned long iflags;
5679
5680 spin_lock_irqsave(&phba->hbalock, iflags);
5681 cq_event = __lpfc_sli4_cq_event_alloc(phba);
5682 spin_unlock_irqrestore(&phba->hbalock, iflags);
5683 return cq_event;
5684}
5685
5686/**
5687 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5688 * @phba: pointer to lpfc hba data structure.
5689 * @cq_event: pointer to the completion queue event to be freed.
5690 *
5691 * This routine is the lock free version of the API invoked to release a
5692 * completion-queue event back into the free pool.
5693 **/
5694void
5695__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5696 struct lpfc_cq_event *cq_event)
5697{
5698 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
5699}
5700
5701/**
5702 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5703 * @phba: pointer to lpfc hba data structure.
5704 * @cq_event: pointer to the completion queue event to be freed.
5705 *
5706 * This routine is the lock version of the API invoked to release a
5707 * completion-queue event back into the free pool.
5708 **/
5709void
5710lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5711 struct lpfc_cq_event *cq_event)
5712{
5713 unsigned long iflags;
5714 spin_lock_irqsave(&phba->hbalock, iflags);
5715 __lpfc_sli4_cq_event_release(phba, cq_event);
5716 spin_unlock_irqrestore(&phba->hbalock, iflags);
5717}
5718
5719/**
5720 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
5721 * @phba: pointer to lpfc hba data structure.
5722 *
5723 * This routine is to free all the pending completion-queue events to the
5724 * back into the free pool for device reset.
5725 **/
5726static void
5727lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
5728{
5729 LIST_HEAD(cqelist);
5730 struct lpfc_cq_event *cqe;
5731 unsigned long iflags;
5732
5733 /* Retrieve all the pending WCQEs from pending WCQE lists */
5734 spin_lock_irqsave(&phba->hbalock, iflags);
5735 /* Pending FCP XRI abort events */
5736 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
5737 &cqelist);
5738 /* Pending ELS XRI abort events */
5739 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
5740 &cqelist);
5741 /* Pending asynnc events */
5742 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
5743 &cqelist);
5744 spin_unlock_irqrestore(&phba->hbalock, iflags);
5745
5746 while (!list_empty(&cqelist)) {
5747 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
5748 lpfc_sli4_cq_event_release(phba, cqe);
5749 }
5750}
5751
5752/**
5753 * lpfc_pci_function_reset - Reset pci function.
5754 * @phba: pointer to lpfc hba data structure.
5755 *
5756 * This routine is invoked to request a PCI function reset. It will destroys
5757 * all resources assigned to the PCI function which originates this request.
5758 *
5759 * Return codes
5760 * 0 - sucessful
5761 * ENOMEM - No availble memory
5762 * EIO - The mailbox failed to complete successfully.
5763 **/
5764int
5765lpfc_pci_function_reset(struct lpfc_hba *phba)
5766{
5767 LPFC_MBOXQ_t *mboxq;
5768 uint32_t rc = 0;
5769 uint32_t shdr_status, shdr_add_status;
5770 union lpfc_sli4_cfg_shdr *shdr;
5771
5772 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5773 if (!mboxq) {
5774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5775 "0494 Unable to allocate memory for issuing "
5776 "SLI_FUNCTION_RESET mailbox command\n");
5777 return -ENOMEM;
5778 }
5779
5780 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
5781 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5782 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
5783 LPFC_SLI4_MBX_EMBED);
5784 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5785 shdr = (union lpfc_sli4_cfg_shdr *)
5786 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5787 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5788 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5789 if (rc != MBX_TIMEOUT)
5790 mempool_free(mboxq, phba->mbox_mem_pool);
5791 if (shdr_status || shdr_add_status || rc) {
5792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5793 "0495 SLI_FUNCTION_RESET mailbox failed with "
5794 "status x%x add_status x%x, mbx status x%x\n",
5795 shdr_status, shdr_add_status, rc);
5796 rc = -ENXIO;
5797 }
5798 return rc;
5799}
5800
5801/**
5802 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
5803 * @phba: pointer to lpfc hba data structure.
5804 * @cnt: number of nop mailbox commands to send.
5805 *
5806 * This routine is invoked to send a number @cnt of NOP mailbox command and
5807 * wait for each command to complete.
5808 *
5809 * Return: the number of NOP mailbox command completed.
5810 **/
5811static int
5812lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
5813{
5814 LPFC_MBOXQ_t *mboxq;
5815 int length, cmdsent;
5816 uint32_t mbox_tmo;
5817 uint32_t rc = 0;
5818 uint32_t shdr_status, shdr_add_status;
5819 union lpfc_sli4_cfg_shdr *shdr;
5820
5821 if (cnt == 0) {
5822 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5823 "2518 Requested to send 0 NOP mailbox cmd\n");
5824 return cnt;
5825 }
5826
5827 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5828 if (!mboxq) {
5829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5830 "2519 Unable to allocate memory for issuing "
5831 "NOP mailbox command\n");
5832 return 0;
5833 }
5834
5835 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
5836 length = (sizeof(struct lpfc_mbx_nop) -
5837 sizeof(struct lpfc_sli4_cfg_mhdr));
5838 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5839 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
5840
5841 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5842 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
5843 if (!phba->sli4_hba.intr_enable)
5844 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5845 else
5846 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
5847 if (rc == MBX_TIMEOUT)
5848 break;
5849 /* Check return status */
5850 shdr = (union lpfc_sli4_cfg_shdr *)
5851 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5852 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5853 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
5854 &shdr->response);
5855 if (shdr_status || shdr_add_status || rc) {
5856 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5857 "2520 NOP mailbox command failed "
5858 "status x%x add_status x%x mbx "
5859 "status x%x\n", shdr_status,
5860 shdr_add_status, rc);
5861 break;
5862 }
5863 }
5864
5865 if (rc != MBX_TIMEOUT)
5866 mempool_free(mboxq, phba->mbox_mem_pool);
5867
5868 return cmdsent;
5869}
5870
5871/**
5872 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
5873 * @phba: pointer to lpfc hba data structure.
5874 * @fcfi: fcf index.
5875 *
5876 * This routine is invoked to unregister a FCFI from device.
5877 **/
5878void
5879lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
5880{
5881 LPFC_MBOXQ_t *mbox;
5882 uint32_t mbox_tmo;
5883 int rc;
5884 unsigned long flags;
5885
5886 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5887
5888 if (!mbox)
5889 return;
5890
5891 lpfc_unreg_fcfi(mbox, fcfi);
5892
5893 if (!phba->sli4_hba.intr_enable)
5894 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5895 else {
5896 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5897 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5898 }
5899 if (rc != MBX_TIMEOUT)
5900 mempool_free(mbox, phba->mbox_mem_pool);
5901 if (rc != MBX_SUCCESS)
5902 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5903 "2517 Unregister FCFI command failed "
5904 "status %d, mbxStatus x%x\n", rc,
5905 bf_get(lpfc_mqe_status, &mbox->u.mqe));
5906 else {
5907 spin_lock_irqsave(&phba->hbalock, flags);
5908 /* Mark the FCFI is no longer registered */
5909 phba->fcf.fcf_flag &=
5910 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_DISCOVERED);
5911 spin_unlock_irqrestore(&phba->hbalock, flags);
5912 }
5913}
5914
5915/**
5916 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
5917 * @phba: pointer to lpfc hba data structure.
5918 *
5919 * This routine is invoked to set up the PCI device memory space for device
5920 * with SLI-4 interface spec.
5921 *
5922 * Return codes
5923 * 0 - sucessful
5924 * other values - error
5925 **/
5926static int
5927lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
5928{
5929 struct pci_dev *pdev;
5930 unsigned long bar0map_len, bar1map_len, bar2map_len;
5931 int error = -ENODEV;
5932
5933 /* Obtain PCI device reference */
5934 if (!phba->pcidev)
5935 return error;
5936 else
5937 pdev = phba->pcidev;
5938
5939 /* Set the device DMA mask size */
5940 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
5941 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5942 return error;
5943
5944 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
5945 * number of bytes required by each mapping. They are actually
5946 * mapping to the PCI BAR regions 1, 2, and 4 by the SLI4 device.
5947 */
5948 phba->pci_bar0_map = pci_resource_start(pdev, LPFC_SLI4_BAR0);
5949 bar0map_len = pci_resource_len(pdev, LPFC_SLI4_BAR0);
5950
5951 phba->pci_bar1_map = pci_resource_start(pdev, LPFC_SLI4_BAR1);
5952 bar1map_len = pci_resource_len(pdev, LPFC_SLI4_BAR1);
5953
5954 phba->pci_bar2_map = pci_resource_start(pdev, LPFC_SLI4_BAR2);
5955 bar2map_len = pci_resource_len(pdev, LPFC_SLI4_BAR2);
5956
5957 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
5958 phba->sli4_hba.conf_regs_memmap_p =
5959 ioremap(phba->pci_bar0_map, bar0map_len);
5960 if (!phba->sli4_hba.conf_regs_memmap_p) {
5961 dev_printk(KERN_ERR, &pdev->dev,
5962 "ioremap failed for SLI4 PCI config registers.\n");
5963 goto out;
5964 }
5965
5966 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
5967 phba->sli4_hba.ctrl_regs_memmap_p =
5968 ioremap(phba->pci_bar1_map, bar1map_len);
5969 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
5970 dev_printk(KERN_ERR, &pdev->dev,
5971 "ioremap failed for SLI4 HBA control registers.\n");
5972 goto out_iounmap_conf;
5973 }
5974
5975 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
5976 phba->sli4_hba.drbl_regs_memmap_p =
5977 ioremap(phba->pci_bar2_map, bar2map_len);
5978 if (!phba->sli4_hba.drbl_regs_memmap_p) {
5979 dev_printk(KERN_ERR, &pdev->dev,
5980 "ioremap failed for SLI4 HBA doorbell registers.\n");
5981 goto out_iounmap_ctrl;
5982 }
5983
5984 /* Set up BAR0 PCI config space register memory map */
5985 lpfc_sli4_bar0_register_memmap(phba);
5986
5987 /* Set up BAR1 register memory map */
5988 lpfc_sli4_bar1_register_memmap(phba);
5989
5990 /* Set up BAR2 register memory map */
5991 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
5992 if (error)
5993 goto out_iounmap_all;
5994
5995 return 0;
5996
5997out_iounmap_all:
5998 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
5999out_iounmap_ctrl:
6000 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6001out_iounmap_conf:
6002 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6003out:
6004 return error;
6005}
6006
6007/**
6008 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6009 * @phba: pointer to lpfc hba data structure.
6010 *
6011 * This routine is invoked to unset the PCI device memory space for device
6012 * with SLI-4 interface spec.
6013 **/
6014static void
6015lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6016{
6017 struct pci_dev *pdev;
6018
6019 /* Obtain PCI device reference */
6020 if (!phba->pcidev)
6021 return;
6022 else
6023 pdev = phba->pcidev;
6024
6025 /* Free coherent DMA memory allocated */
6026
6027 /* Unmap I/O memory space */
6028 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6029 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6030 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6031
6032 return;
6033}
6034
6035/**
6036 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6037 * @phba: pointer to lpfc hba data structure.
6038 *
6039 * This routine is invoked to enable the MSI-X interrupt vectors to device
6040 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6041 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6042 * invoked, enables either all or nothing, depending on the current
6043 * availability of PCI vector resources. The device driver is responsible
6044 * for calling the individual request_irq() to register each MSI-X vector
6045 * with a interrupt handler, which is done in this function. Note that
6046 * later when device is unloading, the driver should always call free_irq()
6047 * on all MSI-X vectors it has done request_irq() on before calling
6048 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6049 * will be left with MSI-X enabled and leaks its vectors.
6050 *
6051 * Return codes
6052 * 0 - sucessful
6053 * other values - error
6054 **/
6055static int
6056lpfc_sli_enable_msix(struct lpfc_hba *phba)
6057{
6058 int rc, i;
6059 LPFC_MBOXQ_t *pmb;
6060
6061 /* Set up MSI-X multi-message vectors */
6062 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6063 phba->msix_entries[i].entry = i;
6064
6065 /* Configure MSI-X capability structure */
6066 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6067 ARRAY_SIZE(phba->msix_entries));
6068 if (rc) {
6069 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6070 "0420 PCI enable MSI-X failed (%d)\n", rc);
6071 goto msi_fail_out;
6072 }
6073 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6074 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6075 "0477 MSI-X entry[%d]: vector=x%x "
6076 "message=%d\n", i,
6077 phba->msix_entries[i].vector,
6078 phba->msix_entries[i].entry);
6079 /*
6080 * Assign MSI-X vectors to interrupt handlers
6081 */
6082
6083 /* vector-0 is associated to slow-path handler */
6084 rc = request_irq(phba->msix_entries[0].vector,
6085 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6086 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6087 if (rc) {
6088 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6089 "0421 MSI-X slow-path request_irq failed "
6090 "(%d)\n", rc);
6091 goto msi_fail_out;
6092 }
6093
6094 /* vector-1 is associated to fast-path handler */
6095 rc = request_irq(phba->msix_entries[1].vector,
6096 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6097 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6098
6099 if (rc) {
6100 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6101 "0429 MSI-X fast-path request_irq failed "
6102 "(%d)\n", rc);
6103 goto irq_fail_out;
6104 }
6105
6106 /*
6107 * Configure HBA MSI-X attention conditions to messages
6108 */
6109 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6110
6111 if (!pmb) {
6112 rc = -ENOMEM;
6113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6114 "0474 Unable to allocate memory for issuing "
6115 "MBOX_CONFIG_MSI command\n");
6116 goto mem_fail_out;
6117 }
6118 rc = lpfc_config_msi(phba, pmb);
6119 if (rc)
6120 goto mbx_fail_out;
6121 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6122 if (rc != MBX_SUCCESS) {
6123 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6124 "0351 Config MSI mailbox command failed, "
6125 "mbxCmd x%x, mbxStatus x%x\n",
6126 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6127 goto mbx_fail_out;
6128 }
6129
6130 /* Free memory allocated for mailbox command */
6131 mempool_free(pmb, phba->mbox_mem_pool);
6132 return rc;
6133
6134mbx_fail_out:
6135 /* Free memory allocated for mailbox command */
6136 mempool_free(pmb, phba->mbox_mem_pool);
6137
6138mem_fail_out:
6139 /* free the irq already requested */
6140 free_irq(phba->msix_entries[1].vector, phba);
6141
6142irq_fail_out:
6143 /* free the irq already requested */
6144 free_irq(phba->msix_entries[0].vector, phba);
6145
6146msi_fail_out:
6147 /* Unconfigure MSI-X capability structure */
6148 pci_disable_msix(phba->pcidev);
6149 return rc;
6150}
6151
6152/**
6153 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6154 * @phba: pointer to lpfc hba data structure.
6155 *
6156 * This routine is invoked to release the MSI-X vectors and then disable the
6157 * MSI-X interrupt mode to device with SLI-3 interface spec.
6158 **/
6159static void
6160lpfc_sli_disable_msix(struct lpfc_hba *phba)
6161{
6162 int i;
6163
6164 /* Free up MSI-X multi-message vectors */
6165 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6166 free_irq(phba->msix_entries[i].vector, phba);
6167 /* Disable MSI-X */
6168 pci_disable_msix(phba->pcidev);
6169
6170 return;
6171}
6172
6173/**
6174 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6175 * @phba: pointer to lpfc hba data structure.
6176 *
6177 * This routine is invoked to enable the MSI interrupt mode to device with
6178 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6179 * enable the MSI vector. The device driver is responsible for calling the
6180 * request_irq() to register MSI vector with a interrupt the handler, which
6181 * is done in this function.
6182 *
6183 * Return codes
6184 * 0 - sucessful
6185 * other values - error
6186 */
6187static int
6188lpfc_sli_enable_msi(struct lpfc_hba *phba)
6189{
6190 int rc;
6191
6192 rc = pci_enable_msi(phba->pcidev);
6193 if (!rc)
6194 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6195 "0462 PCI enable MSI mode success.\n");
6196 else {
6197 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6198 "0471 PCI enable MSI mode failed (%d)\n", rc);
6199 return rc;
6200 }
6201
6202 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6203 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6204 if (rc) {
6205 pci_disable_msi(phba->pcidev);
6206 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6207 "0478 MSI request_irq failed (%d)\n", rc);
6208 }
6209 return rc;
6210}
6211
6212/**
6213 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6214 * @phba: pointer to lpfc hba data structure.
6215 *
6216 * This routine is invoked to disable the MSI interrupt mode to device with
6217 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6218 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6219 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6220 * its vector.
6221 */
6222static void
6223lpfc_sli_disable_msi(struct lpfc_hba *phba)
6224{
6225 free_irq(phba->pcidev->irq, phba);
6226 pci_disable_msi(phba->pcidev);
6227 return;
6228}
6229
6230/**
6231 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6232 * @phba: pointer to lpfc hba data structure.
6233 *
6234 * This routine is invoked to enable device interrupt and associate driver's
6235 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6236 * spec. Depends on the interrupt mode configured to the driver, the driver
6237 * will try to fallback from the configured interrupt mode to an interrupt
6238 * mode which is supported by the platform, kernel, and device in the order
6239 * of:
6240 * MSI-X -> MSI -> IRQ.
6241 *
6242 * Return codes
6243 * 0 - sucessful
6244 * other values - error
6245 **/
6246static uint32_t
6247lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6248{
6249 uint32_t intr_mode = LPFC_INTR_ERROR;
6250 int retval;
6251
6252 if (cfg_mode == 2) {
6253 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6254 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6255 if (!retval) {
6256 /* Now, try to enable MSI-X interrupt mode */
6257 retval = lpfc_sli_enable_msix(phba);
6258 if (!retval) {
6259 /* Indicate initialization to MSI-X mode */
6260 phba->intr_type = MSIX;
6261 intr_mode = 2;
6262 }
6263 }
6264 }
6265
6266 /* Fallback to MSI if MSI-X initialization failed */
6267 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6268 retval = lpfc_sli_enable_msi(phba);
6269 if (!retval) {
6270 /* Indicate initialization to MSI mode */
6271 phba->intr_type = MSI;
6272 intr_mode = 1;
6273 }
6274 }
6275
6276 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6277 if (phba->intr_type == NONE) {
6278 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6279 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6280 if (!retval) {
6281 /* Indicate initialization to INTx mode */
6282 phba->intr_type = INTx;
6283 intr_mode = 0;
6284 }
6285 }
6286 return intr_mode;
6287}
6288
6289/**
6290 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6291 * @phba: pointer to lpfc hba data structure.
6292 *
6293 * This routine is invoked to disable device interrupt and disassociate the
6294 * driver's interrupt handler(s) from interrupt vector(s) to device with
6295 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6296 * release the interrupt vector(s) for the message signaled interrupt.
6297 **/
6298static void
6299lpfc_sli_disable_intr(struct lpfc_hba *phba)
6300{
6301 /* Disable the currently initialized interrupt mode */
6302 if (phba->intr_type == MSIX)
6303 lpfc_sli_disable_msix(phba);
6304 else if (phba->intr_type == MSI)
6305 lpfc_sli_disable_msi(phba);
6306 else if (phba->intr_type == INTx)
6307 free_irq(phba->pcidev->irq, phba);
6308
6309 /* Reset interrupt management states */
6310 phba->intr_type = NONE;
6311 phba->sli.slistat.sli_intr = 0;
6312
6313 return;
6314}
6315
6316/**
6317 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6318 * @phba: pointer to lpfc hba data structure.
6319 *
6320 * This routine is invoked to enable the MSI-X interrupt vectors to device
6321 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6322 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6323 * enables either all or nothing, depending on the current availability of
6324 * PCI vector resources. The device driver is responsible for calling the
6325 * individual request_irq() to register each MSI-X vector with a interrupt
6326 * handler, which is done in this function. Note that later when device is
6327 * unloading, the driver should always call free_irq() on all MSI-X vectors
6328 * it has done request_irq() on before calling pci_disable_msix(). Failure
6329 * to do so results in a BUG_ON() and a device will be left with MSI-X
6330 * enabled and leaks its vectors.
6331 *
6332 * Return codes
6333 * 0 - sucessful
6334 * other values - error
6335 **/
6336static int
6337lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6338{
6339 int rc, index;
6340
6341 /* Set up MSI-X multi-message vectors */
6342 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6343 phba->sli4_hba.msix_entries[index].entry = index;
6344
6345 /* Configure MSI-X capability structure */
6346 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6347 phba->sli4_hba.cfg_eqn);
6348 if (rc) {
6349 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6350 "0484 PCI enable MSI-X failed (%d)\n", rc);
6351 goto msi_fail_out;
6352 }
6353 /* Log MSI-X vector assignment */
6354 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6355 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6356 "0489 MSI-X entry[%d]: vector=x%x "
6357 "message=%d\n", index,
6358 phba->sli4_hba.msix_entries[index].vector,
6359 phba->sli4_hba.msix_entries[index].entry);
6360 /*
6361 * Assign MSI-X vectors to interrupt handlers
6362 */
6363
6364 /* The first vector must associated to slow-path handler for MQ */
6365 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6366 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6367 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6368 if (rc) {
6369 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6370 "0485 MSI-X slow-path request_irq failed "
6371 "(%d)\n", rc);
6372 goto msi_fail_out;
6373 }
6374
6375 /* The rest of the vector(s) are associated to fast-path handler(s) */
6376 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6377 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6378 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6379 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6380 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6381 LPFC_FP_DRIVER_HANDLER_NAME,
6382 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6383 if (rc) {
6384 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6385 "0486 MSI-X fast-path (%d) "
6386 "request_irq failed (%d)\n", index, rc);
6387 goto cfg_fail_out;
6388 }
6389 }
6390
6391 return rc;
6392
6393cfg_fail_out:
6394 /* free the irq already requested */
6395 for (--index; index >= 1; index--)
6396 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6397 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6398
6399 /* free the irq already requested */
6400 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6401
6402msi_fail_out:
6403 /* Unconfigure MSI-X capability structure */
6404 pci_disable_msix(phba->pcidev);
6405 return rc;
6406}
6407
6408/**
6409 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6410 * @phba: pointer to lpfc hba data structure.
6411 *
6412 * This routine is invoked to release the MSI-X vectors and then disable the
6413 * MSI-X interrupt mode to device with SLI-4 interface spec.
6414 **/
6415static void
6416lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6417{
6418 int index;
6419
6420 /* Free up MSI-X multi-message vectors */
6421 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6422
6423 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6424 free_irq(phba->sli4_hba.msix_entries[index].vector,
6425 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6426 /* Disable MSI-X */
6427 pci_disable_msix(phba->pcidev);
6428
6429 return;
6430}
6431
6432/**
6433 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6434 * @phba: pointer to lpfc hba data structure.
6435 *
6436 * This routine is invoked to enable the MSI interrupt mode to device with
6437 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6438 * to enable the MSI vector. The device driver is responsible for calling
6439 * the request_irq() to register MSI vector with a interrupt the handler,
6440 * which is done in this function.
6441 *
6442 * Return codes
6443 * 0 - sucessful
6444 * other values - error
6445 **/
6446static int
6447lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6448{
6449 int rc, index;
6450
6451 rc = pci_enable_msi(phba->pcidev);
6452 if (!rc)
6453 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6454 "0487 PCI enable MSI mode success.\n");
6455 else {
6456 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6457 "0488 PCI enable MSI mode failed (%d)\n", rc);
6458 return rc;
6459 }
6460
6461 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6462 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6463 if (rc) {
6464 pci_disable_msi(phba->pcidev);
6465 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6466 "0490 MSI request_irq failed (%d)\n", rc);
6467 }
6468
6469 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6470 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6471 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6472 }
6473
6474 return rc;
6475}
6476
6477/**
6478 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6479 * @phba: pointer to lpfc hba data structure.
6480 *
6481 * This routine is invoked to disable the MSI interrupt mode to device with
6482 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6483 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6484 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6485 * its vector.
6486 **/
6487static void
6488lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6489{
6490 free_irq(phba->pcidev->irq, phba);
6491 pci_disable_msi(phba->pcidev);
6492 return;
6493}
6494
6495/**
6496 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6497 * @phba: pointer to lpfc hba data structure.
6498 *
6499 * This routine is invoked to enable device interrupt and associate driver's
6500 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6501 * interface spec. Depends on the interrupt mode configured to the driver,
6502 * the driver will try to fallback from the configured interrupt mode to an
6503 * interrupt mode which is supported by the platform, kernel, and device in
6504 * the order of:
6505 * MSI-X -> MSI -> IRQ.
6506 *
6507 * Return codes
6508 * 0 - sucessful
6509 * other values - error
6510 **/
6511static uint32_t
6512lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6513{
6514 uint32_t intr_mode = LPFC_INTR_ERROR;
6515 int retval, index;
6516
6517 if (cfg_mode == 2) {
6518 /* Preparation before conf_msi mbox cmd */
6519 retval = 0;
6520 if (!retval) {
6521 /* Now, try to enable MSI-X interrupt mode */
6522 retval = lpfc_sli4_enable_msix(phba);
6523 if (!retval) {
6524 /* Indicate initialization to MSI-X mode */
6525 phba->intr_type = MSIX;
6526 intr_mode = 2;
6527 }
6528 }
6529 }
6530
6531 /* Fallback to MSI if MSI-X initialization failed */
6532 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6533 retval = lpfc_sli4_enable_msi(phba);
6534 if (!retval) {
6535 /* Indicate initialization to MSI mode */
6536 phba->intr_type = MSI;
6537 intr_mode = 1;
6538 }
6539 }
6540
6541 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6542 if (phba->intr_type == NONE) {
6543 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6544 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6545 if (!retval) {
6546 /* Indicate initialization to INTx mode */
6547 phba->intr_type = INTx;
6548 intr_mode = 0;
6549 for (index = 0; index < phba->cfg_fcp_eq_count;
6550 index++) {
6551 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6552 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6553 }
6554 }
6555 }
6556 return intr_mode;
6557}
6558
6559/**
6560 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6561 * @phba: pointer to lpfc hba data structure.
6562 *
6563 * This routine is invoked to disable device interrupt and disassociate
6564 * the driver's interrupt handler(s) from interrupt vector(s) to device
6565 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6566 * will release the interrupt vector(s) for the message signaled interrupt.
6567 **/
6568static void
6569lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6570{
6571 /* Disable the currently initialized interrupt mode */
6572 if (phba->intr_type == MSIX)
6573 lpfc_sli4_disable_msix(phba);
6574 else if (phba->intr_type == MSI)
6575 lpfc_sli4_disable_msi(phba);
6576 else if (phba->intr_type == INTx)
6577 free_irq(phba->pcidev->irq, phba);
6578
6579 /* Reset interrupt management states */
6580 phba->intr_type = NONE;
6581 phba->sli.slistat.sli_intr = 0;
6582
6583 return;
6584}
6585
6586/**
6587 * lpfc_unset_hba - Unset SLI3 hba device initialization
6588 * @phba: pointer to lpfc hba data structure.
6589 *
6590 * This routine is invoked to unset the HBA device initialization steps to
6591 * a device with SLI-3 interface spec.
6592 **/
6593static void
6594lpfc_unset_hba(struct lpfc_hba *phba)
6595{
6596 struct lpfc_vport *vport = phba->pport;
6597 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6598
6599 spin_lock_irq(shost->host_lock);
6600 vport->load_flag |= FC_UNLOADING;
6601 spin_unlock_irq(shost->host_lock);
6602
6603 lpfc_stop_hba_timers(phba);
6604
6605 phba->pport->work_port_events = 0;
6606
6607 lpfc_sli_hba_down(phba);
6608
6609 lpfc_sli_brdrestart(phba);
6610
6611 lpfc_sli_disable_intr(phba);
6612
6613 return;
6614}
6615
6616/**
6617 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6618 * @phba: pointer to lpfc hba data structure.
6619 *
6620 * This routine is invoked to unset the HBA device initialization steps to
6621 * a device with SLI-4 interface spec.
6622 **/
6623static void
6624lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6625{
6626 struct lpfc_vport *vport = phba->pport;
6627 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6628
6629 spin_lock_irq(shost->host_lock);
6630 vport->load_flag |= FC_UNLOADING;
6631 spin_unlock_irq(shost->host_lock);
6632
6633 phba->pport->work_port_events = 0;
6634
6635 lpfc_sli4_hba_down(phba);
6636
6637 lpfc_sli4_disable_intr(phba);
6638
6639 return;
6640}
6641
6642/**
6643 * lpfc_sli4_hba_unset - Unset the fcoe hba
6644 * @phba: Pointer to HBA context object.
6645 *
6646 * This function is called in the SLI4 code path to reset the HBA's FCoE
6647 * function. The caller is not required to hold any lock. This routine
6648 * issues PCI function reset mailbox command to reset the FCoE function.
6649 * At the end of the function, it calls lpfc_hba_down_post function to
6650 * free any pending commands.
6651 **/
6652static void
6653lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6654{
6655 int wait_cnt = 0;
6656 LPFC_MBOXQ_t *mboxq;
6657
6658 lpfc_stop_hba_timers(phba);
6659 phba->sli4_hba.intr_enable = 0;
6660
6661 /*
6662 * Gracefully wait out the potential current outstanding asynchronous
6663 * mailbox command.
6664 */
6665
6666 /* First, block any pending async mailbox command from posted */
6667 spin_lock_irq(&phba->hbalock);
6668 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6669 spin_unlock_irq(&phba->hbalock);
6670 /* Now, trying to wait it out if we can */
6671 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6672 msleep(10);
6673 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
6674 break;
6675 }
6676 /* Forcefully release the outstanding mailbox command if timed out */
6677 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6678 spin_lock_irq(&phba->hbalock);
6679 mboxq = phba->sli.mbox_active;
6680 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6681 __lpfc_mbox_cmpl_put(phba, mboxq);
6682 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6683 phba->sli.mbox_active = NULL;
6684 spin_unlock_irq(&phba->hbalock);
6685 }
6686
6687 /* Tear down the queues in the HBA */
6688 lpfc_sli4_queue_unset(phba);
6689
6690 /* Disable PCI subsystem interrupt */
6691 lpfc_sli4_disable_intr(phba);
6692
6693 /* Stop kthread signal shall trigger work_done one more time */
6694 kthread_stop(phba->worker_thread);
6695
6696 /* Stop the SLI4 device port */
6697 phba->pport->work_port_events = 0;
6698}
6699
6700/**
6701 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
6702 * @pdev: pointer to PCI device
6703 * @pid: pointer to PCI device identifier
6704 *
6705 * This routine is to be called to attach a device with SLI-3 interface spec
6706 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6707 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
6708 * information of the device and driver to see if the driver state that it can
6709 * support this kind of device. If the match is successful, the driver core
6710 * invokes this routine. If this routine determines it can claim the HBA, it
6711 * does all the initialization that it needs to do to handle the HBA properly.
6712 *
6713 * Return code
6714 * 0 - driver can claim the device
6715 * negative value - driver can not claim the device
6716 **/
6717static int __devinit
6718lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
6719{
6720 struct lpfc_hba *phba;
6721 struct lpfc_vport *vport = NULL;
6722 int error;
6723 uint32_t cfg_mode, intr_mode;
6724
6725 /* Allocate memory for HBA structure */
6726 phba = lpfc_hba_alloc(pdev);
6727 if (!phba)
6728 return -ENOMEM;
6729
6730 /* Perform generic PCI device enabling operation */
6731 error = lpfc_enable_pci_dev(phba);
6732 if (error) {
6733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6734 "1401 Failed to enable pci device.\n");
6735 goto out_free_phba;
6736 }
6737
6738 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
6739 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
6740 if (error)
6741 goto out_disable_pci_dev;
6742
6743 /* Set up SLI-3 specific device PCI memory space */
6744 error = lpfc_sli_pci_mem_setup(phba);
6745 if (error) {
6746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6747 "1402 Failed to set up pci memory space.\n");
6748 goto out_disable_pci_dev;
6749 }
6750
6751 /* Set up phase-1 common device driver resources */
6752 error = lpfc_setup_driver_resource_phase1(phba);
6753 if (error) {
6754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6755 "1403 Failed to set up driver resource.\n");
6756 goto out_unset_pci_mem_s3;
6757 }
6758
6759 /* Set up SLI-3 specific device driver resources */
6760 error = lpfc_sli_driver_resource_setup(phba);
6761 if (error) {
6762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6763 "1404 Failed to set up driver resource.\n");
6764 goto out_unset_pci_mem_s3;
6765 }
6766
6767 /* Initialize and populate the iocb list per host */
6768 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
6769 if (error) {
6770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6771 "1405 Failed to initialize iocb list.\n");
6772 goto out_unset_driver_resource_s3;
6773 }
6774
6775 /* Set up common device driver resources */
6776 error = lpfc_setup_driver_resource_phase2(phba);
6777 if (error) {
6778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6779 "1406 Failed to set up driver resource.\n");
6780 goto out_free_iocb_list;
6781 }
6782
6783 /* Create SCSI host to the physical port */
6784 error = lpfc_create_shost(phba);
6785 if (error) {
6786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6787 "1407 Failed to create scsi host.\n");
6788 goto out_unset_driver_resource;
6789 }
6790
6791 /* Configure sysfs attributes */
6792 vport = phba->pport;
6793 error = lpfc_alloc_sysfs_attr(vport);
6794 if (error) {
6795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6796 "1476 Failed to allocate sysfs attr\n");
6797 goto out_destroy_shost;
6798 }
6799
6800 /* Now, trying to enable interrupt and bring up the device */
6801 cfg_mode = phba->cfg_use_msi;
6802 while (true) {
6803 /* Put device to a known state before enabling interrupt */
6804 lpfc_stop_port(phba);
6805 /* Configure and enable interrupt */
6806 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
6807 if (intr_mode == LPFC_INTR_ERROR) {
6808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6809 "0431 Failed to enable interrupt.\n");
6810 error = -ENODEV;
6811 goto out_free_sysfs_attr;
6812 }
6813 /* SLI-3 HBA setup */
6814 if (lpfc_sli_hba_setup(phba)) {
6815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6816 "1477 Failed to set up hba\n");
6817 error = -ENODEV;
6818 goto out_remove_device;
6819 }
6820
6821 /* Wait 50ms for the interrupts of previous mailbox commands */
6822 msleep(50);
6823 /* Check active interrupts on message signaled interrupts */
6824 if (intr_mode == 0 ||
6825 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
6826 /* Log the current active interrupt mode */
6827 phba->intr_mode = intr_mode;
6828 lpfc_log_intr_mode(phba, intr_mode);
6829 break;
6830 } else {
6831 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6832 "0447 Configure interrupt mode (%d) "
6833 "failed active interrupt test.\n",
6834 intr_mode);
6835 /* Disable the current interrupt mode */
6836 lpfc_sli_disable_intr(phba);
6837 /* Try next level of interrupt mode */
6838 cfg_mode = --intr_mode;
6839 }
6840 }
6841
6842 /* Perform post initialization setup */
6843 lpfc_post_init_setup(phba);
6844
6845 /* Check if there are static vports to be created. */
6846 lpfc_create_static_vport(phba);
6847
6848 return 0;
6849
6850out_remove_device:
6851 lpfc_unset_hba(phba);
6852out_free_sysfs_attr:
6853 lpfc_free_sysfs_attr(vport);
6854out_destroy_shost:
6855 lpfc_destroy_shost(phba);
6856out_unset_driver_resource:
6857 lpfc_unset_driver_resource_phase2(phba);
6858out_free_iocb_list:
6859 lpfc_free_iocb_list(phba);
6860out_unset_driver_resource_s3:
6861 lpfc_sli_driver_resource_unset(phba);
6862out_unset_pci_mem_s3:
6863 lpfc_sli_pci_mem_unset(phba);
6864out_disable_pci_dev:
6865 lpfc_disable_pci_dev(phba);
6866out_free_phba:
6867 lpfc_hba_free(phba);
6868 return error;
6869}
6870
6871/**
6872 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
6873 * @pdev: pointer to PCI device
6874 *
6875 * This routine is to be called to disattach a device with SLI-3 interface
6876 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6877 * removed from PCI bus, it performs all the necessary cleanup for the HBA
6878 * device to be removed from the PCI subsystem properly.
6879 **/
6880static void __devexit
6881lpfc_pci_remove_one_s3(struct pci_dev *pdev)
6882{
6883 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6884 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6885 struct lpfc_vport **vports;
6886 struct lpfc_hba *phba = vport->phba;
6887 int i;
6888 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
6889
6890 spin_lock_irq(&phba->hbalock);
6891 vport->load_flag |= FC_UNLOADING;
6892 spin_unlock_irq(&phba->hbalock);
6893
6894 lpfc_free_sysfs_attr(vport);
6895
6896 /* Release all the vports against this physical port */
6897 vports = lpfc_create_vport_work_array(phba);
6898 if (vports != NULL)
6899 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
6900 fc_vport_terminate(vports[i]->fc_vport);
6901 lpfc_destroy_vport_work_array(phba, vports);
6902
6903 /* Remove FC host and then SCSI host with the physical port */
6904 fc_remove_host(shost);
6905 scsi_remove_host(shost);
6906 lpfc_cleanup(vport);
6907
6908 /*
6909 * Bring down the SLI Layer. This step disable all interrupts,
6910 * clears the rings, discards all mailbox commands, and resets
6911 * the HBA.
6912 */
6913
6914 /* HBA interrupt will be diabled after this call */
6915 lpfc_sli_hba_down(phba);
6916 /* Stop kthread signal shall trigger work_done one more time */
6917 kthread_stop(phba->worker_thread);
6918 /* Final cleanup of txcmplq and reset the HBA */
6919 lpfc_sli_brdrestart(phba);
6920
6921 lpfc_stop_hba_timers(phba);
6922 spin_lock_irq(&phba->hbalock);
6923 list_del_init(&vport->listentry);
6924 spin_unlock_irq(&phba->hbalock);
6925
6926 lpfc_debugfs_terminate(vport);
6927
6928 /* Disable interrupt */
6929 lpfc_sli_disable_intr(phba);
6930
6931 pci_set_drvdata(pdev, NULL);
6932 scsi_host_put(shost);
6933
6934 /*
6935 * Call scsi_free before mem_free since scsi bufs are released to their
6936 * corresponding pools here.
6937 */
6938 lpfc_scsi_free(phba);
6939 lpfc_mem_free_all(phba);
6940
6941 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6942 phba->hbqslimp.virt, phba->hbqslimp.phys);
6943
6944 /* Free resources associated with SLI2 interface */
6945 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6946 phba->slim2p.virt, phba->slim2p.phys);
6947
6948 /* unmap adapter SLIM and Control Registers */
6949 iounmap(phba->ctrl_regs_memmap_p);
6950 iounmap(phba->slim_memmap_p);
6951
6952 lpfc_hba_free(phba);
6953
6954 pci_release_selected_regions(pdev, bars);
6955 pci_disable_device(pdev);
6956}
6957
6958/**
6959 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
6960 * @pdev: pointer to PCI device
6961 * @msg: power management message
6962 *
6963 * This routine is to be called from the kernel's PCI subsystem to support
6964 * system Power Management (PM) to device with SLI-3 interface spec. When
6965 * PM invokes this method, it quiesces the device by stopping the driver's
6966 * worker thread for the device, turning off device's interrupt and DMA,
6967 * and bring the device offline. Note that as the driver implements the
6968 * minimum PM requirements to a power-aware driver's PM support for the
6969 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
6970 * to the suspend() method call will be treated as SUSPEND and the driver will
6971 * fully reinitialize its device during resume() method call, the driver will
6972 * set device to PCI_D3hot state in PCI config space instead of setting it
6973 * according to the @msg provided by the PM.
6974 *
6975 * Return code
6976 * 0 - driver suspended the device
6977 * Error otherwise
6978 **/
6979static int
6980lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
6981{
6982 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6983 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
6984
6985 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6986 "0473 PCI device Power Management suspend.\n");
6987
6988 /* Bring down the device */
6989 lpfc_offline_prep(phba);
6990 lpfc_offline(phba);
6991 kthread_stop(phba->worker_thread);
6992
6993 /* Disable interrupt from device */
6994 lpfc_sli_disable_intr(phba);
6995
6996 /* Save device state to PCI config space */
6997 pci_save_state(pdev);
6998 pci_set_power_state(pdev, PCI_D3hot);
6999
7000 return 0;
7001}
7002
7003/**
7004 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
7005 * @pdev: pointer to PCI device
7006 *
7007 * This routine is to be called from the kernel's PCI subsystem to support
7008 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7009 * invokes this method, it restores the device's PCI config space state and
7010 * fully reinitializes the device and brings it online. Note that as the
7011 * driver implements the minimum PM requirements to a power-aware driver's
7012 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7013 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7014 * driver will fully reinitialize its device during resume() method call,
7015 * the device will be set to PCI_D0 directly in PCI config space before
7016 * restoring the state.
7017 *
7018 * Return code
7019 * 0 - driver suspended the device
7020 * Error otherwise
7021 **/
7022static int
7023lpfc_pci_resume_one_s3(struct pci_dev *pdev)
7024{
7025 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7026 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7027 uint32_t intr_mode;
7028 int error;
7029
7030 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7031 "0452 PCI device Power Management resume.\n");
7032
7033 /* Restore device state from PCI config space */
7034 pci_set_power_state(pdev, PCI_D0);
7035 pci_restore_state(pdev);
7036 if (pdev->is_busmaster)
7037 pci_set_master(pdev);
7038
7039 /* Startup the kernel thread for this host adapter. */
7040 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7041 "lpfc_worker_%d", phba->brd_no);
7042 if (IS_ERR(phba->worker_thread)) {
7043 error = PTR_ERR(phba->worker_thread);
7044 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7045 "0434 PM resume failed to start worker "
7046 "thread: error=x%x.\n", error);
7047 return error;
7048 }
7049
7050 /* Configure and enable interrupt */
7051 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7052 if (intr_mode == LPFC_INTR_ERROR) {
7053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7054 "0430 PM resume Failed to enable interrupt\n");
7055 return -EIO;
7056 } else
7057 phba->intr_mode = intr_mode;
7058
7059 /* Restart HBA and bring it online */
7060 lpfc_sli_brdrestart(phba);
7061 lpfc_online(phba);
7062
7063 /* Log the current active interrupt mode */
7064 lpfc_log_intr_mode(phba, phba->intr_mode);
7065
7066 return 0;
7067}
7068
7069/**
7070 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
7071 * @pdev: pointer to PCI device.
7072 * @state: the current PCI connection state.
7073 *
7074 * This routine is called from the PCI subsystem for I/O error handling to
7075 * device with SLI-3 interface spec. This function is called by the PCI
7076 * subsystem after a PCI bus error affecting this device has been detected.
7077 * When this function is invoked, it will need to stop all the I/Os and
7078 * interrupt(s) to the device. Once that is done, it will return
7079 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7080 * as desired.
7081 *
7082 * Return codes
7083 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7084 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7085 **/
7086static pci_ers_result_t
7087lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
7088{
7089 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7090 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7091 struct lpfc_sli *psli = &phba->sli;
7092 struct lpfc_sli_ring *pring;
7093
7094 if (state == pci_channel_io_perm_failure) {
7095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7096 "0472 PCI channel I/O permanent failure\n");
7097 /* Block all SCSI devices' I/Os on the host */
7098 lpfc_scsi_dev_block(phba);
7099 /* Clean up all driver's outstanding SCSI I/Os */
7100 lpfc_sli_flush_fcp_rings(phba);
7101 return PCI_ERS_RESULT_DISCONNECT;
7102 }
7103
7104 pci_disable_device(pdev);
7105 /*
7106 * There may be I/Os dropped by the firmware.
7107 * Error iocb (I/O) on txcmplq and let the SCSI layer
7108 * retry it after re-establishing link.
7109 */
7110 pring = &psli->ring[psli->fcp_ring];
7111 lpfc_sli_abort_iocb_ring(phba, pring);
7112
7113 /* Disable interrupt */
7114 lpfc_sli_disable_intr(phba);
7115
7116 /* Request a slot reset. */
7117 return PCI_ERS_RESULT_NEED_RESET;
7118}
7119
7120/**
7121 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
7122 * @pdev: pointer to PCI device.
7123 *
7124 * This routine is called from the PCI subsystem for error handling to
7125 * device with SLI-3 interface spec. This is called after PCI bus has been
7126 * reset to restart the PCI card from scratch, as if from a cold-boot.
7127 * During the PCI subsystem error recovery, after driver returns
7128 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7129 * recovery and then call this routine before calling the .resume method
7130 * to recover the device. This function will initialize the HBA device,
7131 * enable the interrupt, but it will just put the HBA to offline state
7132 * without passing any I/O traffic.
7133 *
7134 * Return codes
7135 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7136 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7137 */
7138static pci_ers_result_t
7139lpfc_io_slot_reset_s3(struct pci_dev *pdev)
7140{
7141 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7142 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7143 struct lpfc_sli *psli = &phba->sli;
7144 uint32_t intr_mode;
7145
7146 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
7147 if (pci_enable_device_mem(pdev)) {
7148 printk(KERN_ERR "lpfc: Cannot re-enable "
7149 "PCI device after reset.\n");
7150 return PCI_ERS_RESULT_DISCONNECT;
7151 }
7152
7153 pci_restore_state(pdev);
7154 if (pdev->is_busmaster)
7155 pci_set_master(pdev);
7156
7157 spin_lock_irq(&phba->hbalock);
7158 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7159 spin_unlock_irq(&phba->hbalock);
7160
7161 /* Configure and enable interrupt */
7162 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7163 if (intr_mode == LPFC_INTR_ERROR) {
7164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7165 "0427 Cannot re-enable interrupt after "
7166 "slot reset.\n");
7167 return PCI_ERS_RESULT_DISCONNECT;
7168 } else
7169 phba->intr_mode = intr_mode;
7170
7171 /* Take device offline; this will perform cleanup */
7172 lpfc_offline(phba);
7173 lpfc_sli_brdrestart(phba);
7174
7175 /* Log the current active interrupt mode */
7176 lpfc_log_intr_mode(phba, phba->intr_mode);
7177
7178 return PCI_ERS_RESULT_RECOVERED;
7179}
7180
7181/**
7182 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
7183 * @pdev: pointer to PCI device
7184 *
7185 * This routine is called from the PCI subsystem for error handling to device
7186 * with SLI-3 interface spec. It is called when kernel error recovery tells
7187 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7188 * error recovery. After this call, traffic can start to flow from this device
7189 * again.
7190 */
7191static void
7192lpfc_io_resume_s3(struct pci_dev *pdev)
7193{
7194 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7195 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3772a991 7196
da0436e9
JS
7197 lpfc_online(phba);
7198}
3772a991 7199
da0436e9
JS
7200/**
7201 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7202 * @phba: pointer to lpfc hba data structure.
7203 *
7204 * returns the number of ELS/CT IOCBs to reserve
7205 **/
7206int
7207lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7208{
7209 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7210
f1126688
JS
7211 if (phba->sli_rev == LPFC_SLI_REV4) {
7212 if (max_xri <= 100)
7213 return 4;
7214 else if (max_xri <= 256)
7215 return 8;
7216 else if (max_xri <= 512)
7217 return 16;
7218 else if (max_xri <= 1024)
7219 return 32;
7220 else
7221 return 48;
7222 } else
7223 return 0;
3772a991
JS
7224}
7225
7226/**
da0436e9 7227 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
3772a991
JS
7228 * @pdev: pointer to PCI device
7229 * @pid: pointer to PCI device identifier
7230 *
da0436e9
JS
7231 * This routine is called from the kernel's PCI subsystem to device with
7232 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
3772a991 7233 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
da0436e9
JS
7234 * information of the device and driver to see if the driver state that it
7235 * can support this kind of device. If the match is successful, the driver
7236 * core invokes this routine. If this routine determines it can claim the HBA,
7237 * it does all the initialization that it needs to do to handle the HBA
7238 * properly.
3772a991
JS
7239 *
7240 * Return code
7241 * 0 - driver can claim the device
7242 * negative value - driver can not claim the device
7243 **/
7244static int __devinit
da0436e9 7245lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
3772a991
JS
7246{
7247 struct lpfc_hba *phba;
7248 struct lpfc_vport *vport = NULL;
7249 int error;
7250 uint32_t cfg_mode, intr_mode;
da0436e9 7251 int mcnt;
3772a991
JS
7252
7253 /* Allocate memory for HBA structure */
7254 phba = lpfc_hba_alloc(pdev);
7255 if (!phba)
7256 return -ENOMEM;
7257
7258 /* Perform generic PCI device enabling operation */
7259 error = lpfc_enable_pci_dev(phba);
7260 if (error) {
7261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7262 "1409 Failed to enable pci device.\n");
3772a991
JS
7263 goto out_free_phba;
7264 }
7265
da0436e9
JS
7266 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7267 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
3772a991
JS
7268 if (error)
7269 goto out_disable_pci_dev;
7270
da0436e9
JS
7271 /* Set up SLI-4 specific device PCI memory space */
7272 error = lpfc_sli4_pci_mem_setup(phba);
3772a991
JS
7273 if (error) {
7274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7275 "1410 Failed to set up pci memory space.\n");
3772a991
JS
7276 goto out_disable_pci_dev;
7277 }
7278
7279 /* Set up phase-1 common device driver resources */
7280 error = lpfc_setup_driver_resource_phase1(phba);
7281 if (error) {
7282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
7283 "1411 Failed to set up driver resource.\n");
7284 goto out_unset_pci_mem_s4;
3772a991
JS
7285 }
7286
da0436e9
JS
7287 /* Set up SLI-4 Specific device driver resources */
7288 error = lpfc_sli4_driver_resource_setup(phba);
3772a991
JS
7289 if (error) {
7290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
7291 "1412 Failed to set up driver resource.\n");
7292 goto out_unset_pci_mem_s4;
3772a991
JS
7293 }
7294
7295 /* Initialize and populate the iocb list per host */
da0436e9
JS
7296 error = lpfc_init_iocb_list(phba,
7297 phba->sli4_hba.max_cfg_param.max_xri);
3772a991
JS
7298 if (error) {
7299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
7300 "1413 Failed to initialize iocb list.\n");
7301 goto out_unset_driver_resource_s4;
3772a991
JS
7302 }
7303
7304 /* Set up common device driver resources */
7305 error = lpfc_setup_driver_resource_phase2(phba);
7306 if (error) {
7307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7308 "1414 Failed to set up driver resource.\n");
3772a991
JS
7309 goto out_free_iocb_list;
7310 }
7311
7312 /* Create SCSI host to the physical port */
7313 error = lpfc_create_shost(phba);
7314 if (error) {
7315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7316 "1415 Failed to create scsi host.\n");
3772a991
JS
7317 goto out_unset_driver_resource;
7318 }
9399627f 7319
5b75da2f 7320 /* Configure sysfs attributes */
3772a991
JS
7321 vport = phba->pport;
7322 error = lpfc_alloc_sysfs_attr(vport);
7323 if (error) {
9399627f 7324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7325 "1416 Failed to allocate sysfs attr\n");
3772a991 7326 goto out_destroy_shost;
98c9ea5c 7327 }
875fbdfe 7328
3772a991 7329 /* Now, trying to enable interrupt and bring up the device */
5b75da2f
JS
7330 cfg_mode = phba->cfg_use_msi;
7331 while (true) {
3772a991
JS
7332 /* Put device to a known state before enabling interrupt */
7333 lpfc_stop_port(phba);
5b75da2f 7334 /* Configure and enable interrupt */
da0436e9 7335 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
5b75da2f
JS
7336 if (intr_mode == LPFC_INTR_ERROR) {
7337 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7338 "0426 Failed to enable interrupt.\n");
3772a991 7339 error = -ENODEV;
5b75da2f
JS
7340 goto out_free_sysfs_attr;
7341 }
da0436e9
JS
7342 /* Set up SLI-4 HBA */
7343 if (lpfc_sli4_hba_setup(phba)) {
5b75da2f 7344 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7345 "1421 Failed to set up hba\n");
5b75da2f 7346 error = -ENODEV;
da0436e9 7347 goto out_disable_intr;
5b75da2f
JS
7348 }
7349
da0436e9
JS
7350 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7351 if (intr_mode != 0)
7352 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7353 LPFC_ACT_INTR_CNT);
7354
7355 /* Check active interrupts received only for MSI/MSI-X */
3772a991 7356 if (intr_mode == 0 ||
da0436e9 7357 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
5b75da2f
JS
7358 /* Log the current active interrupt mode */
7359 phba->intr_mode = intr_mode;
7360 lpfc_log_intr_mode(phba, intr_mode);
7361 break;
5b75da2f 7362 }
da0436e9
JS
7363 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7364 "0451 Configure interrupt mode (%d) "
7365 "failed active interrupt test.\n",
7366 intr_mode);
7367 /* Unset the preivous SLI-4 HBA setup */
7368 lpfc_sli4_unset_hba(phba);
7369 /* Try next level of interrupt mode */
7370 cfg_mode = --intr_mode;
98c9ea5c 7371 }
858c9f6c 7372
3772a991
JS
7373 /* Perform post initialization setup */
7374 lpfc_post_init_setup(phba);
dea3101e 7375
1c6834a7
JS
7376 /* Check if there are static vports to be created. */
7377 lpfc_create_static_vport(phba);
7378
dea3101e
JB
7379 return 0;
7380
da0436e9
JS
7381out_disable_intr:
7382 lpfc_sli4_disable_intr(phba);
5b75da2f
JS
7383out_free_sysfs_attr:
7384 lpfc_free_sysfs_attr(vport);
3772a991
JS
7385out_destroy_shost:
7386 lpfc_destroy_shost(phba);
7387out_unset_driver_resource:
7388 lpfc_unset_driver_resource_phase2(phba);
7389out_free_iocb_list:
7390 lpfc_free_iocb_list(phba);
da0436e9
JS
7391out_unset_driver_resource_s4:
7392 lpfc_sli4_driver_resource_unset(phba);
7393out_unset_pci_mem_s4:
7394 lpfc_sli4_pci_mem_unset(phba);
3772a991
JS
7395out_disable_pci_dev:
7396 lpfc_disable_pci_dev(phba);
2e0fef85 7397out_free_phba:
3772a991 7398 lpfc_hba_free(phba);
dea3101e
JB
7399 return error;
7400}
7401
e59058c4 7402/**
da0436e9 7403 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
e59058c4
JS
7404 * @pdev: pointer to PCI device
7405 *
da0436e9
JS
7406 * This routine is called from the kernel's PCI subsystem to device with
7407 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
3772a991
JS
7408 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7409 * device to be removed from the PCI subsystem properly.
e59058c4 7410 **/
dea3101e 7411static void __devexit
da0436e9 7412lpfc_pci_remove_one_s4(struct pci_dev *pdev)
dea3101e 7413{
da0436e9 7414 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2e0fef85 7415 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
eada272d 7416 struct lpfc_vport **vports;
da0436e9 7417 struct lpfc_hba *phba = vport->phba;
eada272d 7418 int i;
8a4df120 7419
da0436e9 7420 /* Mark the device unloading flag */
549e55cd 7421 spin_lock_irq(&phba->hbalock);
51ef4c26 7422 vport->load_flag |= FC_UNLOADING;
549e55cd 7423 spin_unlock_irq(&phba->hbalock);
2e0fef85 7424
da0436e9 7425 /* Free the HBA sysfs attributes */
858c9f6c
JS
7426 lpfc_free_sysfs_attr(vport);
7427
eada272d
JS
7428 /* Release all the vports against this physical port */
7429 vports = lpfc_create_vport_work_array(phba);
7430 if (vports != NULL)
3772a991 7431 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
eada272d
JS
7432 fc_vport_terminate(vports[i]->fc_vport);
7433 lpfc_destroy_vport_work_array(phba, vports);
7434
7435 /* Remove FC host and then SCSI host with the physical port */
858c9f6c
JS
7436 fc_remove_host(shost);
7437 scsi_remove_host(shost);
da0436e9
JS
7438
7439 /* Perform cleanup on the physical port */
87af33fe
JS
7440 lpfc_cleanup(vport);
7441
2e0fef85 7442 /*
da0436e9 7443 * Bring down the SLI Layer. This step disables all interrupts,
2e0fef85 7444 * clears the rings, discards all mailbox commands, and resets
da0436e9 7445 * the HBA FCoE function.
2e0fef85 7446 */
da0436e9
JS
7447 lpfc_debugfs_terminate(vport);
7448 lpfc_sli4_hba_unset(phba);
a257bf90 7449
858c9f6c
JS
7450 spin_lock_irq(&phba->hbalock);
7451 list_del_init(&vport->listentry);
7452 spin_unlock_irq(&phba->hbalock);
7453
da0436e9
JS
7454 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7455 * buffers are released to their corresponding pools here.
2e0fef85
JS
7456 */
7457 lpfc_scsi_free(phba);
da0436e9 7458 lpfc_sli4_driver_resource_unset(phba);
ed957684 7459
da0436e9
JS
7460 /* Unmap adapter Control and Doorbell registers */
7461 lpfc_sli4_pci_mem_unset(phba);
2e0fef85 7462
da0436e9
JS
7463 /* Release PCI resources and disable device's PCI function */
7464 scsi_host_put(shost);
7465 lpfc_disable_pci_dev(phba);
2e0fef85 7466
da0436e9 7467 /* Finally, free the driver's device data structure */
3772a991 7468 lpfc_hba_free(phba);
2e0fef85 7469
da0436e9 7470 return;
dea3101e
JB
7471}
7472
3a55b532 7473/**
da0436e9 7474 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
3a55b532
JS
7475 * @pdev: pointer to PCI device
7476 * @msg: power management message
7477 *
da0436e9
JS
7478 * This routine is called from the kernel's PCI subsystem to support system
7479 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7480 * this method, it quiesces the device by stopping the driver's worker
7481 * thread for the device, turning off device's interrupt and DMA, and bring
7482 * the device offline. Note that as the driver implements the minimum PM
7483 * requirements to a power-aware driver's PM support for suspend/resume -- all
7484 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7485 * method call will be treated as SUSPEND and the driver will fully
7486 * reinitialize its device during resume() method call, the driver will set
7487 * device to PCI_D3hot state in PCI config space instead of setting it
3772a991 7488 * according to the @msg provided by the PM.
3a55b532
JS
7489 *
7490 * Return code
3772a991
JS
7491 * 0 - driver suspended the device
7492 * Error otherwise
3a55b532
JS
7493 **/
7494static int
da0436e9 7495lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
3a55b532
JS
7496{
7497 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7498 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7499
7500 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
da0436e9 7501 "0298 PCI device Power Management suspend.\n");
3a55b532
JS
7502
7503 /* Bring down the device */
7504 lpfc_offline_prep(phba);
7505 lpfc_offline(phba);
7506 kthread_stop(phba->worker_thread);
7507
7508 /* Disable interrupt from device */
da0436e9 7509 lpfc_sli4_disable_intr(phba);
3a55b532
JS
7510
7511 /* Save device state to PCI config space */
7512 pci_save_state(pdev);
7513 pci_set_power_state(pdev, PCI_D3hot);
7514
7515 return 0;
7516}
7517
7518/**
da0436e9 7519 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
3a55b532
JS
7520 * @pdev: pointer to PCI device
7521 *
da0436e9
JS
7522 * This routine is called from the kernel's PCI subsystem to support system
7523 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
7524 * this method, it restores the device's PCI config space state and fully
7525 * reinitializes the device and brings it online. Note that as the driver
7526 * implements the minimum PM requirements to a power-aware driver's PM for
7527 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7528 * to the suspend() method call will be treated as SUSPEND and the driver
7529 * will fully reinitialize its device during resume() method call, the device
7530 * will be set to PCI_D0 directly in PCI config space before restoring the
7531 * state.
3a55b532
JS
7532 *
7533 * Return code
3772a991
JS
7534 * 0 - driver suspended the device
7535 * Error otherwise
3a55b532
JS
7536 **/
7537static int
da0436e9 7538lpfc_pci_resume_one_s4(struct pci_dev *pdev)
3a55b532
JS
7539{
7540 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7541 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
5b75da2f 7542 uint32_t intr_mode;
3a55b532
JS
7543 int error;
7544
7545 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
da0436e9 7546 "0292 PCI device Power Management resume.\n");
3a55b532
JS
7547
7548 /* Restore device state from PCI config space */
7549 pci_set_power_state(pdev, PCI_D0);
7550 pci_restore_state(pdev);
7551 if (pdev->is_busmaster)
7552 pci_set_master(pdev);
7553
da0436e9 7554 /* Startup the kernel thread for this host adapter. */
3a55b532
JS
7555 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7556 "lpfc_worker_%d", phba->brd_no);
7557 if (IS_ERR(phba->worker_thread)) {
7558 error = PTR_ERR(phba->worker_thread);
7559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7560 "0293 PM resume failed to start worker "
3a55b532
JS
7561 "thread: error=x%x.\n", error);
7562 return error;
7563 }
7564
5b75da2f 7565 /* Configure and enable interrupt */
da0436e9 7566 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
5b75da2f 7567 if (intr_mode == LPFC_INTR_ERROR) {
3a55b532 7568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7569 "0294 PM resume Failed to enable interrupt\n");
5b75da2f
JS
7570 return -EIO;
7571 } else
7572 phba->intr_mode = intr_mode;
3a55b532
JS
7573
7574 /* Restart HBA and bring it online */
7575 lpfc_sli_brdrestart(phba);
7576 lpfc_online(phba);
7577
5b75da2f
JS
7578 /* Log the current active interrupt mode */
7579 lpfc_log_intr_mode(phba, phba->intr_mode);
7580
3a55b532
JS
7581 return 0;
7582}
7583
8d63f375 7584/**
da0436e9 7585 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
e59058c4
JS
7586 * @pdev: pointer to PCI device.
7587 * @state: the current PCI connection state.
8d63f375 7588 *
da0436e9
JS
7589 * This routine is called from the PCI subsystem for error handling to device
7590 * with SLI-4 interface spec. This function is called by the PCI subsystem
7591 * after a PCI bus error affecting this device has been detected. When this
7592 * function is invoked, it will need to stop all the I/Os and interrupt(s)
7593 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
7594 * for the PCI subsystem to perform proper recovery as desired.
e59058c4
JS
7595 *
7596 * Return codes
3772a991
JS
7597 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7598 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
e59058c4 7599 **/
3772a991 7600static pci_ers_result_t
da0436e9 7601lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8d63f375 7602{
8d63f375
LV
7603 return PCI_ERS_RESULT_NEED_RESET;
7604}
7605
7606/**
da0436e9 7607 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
e59058c4
JS
7608 * @pdev: pointer to PCI device.
7609 *
da0436e9
JS
7610 * This routine is called from the PCI subsystem for error handling to device
7611 * with SLI-4 interface spec. It is called after PCI bus has been reset to
7612 * restart the PCI card from scratch, as if from a cold-boot. During the
7613 * PCI subsystem error recovery, after the driver returns
3772a991 7614 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
da0436e9
JS
7615 * recovery and then call this routine before calling the .resume method to
7616 * recover the device. This function will initialize the HBA device, enable
7617 * the interrupt, but it will just put the HBA to offline state without
7618 * passing any I/O traffic.
8d63f375 7619 *
e59058c4 7620 * Return codes
3772a991
JS
7621 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7622 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8d63f375 7623 */
3772a991 7624static pci_ers_result_t
da0436e9 7625lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8d63f375 7626{
8d63f375
LV
7627 return PCI_ERS_RESULT_RECOVERED;
7628}
7629
7630/**
da0436e9 7631 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
e59058c4 7632 * @pdev: pointer to PCI device
8d63f375 7633 *
3772a991 7634 * This routine is called from the PCI subsystem for error handling to device
da0436e9 7635 * with SLI-4 interface spec. It is called when kernel error recovery tells
3772a991
JS
7636 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7637 * error recovery. After this call, traffic can start to flow from this device
7638 * again.
da0436e9 7639 **/
3772a991 7640static void
da0436e9 7641lpfc_io_resume_s4(struct pci_dev *pdev)
8d63f375 7642{
da0436e9 7643 return;
8d63f375
LV
7644}
7645
3772a991
JS
7646/**
7647 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
7648 * @pdev: pointer to PCI device
7649 * @pid: pointer to PCI device identifier
7650 *
7651 * This routine is to be registered to the kernel's PCI subsystem. When an
7652 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
7653 * at PCI device-specific information of the device and driver to see if the
7654 * driver state that it can support this kind of device. If the match is
7655 * successful, the driver core invokes this routine. This routine dispatches
7656 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
7657 * do all the initialization that it needs to do to handle the HBA device
7658 * properly.
7659 *
7660 * Return code
7661 * 0 - driver can claim the device
7662 * negative value - driver can not claim the device
7663 **/
7664static int __devinit
7665lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
7666{
7667 int rc;
8fa38513 7668 struct lpfc_sli_intf intf;
3772a991 7669
8fa38513 7670 if (pci_read_config_dword(pdev, LPFC_SLIREV_CONF_WORD, &intf.word0))
3772a991
JS
7671 return -ENODEV;
7672
8fa38513
JS
7673 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
7674 (bf_get(lpfc_sli_intf_rev, &intf) == LPFC_SLIREV_CONF_SLI4))
da0436e9 7675 rc = lpfc_pci_probe_one_s4(pdev, pid);
8fa38513 7676 else
3772a991 7677 rc = lpfc_pci_probe_one_s3(pdev, pid);
8fa38513 7678
3772a991
JS
7679 return rc;
7680}
7681
7682/**
7683 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
7684 * @pdev: pointer to PCI device
7685 *
7686 * This routine is to be registered to the kernel's PCI subsystem. When an
7687 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
7688 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
7689 * remove routine, which will perform all the necessary cleanup for the
7690 * device to be removed from the PCI subsystem properly.
7691 **/
7692static void __devexit
7693lpfc_pci_remove_one(struct pci_dev *pdev)
7694{
7695 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7696 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7697
7698 switch (phba->pci_dev_grp) {
7699 case LPFC_PCI_DEV_LP:
7700 lpfc_pci_remove_one_s3(pdev);
7701 break;
da0436e9
JS
7702 case LPFC_PCI_DEV_OC:
7703 lpfc_pci_remove_one_s4(pdev);
7704 break;
3772a991
JS
7705 default:
7706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7707 "1424 Invalid PCI device group: 0x%x\n",
7708 phba->pci_dev_grp);
7709 break;
7710 }
7711 return;
7712}
7713
7714/**
7715 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
7716 * @pdev: pointer to PCI device
7717 * @msg: power management message
7718 *
7719 * This routine is to be registered to the kernel's PCI subsystem to support
7720 * system Power Management (PM). When PM invokes this method, it dispatches
7721 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
7722 * suspend the device.
7723 *
7724 * Return code
7725 * 0 - driver suspended the device
7726 * Error otherwise
7727 **/
7728static int
7729lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
7730{
7731 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7732 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7733 int rc = -ENODEV;
7734
7735 switch (phba->pci_dev_grp) {
7736 case LPFC_PCI_DEV_LP:
7737 rc = lpfc_pci_suspend_one_s3(pdev, msg);
7738 break;
da0436e9
JS
7739 case LPFC_PCI_DEV_OC:
7740 rc = lpfc_pci_suspend_one_s4(pdev, msg);
7741 break;
3772a991
JS
7742 default:
7743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7744 "1425 Invalid PCI device group: 0x%x\n",
7745 phba->pci_dev_grp);
7746 break;
7747 }
7748 return rc;
7749}
7750
7751/**
7752 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
7753 * @pdev: pointer to PCI device
7754 *
7755 * This routine is to be registered to the kernel's PCI subsystem to support
7756 * system Power Management (PM). When PM invokes this method, it dispatches
7757 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
7758 * resume the device.
7759 *
7760 * Return code
7761 * 0 - driver suspended the device
7762 * Error otherwise
7763 **/
7764static int
7765lpfc_pci_resume_one(struct pci_dev *pdev)
7766{
7767 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7768 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7769 int rc = -ENODEV;
7770
7771 switch (phba->pci_dev_grp) {
7772 case LPFC_PCI_DEV_LP:
7773 rc = lpfc_pci_resume_one_s3(pdev);
7774 break;
da0436e9
JS
7775 case LPFC_PCI_DEV_OC:
7776 rc = lpfc_pci_resume_one_s4(pdev);
7777 break;
3772a991
JS
7778 default:
7779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7780 "1426 Invalid PCI device group: 0x%x\n",
7781 phba->pci_dev_grp);
7782 break;
7783 }
7784 return rc;
7785}
7786
7787/**
7788 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
7789 * @pdev: pointer to PCI device.
7790 * @state: the current PCI connection state.
7791 *
7792 * This routine is registered to the PCI subsystem for error handling. This
7793 * function is called by the PCI subsystem after a PCI bus error affecting
7794 * this device has been detected. When this routine is invoked, it dispatches
7795 * the action to the proper SLI-3 or SLI-4 device error detected handling
7796 * routine, which will perform the proper error detected operation.
7797 *
7798 * Return codes
7799 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7800 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7801 **/
7802static pci_ers_result_t
7803lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7804{
7805 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7806 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7807 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7808
7809 switch (phba->pci_dev_grp) {
7810 case LPFC_PCI_DEV_LP:
7811 rc = lpfc_io_error_detected_s3(pdev, state);
7812 break;
da0436e9
JS
7813 case LPFC_PCI_DEV_OC:
7814 rc = lpfc_io_error_detected_s4(pdev, state);
7815 break;
3772a991
JS
7816 default:
7817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7818 "1427 Invalid PCI device group: 0x%x\n",
7819 phba->pci_dev_grp);
7820 break;
7821 }
7822 return rc;
7823}
7824
7825/**
7826 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
7827 * @pdev: pointer to PCI device.
7828 *
7829 * This routine is registered to the PCI subsystem for error handling. This
7830 * function is called after PCI bus has been reset to restart the PCI card
7831 * from scratch, as if from a cold-boot. When this routine is invoked, it
7832 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
7833 * routine, which will perform the proper device reset.
7834 *
7835 * Return codes
7836 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7837 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7838 **/
7839static pci_ers_result_t
7840lpfc_io_slot_reset(struct pci_dev *pdev)
7841{
7842 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7843 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7844 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7845
7846 switch (phba->pci_dev_grp) {
7847 case LPFC_PCI_DEV_LP:
7848 rc = lpfc_io_slot_reset_s3(pdev);
7849 break;
da0436e9
JS
7850 case LPFC_PCI_DEV_OC:
7851 rc = lpfc_io_slot_reset_s4(pdev);
7852 break;
3772a991
JS
7853 default:
7854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7855 "1428 Invalid PCI device group: 0x%x\n",
7856 phba->pci_dev_grp);
7857 break;
7858 }
7859 return rc;
7860}
7861
7862/**
7863 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
7864 * @pdev: pointer to PCI device
7865 *
7866 * This routine is registered to the PCI subsystem for error handling. It
7867 * is called when kernel error recovery tells the lpfc driver that it is
7868 * OK to resume normal PCI operation after PCI bus error recovery. When
7869 * this routine is invoked, it dispatches the action to the proper SLI-3
7870 * or SLI-4 device io_resume routine, which will resume the device operation.
7871 **/
7872static void
7873lpfc_io_resume(struct pci_dev *pdev)
7874{
7875 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7876 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7877
7878 switch (phba->pci_dev_grp) {
7879 case LPFC_PCI_DEV_LP:
7880 lpfc_io_resume_s3(pdev);
7881 break;
da0436e9
JS
7882 case LPFC_PCI_DEV_OC:
7883 lpfc_io_resume_s4(pdev);
7884 break;
3772a991
JS
7885 default:
7886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7887 "1429 Invalid PCI device group: 0x%x\n",
7888 phba->pci_dev_grp);
7889 break;
7890 }
7891 return;
7892}
7893
dea3101e
JB
7894static struct pci_device_id lpfc_id_table[] = {
7895 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
7896 PCI_ANY_ID, PCI_ANY_ID, },
06325e74
JSEC
7897 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
7898 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
7899 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
7900 PCI_ANY_ID, PCI_ANY_ID, },
7901 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
7902 PCI_ANY_ID, PCI_ANY_ID, },
7903 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
7904 PCI_ANY_ID, PCI_ANY_ID, },
7905 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
7906 PCI_ANY_ID, PCI_ANY_ID, },
7907 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
7908 PCI_ANY_ID, PCI_ANY_ID, },
7909 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
7910 PCI_ANY_ID, PCI_ANY_ID, },
7911 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
7912 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
7913 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
7914 PCI_ANY_ID, PCI_ANY_ID, },
7915 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
7916 PCI_ANY_ID, PCI_ANY_ID, },
7917 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
7918 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
7919 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
7920 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
7921 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
7922 PCI_ANY_ID, PCI_ANY_ID, },
7923 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
7924 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
7925 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
7926 PCI_ANY_ID, PCI_ANY_ID, },
7927 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
7928 PCI_ANY_ID, PCI_ANY_ID, },
7929 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
7930 PCI_ANY_ID, PCI_ANY_ID, },
84774a4d
JS
7931 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
7932 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
7933 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
7934 PCI_ANY_ID, PCI_ANY_ID, },
7935 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
7936 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
7937 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
7938 PCI_ANY_ID, PCI_ANY_ID, },
7939 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
7940 PCI_ANY_ID, PCI_ANY_ID, },
7941 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
7942 PCI_ANY_ID, PCI_ANY_ID, },
7943 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
7944 PCI_ANY_ID, PCI_ANY_ID, },
7945 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
7946 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
7947 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
7948 PCI_ANY_ID, PCI_ANY_ID, },
7949 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
7950 PCI_ANY_ID, PCI_ANY_ID, },
b87eab38
JS
7951 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
7952 PCI_ANY_ID, PCI_ANY_ID, },
7953 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
7954 PCI_ANY_ID, PCI_ANY_ID, },
7955 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
7956 PCI_ANY_ID, PCI_ANY_ID, },
7957 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
7958 PCI_ANY_ID, PCI_ANY_ID, },
7959 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
7960 PCI_ANY_ID, PCI_ANY_ID, },
7961 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
7962 PCI_ANY_ID, PCI_ANY_ID, },
84774a4d
JS
7963 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
7964 PCI_ANY_ID, PCI_ANY_ID, },
7965 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
7966 PCI_ANY_ID, PCI_ANY_ID, },
7967 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
7968 PCI_ANY_ID, PCI_ANY_ID, },
3772a991
JS
7969 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
7970 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
7971 { 0 }
7972};
7973
7974MODULE_DEVICE_TABLE(pci, lpfc_id_table);
7975
8d63f375
LV
7976static struct pci_error_handlers lpfc_err_handler = {
7977 .error_detected = lpfc_io_error_detected,
7978 .slot_reset = lpfc_io_slot_reset,
7979 .resume = lpfc_io_resume,
7980};
7981
dea3101e
JB
7982static struct pci_driver lpfc_driver = {
7983 .name = LPFC_DRIVER_NAME,
7984 .id_table = lpfc_id_table,
7985 .probe = lpfc_pci_probe_one,
7986 .remove = __devexit_p(lpfc_pci_remove_one),
3a55b532 7987 .suspend = lpfc_pci_suspend_one,
3772a991 7988 .resume = lpfc_pci_resume_one,
2e0fef85 7989 .err_handler = &lpfc_err_handler,
dea3101e
JB
7990};
7991
e59058c4 7992/**
3621a710 7993 * lpfc_init - lpfc module initialization routine
e59058c4
JS
7994 *
7995 * This routine is to be invoked when the lpfc module is loaded into the
7996 * kernel. The special kernel macro module_init() is used to indicate the
7997 * role of this routine to the kernel as lpfc module entry point.
7998 *
7999 * Return codes
8000 * 0 - successful
8001 * -ENOMEM - FC attach transport failed
8002 * all others - failed
8003 */
dea3101e
JB
8004static int __init
8005lpfc_init(void)
8006{
8007 int error = 0;
8008
8009 printk(LPFC_MODULE_DESC "\n");
c44ce173 8010 printk(LPFC_COPYRIGHT "\n");
dea3101e 8011
7ee5d43e
JS
8012 if (lpfc_enable_npiv) {
8013 lpfc_transport_functions.vport_create = lpfc_vport_create;
8014 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8015 }
dea3101e
JB
8016 lpfc_transport_template =
8017 fc_attach_transport(&lpfc_transport_functions);
7ee5d43e 8018 if (lpfc_transport_template == NULL)
dea3101e 8019 return -ENOMEM;
7ee5d43e 8020 if (lpfc_enable_npiv) {
7ee5d43e 8021 lpfc_vport_transport_template =
98c9ea5c
JS
8022 fc_attach_transport(&lpfc_vport_transport_functions);
8023 if (lpfc_vport_transport_template == NULL) {
8024 fc_release_transport(lpfc_transport_template);
7ee5d43e 8025 return -ENOMEM;
98c9ea5c 8026 }
7ee5d43e 8027 }
dea3101e 8028 error = pci_register_driver(&lpfc_driver);
92d7f7b0 8029 if (error) {
dea3101e 8030 fc_release_transport(lpfc_transport_template);
d7c255b2
JS
8031 if (lpfc_enable_npiv)
8032 fc_release_transport(lpfc_vport_transport_template);
92d7f7b0 8033 }
dea3101e
JB
8034
8035 return error;
8036}
8037
e59058c4 8038/**
3621a710 8039 * lpfc_exit - lpfc module removal routine
e59058c4
JS
8040 *
8041 * This routine is invoked when the lpfc module is removed from the kernel.
8042 * The special kernel macro module_exit() is used to indicate the role of
8043 * this routine to the kernel as lpfc module exit point.
8044 */
dea3101e
JB
8045static void __exit
8046lpfc_exit(void)
8047{
8048 pci_unregister_driver(&lpfc_driver);
8049 fc_release_transport(lpfc_transport_template);
7ee5d43e
JS
8050 if (lpfc_enable_npiv)
8051 fc_release_transport(lpfc_vport_transport_template);
81301a9b
JS
8052 if (_dump_buf_data) {
8053 printk(KERN_ERR "BLKGRD freeing %lu pages for _dump_buf_data "
8054 "at 0x%p\n",
8055 (1L << _dump_buf_data_order), _dump_buf_data);
8056 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8057 }
8058
8059 if (_dump_buf_dif) {
8060 printk(KERN_ERR "BLKGRD freeing %lu pages for _dump_buf_dif "
8061 "at 0x%p\n",
8062 (1L << _dump_buf_dif_order), _dump_buf_dif);
8063 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8064 }
dea3101e
JB
8065}
8066
8067module_init(lpfc_init);
8068module_exit(lpfc_exit);
8069MODULE_LICENSE("GPL");
8070MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8071MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8072MODULE_VERSION("0:" LPFC_DRIVER_VERSION);