Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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. *
d4379acd 4 * Copyright (C) 2004-2012 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>
acf3368f 27#include <linux/module.h>
dea3101e
JB
28#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
92d7f7b0 31#include <linux/ctype.h>
0d878419 32#include <linux/aer.h>
5a0e3ad6 33#include <linux/slab.h>
52d52440 34#include <linux/firmware.h>
3ef6d24c 35#include <linux/miscdevice.h>
7bb03bbf 36#include <linux/percpu.h>
dea3101e 37
91886523 38#include <scsi/scsi.h>
dea3101e
JB
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_transport_fc.h>
42
da0436e9 43#include "lpfc_hw4.h"
dea3101e
JB
44#include "lpfc_hw.h"
45#include "lpfc_sli.h"
da0436e9 46#include "lpfc_sli4.h"
ea2151b4 47#include "lpfc_nl.h"
dea3101e
JB
48#include "lpfc_disc.h"
49#include "lpfc_scsi.h"
50#include "lpfc.h"
51#include "lpfc_logmsg.h"
52#include "lpfc_crtn.h"
92d7f7b0 53#include "lpfc_vport.h"
dea3101e
JB
54#include "lpfc_version.h"
55
81301a9b
JS
56char *_dump_buf_data;
57unsigned long _dump_buf_data_order;
58char *_dump_buf_dif;
59unsigned long _dump_buf_dif_order;
60spinlock_t _dump_buf_lock;
61
7bb03bbf
JS
62/* Used when mapping IRQ vectors in a driver centric manner */
63uint16_t lpfc_used_cpu[LPFC_MAX_CPU];
64
dea3101e
JB
65static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
66static int lpfc_post_rcv_buf(struct lpfc_hba *);
5350d872 67static int lpfc_sli4_queue_verify(struct lpfc_hba *);
da0436e9
JS
68static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
69static int lpfc_setup_endian_order(struct lpfc_hba *);
da0436e9 70static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
8a9d2e80
JS
71static void lpfc_free_els_sgl_list(struct lpfc_hba *);
72static void lpfc_init_sgl_list(struct lpfc_hba *);
da0436e9
JS
73static int lpfc_init_active_sgl_array(struct lpfc_hba *);
74static void lpfc_free_active_sgl(struct lpfc_hba *);
75static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
76static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
77static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
78static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
79static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
618a5230
JS
80static void lpfc_sli4_disable_intr(struct lpfc_hba *);
81static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
dea3101e
JB
82
83static struct scsi_transport_template *lpfc_transport_template = NULL;
92d7f7b0 84static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea3101e
JB
85static DEFINE_IDR(lpfc_hba_index);
86
e59058c4 87/**
3621a710 88 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
e59058c4
JS
89 * @phba: pointer to lpfc hba data structure.
90 *
91 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
92 * mailbox command. It retrieves the revision information from the HBA and
93 * collects the Vital Product Data (VPD) about the HBA for preparing the
94 * configuration of the HBA.
95 *
96 * Return codes:
97 * 0 - success.
98 * -ERESTART - requests the SLI layer to reset the HBA and try again.
99 * Any other value - indicates an error.
100 **/
dea3101e 101int
2e0fef85 102lpfc_config_port_prep(struct lpfc_hba *phba)
dea3101e
JB
103{
104 lpfc_vpd_t *vp = &phba->vpd;
105 int i = 0, rc;
106 LPFC_MBOXQ_t *pmb;
107 MAILBOX_t *mb;
108 char *lpfc_vpd_data = NULL;
109 uint16_t offset = 0;
110 static char licensed[56] =
111 "key unlock for use with gnu public licensed code only\0";
65a29c16 112 static int init_key = 1;
dea3101e
JB
113
114 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
115 if (!pmb) {
2e0fef85 116 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
117 return -ENOMEM;
118 }
119
04c68496 120 mb = &pmb->u.mb;
2e0fef85 121 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
122
123 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
65a29c16
JS
124 if (init_key) {
125 uint32_t *ptext = (uint32_t *) licensed;
dea3101e 126
65a29c16
JS
127 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
128 *ptext = cpu_to_be32(*ptext);
129 init_key = 0;
130 }
dea3101e
JB
131
132 lpfc_read_nv(phba, pmb);
133 memset((char*)mb->un.varRDnvp.rsvd3, 0,
134 sizeof (mb->un.varRDnvp.rsvd3));
135 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
136 sizeof (licensed));
137
138 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
139
140 if (rc != MBX_SUCCESS) {
ed957684 141 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
e8b62011 142 "0324 Config Port initialization "
dea3101e
JB
143 "error, mbxCmd x%x READ_NVPARM, "
144 "mbxStatus x%x\n",
dea3101e
JB
145 mb->mbxCommand, mb->mbxStatus);
146 mempool_free(pmb, phba->mbox_mem_pool);
147 return -ERESTART;
148 }
149 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
2e0fef85
JS
150 sizeof(phba->wwnn));
151 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
152 sizeof(phba->wwpn));
dea3101e
JB
153 }
154
92d7f7b0
JS
155 phba->sli3_options = 0x0;
156
dea3101e
JB
157 /* Setup and issue mailbox READ REV command */
158 lpfc_read_rev(phba, pmb);
159 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
160 if (rc != MBX_SUCCESS) {
ed957684 161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 162 "0439 Adapter failed to init, mbxCmd x%x "
dea3101e 163 "READ_REV, mbxStatus x%x\n",
dea3101e
JB
164 mb->mbxCommand, mb->mbxStatus);
165 mempool_free( pmb, phba->mbox_mem_pool);
166 return -ERESTART;
167 }
168
92d7f7b0 169
1de933f3
JSEC
170 /*
171 * The value of rr must be 1 since the driver set the cv field to 1.
172 * This setting requires the FW to set all revision fields.
dea3101e 173 */
1de933f3 174 if (mb->un.varRdRev.rr == 0) {
dea3101e 175 vp->rev.rBit = 0;
1de933f3 176 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011
JS
177 "0440 Adapter failed to init, READ_REV has "
178 "missing revision information.\n");
dea3101e
JB
179 mempool_free(pmb, phba->mbox_mem_pool);
180 return -ERESTART;
dea3101e
JB
181 }
182
495a714c
JS
183 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
184 mempool_free(pmb, phba->mbox_mem_pool);
ed957684 185 return -EINVAL;
495a714c 186 }
ed957684 187
dea3101e 188 /* Save information as VPD data */
1de933f3 189 vp->rev.rBit = 1;
92d7f7b0 190 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
1de933f3
JSEC
191 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
192 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
193 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
194 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea3101e
JB
195 vp->rev.biuRev = mb->un.varRdRev.biuRev;
196 vp->rev.smRev = mb->un.varRdRev.smRev;
197 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
198 vp->rev.endecRev = mb->un.varRdRev.endecRev;
199 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
200 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
201 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
202 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
203 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
204 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
205
92d7f7b0
JS
206 /* If the sli feature level is less then 9, we must
207 * tear down all RPIs and VPIs on link down if NPIV
208 * is enabled.
209 */
210 if (vp->rev.feaLevelHigh < 9)
211 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
212
dea3101e
JB
213 if (lpfc_is_LC_HBA(phba->pcidev->device))
214 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
215 sizeof (phba->RandomData));
216
dea3101e 217 /* Get adapter VPD information */
dea3101e
JB
218 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
219 if (!lpfc_vpd_data)
d7c255b2 220 goto out_free_mbox;
dea3101e 221 do {
a0c87cbd 222 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea3101e
JB
223 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
224
225 if (rc != MBX_SUCCESS) {
226 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 227 "0441 VPD not present on adapter, "
dea3101e 228 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea3101e 229 mb->mbxCommand, mb->mbxStatus);
74b72a59 230 mb->un.varDmp.word_cnt = 0;
dea3101e 231 }
04c68496
JS
232 /* dump mem may return a zero when finished or we got a
233 * mailbox error, either way we are done.
234 */
235 if (mb->un.varDmp.word_cnt == 0)
236 break;
74b72a59
JW
237 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
238 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
d7c255b2
JS
239 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
240 lpfc_vpd_data + offset,
92d7f7b0 241 mb->un.varDmp.word_cnt);
dea3101e 242 offset += mb->un.varDmp.word_cnt;
74b72a59
JW
243 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
244 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea3101e
JB
245
246 kfree(lpfc_vpd_data);
dea3101e
JB
247out_free_mbox:
248 mempool_free(pmb, phba->mbox_mem_pool);
249 return 0;
250}
251
e59058c4 252/**
3621a710 253 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
e59058c4
JS
254 * @phba: pointer to lpfc hba data structure.
255 * @pmboxq: pointer to the driver internal queue element for mailbox command.
256 *
257 * This is the completion handler for driver's configuring asynchronous event
258 * mailbox command to the device. If the mailbox command returns successfully,
259 * it will set internal async event support flag to 1; otherwise, it will
260 * set internal async event support flag to 0.
261 **/
57127f15
JS
262static void
263lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
264{
04c68496 265 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
57127f15
JS
266 phba->temp_sensor_support = 1;
267 else
268 phba->temp_sensor_support = 0;
269 mempool_free(pmboxq, phba->mbox_mem_pool);
270 return;
271}
272
97207482 273/**
3621a710 274 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
97207482
JS
275 * @phba: pointer to lpfc hba data structure.
276 * @pmboxq: pointer to the driver internal queue element for mailbox command.
277 *
278 * This is the completion handler for dump mailbox command for getting
279 * wake up parameters. When this command complete, the response contain
280 * Option rom version of the HBA. This function translate the version number
281 * into a human readable string and store it in OptionROMVersion.
282 **/
283static void
284lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
285{
286 struct prog_id *prg;
287 uint32_t prog_id_word;
288 char dist = ' ';
289 /* character array used for decoding dist type. */
290 char dist_char[] = "nabx";
291
04c68496 292 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
9f1e1b50 293 mempool_free(pmboxq, phba->mbox_mem_pool);
97207482 294 return;
9f1e1b50 295 }
97207482
JS
296
297 prg = (struct prog_id *) &prog_id_word;
298
299 /* word 7 contain option rom version */
04c68496 300 prog_id_word = pmboxq->u.mb.un.varWords[7];
97207482
JS
301
302 /* Decode the Option rom version word to a readable string */
303 if (prg->dist < 4)
304 dist = dist_char[prg->dist];
305
306 if ((prg->dist == 3) && (prg->num == 0))
307 sprintf(phba->OptionROMVersion, "%d.%d%d",
308 prg->ver, prg->rev, prg->lev);
309 else
310 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
311 prg->ver, prg->rev, prg->lev,
312 dist, prg->num);
9f1e1b50 313 mempool_free(pmboxq, phba->mbox_mem_pool);
97207482
JS
314 return;
315}
316
0558056c
JS
317/**
318 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
319 * cfg_soft_wwnn, cfg_soft_wwpn
320 * @vport: pointer to lpfc vport data structure.
321 *
322 *
323 * Return codes
324 * None.
325 **/
326void
327lpfc_update_vport_wwn(struct lpfc_vport *vport)
328{
329 /* If the soft name exists then update it using the service params */
330 if (vport->phba->cfg_soft_wwnn)
331 u64_to_wwn(vport->phba->cfg_soft_wwnn,
332 vport->fc_sparam.nodeName.u.wwn);
333 if (vport->phba->cfg_soft_wwpn)
334 u64_to_wwn(vport->phba->cfg_soft_wwpn,
335 vport->fc_sparam.portName.u.wwn);
336
337 /*
338 * If the name is empty or there exists a soft name
339 * then copy the service params name, otherwise use the fc name
340 */
341 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
342 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
343 sizeof(struct lpfc_name));
344 else
345 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
346 sizeof(struct lpfc_name));
347
348 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
349 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
350 sizeof(struct lpfc_name));
351 else
352 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
353 sizeof(struct lpfc_name));
354}
355
e59058c4 356/**
3621a710 357 * lpfc_config_port_post - Perform lpfc initialization after config port
e59058c4
JS
358 * @phba: pointer to lpfc hba data structure.
359 *
360 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
361 * command call. It performs all internal resource and state setups on the
362 * port: post IOCB buffers, enable appropriate host interrupt attentions,
363 * ELS ring timers, etc.
364 *
365 * Return codes
366 * 0 - success.
367 * Any other value - error.
368 **/
dea3101e 369int
2e0fef85 370lpfc_config_port_post(struct lpfc_hba *phba)
dea3101e 371{
2e0fef85 372 struct lpfc_vport *vport = phba->pport;
a257bf90 373 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e
JB
374 LPFC_MBOXQ_t *pmb;
375 MAILBOX_t *mb;
376 struct lpfc_dmabuf *mp;
377 struct lpfc_sli *psli = &phba->sli;
378 uint32_t status, timeout;
2e0fef85
JS
379 int i, j;
380 int rc;
dea3101e 381
7af67051
JS
382 spin_lock_irq(&phba->hbalock);
383 /*
384 * If the Config port completed correctly the HBA is not
385 * over heated any more.
386 */
387 if (phba->over_temp_state == HBA_OVER_TEMP)
388 phba->over_temp_state = HBA_NORMAL_TEMP;
389 spin_unlock_irq(&phba->hbalock);
390
dea3101e
JB
391 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
392 if (!pmb) {
2e0fef85 393 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
394 return -ENOMEM;
395 }
04c68496 396 mb = &pmb->u.mb;
dea3101e 397
dea3101e 398 /* Get login parameters for NID. */
9f1177a3
JS
399 rc = lpfc_read_sparam(phba, pmb, 0);
400 if (rc) {
401 mempool_free(pmb, phba->mbox_mem_pool);
402 return -ENOMEM;
403 }
404
ed957684 405 pmb->vport = vport;
dea3101e 406 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
ed957684 407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 408 "0448 Adapter failed init, mbxCmd x%x "
dea3101e 409 "READ_SPARM mbxStatus x%x\n",
dea3101e 410 mb->mbxCommand, mb->mbxStatus);
2e0fef85 411 phba->link_state = LPFC_HBA_ERROR;
dea3101e 412 mp = (struct lpfc_dmabuf *) pmb->context1;
9f1177a3 413 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e
JB
414 lpfc_mbuf_free(phba, mp->virt, mp->phys);
415 kfree(mp);
416 return -EIO;
417 }
418
419 mp = (struct lpfc_dmabuf *) pmb->context1;
420
2e0fef85 421 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea3101e
JB
422 lpfc_mbuf_free(phba, mp->virt, mp->phys);
423 kfree(mp);
424 pmb->context1 = NULL;
0558056c 425 lpfc_update_vport_wwn(vport);
a257bf90
JS
426
427 /* Update the fc_host data structures with new wwn. */
428 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
429 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
21e9a0a5 430 fc_host_max_npiv_vports(shost) = phba->max_vpi;
a257bf90 431
dea3101e
JB
432 /* If no serial number in VPD data, use low 6 bytes of WWNN */
433 /* This should be consolidated into parse_vpd ? - mr */
434 if (phba->SerialNumber[0] == 0) {
435 uint8_t *outptr;
436
2e0fef85 437 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea3101e
JB
438 for (i = 0; i < 12; i++) {
439 status = *outptr++;
440 j = ((status & 0xf0) >> 4);
441 if (j <= 9)
442 phba->SerialNumber[i] =
443 (char)((uint8_t) 0x30 + (uint8_t) j);
444 else
445 phba->SerialNumber[i] =
446 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
447 i++;
448 j = (status & 0xf);
449 if (j <= 9)
450 phba->SerialNumber[i] =
451 (char)((uint8_t) 0x30 + (uint8_t) j);
452 else
453 phba->SerialNumber[i] =
454 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
455 }
456 }
457
dea3101e 458 lpfc_read_config(phba, pmb);
ed957684 459 pmb->vport = vport;
dea3101e 460 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
ed957684 461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 462 "0453 Adapter failed to init, mbxCmd x%x "
dea3101e 463 "READ_CONFIG, mbxStatus x%x\n",
dea3101e 464 mb->mbxCommand, mb->mbxStatus);
2e0fef85 465 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
466 mempool_free( pmb, phba->mbox_mem_pool);
467 return -EIO;
468 }
469
a0c87cbd
JS
470 /* Check if the port is disabled */
471 lpfc_sli_read_link_ste(phba);
472
dea3101e
JB
473 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
474 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
475 phba->cfg_hba_queue_depth =
f1126688
JS
476 (mb->un.varRdConfig.max_xri + 1) -
477 lpfc_sli4_get_els_iocb_cnt(phba);
dea3101e
JB
478
479 phba->lmt = mb->un.varRdConfig.lmt;
74b72a59
JW
480
481 /* Get the default values for Model Name and Description */
482 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
483
2e0fef85 484 phba->link_state = LPFC_LINK_DOWN;
dea3101e 485
0b727fea 486 /* Only process IOCBs on ELS ring till hba_state is READY */
7e56aa25 487 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
a4bc3379 488 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
7e56aa25 489 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea3101e 490 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
7e56aa25 491 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea3101e
JB
492 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
493
494 /* Post receive buffers for desired rings */
ed957684
JS
495 if (phba->sli_rev != 3)
496 lpfc_post_rcv_buf(phba);
dea3101e 497
9399627f
JS
498 /*
499 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
500 */
501 if (phba->intr_type == MSIX) {
502 rc = lpfc_config_msi(phba, pmb);
503 if (rc) {
504 mempool_free(pmb, phba->mbox_mem_pool);
505 return -EIO;
506 }
507 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
508 if (rc != MBX_SUCCESS) {
509 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
510 "0352 Config MSI mailbox command "
511 "failed, mbxCmd x%x, mbxStatus x%x\n",
04c68496
JS
512 pmb->u.mb.mbxCommand,
513 pmb->u.mb.mbxStatus);
9399627f
JS
514 mempool_free(pmb, phba->mbox_mem_pool);
515 return -EIO;
516 }
517 }
518
04c68496 519 spin_lock_irq(&phba->hbalock);
9399627f
JS
520 /* Initialize ERATT handling flag */
521 phba->hba_flag &= ~HBA_ERATT_HANDLED;
522
dea3101e 523 /* Enable appropriate host interrupts */
9940b97b
JS
524 if (lpfc_readl(phba->HCregaddr, &status)) {
525 spin_unlock_irq(&phba->hbalock);
526 return -EIO;
527 }
dea3101e
JB
528 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
529 if (psli->num_rings > 0)
530 status |= HC_R0INT_ENA;
531 if (psli->num_rings > 1)
532 status |= HC_R1INT_ENA;
533 if (psli->num_rings > 2)
534 status |= HC_R2INT_ENA;
535 if (psli->num_rings > 3)
536 status |= HC_R3INT_ENA;
537
875fbdfe
JSEC
538 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
539 (phba->cfg_poll & DISABLE_FCP_RING_INT))
9399627f 540 status &= ~(HC_R0INT_ENA);
875fbdfe 541
dea3101e
JB
542 writel(status, phba->HCregaddr);
543 readl(phba->HCregaddr); /* flush */
2e0fef85 544 spin_unlock_irq(&phba->hbalock);
dea3101e 545
9399627f
JS
546 /* Set up ring-0 (ELS) timer */
547 timeout = phba->fc_ratov * 2;
256ec0d0
JS
548 mod_timer(&vport->els_tmofunc,
549 jiffies + msecs_to_jiffies(1000 * timeout));
9399627f 550 /* Set up heart beat (HB) timer */
256ec0d0
JS
551 mod_timer(&phba->hb_tmofunc,
552 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
858c9f6c
JS
553 phba->hb_outstanding = 0;
554 phba->last_completion_time = jiffies;
9399627f 555 /* Set up error attention (ERATT) polling timer */
256ec0d0
JS
556 mod_timer(&phba->eratt_poll,
557 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
dea3101e 558
a0c87cbd
JS
559 if (phba->hba_flag & LINK_DISABLED) {
560 lpfc_printf_log(phba,
561 KERN_ERR, LOG_INIT,
562 "2598 Adapter Link is disabled.\n");
563 lpfc_down_link(phba, pmb);
564 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
565 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
566 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
567 lpfc_printf_log(phba,
568 KERN_ERR, LOG_INIT,
569 "2599 Adapter failed to issue DOWN_LINK"
570 " mbox command rc 0x%x\n", rc);
571
572 mempool_free(pmb, phba->mbox_mem_pool);
573 return -EIO;
574 }
e40a02c1 575 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
026abb87
JS
576 mempool_free(pmb, phba->mbox_mem_pool);
577 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
578 if (rc)
579 return rc;
dea3101e
JB
580 }
581 /* MBOX buffer will be freed in mbox compl */
57127f15 582 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9f1177a3
JS
583 if (!pmb) {
584 phba->link_state = LPFC_HBA_ERROR;
585 return -ENOMEM;
586 }
587
57127f15
JS
588 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
589 pmb->mbox_cmpl = lpfc_config_async_cmpl;
590 pmb->vport = phba->pport;
591 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea3101e 592
57127f15
JS
593 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
594 lpfc_printf_log(phba,
595 KERN_ERR,
596 LOG_INIT,
597 "0456 Adapter failed to issue "
e4e74273 598 "ASYNCEVT_ENABLE mbox status x%x\n",
57127f15
JS
599 rc);
600 mempool_free(pmb, phba->mbox_mem_pool);
601 }
97207482
JS
602
603 /* Get Option rom version */
604 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9f1177a3
JS
605 if (!pmb) {
606 phba->link_state = LPFC_HBA_ERROR;
607 return -ENOMEM;
608 }
609
97207482
JS
610 lpfc_dump_wakeup_param(phba, pmb);
611 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
612 pmb->vport = phba->pport;
613 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
614
615 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
e4e74273 617 "to get Option ROM version status x%x\n", rc);
97207482
JS
618 mempool_free(pmb, phba->mbox_mem_pool);
619 }
620
d7c255b2 621 return 0;
ce8b3ce5
JS
622}
623
84d1b006
JS
624/**
625 * lpfc_hba_init_link - Initialize the FC link
626 * @phba: pointer to lpfc hba data structure.
6e7288d9 627 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
84d1b006
JS
628 *
629 * This routine will issue the INIT_LINK mailbox command call.
630 * It is available to other drivers through the lpfc_hba data
631 * structure for use as a delayed link up mechanism with the
632 * module parameter lpfc_suppress_link_up.
633 *
634 * Return code
635 * 0 - success
636 * Any other value - error
637 **/
638int
6e7288d9 639lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
1b51197d
JS
640{
641 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
642}
643
644/**
645 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
646 * @phba: pointer to lpfc hba data structure.
647 * @fc_topology: desired fc topology.
648 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
649 *
650 * This routine will issue the INIT_LINK mailbox command call.
651 * It is available to other drivers through the lpfc_hba data
652 * structure for use as a delayed link up mechanism with the
653 * module parameter lpfc_suppress_link_up.
654 *
655 * Return code
656 * 0 - success
657 * Any other value - error
658 **/
659int
660lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
661 uint32_t flag)
84d1b006
JS
662{
663 struct lpfc_vport *vport = phba->pport;
664 LPFC_MBOXQ_t *pmb;
665 MAILBOX_t *mb;
666 int rc;
667
668 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
669 if (!pmb) {
670 phba->link_state = LPFC_HBA_ERROR;
671 return -ENOMEM;
672 }
673 mb = &pmb->u.mb;
674 pmb->vport = vport;
675
026abb87
JS
676 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
677 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
678 !(phba->lmt & LMT_1Gb)) ||
679 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
680 !(phba->lmt & LMT_2Gb)) ||
681 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
682 !(phba->lmt & LMT_4Gb)) ||
683 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
684 !(phba->lmt & LMT_8Gb)) ||
685 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
686 !(phba->lmt & LMT_10Gb)) ||
687 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
688 !(phba->lmt & LMT_16Gb))) {
689 /* Reset link speed to auto */
690 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
691 "1302 Invalid speed for this board:%d "
692 "Reset link speed to auto.\n",
693 phba->cfg_link_speed);
694 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
695 }
1b51197d 696 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
84d1b006 697 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1b51197d
JS
698 if (phba->sli_rev < LPFC_SLI_REV4)
699 lpfc_set_loopback_flag(phba);
6e7288d9 700 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
76a95d75 701 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
84d1b006
JS
702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
703 "0498 Adapter failed to init, mbxCmd x%x "
704 "INIT_LINK, mbxStatus x%x\n",
705 mb->mbxCommand, mb->mbxStatus);
76a95d75
JS
706 if (phba->sli_rev <= LPFC_SLI_REV3) {
707 /* Clear all interrupt enable conditions */
708 writel(0, phba->HCregaddr);
709 readl(phba->HCregaddr); /* flush */
710 /* Clear all pending interrupts */
711 writel(0xffffffff, phba->HAregaddr);
712 readl(phba->HAregaddr); /* flush */
713 }
84d1b006 714 phba->link_state = LPFC_HBA_ERROR;
6e7288d9 715 if (rc != MBX_BUSY || flag == MBX_POLL)
84d1b006
JS
716 mempool_free(pmb, phba->mbox_mem_pool);
717 return -EIO;
718 }
e40a02c1 719 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
6e7288d9
JS
720 if (flag == MBX_POLL)
721 mempool_free(pmb, phba->mbox_mem_pool);
84d1b006
JS
722
723 return 0;
724}
725
726/**
727 * lpfc_hba_down_link - this routine downs the FC link
6e7288d9
JS
728 * @phba: pointer to lpfc hba data structure.
729 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
84d1b006
JS
730 *
731 * This routine will issue the DOWN_LINK mailbox command call.
732 * It is available to other drivers through the lpfc_hba data
733 * structure for use to stop the link.
734 *
735 * Return code
736 * 0 - success
737 * Any other value - error
738 **/
739int
6e7288d9 740lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
84d1b006
JS
741{
742 LPFC_MBOXQ_t *pmb;
743 int rc;
744
745 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
746 if (!pmb) {
747 phba->link_state = LPFC_HBA_ERROR;
748 return -ENOMEM;
749 }
750
751 lpfc_printf_log(phba,
752 KERN_ERR, LOG_INIT,
753 "0491 Adapter Link is disabled.\n");
754 lpfc_down_link(phba, pmb);
755 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6e7288d9 756 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
84d1b006
JS
757 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
758 lpfc_printf_log(phba,
759 KERN_ERR, LOG_INIT,
760 "2522 Adapter failed to issue DOWN_LINK"
761 " mbox command rc 0x%x\n", rc);
762
763 mempool_free(pmb, phba->mbox_mem_pool);
764 return -EIO;
765 }
6e7288d9
JS
766 if (flag == MBX_POLL)
767 mempool_free(pmb, phba->mbox_mem_pool);
768
84d1b006
JS
769 return 0;
770}
771
e59058c4 772/**
3621a710 773 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
e59058c4
JS
774 * @phba: pointer to lpfc HBA data structure.
775 *
776 * This routine will do LPFC uninitialization before the HBA is reset when
777 * bringing down the SLI Layer.
778 *
779 * Return codes
780 * 0 - success.
781 * Any other value - error.
782 **/
dea3101e 783int
2e0fef85 784lpfc_hba_down_prep(struct lpfc_hba *phba)
dea3101e 785{
1b32f6aa
JS
786 struct lpfc_vport **vports;
787 int i;
3772a991
JS
788
789 if (phba->sli_rev <= LPFC_SLI_REV3) {
790 /* Disable interrupts */
791 writel(0, phba->HCregaddr);
792 readl(phba->HCregaddr); /* flush */
793 }
dea3101e 794
1b32f6aa
JS
795 if (phba->pport->load_flag & FC_UNLOADING)
796 lpfc_cleanup_discovery_resources(phba->pport);
797 else {
798 vports = lpfc_create_vport_work_array(phba);
799 if (vports != NULL)
3772a991
JS
800 for (i = 0; i <= phba->max_vports &&
801 vports[i] != NULL; i++)
1b32f6aa
JS
802 lpfc_cleanup_discovery_resources(vports[i]);
803 lpfc_destroy_vport_work_array(phba, vports);
7f5f3d0d
JS
804 }
805 return 0;
dea3101e
JB
806}
807
e59058c4 808/**
3772a991 809 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
e59058c4
JS
810 * @phba: pointer to lpfc HBA data structure.
811 *
812 * This routine will do uninitialization after the HBA is reset when bring
813 * down the SLI Layer.
814 *
815 * Return codes
af901ca1 816 * 0 - success.
e59058c4
JS
817 * Any other value - error.
818 **/
3772a991
JS
819static int
820lpfc_hba_down_post_s3(struct lpfc_hba *phba)
41415862
JW
821{
822 struct lpfc_sli *psli = &phba->sli;
823 struct lpfc_sli_ring *pring;
824 struct lpfc_dmabuf *mp, *next_mp;
09372820 825 LIST_HEAD(completions);
41415862
JW
826 int i;
827
92d7f7b0
JS
828 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
829 lpfc_sli_hbqbuf_free_all(phba);
830 else {
831 /* Cleanup preposted buffers on the ELS ring */
832 pring = &psli->ring[LPFC_ELS_RING];
833 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
834 list_del(&mp->list);
835 pring->postbufq_cnt--;
836 lpfc_mbuf_free(phba, mp->virt, mp->phys);
837 kfree(mp);
838 }
41415862
JW
839 }
840
09372820 841 spin_lock_irq(&phba->hbalock);
41415862
JW
842 for (i = 0; i < psli->num_rings; i++) {
843 pring = &psli->ring[i];
09372820
JS
844
845 /* At this point in time the HBA is either reset or DOA. Either
846 * way, nothing should be on txcmplq as it will NEVER complete.
847 */
848 list_splice_init(&pring->txcmplq, &completions);
09372820
JS
849 spin_unlock_irq(&phba->hbalock);
850
a257bf90
JS
851 /* Cancel all the IOCBs from the completions list */
852 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
853 IOERR_SLI_ABORTED);
09372820 854
41415862 855 lpfc_sli_abort_iocb_ring(phba, pring);
09372820 856 spin_lock_irq(&phba->hbalock);
41415862 857 }
09372820 858 spin_unlock_irq(&phba->hbalock);
41415862
JW
859
860 return 0;
861}
5af5eee7 862
da0436e9
JS
863/**
864 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
865 * @phba: pointer to lpfc HBA data structure.
866 *
867 * This routine will do uninitialization after the HBA is reset when bring
868 * down the SLI Layer.
869 *
870 * Return codes
af901ca1 871 * 0 - success.
da0436e9
JS
872 * Any other value - error.
873 **/
874static int
875lpfc_hba_down_post_s4(struct lpfc_hba *phba)
876{
877 struct lpfc_scsi_buf *psb, *psb_next;
878 LIST_HEAD(aborts);
879 int ret;
880 unsigned long iflag = 0;
0f65ff68
JS
881 struct lpfc_sglq *sglq_entry = NULL;
882
da0436e9
JS
883 ret = lpfc_hba_down_post_s3(phba);
884 if (ret)
885 return ret;
886 /* At this point in time the HBA is either reset or DOA. Either
887 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
888 * on the lpfc_sgl_list so that it can either be freed if the
889 * driver is unloading or reposted if the driver is restarting
890 * the port.
891 */
892 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
893 /* scsl_buf_list */
894 /* abts_sgl_list_lock required because worker thread uses this
895 * list.
896 */
897 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
0f65ff68
JS
898 list_for_each_entry(sglq_entry,
899 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
900 sglq_entry->state = SGL_FREED;
901
da0436e9
JS
902 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
903 &phba->sli4_hba.lpfc_sgl_list);
904 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
905 /* abts_scsi_buf_list_lock required because worker thread uses this
906 * list.
907 */
908 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
909 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
910 &aborts);
911 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
912 spin_unlock_irq(&phba->hbalock);
913
914 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
915 psb->pCmd = NULL;
916 psb->status = IOSTAT_SUCCESS;
917 }
a40fc5f0
JS
918 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
919 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
920 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
da0436e9
JS
921 return 0;
922}
923
924/**
925 * lpfc_hba_down_post - Wrapper func for hba down post routine
926 * @phba: pointer to lpfc HBA data structure.
927 *
928 * This routine wraps the actual SLI3 or SLI4 routine for performing
929 * uninitialization after the HBA is reset when bring down the SLI Layer.
930 *
931 * Return codes
af901ca1 932 * 0 - success.
da0436e9
JS
933 * Any other value - error.
934 **/
935int
936lpfc_hba_down_post(struct lpfc_hba *phba)
937{
938 return (*phba->lpfc_hba_down_post)(phba);
939}
41415862 940
e59058c4 941/**
3621a710 942 * lpfc_hb_timeout - The HBA-timer timeout handler
e59058c4
JS
943 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
944 *
945 * This is the HBA-timer timeout handler registered to the lpfc driver. When
946 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
947 * work-port-events bitmap and the worker thread is notified. This timeout
948 * event will be used by the worker thread to invoke the actual timeout
949 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
950 * be performed in the timeout handler and the HBA timeout event bit shall
951 * be cleared by the worker thread after it has taken the event bitmap out.
952 **/
a6ababd2 953static void
858c9f6c
JS
954lpfc_hb_timeout(unsigned long ptr)
955{
956 struct lpfc_hba *phba;
5e9d9b82 957 uint32_t tmo_posted;
858c9f6c
JS
958 unsigned long iflag;
959
960 phba = (struct lpfc_hba *)ptr;
9399627f
JS
961
962 /* Check for heart beat timeout conditions */
858c9f6c 963 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5e9d9b82
JS
964 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
965 if (!tmo_posted)
858c9f6c
JS
966 phba->pport->work_port_events |= WORKER_HB_TMO;
967 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
968
9399627f 969 /* Tell the worker thread there is work to do */
5e9d9b82
JS
970 if (!tmo_posted)
971 lpfc_worker_wake_up(phba);
858c9f6c
JS
972 return;
973}
974
19ca7609
JS
975/**
976 * lpfc_rrq_timeout - The RRQ-timer timeout handler
977 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
978 *
979 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
980 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
981 * work-port-events bitmap and the worker thread is notified. This timeout
982 * event will be used by the worker thread to invoke the actual timeout
983 * handler routine, lpfc_rrq_handler. Any periodical operations will
984 * be performed in the timeout handler and the RRQ timeout event bit shall
985 * be cleared by the worker thread after it has taken the event bitmap out.
986 **/
987static void
988lpfc_rrq_timeout(unsigned long ptr)
989{
990 struct lpfc_hba *phba;
19ca7609
JS
991 unsigned long iflag;
992
993 phba = (struct lpfc_hba *)ptr;
994 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1151e3ec 995 phba->hba_flag |= HBA_RRQ_ACTIVE;
19ca7609 996 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1151e3ec 997 lpfc_worker_wake_up(phba);
19ca7609
JS
998}
999
e59058c4 1000/**
3621a710 1001 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
e59058c4
JS
1002 * @phba: pointer to lpfc hba data structure.
1003 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1004 *
1005 * This is the callback function to the lpfc heart-beat mailbox command.
1006 * If configured, the lpfc driver issues the heart-beat mailbox command to
1007 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1008 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1009 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1010 * heart-beat outstanding state. Once the mailbox command comes back and
1011 * no error conditions detected, the heart-beat mailbox command timer is
1012 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1013 * state is cleared for the next heart-beat. If the timer expired with the
1014 * heart-beat outstanding state set, the driver will put the HBA offline.
1015 **/
858c9f6c
JS
1016static void
1017lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1018{
1019 unsigned long drvr_flag;
1020
1021 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1022 phba->hb_outstanding = 0;
1023 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1024
9399627f 1025 /* Check and reset heart-beat timer is necessary */
858c9f6c
JS
1026 mempool_free(pmboxq, phba->mbox_mem_pool);
1027 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1028 !(phba->link_state == LPFC_HBA_ERROR) &&
51ef4c26 1029 !(phba->pport->load_flag & FC_UNLOADING))
858c9f6c 1030 mod_timer(&phba->hb_tmofunc,
256ec0d0
JS
1031 jiffies +
1032 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
858c9f6c
JS
1033 return;
1034}
1035
e59058c4 1036/**
3621a710 1037 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
e59058c4
JS
1038 * @phba: pointer to lpfc hba data structure.
1039 *
1040 * This is the actual HBA-timer timeout handler to be invoked by the worker
1041 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1042 * handler performs any periodic operations needed for the device. If such
1043 * periodic event has already been attended to either in the interrupt handler
1044 * or by processing slow-ring or fast-ring events within the HBA-timer
1045 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1046 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1047 * is configured and there is no heart-beat mailbox command outstanding, a
1048 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1049 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1050 * to offline.
1051 **/
858c9f6c
JS
1052void
1053lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1054{
45ed1190 1055 struct lpfc_vport **vports;
858c9f6c 1056 LPFC_MBOXQ_t *pmboxq;
0ff10d46 1057 struct lpfc_dmabuf *buf_ptr;
45ed1190 1058 int retval, i;
858c9f6c 1059 struct lpfc_sli *psli = &phba->sli;
0ff10d46 1060 LIST_HEAD(completions);
858c9f6c 1061
45ed1190
JS
1062 vports = lpfc_create_vport_work_array(phba);
1063 if (vports != NULL)
1064 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1065 lpfc_rcv_seq_check_edtov(vports[i]);
1066 lpfc_destroy_vport_work_array(phba, vports);
1067
858c9f6c 1068 if ((phba->link_state == LPFC_HBA_ERROR) ||
51ef4c26 1069 (phba->pport->load_flag & FC_UNLOADING) ||
858c9f6c
JS
1070 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1071 return;
1072
1073 spin_lock_irq(&phba->pport->work_port_lock);
858c9f6c 1074
256ec0d0
JS
1075 if (time_after(phba->last_completion_time +
1076 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1077 jiffies)) {
858c9f6c
JS
1078 spin_unlock_irq(&phba->pport->work_port_lock);
1079 if (!phba->hb_outstanding)
1080 mod_timer(&phba->hb_tmofunc,
256ec0d0
JS
1081 jiffies +
1082 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
858c9f6c
JS
1083 else
1084 mod_timer(&phba->hb_tmofunc,
256ec0d0
JS
1085 jiffies +
1086 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
858c9f6c
JS
1087 return;
1088 }
1089 spin_unlock_irq(&phba->pport->work_port_lock);
1090
0ff10d46
JS
1091 if (phba->elsbuf_cnt &&
1092 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1093 spin_lock_irq(&phba->hbalock);
1094 list_splice_init(&phba->elsbuf, &completions);
1095 phba->elsbuf_cnt = 0;
1096 phba->elsbuf_prev_cnt = 0;
1097 spin_unlock_irq(&phba->hbalock);
1098
1099 while (!list_empty(&completions)) {
1100 list_remove_head(&completions, buf_ptr,
1101 struct lpfc_dmabuf, list);
1102 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1103 kfree(buf_ptr);
1104 }
1105 }
1106 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1107
858c9f6c 1108 /* If there is no heart beat outstanding, issue a heartbeat command */
13815c83
JS
1109 if (phba->cfg_enable_hba_heartbeat) {
1110 if (!phba->hb_outstanding) {
bc73905a
JS
1111 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1112 (list_empty(&psli->mboxq))) {
1113 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1114 GFP_KERNEL);
1115 if (!pmboxq) {
1116 mod_timer(&phba->hb_tmofunc,
1117 jiffies +
256ec0d0
JS
1118 msecs_to_jiffies(1000 *
1119 LPFC_HB_MBOX_INTERVAL));
bc73905a
JS
1120 return;
1121 }
1122
1123 lpfc_heart_beat(phba, pmboxq);
1124 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1125 pmboxq->vport = phba->pport;
1126 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1127 MBX_NOWAIT);
1128
1129 if (retval != MBX_BUSY &&
1130 retval != MBX_SUCCESS) {
1131 mempool_free(pmboxq,
1132 phba->mbox_mem_pool);
1133 mod_timer(&phba->hb_tmofunc,
1134 jiffies +
256ec0d0
JS
1135 msecs_to_jiffies(1000 *
1136 LPFC_HB_MBOX_INTERVAL));
bc73905a
JS
1137 return;
1138 }
1139 phba->skipped_hb = 0;
1140 phba->hb_outstanding = 1;
1141 } else if (time_before_eq(phba->last_completion_time,
1142 phba->skipped_hb)) {
1143 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1144 "2857 Last completion time not "
1145 " updated in %d ms\n",
1146 jiffies_to_msecs(jiffies
1147 - phba->last_completion_time));
1148 } else
1149 phba->skipped_hb = jiffies;
1150
858c9f6c 1151 mod_timer(&phba->hb_tmofunc,
256ec0d0
JS
1152 jiffies +
1153 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
858c9f6c 1154 return;
13815c83
JS
1155 } else {
1156 /*
1157 * If heart beat timeout called with hb_outstanding set
dcf2a4e0
JS
1158 * we need to give the hb mailbox cmd a chance to
1159 * complete or TMO.
13815c83 1160 */
dcf2a4e0
JS
1161 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1162 "0459 Adapter heartbeat still out"
1163 "standing:last compl time was %d ms.\n",
1164 jiffies_to_msecs(jiffies
1165 - phba->last_completion_time));
1166 mod_timer(&phba->hb_tmofunc,
256ec0d0
JS
1167 jiffies +
1168 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
858c9f6c 1169 }
858c9f6c
JS
1170 }
1171}
1172
e59058c4 1173/**
3621a710 1174 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
e59058c4
JS
1175 * @phba: pointer to lpfc hba data structure.
1176 *
1177 * This routine is called to bring the HBA offline when HBA hardware error
1178 * other than Port Error 6 has been detected.
1179 **/
09372820
JS
1180static void
1181lpfc_offline_eratt(struct lpfc_hba *phba)
1182{
1183 struct lpfc_sli *psli = &phba->sli;
1184
1185 spin_lock_irq(&phba->hbalock);
f4b4c68f 1186 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
09372820 1187 spin_unlock_irq(&phba->hbalock);
618a5230 1188 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
09372820
JS
1189
1190 lpfc_offline(phba);
1191 lpfc_reset_barrier(phba);
f4b4c68f 1192 spin_lock_irq(&phba->hbalock);
09372820 1193 lpfc_sli_brdreset(phba);
f4b4c68f 1194 spin_unlock_irq(&phba->hbalock);
09372820
JS
1195 lpfc_hba_down_post(phba);
1196 lpfc_sli_brdready(phba, HS_MBRDY);
1197 lpfc_unblock_mgmt_io(phba);
1198 phba->link_state = LPFC_HBA_ERROR;
1199 return;
1200}
1201
da0436e9
JS
1202/**
1203 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1204 * @phba: pointer to lpfc hba data structure.
1205 *
1206 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1207 * other than Port Error 6 has been detected.
1208 **/
a88dbb6a 1209void
da0436e9
JS
1210lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1211{
618a5230 1212 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
da0436e9
JS
1213 lpfc_offline(phba);
1214 lpfc_sli4_brdreset(phba);
1215 lpfc_hba_down_post(phba);
1216 lpfc_sli4_post_status_check(phba);
1217 lpfc_unblock_mgmt_io(phba);
1218 phba->link_state = LPFC_HBA_ERROR;
1219}
1220
a257bf90
JS
1221/**
1222 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1223 * @phba: pointer to lpfc hba data structure.
1224 *
1225 * This routine is invoked to handle the deferred HBA hardware error
1226 * conditions. This type of error is indicated by HBA by setting ER1
1227 * and another ER bit in the host status register. The driver will
1228 * wait until the ER1 bit clears before handling the error condition.
1229 **/
1230static void
1231lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1232{
1233 uint32_t old_host_status = phba->work_hs;
1234 struct lpfc_sli_ring *pring;
1235 struct lpfc_sli *psli = &phba->sli;
1236
f4b4c68f
JS
1237 /* If the pci channel is offline, ignore possible errors,
1238 * since we cannot communicate with the pci card anyway.
1239 */
1240 if (pci_channel_offline(phba->pcidev)) {
1241 spin_lock_irq(&phba->hbalock);
1242 phba->hba_flag &= ~DEFER_ERATT;
1243 spin_unlock_irq(&phba->hbalock);
1244 return;
1245 }
1246
a257bf90
JS
1247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1248 "0479 Deferred Adapter Hardware Error "
1249 "Data: x%x x%x x%x\n",
1250 phba->work_hs,
1251 phba->work_status[0], phba->work_status[1]);
1252
1253 spin_lock_irq(&phba->hbalock);
f4b4c68f 1254 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
a257bf90
JS
1255 spin_unlock_irq(&phba->hbalock);
1256
1257
1258 /*
1259 * Firmware stops when it triggred erratt. That could cause the I/Os
1260 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1261 * SCSI layer retry it after re-establishing link.
1262 */
1263 pring = &psli->ring[psli->fcp_ring];
1264 lpfc_sli_abort_iocb_ring(phba, pring);
1265
1266 /*
1267 * There was a firmware error. Take the hba offline and then
1268 * attempt to restart it.
1269 */
618a5230 1270 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
a257bf90
JS
1271 lpfc_offline(phba);
1272
1273 /* Wait for the ER1 bit to clear.*/
1274 while (phba->work_hs & HS_FFER1) {
1275 msleep(100);
9940b97b
JS
1276 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1277 phba->work_hs = UNPLUG_ERR ;
1278 break;
1279 }
a257bf90
JS
1280 /* If driver is unloading let the worker thread continue */
1281 if (phba->pport->load_flag & FC_UNLOADING) {
1282 phba->work_hs = 0;
1283 break;
1284 }
1285 }
1286
1287 /*
1288 * This is to ptrotect against a race condition in which
1289 * first write to the host attention register clear the
1290 * host status register.
1291 */
1292 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1293 phba->work_hs = old_host_status & ~HS_FFER1;
1294
3772a991 1295 spin_lock_irq(&phba->hbalock);
a257bf90 1296 phba->hba_flag &= ~DEFER_ERATT;
3772a991 1297 spin_unlock_irq(&phba->hbalock);
a257bf90
JS
1298 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1299 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1300}
1301
3772a991
JS
1302static void
1303lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1304{
1305 struct lpfc_board_event_header board_event;
1306 struct Scsi_Host *shost;
1307
1308 board_event.event_type = FC_REG_BOARD_EVENT;
1309 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1310 shost = lpfc_shost_from_vport(phba->pport);
1311 fc_host_post_vendor_event(shost, fc_get_event_number(),
1312 sizeof(board_event),
1313 (char *) &board_event,
1314 LPFC_NL_VENDOR_ID);
1315}
1316
e59058c4 1317/**
3772a991 1318 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
e59058c4
JS
1319 * @phba: pointer to lpfc hba data structure.
1320 *
1321 * This routine is invoked to handle the following HBA hardware error
1322 * conditions:
1323 * 1 - HBA error attention interrupt
1324 * 2 - DMA ring index out of range
1325 * 3 - Mailbox command came back as unknown
1326 **/
3772a991
JS
1327static void
1328lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea3101e 1329{
2e0fef85 1330 struct lpfc_vport *vport = phba->pport;
2e0fef85 1331 struct lpfc_sli *psli = &phba->sli;
dea3101e 1332 struct lpfc_sli_ring *pring;
d2873e4c 1333 uint32_t event_data;
57127f15
JS
1334 unsigned long temperature;
1335 struct temp_event temp_event_data;
92d7f7b0 1336 struct Scsi_Host *shost;
2e0fef85 1337
8d63f375 1338 /* If the pci channel is offline, ignore possible errors,
3772a991
JS
1339 * since we cannot communicate with the pci card anyway.
1340 */
1341 if (pci_channel_offline(phba->pcidev)) {
1342 spin_lock_irq(&phba->hbalock);
1343 phba->hba_flag &= ~DEFER_ERATT;
1344 spin_unlock_irq(&phba->hbalock);
8d63f375 1345 return;
3772a991
JS
1346 }
1347
13815c83
JS
1348 /* If resets are disabled then leave the HBA alone and return */
1349 if (!phba->cfg_enable_hba_reset)
1350 return;
dea3101e 1351
ea2151b4 1352 /* Send an internal error event to mgmt application */
3772a991 1353 lpfc_board_errevt_to_mgmt(phba);
ea2151b4 1354
a257bf90
JS
1355 if (phba->hba_flag & DEFER_ERATT)
1356 lpfc_handle_deferred_eratt(phba);
1357
dcf2a4e0
JS
1358 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1359 if (phba->work_hs & HS_FFER6)
1360 /* Re-establishing Link */
1361 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1362 "1301 Re-establishing Link "
1363 "Data: x%x x%x x%x\n",
1364 phba->work_hs, phba->work_status[0],
1365 phba->work_status[1]);
1366 if (phba->work_hs & HS_FFER8)
1367 /* Device Zeroization */
1368 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1369 "2861 Host Authentication device "
1370 "zeroization Data:x%x x%x x%x\n",
1371 phba->work_hs, phba->work_status[0],
1372 phba->work_status[1]);
58da1ffb 1373
92d7f7b0 1374 spin_lock_irq(&phba->hbalock);
f4b4c68f 1375 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
92d7f7b0 1376 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
1377
1378 /*
1379 * Firmware stops when it triggled erratt with HS_FFER6.
1380 * That could cause the I/Os dropped by the firmware.
1381 * Error iocb (I/O) on txcmplq and let the SCSI layer
1382 * retry it after re-establishing link.
1383 */
1384 pring = &psli->ring[psli->fcp_ring];
1385 lpfc_sli_abort_iocb_ring(phba, pring);
1386
dea3101e
JB
1387 /*
1388 * There was a firmware error. Take the hba offline and then
1389 * attempt to restart it.
1390 */
618a5230 1391 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea3101e 1392 lpfc_offline(phba);
41415862 1393 lpfc_sli_brdrestart(phba);
dea3101e 1394 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
46fa311e 1395 lpfc_unblock_mgmt_io(phba);
dea3101e
JB
1396 return;
1397 }
46fa311e 1398 lpfc_unblock_mgmt_io(phba);
57127f15
JS
1399 } else if (phba->work_hs & HS_CRIT_TEMP) {
1400 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1401 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1402 temp_event_data.event_code = LPFC_CRIT_TEMP;
1403 temp_event_data.data = (uint32_t)temperature;
1404
1405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 1406 "0406 Adapter maximum temperature exceeded "
57127f15
JS
1407 "(%ld), taking this port offline "
1408 "Data: x%x x%x x%x\n",
1409 temperature, phba->work_hs,
1410 phba->work_status[0], phba->work_status[1]);
1411
1412 shost = lpfc_shost_from_vport(phba->pport);
1413 fc_host_post_vendor_event(shost, fc_get_event_number(),
1414 sizeof(temp_event_data),
1415 (char *) &temp_event_data,
1416 SCSI_NL_VID_TYPE_PCI
1417 | PCI_VENDOR_ID_EMULEX);
1418
7af67051 1419 spin_lock_irq(&phba->hbalock);
7af67051
JS
1420 phba->over_temp_state = HBA_OVER_TEMP;
1421 spin_unlock_irq(&phba->hbalock);
09372820 1422 lpfc_offline_eratt(phba);
57127f15 1423
dea3101e
JB
1424 } else {
1425 /* The if clause above forces this code path when the status
9399627f
JS
1426 * failure is a value other than FFER6. Do not call the offline
1427 * twice. This is the adapter hardware error path.
dea3101e
JB
1428 */
1429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1430 "0457 Adapter Hardware Error "
dea3101e 1431 "Data: x%x x%x x%x\n",
e8b62011 1432 phba->work_hs,
dea3101e
JB
1433 phba->work_status[0], phba->work_status[1]);
1434
d2873e4c 1435 event_data = FC_REG_DUMP_EVENT;
92d7f7b0 1436 shost = lpfc_shost_from_vport(vport);
2e0fef85 1437 fc_host_post_vendor_event(shost, fc_get_event_number(),
d2873e4c
JS
1438 sizeof(event_data), (char *) &event_data,
1439 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1440
09372820 1441 lpfc_offline_eratt(phba);
dea3101e 1442 }
9399627f 1443 return;
dea3101e
JB
1444}
1445
618a5230
JS
1446/**
1447 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1448 * @phba: pointer to lpfc hba data structure.
1449 * @mbx_action: flag for mailbox shutdown action.
1450 *
1451 * This routine is invoked to perform an SLI4 port PCI function reset in
1452 * response to port status register polling attention. It waits for port
1453 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1454 * During this process, interrupt vectors are freed and later requested
1455 * for handling possible port resource change.
1456 **/
1457static int
1458lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1459{
1460 int rc;
1461 uint32_t intr_mode;
1462
1463 /*
1464 * On error status condition, driver need to wait for port
1465 * ready before performing reset.
1466 */
1467 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1468 if (!rc) {
1469 /* need reset: attempt for port recovery */
1470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1471 "2887 Reset Needed: Attempting Port "
1472 "Recovery...\n");
1473 lpfc_offline_prep(phba, mbx_action);
1474 lpfc_offline(phba);
1475 /* release interrupt for possible resource change */
1476 lpfc_sli4_disable_intr(phba);
1477 lpfc_sli_brdrestart(phba);
1478 /* request and enable interrupt */
1479 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1480 if (intr_mode == LPFC_INTR_ERROR) {
1481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1482 "3175 Failed to enable interrupt\n");
1483 return -EIO;
1484 } else {
1485 phba->intr_mode = intr_mode;
1486 }
1487 rc = lpfc_online(phba);
1488 if (rc == 0)
1489 lpfc_unblock_mgmt_io(phba);
1490 }
1491 return rc;
1492}
1493
da0436e9
JS
1494/**
1495 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1496 * @phba: pointer to lpfc hba data structure.
1497 *
1498 * This routine is invoked to handle the SLI4 HBA hardware error attention
1499 * conditions.
1500 **/
1501static void
1502lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1503{
1504 struct lpfc_vport *vport = phba->pport;
1505 uint32_t event_data;
1506 struct Scsi_Host *shost;
2fcee4bf 1507 uint32_t if_type;
2e90f4b5
JS
1508 struct lpfc_register portstat_reg = {0};
1509 uint32_t reg_err1, reg_err2;
1510 uint32_t uerrlo_reg, uemasklo_reg;
1511 uint32_t pci_rd_rc1, pci_rd_rc2;
73d91e50 1512 int rc;
da0436e9
JS
1513
1514 /* If the pci channel is offline, ignore possible errors, since
1515 * we cannot communicate with the pci card anyway.
1516 */
1517 if (pci_channel_offline(phba->pcidev))
1518 return;
1519 /* If resets are disabled then leave the HBA alone and return */
1520 if (!phba->cfg_enable_hba_reset)
1521 return;
1522
2fcee4bf
JS
1523 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1524 switch (if_type) {
1525 case LPFC_SLI_INTF_IF_TYPE_0:
2e90f4b5
JS
1526 pci_rd_rc1 = lpfc_readl(
1527 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1528 &uerrlo_reg);
1529 pci_rd_rc2 = lpfc_readl(
1530 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1531 &uemasklo_reg);
1532 /* consider PCI bus read error as pci_channel_offline */
1533 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1534 return;
2fcee4bf
JS
1535 lpfc_sli4_offline_eratt(phba);
1536 break;
1537 case LPFC_SLI_INTF_IF_TYPE_2:
2e90f4b5
JS
1538 pci_rd_rc1 = lpfc_readl(
1539 phba->sli4_hba.u.if_type2.STATUSregaddr,
1540 &portstat_reg.word0);
1541 /* consider PCI bus read error as pci_channel_offline */
6b5151fd
JS
1542 if (pci_rd_rc1 == -EIO) {
1543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1544 "3151 PCI bus read access failure: x%x\n",
1545 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
2e90f4b5 1546 return;
6b5151fd 1547 }
2e90f4b5
JS
1548 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1549 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
2fcee4bf
JS
1550 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1551 /* TODO: Register for Overtemp async events. */
1552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1553 "2889 Port Overtemperature event, "
026abb87 1554 "taking port offline\n");
2fcee4bf
JS
1555 spin_lock_irq(&phba->hbalock);
1556 phba->over_temp_state = HBA_OVER_TEMP;
1557 spin_unlock_irq(&phba->hbalock);
1558 lpfc_sli4_offline_eratt(phba);
2e90f4b5 1559 break;
2fcee4bf 1560 }
2e90f4b5
JS
1561 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1562 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1564 "3143 Port Down: Firmware Restarted\n");
1565 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1566 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1568 "3144 Port Down: Debug Dump\n");
1569 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1570 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1572 "3145 Port Down: Provisioning\n");
618a5230
JS
1573
1574 /* Check port status register for function reset */
1575 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1576 if (rc == 0) {
1577 /* don't report event on forced debug dump */
1578 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1579 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1580 return;
1581 else
1582 break;
2fcee4bf 1583 }
618a5230 1584 /* fall through for not able to recover */
6b5151fd
JS
1585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1586 "3152 Unrecoverable error, bring the port "
1587 "offline\n");
2fcee4bf
JS
1588 lpfc_sli4_offline_eratt(phba);
1589 break;
1590 case LPFC_SLI_INTF_IF_TYPE_1:
1591 default:
1592 break;
1593 }
2e90f4b5
JS
1594 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1595 "3123 Report dump event to upper layer\n");
1596 /* Send an internal error event to mgmt application */
1597 lpfc_board_errevt_to_mgmt(phba);
1598
1599 event_data = FC_REG_DUMP_EVENT;
1600 shost = lpfc_shost_from_vport(vport);
1601 fc_host_post_vendor_event(shost, fc_get_event_number(),
1602 sizeof(event_data), (char *) &event_data,
1603 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
da0436e9
JS
1604}
1605
1606/**
1607 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1608 * @phba: pointer to lpfc HBA data structure.
1609 *
1610 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1611 * routine from the API jump table function pointer from the lpfc_hba struct.
1612 *
1613 * Return codes
af901ca1 1614 * 0 - success.
da0436e9
JS
1615 * Any other value - error.
1616 **/
1617void
1618lpfc_handle_eratt(struct lpfc_hba *phba)
1619{
1620 (*phba->lpfc_handle_eratt)(phba);
1621}
1622
e59058c4 1623/**
3621a710 1624 * lpfc_handle_latt - The HBA link event handler
e59058c4
JS
1625 * @phba: pointer to lpfc hba data structure.
1626 *
1627 * This routine is invoked from the worker thread to handle a HBA host
1628 * attention link event.
1629 **/
dea3101e 1630void
2e0fef85 1631lpfc_handle_latt(struct lpfc_hba *phba)
dea3101e 1632{
2e0fef85
JS
1633 struct lpfc_vport *vport = phba->pport;
1634 struct lpfc_sli *psli = &phba->sli;
dea3101e
JB
1635 LPFC_MBOXQ_t *pmb;
1636 volatile uint32_t control;
1637 struct lpfc_dmabuf *mp;
09372820 1638 int rc = 0;
dea3101e
JB
1639
1640 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
09372820
JS
1641 if (!pmb) {
1642 rc = 1;
dea3101e 1643 goto lpfc_handle_latt_err_exit;
09372820 1644 }
dea3101e
JB
1645
1646 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
09372820
JS
1647 if (!mp) {
1648 rc = 2;
dea3101e 1649 goto lpfc_handle_latt_free_pmb;
09372820 1650 }
dea3101e
JB
1651
1652 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
09372820
JS
1653 if (!mp->virt) {
1654 rc = 3;
dea3101e 1655 goto lpfc_handle_latt_free_mp;
09372820 1656 }
dea3101e 1657
6281bfe0 1658 /* Cleanup any outstanding ELS commands */
549e55cd 1659 lpfc_els_flush_all_cmd(phba);
dea3101e
JB
1660
1661 psli->slistat.link_event++;
76a95d75
JS
1662 lpfc_read_topology(phba, pmb, mp);
1663 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
2e0fef85 1664 pmb->vport = vport;
0d2b6b83
JS
1665 /* Block ELS IOCBs until we have processed this mbox command */
1666 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
0b727fea 1667 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
09372820
JS
1668 if (rc == MBX_NOT_FINISHED) {
1669 rc = 4;
14691150 1670 goto lpfc_handle_latt_free_mbuf;
09372820 1671 }
dea3101e
JB
1672
1673 /* Clear Link Attention in HA REG */
2e0fef85 1674 spin_lock_irq(&phba->hbalock);
dea3101e
JB
1675 writel(HA_LATT, phba->HAregaddr);
1676 readl(phba->HAregaddr); /* flush */
2e0fef85 1677 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
1678
1679 return;
1680
14691150 1681lpfc_handle_latt_free_mbuf:
0d2b6b83 1682 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
14691150 1683 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea3101e
JB
1684lpfc_handle_latt_free_mp:
1685 kfree(mp);
1686lpfc_handle_latt_free_pmb:
1dcb58e5 1687 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e
JB
1688lpfc_handle_latt_err_exit:
1689 /* Enable Link attention interrupts */
2e0fef85 1690 spin_lock_irq(&phba->hbalock);
dea3101e
JB
1691 psli->sli_flag |= LPFC_PROCESS_LA;
1692 control = readl(phba->HCregaddr);
1693 control |= HC_LAINT_ENA;
1694 writel(control, phba->HCregaddr);
1695 readl(phba->HCregaddr); /* flush */
1696
1697 /* Clear Link Attention in HA REG */
1698 writel(HA_LATT, phba->HAregaddr);
1699 readl(phba->HAregaddr); /* flush */
2e0fef85 1700 spin_unlock_irq(&phba->hbalock);
dea3101e 1701 lpfc_linkdown(phba);
2e0fef85 1702 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1703
09372820
JS
1704 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1705 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea3101e
JB
1706
1707 return;
1708}
1709
e59058c4 1710/**
3621a710 1711 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
e59058c4
JS
1712 * @phba: pointer to lpfc hba data structure.
1713 * @vpd: pointer to the vital product data.
1714 * @len: length of the vital product data in bytes.
1715 *
1716 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1717 * an array of characters. In this routine, the ModelName, ProgramType, and
1718 * ModelDesc, etc. fields of the phba data structure will be populated.
1719 *
1720 * Return codes
1721 * 0 - pointer to the VPD passed in is NULL
1722 * 1 - success
1723 **/
3772a991 1724int
2e0fef85 1725lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea3101e
JB
1726{
1727 uint8_t lenlo, lenhi;
07da60c1 1728 int Length;
dea3101e
JB
1729 int i, j;
1730 int finished = 0;
1731 int index = 0;
1732
1733 if (!vpd)
1734 return 0;
1735
1736 /* Vital Product */
ed957684 1737 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 1738 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea3101e
JB
1739 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1740 (uint32_t) vpd[3]);
74b72a59 1741 while (!finished && (index < (len - 4))) {
dea3101e
JB
1742 switch (vpd[index]) {
1743 case 0x82:
74b72a59 1744 case 0x91:
dea3101e
JB
1745 index += 1;
1746 lenlo = vpd[index];
1747 index += 1;
1748 lenhi = vpd[index];
1749 index += 1;
1750 i = ((((unsigned short)lenhi) << 8) + lenlo);
1751 index += i;
1752 break;
1753 case 0x90:
1754 index += 1;
1755 lenlo = vpd[index];
1756 index += 1;
1757 lenhi = vpd[index];
1758 index += 1;
1759 Length = ((((unsigned short)lenhi) << 8) + lenlo);
74b72a59
JW
1760 if (Length > len - index)
1761 Length = len - index;
dea3101e
JB
1762 while (Length > 0) {
1763 /* Look for Serial Number */
1764 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1765 index += 2;
1766 i = vpd[index];
1767 index += 1;
1768 j = 0;
1769 Length -= (3+i);
1770 while(i--) {
1771 phba->SerialNumber[j++] = vpd[index++];
1772 if (j == 31)
1773 break;
1774 }
1775 phba->SerialNumber[j] = 0;
1776 continue;
1777 }
1778 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1779 phba->vpd_flag |= VPD_MODEL_DESC;
1780 index += 2;
1781 i = vpd[index];
1782 index += 1;
1783 j = 0;
1784 Length -= (3+i);
1785 while(i--) {
1786 phba->ModelDesc[j++] = vpd[index++];
1787 if (j == 255)
1788 break;
1789 }
1790 phba->ModelDesc[j] = 0;
1791 continue;
1792 }
1793 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1794 phba->vpd_flag |= VPD_MODEL_NAME;
1795 index += 2;
1796 i = vpd[index];
1797 index += 1;
1798 j = 0;
1799 Length -= (3+i);
1800 while(i--) {
1801 phba->ModelName[j++] = vpd[index++];
1802 if (j == 79)
1803 break;
1804 }
1805 phba->ModelName[j] = 0;
1806 continue;
1807 }
1808 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1809 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1810 index += 2;
1811 i = vpd[index];
1812 index += 1;
1813 j = 0;
1814 Length -= (3+i);
1815 while(i--) {
1816 phba->ProgramType[j++] = vpd[index++];
1817 if (j == 255)
1818 break;
1819 }
1820 phba->ProgramType[j] = 0;
1821 continue;
1822 }
1823 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1824 phba->vpd_flag |= VPD_PORT;
1825 index += 2;
1826 i = vpd[index];
1827 index += 1;
1828 j = 0;
1829 Length -= (3+i);
1830 while(i--) {
cd1c8301
JS
1831 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1832 (phba->sli4_hba.pport_name_sta ==
1833 LPFC_SLI4_PPNAME_GET)) {
1834 j++;
1835 index++;
1836 } else
1837 phba->Port[j++] = vpd[index++];
1838 if (j == 19)
1839 break;
dea3101e 1840 }
cd1c8301
JS
1841 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1842 (phba->sli4_hba.pport_name_sta ==
1843 LPFC_SLI4_PPNAME_NON))
1844 phba->Port[j] = 0;
dea3101e
JB
1845 continue;
1846 }
1847 else {
1848 index += 2;
1849 i = vpd[index];
1850 index += 1;
1851 index += i;
1852 Length -= (3 + i);
1853 }
1854 }
1855 finished = 0;
1856 break;
1857 case 0x78:
1858 finished = 1;
1859 break;
1860 default:
1861 index ++;
1862 break;
1863 }
74b72a59 1864 }
dea3101e
JB
1865
1866 return(1);
1867}
1868
e59058c4 1869/**
3621a710 1870 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
e59058c4
JS
1871 * @phba: pointer to lpfc hba data structure.
1872 * @mdp: pointer to the data structure to hold the derived model name.
1873 * @descp: pointer to the data structure to hold the derived description.
1874 *
1875 * This routine retrieves HBA's description based on its registered PCI device
1876 * ID. The @descp passed into this function points to an array of 256 chars. It
1877 * shall be returned with the model name, maximum speed, and the host bus type.
1878 * The @mdp passed into this function points to an array of 80 chars. When the
1879 * function returns, the @mdp will be filled with the model name.
1880 **/
dea3101e 1881static void
2e0fef85 1882lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea3101e
JB
1883{
1884 lpfc_vpd_t *vp;
fefcb2b6 1885 uint16_t dev_id = phba->pcidev->device;
74b72a59 1886 int max_speed;
84774a4d 1887 int GE = 0;
da0436e9 1888 int oneConnect = 0; /* default is not a oneConnect */
74b72a59 1889 struct {
a747c9ce
JS
1890 char *name;
1891 char *bus;
1892 char *function;
1893 } m = {"<Unknown>", "", ""};
74b72a59
JW
1894
1895 if (mdp && mdp[0] != '\0'
1896 && descp && descp[0] != '\0')
1897 return;
1898
c0c11512
JS
1899 if (phba->lmt & LMT_16Gb)
1900 max_speed = 16;
1901 else if (phba->lmt & LMT_10Gb)
74b72a59
JW
1902 max_speed = 10;
1903 else if (phba->lmt & LMT_8Gb)
1904 max_speed = 8;
1905 else if (phba->lmt & LMT_4Gb)
1906 max_speed = 4;
1907 else if (phba->lmt & LMT_2Gb)
1908 max_speed = 2;
4169d868 1909 else if (phba->lmt & LMT_1Gb)
74b72a59 1910 max_speed = 1;
4169d868
JS
1911 else
1912 max_speed = 0;
dea3101e
JB
1913
1914 vp = &phba->vpd;
dea3101e 1915
e4adb204 1916 switch (dev_id) {
06325e74 1917 case PCI_DEVICE_ID_FIREFLY:
a747c9ce 1918 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
06325e74 1919 break;
dea3101e
JB
1920 case PCI_DEVICE_ID_SUPERFLY:
1921 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
a747c9ce
JS
1922 m = (typeof(m)){"LP7000", "PCI",
1923 "Fibre Channel Adapter"};
dea3101e 1924 else
a747c9ce
JS
1925 m = (typeof(m)){"LP7000E", "PCI",
1926 "Fibre Channel Adapter"};
dea3101e
JB
1927 break;
1928 case PCI_DEVICE_ID_DRAGONFLY:
a747c9ce
JS
1929 m = (typeof(m)){"LP8000", "PCI",
1930 "Fibre Channel Adapter"};
dea3101e
JB
1931 break;
1932 case PCI_DEVICE_ID_CENTAUR:
1933 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
a747c9ce
JS
1934 m = (typeof(m)){"LP9002", "PCI",
1935 "Fibre Channel Adapter"};
dea3101e 1936 else
a747c9ce
JS
1937 m = (typeof(m)){"LP9000", "PCI",
1938 "Fibre Channel Adapter"};
dea3101e
JB
1939 break;
1940 case PCI_DEVICE_ID_RFLY:
a747c9ce
JS
1941 m = (typeof(m)){"LP952", "PCI",
1942 "Fibre Channel Adapter"};
dea3101e
JB
1943 break;
1944 case PCI_DEVICE_ID_PEGASUS:
a747c9ce
JS
1945 m = (typeof(m)){"LP9802", "PCI-X",
1946 "Fibre Channel Adapter"};
dea3101e
JB
1947 break;
1948 case PCI_DEVICE_ID_THOR:
a747c9ce
JS
1949 m = (typeof(m)){"LP10000", "PCI-X",
1950 "Fibre Channel Adapter"};
dea3101e
JB
1951 break;
1952 case PCI_DEVICE_ID_VIPER:
a747c9ce
JS
1953 m = (typeof(m)){"LPX1000", "PCI-X",
1954 "Fibre Channel Adapter"};
dea3101e
JB
1955 break;
1956 case PCI_DEVICE_ID_PFLY:
a747c9ce
JS
1957 m = (typeof(m)){"LP982", "PCI-X",
1958 "Fibre Channel Adapter"};
dea3101e
JB
1959 break;
1960 case PCI_DEVICE_ID_TFLY:
a747c9ce
JS
1961 m = (typeof(m)){"LP1050", "PCI-X",
1962 "Fibre Channel Adapter"};
dea3101e
JB
1963 break;
1964 case PCI_DEVICE_ID_HELIOS:
a747c9ce
JS
1965 m = (typeof(m)){"LP11000", "PCI-X2",
1966 "Fibre Channel Adapter"};
dea3101e 1967 break;
e4adb204 1968 case PCI_DEVICE_ID_HELIOS_SCSP:
a747c9ce
JS
1969 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1970 "Fibre Channel Adapter"};
e4adb204
JSEC
1971 break;
1972 case PCI_DEVICE_ID_HELIOS_DCSP:
a747c9ce
JS
1973 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1974 "Fibre Channel Adapter"};
e4adb204
JSEC
1975 break;
1976 case PCI_DEVICE_ID_NEPTUNE:
a747c9ce 1977 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
e4adb204
JSEC
1978 break;
1979 case PCI_DEVICE_ID_NEPTUNE_SCSP:
a747c9ce 1980 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
e4adb204
JSEC
1981 break;
1982 case PCI_DEVICE_ID_NEPTUNE_DCSP:
a747c9ce 1983 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
e4adb204 1984 break;
dea3101e 1985 case PCI_DEVICE_ID_BMID:
a747c9ce 1986 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea3101e
JB
1987 break;
1988 case PCI_DEVICE_ID_BSMB:
a747c9ce 1989 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea3101e
JB
1990 break;
1991 case PCI_DEVICE_ID_ZEPHYR:
a747c9ce 1992 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea3101e 1993 break;
e4adb204 1994 case PCI_DEVICE_ID_ZEPHYR_SCSP:
a747c9ce 1995 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
e4adb204
JSEC
1996 break;
1997 case PCI_DEVICE_ID_ZEPHYR_DCSP:
a747c9ce 1998 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
a257bf90 1999 GE = 1;
e4adb204 2000 break;
dea3101e 2001 case PCI_DEVICE_ID_ZMID:
a747c9ce 2002 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea3101e
JB
2003 break;
2004 case PCI_DEVICE_ID_ZSMB:
a747c9ce 2005 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea3101e
JB
2006 break;
2007 case PCI_DEVICE_ID_LP101:
a747c9ce 2008 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea3101e
JB
2009 break;
2010 case PCI_DEVICE_ID_LP10000S:
a747c9ce 2011 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
06325e74 2012 break;
e4adb204 2013 case PCI_DEVICE_ID_LP11000S:
a747c9ce 2014 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
18a3b596 2015 break;
e4adb204 2016 case PCI_DEVICE_ID_LPE11000S:
a747c9ce 2017 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
5cc36b3c 2018 break;
b87eab38 2019 case PCI_DEVICE_ID_SAT:
a747c9ce 2020 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
2021 break;
2022 case PCI_DEVICE_ID_SAT_MID:
a747c9ce 2023 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
2024 break;
2025 case PCI_DEVICE_ID_SAT_SMB:
a747c9ce 2026 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
2027 break;
2028 case PCI_DEVICE_ID_SAT_DCSP:
a747c9ce 2029 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
2030 break;
2031 case PCI_DEVICE_ID_SAT_SCSP:
a747c9ce 2032 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
2033 break;
2034 case PCI_DEVICE_ID_SAT_S:
a747c9ce 2035 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
b87eab38 2036 break;
84774a4d 2037 case PCI_DEVICE_ID_HORNET:
a747c9ce 2038 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
84774a4d
JS
2039 GE = 1;
2040 break;
2041 case PCI_DEVICE_ID_PROTEUS_VF:
a747c9ce
JS
2042 m = (typeof(m)){"LPev12000", "PCIe IOV",
2043 "Fibre Channel Adapter"};
84774a4d
JS
2044 break;
2045 case PCI_DEVICE_ID_PROTEUS_PF:
a747c9ce
JS
2046 m = (typeof(m)){"LPev12000", "PCIe IOV",
2047 "Fibre Channel Adapter"};
84774a4d
JS
2048 break;
2049 case PCI_DEVICE_ID_PROTEUS_S:
a747c9ce
JS
2050 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2051 "Fibre Channel Adapter"};
84774a4d 2052 break;
da0436e9
JS
2053 case PCI_DEVICE_ID_TIGERSHARK:
2054 oneConnect = 1;
a747c9ce 2055 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
da0436e9 2056 break;
a747c9ce 2057 case PCI_DEVICE_ID_TOMCAT:
6669f9bb 2058 oneConnect = 1;
a747c9ce
JS
2059 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2060 break;
2061 case PCI_DEVICE_ID_FALCON:
2062 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2063 "EmulexSecure Fibre"};
6669f9bb 2064 break;
98fc5dd9
JS
2065 case PCI_DEVICE_ID_BALIUS:
2066 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2067 "Fibre Channel Adapter"};
2068 break;
085c647c 2069 case PCI_DEVICE_ID_LANCER_FC:
c0c11512
JS
2070 case PCI_DEVICE_ID_LANCER_FC_VF:
2071 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
085c647c
JS
2072 break;
2073 case PCI_DEVICE_ID_LANCER_FCOE:
c0c11512 2074 case PCI_DEVICE_ID_LANCER_FCOE_VF:
085c647c 2075 oneConnect = 1;
079b5c91 2076 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
085c647c 2077 break;
f8cafd38
JS
2078 case PCI_DEVICE_ID_SKYHAWK:
2079 case PCI_DEVICE_ID_SKYHAWK_VF:
2080 oneConnect = 1;
2081 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2082 break;
5cc36b3c 2083 default:
a747c9ce 2084 m = (typeof(m)){"Unknown", "", ""};
e4adb204 2085 break;
dea3101e 2086 }
74b72a59
JW
2087
2088 if (mdp && mdp[0] == '\0')
2089 snprintf(mdp, 79,"%s", m.name);
c0c11512
JS
2090 /*
2091 * oneConnect hba requires special processing, they are all initiators
da0436e9
JS
2092 * and we put the port number on the end
2093 */
2094 if (descp && descp[0] == '\0') {
2095 if (oneConnect)
2096 snprintf(descp, 255,
4169d868 2097 "Emulex OneConnect %s, %s Initiator %s",
a747c9ce 2098 m.name, m.function,
da0436e9 2099 phba->Port);
4169d868
JS
2100 else if (max_speed == 0)
2101 snprintf(descp, 255,
2102 "Emulex %s %s %s ",
2103 m.name, m.bus, m.function);
da0436e9
JS
2104 else
2105 snprintf(descp, 255,
2106 "Emulex %s %d%s %s %s",
a747c9ce
JS
2107 m.name, max_speed, (GE) ? "GE" : "Gb",
2108 m.bus, m.function);
da0436e9 2109 }
dea3101e
JB
2110}
2111
e59058c4 2112/**
3621a710 2113 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
e59058c4
JS
2114 * @phba: pointer to lpfc hba data structure.
2115 * @pring: pointer to a IOCB ring.
2116 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2117 *
2118 * This routine posts a given number of IOCBs with the associated DMA buffer
2119 * descriptors specified by the cnt argument to the given IOCB ring.
2120 *
2121 * Return codes
2122 * The number of IOCBs NOT able to be posted to the IOCB ring.
2123 **/
dea3101e 2124int
495a714c 2125lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea3101e
JB
2126{
2127 IOCB_t *icmd;
0bd4ca25 2128 struct lpfc_iocbq *iocb;
dea3101e
JB
2129 struct lpfc_dmabuf *mp1, *mp2;
2130
2131 cnt += pring->missbufcnt;
2132
2133 /* While there are buffers to post */
2134 while (cnt > 0) {
2135 /* Allocate buffer for command iocb */
0bd4ca25 2136 iocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
2137 if (iocb == NULL) {
2138 pring->missbufcnt = cnt;
2139 return cnt;
2140 }
dea3101e
JB
2141 icmd = &iocb->iocb;
2142
2143 /* 2 buffers can be posted per command */
2144 /* Allocate buffer to post */
2145 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2146 if (mp1)
98c9ea5c
JS
2147 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2148 if (!mp1 || !mp1->virt) {
c9475cb0 2149 kfree(mp1);
604a3e30 2150 lpfc_sli_release_iocbq(phba, iocb);
dea3101e
JB
2151 pring->missbufcnt = cnt;
2152 return cnt;
2153 }
2154
2155 INIT_LIST_HEAD(&mp1->list);
2156 /* Allocate buffer to post */
2157 if (cnt > 1) {
2158 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2159 if (mp2)
2160 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2161 &mp2->phys);
98c9ea5c 2162 if (!mp2 || !mp2->virt) {
c9475cb0 2163 kfree(mp2);
dea3101e
JB
2164 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2165 kfree(mp1);
604a3e30 2166 lpfc_sli_release_iocbq(phba, iocb);
dea3101e
JB
2167 pring->missbufcnt = cnt;
2168 return cnt;
2169 }
2170
2171 INIT_LIST_HEAD(&mp2->list);
2172 } else {
2173 mp2 = NULL;
2174 }
2175
2176 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2177 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2178 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2179 icmd->ulpBdeCount = 1;
2180 cnt--;
2181 if (mp2) {
2182 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2183 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2184 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2185 cnt--;
2186 icmd->ulpBdeCount = 2;
2187 }
2188
2189 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2190 icmd->ulpLe = 1;
2191
3772a991
JS
2192 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2193 IOCB_ERROR) {
dea3101e
JB
2194 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2195 kfree(mp1);
2196 cnt++;
2197 if (mp2) {
2198 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2199 kfree(mp2);
2200 cnt++;
2201 }
604a3e30 2202 lpfc_sli_release_iocbq(phba, iocb);
dea3101e 2203 pring->missbufcnt = cnt;
dea3101e
JB
2204 return cnt;
2205 }
dea3101e 2206 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
92d7f7b0 2207 if (mp2)
dea3101e 2208 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea3101e
JB
2209 }
2210 pring->missbufcnt = 0;
2211 return 0;
2212}
2213
e59058c4 2214/**
3621a710 2215 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
e59058c4
JS
2216 * @phba: pointer to lpfc hba data structure.
2217 *
2218 * This routine posts initial receive IOCB buffers to the ELS ring. The
2219 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2220 * set to 64 IOCBs.
2221 *
2222 * Return codes
2223 * 0 - success (currently always success)
2224 **/
dea3101e 2225static int
2e0fef85 2226lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea3101e
JB
2227{
2228 struct lpfc_sli *psli = &phba->sli;
2229
2230 /* Ring 0, ELS / CT buffers */
495a714c 2231 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea3101e
JB
2232 /* Ring 2 - FCP no buffers needed */
2233
2234 return 0;
2235}
2236
2237#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2238
e59058c4 2239/**
3621a710 2240 * lpfc_sha_init - Set up initial array of hash table entries
e59058c4
JS
2241 * @HashResultPointer: pointer to an array as hash table.
2242 *
2243 * This routine sets up the initial values to the array of hash table entries
2244 * for the LC HBAs.
2245 **/
dea3101e
JB
2246static void
2247lpfc_sha_init(uint32_t * HashResultPointer)
2248{
2249 HashResultPointer[0] = 0x67452301;
2250 HashResultPointer[1] = 0xEFCDAB89;
2251 HashResultPointer[2] = 0x98BADCFE;
2252 HashResultPointer[3] = 0x10325476;
2253 HashResultPointer[4] = 0xC3D2E1F0;
2254}
2255
e59058c4 2256/**
3621a710 2257 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
e59058c4
JS
2258 * @HashResultPointer: pointer to an initial/result hash table.
2259 * @HashWorkingPointer: pointer to an working hash table.
2260 *
2261 * This routine iterates an initial hash table pointed by @HashResultPointer
2262 * with the values from the working hash table pointeed by @HashWorkingPointer.
2263 * The results are putting back to the initial hash table, returned through
2264 * the @HashResultPointer as the result hash table.
2265 **/
dea3101e
JB
2266static void
2267lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2268{
2269 int t;
2270 uint32_t TEMP;
2271 uint32_t A, B, C, D, E;
2272 t = 16;
2273 do {
2274 HashWorkingPointer[t] =
2275 S(1,
2276 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2277 8] ^
2278 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2279 } while (++t <= 79);
2280 t = 0;
2281 A = HashResultPointer[0];
2282 B = HashResultPointer[1];
2283 C = HashResultPointer[2];
2284 D = HashResultPointer[3];
2285 E = HashResultPointer[4];
2286
2287 do {
2288 if (t < 20) {
2289 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2290 } else if (t < 40) {
2291 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2292 } else if (t < 60) {
2293 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2294 } else {
2295 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2296 }
2297 TEMP += S(5, A) + E + HashWorkingPointer[t];
2298 E = D;
2299 D = C;
2300 C = S(30, B);
2301 B = A;
2302 A = TEMP;
2303 } while (++t <= 79);
2304
2305 HashResultPointer[0] += A;
2306 HashResultPointer[1] += B;
2307 HashResultPointer[2] += C;
2308 HashResultPointer[3] += D;
2309 HashResultPointer[4] += E;
2310
2311}
2312
e59058c4 2313/**
3621a710 2314 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
e59058c4
JS
2315 * @RandomChallenge: pointer to the entry of host challenge random number array.
2316 * @HashWorking: pointer to the entry of the working hash array.
2317 *
2318 * This routine calculates the working hash array referred by @HashWorking
2319 * from the challenge random numbers associated with the host, referred by
2320 * @RandomChallenge. The result is put into the entry of the working hash
2321 * array and returned by reference through @HashWorking.
2322 **/
dea3101e
JB
2323static void
2324lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2325{
2326 *HashWorking = (*RandomChallenge ^ *HashWorking);
2327}
2328
e59058c4 2329/**
3621a710 2330 * lpfc_hba_init - Perform special handling for LC HBA initialization
e59058c4
JS
2331 * @phba: pointer to lpfc hba data structure.
2332 * @hbainit: pointer to an array of unsigned 32-bit integers.
2333 *
2334 * This routine performs the special handling for LC HBA initialization.
2335 **/
dea3101e
JB
2336void
2337lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2338{
2339 int t;
2340 uint32_t *HashWorking;
2e0fef85 2341 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea3101e 2342
bbfbbbc1 2343 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea3101e
JB
2344 if (!HashWorking)
2345 return;
2346
dea3101e
JB
2347 HashWorking[0] = HashWorking[78] = *pwwnn++;
2348 HashWorking[1] = HashWorking[79] = *pwwnn;
2349
2350 for (t = 0; t < 7; t++)
2351 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2352
2353 lpfc_sha_init(hbainit);
2354 lpfc_sha_iterate(hbainit, HashWorking);
2355 kfree(HashWorking);
2356}
2357
e59058c4 2358/**
3621a710 2359 * lpfc_cleanup - Performs vport cleanups before deleting a vport
e59058c4
JS
2360 * @vport: pointer to a virtual N_Port data structure.
2361 *
2362 * This routine performs the necessary cleanups before deleting the @vport.
2363 * It invokes the discovery state machine to perform necessary state
2364 * transitions and to release the ndlps associated with the @vport. Note,
2365 * the physical port is treated as @vport 0.
2366 **/
87af33fe 2367void
2e0fef85 2368lpfc_cleanup(struct lpfc_vport *vport)
dea3101e 2369{
87af33fe 2370 struct lpfc_hba *phba = vport->phba;
dea3101e 2371 struct lpfc_nodelist *ndlp, *next_ndlp;
a8adb832 2372 int i = 0;
dea3101e 2373
87af33fe
JS
2374 if (phba->link_state > LPFC_LINK_DOWN)
2375 lpfc_port_link_failure(vport);
2376
2377 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
2378 if (!NLP_CHK_NODE_ACT(ndlp)) {
2379 ndlp = lpfc_enable_node(vport, ndlp,
2380 NLP_STE_UNUSED_NODE);
2381 if (!ndlp)
2382 continue;
2383 spin_lock_irq(&phba->ndlp_lock);
2384 NLP_SET_FREE_REQ(ndlp);
2385 spin_unlock_irq(&phba->ndlp_lock);
2386 /* Trigger the release of the ndlp memory */
2387 lpfc_nlp_put(ndlp);
2388 continue;
2389 }
2390 spin_lock_irq(&phba->ndlp_lock);
2391 if (NLP_CHK_FREE_REQ(ndlp)) {
2392 /* The ndlp should not be in memory free mode already */
2393 spin_unlock_irq(&phba->ndlp_lock);
2394 continue;
2395 } else
2396 /* Indicate request for freeing ndlp memory */
2397 NLP_SET_FREE_REQ(ndlp);
2398 spin_unlock_irq(&phba->ndlp_lock);
2399
58da1ffb
JS
2400 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2401 ndlp->nlp_DID == Fabric_DID) {
2402 /* Just free up ndlp with Fabric_DID for vports */
2403 lpfc_nlp_put(ndlp);
2404 continue;
2405 }
2406
eff4a01b
JS
2407 /* take care of nodes in unused state before the state
2408 * machine taking action.
2409 */
2410 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2411 lpfc_nlp_put(ndlp);
2412 continue;
2413 }
2414
87af33fe
JS
2415 if (ndlp->nlp_type & NLP_FABRIC)
2416 lpfc_disc_state_machine(vport, ndlp, NULL,
2417 NLP_EVT_DEVICE_RECOVERY);
e47c9093 2418
87af33fe
JS
2419 lpfc_disc_state_machine(vport, ndlp, NULL,
2420 NLP_EVT_DEVICE_RM);
2421 }
2422
a8adb832
JS
2423 /* At this point, ALL ndlp's should be gone
2424 * because of the previous NLP_EVT_DEVICE_RM.
2425 * Lets wait for this to happen, if needed.
2426 */
87af33fe 2427 while (!list_empty(&vport->fc_nodes)) {
a8adb832 2428 if (i++ > 3000) {
87af33fe 2429 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
a8adb832 2430 "0233 Nodelist not empty\n");
e47c9093
JS
2431 list_for_each_entry_safe(ndlp, next_ndlp,
2432 &vport->fc_nodes, nlp_listp) {
2433 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2434 LOG_NODE,
d7c255b2 2435 "0282 did:x%x ndlp:x%p "
e47c9093
JS
2436 "usgmap:x%x refcnt:%d\n",
2437 ndlp->nlp_DID, (void *)ndlp,
2438 ndlp->nlp_usg_map,
2439 atomic_read(
2440 &ndlp->kref.refcount));
2441 }
a8adb832 2442 break;
87af33fe 2443 }
a8adb832
JS
2444
2445 /* Wait for any activity on ndlps to settle */
2446 msleep(10);
87af33fe 2447 }
1151e3ec 2448 lpfc_cleanup_vports_rrqs(vport, NULL);
dea3101e
JB
2449}
2450
e59058c4 2451/**
3621a710 2452 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
e59058c4
JS
2453 * @vport: pointer to a virtual N_Port data structure.
2454 *
2455 * This routine stops all the timers associated with a @vport. This function
2456 * is invoked before disabling or deleting a @vport. Note that the physical
2457 * port is treated as @vport 0.
2458 **/
92d7f7b0
JS
2459void
2460lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea3101e 2461{
92d7f7b0
JS
2462 del_timer_sync(&vport->els_tmofunc);
2463 del_timer_sync(&vport->fc_fdmitmo);
92494144 2464 del_timer_sync(&vport->delayed_disc_tmo);
92d7f7b0
JS
2465 lpfc_can_disctmo(vport);
2466 return;
dea3101e
JB
2467}
2468
ecfd03c6
JS
2469/**
2470 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2471 * @phba: pointer to lpfc hba data structure.
2472 *
2473 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2474 * caller of this routine should already hold the host lock.
2475 **/
2476void
2477__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2478{
5ac6b303
JS
2479 /* Clear pending FCF rediscovery wait flag */
2480 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2481
ecfd03c6
JS
2482 /* Now, try to stop the timer */
2483 del_timer(&phba->fcf.redisc_wait);
2484}
2485
2486/**
2487 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2488 * @phba: pointer to lpfc hba data structure.
2489 *
2490 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2491 * checks whether the FCF rediscovery wait timer is pending with the host
2492 * lock held before proceeding with disabling the timer and clearing the
2493 * wait timer pendig flag.
2494 **/
2495void
2496lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2497{
2498 spin_lock_irq(&phba->hbalock);
2499 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2500 /* FCF rediscovery timer already fired or stopped */
2501 spin_unlock_irq(&phba->hbalock);
2502 return;
2503 }
2504 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
5ac6b303
JS
2505 /* Clear failover in progress flags */
2506 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
ecfd03c6
JS
2507 spin_unlock_irq(&phba->hbalock);
2508}
2509
e59058c4 2510/**
3772a991 2511 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
e59058c4
JS
2512 * @phba: pointer to lpfc hba data structure.
2513 *
2514 * This routine stops all the timers associated with a HBA. This function is
2515 * invoked before either putting a HBA offline or unloading the driver.
2516 **/
3772a991
JS
2517void
2518lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea3101e 2519{
51ef4c26 2520 lpfc_stop_vport_timers(phba->pport);
2e0fef85 2521 del_timer_sync(&phba->sli.mbox_tmo);
92d7f7b0 2522 del_timer_sync(&phba->fabric_block_timer);
9399627f 2523 del_timer_sync(&phba->eratt_poll);
3772a991 2524 del_timer_sync(&phba->hb_tmofunc);
1151e3ec
JS
2525 if (phba->sli_rev == LPFC_SLI_REV4) {
2526 del_timer_sync(&phba->rrq_tmr);
2527 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2528 }
3772a991
JS
2529 phba->hb_outstanding = 0;
2530
2531 switch (phba->pci_dev_grp) {
2532 case LPFC_PCI_DEV_LP:
2533 /* Stop any LightPulse device specific driver timers */
2534 del_timer_sync(&phba->fcp_poll_timer);
2535 break;
2536 case LPFC_PCI_DEV_OC:
2537 /* Stop any OneConnect device sepcific driver timers */
ecfd03c6 2538 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
3772a991
JS
2539 break;
2540 default:
2541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2542 "0297 Invalid device group (x%x)\n",
2543 phba->pci_dev_grp);
2544 break;
2545 }
2e0fef85 2546 return;
dea3101e
JB
2547}
2548
e59058c4 2549/**
3621a710 2550 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
e59058c4
JS
2551 * @phba: pointer to lpfc hba data structure.
2552 *
2553 * This routine marks a HBA's management interface as blocked. Once the HBA's
2554 * management interface is marked as blocked, all the user space access to
2555 * the HBA, whether they are from sysfs interface or libdfc interface will
2556 * all be blocked. The HBA is set to block the management interface when the
2557 * driver prepares the HBA interface for online or offline.
2558 **/
a6ababd2 2559static void
618a5230 2560lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
a6ababd2
AB
2561{
2562 unsigned long iflag;
6e7288d9
JS
2563 uint8_t actcmd = MBX_HEARTBEAT;
2564 unsigned long timeout;
2565
a6ababd2
AB
2566 spin_lock_irqsave(&phba->hbalock, iflag);
2567 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
618a5230
JS
2568 spin_unlock_irqrestore(&phba->hbalock, iflag);
2569 if (mbx_action == LPFC_MBX_NO_WAIT)
2570 return;
2571 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2572 spin_lock_irqsave(&phba->hbalock, iflag);
a183a15f 2573 if (phba->sli.mbox_active) {
6e7288d9 2574 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
a183a15f
JS
2575 /* Determine how long we might wait for the active mailbox
2576 * command to be gracefully completed by firmware.
2577 */
2578 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2579 phba->sli.mbox_active) * 1000) + jiffies;
2580 }
a6ababd2 2581 spin_unlock_irqrestore(&phba->hbalock, iflag);
a183a15f 2582
6e7288d9
JS
2583 /* Wait for the outstnading mailbox command to complete */
2584 while (phba->sli.mbox_active) {
2585 /* Check active mailbox complete status every 2ms */
2586 msleep(2);
2587 if (time_after(jiffies, timeout)) {
2588 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2589 "2813 Mgmt IO is Blocked %x "
2590 "- mbox cmd %x still active\n",
2591 phba->sli.sli_flag, actcmd);
2592 break;
2593 }
2594 }
a6ababd2
AB
2595}
2596
6b5151fd
JS
2597/**
2598 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2599 * @phba: pointer to lpfc hba data structure.
2600 *
2601 * Allocate RPIs for all active remote nodes. This is needed whenever
2602 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2603 * is to fixup the temporary rpi assignments.
2604 **/
2605void
2606lpfc_sli4_node_prep(struct lpfc_hba *phba)
2607{
2608 struct lpfc_nodelist *ndlp, *next_ndlp;
2609 struct lpfc_vport **vports;
2610 int i;
2611
2612 if (phba->sli_rev != LPFC_SLI_REV4)
2613 return;
2614
2615 vports = lpfc_create_vport_work_array(phba);
2616 if (vports != NULL) {
2617 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2618 if (vports[i]->load_flag & FC_UNLOADING)
2619 continue;
2620
2621 list_for_each_entry_safe(ndlp, next_ndlp,
2622 &vports[i]->fc_nodes,
2623 nlp_listp) {
2624 if (NLP_CHK_NODE_ACT(ndlp))
2625 ndlp->nlp_rpi =
2626 lpfc_sli4_alloc_rpi(phba);
2627 }
2628 }
2629 }
2630 lpfc_destroy_vport_work_array(phba, vports);
2631}
2632
e59058c4 2633/**
3621a710 2634 * lpfc_online - Initialize and bring a HBA online
e59058c4
JS
2635 * @phba: pointer to lpfc hba data structure.
2636 *
2637 * This routine initializes the HBA and brings a HBA online. During this
2638 * process, the management interface is blocked to prevent user space access
2639 * to the HBA interfering with the driver initialization.
2640 *
2641 * Return codes
2642 * 0 - successful
2643 * 1 - failed
2644 **/
dea3101e 2645int
2e0fef85 2646lpfc_online(struct lpfc_hba *phba)
dea3101e 2647{
372bd282 2648 struct lpfc_vport *vport;
549e55cd
JS
2649 struct lpfc_vport **vports;
2650 int i;
16a3a208 2651 bool vpis_cleared = false;
2e0fef85 2652
dea3101e
JB
2653 if (!phba)
2654 return 0;
372bd282 2655 vport = phba->pport;
dea3101e 2656
2e0fef85 2657 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea3101e
JB
2658 return 0;
2659
ed957684 2660 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
e8b62011 2661 "0458 Bring Adapter online\n");
dea3101e 2662
618a5230 2663 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
46fa311e
JS
2664
2665 if (!lpfc_sli_queue_setup(phba)) {
2666 lpfc_unblock_mgmt_io(phba);
dea3101e 2667 return 1;
46fa311e 2668 }
dea3101e 2669
da0436e9
JS
2670 if (phba->sli_rev == LPFC_SLI_REV4) {
2671 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2672 lpfc_unblock_mgmt_io(phba);
2673 return 1;
2674 }
16a3a208
JS
2675 spin_lock_irq(&phba->hbalock);
2676 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2677 vpis_cleared = true;
2678 spin_unlock_irq(&phba->hbalock);
da0436e9
JS
2679 } else {
2680 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2681 lpfc_unblock_mgmt_io(phba);
2682 return 1;
2683 }
46fa311e 2684 }
dea3101e 2685
549e55cd 2686 vports = lpfc_create_vport_work_array(phba);
834f5956 2687 if (vports != NULL) {
da0436e9 2688 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
2689 struct Scsi_Host *shost;
2690 shost = lpfc_shost_from_vport(vports[i]);
2691 spin_lock_irq(shost->host_lock);
2692 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2693 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2694 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
16a3a208 2695 if (phba->sli_rev == LPFC_SLI_REV4) {
1c6834a7 2696 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
16a3a208
JS
2697 if ((vpis_cleared) &&
2698 (vports[i]->port_type !=
2699 LPFC_PHYSICAL_PORT))
2700 vports[i]->vpi = 0;
2701 }
549e55cd
JS
2702 spin_unlock_irq(shost->host_lock);
2703 }
834f5956
AB
2704 }
2705 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 2706
46fa311e 2707 lpfc_unblock_mgmt_io(phba);
dea3101e
JB
2708 return 0;
2709}
2710
e59058c4 2711/**
3621a710 2712 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
e59058c4
JS
2713 * @phba: pointer to lpfc hba data structure.
2714 *
2715 * This routine marks a HBA's management interface as not blocked. Once the
2716 * HBA's management interface is marked as not blocked, all the user space
2717 * access to the HBA, whether they are from sysfs interface or libdfc
2718 * interface will be allowed. The HBA is set to block the management interface
2719 * when the driver prepares the HBA interface for online or offline and then
2720 * set to unblock the management interface afterwards.
2721 **/
46fa311e
JS
2722void
2723lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2724{
2725 unsigned long iflag;
2726
2e0fef85
JS
2727 spin_lock_irqsave(&phba->hbalock, iflag);
2728 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2729 spin_unlock_irqrestore(&phba->hbalock, iflag);
46fa311e
JS
2730}
2731
e59058c4 2732/**
3621a710 2733 * lpfc_offline_prep - Prepare a HBA to be brought offline
e59058c4
JS
2734 * @phba: pointer to lpfc hba data structure.
2735 *
2736 * This routine is invoked to prepare a HBA to be brought offline. It performs
2737 * unregistration login to all the nodes on all vports and flushes the mailbox
2738 * queue to make it ready to be brought offline.
2739 **/
46fa311e 2740void
618a5230 2741lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
46fa311e 2742{
2e0fef85 2743 struct lpfc_vport *vport = phba->pport;
46fa311e 2744 struct lpfc_nodelist *ndlp, *next_ndlp;
87af33fe 2745 struct lpfc_vport **vports;
72100cc4 2746 struct Scsi_Host *shost;
87af33fe 2747 int i;
dea3101e 2748
2e0fef85 2749 if (vport->fc_flag & FC_OFFLINE_MODE)
46fa311e 2750 return;
dea3101e 2751
618a5230 2752 lpfc_block_mgmt_io(phba, mbx_action);
dea3101e
JB
2753
2754 lpfc_linkdown(phba);
2755
87af33fe
JS
2756 /* Issue an unreg_login to all nodes on all vports */
2757 vports = lpfc_create_vport_work_array(phba);
2758 if (vports != NULL) {
da0436e9 2759 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
a8adb832
JS
2760 if (vports[i]->load_flag & FC_UNLOADING)
2761 continue;
72100cc4
JS
2762 shost = lpfc_shost_from_vport(vports[i]);
2763 spin_lock_irq(shost->host_lock);
c868595d 2764 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
695a814e
JS
2765 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2766 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
72100cc4 2767 spin_unlock_irq(shost->host_lock);
695a814e 2768
87af33fe
JS
2769 shost = lpfc_shost_from_vport(vports[i]);
2770 list_for_each_entry_safe(ndlp, next_ndlp,
2771 &vports[i]->fc_nodes,
2772 nlp_listp) {
e47c9093
JS
2773 if (!NLP_CHK_NODE_ACT(ndlp))
2774 continue;
87af33fe
JS
2775 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2776 continue;
2777 if (ndlp->nlp_type & NLP_FABRIC) {
2778 lpfc_disc_state_machine(vports[i], ndlp,
2779 NULL, NLP_EVT_DEVICE_RECOVERY);
2780 lpfc_disc_state_machine(vports[i], ndlp,
2781 NULL, NLP_EVT_DEVICE_RM);
2782 }
2783 spin_lock_irq(shost->host_lock);
2784 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
401ee0c1 2785 spin_unlock_irq(shost->host_lock);
6b5151fd
JS
2786 /*
2787 * Whenever an SLI4 port goes offline, free the
401ee0c1
JS
2788 * RPI. Get a new RPI when the adapter port
2789 * comes back online.
6b5151fd
JS
2790 */
2791 if (phba->sli_rev == LPFC_SLI_REV4)
2792 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
87af33fe
JS
2793 lpfc_unreg_rpi(vports[i], ndlp);
2794 }
2795 }
2796 }
09372820 2797 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 2798
618a5230 2799 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
46fa311e
JS
2800}
2801
e59058c4 2802/**
3621a710 2803 * lpfc_offline - Bring a HBA offline
e59058c4
JS
2804 * @phba: pointer to lpfc hba data structure.
2805 *
2806 * This routine actually brings a HBA offline. It stops all the timers
2807 * associated with the HBA, brings down the SLI layer, and eventually
2808 * marks the HBA as in offline state for the upper layer protocol.
2809 **/
46fa311e 2810void
2e0fef85 2811lpfc_offline(struct lpfc_hba *phba)
46fa311e 2812{
549e55cd
JS
2813 struct Scsi_Host *shost;
2814 struct lpfc_vport **vports;
2815 int i;
46fa311e 2816
549e55cd 2817 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
46fa311e 2818 return;
688a8863 2819
da0436e9
JS
2820 /* stop port and all timers associated with this hba */
2821 lpfc_stop_port(phba);
51ef4c26
JS
2822 vports = lpfc_create_vport_work_array(phba);
2823 if (vports != NULL)
da0436e9 2824 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
51ef4c26 2825 lpfc_stop_vport_timers(vports[i]);
09372820 2826 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0 2827 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
e8b62011 2828 "0460 Bring Adapter offline\n");
dea3101e
JB
2829 /* Bring down the SLI Layer and cleanup. The HBA is offline
2830 now. */
2831 lpfc_sli_hba_down(phba);
92d7f7b0 2832 spin_lock_irq(&phba->hbalock);
7054a606 2833 phba->work_ha = 0;
92d7f7b0 2834 spin_unlock_irq(&phba->hbalock);
549e55cd
JS
2835 vports = lpfc_create_vport_work_array(phba);
2836 if (vports != NULL)
da0436e9 2837 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd 2838 shost = lpfc_shost_from_vport(vports[i]);
549e55cd
JS
2839 spin_lock_irq(shost->host_lock);
2840 vports[i]->work_port_events = 0;
2841 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2842 spin_unlock_irq(shost->host_lock);
2843 }
09372820 2844 lpfc_destroy_vport_work_array(phba, vports);
dea3101e
JB
2845}
2846
e59058c4 2847/**
3621a710 2848 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
e59058c4
JS
2849 * @phba: pointer to lpfc hba data structure.
2850 *
2851 * This routine is to free all the SCSI buffers and IOCBs from the driver
2852 * list back to kernel. It is called from lpfc_pci_remove_one to free
2853 * the internal resources before the device is removed from the system.
e59058c4 2854 **/
8a9d2e80 2855static void
2e0fef85 2856lpfc_scsi_free(struct lpfc_hba *phba)
dea3101e
JB
2857{
2858 struct lpfc_scsi_buf *sb, *sb_next;
2859 struct lpfc_iocbq *io, *io_next;
2860
2e0fef85 2861 spin_lock_irq(&phba->hbalock);
a40fc5f0 2862
dea3101e 2863 /* Release all the lpfc_scsi_bufs maintained by this host. */
a40fc5f0
JS
2864
2865 spin_lock(&phba->scsi_buf_list_put_lock);
2866 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
2867 list) {
dea3101e
JB
2868 list_del(&sb->list);
2869 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
92d7f7b0 2870 sb->dma_handle);
dea3101e
JB
2871 kfree(sb);
2872 phba->total_scsi_bufs--;
2873 }
a40fc5f0
JS
2874 spin_unlock(&phba->scsi_buf_list_put_lock);
2875
2876 spin_lock(&phba->scsi_buf_list_get_lock);
2877 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
2878 list) {
dea3101e
JB
2879 list_del(&sb->list);
2880 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
92d7f7b0 2881 sb->dma_handle);
dea3101e
JB
2882 kfree(sb);
2883 phba->total_scsi_bufs--;
2884 }
a40fc5f0 2885 spin_unlock(&phba->scsi_buf_list_get_lock);
dea3101e
JB
2886
2887 /* Release all the lpfc_iocbq entries maintained by this host. */
2888 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2889 list_del(&io->list);
2890 kfree(io);
2891 phba->total_iocbq_bufs--;
2892 }
6d368e53 2893
2e0fef85 2894 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
2895}
2896
2897/**
2898 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2899 * @phba: pointer to lpfc hba data structure.
2900 *
2901 * This routine first calculates the sizes of the current els and allocated
2902 * scsi sgl lists, and then goes through all sgls to updates the physical
2903 * XRIs assigned due to port function reset. During port initialization, the
2904 * current els and allocated scsi sgl lists are 0s.
2905 *
2906 * Return codes
2907 * 0 - successful (for now, it always returns 0)
2908 **/
2909int
2910lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2911{
2912 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2913 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2914 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2915 LIST_HEAD(els_sgl_list);
2916 LIST_HEAD(scsi_sgl_list);
2917 int rc;
2918
2919 /*
2920 * update on pci function's els xri-sgl list
2921 */
2922 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2923 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2924 /* els xri-sgl expanded */
2925 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2926 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2927 "3157 ELS xri-sgl count increased from "
2928 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2929 els_xri_cnt);
2930 /* allocate the additional els sgls */
2931 for (i = 0; i < xri_cnt; i++) {
2932 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2933 GFP_KERNEL);
2934 if (sglq_entry == NULL) {
2935 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2936 "2562 Failure to allocate an "
2937 "ELS sgl entry:%d\n", i);
2938 rc = -ENOMEM;
2939 goto out_free_mem;
2940 }
2941 sglq_entry->buff_type = GEN_BUFF_TYPE;
2942 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2943 &sglq_entry->phys);
2944 if (sglq_entry->virt == NULL) {
2945 kfree(sglq_entry);
2946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2947 "2563 Failure to allocate an "
2948 "ELS mbuf:%d\n", i);
2949 rc = -ENOMEM;
2950 goto out_free_mem;
2951 }
2952 sglq_entry->sgl = sglq_entry->virt;
2953 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2954 sglq_entry->state = SGL_FREED;
2955 list_add_tail(&sglq_entry->list, &els_sgl_list);
2956 }
38c20673 2957 spin_lock_irq(&phba->hbalock);
8a9d2e80 2958 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
38c20673 2959 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
2960 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2961 /* els xri-sgl shrinked */
2962 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2963 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2964 "3158 ELS xri-sgl count decreased from "
2965 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2966 els_xri_cnt);
2967 spin_lock_irq(&phba->hbalock);
2968 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2969 spin_unlock_irq(&phba->hbalock);
2970 /* release extra els sgls from list */
2971 for (i = 0; i < xri_cnt; i++) {
2972 list_remove_head(&els_sgl_list,
2973 sglq_entry, struct lpfc_sglq, list);
2974 if (sglq_entry) {
2975 lpfc_mbuf_free(phba, sglq_entry->virt,
2976 sglq_entry->phys);
2977 kfree(sglq_entry);
2978 }
2979 }
2980 spin_lock_irq(&phba->hbalock);
2981 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2982 spin_unlock_irq(&phba->hbalock);
2983 } else
2984 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2985 "3163 ELS xri-sgl count unchanged: %d\n",
2986 els_xri_cnt);
2987 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2988
2989 /* update xris to els sgls on the list */
2990 sglq_entry = NULL;
2991 sglq_entry_next = NULL;
2992 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2993 &phba->sli4_hba.lpfc_sgl_list, list) {
2994 lxri = lpfc_sli4_next_xritag(phba);
2995 if (lxri == NO_XRI) {
2996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2997 "2400 Failed to allocate xri for "
2998 "ELS sgl\n");
2999 rc = -ENOMEM;
3000 goto out_free_mem;
3001 }
3002 sglq_entry->sli4_lxritag = lxri;
3003 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3004 }
3005
3006 /*
3007 * update on pci function's allocated scsi xri-sgl list
3008 */
3009 phba->total_scsi_bufs = 0;
3010
3011 /* maximum number of xris available for scsi buffers */
3012 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3013 els_xri_cnt;
3014
3015 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3016 "2401 Current allocated SCSI xri-sgl count:%d, "
3017 "maximum SCSI xri count:%d\n",
3018 phba->sli4_hba.scsi_xri_cnt,
3019 phba->sli4_hba.scsi_xri_max);
3020
a40fc5f0
JS
3021 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3022 spin_lock_irq(&phba->scsi_buf_list_put_lock);
3023 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3024 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
3025 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
3026 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
8a9d2e80
JS
3027
3028 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3029 /* max scsi xri shrinked below the allocated scsi buffers */
3030 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3031 phba->sli4_hba.scsi_xri_max;
3032 /* release the extra allocated scsi buffers */
3033 for (i = 0; i < scsi_xri_cnt; i++) {
3034 list_remove_head(&scsi_sgl_list, psb,
3035 struct lpfc_scsi_buf, list);
3036 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3037 psb->dma_handle);
3038 kfree(psb);
3039 }
a40fc5f0 3040 spin_lock_irq(&phba->scsi_buf_list_get_lock);
8a9d2e80 3041 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
a40fc5f0 3042 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
8a9d2e80
JS
3043 }
3044
3045 /* update xris associated to remaining allocated scsi buffers */
3046 psb = NULL;
3047 psb_next = NULL;
3048 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3049 lxri = lpfc_sli4_next_xritag(phba);
3050 if (lxri == NO_XRI) {
3051 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3052 "2560 Failed to allocate xri for "
3053 "scsi buffer\n");
3054 rc = -ENOMEM;
3055 goto out_free_mem;
3056 }
3057 psb->cur_iocbq.sli4_lxritag = lxri;
3058 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3059 }
a40fc5f0
JS
3060 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3061 spin_lock_irq(&phba->scsi_buf_list_put_lock);
3062 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3063 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
3064 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
3065 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
8a9d2e80 3066
dea3101e 3067 return 0;
8a9d2e80
JS
3068
3069out_free_mem:
3070 lpfc_free_els_sgl_list(phba);
3071 lpfc_scsi_free(phba);
3072 return rc;
dea3101e
JB
3073}
3074
e59058c4 3075/**
3621a710 3076 * lpfc_create_port - Create an FC port
e59058c4
JS
3077 * @phba: pointer to lpfc hba data structure.
3078 * @instance: a unique integer ID to this FC port.
3079 * @dev: pointer to the device data structure.
3080 *
3081 * This routine creates a FC port for the upper layer protocol. The FC port
3082 * can be created on top of either a physical port or a virtual port provided
3083 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3084 * and associates the FC port created before adding the shost into the SCSI
3085 * layer.
3086 *
3087 * Return codes
3088 * @vport - pointer to the virtual N_Port data structure.
3089 * NULL - port create failed.
3090 **/
2e0fef85 3091struct lpfc_vport *
3de2a653 3092lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
47a8617c 3093{
2e0fef85
JS
3094 struct lpfc_vport *vport;
3095 struct Scsi_Host *shost;
3096 int error = 0;
47a8617c 3097
3de2a653
JS
3098 if (dev != &phba->pcidev->dev)
3099 shost = scsi_host_alloc(&lpfc_vport_template,
3100 sizeof(struct lpfc_vport));
3101 else
3102 shost = scsi_host_alloc(&lpfc_template,
3103 sizeof(struct lpfc_vport));
2e0fef85
JS
3104 if (!shost)
3105 goto out;
47a8617c 3106
2e0fef85
JS
3107 vport = (struct lpfc_vport *) shost->hostdata;
3108 vport->phba = phba;
2e0fef85 3109 vport->load_flag |= FC_LOADING;
92d7f7b0 3110 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7f5f3d0d 3111 vport->fc_rscn_flush = 0;
47a8617c 3112
3de2a653 3113 lpfc_get_vport_cfgparam(vport);
2e0fef85
JS
3114 shost->unique_id = instance;
3115 shost->max_id = LPFC_MAX_TARGET;
3de2a653 3116 shost->max_lun = vport->cfg_max_luns;
2e0fef85
JS
3117 shost->this_id = -1;
3118 shost->max_cmd_len = 16;
da0436e9 3119 if (phba->sli_rev == LPFC_SLI_REV4) {
28baac74 3120 shost->dma_boundary =
cb5172ea 3121 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
da0436e9
JS
3122 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3123 }
81301a9b 3124
47a8617c 3125 /*
2e0fef85
JS
3126 * Set initial can_queue value since 0 is no longer supported and
3127 * scsi_add_host will fail. This will be adjusted later based on the
3128 * max xri value determined in hba setup.
47a8617c 3129 */
2e0fef85 3130 shost->can_queue = phba->cfg_hba_queue_depth - 10;
3de2a653 3131 if (dev != &phba->pcidev->dev) {
92d7f7b0
JS
3132 shost->transportt = lpfc_vport_transport_template;
3133 vport->port_type = LPFC_NPIV_PORT;
3134 } else {
3135 shost->transportt = lpfc_transport_template;
3136 vport->port_type = LPFC_PHYSICAL_PORT;
3137 }
47a8617c 3138
2e0fef85
JS
3139 /* Initialize all internally managed lists. */
3140 INIT_LIST_HEAD(&vport->fc_nodes);
da0436e9 3141 INIT_LIST_HEAD(&vport->rcv_buffer_list);
2e0fef85 3142 spin_lock_init(&vport->work_port_lock);
47a8617c 3143
2e0fef85
JS
3144 init_timer(&vport->fc_disctmo);
3145 vport->fc_disctmo.function = lpfc_disc_timeout;
92d7f7b0 3146 vport->fc_disctmo.data = (unsigned long)vport;
47a8617c 3147
2e0fef85
JS
3148 init_timer(&vport->fc_fdmitmo);
3149 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
92d7f7b0 3150 vport->fc_fdmitmo.data = (unsigned long)vport;
47a8617c 3151
2e0fef85
JS
3152 init_timer(&vport->els_tmofunc);
3153 vport->els_tmofunc.function = lpfc_els_timeout;
92d7f7b0 3154 vport->els_tmofunc.data = (unsigned long)vport;
92494144
JS
3155
3156 init_timer(&vport->delayed_disc_tmo);
3157 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3158 vport->delayed_disc_tmo.data = (unsigned long)vport;
3159
d139b9bd 3160 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2e0fef85
JS
3161 if (error)
3162 goto out_put_shost;
47a8617c 3163
549e55cd 3164 spin_lock_irq(&phba->hbalock);
2e0fef85 3165 list_add_tail(&vport->listentry, &phba->port_list);
549e55cd 3166 spin_unlock_irq(&phba->hbalock);
2e0fef85 3167 return vport;
47a8617c 3168
2e0fef85
JS
3169out_put_shost:
3170 scsi_host_put(shost);
3171out:
3172 return NULL;
47a8617c
JS
3173}
3174
e59058c4 3175/**
3621a710 3176 * destroy_port - destroy an FC port
e59058c4
JS
3177 * @vport: pointer to an lpfc virtual N_Port data structure.
3178 *
3179 * This routine destroys a FC port from the upper layer protocol. All the
3180 * resources associated with the port are released.
3181 **/
2e0fef85
JS
3182void
3183destroy_port(struct lpfc_vport *vport)
47a8617c 3184{
92d7f7b0
JS
3185 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3186 struct lpfc_hba *phba = vport->phba;
47a8617c 3187
858c9f6c 3188 lpfc_debugfs_terminate(vport);
92d7f7b0
JS
3189 fc_remove_host(shost);
3190 scsi_remove_host(shost);
47a8617c 3191
92d7f7b0
JS
3192 spin_lock_irq(&phba->hbalock);
3193 list_del_init(&vport->listentry);
3194 spin_unlock_irq(&phba->hbalock);
47a8617c 3195
92d7f7b0 3196 lpfc_cleanup(vport);
47a8617c 3197 return;
47a8617c
JS
3198}
3199
e59058c4 3200/**
3621a710 3201 * lpfc_get_instance - Get a unique integer ID
e59058c4
JS
3202 *
3203 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3204 * uses the kernel idr facility to perform the task.
3205 *
3206 * Return codes:
3207 * instance - a unique integer ID allocated as the new instance.
3208 * -1 - lpfc get instance failed.
3209 **/
92d7f7b0
JS
3210int
3211lpfc_get_instance(void)
3212{
ab516036
TH
3213 int ret;
3214
3215 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3216 return ret < 0 ? -1 : ret;
47a8617c
JS
3217}
3218
e59058c4 3219/**
3621a710 3220 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
e59058c4
JS
3221 * @shost: pointer to SCSI host data structure.
3222 * @time: elapsed time of the scan in jiffies.
3223 *
3224 * This routine is called by the SCSI layer with a SCSI host to determine
3225 * whether the scan host is finished.
3226 *
3227 * Note: there is no scan_start function as adapter initialization will have
3228 * asynchronously kicked off the link initialization.
3229 *
3230 * Return codes
3231 * 0 - SCSI host scan is not over yet.
3232 * 1 - SCSI host scan is over.
3233 **/
47a8617c
JS
3234int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3235{
2e0fef85
JS
3236 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3237 struct lpfc_hba *phba = vport->phba;
858c9f6c 3238 int stat = 0;
47a8617c 3239
858c9f6c
JS
3240 spin_lock_irq(shost->host_lock);
3241
51ef4c26 3242 if (vport->load_flag & FC_UNLOADING) {
858c9f6c
JS
3243 stat = 1;
3244 goto finished;
3245 }
256ec0d0 3246 if (time >= msecs_to_jiffies(30 * 1000)) {
2e0fef85 3247 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
3248 "0461 Scanning longer than 30 "
3249 "seconds. Continuing initialization\n");
858c9f6c 3250 stat = 1;
47a8617c 3251 goto finished;
2e0fef85 3252 }
256ec0d0
JS
3253 if (time >= msecs_to_jiffies(15 * 1000) &&
3254 phba->link_state <= LPFC_LINK_DOWN) {
2e0fef85 3255 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
3256 "0465 Link down longer than 15 "
3257 "seconds. Continuing initialization\n");
858c9f6c 3258 stat = 1;
47a8617c 3259 goto finished;
2e0fef85 3260 }
47a8617c 3261
2e0fef85 3262 if (vport->port_state != LPFC_VPORT_READY)
858c9f6c 3263 goto finished;
2e0fef85 3264 if (vport->num_disc_nodes || vport->fc_prli_sent)
858c9f6c 3265 goto finished;
256ec0d0 3266 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
858c9f6c 3267 goto finished;
2e0fef85 3268 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
858c9f6c
JS
3269 goto finished;
3270
3271 stat = 1;
47a8617c
JS
3272
3273finished:
858c9f6c
JS
3274 spin_unlock_irq(shost->host_lock);
3275 return stat;
92d7f7b0 3276}
47a8617c 3277
e59058c4 3278/**
3621a710 3279 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
e59058c4
JS
3280 * @shost: pointer to SCSI host data structure.
3281 *
3282 * This routine initializes a given SCSI host attributes on a FC port. The
3283 * SCSI host can be either on top of a physical port or a virtual port.
3284 **/
92d7f7b0
JS
3285void lpfc_host_attrib_init(struct Scsi_Host *shost)
3286{
3287 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3288 struct lpfc_hba *phba = vport->phba;
47a8617c 3289 /*
2e0fef85 3290 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
47a8617c
JS
3291 */
3292
2e0fef85
JS
3293 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3294 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
47a8617c
JS
3295 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3296
3297 memset(fc_host_supported_fc4s(shost), 0,
2e0fef85 3298 sizeof(fc_host_supported_fc4s(shost)));
47a8617c
JS
3299 fc_host_supported_fc4s(shost)[2] = 1;
3300 fc_host_supported_fc4s(shost)[7] = 1;
3301
92d7f7b0
JS
3302 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3303 sizeof fc_host_symbolic_name(shost));
47a8617c
JS
3304
3305 fc_host_supported_speeds(shost) = 0;
88a2cfbb
JS
3306 if (phba->lmt & LMT_16Gb)
3307 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
47a8617c
JS
3308 if (phba->lmt & LMT_10Gb)
3309 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
a8adb832
JS
3310 if (phba->lmt & LMT_8Gb)
3311 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
47a8617c
JS
3312 if (phba->lmt & LMT_4Gb)
3313 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3314 if (phba->lmt & LMT_2Gb)
3315 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3316 if (phba->lmt & LMT_1Gb)
3317 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3318
3319 fc_host_maxframe_size(shost) =
2e0fef85
JS
3320 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3321 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
47a8617c 3322
0af5d708
MC
3323 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3324
47a8617c
JS
3325 /* This value is also unchanging */
3326 memset(fc_host_active_fc4s(shost), 0,
2e0fef85 3327 sizeof(fc_host_active_fc4s(shost)));
47a8617c
JS
3328 fc_host_active_fc4s(shost)[2] = 1;
3329 fc_host_active_fc4s(shost)[7] = 1;
3330
92d7f7b0 3331 fc_host_max_npiv_vports(shost) = phba->max_vpi;
47a8617c 3332 spin_lock_irq(shost->host_lock);
51ef4c26 3333 vport->load_flag &= ~FC_LOADING;
47a8617c 3334 spin_unlock_irq(shost->host_lock);
47a8617c 3335}
dea3101e 3336
e59058c4 3337/**
da0436e9 3338 * lpfc_stop_port_s3 - Stop SLI3 device port
e59058c4
JS
3339 * @phba: pointer to lpfc hba data structure.
3340 *
da0436e9
JS
3341 * This routine is invoked to stop an SLI3 device port, it stops the device
3342 * from generating interrupts and stops the device driver's timers for the
3343 * device.
e59058c4 3344 **/
da0436e9
JS
3345static void
3346lpfc_stop_port_s3(struct lpfc_hba *phba)
db2378e0 3347{
da0436e9
JS
3348 /* Clear all interrupt enable conditions */
3349 writel(0, phba->HCregaddr);
3350 readl(phba->HCregaddr); /* flush */
3351 /* Clear all pending interrupts */
3352 writel(0xffffffff, phba->HAregaddr);
3353 readl(phba->HAregaddr); /* flush */
db2378e0 3354
da0436e9
JS
3355 /* Reset some HBA SLI setup states */
3356 lpfc_stop_hba_timers(phba);
3357 phba->pport->work_port_events = 0;
3358}
db2378e0 3359
da0436e9
JS
3360/**
3361 * lpfc_stop_port_s4 - Stop SLI4 device port
3362 * @phba: pointer to lpfc hba data structure.
3363 *
3364 * This routine is invoked to stop an SLI4 device port, it stops the device
3365 * from generating interrupts and stops the device driver's timers for the
3366 * device.
3367 **/
3368static void
3369lpfc_stop_port_s4(struct lpfc_hba *phba)
3370{
3371 /* Reset some HBA SLI4 setup states */
3372 lpfc_stop_hba_timers(phba);
3373 phba->pport->work_port_events = 0;
3374 phba->sli4_hba.intr_enable = 0;
da0436e9 3375}
9399627f 3376
da0436e9
JS
3377/**
3378 * lpfc_stop_port - Wrapper function for stopping hba port
3379 * @phba: Pointer to HBA context object.
3380 *
3381 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3382 * the API jump table function pointer from the lpfc_hba struct.
3383 **/
3384void
3385lpfc_stop_port(struct lpfc_hba *phba)
3386{
3387 phba->lpfc_stop_port(phba);
3388}
db2378e0 3389
ecfd03c6
JS
3390/**
3391 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3392 * @phba: Pointer to hba for which this call is being executed.
3393 *
3394 * This routine starts the timer waiting for the FCF rediscovery to complete.
3395 **/
3396void
3397lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3398{
3399 unsigned long fcf_redisc_wait_tmo =
3400 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3401 /* Start fcf rediscovery wait period timer */
3402 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3403 spin_lock_irq(&phba->hbalock);
3404 /* Allow action to new fcf asynchronous event */
3405 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3406 /* Mark the FCF rediscovery pending state */
3407 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3408 spin_unlock_irq(&phba->hbalock);
3409}
3410
3411/**
3412 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3413 * @ptr: Map to lpfc_hba data structure pointer.
3414 *
3415 * This routine is invoked when waiting for FCF table rediscover has been
3416 * timed out. If new FCF record(s) has (have) been discovered during the
3417 * wait period, a new FCF event shall be added to the FCOE async event
3418 * list, and then worker thread shall be waked up for processing from the
3419 * worker thread context.
3420 **/
3421void
3422lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3423{
3424 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3425
3426 /* Don't send FCF rediscovery event if timer cancelled */
3427 spin_lock_irq(&phba->hbalock);
3428 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3429 spin_unlock_irq(&phba->hbalock);
3430 return;
3431 }
3432 /* Clear FCF rediscovery timer pending flag */
3433 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3434 /* FCF rediscovery event to worker thread */
3435 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3436 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 3437 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 3438 "2776 FCF rediscover quiescent timer expired\n");
ecfd03c6
JS
3439 /* wake up worker thread */
3440 lpfc_worker_wake_up(phba);
3441}
3442
e59058c4 3443/**
da0436e9 3444 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
e59058c4 3445 * @phba: pointer to lpfc hba data structure.
da0436e9 3446 * @acqe_link: pointer to the async link completion queue entry.
e59058c4 3447 *
da0436e9
JS
3448 * This routine is to parse the SLI4 link-attention link fault code and
3449 * translate it into the base driver's read link attention mailbox command
3450 * status.
3451 *
3452 * Return: Link-attention status in terms of base driver's coding.
e59058c4 3453 **/
da0436e9
JS
3454static uint16_t
3455lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3456 struct lpfc_acqe_link *acqe_link)
db2378e0 3457{
da0436e9 3458 uint16_t latt_fault;
9399627f 3459
da0436e9
JS
3460 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3461 case LPFC_ASYNC_LINK_FAULT_NONE:
3462 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3463 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3464 latt_fault = 0;
3465 break;
3466 default:
3467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3468 "0398 Invalid link fault code: x%x\n",
3469 bf_get(lpfc_acqe_link_fault, acqe_link));
3470 latt_fault = MBXERR_ERROR;
3471 break;
3472 }
3473 return latt_fault;
db2378e0
JS
3474}
3475
5b75da2f 3476/**
da0436e9 3477 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
5b75da2f 3478 * @phba: pointer to lpfc hba data structure.
da0436e9 3479 * @acqe_link: pointer to the async link completion queue entry.
5b75da2f 3480 *
da0436e9
JS
3481 * This routine is to parse the SLI4 link attention type and translate it
3482 * into the base driver's link attention type coding.
5b75da2f 3483 *
da0436e9
JS
3484 * Return: Link attention type in terms of base driver's coding.
3485 **/
3486static uint8_t
3487lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3488 struct lpfc_acqe_link *acqe_link)
5b75da2f 3489{
da0436e9 3490 uint8_t att_type;
5b75da2f 3491
da0436e9
JS
3492 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3493 case LPFC_ASYNC_LINK_STATUS_DOWN:
3494 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
76a95d75 3495 att_type = LPFC_ATT_LINK_DOWN;
da0436e9
JS
3496 break;
3497 case LPFC_ASYNC_LINK_STATUS_UP:
3498 /* Ignore physical link up events - wait for logical link up */
76a95d75 3499 att_type = LPFC_ATT_RESERVED;
da0436e9
JS
3500 break;
3501 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
76a95d75 3502 att_type = LPFC_ATT_LINK_UP;
da0436e9
JS
3503 break;
3504 default:
3505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3506 "0399 Invalid link attention type: x%x\n",
3507 bf_get(lpfc_acqe_link_status, acqe_link));
76a95d75 3508 att_type = LPFC_ATT_RESERVED;
da0436e9 3509 break;
5b75da2f 3510 }
da0436e9 3511 return att_type;
5b75da2f
JS
3512}
3513
3514/**
da0436e9 3515 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
5b75da2f 3516 * @phba: pointer to lpfc hba data structure.
da0436e9 3517 * @acqe_link: pointer to the async link completion queue entry.
5b75da2f 3518 *
da0436e9
JS
3519 * This routine is to parse the SLI4 link-attention link speed and translate
3520 * it into the base driver's link-attention link speed coding.
3521 *
3522 * Return: Link-attention link speed in terms of base driver's coding.
3523 **/
3524static uint8_t
3525lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3526 struct lpfc_acqe_link *acqe_link)
5b75da2f 3527{
da0436e9
JS
3528 uint8_t link_speed;
3529
3530 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3531 case LPFC_ASYNC_LINK_SPEED_ZERO:
da0436e9 3532 case LPFC_ASYNC_LINK_SPEED_10MBPS:
da0436e9 3533 case LPFC_ASYNC_LINK_SPEED_100MBPS:
76a95d75 3534 link_speed = LPFC_LINK_SPEED_UNKNOWN;
da0436e9
JS
3535 break;
3536 case LPFC_ASYNC_LINK_SPEED_1GBPS:
76a95d75 3537 link_speed = LPFC_LINK_SPEED_1GHZ;
da0436e9
JS
3538 break;
3539 case LPFC_ASYNC_LINK_SPEED_10GBPS:
76a95d75 3540 link_speed = LPFC_LINK_SPEED_10GHZ;
da0436e9
JS
3541 break;
3542 default:
3543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3544 "0483 Invalid link-attention link speed: x%x\n",
3545 bf_get(lpfc_acqe_link_speed, acqe_link));
76a95d75 3546 link_speed = LPFC_LINK_SPEED_UNKNOWN;
da0436e9
JS
3547 break;
3548 }
3549 return link_speed;
3550}
3551
8b68cd52
JS
3552/**
3553 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3554 * @phba: pointer to lpfc hba data structure.
3555 *
3556 * This routine is to get an SLI3 FC port's link speed in Mbps.
3557 *
3558 * Return: link speed in terms of Mbps.
3559 **/
3560uint32_t
3561lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3562{
3563 uint32_t link_speed;
3564
3565 if (!lpfc_is_link_up(phba))
3566 return 0;
3567
3568 switch (phba->fc_linkspeed) {
3569 case LPFC_LINK_SPEED_1GHZ:
3570 link_speed = 1000;
3571 break;
3572 case LPFC_LINK_SPEED_2GHZ:
3573 link_speed = 2000;
3574 break;
3575 case LPFC_LINK_SPEED_4GHZ:
3576 link_speed = 4000;
3577 break;
3578 case LPFC_LINK_SPEED_8GHZ:
3579 link_speed = 8000;
3580 break;
3581 case LPFC_LINK_SPEED_10GHZ:
3582 link_speed = 10000;
3583 break;
3584 case LPFC_LINK_SPEED_16GHZ:
3585 link_speed = 16000;
3586 break;
3587 default:
3588 link_speed = 0;
3589 }
3590 return link_speed;
3591}
3592
3593/**
3594 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3595 * @phba: pointer to lpfc hba data structure.
3596 * @evt_code: asynchronous event code.
3597 * @speed_code: asynchronous event link speed code.
3598 *
3599 * This routine is to parse the giving SLI4 async event link speed code into
3600 * value of Mbps for the link speed.
3601 *
3602 * Return: link speed in terms of Mbps.
3603 **/
3604static uint32_t
3605lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3606 uint8_t speed_code)
3607{
3608 uint32_t port_speed;
3609
3610 switch (evt_code) {
3611 case LPFC_TRAILER_CODE_LINK:
3612 switch (speed_code) {
3613 case LPFC_EVT_CODE_LINK_NO_LINK:
3614 port_speed = 0;
3615 break;
3616 case LPFC_EVT_CODE_LINK_10_MBIT:
3617 port_speed = 10;
3618 break;
3619 case LPFC_EVT_CODE_LINK_100_MBIT:
3620 port_speed = 100;
3621 break;
3622 case LPFC_EVT_CODE_LINK_1_GBIT:
3623 port_speed = 1000;
3624 break;
3625 case LPFC_EVT_CODE_LINK_10_GBIT:
3626 port_speed = 10000;
3627 break;
3628 default:
3629 port_speed = 0;
3630 }
3631 break;
3632 case LPFC_TRAILER_CODE_FC:
3633 switch (speed_code) {
3634 case LPFC_EVT_CODE_FC_NO_LINK:
3635 port_speed = 0;
3636 break;
3637 case LPFC_EVT_CODE_FC_1_GBAUD:
3638 port_speed = 1000;
3639 break;
3640 case LPFC_EVT_CODE_FC_2_GBAUD:
3641 port_speed = 2000;
3642 break;
3643 case LPFC_EVT_CODE_FC_4_GBAUD:
3644 port_speed = 4000;
3645 break;
3646 case LPFC_EVT_CODE_FC_8_GBAUD:
3647 port_speed = 8000;
3648 break;
3649 case LPFC_EVT_CODE_FC_10_GBAUD:
3650 port_speed = 10000;
3651 break;
3652 case LPFC_EVT_CODE_FC_16_GBAUD:
3653 port_speed = 16000;
3654 break;
3655 default:
3656 port_speed = 0;
3657 }
3658 break;
3659 default:
3660 port_speed = 0;
3661 }
3662 return port_speed;
3663}
3664
da0436e9 3665/**
70f3c073 3666 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
da0436e9
JS
3667 * @phba: pointer to lpfc hba data structure.
3668 * @acqe_link: pointer to the async link completion queue entry.
3669 *
70f3c073 3670 * This routine is to handle the SLI4 asynchronous FCoE link event.
da0436e9
JS
3671 **/
3672static void
3673lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3674 struct lpfc_acqe_link *acqe_link)
3675{
3676 struct lpfc_dmabuf *mp;
3677 LPFC_MBOXQ_t *pmb;
3678 MAILBOX_t *mb;
76a95d75 3679 struct lpfc_mbx_read_top *la;
da0436e9 3680 uint8_t att_type;
76a95d75 3681 int rc;
da0436e9
JS
3682
3683 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
76a95d75 3684 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
da0436e9 3685 return;
32b9793f 3686 phba->fcoe_eventtag = acqe_link->event_tag;
da0436e9
JS
3687 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3688 if (!pmb) {
3689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3690 "0395 The mboxq allocation failed\n");
3691 return;
3692 }
3693 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3694 if (!mp) {
3695 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3696 "0396 The lpfc_dmabuf allocation failed\n");
3697 goto out_free_pmb;
3698 }
3699 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3700 if (!mp->virt) {
3701 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3702 "0397 The mbuf allocation failed\n");
3703 goto out_free_dmabuf;
3704 }
3705
3706 /* Cleanup any outstanding ELS commands */
3707 lpfc_els_flush_all_cmd(phba);
3708
3709 /* Block ELS IOCBs until we have done process link event */
3710 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3711
3712 /* Update link event statistics */
3713 phba->sli.slistat.link_event++;
3714
76a95d75
JS
3715 /* Create lpfc_handle_latt mailbox command from link ACQE */
3716 lpfc_read_topology(phba, pmb, mp);
3717 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
da0436e9
JS
3718 pmb->vport = phba->pport;
3719
da0436e9
JS
3720 /* Keep the link status for extra SLI4 state machine reference */
3721 phba->sli4_hba.link_state.speed =
8b68cd52
JS
3722 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3723 bf_get(lpfc_acqe_link_speed, acqe_link));
da0436e9
JS
3724 phba->sli4_hba.link_state.duplex =
3725 bf_get(lpfc_acqe_link_duplex, acqe_link);
3726 phba->sli4_hba.link_state.status =
3727 bf_get(lpfc_acqe_link_status, acqe_link);
70f3c073
JS
3728 phba->sli4_hba.link_state.type =
3729 bf_get(lpfc_acqe_link_type, acqe_link);
3730 phba->sli4_hba.link_state.number =
3731 bf_get(lpfc_acqe_link_number, acqe_link);
da0436e9
JS
3732 phba->sli4_hba.link_state.fault =
3733 bf_get(lpfc_acqe_link_fault, acqe_link);
65467b6b 3734 phba->sli4_hba.link_state.logical_speed =
8b68cd52
JS
3735 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3736
70f3c073 3737 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
c31098ce
JS
3738 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3739 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3740 "Logical speed:%dMbps Fault:%d\n",
70f3c073
JS
3741 phba->sli4_hba.link_state.speed,
3742 phba->sli4_hba.link_state.topology,
3743 phba->sli4_hba.link_state.status,
3744 phba->sli4_hba.link_state.type,
3745 phba->sli4_hba.link_state.number,
8b68cd52 3746 phba->sli4_hba.link_state.logical_speed,
70f3c073 3747 phba->sli4_hba.link_state.fault);
76a95d75
JS
3748 /*
3749 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3750 * topology info. Note: Optional for non FC-AL ports.
3751 */
3752 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3753 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3754 if (rc == MBX_NOT_FINISHED)
3755 goto out_free_dmabuf;
3756 return;
3757 }
3758 /*
3759 * For FCoE Mode: fill in all the topology information we need and call
3760 * the READ_TOPOLOGY completion routine to continue without actually
3761 * sending the READ_TOPOLOGY mailbox command to the port.
3762 */
3763 /* Parse and translate status field */
3764 mb = &pmb->u.mb;
3765 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3766
3767 /* Parse and translate link attention fields */
3768 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3769 la->eventTag = acqe_link->event_tag;
3770 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3771 bf_set(lpfc_mbx_read_top_link_spd, la,
3772 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3773
3774 /* Fake the the following irrelvant fields */
3775 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3776 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3777 bf_set(lpfc_mbx_read_top_il, la, 0);
3778 bf_set(lpfc_mbx_read_top_pb, la, 0);
3779 bf_set(lpfc_mbx_read_top_fa, la, 0);
3780 bf_set(lpfc_mbx_read_top_mm, la, 0);
da0436e9
JS
3781
3782 /* Invoke the lpfc_handle_latt mailbox command callback function */
76a95d75 3783 lpfc_mbx_cmpl_read_topology(phba, pmb);
da0436e9 3784
5b75da2f 3785 return;
da0436e9
JS
3786
3787out_free_dmabuf:
3788 kfree(mp);
3789out_free_pmb:
3790 mempool_free(pmb, phba->mbox_mem_pool);
3791}
3792
70f3c073
JS
3793/**
3794 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3795 * @phba: pointer to lpfc hba data structure.
3796 * @acqe_fc: pointer to the async fc completion queue entry.
3797 *
3798 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3799 * that the event was received and then issue a read_topology mailbox command so
3800 * that the rest of the driver will treat it the same as SLI3.
3801 **/
3802static void
3803lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3804{
3805 struct lpfc_dmabuf *mp;
3806 LPFC_MBOXQ_t *pmb;
3807 int rc;
3808
3809 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3810 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3811 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3812 "2895 Non FC link Event detected.(%d)\n",
3813 bf_get(lpfc_trailer_type, acqe_fc));
3814 return;
3815 }
3816 /* Keep the link status for extra SLI4 state machine reference */
3817 phba->sli4_hba.link_state.speed =
8b68cd52
JS
3818 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3819 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
70f3c073
JS
3820 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3821 phba->sli4_hba.link_state.topology =
3822 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3823 phba->sli4_hba.link_state.status =
3824 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3825 phba->sli4_hba.link_state.type =
3826 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3827 phba->sli4_hba.link_state.number =
3828 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3829 phba->sli4_hba.link_state.fault =
3830 bf_get(lpfc_acqe_link_fault, acqe_fc);
3831 phba->sli4_hba.link_state.logical_speed =
8b68cd52 3832 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
70f3c073
JS
3833 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3834 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3835 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3836 "%dMbps Fault:%d\n",
3837 phba->sli4_hba.link_state.speed,
3838 phba->sli4_hba.link_state.topology,
3839 phba->sli4_hba.link_state.status,
3840 phba->sli4_hba.link_state.type,
3841 phba->sli4_hba.link_state.number,
8b68cd52 3842 phba->sli4_hba.link_state.logical_speed,
70f3c073
JS
3843 phba->sli4_hba.link_state.fault);
3844 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3845 if (!pmb) {
3846 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3847 "2897 The mboxq allocation failed\n");
3848 return;
3849 }
3850 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3851 if (!mp) {
3852 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3853 "2898 The lpfc_dmabuf allocation failed\n");
3854 goto out_free_pmb;
3855 }
3856 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3857 if (!mp->virt) {
3858 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3859 "2899 The mbuf allocation failed\n");
3860 goto out_free_dmabuf;
3861 }
3862
3863 /* Cleanup any outstanding ELS commands */
3864 lpfc_els_flush_all_cmd(phba);
3865
3866 /* Block ELS IOCBs until we have done process link event */
3867 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3868
3869 /* Update link event statistics */
3870 phba->sli.slistat.link_event++;
3871
3872 /* Create lpfc_handle_latt mailbox command from link ACQE */
3873 lpfc_read_topology(phba, pmb, mp);
3874 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3875 pmb->vport = phba->pport;
3876
3877 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3878 if (rc == MBX_NOT_FINISHED)
3879 goto out_free_dmabuf;
3880 return;
3881
3882out_free_dmabuf:
3883 kfree(mp);
3884out_free_pmb:
3885 mempool_free(pmb, phba->mbox_mem_pool);
3886}
3887
3888/**
3889 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3890 * @phba: pointer to lpfc hba data structure.
3891 * @acqe_fc: pointer to the async SLI completion queue entry.
3892 *
3893 * This routine is to handle the SLI4 asynchronous SLI events.
3894 **/
3895static void
3896lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3897{
4b8bae08 3898 char port_name;
8c1312e1 3899 char message[128];
4b8bae08
JS
3900 uint8_t status;
3901 struct lpfc_acqe_misconfigured_event *misconfigured;
3902
3903 /* special case misconfigured event as it contains data for all ports */
3904 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3905 LPFC_SLI_INTF_IF_TYPE_2) ||
3906 (bf_get(lpfc_trailer_type, acqe_sli) !=
3907 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3908 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3909 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3910 "x%08x SLI Event Type:%d\n",
3911 acqe_sli->event_data1, acqe_sli->event_data2,
3912 bf_get(lpfc_trailer_type, acqe_sli));
3913 return;
3914 }
3915
3916 port_name = phba->Port[0];
3917 if (port_name == 0x00)
3918 port_name = '?'; /* get port name is empty */
3919
3920 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3921 &acqe_sli->event_data1;
3922
3923 /* fetch the status for this port */
3924 switch (phba->sli4_hba.lnk_info.lnk_no) {
3925 case LPFC_LINK_NUMBER_0:
3926 status = bf_get(lpfc_sli_misconfigured_port0,
3927 &misconfigured->theEvent);
3928 break;
3929 case LPFC_LINK_NUMBER_1:
3930 status = bf_get(lpfc_sli_misconfigured_port1,
3931 &misconfigured->theEvent);
3932 break;
3933 case LPFC_LINK_NUMBER_2:
3934 status = bf_get(lpfc_sli_misconfigured_port2,
3935 &misconfigured->theEvent);
3936 break;
3937 case LPFC_LINK_NUMBER_3:
3938 status = bf_get(lpfc_sli_misconfigured_port3,
3939 &misconfigured->theEvent);
3940 break;
3941 default:
3942 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3943 break;
3944 }
3945
3946 switch (status) {
3947 case LPFC_SLI_EVENT_STATUS_VALID:
3948 return; /* no message if the sfp is okay */
3949 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
292098be
JS
3950 sprintf(message, "Optics faulted/incorrectly installed/not " \
3951 "installed - Reseat optics, if issue not "
3952 "resolved, replace.");
4b8bae08
JS
3953 break;
3954 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3955 sprintf(message,
292098be
JS
3956 "Optics of two types installed - Remove one optic or " \
3957 "install matching pair of optics.");
4b8bae08
JS
3958 break;
3959 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
292098be
JS
3960 sprintf(message, "Incompatible optics - Replace with " \
3961 "compatible optics for card to function.");
4b8bae08
JS
3962 break;
3963 default:
3964 /* firmware is reporting a status we don't know about */
3965 sprintf(message, "Unknown event status x%02x", status);
3966 break;
3967 }
3968
3969 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3970 "3176 Misconfigured Physical Port - "
3971 "Port Name %c %s\n", port_name, message);
70f3c073
JS
3972}
3973
fc2b989b
JS
3974/**
3975 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3976 * @vport: pointer to vport data structure.
3977 *
3978 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3979 * response to a CVL event.
3980 *
3981 * Return the pointer to the ndlp with the vport if successful, otherwise
3982 * return NULL.
3983 **/
3984static struct lpfc_nodelist *
3985lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3986{
3987 struct lpfc_nodelist *ndlp;
3988 struct Scsi_Host *shost;
3989 struct lpfc_hba *phba;
3990
3991 if (!vport)
3992 return NULL;
fc2b989b
JS
3993 phba = vport->phba;
3994 if (!phba)
3995 return NULL;
78730cfe
JS
3996 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3997 if (!ndlp) {
3998 /* Cannot find existing Fabric ndlp, so allocate a new one */
3999 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4000 if (!ndlp)
4001 return 0;
4002 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4003 /* Set the node type */
4004 ndlp->nlp_type |= NLP_FABRIC;
4005 /* Put ndlp onto node list */
4006 lpfc_enqueue_node(vport, ndlp);
4007 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4008 /* re-setup ndlp without removing from node list */
4009 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4010 if (!ndlp)
4011 return 0;
4012 }
63e801ce
JS
4013 if ((phba->pport->port_state < LPFC_FLOGI) &&
4014 (phba->pport->port_state != LPFC_VPORT_FAILED))
fc2b989b
JS
4015 return NULL;
4016 /* If virtual link is not yet instantiated ignore CVL */
63e801ce
JS
4017 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4018 && (vport->port_state != LPFC_VPORT_FAILED))
fc2b989b
JS
4019 return NULL;
4020 shost = lpfc_shost_from_vport(vport);
4021 if (!shost)
4022 return NULL;
4023 lpfc_linkdown_port(vport);
4024 lpfc_cleanup_pending_mbox(vport);
4025 spin_lock_irq(shost->host_lock);
4026 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4027 spin_unlock_irq(shost->host_lock);
4028
4029 return ndlp;
4030}
4031
4032/**
4033 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4034 * @vport: pointer to lpfc hba data structure.
4035 *
4036 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4037 * response to a FCF dead event.
4038 **/
4039static void
4040lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4041{
4042 struct lpfc_vport **vports;
4043 int i;
4044
4045 vports = lpfc_create_vport_work_array(phba);
4046 if (vports)
4047 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4048 lpfc_sli4_perform_vport_cvl(vports[i]);
4049 lpfc_destroy_vport_work_array(phba, vports);
4050}
4051
25aee407
JS
4052/**
4053 * lpfc_sli4_perform_inuse_fcf_recovery - Perform inuse fcf recovery
4054 * @vport: pointer to lpfc hba data structure.
4055 *
4056 * This routine is to perform FCF recovery when the in-use FCF either dead or
4057 * got modified.
4058 **/
4059static void
4060lpfc_sli4_perform_inuse_fcf_recovery(struct lpfc_hba *phba,
4061 struct lpfc_acqe_fip *acqe_fip)
4062{
4063 int rc;
4064
4065 spin_lock_irq(&phba->hbalock);
4066 /* Mark the fast failover process in progress */
4067 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4068 spin_unlock_irq(&phba->hbalock);
4069
4070 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4071 "2771 Start FCF fast failover process due to in-use "
4072 "FCF DEAD/MODIFIED event: evt_tag:x%x, index:x%x\n",
4073 acqe_fip->event_tag, acqe_fip->index);
4074 rc = lpfc_sli4_redisc_fcf_table(phba);
4075 if (rc) {
4076 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4077 "2772 Issue FCF rediscover mabilbox command "
4078 "failed, fail through to FCF dead event\n");
4079 spin_lock_irq(&phba->hbalock);
4080 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4081 spin_unlock_irq(&phba->hbalock);
4082 /*
4083 * Last resort will fail over by treating this as a link
4084 * down to FCF registration.
4085 */
4086 lpfc_sli4_fcf_dead_failthrough(phba);
4087 } else {
4088 /* Reset FCF roundrobin bmask for new discovery */
4089 lpfc_sli4_clear_fcf_rr_bmask(phba);
4090 /*
4091 * Handling fast FCF failover to a DEAD FCF event is
4092 * considered equalivant to receiving CVL to all vports.
4093 */
4094 lpfc_sli4_perform_all_vport_cvl(phba);
4095 }
4096}
4097
da0436e9 4098/**
76a95d75 4099 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
da0436e9
JS
4100 * @phba: pointer to lpfc hba data structure.
4101 * @acqe_link: pointer to the async fcoe completion queue entry.
4102 *
4103 * This routine is to handle the SLI4 asynchronous fcoe event.
4104 **/
4105static void
76a95d75 4106lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
70f3c073 4107 struct lpfc_acqe_fip *acqe_fip)
da0436e9 4108{
70f3c073 4109 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
da0436e9 4110 int rc;
6669f9bb
JS
4111 struct lpfc_vport *vport;
4112 struct lpfc_nodelist *ndlp;
4113 struct Scsi_Host *shost;
695a814e
JS
4114 int active_vlink_present;
4115 struct lpfc_vport **vports;
4116 int i;
da0436e9 4117
70f3c073
JS
4118 phba->fc_eventTag = acqe_fip->event_tag;
4119 phba->fcoe_eventtag = acqe_fip->event_tag;
da0436e9 4120 switch (event_type) {
70f3c073
JS
4121 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4122 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4123 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
999d813f
JS
4124 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4125 LOG_DISCOVERY,
a93ff37a
JS
4126 "2546 New FCF event, evt_tag:x%x, "
4127 "index:x%x\n",
70f3c073
JS
4128 acqe_fip->event_tag,
4129 acqe_fip->index);
999d813f
JS
4130 else
4131 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4132 LOG_DISCOVERY,
a93ff37a
JS
4133 "2788 FCF param modified event, "
4134 "evt_tag:x%x, index:x%x\n",
70f3c073
JS
4135 acqe_fip->event_tag,
4136 acqe_fip->index);
38b92ef8 4137 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
0c9ab6f5
JS
4138 /*
4139 * During period of FCF discovery, read the FCF
4140 * table record indexed by the event to update
a93ff37a 4141 * FCF roundrobin failover eligible FCF bmask.
0c9ab6f5
JS
4142 */
4143 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4144 LOG_DISCOVERY,
a93ff37a
JS
4145 "2779 Read FCF (x%x) for updating "
4146 "roundrobin FCF failover bmask\n",
70f3c073
JS
4147 acqe_fip->index);
4148 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
0c9ab6f5 4149 }
38b92ef8
JS
4150
4151 /* If the FCF discovery is in progress, do nothing. */
3804dc84 4152 spin_lock_irq(&phba->hbalock);
a93ff37a 4153 if (phba->hba_flag & FCF_TS_INPROG) {
38b92ef8
JS
4154 spin_unlock_irq(&phba->hbalock);
4155 break;
4156 }
4157 /* If fast FCF failover rescan event is pending, do nothing */
4158 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4159 spin_unlock_irq(&phba->hbalock);
4160 break;
4161 }
4162
25aee407
JS
4163 /* If FCF has been in discovered state, perform rediscovery
4164 * only if the FCF with the same index of the in-use FCF got
4165 * modified during normal operation. Otherwise, do nothing.
4166 */
4167 if (phba->pport->port_state > LPFC_FLOGI) {
3804dc84 4168 spin_unlock_irq(&phba->hbalock);
25aee407
JS
4169 if (phba->fcf.current_rec.fcf_indx ==
4170 acqe_fip->index) {
4171 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
4172 "3300 In-use FCF (%d) "
4173 "modified, perform FCF "
4174 "rediscovery\n",
4175 acqe_fip->index);
4176 lpfc_sli4_perform_inuse_fcf_recovery(phba,
4177 acqe_fip);
4178 }
3804dc84
JS
4179 break;
4180 }
4181 spin_unlock_irq(&phba->hbalock);
38b92ef8 4182
0c9ab6f5
JS
4183 /* Otherwise, scan the entire FCF table and re-discover SAN */
4184 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
a93ff37a
JS
4185 "2770 Start FCF table scan per async FCF "
4186 "event, evt_tag:x%x, index:x%x\n",
70f3c073 4187 acqe_fip->event_tag, acqe_fip->index);
0c9ab6f5
JS
4188 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4189 LPFC_FCOE_FCF_GET_FIRST);
da0436e9 4190 if (rc)
0c9ab6f5
JS
4191 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4192 "2547 Issue FCF scan read FCF mailbox "
a93ff37a 4193 "command failed (x%x)\n", rc);
da0436e9
JS
4194 break;
4195
70f3c073 4196 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
da0436e9 4197 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e4e74273 4198 "2548 FCF Table full count 0x%x tag 0x%x\n",
70f3c073
JS
4199 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4200 acqe_fip->event_tag);
da0436e9
JS
4201 break;
4202
70f3c073 4203 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
80c17849 4204 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
0c9ab6f5 4205 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
a93ff37a 4206 "2549 FCF (x%x) disconnected from network, "
70f3c073 4207 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
38b92ef8
JS
4208 /*
4209 * If we are in the middle of FCF failover process, clear
4210 * the corresponding FCF bit in the roundrobin bitmap.
da0436e9 4211 */
fc2b989b 4212 spin_lock_irq(&phba->hbalock);
0c9ab6f5 4213 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
fc2b989b 4214 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 4215 /* Update FLOGI FCF failover eligible FCF bmask */
70f3c073 4216 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
fc2b989b
JS
4217 break;
4218 }
38b92ef8
JS
4219 spin_unlock_irq(&phba->hbalock);
4220
4221 /* If the event is not for currently used fcf do nothing */
70f3c073 4222 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
38b92ef8
JS
4223 break;
4224
4225 /*
4226 * Otherwise, request the port to rediscover the entire FCF
4227 * table for a fast recovery from case that the current FCF
4228 * is no longer valid as we are not in the middle of FCF
4229 * failover process already.
4230 */
25aee407 4231 lpfc_sli4_perform_inuse_fcf_recovery(phba, acqe_fip);
da0436e9 4232 break;
70f3c073 4233 case LPFC_FIP_EVENT_TYPE_CVL:
80c17849 4234 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
0c9ab6f5 4235 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
6669f9bb 4236 "2718 Clear Virtual Link Received for VPI 0x%x"
70f3c073 4237 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
6d368e53 4238
6669f9bb 4239 vport = lpfc_find_vport_by_vpid(phba,
5248a749 4240 acqe_fip->index);
fc2b989b 4241 ndlp = lpfc_sli4_perform_vport_cvl(vport);
6669f9bb
JS
4242 if (!ndlp)
4243 break;
695a814e
JS
4244 active_vlink_present = 0;
4245
4246 vports = lpfc_create_vport_work_array(phba);
4247 if (vports) {
4248 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4249 i++) {
4250 if ((!(vports[i]->fc_flag &
4251 FC_VPORT_CVL_RCVD)) &&
4252 (vports[i]->port_state > LPFC_FDISC)) {
4253 active_vlink_present = 1;
4254 break;
4255 }
4256 }
4257 lpfc_destroy_vport_work_array(phba, vports);
4258 }
4259
4260 if (active_vlink_present) {
4261 /*
4262 * If there are other active VLinks present,
4263 * re-instantiate the Vlink using FDISC.
4264 */
256ec0d0
JS
4265 mod_timer(&ndlp->nlp_delayfunc,
4266 jiffies + msecs_to_jiffies(1000));
fc2b989b 4267 shost = lpfc_shost_from_vport(vport);
6669f9bb
JS
4268 spin_lock_irq(shost->host_lock);
4269 ndlp->nlp_flag |= NLP_DELAY_TMO;
4270 spin_unlock_irq(shost->host_lock);
695a814e
JS
4271 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4272 vport->port_state = LPFC_FDISC;
4273 } else {
ecfd03c6
JS
4274 /*
4275 * Otherwise, we request port to rediscover
4276 * the entire FCF table for a fast recovery
4277 * from possible case that the current FCF
0c9ab6f5
JS
4278 * is no longer valid if we are not already
4279 * in the FCF failover process.
ecfd03c6 4280 */
fc2b989b 4281 spin_lock_irq(&phba->hbalock);
0c9ab6f5 4282 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
fc2b989b
JS
4283 spin_unlock_irq(&phba->hbalock);
4284 break;
4285 }
4286 /* Mark the fast failover process in progress */
0c9ab6f5 4287 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
fc2b989b 4288 spin_unlock_irq(&phba->hbalock);
0c9ab6f5
JS
4289 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4290 LOG_DISCOVERY,
a93ff37a 4291 "2773 Start FCF failover per CVL, "
70f3c073 4292 "evt_tag:x%x\n", acqe_fip->event_tag);
ecfd03c6 4293 rc = lpfc_sli4_redisc_fcf_table(phba);
fc2b989b 4294 if (rc) {
0c9ab6f5
JS
4295 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4296 LOG_DISCOVERY,
4297 "2774 Issue FCF rediscover "
4298 "mabilbox command failed, "
4299 "through to CVL event\n");
fc2b989b 4300 spin_lock_irq(&phba->hbalock);
0c9ab6f5 4301 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b 4302 spin_unlock_irq(&phba->hbalock);
ecfd03c6
JS
4303 /*
4304 * Last resort will be re-try on the
4305 * the current registered FCF entry.
4306 */
4307 lpfc_retry_pport_discovery(phba);
38b92ef8
JS
4308 } else
4309 /*
4310 * Reset FCF roundrobin bmask for new
4311 * discovery.
4312 */
7d791df7 4313 lpfc_sli4_clear_fcf_rr_bmask(phba);
6669f9bb
JS
4314 }
4315 break;
da0436e9
JS
4316 default:
4317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4318 "0288 Unknown FCoE event type 0x%x event tag "
70f3c073 4319 "0x%x\n", event_type, acqe_fip->event_tag);
da0436e9
JS
4320 break;
4321 }
4322}
4323
4324/**
4325 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4326 * @phba: pointer to lpfc hba data structure.
4327 * @acqe_link: pointer to the async dcbx completion queue entry.
4328 *
4329 * This routine is to handle the SLI4 asynchronous dcbx event.
4330 **/
4331static void
4332lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4333 struct lpfc_acqe_dcbx *acqe_dcbx)
4334{
4d9ab994 4335 phba->fc_eventTag = acqe_dcbx->event_tag;
da0436e9
JS
4336 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4337 "0290 The SLI4 DCBX asynchronous event is not "
4338 "handled yet\n");
4339}
4340
b19a061a
JS
4341/**
4342 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4343 * @phba: pointer to lpfc hba data structure.
4344 * @acqe_link: pointer to the async grp5 completion queue entry.
4345 *
4346 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4347 * is an asynchronous notified of a logical link speed change. The Port
4348 * reports the logical link speed in units of 10Mbps.
4349 **/
4350static void
4351lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4352 struct lpfc_acqe_grp5 *acqe_grp5)
4353{
4354 uint16_t prev_ll_spd;
4355
4356 phba->fc_eventTag = acqe_grp5->event_tag;
4357 phba->fcoe_eventtag = acqe_grp5->event_tag;
4358 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4359 phba->sli4_hba.link_state.logical_speed =
8b68cd52 4360 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
b19a061a
JS
4361 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4362 "2789 GRP5 Async Event: Updating logical link speed "
8b68cd52
JS
4363 "from %dMbps to %dMbps\n", prev_ll_spd,
4364 phba->sli4_hba.link_state.logical_speed);
b19a061a
JS
4365}
4366
da0436e9
JS
4367/**
4368 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4369 * @phba: pointer to lpfc hba data structure.
4370 *
4371 * This routine is invoked by the worker thread to process all the pending
4372 * SLI4 asynchronous events.
4373 **/
4374void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4375{
4376 struct lpfc_cq_event *cq_event;
4377
4378 /* First, declare the async event has been handled */
4379 spin_lock_irq(&phba->hbalock);
4380 phba->hba_flag &= ~ASYNC_EVENT;
4381 spin_unlock_irq(&phba->hbalock);
4382 /* Now, handle all the async events */
4383 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4384 /* Get the first event from the head of the event queue */
4385 spin_lock_irq(&phba->hbalock);
4386 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4387 cq_event, struct lpfc_cq_event, list);
4388 spin_unlock_irq(&phba->hbalock);
4389 /* Process the asynchronous event */
4390 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4391 case LPFC_TRAILER_CODE_LINK:
4392 lpfc_sli4_async_link_evt(phba,
4393 &cq_event->cqe.acqe_link);
4394 break;
4395 case LPFC_TRAILER_CODE_FCOE:
70f3c073 4396 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
da0436e9
JS
4397 break;
4398 case LPFC_TRAILER_CODE_DCBX:
4399 lpfc_sli4_async_dcbx_evt(phba,
4400 &cq_event->cqe.acqe_dcbx);
4401 break;
b19a061a
JS
4402 case LPFC_TRAILER_CODE_GRP5:
4403 lpfc_sli4_async_grp5_evt(phba,
4404 &cq_event->cqe.acqe_grp5);
4405 break;
70f3c073
JS
4406 case LPFC_TRAILER_CODE_FC:
4407 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4408 break;
4409 case LPFC_TRAILER_CODE_SLI:
4410 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4411 break;
da0436e9
JS
4412 default:
4413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4414 "1804 Invalid asynchrous event code: "
4415 "x%x\n", bf_get(lpfc_trailer_code,
4416 &cq_event->cqe.mcqe_cmpl));
4417 break;
4418 }
4419 /* Free the completion event processed to the free pool */
4420 lpfc_sli4_cq_event_release(phba, cq_event);
4421 }
4422}
4423
ecfd03c6
JS
4424/**
4425 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4426 * @phba: pointer to lpfc hba data structure.
4427 *
4428 * This routine is invoked by the worker thread to process FCF table
4429 * rediscovery pending completion event.
4430 **/
4431void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4432{
4433 int rc;
4434
4435 spin_lock_irq(&phba->hbalock);
4436 /* Clear FCF rediscovery timeout event */
4437 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4438 /* Clear driver fast failover FCF record flag */
4439 phba->fcf.failover_rec.flag = 0;
4440 /* Set state for FCF fast failover */
4441 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4442 spin_unlock_irq(&phba->hbalock);
4443
4444 /* Scan FCF table from the first entry to re-discover SAN */
0c9ab6f5 4445 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
a93ff37a 4446 "2777 Start post-quiescent FCF table scan\n");
0c9ab6f5 4447 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
ecfd03c6 4448 if (rc)
0c9ab6f5
JS
4449 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4450 "2747 Issue FCF scan read FCF mailbox "
4451 "command failed 0x%x\n", rc);
ecfd03c6
JS
4452}
4453
da0436e9
JS
4454/**
4455 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4456 * @phba: pointer to lpfc hba data structure.
4457 * @dev_grp: The HBA PCI-Device group number.
4458 *
4459 * This routine is invoked to set up the per HBA PCI-Device group function
4460 * API jump table entries.
4461 *
4462 * Return: 0 if success, otherwise -ENODEV
4463 **/
4464int
4465lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4466{
4467 int rc;
4468
4469 /* Set up lpfc PCI-device group */
4470 phba->pci_dev_grp = dev_grp;
4471
4472 /* The LPFC_PCI_DEV_OC uses SLI4 */
4473 if (dev_grp == LPFC_PCI_DEV_OC)
4474 phba->sli_rev = LPFC_SLI_REV4;
4475
4476 /* Set up device INIT API function jump table */
4477 rc = lpfc_init_api_table_setup(phba, dev_grp);
4478 if (rc)
4479 return -ENODEV;
4480 /* Set up SCSI API function jump table */
4481 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4482 if (rc)
4483 return -ENODEV;
4484 /* Set up SLI API function jump table */
4485 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4486 if (rc)
4487 return -ENODEV;
4488 /* Set up MBOX API function jump table */
4489 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4490 if (rc)
4491 return -ENODEV;
4492
4493 return 0;
5b75da2f
JS
4494}
4495
4496/**
3621a710 4497 * lpfc_log_intr_mode - Log the active interrupt mode
5b75da2f
JS
4498 * @phba: pointer to lpfc hba data structure.
4499 * @intr_mode: active interrupt mode adopted.
4500 *
4501 * This routine it invoked to log the currently used active interrupt mode
4502 * to the device.
3772a991
JS
4503 **/
4504static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5b75da2f
JS
4505{
4506 switch (intr_mode) {
4507 case 0:
4508 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4509 "0470 Enable INTx interrupt mode.\n");
4510 break;
4511 case 1:
4512 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4513 "0481 Enabled MSI interrupt mode.\n");
4514 break;
4515 case 2:
4516 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4517 "0480 Enabled MSI-X interrupt mode.\n");
4518 break;
4519 default:
4520 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4521 "0482 Illegal interrupt mode.\n");
4522 break;
4523 }
4524 return;
4525}
4526
5b75da2f 4527/**
3772a991 4528 * lpfc_enable_pci_dev - Enable a generic PCI device.
5b75da2f
JS
4529 * @phba: pointer to lpfc hba data structure.
4530 *
3772a991
JS
4531 * This routine is invoked to enable the PCI device that is common to all
4532 * PCI devices.
5b75da2f
JS
4533 *
4534 * Return codes
af901ca1 4535 * 0 - successful
3772a991 4536 * other values - error
5b75da2f 4537 **/
3772a991
JS
4538static int
4539lpfc_enable_pci_dev(struct lpfc_hba *phba)
5b75da2f 4540{
3772a991 4541 struct pci_dev *pdev;
079b5c91 4542 int bars = 0;
5b75da2f 4543
3772a991
JS
4544 /* Obtain PCI device reference */
4545 if (!phba->pcidev)
4546 goto out_error;
4547 else
4548 pdev = phba->pcidev;
4549 /* Select PCI BARs */
4550 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4551 /* Enable PCI device */
4552 if (pci_enable_device_mem(pdev))
4553 goto out_error;
4554 /* Request PCI resource for the device */
4555 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4556 goto out_disable_device;
4557 /* Set up device as PCI master and save state for EEH */
4558 pci_set_master(pdev);
4559 pci_try_set_mwi(pdev);
4560 pci_save_state(pdev);
5b75da2f 4561
0558056c
JS
4562 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4563 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4564 pdev->needs_freset = 1;
4565
3772a991 4566 return 0;
5b75da2f 4567
3772a991
JS
4568out_disable_device:
4569 pci_disable_device(pdev);
4570out_error:
079b5c91
JS
4571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4572 "1401 Failed to enable pci device, bars:x%x\n", bars);
3772a991 4573 return -ENODEV;
5b75da2f
JS
4574}
4575
4576/**
3772a991 4577 * lpfc_disable_pci_dev - Disable a generic PCI device.
5b75da2f
JS
4578 * @phba: pointer to lpfc hba data structure.
4579 *
3772a991
JS
4580 * This routine is invoked to disable the PCI device that is common to all
4581 * PCI devices.
5b75da2f
JS
4582 **/
4583static void
3772a991 4584lpfc_disable_pci_dev(struct lpfc_hba *phba)
5b75da2f 4585{
3772a991
JS
4586 struct pci_dev *pdev;
4587 int bars;
5b75da2f 4588
3772a991
JS
4589 /* Obtain PCI device reference */
4590 if (!phba->pcidev)
4591 return;
4592 else
4593 pdev = phba->pcidev;
4594 /* Select PCI BARs */
4595 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4596 /* Release PCI resource and disable PCI device */
4597 pci_release_selected_regions(pdev, bars);
4598 pci_disable_device(pdev);
4599 /* Null out PCI private reference to driver */
4600 pci_set_drvdata(pdev, NULL);
5b75da2f
JS
4601
4602 return;
4603}
4604
e59058c4 4605/**
3772a991
JS
4606 * lpfc_reset_hba - Reset a hba
4607 * @phba: pointer to lpfc hba data structure.
e59058c4 4608 *
3772a991
JS
4609 * This routine is invoked to reset a hba device. It brings the HBA
4610 * offline, performs a board restart, and then brings the board back
4611 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4612 * on outstanding mailbox commands.
e59058c4 4613 **/
3772a991
JS
4614void
4615lpfc_reset_hba(struct lpfc_hba *phba)
dea3101e 4616{
3772a991
JS
4617 /* If resets are disabled then set error state and return. */
4618 if (!phba->cfg_enable_hba_reset) {
4619 phba->link_state = LPFC_HBA_ERROR;
4620 return;
4621 }
618a5230 4622 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
3772a991
JS
4623 lpfc_offline(phba);
4624 lpfc_sli_brdrestart(phba);
4625 lpfc_online(phba);
4626 lpfc_unblock_mgmt_io(phba);
4627}
dea3101e 4628
0a96e975
JS
4629/**
4630 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4631 * @phba: pointer to lpfc hba data structure.
4632 *
4633 * This function enables the PCI SR-IOV virtual functions to a physical
4634 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4635 * enable the number of virtual functions to the physical function. As
4636 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4637 * API call does not considered as an error condition for most of the device.
4638 **/
4639uint16_t
4640lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4641{
4642 struct pci_dev *pdev = phba->pcidev;
4643 uint16_t nr_virtfn;
4644 int pos;
4645
0a96e975
JS
4646 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4647 if (pos == 0)
4648 return 0;
4649
4650 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4651 return nr_virtfn;
4652}
4653
912e3acd
JS
4654/**
4655 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4656 * @phba: pointer to lpfc hba data structure.
4657 * @nr_vfn: number of virtual functions to be enabled.
4658 *
4659 * This function enables the PCI SR-IOV virtual functions to a physical
4660 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4661 * enable the number of virtual functions to the physical function. As
4662 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4663 * API call does not considered as an error condition for most of the device.
4664 **/
4665int
4666lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4667{
4668 struct pci_dev *pdev = phba->pcidev;
0a96e975 4669 uint16_t max_nr_vfn;
912e3acd
JS
4670 int rc;
4671
0a96e975
JS
4672 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4673 if (nr_vfn > max_nr_vfn) {
4674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4675 "3057 Requested vfs (%d) greater than "
4676 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4677 return -EINVAL;
4678 }
4679
912e3acd
JS
4680 rc = pci_enable_sriov(pdev, nr_vfn);
4681 if (rc) {
4682 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4683 "2806 Failed to enable sriov on this device "
4684 "with vfn number nr_vf:%d, rc:%d\n",
4685 nr_vfn, rc);
4686 } else
4687 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4688 "2807 Successful enable sriov on this device "
4689 "with vfn number nr_vf:%d\n", nr_vfn);
4690 return rc;
4691}
4692
3772a991
JS
4693/**
4694 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4695 * @phba: pointer to lpfc hba data structure.
4696 *
4697 * This routine is invoked to set up the driver internal resources specific to
4698 * support the SLI-3 HBA device it attached to.
4699 *
4700 * Return codes
af901ca1 4701 * 0 - successful
3772a991
JS
4702 * other values - error
4703 **/
4704static int
4705lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4706{
4707 struct lpfc_sli *psli;
912e3acd 4708 int rc;
dea3101e 4709
2e0fef85 4710 /*
3772a991 4711 * Initialize timers used by driver
2e0fef85 4712 */
dea3101e 4713
3772a991 4714 /* Heartbeat timer */
858c9f6c
JS
4715 init_timer(&phba->hb_tmofunc);
4716 phba->hb_tmofunc.function = lpfc_hb_timeout;
4717 phba->hb_tmofunc.data = (unsigned long)phba;
4718
dea3101e 4719 psli = &phba->sli;
3772a991 4720 /* MBOX heartbeat timer */
dea3101e
JB
4721 init_timer(&psli->mbox_tmo);
4722 psli->mbox_tmo.function = lpfc_mbox_timeout;
2e0fef85 4723 psli->mbox_tmo.data = (unsigned long) phba;
3772a991 4724 /* FCP polling mode timer */
875fbdfe
JSEC
4725 init_timer(&phba->fcp_poll_timer);
4726 phba->fcp_poll_timer.function = lpfc_poll_timeout;
2e0fef85 4727 phba->fcp_poll_timer.data = (unsigned long) phba;
3772a991 4728 /* Fabric block timer */
92d7f7b0
JS
4729 init_timer(&phba->fabric_block_timer);
4730 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4731 phba->fabric_block_timer.data = (unsigned long) phba;
3772a991 4732 /* EA polling mode timer */
9399627f
JS
4733 init_timer(&phba->eratt_poll);
4734 phba->eratt_poll.function = lpfc_poll_eratt;
4735 phba->eratt_poll.data = (unsigned long) phba;
dea3101e 4736
3772a991
JS
4737 /* Host attention work mask setup */
4738 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4739 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
dea3101e 4740
3772a991
JS
4741 /* Get all the module params for configuring this host */
4742 lpfc_get_cfgparam(phba);
49198b37
JS
4743 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4744 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4745 /* check for menlo minimum sg count */
4746 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4747 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4748 }
4749
2a76a283
JS
4750 if (!phba->sli.ring)
4751 phba->sli.ring = (struct lpfc_sli_ring *)
4752 kzalloc(LPFC_SLI3_MAX_RING *
4753 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4754 if (!phba->sli.ring)
4755 return -ENOMEM;
4756
dea3101e 4757 /*
96f7077f 4758 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
3772a991 4759 * used to create the sg_dma_buf_pool must be dynamically calculated.
dea3101e 4760 */
3772a991 4761
96f7077f
JS
4762 /* Initialize the host templates the configured values. */
4763 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4764 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4765
4766 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
3772a991 4767 if (phba->cfg_enable_bg) {
96f7077f
JS
4768 /*
4769 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4770 * the FCP rsp, and a BDE for each. Sice we have no control
4771 * over how many protection data segments the SCSI Layer
4772 * will hand us (ie: there could be one for every block
4773 * in the IO), we just allocate enough BDEs to accomidate
4774 * our max amount and we need to limit lpfc_sg_seg_cnt to
4775 * minimize the risk of running out.
4776 */
4777 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4778 sizeof(struct fcp_rsp) +
4779 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4780
4781 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4782 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4783
4784 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4785 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4786 } else {
4787 /*
4788 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4789 * the FCP rsp, a BDE for each, and a BDE for up to
4790 * cfg_sg_seg_cnt data segments.
4791 */
4792 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4793 sizeof(struct fcp_rsp) +
4794 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4795
4796 /* Total BDEs in BPL for scsi_sg_list */
4797 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
901a920f 4798 }
dea3101e 4799
96f7077f
JS
4800 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4801 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4802 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4803 phba->cfg_total_seg_cnt);
dea3101e 4804
3772a991
JS
4805 phba->max_vpi = LPFC_MAX_VPI;
4806 /* This will be set to correct value after config_port mbox */
4807 phba->max_vports = 0;
dea3101e 4808
3772a991
JS
4809 /*
4810 * Initialize the SLI Layer to run with lpfc HBAs.
4811 */
4812 lpfc_sli_setup(phba);
4813 lpfc_sli_queue_setup(phba);
ed957684 4814
3772a991
JS
4815 /* Allocate device driver memory */
4816 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4817 return -ENOMEM;
51ef4c26 4818
912e3acd
JS
4819 /*
4820 * Enable sr-iov virtual functions if supported and configured
4821 * through the module parameter.
4822 */
4823 if (phba->cfg_sriov_nr_virtfn > 0) {
4824 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4825 phba->cfg_sriov_nr_virtfn);
4826 if (rc) {
4827 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4828 "2808 Requested number of SR-IOV "
4829 "virtual functions (%d) is not "
4830 "supported\n",
4831 phba->cfg_sriov_nr_virtfn);
4832 phba->cfg_sriov_nr_virtfn = 0;
4833 }
4834 }
4835
3772a991
JS
4836 return 0;
4837}
ed957684 4838
3772a991
JS
4839/**
4840 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4841 * @phba: pointer to lpfc hba data structure.
4842 *
4843 * This routine is invoked to unset the driver internal resources set up
4844 * specific for supporting the SLI-3 HBA device it attached to.
4845 **/
4846static void
4847lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4848{
4849 /* Free device driver memory allocated */
4850 lpfc_mem_free_all(phba);
3163f725 4851
3772a991
JS
4852 return;
4853}
dea3101e 4854
3772a991 4855/**
da0436e9 4856 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3772a991
JS
4857 * @phba: pointer to lpfc hba data structure.
4858 *
da0436e9
JS
4859 * This routine is invoked to set up the driver internal resources specific to
4860 * support the SLI-4 HBA device it attached to.
3772a991
JS
4861 *
4862 * Return codes
af901ca1 4863 * 0 - successful
da0436e9 4864 * other values - error
3772a991
JS
4865 **/
4866static int
da0436e9 4867lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3772a991 4868{
7bb03bbf 4869 struct lpfc_vector_map_info *cpup;
da0436e9 4870 struct lpfc_sli *psli;
28baac74 4871 LPFC_MBOXQ_t *mboxq;
96f7077f 4872 int rc, i, hbq_count, max_buf_size;
28baac74
JS
4873 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4874 struct lpfc_mqe *mqe;
09294d46 4875 int longs;
da0436e9
JS
4876
4877 /* Before proceed, wait for POST done and device ready */
4878 rc = lpfc_sli4_post_status_check(phba);
4879 if (rc)
4880 return -ENODEV;
4881
3772a991 4882 /*
da0436e9 4883 * Initialize timers used by driver
3772a991 4884 */
3772a991 4885
da0436e9
JS
4886 /* Heartbeat timer */
4887 init_timer(&phba->hb_tmofunc);
4888 phba->hb_tmofunc.function = lpfc_hb_timeout;
4889 phba->hb_tmofunc.data = (unsigned long)phba;
19ca7609
JS
4890 init_timer(&phba->rrq_tmr);
4891 phba->rrq_tmr.function = lpfc_rrq_timeout;
4892 phba->rrq_tmr.data = (unsigned long)phba;
3772a991 4893
da0436e9
JS
4894 psli = &phba->sli;
4895 /* MBOX heartbeat timer */
4896 init_timer(&psli->mbox_tmo);
4897 psli->mbox_tmo.function = lpfc_mbox_timeout;
4898 psli->mbox_tmo.data = (unsigned long) phba;
4899 /* Fabric block timer */
4900 init_timer(&phba->fabric_block_timer);
4901 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4902 phba->fabric_block_timer.data = (unsigned long) phba;
4903 /* EA polling mode timer */
4904 init_timer(&phba->eratt_poll);
4905 phba->eratt_poll.function = lpfc_poll_eratt;
4906 phba->eratt_poll.data = (unsigned long) phba;
ecfd03c6
JS
4907 /* FCF rediscover timer */
4908 init_timer(&phba->fcf.redisc_wait);
4909 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4910 phba->fcf.redisc_wait.data = (unsigned long)phba;
4911
7ad20aa9
JS
4912 /*
4913 * Control structure for handling external multi-buffer mailbox
4914 * command pass-through.
4915 */
4916 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4917 sizeof(struct lpfc_mbox_ext_buf_ctx));
4918 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4919
da0436e9
JS
4920 /*
4921 * We need to do a READ_CONFIG mailbox command here before
4922 * calling lpfc_get_cfgparam. For VFs this will report the
4923 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4924 * All of the resources allocated
4925 * for this Port are tied to these values.
4926 */
4927 /* Get all the module params for configuring this host */
4928 lpfc_get_cfgparam(phba);
4929 phba->max_vpi = LPFC_MAX_VPI;
67d12733
JS
4930
4931 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
4932 phba->cfg_fcp_io_channel = phba->cfg_fcp_eq_count;
4933
da0436e9
JS
4934 /* This will be set to correct value after the read_config mbox */
4935 phba->max_vports = 0;
3772a991 4936
da0436e9
JS
4937 /* Program the default value of vlan_id and fc_map */
4938 phba->valid_vlan = 0;
4939 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4940 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4941 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3772a991 4942
2a76a283
JS
4943 /*
4944 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4945 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4946 */
4947 if (!phba->sli.ring)
4948 phba->sli.ring = kzalloc(
67d12733 4949 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
2a76a283
JS
4950 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4951 if (!phba->sli.ring)
4952 return -ENOMEM;
09294d46 4953
da0436e9 4954 /*
09294d46
JS
4955 * It doesn't matter what family our adapter is in, we are
4956 * limited to 2 Pages, 512 SGEs, for our SGL.
4957 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
4958 */
4959 max_buf_size = (2 * SLI4_PAGE_SIZE);
4960 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
4961 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
09294d46 4962
da0436e9 4963 /*
96f7077f 4964 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
da0436e9 4965 * used to create the sg_dma_buf_pool must be dynamically calculated.
da0436e9 4966 */
96f7077f
JS
4967
4968 if (phba->cfg_enable_bg) {
4969 /*
4970 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4971 * the FCP rsp, and a SGE for each. Sice we have no control
4972 * over how many protection data segments the SCSI Layer
4973 * will hand us (ie: there could be one for every block
4974 * in the IO), we just allocate enough SGEs to accomidate
4975 * our max amount and we need to limit lpfc_sg_seg_cnt to
4976 * minimize the risk of running out.
4977 */
4978 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4979 sizeof(struct fcp_rsp) + max_buf_size;
4980
4981 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
4982 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
4983
4984 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
4985 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
4986 } else {
4987 /*
4988 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4989 * the FCP rsp, a SGE for each, and a SGE for up to
4990 * cfg_sg_seg_cnt data segments.
4991 */
4992 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4993 sizeof(struct fcp_rsp) +
4994 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
4995
4996 /* Total SGEs for scsi_sg_list */
4997 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4998 /*
4999 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5000 * to post 1 page for the SGL.
5001 */
085c647c 5002 }
acd6859b 5003
96f7077f
JS
5004 /* Initialize the host templates with the updated values. */
5005 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5006 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5007
5008 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5009 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5010 else
5011 phba->cfg_sg_dma_buf_size =
5012 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5013
5014 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5015 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5016 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5017 phba->cfg_total_seg_cnt);
3772a991 5018
da0436e9
JS
5019 /* Initialize buffer queue management fields */
5020 hbq_count = lpfc_sli_hbq_count();
5021 for (i = 0; i < hbq_count; ++i)
5022 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5023 INIT_LIST_HEAD(&phba->rb_pend_list);
5024 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5025 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3772a991 5026
da0436e9
JS
5027 /*
5028 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5029 */
5030 /* Initialize the Abort scsi buffer list used by driver */
5031 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5032 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5033 /* This abort list used by worker thread */
5034 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3772a991 5035
da0436e9 5036 /*
6d368e53 5037 * Initialize driver internal slow-path work queues
da0436e9 5038 */
3772a991 5039
da0436e9
JS
5040 /* Driver internel slow-path CQ Event pool */
5041 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5042 /* Response IOCB work queue list */
45ed1190 5043 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
da0436e9
JS
5044 /* Asynchronous event CQ Event work queue list */
5045 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5046 /* Fast-path XRI aborted CQ Event work queue list */
5047 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5048 /* Slow-path XRI aborted CQ Event work queue list */
5049 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5050 /* Receive queue CQ Event work queue list */
5051 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5052
6d368e53
JS
5053 /* Initialize extent block lists. */
5054 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5055 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5056 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5057 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5058
da0436e9
JS
5059 /* Initialize the driver internal SLI layer lists. */
5060 lpfc_sli_setup(phba);
5061 lpfc_sli_queue_setup(phba);
3772a991 5062
da0436e9
JS
5063 /* Allocate device driver memory */
5064 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5065 if (rc)
5066 return -ENOMEM;
5067
2fcee4bf
JS
5068 /* IF Type 2 ports get initialized now. */
5069 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5070 LPFC_SLI_INTF_IF_TYPE_2) {
5071 rc = lpfc_pci_function_reset(phba);
5072 if (unlikely(rc))
5073 return -ENODEV;
5074 }
5075
da0436e9
JS
5076 /* Create the bootstrap mailbox command */
5077 rc = lpfc_create_bootstrap_mbox(phba);
5078 if (unlikely(rc))
5079 goto out_free_mem;
5080
5081 /* Set up the host's endian order with the device. */
5082 rc = lpfc_setup_endian_order(phba);
5083 if (unlikely(rc))
5084 goto out_free_bsmbx;
5085
5086 /* Set up the hba's configuration parameters. */
5087 rc = lpfc_sli4_read_config(phba);
5088 if (unlikely(rc))
5089 goto out_free_bsmbx;
5090
2fcee4bf
JS
5091 /* IF Type 0 ports get initialized now. */
5092 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5093 LPFC_SLI_INTF_IF_TYPE_0) {
5094 rc = lpfc_pci_function_reset(phba);
5095 if (unlikely(rc))
5096 goto out_free_bsmbx;
5097 }
da0436e9 5098
cb5172ea
JS
5099 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5100 GFP_KERNEL);
5101 if (!mboxq) {
5102 rc = -ENOMEM;
5103 goto out_free_bsmbx;
5104 }
5105
fedd3b7b 5106 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
cb5172ea
JS
5107 lpfc_supported_pages(mboxq);
5108 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
fedd3b7b
JS
5109 if (!rc) {
5110 mqe = &mboxq->u.mqe;
5111 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5112 LPFC_MAX_SUPPORTED_PAGES);
5113 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5114 switch (pn_page[i]) {
5115 case LPFC_SLI4_PARAMETERS:
5116 phba->sli4_hba.pc_sli4_params.supported = 1;
5117 break;
5118 default:
5119 break;
5120 }
5121 }
5122 /* Read the port's SLI4 Parameters capabilities if supported. */
5123 if (phba->sli4_hba.pc_sli4_params.supported)
5124 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5125 if (rc) {
5126 mempool_free(mboxq, phba->mbox_mem_pool);
5127 rc = -EIO;
5128 goto out_free_bsmbx;
cb5172ea
JS
5129 }
5130 }
fedd3b7b
JS
5131 /*
5132 * Get sli4 parameters that override parameters from Port capabilities.
6d368e53
JS
5133 * If this call fails, it isn't critical unless the SLI4 parameters come
5134 * back in conflict.
fedd3b7b 5135 */
6d368e53
JS
5136 rc = lpfc_get_sli4_parameters(phba, mboxq);
5137 if (rc) {
5138 if (phba->sli4_hba.extents_in_use &&
5139 phba->sli4_hba.rpi_hdrs_in_use) {
5140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5141 "2999 Unsupported SLI4 Parameters "
5142 "Extents and RPI headers enabled.\n");
5143 goto out_free_bsmbx;
5144 }
5145 }
cb5172ea 5146 mempool_free(mboxq, phba->mbox_mem_pool);
5350d872
JS
5147 /* Verify all the SLI4 queues */
5148 rc = lpfc_sli4_queue_verify(phba);
da0436e9
JS
5149 if (rc)
5150 goto out_free_bsmbx;
5151
5152 /* Create driver internal CQE event pool */
5153 rc = lpfc_sli4_cq_event_pool_create(phba);
5154 if (rc)
5350d872 5155 goto out_free_bsmbx;
da0436e9 5156
8a9d2e80
JS
5157 /* Initialize sgl lists per host */
5158 lpfc_init_sgl_list(phba);
5159
5160 /* Allocate and initialize active sgl array */
da0436e9
JS
5161 rc = lpfc_init_active_sgl_array(phba);
5162 if (rc) {
5163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5164 "1430 Failed to initialize sgl list.\n");
8a9d2e80 5165 goto out_destroy_cq_event_pool;
da0436e9 5166 }
da0436e9
JS
5167 rc = lpfc_sli4_init_rpi_hdrs(phba);
5168 if (rc) {
5169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5170 "1432 Failed to initialize rpi headers.\n");
5171 goto out_free_active_sgl;
5172 }
5173
a93ff37a 5174 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
0c9ab6f5
JS
5175 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5176 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5177 GFP_KERNEL);
5178 if (!phba->fcf.fcf_rr_bmask) {
5179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5180 "2759 Failed allocate memory for FCF round "
5181 "robin failover bmask\n");
0558056c 5182 rc = -ENOMEM;
0c9ab6f5
JS
5183 goto out_remove_rpi_hdrs;
5184 }
5185
67d12733
JS
5186 phba->sli4_hba.fcp_eq_hdl =
5187 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
5188 phba->cfg_fcp_io_channel), GFP_KERNEL);
5189 if (!phba->sli4_hba.fcp_eq_hdl) {
5190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5191 "2572 Failed allocate memory for "
5192 "fast-path per-EQ handle array\n");
5193 rc = -ENOMEM;
5194 goto out_free_fcf_rr_bmask;
da0436e9
JS
5195 }
5196
5197 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
82c3e9ba 5198 phba->cfg_fcp_io_channel), GFP_KERNEL);
da0436e9
JS
5199 if (!phba->sli4_hba.msix_entries) {
5200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5201 "2573 Failed allocate memory for msi-x "
5202 "interrupt vector entries\n");
0558056c 5203 rc = -ENOMEM;
da0436e9
JS
5204 goto out_free_fcp_eq_hdl;
5205 }
5206
7bb03bbf
JS
5207 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5208 phba->sli4_hba.num_present_cpu),
5209 GFP_KERNEL);
5210 if (!phba->sli4_hba.cpu_map) {
5211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5212 "3327 Failed allocate memory for msi-x "
5213 "interrupt vector mapping\n");
5214 rc = -ENOMEM;
5215 goto out_free_msix;
5216 }
5217 /* Initialize io channels for round robin */
5218 cpup = phba->sli4_hba.cpu_map;
5219 rc = 0;
5220 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5221 cpup->channel_id = rc;
5222 rc++;
5223 if (rc >= phba->cfg_fcp_io_channel)
5224 rc = 0;
5225 }
5226
912e3acd
JS
5227 /*
5228 * Enable sr-iov virtual functions if supported and configured
5229 * through the module parameter.
5230 */
5231 if (phba->cfg_sriov_nr_virtfn > 0) {
5232 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5233 phba->cfg_sriov_nr_virtfn);
5234 if (rc) {
5235 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5236 "3020 Requested number of SR-IOV "
5237 "virtual functions (%d) is not "
5238 "supported\n",
5239 phba->cfg_sriov_nr_virtfn);
5240 phba->cfg_sriov_nr_virtfn = 0;
5241 }
5242 }
5243
5248a749 5244 return 0;
da0436e9 5245
7bb03bbf
JS
5246out_free_msix:
5247 kfree(phba->sli4_hba.msix_entries);
da0436e9
JS
5248out_free_fcp_eq_hdl:
5249 kfree(phba->sli4_hba.fcp_eq_hdl);
0c9ab6f5
JS
5250out_free_fcf_rr_bmask:
5251 kfree(phba->fcf.fcf_rr_bmask);
da0436e9
JS
5252out_remove_rpi_hdrs:
5253 lpfc_sli4_remove_rpi_hdrs(phba);
5254out_free_active_sgl:
5255 lpfc_free_active_sgl(phba);
da0436e9
JS
5256out_destroy_cq_event_pool:
5257 lpfc_sli4_cq_event_pool_destroy(phba);
da0436e9
JS
5258out_free_bsmbx:
5259 lpfc_destroy_bootstrap_mbox(phba);
5260out_free_mem:
5261 lpfc_mem_free(phba);
5262 return rc;
5263}
5264
5265/**
5266 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5267 * @phba: pointer to lpfc hba data structure.
5268 *
5269 * This routine is invoked to unset the driver internal resources set up
5270 * specific for supporting the SLI-4 HBA device it attached to.
5271 **/
5272static void
5273lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5274{
5275 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5276
7bb03bbf
JS
5277 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5278 kfree(phba->sli4_hba.cpu_map);
5279 phba->sli4_hba.num_present_cpu = 0;
5280 phba->sli4_hba.num_online_cpu = 0;
5281
da0436e9
JS
5282 /* Free memory allocated for msi-x interrupt vector entries */
5283 kfree(phba->sli4_hba.msix_entries);
5284
5285 /* Free memory allocated for fast-path work queue handles */
5286 kfree(phba->sli4_hba.fcp_eq_hdl);
5287
5288 /* Free the allocated rpi headers. */
5289 lpfc_sli4_remove_rpi_hdrs(phba);
d11e31dd 5290 lpfc_sli4_remove_rpis(phba);
da0436e9 5291
0c9ab6f5
JS
5292 /* Free eligible FCF index bmask */
5293 kfree(phba->fcf.fcf_rr_bmask);
5294
da0436e9
JS
5295 /* Free the ELS sgl list */
5296 lpfc_free_active_sgl(phba);
8a9d2e80 5297 lpfc_free_els_sgl_list(phba);
da0436e9 5298
da0436e9
JS
5299 /* Free the completion queue EQ event pool */
5300 lpfc_sli4_cq_event_release_all(phba);
5301 lpfc_sli4_cq_event_pool_destroy(phba);
5302
6d368e53
JS
5303 /* Release resource identifiers. */
5304 lpfc_sli4_dealloc_resource_identifiers(phba);
5305
da0436e9
JS
5306 /* Free the bsmbx region. */
5307 lpfc_destroy_bootstrap_mbox(phba);
5308
5309 /* Free the SLI Layer memory with SLI4 HBAs */
5310 lpfc_mem_free_all(phba);
5311
5312 /* Free the current connect table */
5313 list_for_each_entry_safe(conn_entry, next_conn_entry,
4d9ab994
JS
5314 &phba->fcf_conn_rec_list, list) {
5315 list_del_init(&conn_entry->list);
da0436e9 5316 kfree(conn_entry);
4d9ab994 5317 }
da0436e9
JS
5318
5319 return;
5320}
5321
5322/**
25985edc 5323 * lpfc_init_api_table_setup - Set up init api function jump table
da0436e9
JS
5324 * @phba: The hba struct for which this call is being executed.
5325 * @dev_grp: The HBA PCI-Device group number.
5326 *
5327 * This routine sets up the device INIT interface API function jump table
5328 * in @phba struct.
5329 *
5330 * Returns: 0 - success, -ENODEV - failure.
5331 **/
5332int
5333lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5334{
84d1b006
JS
5335 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5336 phba->lpfc_hba_down_link = lpfc_hba_down_link;
7f86059a 5337 phba->lpfc_selective_reset = lpfc_selective_reset;
da0436e9
JS
5338 switch (dev_grp) {
5339 case LPFC_PCI_DEV_LP:
5340 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5341 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5342 phba->lpfc_stop_port = lpfc_stop_port_s3;
5343 break;
5344 case LPFC_PCI_DEV_OC:
5345 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5346 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5347 phba->lpfc_stop_port = lpfc_stop_port_s4;
5348 break;
5349 default:
5350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5351 "1431 Invalid HBA PCI-device group: 0x%x\n",
5352 dev_grp);
5353 return -ENODEV;
5354 break;
5355 }
5356 return 0;
5357}
5358
5359/**
5360 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5361 * @phba: pointer to lpfc hba data structure.
5362 *
5363 * This routine is invoked to set up the driver internal resources before the
5364 * device specific resource setup to support the HBA device it attached to.
5365 *
5366 * Return codes
af901ca1 5367 * 0 - successful
da0436e9
JS
5368 * other values - error
5369 **/
5370static int
5371lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5372{
5373 /*
5374 * Driver resources common to all SLI revisions
5375 */
5376 atomic_set(&phba->fast_event_count, 0);
5377 spin_lock_init(&phba->hbalock);
5378
5379 /* Initialize ndlp management spinlock */
5380 spin_lock_init(&phba->ndlp_lock);
5381
5382 INIT_LIST_HEAD(&phba->port_list);
5383 INIT_LIST_HEAD(&phba->work_list);
5384 init_waitqueue_head(&phba->wait_4_mlo_m_q);
5385
5386 /* Initialize the wait queue head for the kernel thread */
5387 init_waitqueue_head(&phba->work_waitq);
5388
5389 /* Initialize the scsi buffer list used by driver for scsi IO */
a40fc5f0
JS
5390 spin_lock_init(&phba->scsi_buf_list_get_lock);
5391 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5392 spin_lock_init(&phba->scsi_buf_list_put_lock);
5393 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
da0436e9
JS
5394
5395 /* Initialize the fabric iocb list */
5396 INIT_LIST_HEAD(&phba->fabric_iocb_list);
5397
5398 /* Initialize list to save ELS buffers */
5399 INIT_LIST_HEAD(&phba->elsbuf);
5400
5401 /* Initialize FCF connection rec list */
5402 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5403
5404 return 0;
5405}
5406
5407/**
5408 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5409 * @phba: pointer to lpfc hba data structure.
5410 *
5411 * This routine is invoked to set up the driver internal resources after the
5412 * device specific resource setup to support the HBA device it attached to.
5413 *
5414 * Return codes
af901ca1 5415 * 0 - successful
da0436e9
JS
5416 * other values - error
5417 **/
5418static int
5419lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5420{
5421 int error;
5422
5423 /* Startup the kernel thread for this host adapter. */
5424 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5425 "lpfc_worker_%d", phba->brd_no);
5426 if (IS_ERR(phba->worker_thread)) {
5427 error = PTR_ERR(phba->worker_thread);
5428 return error;
3772a991
JS
5429 }
5430
5431 return 0;
5432}
5433
5434/**
5435 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5436 * @phba: pointer to lpfc hba data structure.
5437 *
5438 * This routine is invoked to unset the driver internal resources set up after
5439 * the device specific resource setup for supporting the HBA device it
5440 * attached to.
5441 **/
5442static void
5443lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5444{
5445 /* Stop kernel worker thread */
5446 kthread_stop(phba->worker_thread);
5447}
5448
5449/**
5450 * lpfc_free_iocb_list - Free iocb list.
5451 * @phba: pointer to lpfc hba data structure.
5452 *
5453 * This routine is invoked to free the driver's IOCB list and memory.
5454 **/
5455static void
5456lpfc_free_iocb_list(struct lpfc_hba *phba)
5457{
5458 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5459
5460 spin_lock_irq(&phba->hbalock);
5461 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5462 &phba->lpfc_iocb_list, list) {
5463 list_del(&iocbq_entry->list);
5464 kfree(iocbq_entry);
5465 phba->total_iocbq_bufs--;
98c9ea5c 5466 }
3772a991
JS
5467 spin_unlock_irq(&phba->hbalock);
5468
5469 return;
5470}
5471
5472/**
5473 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5474 * @phba: pointer to lpfc hba data structure.
5475 *
5476 * This routine is invoked to allocate and initizlize the driver's IOCB
5477 * list and set up the IOCB tag array accordingly.
5478 *
5479 * Return codes
af901ca1 5480 * 0 - successful
3772a991
JS
5481 * other values - error
5482 **/
5483static int
5484lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5485{
5486 struct lpfc_iocbq *iocbq_entry = NULL;
5487 uint16_t iotag;
5488 int i;
dea3101e
JB
5489
5490 /* Initialize and populate the iocb list per host. */
5491 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
3772a991 5492 for (i = 0; i < iocb_count; i++) {
dd00cc48 5493 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea3101e
JB
5494 if (iocbq_entry == NULL) {
5495 printk(KERN_ERR "%s: only allocated %d iocbs of "
5496 "expected %d count. Unloading driver.\n",
cadbd4a5 5497 __func__, i, LPFC_IOCB_LIST_CNT);
dea3101e
JB
5498 goto out_free_iocbq;
5499 }
5500
604a3e30
JB
5501 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5502 if (iotag == 0) {
3772a991 5503 kfree(iocbq_entry);
604a3e30 5504 printk(KERN_ERR "%s: failed to allocate IOTAG. "
3772a991 5505 "Unloading driver.\n", __func__);
604a3e30
JB
5506 goto out_free_iocbq;
5507 }
6d368e53 5508 iocbq_entry->sli4_lxritag = NO_XRI;
3772a991 5509 iocbq_entry->sli4_xritag = NO_XRI;
2e0fef85
JS
5510
5511 spin_lock_irq(&phba->hbalock);
dea3101e
JB
5512 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5513 phba->total_iocbq_bufs++;
2e0fef85 5514 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
5515 }
5516
3772a991 5517 return 0;
dea3101e 5518
3772a991
JS
5519out_free_iocbq:
5520 lpfc_free_iocb_list(phba);
dea3101e 5521
3772a991
JS
5522 return -ENOMEM;
5523}
5e9d9b82 5524
3772a991 5525/**
8a9d2e80 5526 * lpfc_free_sgl_list - Free a given sgl list.
da0436e9 5527 * @phba: pointer to lpfc hba data structure.
8a9d2e80 5528 * @sglq_list: pointer to the head of sgl list.
3772a991 5529 *
8a9d2e80 5530 * This routine is invoked to free a give sgl list and memory.
3772a991 5531 **/
8a9d2e80
JS
5532void
5533lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
3772a991 5534{
da0436e9 5535 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
8a9d2e80
JS
5536
5537 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5538 list_del(&sglq_entry->list);
5539 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5540 kfree(sglq_entry);
5541 }
5542}
5543
5544/**
5545 * lpfc_free_els_sgl_list - Free els sgl list.
5546 * @phba: pointer to lpfc hba data structure.
5547 *
5548 * This routine is invoked to free the driver's els sgl list and memory.
5549 **/
5550static void
5551lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5552{
da0436e9 5553 LIST_HEAD(sglq_list);
dea3101e 5554
8a9d2e80 5555 /* Retrieve all els sgls from driver list */
da0436e9
JS
5556 spin_lock_irq(&phba->hbalock);
5557 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5558 spin_unlock_irq(&phba->hbalock);
dea3101e 5559
8a9d2e80
JS
5560 /* Now free the sgl list */
5561 lpfc_free_sgl_list(phba, &sglq_list);
da0436e9 5562}
92d7f7b0 5563
da0436e9
JS
5564/**
5565 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5566 * @phba: pointer to lpfc hba data structure.
5567 *
5568 * This routine is invoked to allocate the driver's active sgl memory.
5569 * This array will hold the sglq_entry's for active IOs.
5570 **/
5571static int
5572lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5573{
5574 int size;
5575 size = sizeof(struct lpfc_sglq *);
5576 size *= phba->sli4_hba.max_cfg_param.max_xri;
5577
5578 phba->sli4_hba.lpfc_sglq_active_list =
5579 kzalloc(size, GFP_KERNEL);
5580 if (!phba->sli4_hba.lpfc_sglq_active_list)
5581 return -ENOMEM;
5582 return 0;
3772a991
JS
5583}
5584
5585/**
da0436e9 5586 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3772a991
JS
5587 * @phba: pointer to lpfc hba data structure.
5588 *
da0436e9
JS
5589 * This routine is invoked to walk through the array of active sglq entries
5590 * and free all of the resources.
5591 * This is just a place holder for now.
3772a991
JS
5592 **/
5593static void
da0436e9 5594lpfc_free_active_sgl(struct lpfc_hba *phba)
3772a991 5595{
da0436e9 5596 kfree(phba->sli4_hba.lpfc_sglq_active_list);
3772a991
JS
5597}
5598
5599/**
da0436e9 5600 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3772a991
JS
5601 * @phba: pointer to lpfc hba data structure.
5602 *
da0436e9
JS
5603 * This routine is invoked to allocate and initizlize the driver's sgl
5604 * list and set up the sgl xritag tag array accordingly.
3772a991 5605 *
3772a991 5606 **/
8a9d2e80 5607static void
da0436e9 5608lpfc_init_sgl_list(struct lpfc_hba *phba)
3772a991 5609{
da0436e9
JS
5610 /* Initialize and populate the sglq list per host/VF. */
5611 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5612 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5613
8a9d2e80
JS
5614 /* els xri-sgl book keeping */
5615 phba->sli4_hba.els_xri_cnt = 0;
0ff10d46 5616
8a9d2e80 5617 /* scsi xri-buffer book keeping */
da0436e9 5618 phba->sli4_hba.scsi_xri_cnt = 0;
da0436e9
JS
5619}
5620
5621/**
5622 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5623 * @phba: pointer to lpfc hba data structure.
5624 *
5625 * This routine is invoked to post rpi header templates to the
88a2cfbb 5626 * port for those SLI4 ports that do not support extents. This routine
da0436e9 5627 * posts a PAGE_SIZE memory region to the port to hold up to
88a2cfbb
JS
5628 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5629 * and should be called only when interrupts are disabled.
da0436e9
JS
5630 *
5631 * Return codes
af901ca1 5632 * 0 - successful
88a2cfbb 5633 * -ERROR - otherwise.
da0436e9
JS
5634 **/
5635int
5636lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5637{
5638 int rc = 0;
da0436e9
JS
5639 struct lpfc_rpi_hdr *rpi_hdr;
5640
5641 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
ff78d8f9 5642 if (!phba->sli4_hba.rpi_hdrs_in_use)
6d368e53 5643 return rc;
6d368e53
JS
5644 if (phba->sli4_hba.extents_in_use)
5645 return -EIO;
da0436e9
JS
5646
5647 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5648 if (!rpi_hdr) {
5649 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5650 "0391 Error during rpi post operation\n");
5651 lpfc_sli4_remove_rpis(phba);
5652 rc = -ENODEV;
5653 }
5654
5655 return rc;
5656}
5657
5658/**
5659 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5660 * @phba: pointer to lpfc hba data structure.
5661 *
5662 * This routine is invoked to allocate a single 4KB memory region to
5663 * support rpis and stores them in the phba. This single region
5664 * provides support for up to 64 rpis. The region is used globally
5665 * by the device.
5666 *
5667 * Returns:
5668 * A valid rpi hdr on success.
5669 * A NULL pointer on any failure.
5670 **/
5671struct lpfc_rpi_hdr *
5672lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5673{
5674 uint16_t rpi_limit, curr_rpi_range;
5675 struct lpfc_dmabuf *dmabuf;
5676 struct lpfc_rpi_hdr *rpi_hdr;
9589b062 5677 uint32_t rpi_count;
da0436e9 5678
6d368e53
JS
5679 /*
5680 * If the SLI4 port supports extents, posting the rpi header isn't
5681 * required. Set the expected maximum count and let the actual value
5682 * get set when extents are fully allocated.
5683 */
5684 if (!phba->sli4_hba.rpi_hdrs_in_use)
5685 return NULL;
5686 if (phba->sli4_hba.extents_in_use)
5687 return NULL;
5688
5689 /* The limit on the logical index is just the max_rpi count. */
da0436e9 5690 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
6d368e53 5691 phba->sli4_hba.max_cfg_param.max_rpi - 1;
da0436e9
JS
5692
5693 spin_lock_irq(&phba->hbalock);
6d368e53
JS
5694 /*
5695 * Establish the starting RPI in this header block. The starting
5696 * rpi is normalized to a zero base because the physical rpi is
5697 * port based.
5698 */
97f2ecf1 5699 curr_rpi_range = phba->sli4_hba.next_rpi;
da0436e9
JS
5700 spin_unlock_irq(&phba->hbalock);
5701
5702 /*
5703 * The port has a limited number of rpis. The increment here
5704 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5705 * and to allow the full max_rpi range per port.
5706 */
5707 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
9589b062
JS
5708 rpi_count = rpi_limit - curr_rpi_range;
5709 else
5710 rpi_count = LPFC_RPI_HDR_COUNT;
da0436e9 5711
6d368e53
JS
5712 if (!rpi_count)
5713 return NULL;
da0436e9
JS
5714 /*
5715 * First allocate the protocol header region for the port. The
5716 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5717 */
5718 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5719 if (!dmabuf)
5720 return NULL;
5721
5722 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5723 LPFC_HDR_TEMPLATE_SIZE,
5724 &dmabuf->phys,
5725 GFP_KERNEL);
5726 if (!dmabuf->virt) {
5727 rpi_hdr = NULL;
5728 goto err_free_dmabuf;
5729 }
5730
5731 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5732 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5733 rpi_hdr = NULL;
5734 goto err_free_coherent;
5735 }
5736
5737 /* Save the rpi header data for cleanup later. */
5738 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5739 if (!rpi_hdr)
5740 goto err_free_coherent;
5741
5742 rpi_hdr->dmabuf = dmabuf;
5743 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5744 rpi_hdr->page_count = 1;
5745 spin_lock_irq(&phba->hbalock);
6d368e53
JS
5746
5747 /* The rpi_hdr stores the logical index only. */
5748 rpi_hdr->start_rpi = curr_rpi_range;
da0436e9
JS
5749 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5750
5751 /*
6d368e53
JS
5752 * The next_rpi stores the next logical module-64 rpi value used
5753 * to post physical rpis in subsequent rpi postings.
da0436e9 5754 */
9589b062 5755 phba->sli4_hba.next_rpi += rpi_count;
da0436e9
JS
5756 spin_unlock_irq(&phba->hbalock);
5757 return rpi_hdr;
5758
5759 err_free_coherent:
5760 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5761 dmabuf->virt, dmabuf->phys);
5762 err_free_dmabuf:
5763 kfree(dmabuf);
5764 return NULL;
5765}
5766
5767/**
5768 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5769 * @phba: pointer to lpfc hba data structure.
5770 *
5771 * This routine is invoked to remove all memory resources allocated
6d368e53
JS
5772 * to support rpis for SLI4 ports not supporting extents. This routine
5773 * presumes the caller has released all rpis consumed by fabric or port
5774 * logins and is prepared to have the header pages removed.
da0436e9
JS
5775 **/
5776void
5777lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5778{
5779 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5780
6d368e53
JS
5781 if (!phba->sli4_hba.rpi_hdrs_in_use)
5782 goto exit;
5783
da0436e9
JS
5784 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5785 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5786 list_del(&rpi_hdr->list);
5787 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5788 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5789 kfree(rpi_hdr->dmabuf);
5790 kfree(rpi_hdr);
5791 }
6d368e53
JS
5792 exit:
5793 /* There are no rpis available to the port now. */
5794 phba->sli4_hba.next_rpi = 0;
da0436e9
JS
5795}
5796
5797/**
5798 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5799 * @pdev: pointer to pci device data structure.
5800 *
5801 * This routine is invoked to allocate the driver hba data structure for an
5802 * HBA device. If the allocation is successful, the phba reference to the
5803 * PCI device data structure is set.
5804 *
5805 * Return codes
af901ca1 5806 * pointer to @phba - successful
da0436e9
JS
5807 * NULL - error
5808 **/
5809static struct lpfc_hba *
5810lpfc_hba_alloc(struct pci_dev *pdev)
5811{
5812 struct lpfc_hba *phba;
5813
5814 /* Allocate memory for HBA structure */
5815 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5816 if (!phba) {
e34ccdfe 5817 dev_err(&pdev->dev, "failed to allocate hba struct\n");
da0436e9
JS
5818 return NULL;
5819 }
5820
5821 /* Set reference to PCI device in HBA structure */
5822 phba->pcidev = pdev;
5823
5824 /* Assign an unused board number */
5825 phba->brd_no = lpfc_get_instance();
5826 if (phba->brd_no < 0) {
5827 kfree(phba);
5828 return NULL;
5829 }
5830
4fede78f 5831 spin_lock_init(&phba->ct_ev_lock);
f1c3b0fc
JS
5832 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5833
da0436e9
JS
5834 return phba;
5835}
5836
5837/**
5838 * lpfc_hba_free - Free driver hba data structure with a device.
5839 * @phba: pointer to lpfc hba data structure.
5840 *
5841 * This routine is invoked to free the driver hba data structure with an
5842 * HBA device.
5843 **/
5844static void
5845lpfc_hba_free(struct lpfc_hba *phba)
5846{
5847 /* Release the driver assigned board number */
5848 idr_remove(&lpfc_hba_index, phba->brd_no);
5849
2a76a283
JS
5850 /* Free memory allocated with sli rings */
5851 kfree(phba->sli.ring);
5852 phba->sli.ring = NULL;
5853
da0436e9
JS
5854 kfree(phba);
5855 return;
5856}
5857
5858/**
5859 * lpfc_create_shost - Create hba physical port with associated scsi host.
5860 * @phba: pointer to lpfc hba data structure.
5861 *
5862 * This routine is invoked to create HBA physical port and associate a SCSI
5863 * host with it.
5864 *
5865 * Return codes
af901ca1 5866 * 0 - successful
da0436e9
JS
5867 * other values - error
5868 **/
5869static int
5870lpfc_create_shost(struct lpfc_hba *phba)
5871{
5872 struct lpfc_vport *vport;
5873 struct Scsi_Host *shost;
5874
5875 /* Initialize HBA FC structure */
5876 phba->fc_edtov = FF_DEF_EDTOV;
5877 phba->fc_ratov = FF_DEF_RATOV;
5878 phba->fc_altov = FF_DEF_ALTOV;
5879 phba->fc_arbtov = FF_DEF_ARBTOV;
5880
d7c47992 5881 atomic_set(&phba->sdev_cnt, 0);
da0436e9
JS
5882 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
5883 if (!vport)
5884 return -ENODEV;
5885
5886 shost = lpfc_shost_from_vport(vport);
5887 phba->pport = vport;
5888 lpfc_debugfs_initialize(vport);
5889 /* Put reference to SCSI host to driver's device private data */
5890 pci_set_drvdata(phba->pcidev, shost);
2e0fef85 5891
3772a991
JS
5892 return 0;
5893}
db2378e0 5894
3772a991
JS
5895/**
5896 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5897 * @phba: pointer to lpfc hba data structure.
5898 *
5899 * This routine is invoked to destroy HBA physical port and the associated
5900 * SCSI host.
5901 **/
5902static void
5903lpfc_destroy_shost(struct lpfc_hba *phba)
5904{
5905 struct lpfc_vport *vport = phba->pport;
5906
5907 /* Destroy physical port that associated with the SCSI host */
5908 destroy_port(vport);
5909
5910 return;
5911}
5912
5913/**
5914 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5915 * @phba: pointer to lpfc hba data structure.
5916 * @shost: the shost to be used to detect Block guard settings.
5917 *
5918 * This routine sets up the local Block guard protocol settings for @shost.
5919 * This routine also allocates memory for debugging bg buffers.
5920 **/
5921static void
5922lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5923{
bbeb79b9
JS
5924 uint32_t old_mask;
5925 uint32_t old_guard;
5926
3772a991
JS
5927 int pagecnt = 10;
5928 if (lpfc_prot_mask && lpfc_prot_guard) {
5929 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5930 "1478 Registering BlockGuard with the "
5931 "SCSI layer\n");
bbeb79b9
JS
5932
5933 old_mask = lpfc_prot_mask;
5934 old_guard = lpfc_prot_guard;
5935
5936 /* Only allow supported values */
5937 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5938 SHOST_DIX_TYPE0_PROTECTION |
5939 SHOST_DIX_TYPE1_PROTECTION);
5940 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5941
5942 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5943 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5944 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5945
5946 if (lpfc_prot_mask && lpfc_prot_guard) {
5947 if ((old_mask != lpfc_prot_mask) ||
5948 (old_guard != lpfc_prot_guard))
5949 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5950 "1475 Registering BlockGuard with the "
5951 "SCSI layer: mask %d guard %d\n",
5952 lpfc_prot_mask, lpfc_prot_guard);
5953
5954 scsi_host_set_prot(shost, lpfc_prot_mask);
5955 scsi_host_set_guard(shost, lpfc_prot_guard);
5956 } else
5957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5958 "1479 Not Registering BlockGuard with the SCSI "
5959 "layer, Bad protection parameters: %d %d\n",
5960 old_mask, old_guard);
3772a991 5961 }
bbeb79b9 5962
3772a991
JS
5963 if (!_dump_buf_data) {
5964 while (pagecnt) {
5965 spin_lock_init(&_dump_buf_lock);
5966 _dump_buf_data =
5967 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5968 if (_dump_buf_data) {
6a9c52cf
JS
5969 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5970 "9043 BLKGRD: allocated %d pages for "
3772a991
JS
5971 "_dump_buf_data at 0x%p\n",
5972 (1 << pagecnt), _dump_buf_data);
5973 _dump_buf_data_order = pagecnt;
5974 memset(_dump_buf_data, 0,
5975 ((1 << PAGE_SHIFT) << pagecnt));
5976 break;
5977 } else
5978 --pagecnt;
5979 }
5980 if (!_dump_buf_data_order)
6a9c52cf
JS
5981 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5982 "9044 BLKGRD: ERROR unable to allocate "
3772a991
JS
5983 "memory for hexdump\n");
5984 } else
6a9c52cf
JS
5985 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5986 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
3772a991
JS
5987 "\n", _dump_buf_data);
5988 if (!_dump_buf_dif) {
5989 while (pagecnt) {
5990 _dump_buf_dif =
5991 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5992 if (_dump_buf_dif) {
6a9c52cf
JS
5993 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5994 "9046 BLKGRD: allocated %d pages for "
3772a991
JS
5995 "_dump_buf_dif at 0x%p\n",
5996 (1 << pagecnt), _dump_buf_dif);
5997 _dump_buf_dif_order = pagecnt;
5998 memset(_dump_buf_dif, 0,
5999 ((1 << PAGE_SHIFT) << pagecnt));
6000 break;
6001 } else
6002 --pagecnt;
6003 }
6004 if (!_dump_buf_dif_order)
6a9c52cf
JS
6005 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6006 "9047 BLKGRD: ERROR unable to allocate "
3772a991
JS
6007 "memory for hexdump\n");
6008 } else
6a9c52cf
JS
6009 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6010 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
3772a991
JS
6011 _dump_buf_dif);
6012}
6013
6014/**
6015 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6016 * @phba: pointer to lpfc hba data structure.
6017 *
6018 * This routine is invoked to perform all the necessary post initialization
6019 * setup for the device.
6020 **/
6021static void
6022lpfc_post_init_setup(struct lpfc_hba *phba)
6023{
6024 struct Scsi_Host *shost;
6025 struct lpfc_adapter_event_header adapter_event;
6026
6027 /* Get the default values for Model Name and Description */
6028 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6029
6030 /*
6031 * hba setup may have changed the hba_queue_depth so we need to
6032 * adjust the value of can_queue.
6033 */
6034 shost = pci_get_drvdata(phba->pcidev);
6035 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6036 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6037 lpfc_setup_bg(phba, shost);
6038
6039 lpfc_host_attrib_init(shost);
6040
6041 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6042 spin_lock_irq(shost->host_lock);
6043 lpfc_poll_start_timer(phba);
6044 spin_unlock_irq(shost->host_lock);
6045 }
6046
6047 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6048 "0428 Perform SCSI scan\n");
6049 /* Send board arrival event to upper layer */
6050 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6051 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6052 fc_host_post_vendor_event(shost, fc_get_event_number(),
6053 sizeof(adapter_event),
6054 (char *) &adapter_event,
6055 LPFC_NL_VENDOR_ID);
6056 return;
6057}
6058
6059/**
6060 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6061 * @phba: pointer to lpfc hba data structure.
6062 *
6063 * This routine is invoked to set up the PCI device memory space for device
6064 * with SLI-3 interface spec.
6065 *
6066 * Return codes
af901ca1 6067 * 0 - successful
3772a991
JS
6068 * other values - error
6069 **/
6070static int
6071lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6072{
6073 struct pci_dev *pdev;
6074 unsigned long bar0map_len, bar2map_len;
6075 int i, hbq_count;
6076 void *ptr;
6077 int error = -ENODEV;
6078
6079 /* Obtain PCI device reference */
6080 if (!phba->pcidev)
6081 return error;
6082 else
6083 pdev = phba->pcidev;
6084
6085 /* Set the device DMA mask size */
8e68597d
MR
6086 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6087 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6088 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6089 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
3772a991 6090 return error;
8e68597d
MR
6091 }
6092 }
3772a991
JS
6093
6094 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6095 * required by each mapping.
6096 */
6097 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6098 bar0map_len = pci_resource_len(pdev, 0);
6099
6100 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6101 bar2map_len = pci_resource_len(pdev, 2);
6102
6103 /* Map HBA SLIM to a kernel virtual address. */
6104 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6105 if (!phba->slim_memmap_p) {
6106 dev_printk(KERN_ERR, &pdev->dev,
6107 "ioremap failed for SLIM memory.\n");
6108 goto out;
6109 }
6110
6111 /* Map HBA Control Registers to a kernel virtual address. */
6112 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6113 if (!phba->ctrl_regs_memmap_p) {
6114 dev_printk(KERN_ERR, &pdev->dev,
6115 "ioremap failed for HBA control registers.\n");
6116 goto out_iounmap_slim;
6117 }
6118
6119 /* Allocate memory for SLI-2 structures */
6120 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6121 SLI2_SLIM_SIZE,
6122 &phba->slim2p.phys,
6123 GFP_KERNEL);
6124 if (!phba->slim2p.virt)
6125 goto out_iounmap;
6126
6127 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6128 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
7a470277
JS
6129 phba->mbox_ext = (phba->slim2p.virt +
6130 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
3772a991
JS
6131 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6132 phba->IOCBs = (phba->slim2p.virt +
6133 offsetof(struct lpfc_sli2_slim, IOCBs));
6134
6135 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6136 lpfc_sli_hbq_size(),
6137 &phba->hbqslimp.phys,
6138 GFP_KERNEL);
6139 if (!phba->hbqslimp.virt)
6140 goto out_free_slim;
6141
6142 hbq_count = lpfc_sli_hbq_count();
6143 ptr = phba->hbqslimp.virt;
6144 for (i = 0; i < hbq_count; ++i) {
6145 phba->hbqs[i].hbq_virt = ptr;
6146 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6147 ptr += (lpfc_hbq_defs[i]->entry_count *
6148 sizeof(struct lpfc_hbq_entry));
6149 }
6150 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6151 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6152
6153 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6154
6155 INIT_LIST_HEAD(&phba->rb_pend_list);
6156
6157 phba->MBslimaddr = phba->slim_memmap_p;
6158 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6159 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6160 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6161 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
6162
6163 return 0;
6164
6165out_free_slim:
6166 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6167 phba->slim2p.virt, phba->slim2p.phys);
6168out_iounmap:
6169 iounmap(phba->ctrl_regs_memmap_p);
6170out_iounmap_slim:
6171 iounmap(phba->slim_memmap_p);
6172out:
6173 return error;
6174}
6175
6176/**
6177 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6178 * @phba: pointer to lpfc hba data structure.
6179 *
6180 * This routine is invoked to unset the PCI device memory space for device
6181 * with SLI-3 interface spec.
6182 **/
6183static void
6184lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6185{
6186 struct pci_dev *pdev;
6187
6188 /* Obtain PCI device reference */
6189 if (!phba->pcidev)
6190 return;
6191 else
6192 pdev = phba->pcidev;
6193
6194 /* Free coherent DMA memory allocated */
6195 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6196 phba->hbqslimp.virt, phba->hbqslimp.phys);
6197 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6198 phba->slim2p.virt, phba->slim2p.phys);
6199
6200 /* I/O memory unmap */
6201 iounmap(phba->ctrl_regs_memmap_p);
6202 iounmap(phba->slim_memmap_p);
6203
6204 return;
6205}
6206
6207/**
da0436e9 6208 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
3772a991
JS
6209 * @phba: pointer to lpfc hba data structure.
6210 *
da0436e9
JS
6211 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6212 * done and check status.
3772a991 6213 *
da0436e9 6214 * Return 0 if successful, otherwise -ENODEV.
3772a991 6215 **/
da0436e9
JS
6216int
6217lpfc_sli4_post_status_check(struct lpfc_hba *phba)
3772a991 6218{
2fcee4bf
JS
6219 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6220 struct lpfc_register reg_data;
6221 int i, port_error = 0;
6222 uint32_t if_type;
3772a991 6223
9940b97b
JS
6224 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6225 memset(&reg_data, 0, sizeof(reg_data));
2fcee4bf 6226 if (!phba->sli4_hba.PSMPHRregaddr)
da0436e9 6227 return -ENODEV;
3772a991 6228
da0436e9
JS
6229 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6230 for (i = 0; i < 3000; i++) {
9940b97b
JS
6231 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6232 &portsmphr_reg.word0) ||
6233 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
2fcee4bf 6234 /* Port has a fatal POST error, break out */
da0436e9
JS
6235 port_error = -ENODEV;
6236 break;
6237 }
2fcee4bf
JS
6238 if (LPFC_POST_STAGE_PORT_READY ==
6239 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
da0436e9 6240 break;
da0436e9 6241 msleep(10);
3772a991
JS
6242 }
6243
2fcee4bf
JS
6244 /*
6245 * If there was a port error during POST, then don't proceed with
6246 * other register reads as the data may not be valid. Just exit.
6247 */
6248 if (port_error) {
da0436e9 6249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
6250 "1408 Port Failed POST - portsmphr=0x%x, "
6251 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6252 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6253 portsmphr_reg.word0,
6254 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6255 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6256 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6257 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6258 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6259 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6260 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6261 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6262 } else {
28baac74 6263 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2fcee4bf
JS
6264 "2534 Device Info: SLIFamily=0x%x, "
6265 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6266 "SLIHint_2=0x%x, FT=0x%x\n",
28baac74
JS
6267 bf_get(lpfc_sli_intf_sli_family,
6268 &phba->sli4_hba.sli_intf),
6269 bf_get(lpfc_sli_intf_slirev,
6270 &phba->sli4_hba.sli_intf),
085c647c
JS
6271 bf_get(lpfc_sli_intf_if_type,
6272 &phba->sli4_hba.sli_intf),
6273 bf_get(lpfc_sli_intf_sli_hint1,
28baac74 6274 &phba->sli4_hba.sli_intf),
085c647c
JS
6275 bf_get(lpfc_sli_intf_sli_hint2,
6276 &phba->sli4_hba.sli_intf),
6277 bf_get(lpfc_sli_intf_func_type,
28baac74 6278 &phba->sli4_hba.sli_intf));
2fcee4bf
JS
6279 /*
6280 * Check for other Port errors during the initialization
6281 * process. Fail the load if the port did not come up
6282 * correctly.
6283 */
6284 if_type = bf_get(lpfc_sli_intf_if_type,
6285 &phba->sli4_hba.sli_intf);
6286 switch (if_type) {
6287 case LPFC_SLI_INTF_IF_TYPE_0:
6288 phba->sli4_hba.ue_mask_lo =
6289 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6290 phba->sli4_hba.ue_mask_hi =
6291 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6292 uerrlo_reg.word0 =
6293 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6294 uerrhi_reg.word0 =
6295 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6296 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6297 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6299 "1422 Unrecoverable Error "
6300 "Detected during POST "
6301 "uerr_lo_reg=0x%x, "
6302 "uerr_hi_reg=0x%x, "
6303 "ue_mask_lo_reg=0x%x, "
6304 "ue_mask_hi_reg=0x%x\n",
6305 uerrlo_reg.word0,
6306 uerrhi_reg.word0,
6307 phba->sli4_hba.ue_mask_lo,
6308 phba->sli4_hba.ue_mask_hi);
6309 port_error = -ENODEV;
6310 }
6311 break;
6312 case LPFC_SLI_INTF_IF_TYPE_2:
6313 /* Final checks. The port status should be clean. */
9940b97b
JS
6314 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6315 &reg_data.word0) ||
0558056c
JS
6316 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6317 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
2fcee4bf
JS
6318 phba->work_status[0] =
6319 readl(phba->sli4_hba.u.if_type2.
6320 ERR1regaddr);
6321 phba->work_status[1] =
6322 readl(phba->sli4_hba.u.if_type2.
6323 ERR2regaddr);
6324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8fcb8acd
JS
6325 "2888 Unrecoverable port error "
6326 "following POST: port status reg "
6327 "0x%x, port_smphr reg 0x%x, "
2fcee4bf
JS
6328 "error 1=0x%x, error 2=0x%x\n",
6329 reg_data.word0,
6330 portsmphr_reg.word0,
6331 phba->work_status[0],
6332 phba->work_status[1]);
6333 port_error = -ENODEV;
6334 }
6335 break;
6336 case LPFC_SLI_INTF_IF_TYPE_1:
6337 default:
6338 break;
6339 }
28baac74 6340 }
da0436e9
JS
6341 return port_error;
6342}
3772a991 6343
da0436e9
JS
6344/**
6345 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6346 * @phba: pointer to lpfc hba data structure.
2fcee4bf 6347 * @if_type: The SLI4 interface type getting configured.
da0436e9
JS
6348 *
6349 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6350 * memory map.
6351 **/
6352static void
2fcee4bf
JS
6353lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
6354{
6355 switch (if_type) {
6356 case LPFC_SLI_INTF_IF_TYPE_0:
6357 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6358 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6359 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6360 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6361 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6362 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6363 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6364 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6365 phba->sli4_hba.SLIINTFregaddr =
6366 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6367 break;
6368 case LPFC_SLI_INTF_IF_TYPE_2:
6369 phba->sli4_hba.u.if_type2.ERR1regaddr =
88a2cfbb
JS
6370 phba->sli4_hba.conf_regs_memmap_p +
6371 LPFC_CTL_PORT_ER1_OFFSET;
2fcee4bf 6372 phba->sli4_hba.u.if_type2.ERR2regaddr =
88a2cfbb
JS
6373 phba->sli4_hba.conf_regs_memmap_p +
6374 LPFC_CTL_PORT_ER2_OFFSET;
2fcee4bf 6375 phba->sli4_hba.u.if_type2.CTRLregaddr =
88a2cfbb
JS
6376 phba->sli4_hba.conf_regs_memmap_p +
6377 LPFC_CTL_PORT_CTL_OFFSET;
2fcee4bf 6378 phba->sli4_hba.u.if_type2.STATUSregaddr =
88a2cfbb
JS
6379 phba->sli4_hba.conf_regs_memmap_p +
6380 LPFC_CTL_PORT_STA_OFFSET;
2fcee4bf
JS
6381 phba->sli4_hba.SLIINTFregaddr =
6382 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6383 phba->sli4_hba.PSMPHRregaddr =
88a2cfbb
JS
6384 phba->sli4_hba.conf_regs_memmap_p +
6385 LPFC_CTL_PORT_SEM_OFFSET;
2fcee4bf 6386 phba->sli4_hba.RQDBregaddr =
962bc51b
JS
6387 phba->sli4_hba.conf_regs_memmap_p +
6388 LPFC_ULP0_RQ_DOORBELL;
2fcee4bf 6389 phba->sli4_hba.WQDBregaddr =
962bc51b
JS
6390 phba->sli4_hba.conf_regs_memmap_p +
6391 LPFC_ULP0_WQ_DOORBELL;
2fcee4bf
JS
6392 phba->sli4_hba.EQCQDBregaddr =
6393 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6394 phba->sli4_hba.MQDBregaddr =
6395 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6396 phba->sli4_hba.BMBXregaddr =
6397 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6398 break;
6399 case LPFC_SLI_INTF_IF_TYPE_1:
6400 default:
6401 dev_printk(KERN_ERR, &phba->pcidev->dev,
6402 "FATAL - unsupported SLI4 interface type - %d\n",
6403 if_type);
6404 break;
6405 }
da0436e9 6406}
3772a991 6407
da0436e9
JS
6408/**
6409 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6410 * @phba: pointer to lpfc hba data structure.
6411 *
6412 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6413 * memory map.
6414 **/
6415static void
6416lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6417{
2fcee4bf
JS
6418 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6419 LPFC_SLIPORT_IF0_SMPHR;
da0436e9 6420 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
2fcee4bf 6421 LPFC_HST_ISR0;
da0436e9 6422 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
2fcee4bf 6423 LPFC_HST_IMR0;
da0436e9 6424 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
2fcee4bf 6425 LPFC_HST_ISCR0;
3772a991
JS
6426}
6427
6428/**
da0436e9 6429 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
3772a991 6430 * @phba: pointer to lpfc hba data structure.
da0436e9 6431 * @vf: virtual function number
3772a991 6432 *
da0436e9
JS
6433 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6434 * based on the given viftual function number, @vf.
6435 *
6436 * Return 0 if successful, otherwise -ENODEV.
3772a991 6437 **/
da0436e9
JS
6438static int
6439lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
3772a991 6440{
da0436e9
JS
6441 if (vf > LPFC_VIR_FUNC_MAX)
6442 return -ENODEV;
3772a991 6443
da0436e9 6444 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
962bc51b
JS
6445 vf * LPFC_VFR_PAGE_SIZE +
6446 LPFC_ULP0_RQ_DOORBELL);
da0436e9 6447 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
962bc51b
JS
6448 vf * LPFC_VFR_PAGE_SIZE +
6449 LPFC_ULP0_WQ_DOORBELL);
da0436e9
JS
6450 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6451 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6452 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6453 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6454 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6455 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6456 return 0;
3772a991
JS
6457}
6458
6459/**
da0436e9 6460 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
3772a991
JS
6461 * @phba: pointer to lpfc hba data structure.
6462 *
da0436e9
JS
6463 * This routine is invoked to create the bootstrap mailbox
6464 * region consistent with the SLI-4 interface spec. This
6465 * routine allocates all memory necessary to communicate
6466 * mailbox commands to the port and sets up all alignment
6467 * needs. No locks are expected to be held when calling
6468 * this routine.
3772a991
JS
6469 *
6470 * Return codes
af901ca1 6471 * 0 - successful
d439d286 6472 * -ENOMEM - could not allocated memory.
da0436e9 6473 **/
3772a991 6474static int
da0436e9 6475lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
3772a991 6476{
da0436e9
JS
6477 uint32_t bmbx_size;
6478 struct lpfc_dmabuf *dmabuf;
6479 struct dma_address *dma_address;
6480 uint32_t pa_addr;
6481 uint64_t phys_addr;
6482
6483 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6484 if (!dmabuf)
6485 return -ENOMEM;
3772a991 6486
da0436e9
JS
6487 /*
6488 * The bootstrap mailbox region is comprised of 2 parts
6489 * plus an alignment restriction of 16 bytes.
6490 */
6491 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6492 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6493 bmbx_size,
6494 &dmabuf->phys,
6495 GFP_KERNEL);
6496 if (!dmabuf->virt) {
6497 kfree(dmabuf);
6498 return -ENOMEM;
3772a991 6499 }
da0436e9 6500 memset(dmabuf->virt, 0, bmbx_size);
3772a991 6501
da0436e9
JS
6502 /*
6503 * Initialize the bootstrap mailbox pointers now so that the register
6504 * operations are simple later. The mailbox dma address is required
6505 * to be 16-byte aligned. Also align the virtual memory as each
6506 * maibox is copied into the bmbx mailbox region before issuing the
6507 * command to the port.
6508 */
6509 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6510 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6511
6512 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6513 LPFC_ALIGN_16_BYTE);
6514 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6515 LPFC_ALIGN_16_BYTE);
6516
6517 /*
6518 * Set the high and low physical addresses now. The SLI4 alignment
6519 * requirement is 16 bytes and the mailbox is posted to the port
6520 * as two 30-bit addresses. The other data is a bit marking whether
6521 * the 30-bit address is the high or low address.
6522 * Upcast bmbx aphys to 64bits so shift instruction compiles
6523 * clean on 32 bit machines.
6524 */
6525 dma_address = &phba->sli4_hba.bmbx.dma_address;
6526 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6527 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6528 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6529 LPFC_BMBX_BIT1_ADDR_HI);
6530
6531 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6532 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6533 LPFC_BMBX_BIT1_ADDR_LO);
6534 return 0;
3772a991
JS
6535}
6536
6537/**
da0436e9 6538 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
3772a991
JS
6539 * @phba: pointer to lpfc hba data structure.
6540 *
da0436e9
JS
6541 * This routine is invoked to teardown the bootstrap mailbox
6542 * region and release all host resources. This routine requires
6543 * the caller to ensure all mailbox commands recovered, no
6544 * additional mailbox comands are sent, and interrupts are disabled
6545 * before calling this routine.
6546 *
6547 **/
3772a991 6548static void
da0436e9 6549lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
3772a991 6550{
da0436e9
JS
6551 dma_free_coherent(&phba->pcidev->dev,
6552 phba->sli4_hba.bmbx.bmbx_size,
6553 phba->sli4_hba.bmbx.dmabuf->virt,
6554 phba->sli4_hba.bmbx.dmabuf->phys);
6555
6556 kfree(phba->sli4_hba.bmbx.dmabuf);
6557 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
3772a991
JS
6558}
6559
6560/**
da0436e9 6561 * lpfc_sli4_read_config - Get the config parameters.
3772a991
JS
6562 * @phba: pointer to lpfc hba data structure.
6563 *
da0436e9
JS
6564 * This routine is invoked to read the configuration parameters from the HBA.
6565 * The configuration parameters are used to set the base and maximum values
6566 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6567 * allocation for the port.
3772a991
JS
6568 *
6569 * Return codes
af901ca1 6570 * 0 - successful
25985edc 6571 * -ENOMEM - No available memory
d439d286 6572 * -EIO - The mailbox failed to complete successfully.
3772a991 6573 **/
ff78d8f9 6574int
da0436e9 6575lpfc_sli4_read_config(struct lpfc_hba *phba)
3772a991 6576{
da0436e9
JS
6577 LPFC_MBOXQ_t *pmb;
6578 struct lpfc_mbx_read_config *rd_config;
912e3acd
JS
6579 union lpfc_sli4_cfg_shdr *shdr;
6580 uint32_t shdr_status, shdr_add_status;
6581 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6582 struct lpfc_rsrc_desc_fcfcoe *desc;
8aa134a8 6583 char *pdesc_0;
912e3acd 6584 uint32_t desc_count;
8aa134a8 6585 int length, i, rc = 0, rc2;
3772a991 6586
da0436e9
JS
6587 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6588 if (!pmb) {
6589 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6590 "2011 Unable to allocate memory for issuing "
6591 "SLI_CONFIG_SPECIAL mailbox command\n");
6592 return -ENOMEM;
3772a991
JS
6593 }
6594
da0436e9 6595 lpfc_read_config(phba, pmb);
3772a991 6596
da0436e9
JS
6597 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6598 if (rc != MBX_SUCCESS) {
6599 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6600 "2012 Mailbox failed , mbxCmd x%x "
6601 "READ_CONFIG, mbxStatus x%x\n",
6602 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6603 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6604 rc = -EIO;
6605 } else {
6606 rd_config = &pmb->u.mqe.un.rd_config;
ff78d8f9
JS
6607 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6608 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6609 phba->sli4_hba.lnk_info.lnk_tp =
6610 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6611 phba->sli4_hba.lnk_info.lnk_no =
6612 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6613 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6614 "3081 lnk_type:%d, lnk_numb:%d\n",
6615 phba->sli4_hba.lnk_info.lnk_tp,
6616 phba->sli4_hba.lnk_info.lnk_no);
6617 } else
6618 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6619 "3082 Mailbox (x%x) returned ldv:x0\n",
6620 bf_get(lpfc_mqe_command, &pmb->u.mqe));
6d368e53
JS
6621 phba->sli4_hba.extents_in_use =
6622 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
da0436e9
JS
6623 phba->sli4_hba.max_cfg_param.max_xri =
6624 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6625 phba->sli4_hba.max_cfg_param.xri_base =
6626 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6627 phba->sli4_hba.max_cfg_param.max_vpi =
6628 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6629 phba->sli4_hba.max_cfg_param.vpi_base =
6630 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6631 phba->sli4_hba.max_cfg_param.max_rpi =
6632 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6633 phba->sli4_hba.max_cfg_param.rpi_base =
6634 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6635 phba->sli4_hba.max_cfg_param.max_vfi =
6636 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6637 phba->sli4_hba.max_cfg_param.vfi_base =
6638 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6639 phba->sli4_hba.max_cfg_param.max_fcfi =
6640 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
da0436e9
JS
6641 phba->sli4_hba.max_cfg_param.max_eq =
6642 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6643 phba->sli4_hba.max_cfg_param.max_rq =
6644 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6645 phba->sli4_hba.max_cfg_param.max_wq =
6646 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6647 phba->sli4_hba.max_cfg_param.max_cq =
6648 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6649 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6650 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6651 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6652 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5ffc266e
JS
6653 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6654 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
da0436e9
JS
6655 phba->max_vports = phba->max_vpi;
6656 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6d368e53
JS
6657 "2003 cfg params Extents? %d "
6658 "XRI(B:%d M:%d), "
da0436e9
JS
6659 "VPI(B:%d M:%d) "
6660 "VFI(B:%d M:%d) "
6661 "RPI(B:%d M:%d) "
6d368e53
JS
6662 "FCFI(Count:%d)\n",
6663 phba->sli4_hba.extents_in_use,
da0436e9
JS
6664 phba->sli4_hba.max_cfg_param.xri_base,
6665 phba->sli4_hba.max_cfg_param.max_xri,
6666 phba->sli4_hba.max_cfg_param.vpi_base,
6667 phba->sli4_hba.max_cfg_param.max_vpi,
6668 phba->sli4_hba.max_cfg_param.vfi_base,
6669 phba->sli4_hba.max_cfg_param.max_vfi,
6670 phba->sli4_hba.max_cfg_param.rpi_base,
6671 phba->sli4_hba.max_cfg_param.max_rpi,
da0436e9 6672 phba->sli4_hba.max_cfg_param.max_fcfi);
3772a991 6673 }
912e3acd
JS
6674
6675 if (rc)
6676 goto read_cfg_out;
da0436e9
JS
6677
6678 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
b92938b4
JS
6679 if (phba->cfg_hba_queue_depth >
6680 (phba->sli4_hba.max_cfg_param.max_xri -
6681 lpfc_sli4_get_els_iocb_cnt(phba)))
da0436e9 6682 phba->cfg_hba_queue_depth =
b92938b4
JS
6683 phba->sli4_hba.max_cfg_param.max_xri -
6684 lpfc_sli4_get_els_iocb_cnt(phba);
912e3acd
JS
6685
6686 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6687 LPFC_SLI_INTF_IF_TYPE_2)
6688 goto read_cfg_out;
6689
6690 /* get the pf# and vf# for SLI4 if_type 2 port */
6691 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6692 sizeof(struct lpfc_sli4_cfg_mhdr));
6693 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6694 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6695 length, LPFC_SLI4_MBX_EMBED);
6696
8aa134a8 6697 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
912e3acd
JS
6698 shdr = (union lpfc_sli4_cfg_shdr *)
6699 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6700 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6701 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8aa134a8 6702 if (rc2 || shdr_status || shdr_add_status) {
912e3acd
JS
6703 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6704 "3026 Mailbox failed , mbxCmd x%x "
6705 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6706 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6707 bf_get(lpfc_mqe_status, &pmb->u.mqe));
912e3acd
JS
6708 goto read_cfg_out;
6709 }
6710
6711 /* search for fc_fcoe resrouce descriptor */
6712 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6713 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6714
8aa134a8
JS
6715 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6716 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6717 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6718 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6719 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6720 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6721 goto read_cfg_out;
6722
912e3acd 6723 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
8aa134a8 6724 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
912e3acd 6725 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
8aa134a8 6726 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
912e3acd
JS
6727 phba->sli4_hba.iov.pf_number =
6728 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6729 phba->sli4_hba.iov.vf_number =
6730 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6731 break;
6732 }
6733 }
6734
6735 if (i < LPFC_RSRC_DESC_MAX_NUM)
6736 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6737 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6738 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6739 phba->sli4_hba.iov.vf_number);
8aa134a8 6740 else
912e3acd
JS
6741 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6742 "3028 GET_FUNCTION_CONFIG: failed to find "
6743 "Resrouce Descriptor:x%x\n",
6744 LPFC_RSRC_DESC_TYPE_FCFCOE);
912e3acd
JS
6745
6746read_cfg_out:
6747 mempool_free(pmb, phba->mbox_mem_pool);
da0436e9 6748 return rc;
3772a991
JS
6749}
6750
6751/**
2fcee4bf 6752 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
3772a991
JS
6753 * @phba: pointer to lpfc hba data structure.
6754 *
2fcee4bf
JS
6755 * This routine is invoked to setup the port-side endian order when
6756 * the port if_type is 0. This routine has no function for other
6757 * if_types.
da0436e9
JS
6758 *
6759 * Return codes
af901ca1 6760 * 0 - successful
25985edc 6761 * -ENOMEM - No available memory
d439d286 6762 * -EIO - The mailbox failed to complete successfully.
3772a991 6763 **/
da0436e9
JS
6764static int
6765lpfc_setup_endian_order(struct lpfc_hba *phba)
3772a991 6766{
da0436e9 6767 LPFC_MBOXQ_t *mboxq;
2fcee4bf 6768 uint32_t if_type, rc = 0;
da0436e9
JS
6769 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6770 HOST_ENDIAN_HIGH_WORD1};
3772a991 6771
2fcee4bf
JS
6772 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6773 switch (if_type) {
6774 case LPFC_SLI_INTF_IF_TYPE_0:
6775 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6776 GFP_KERNEL);
6777 if (!mboxq) {
6778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6779 "0492 Unable to allocate memory for "
6780 "issuing SLI_CONFIG_SPECIAL mailbox "
6781 "command\n");
6782 return -ENOMEM;
6783 }
3772a991 6784
2fcee4bf
JS
6785 /*
6786 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6787 * two words to contain special data values and no other data.
6788 */
6789 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6790 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6791 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6792 if (rc != MBX_SUCCESS) {
6793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6794 "0493 SLI_CONFIG_SPECIAL mailbox "
6795 "failed with status x%x\n",
6796 rc);
6797 rc = -EIO;
6798 }
6799 mempool_free(mboxq, phba->mbox_mem_pool);
6800 break;
6801 case LPFC_SLI_INTF_IF_TYPE_2:
6802 case LPFC_SLI_INTF_IF_TYPE_1:
6803 default:
6804 break;
da0436e9 6805 }
da0436e9 6806 return rc;
3772a991
JS
6807}
6808
6809/**
5350d872 6810 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
3772a991
JS
6811 * @phba: pointer to lpfc hba data structure.
6812 *
5350d872
JS
6813 * This routine is invoked to check the user settable queue counts for EQs and
6814 * CQs. after this routine is called the counts will be set to valid values that
6815 * adhere to the constraints of the system's interrupt vectors and the port's
6816 * queue resources.
da0436e9
JS
6817 *
6818 * Return codes
af901ca1 6819 * 0 - successful
25985edc 6820 * -ENOMEM - No available memory
3772a991 6821 **/
da0436e9 6822static int
5350d872 6823lpfc_sli4_queue_verify(struct lpfc_hba *phba)
3772a991 6824{
67d12733 6825 int cfg_fcp_io_channel;
90695ee0
JS
6826 uint32_t cpu;
6827 uint32_t i = 0;
7bb03bbf 6828 uint32_t j = 0;
90695ee0 6829
3772a991 6830
da0436e9 6831 /*
67d12733 6832 * Sanity check for configured queue parameters against the run-time
da0436e9
JS
6833 * device parameters
6834 */
3772a991 6835
67d12733
JS
6836 /* Sanity check on HBA EQ parameters */
6837 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
6838
7bb03bbf
JS
6839 /* It doesn't make sense to have more io channels then online CPUs */
6840 for_each_present_cpu(cpu) {
6841 if (cpu_online(cpu))
6842 i++;
6843 j++;
90695ee0 6844 }
7bb03bbf
JS
6845 phba->sli4_hba.num_online_cpu = i;
6846 phba->sli4_hba.num_present_cpu = j;
6847
90695ee0 6848 if (i < cfg_fcp_io_channel) {
82c3e9ba
JS
6849 lpfc_printf_log(phba,
6850 KERN_ERR, LOG_INIT,
90695ee0 6851 "3188 Reducing IO channels to match number of "
7bb03bbf
JS
6852 "online CPUs: from %d to %d\n",
6853 cfg_fcp_io_channel, i);
90695ee0
JS
6854 cfg_fcp_io_channel = i;
6855 }
6856
67d12733
JS
6857 if (cfg_fcp_io_channel >
6858 phba->sli4_hba.max_cfg_param.max_eq) {
82c3e9ba
JS
6859 if (phba->sli4_hba.max_cfg_param.max_eq <
6860 LPFC_FCP_IO_CHAN_MIN) {
da0436e9
JS
6861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6862 "2574 Not enough EQs (%d) from the "
6863 "pci function for supporting FCP "
6864 "EQs (%d)\n",
6865 phba->sli4_hba.max_cfg_param.max_eq,
67d12733 6866 phba->cfg_fcp_io_channel);
da0436e9
JS
6867 goto out_error;
6868 }
82c3e9ba
JS
6869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6870 "2575 Reducing IO channels to match number of "
6871 "available EQs: from %d to %d\n",
6872 cfg_fcp_io_channel,
6873 phba->sli4_hba.max_cfg_param.max_eq);
6874 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq;
da0436e9 6875 }
67d12733
JS
6876
6877 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
6878
da0436e9 6879 /* The actual number of FCP event queues adopted */
67d12733
JS
6880 phba->cfg_fcp_eq_count = cfg_fcp_io_channel;
6881 phba->cfg_fcp_wq_count = cfg_fcp_io_channel;
6882 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
3772a991 6883
da0436e9
JS
6884 /* Get EQ depth from module parameter, fake the default for now */
6885 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6886 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
3772a991 6887
5350d872
JS
6888 /* Get CQ depth from module parameter, fake the default for now */
6889 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6890 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6891
6892 return 0;
6893out_error:
6894 return -ENOMEM;
6895}
6896
6897/**
6898 * lpfc_sli4_queue_create - Create all the SLI4 queues
6899 * @phba: pointer to lpfc hba data structure.
6900 *
6901 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6902 * operation. For each SLI4 queue type, the parameters such as queue entry
6903 * count (queue depth) shall be taken from the module parameter. For now,
6904 * we just use some constant number as place holder.
6905 *
6906 * Return codes
4907cb7b 6907 * 0 - successful
5350d872
JS
6908 * -ENOMEM - No availble memory
6909 * -EIO - The mailbox failed to complete successfully.
6910 **/
6911int
6912lpfc_sli4_queue_create(struct lpfc_hba *phba)
6913{
6914 struct lpfc_queue *qdesc;
67d12733 6915 int idx;
5350d872
JS
6916
6917 /*
67d12733 6918 * Create HBA Record arrays.
5350d872 6919 */
67d12733
JS
6920 if (!phba->cfg_fcp_io_channel)
6921 return -ERANGE;
5350d872 6922
67d12733
JS
6923 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6924 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6925 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6926 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6927 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6928 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6929
6930 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6931 phba->cfg_fcp_io_channel), GFP_KERNEL);
6932 if (!phba->sli4_hba.hba_eq) {
6933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6934 "2576 Failed allocate memory for "
6935 "fast-path EQ record array\n");
6936 goto out_error;
6937 }
6938
6939 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6940 phba->cfg_fcp_io_channel), GFP_KERNEL);
6941 if (!phba->sli4_hba.fcp_cq) {
da0436e9 6942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
67d12733
JS
6943 "2577 Failed allocate memory for fast-path "
6944 "CQ record array\n");
6945 goto out_error;
6946 }
6947
6948 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6949 phba->cfg_fcp_io_channel), GFP_KERNEL);
6950 if (!phba->sli4_hba.fcp_wq) {
6951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6952 "2578 Failed allocate memory for fast-path "
6953 "WQ record array\n");
da0436e9
JS
6954 goto out_error;
6955 }
da0436e9 6956
5350d872 6957 /*
67d12733
JS
6958 * Since the first EQ can have multiple CQs associated with it,
6959 * this array is used to quickly see if we have a FCP fast-path
6960 * CQ match.
5350d872 6961 */
67d12733
JS
6962 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
6963 phba->cfg_fcp_io_channel), GFP_KERNEL);
6964 if (!phba->sli4_hba.fcp_cq_map) {
6965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6966 "2545 Failed allocate memory for fast-path "
6967 "CQ map\n");
6968 goto out_error;
da0436e9 6969 }
67d12733
JS
6970
6971 /*
6972 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
6973 * how many EQs to create.
6974 */
6975 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6976
6977 /* Create EQs */
da0436e9
JS
6978 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6979 phba->sli4_hba.eq_ecount);
6980 if (!qdesc) {
6981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
67d12733
JS
6982 "0497 Failed allocate EQ (%d)\n", idx);
6983 goto out_error;
da0436e9 6984 }
67d12733
JS
6985 phba->sli4_hba.hba_eq[idx] = qdesc;
6986
6987 /* Create Fast Path FCP CQs */
6988 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6989 phba->sli4_hba.cq_ecount);
6990 if (!qdesc) {
6991 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6992 "0499 Failed allocate fast-path FCP "
6993 "CQ (%d)\n", idx);
6994 goto out_error;
6995 }
6996 phba->sli4_hba.fcp_cq[idx] = qdesc;
6997
6998 /* Create Fast Path FCP WQs */
6999 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7000 phba->sli4_hba.wq_ecount);
7001 if (!qdesc) {
7002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7003 "0503 Failed allocate fast-path FCP "
7004 "WQ (%d)\n", idx);
7005 goto out_error;
7006 }
7007 phba->sli4_hba.fcp_wq[idx] = qdesc;
da0436e9
JS
7008 }
7009
67d12733 7010
da0436e9 7011 /*
67d12733 7012 * Create Slow Path Completion Queues (CQs)
da0436e9
JS
7013 */
7014
da0436e9
JS
7015 /* Create slow-path Mailbox Command Complete Queue */
7016 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7017 phba->sli4_hba.cq_ecount);
7018 if (!qdesc) {
7019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7020 "0500 Failed allocate slow-path mailbox CQ\n");
67d12733 7021 goto out_error;
da0436e9
JS
7022 }
7023 phba->sli4_hba.mbx_cq = qdesc;
7024
7025 /* Create slow-path ELS Complete Queue */
7026 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7027 phba->sli4_hba.cq_ecount);
7028 if (!qdesc) {
7029 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7030 "0501 Failed allocate slow-path ELS CQ\n");
67d12733 7031 goto out_error;
da0436e9
JS
7032 }
7033 phba->sli4_hba.els_cq = qdesc;
7034
da0436e9 7035
5350d872 7036 /*
67d12733 7037 * Create Slow Path Work Queues (WQs)
5350d872 7038 */
da0436e9
JS
7039
7040 /* Create Mailbox Command Queue */
da0436e9
JS
7041
7042 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7043 phba->sli4_hba.mq_ecount);
7044 if (!qdesc) {
7045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7046 "0505 Failed allocate slow-path MQ\n");
67d12733 7047 goto out_error;
da0436e9
JS
7048 }
7049 phba->sli4_hba.mbx_wq = qdesc;
7050
7051 /*
67d12733 7052 * Create ELS Work Queues
da0436e9 7053 */
da0436e9
JS
7054
7055 /* Create slow-path ELS Work Queue */
7056 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7057 phba->sli4_hba.wq_ecount);
7058 if (!qdesc) {
7059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7060 "0504 Failed allocate slow-path ELS WQ\n");
67d12733 7061 goto out_error;
da0436e9
JS
7062 }
7063 phba->sli4_hba.els_wq = qdesc;
7064
da0436e9
JS
7065 /*
7066 * Create Receive Queue (RQ)
7067 */
da0436e9
JS
7068
7069 /* Create Receive Queue for header */
7070 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7071 phba->sli4_hba.rq_ecount);
7072 if (!qdesc) {
7073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7074 "0506 Failed allocate receive HRQ\n");
67d12733 7075 goto out_error;
da0436e9
JS
7076 }
7077 phba->sli4_hba.hdr_rq = qdesc;
7078
7079 /* Create Receive Queue for data */
7080 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7081 phba->sli4_hba.rq_ecount);
7082 if (!qdesc) {
7083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7084 "0507 Failed allocate receive DRQ\n");
67d12733 7085 goto out_error;
da0436e9
JS
7086 }
7087 phba->sli4_hba.dat_rq = qdesc;
7088
7089 return 0;
7090
da0436e9 7091out_error:
67d12733 7092 lpfc_sli4_queue_destroy(phba);
da0436e9
JS
7093 return -ENOMEM;
7094}
7095
7096/**
7097 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7098 * @phba: pointer to lpfc hba data structure.
7099 *
7100 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7101 * operation.
7102 *
7103 * Return codes
af901ca1 7104 * 0 - successful
25985edc 7105 * -ENOMEM - No available memory
d439d286 7106 * -EIO - The mailbox failed to complete successfully.
da0436e9 7107 **/
5350d872 7108void
da0436e9
JS
7109lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7110{
67d12733
JS
7111 int idx;
7112
7113 if (phba->sli4_hba.hba_eq != NULL) {
7114 /* Release HBA event queue */
7115 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7116 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7117 lpfc_sli4_queue_free(
7118 phba->sli4_hba.hba_eq[idx]);
7119 phba->sli4_hba.hba_eq[idx] = NULL;
7120 }
7121 }
7122 kfree(phba->sli4_hba.hba_eq);
7123 phba->sli4_hba.hba_eq = NULL;
7124 }
7125
7126 if (phba->sli4_hba.fcp_cq != NULL) {
7127 /* Release FCP completion queue */
7128 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7129 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7130 lpfc_sli4_queue_free(
7131 phba->sli4_hba.fcp_cq[idx]);
7132 phba->sli4_hba.fcp_cq[idx] = NULL;
7133 }
7134 }
7135 kfree(phba->sli4_hba.fcp_cq);
7136 phba->sli4_hba.fcp_cq = NULL;
7137 }
7138
7139 if (phba->sli4_hba.fcp_wq != NULL) {
7140 /* Release FCP work queue */
7141 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7142 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7143 lpfc_sli4_queue_free(
7144 phba->sli4_hba.fcp_wq[idx]);
7145 phba->sli4_hba.fcp_wq[idx] = NULL;
7146 }
7147 }
7148 kfree(phba->sli4_hba.fcp_wq);
7149 phba->sli4_hba.fcp_wq = NULL;
7150 }
7151
962bc51b
JS
7152 if (phba->pci_bar0_memmap_p) {
7153 iounmap(phba->pci_bar0_memmap_p);
7154 phba->pci_bar0_memmap_p = NULL;
7155 }
7156 if (phba->pci_bar2_memmap_p) {
7157 iounmap(phba->pci_bar2_memmap_p);
7158 phba->pci_bar2_memmap_p = NULL;
7159 }
7160 if (phba->pci_bar4_memmap_p) {
7161 iounmap(phba->pci_bar4_memmap_p);
7162 phba->pci_bar4_memmap_p = NULL;
7163 }
7164
67d12733
JS
7165 /* Release FCP CQ mapping array */
7166 if (phba->sli4_hba.fcp_cq_map != NULL) {
7167 kfree(phba->sli4_hba.fcp_cq_map);
7168 phba->sli4_hba.fcp_cq_map = NULL;
7169 }
da0436e9
JS
7170
7171 /* Release mailbox command work queue */
67d12733
JS
7172 if (phba->sli4_hba.mbx_wq != NULL) {
7173 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7174 phba->sli4_hba.mbx_wq = NULL;
7175 }
da0436e9
JS
7176
7177 /* Release ELS work queue */
67d12733
JS
7178 if (phba->sli4_hba.els_wq != NULL) {
7179 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7180 phba->sli4_hba.els_wq = NULL;
7181 }
da0436e9
JS
7182
7183 /* Release unsolicited receive queue */
67d12733
JS
7184 if (phba->sli4_hba.hdr_rq != NULL) {
7185 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7186 phba->sli4_hba.hdr_rq = NULL;
7187 }
7188 if (phba->sli4_hba.dat_rq != NULL) {
7189 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7190 phba->sli4_hba.dat_rq = NULL;
7191 }
da0436e9 7192
da0436e9 7193 /* Release ELS complete queue */
67d12733
JS
7194 if (phba->sli4_hba.els_cq != NULL) {
7195 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7196 phba->sli4_hba.els_cq = NULL;
7197 }
da0436e9
JS
7198
7199 /* Release mailbox command complete queue */
67d12733
JS
7200 if (phba->sli4_hba.mbx_cq != NULL) {
7201 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7202 phba->sli4_hba.mbx_cq = NULL;
7203 }
da0436e9
JS
7204
7205 return;
7206}
7207
7208/**
7209 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7210 * @phba: pointer to lpfc hba data structure.
7211 *
7212 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7213 * operation.
7214 *
7215 * Return codes
af901ca1 7216 * 0 - successful
25985edc 7217 * -ENOMEM - No available memory
d439d286 7218 * -EIO - The mailbox failed to complete successfully.
da0436e9
JS
7219 **/
7220int
7221lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7222{
2a76a283
JS
7223 struct lpfc_sli *psli = &phba->sli;
7224 struct lpfc_sli_ring *pring;
da0436e9
JS
7225 int rc = -ENOMEM;
7226 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7227 int fcp_cq_index = 0;
962bc51b
JS
7228 uint32_t shdr_status, shdr_add_status;
7229 union lpfc_sli4_cfg_shdr *shdr;
7230 LPFC_MBOXQ_t *mboxq;
7231 uint32_t length;
7232
7233 /* Check for dual-ULP support */
7234 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7235 if (!mboxq) {
7236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7237 "3249 Unable to allocate memory for "
7238 "QUERY_FW_CFG mailbox command\n");
7239 return -ENOMEM;
7240 }
7241 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7242 sizeof(struct lpfc_sli4_cfg_mhdr));
7243 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7244 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7245 length, LPFC_SLI4_MBX_EMBED);
7246
7247 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7248
7249 shdr = (union lpfc_sli4_cfg_shdr *)
7250 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7251 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7252 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7253 if (shdr_status || shdr_add_status || rc) {
7254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7255 "3250 QUERY_FW_CFG mailbox failed with status "
7256 "x%x add_status x%x, mbx status x%x\n",
7257 shdr_status, shdr_add_status, rc);
7258 if (rc != MBX_TIMEOUT)
7259 mempool_free(mboxq, phba->mbox_mem_pool);
7260 rc = -ENXIO;
7261 goto out_error;
7262 }
7263
7264 phba->sli4_hba.fw_func_mode =
7265 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7266 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7267 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7268 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7269 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7270 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7271 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7272
7273 if (rc != MBX_TIMEOUT)
7274 mempool_free(mboxq, phba->mbox_mem_pool);
da0436e9
JS
7275
7276 /*
67d12733 7277 * Set up HBA Event Queues (EQs)
da0436e9
JS
7278 */
7279
67d12733
JS
7280 /* Set up HBA event queue */
7281 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
2e90f4b5
JS
7282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7283 "3147 Fast-path EQs not allocated\n");
1b51197d 7284 rc = -ENOMEM;
67d12733 7285 goto out_error;
2e90f4b5 7286 }
67d12733
JS
7287 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7288 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
da0436e9
JS
7289 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7290 "0522 Fast-path EQ (%d) not "
7291 "allocated\n", fcp_eqidx);
1b51197d 7292 rc = -ENOMEM;
67d12733 7293 goto out_destroy_hba_eq;
da0436e9 7294 }
67d12733 7295 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
bf8dae83 7296 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
da0436e9
JS
7297 if (rc) {
7298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7299 "0523 Failed setup of fast-path EQ "
7300 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
67d12733 7301 goto out_destroy_hba_eq;
da0436e9
JS
7302 }
7303 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
67d12733 7304 "2584 HBA EQ setup: "
da0436e9 7305 "queue[%d]-id=%d\n", fcp_eqidx,
67d12733 7306 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
da0436e9
JS
7307 }
7308
67d12733
JS
7309 /* Set up fast-path FCP Response Complete Queue */
7310 if (!phba->sli4_hba.fcp_cq) {
7311 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7312 "3148 Fast-path FCP CQ array not "
7313 "allocated\n");
7314 rc = -ENOMEM;
7315 goto out_destroy_hba_eq;
7316 }
7317
7318 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7319 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7321 "0526 Fast-path FCP CQ (%d) not "
7322 "allocated\n", fcp_cqidx);
7323 rc = -ENOMEM;
7324 goto out_destroy_fcp_cq;
7325 }
7326 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7327 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7328 if (rc) {
7329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7330 "0527 Failed setup of fast-path FCP "
7331 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7332 goto out_destroy_fcp_cq;
7333 }
7334
7335 /* Setup fcp_cq_map for fast lookup */
7336 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7337 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7338
7339 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7340 "2588 FCP CQ setup: cq[%d]-id=%d, "
7341 "parent seq[%d]-id=%d\n",
7342 fcp_cqidx,
7343 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7344 fcp_cqidx,
7345 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7346 }
7347
7348 /* Set up fast-path FCP Work Queue */
7349 if (!phba->sli4_hba.fcp_wq) {
7350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7351 "3149 Fast-path FCP WQ array not "
7352 "allocated\n");
7353 rc = -ENOMEM;
7354 goto out_destroy_fcp_cq;
7355 }
7356
7357 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7358 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7360 "0534 Fast-path FCP WQ (%d) not "
7361 "allocated\n", fcp_wqidx);
7362 rc = -ENOMEM;
7363 goto out_destroy_fcp_wq;
7364 }
7365 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7366 phba->sli4_hba.fcp_cq[fcp_wqidx],
7367 LPFC_FCP);
7368 if (rc) {
7369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7370 "0535 Failed setup of fast-path FCP "
7371 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7372 goto out_destroy_fcp_wq;
7373 }
7374
7375 /* Bind this WQ to the next FCP ring */
7376 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7377 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7378 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7379
7380 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7381 "2591 FCP WQ setup: wq[%d]-id=%d, "
7382 "parent cq[%d]-id=%d\n",
7383 fcp_wqidx,
7384 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7385 fcp_cq_index,
7386 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7387 }
da0436e9
JS
7388 /*
7389 * Set up Complete Queues (CQs)
7390 */
7391
7392 /* Set up slow-path MBOX Complete Queue as the first CQ */
7393 if (!phba->sli4_hba.mbx_cq) {
7394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7395 "0528 Mailbox CQ not allocated\n");
1b51197d 7396 rc = -ENOMEM;
67d12733 7397 goto out_destroy_fcp_wq;
da0436e9 7398 }
67d12733
JS
7399 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7400 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
da0436e9
JS
7401 if (rc) {
7402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7403 "0529 Failed setup of slow-path mailbox CQ: "
7404 "rc = 0x%x\n", rc);
67d12733 7405 goto out_destroy_fcp_wq;
da0436e9
JS
7406 }
7407 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7408 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7409 phba->sli4_hba.mbx_cq->queue_id,
67d12733 7410 phba->sli4_hba.hba_eq[0]->queue_id);
da0436e9
JS
7411
7412 /* Set up slow-path ELS Complete Queue */
7413 if (!phba->sli4_hba.els_cq) {
7414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7415 "0530 ELS CQ not allocated\n");
1b51197d 7416 rc = -ENOMEM;
da0436e9
JS
7417 goto out_destroy_mbx_cq;
7418 }
67d12733
JS
7419 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7420 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
da0436e9
JS
7421 if (rc) {
7422 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7423 "0531 Failed setup of slow-path ELS CQ: "
7424 "rc = 0x%x\n", rc);
7425 goto out_destroy_mbx_cq;
7426 }
7427 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7428 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7429 phba->sli4_hba.els_cq->queue_id,
67d12733 7430 phba->sli4_hba.hba_eq[0]->queue_id);
da0436e9
JS
7431
7432 /*
7433 * Set up all the Work Queues (WQs)
7434 */
7435
7436 /* Set up Mailbox Command Queue */
7437 if (!phba->sli4_hba.mbx_wq) {
7438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7439 "0538 Slow-path MQ not allocated\n");
1b51197d 7440 rc = -ENOMEM;
67d12733 7441 goto out_destroy_els_cq;
da0436e9
JS
7442 }
7443 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7444 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7445 if (rc) {
7446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7447 "0539 Failed setup of slow-path MQ: "
7448 "rc = 0x%x\n", rc);
67d12733 7449 goto out_destroy_els_cq;
da0436e9
JS
7450 }
7451 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7452 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7453 phba->sli4_hba.mbx_wq->queue_id,
7454 phba->sli4_hba.mbx_cq->queue_id);
7455
7456 /* Set up slow-path ELS Work Queue */
7457 if (!phba->sli4_hba.els_wq) {
7458 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7459 "0536 Slow-path ELS WQ not allocated\n");
1b51197d 7460 rc = -ENOMEM;
da0436e9
JS
7461 goto out_destroy_mbx_wq;
7462 }
7463 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7464 phba->sli4_hba.els_cq, LPFC_ELS);
7465 if (rc) {
7466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7467 "0537 Failed setup of slow-path ELS WQ: "
7468 "rc = 0x%x\n", rc);
7469 goto out_destroy_mbx_wq;
7470 }
2a76a283
JS
7471
7472 /* Bind this WQ to the ELS ring */
7473 pring = &psli->ring[LPFC_ELS_RING];
7474 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7475 phba->sli4_hba.els_cq->pring = pring;
7476
da0436e9
JS
7477 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7478 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7479 phba->sli4_hba.els_wq->queue_id,
7480 phba->sli4_hba.els_cq->queue_id);
7481
da0436e9
JS
7482 /*
7483 * Create Receive Queue (RQ)
7484 */
7485 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7487 "0540 Receive Queue not allocated\n");
1b51197d 7488 rc = -ENOMEM;
67d12733 7489 goto out_destroy_els_wq;
da0436e9 7490 }
73d91e50
JS
7491
7492 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7493 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7494
da0436e9 7495 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
4d9ab994 7496 phba->sli4_hba.els_cq, LPFC_USOL);
da0436e9
JS
7497 if (rc) {
7498 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7499 "0541 Failed setup of Receive Queue: "
7500 "rc = 0x%x\n", rc);
7501 goto out_destroy_fcp_wq;
7502 }
73d91e50 7503
da0436e9
JS
7504 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7505 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7506 "parent cq-id=%d\n",
7507 phba->sli4_hba.hdr_rq->queue_id,
7508 phba->sli4_hba.dat_rq->queue_id,
4d9ab994 7509 phba->sli4_hba.els_cq->queue_id);
da0436e9
JS
7510 return 0;
7511
2e90f4b5 7512out_destroy_els_wq:
da0436e9
JS
7513 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7514out_destroy_mbx_wq:
7515 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
2e90f4b5 7516out_destroy_els_cq:
da0436e9
JS
7517 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7518out_destroy_mbx_cq:
7519 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
67d12733
JS
7520out_destroy_fcp_wq:
7521 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7522 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7523out_destroy_fcp_cq:
7524 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7525 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7526out_destroy_hba_eq:
da0436e9 7527 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
67d12733 7528 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
da0436e9
JS
7529out_error:
7530 return rc;
7531}
7532
7533/**
7534 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7535 * @phba: pointer to lpfc hba data structure.
7536 *
7537 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7538 * operation.
7539 *
7540 * Return codes
af901ca1 7541 * 0 - successful
25985edc 7542 * -ENOMEM - No available memory
d439d286 7543 * -EIO - The mailbox failed to complete successfully.
da0436e9
JS
7544 **/
7545void
7546lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7547{
7548 int fcp_qidx;
7549
7550 /* Unset mailbox command work queue */
7551 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7552 /* Unset ELS work queue */
7553 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7554 /* Unset unsolicited receive queue */
7555 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7556 /* Unset FCP work queue */
67d12733
JS
7557 if (phba->sli4_hba.fcp_wq) {
7558 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7559 fcp_qidx++)
7560 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7561 }
da0436e9
JS
7562 /* Unset mailbox command complete queue */
7563 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7564 /* Unset ELS complete queue */
7565 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
da0436e9 7566 /* Unset FCP response complete queue */
2e90f4b5 7567 if (phba->sli4_hba.fcp_cq) {
67d12733
JS
7568 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7569 fcp_qidx++)
2e90f4b5 7570 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
2e90f4b5 7571 }
da0436e9 7572 /* Unset fast-path event queue */
67d12733
JS
7573 if (phba->sli4_hba.hba_eq) {
7574 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
2e90f4b5 7575 fcp_qidx++)
67d12733 7576 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
2e90f4b5 7577 }
da0436e9
JS
7578}
7579
7580/**
7581 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7582 * @phba: pointer to lpfc hba data structure.
7583 *
7584 * This routine is invoked to allocate and set up a pool of completion queue
7585 * events. The body of the completion queue event is a completion queue entry
7586 * CQE. For now, this pool is used for the interrupt service routine to queue
7587 * the following HBA completion queue events for the worker thread to process:
7588 * - Mailbox asynchronous events
7589 * - Receive queue completion unsolicited events
7590 * Later, this can be used for all the slow-path events.
7591 *
7592 * Return codes
af901ca1 7593 * 0 - successful
25985edc 7594 * -ENOMEM - No available memory
da0436e9
JS
7595 **/
7596static int
7597lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7598{
7599 struct lpfc_cq_event *cq_event;
7600 int i;
7601
7602 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7603 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7604 if (!cq_event)
7605 goto out_pool_create_fail;
7606 list_add_tail(&cq_event->list,
7607 &phba->sli4_hba.sp_cqe_event_pool);
7608 }
7609 return 0;
7610
7611out_pool_create_fail:
7612 lpfc_sli4_cq_event_pool_destroy(phba);
7613 return -ENOMEM;
7614}
7615
7616/**
7617 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7618 * @phba: pointer to lpfc hba data structure.
7619 *
7620 * This routine is invoked to free the pool of completion queue events at
7621 * driver unload time. Note that, it is the responsibility of the driver
7622 * cleanup routine to free all the outstanding completion-queue events
7623 * allocated from this pool back into the pool before invoking this routine
7624 * to destroy the pool.
7625 **/
7626static void
7627lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7628{
7629 struct lpfc_cq_event *cq_event, *next_cq_event;
7630
7631 list_for_each_entry_safe(cq_event, next_cq_event,
7632 &phba->sli4_hba.sp_cqe_event_pool, list) {
7633 list_del(&cq_event->list);
7634 kfree(cq_event);
7635 }
7636}
7637
7638/**
7639 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7640 * @phba: pointer to lpfc hba data structure.
7641 *
7642 * This routine is the lock free version of the API invoked to allocate a
7643 * completion-queue event from the free pool.
7644 *
7645 * Return: Pointer to the newly allocated completion-queue event if successful
7646 * NULL otherwise.
7647 **/
7648struct lpfc_cq_event *
7649__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7650{
7651 struct lpfc_cq_event *cq_event = NULL;
7652
7653 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7654 struct lpfc_cq_event, list);
7655 return cq_event;
7656}
7657
7658/**
7659 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7660 * @phba: pointer to lpfc hba data structure.
7661 *
7662 * This routine is the lock version of the API invoked to allocate a
7663 * completion-queue event from the free pool.
7664 *
7665 * Return: Pointer to the newly allocated completion-queue event if successful
7666 * NULL otherwise.
7667 **/
7668struct lpfc_cq_event *
7669lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7670{
7671 struct lpfc_cq_event *cq_event;
7672 unsigned long iflags;
7673
7674 spin_lock_irqsave(&phba->hbalock, iflags);
7675 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7676 spin_unlock_irqrestore(&phba->hbalock, iflags);
7677 return cq_event;
7678}
7679
7680/**
7681 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7682 * @phba: pointer to lpfc hba data structure.
7683 * @cq_event: pointer to the completion queue event to be freed.
7684 *
7685 * This routine is the lock free version of the API invoked to release a
7686 * completion-queue event back into the free pool.
7687 **/
7688void
7689__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7690 struct lpfc_cq_event *cq_event)
7691{
7692 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7693}
7694
7695/**
7696 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7697 * @phba: pointer to lpfc hba data structure.
7698 * @cq_event: pointer to the completion queue event to be freed.
7699 *
7700 * This routine is the lock version of the API invoked to release a
7701 * completion-queue event back into the free pool.
7702 **/
7703void
7704lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7705 struct lpfc_cq_event *cq_event)
7706{
7707 unsigned long iflags;
7708 spin_lock_irqsave(&phba->hbalock, iflags);
7709 __lpfc_sli4_cq_event_release(phba, cq_event);
7710 spin_unlock_irqrestore(&phba->hbalock, iflags);
7711}
7712
7713/**
7714 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7715 * @phba: pointer to lpfc hba data structure.
7716 *
7717 * This routine is to free all the pending completion-queue events to the
7718 * back into the free pool for device reset.
7719 **/
7720static void
7721lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7722{
7723 LIST_HEAD(cqelist);
7724 struct lpfc_cq_event *cqe;
7725 unsigned long iflags;
7726
7727 /* Retrieve all the pending WCQEs from pending WCQE lists */
7728 spin_lock_irqsave(&phba->hbalock, iflags);
7729 /* Pending FCP XRI abort events */
7730 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7731 &cqelist);
7732 /* Pending ELS XRI abort events */
7733 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7734 &cqelist);
7735 /* Pending asynnc events */
7736 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7737 &cqelist);
7738 spin_unlock_irqrestore(&phba->hbalock, iflags);
7739
7740 while (!list_empty(&cqelist)) {
7741 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7742 lpfc_sli4_cq_event_release(phba, cqe);
7743 }
7744}
7745
7746/**
7747 * lpfc_pci_function_reset - Reset pci function.
7748 * @phba: pointer to lpfc hba data structure.
7749 *
7750 * This routine is invoked to request a PCI function reset. It will destroys
7751 * all resources assigned to the PCI function which originates this request.
7752 *
7753 * Return codes
af901ca1 7754 * 0 - successful
25985edc 7755 * -ENOMEM - No available memory
d439d286 7756 * -EIO - The mailbox failed to complete successfully.
da0436e9
JS
7757 **/
7758int
7759lpfc_pci_function_reset(struct lpfc_hba *phba)
7760{
7761 LPFC_MBOXQ_t *mboxq;
2fcee4bf 7762 uint32_t rc = 0, if_type;
da0436e9 7763 uint32_t shdr_status, shdr_add_status;
2fcee4bf 7764 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
da0436e9 7765 union lpfc_sli4_cfg_shdr *shdr;
2fcee4bf 7766 struct lpfc_register reg_data;
2b81f942 7767 uint16_t devid;
da0436e9 7768
2fcee4bf
JS
7769 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7770 switch (if_type) {
7771 case LPFC_SLI_INTF_IF_TYPE_0:
7772 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7773 GFP_KERNEL);
7774 if (!mboxq) {
7775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7776 "0494 Unable to allocate memory for "
7777 "issuing SLI_FUNCTION_RESET mailbox "
7778 "command\n");
7779 return -ENOMEM;
7780 }
da0436e9 7781
2fcee4bf
JS
7782 /* Setup PCI function reset mailbox-ioctl command */
7783 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7784 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7785 LPFC_SLI4_MBX_EMBED);
7786 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7787 shdr = (union lpfc_sli4_cfg_shdr *)
7788 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7789 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7790 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7791 &shdr->response);
7792 if (rc != MBX_TIMEOUT)
7793 mempool_free(mboxq, phba->mbox_mem_pool);
7794 if (shdr_status || shdr_add_status || rc) {
7795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7796 "0495 SLI_FUNCTION_RESET mailbox "
7797 "failed with status x%x add_status x%x,"
7798 " mbx status x%x\n",
7799 shdr_status, shdr_add_status, rc);
7800 rc = -ENXIO;
7801 }
7802 break;
7803 case LPFC_SLI_INTF_IF_TYPE_2:
7804 for (num_resets = 0;
7805 num_resets < MAX_IF_TYPE_2_RESETS;
7806 num_resets++) {
7807 reg_data.word0 = 0;
7808 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7809 LPFC_SLIPORT_LITTLE_ENDIAN);
7810 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7811 LPFC_SLIPORT_INIT_PORT);
7812 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7813 CTRLregaddr);
8fcb8acd 7814 /* flush */
2b81f942
JS
7815 pci_read_config_word(phba->pcidev,
7816 PCI_DEVICE_ID, &devid);
2fcee4bf
JS
7817 /*
7818 * Poll the Port Status Register and wait for RDY for
7819 * up to 10 seconds. If the port doesn't respond, treat
7820 * it as an error. If the port responds with RN, start
7821 * the loop again.
7822 */
7823 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
73d91e50 7824 msleep(10);
9940b97b
JS
7825 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7826 STATUSregaddr, &reg_data.word0)) {
7827 rc = -ENODEV;
73d91e50 7828 goto out;
9940b97b 7829 }
6b5151fd 7830 if (bf_get(lpfc_sliport_status_rn, &reg_data))
2fcee4bf 7831 reset_again++;
6b5151fd 7832 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
2fcee4bf 7833 break;
2fcee4bf
JS
7834 }
7835
7836 /*
7837 * If the port responds to the init request with
7838 * reset needed, delay for a bit and restart the loop.
7839 */
6b5151fd 7840 if (reset_again && (rdy_chk < 1000)) {
2fcee4bf
JS
7841 msleep(10);
7842 reset_again = 0;
7843 continue;
7844 }
7845
7846 /* Detect any port errors. */
2fcee4bf
JS
7847 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7848 (rdy_chk >= 1000)) {
7849 phba->work_status[0] = readl(
7850 phba->sli4_hba.u.if_type2.ERR1regaddr);
7851 phba->work_status[1] = readl(
7852 phba->sli4_hba.u.if_type2.ERR2regaddr);
7853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8fcb8acd 7854 "2890 Port error detected during port "
8a9d2e80
JS
7855 "reset(%d): wait_tmo:%d ms, "
7856 "port status reg 0x%x, "
2fcee4bf 7857 "error 1=0x%x, error 2=0x%x\n",
8a9d2e80
JS
7858 num_resets, rdy_chk*10,
7859 reg_data.word0,
2fcee4bf
JS
7860 phba->work_status[0],
7861 phba->work_status[1]);
7862 rc = -ENODEV;
7863 }
7864
7865 /*
7866 * Terminate the outer loop provided the Port indicated
7867 * ready within 10 seconds.
7868 */
7869 if (rdy_chk < 1000)
7870 break;
7871 }
0558056c
JS
7872 /* delay driver action following IF_TYPE_2 function reset */
7873 msleep(100);
2fcee4bf
JS
7874 break;
7875 case LPFC_SLI_INTF_IF_TYPE_1:
7876 default:
7877 break;
da0436e9 7878 }
2fcee4bf 7879
73d91e50 7880out:
2fcee4bf 7881 /* Catch the not-ready port failure after a port reset. */
229adb0e
JS
7882 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
7883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7884 "3317 HBA not functional: IP Reset Failed "
7885 "after (%d) retries, try: "
7886 "echo fw_reset > board_mode\n", num_resets);
2fcee4bf 7887 rc = -ENODEV;
229adb0e 7888 }
2fcee4bf 7889
da0436e9
JS
7890 return rc;
7891}
7892
da0436e9
JS
7893/**
7894 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7895 * @phba: pointer to lpfc hba data structure.
7896 *
7897 * This routine is invoked to set up the PCI device memory space for device
7898 * with SLI-4 interface spec.
7899 *
7900 * Return codes
af901ca1 7901 * 0 - successful
da0436e9
JS
7902 * other values - error
7903 **/
7904static int
7905lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7906{
7907 struct pci_dev *pdev;
7908 unsigned long bar0map_len, bar1map_len, bar2map_len;
7909 int error = -ENODEV;
2fcee4bf 7910 uint32_t if_type;
da0436e9
JS
7911
7912 /* Obtain PCI device reference */
7913 if (!phba->pcidev)
7914 return error;
7915 else
7916 pdev = phba->pcidev;
7917
7918 /* Set the device DMA mask size */
8e68597d
MR
7919 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7920 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7921 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7922 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
da0436e9 7923 return error;
8e68597d
MR
7924 }
7925 }
da0436e9 7926
2fcee4bf
JS
7927 /*
7928 * The BARs and register set definitions and offset locations are
7929 * dependent on the if_type.
7930 */
7931 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7932 &phba->sli4_hba.sli_intf.word0)) {
7933 return error;
7934 }
7935
7936 /* There is no SLI3 failback for SLI4 devices. */
7937 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7938 LPFC_SLI_INTF_VALID) {
7939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7940 "2894 SLI_INTF reg contents invalid "
7941 "sli_intf reg 0x%x\n",
7942 phba->sli4_hba.sli_intf.word0);
7943 return error;
7944 }
7945
7946 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7947 /*
7948 * Get the bus address of SLI4 device Bar regions and the
7949 * number of bytes required by each mapping. The mapping of the
7950 * particular PCI BARs regions is dependent on the type of
7951 * SLI4 device.
da0436e9 7952 */
1dfb5a47
JS
7953 if (pci_resource_start(pdev, 0)) {
7954 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7955 bar0map_len = pci_resource_len(pdev, 0);
2fcee4bf
JS
7956
7957 /*
7958 * Map SLI4 PCI Config Space Register base to a kernel virtual
7959 * addr
7960 */
7961 phba->sli4_hba.conf_regs_memmap_p =
7962 ioremap(phba->pci_bar0_map, bar0map_len);
7963 if (!phba->sli4_hba.conf_regs_memmap_p) {
7964 dev_printk(KERN_ERR, &pdev->dev,
7965 "ioremap failed for SLI4 PCI config "
7966 "registers.\n");
7967 goto out;
7968 }
7969 /* Set up BAR0 PCI config space register memory map */
7970 lpfc_sli4_bar0_register_memmap(phba, if_type);
1dfb5a47
JS
7971 } else {
7972 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7973 bar0map_len = pci_resource_len(pdev, 1);
2fcee4bf
JS
7974 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7975 dev_printk(KERN_ERR, &pdev->dev,
7976 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7977 goto out;
7978 }
7979 phba->sli4_hba.conf_regs_memmap_p =
da0436e9 7980 ioremap(phba->pci_bar0_map, bar0map_len);
2fcee4bf
JS
7981 if (!phba->sli4_hba.conf_regs_memmap_p) {
7982 dev_printk(KERN_ERR, &pdev->dev,
7983 "ioremap failed for SLI4 PCI config "
7984 "registers.\n");
7985 goto out;
7986 }
7987 lpfc_sli4_bar0_register_memmap(phba, if_type);
da0436e9
JS
7988 }
7989
c31098ce
JS
7990 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7991 (pci_resource_start(pdev, 2))) {
2fcee4bf
JS
7992 /*
7993 * Map SLI4 if type 0 HBA Control Register base to a kernel
7994 * virtual address and setup the registers.
7995 */
7996 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7997 bar1map_len = pci_resource_len(pdev, 2);
7998 phba->sli4_hba.ctrl_regs_memmap_p =
da0436e9 7999 ioremap(phba->pci_bar1_map, bar1map_len);
2fcee4bf
JS
8000 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8001 dev_printk(KERN_ERR, &pdev->dev,
da0436e9 8002 "ioremap failed for SLI4 HBA control registers.\n");
2fcee4bf
JS
8003 goto out_iounmap_conf;
8004 }
8005 lpfc_sli4_bar1_register_memmap(phba);
da0436e9
JS
8006 }
8007
c31098ce
JS
8008 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
8009 (pci_resource_start(pdev, 4))) {
2fcee4bf
JS
8010 /*
8011 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8012 * virtual address and setup the registers.
8013 */
8014 phba->pci_bar2_map = pci_resource_start(pdev, 4);
8015 bar2map_len = pci_resource_len(pdev, 4);
8016 phba->sli4_hba.drbl_regs_memmap_p =
da0436e9 8017 ioremap(phba->pci_bar2_map, bar2map_len);
2fcee4bf
JS
8018 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8019 dev_printk(KERN_ERR, &pdev->dev,
da0436e9 8020 "ioremap failed for SLI4 HBA doorbell registers.\n");
2fcee4bf
JS
8021 goto out_iounmap_ctrl;
8022 }
8023 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8024 if (error)
8025 goto out_iounmap_all;
da0436e9
JS
8026 }
8027
da0436e9
JS
8028 return 0;
8029
8030out_iounmap_all:
8031 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8032out_iounmap_ctrl:
8033 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8034out_iounmap_conf:
8035 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8036out:
8037 return error;
8038}
8039
8040/**
8041 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8042 * @phba: pointer to lpfc hba data structure.
8043 *
8044 * This routine is invoked to unset the PCI device memory space for device
8045 * with SLI-4 interface spec.
8046 **/
8047static void
8048lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8049{
2e90f4b5
JS
8050 uint32_t if_type;
8051 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
da0436e9 8052
2e90f4b5
JS
8053 switch (if_type) {
8054 case LPFC_SLI_INTF_IF_TYPE_0:
8055 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8056 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8057 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8058 break;
8059 case LPFC_SLI_INTF_IF_TYPE_2:
8060 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8061 break;
8062 case LPFC_SLI_INTF_IF_TYPE_1:
8063 default:
8064 dev_printk(KERN_ERR, &phba->pcidev->dev,
8065 "FATAL - unsupported SLI4 interface type - %d\n",
8066 if_type);
8067 break;
8068 }
da0436e9
JS
8069}
8070
8071/**
8072 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8073 * @phba: pointer to lpfc hba data structure.
8074 *
8075 * This routine is invoked to enable the MSI-X interrupt vectors to device
8076 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8077 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8078 * invoked, enables either all or nothing, depending on the current
8079 * availability of PCI vector resources. The device driver is responsible
8080 * for calling the individual request_irq() to register each MSI-X vector
8081 * with a interrupt handler, which is done in this function. Note that
8082 * later when device is unloading, the driver should always call free_irq()
8083 * on all MSI-X vectors it has done request_irq() on before calling
8084 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8085 * will be left with MSI-X enabled and leaks its vectors.
8086 *
8087 * Return codes
af901ca1 8088 * 0 - successful
da0436e9
JS
8089 * other values - error
8090 **/
8091static int
8092lpfc_sli_enable_msix(struct lpfc_hba *phba)
8093{
8094 int rc, i;
8095 LPFC_MBOXQ_t *pmb;
8096
8097 /* Set up MSI-X multi-message vectors */
8098 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8099 phba->msix_entries[i].entry = i;
8100
8101 /* Configure MSI-X capability structure */
8102 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8103 ARRAY_SIZE(phba->msix_entries));
8104 if (rc) {
8105 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8106 "0420 PCI enable MSI-X failed (%d)\n", rc);
8107 goto msi_fail_out;
8108 }
8109 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8110 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8111 "0477 MSI-X entry[%d]: vector=x%x "
8112 "message=%d\n", i,
8113 phba->msix_entries[i].vector,
8114 phba->msix_entries[i].entry);
8115 /*
8116 * Assign MSI-X vectors to interrupt handlers
8117 */
8118
8119 /* vector-0 is associated to slow-path handler */
8120 rc = request_irq(phba->msix_entries[0].vector,
8121 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8122 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8123 if (rc) {
8124 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8125 "0421 MSI-X slow-path request_irq failed "
8126 "(%d)\n", rc);
8127 goto msi_fail_out;
8128 }
8129
8130 /* vector-1 is associated to fast-path handler */
8131 rc = request_irq(phba->msix_entries[1].vector,
8132 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8133 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8134
8135 if (rc) {
8136 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8137 "0429 MSI-X fast-path request_irq failed "
8138 "(%d)\n", rc);
8139 goto irq_fail_out;
8140 }
8141
8142 /*
8143 * Configure HBA MSI-X attention conditions to messages
8144 */
8145 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8146
8147 if (!pmb) {
8148 rc = -ENOMEM;
8149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8150 "0474 Unable to allocate memory for issuing "
8151 "MBOX_CONFIG_MSI command\n");
8152 goto mem_fail_out;
8153 }
8154 rc = lpfc_config_msi(phba, pmb);
8155 if (rc)
8156 goto mbx_fail_out;
8157 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8158 if (rc != MBX_SUCCESS) {
8159 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8160 "0351 Config MSI mailbox command failed, "
8161 "mbxCmd x%x, mbxStatus x%x\n",
8162 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8163 goto mbx_fail_out;
8164 }
8165
8166 /* Free memory allocated for mailbox command */
8167 mempool_free(pmb, phba->mbox_mem_pool);
8168 return rc;
8169
8170mbx_fail_out:
8171 /* Free memory allocated for mailbox command */
8172 mempool_free(pmb, phba->mbox_mem_pool);
8173
8174mem_fail_out:
8175 /* free the irq already requested */
8176 free_irq(phba->msix_entries[1].vector, phba);
8177
8178irq_fail_out:
8179 /* free the irq already requested */
8180 free_irq(phba->msix_entries[0].vector, phba);
8181
8182msi_fail_out:
8183 /* Unconfigure MSI-X capability structure */
8184 pci_disable_msix(phba->pcidev);
8185 return rc;
8186}
8187
8188/**
8189 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8190 * @phba: pointer to lpfc hba data structure.
8191 *
8192 * This routine is invoked to release the MSI-X vectors and then disable the
8193 * MSI-X interrupt mode to device with SLI-3 interface spec.
8194 **/
8195static void
8196lpfc_sli_disable_msix(struct lpfc_hba *phba)
8197{
8198 int i;
8199
8200 /* Free up MSI-X multi-message vectors */
8201 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8202 free_irq(phba->msix_entries[i].vector, phba);
8203 /* Disable MSI-X */
8204 pci_disable_msix(phba->pcidev);
8205
8206 return;
8207}
8208
8209/**
8210 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8211 * @phba: pointer to lpfc hba data structure.
8212 *
8213 * This routine is invoked to enable the MSI interrupt mode to device with
8214 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8215 * enable the MSI vector. The device driver is responsible for calling the
8216 * request_irq() to register MSI vector with a interrupt the handler, which
8217 * is done in this function.
8218 *
8219 * Return codes
af901ca1 8220 * 0 - successful
da0436e9
JS
8221 * other values - error
8222 */
8223static int
8224lpfc_sli_enable_msi(struct lpfc_hba *phba)
8225{
8226 int rc;
8227
8228 rc = pci_enable_msi(phba->pcidev);
8229 if (!rc)
8230 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8231 "0462 PCI enable MSI mode success.\n");
8232 else {
8233 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8234 "0471 PCI enable MSI mode failed (%d)\n", rc);
8235 return rc;
8236 }
8237
8238 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8239 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8240 if (rc) {
8241 pci_disable_msi(phba->pcidev);
8242 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8243 "0478 MSI request_irq failed (%d)\n", rc);
8244 }
8245 return rc;
8246}
8247
8248/**
8249 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8250 * @phba: pointer to lpfc hba data structure.
8251 *
8252 * This routine is invoked to disable the MSI interrupt mode to device with
8253 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8254 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8255 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8256 * its vector.
8257 */
8258static void
8259lpfc_sli_disable_msi(struct lpfc_hba *phba)
8260{
8261 free_irq(phba->pcidev->irq, phba);
8262 pci_disable_msi(phba->pcidev);
8263 return;
8264}
8265
8266/**
8267 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8268 * @phba: pointer to lpfc hba data structure.
8269 *
8270 * This routine is invoked to enable device interrupt and associate driver's
8271 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8272 * spec. Depends on the interrupt mode configured to the driver, the driver
8273 * will try to fallback from the configured interrupt mode to an interrupt
8274 * mode which is supported by the platform, kernel, and device in the order
8275 * of:
8276 * MSI-X -> MSI -> IRQ.
8277 *
8278 * Return codes
af901ca1 8279 * 0 - successful
da0436e9
JS
8280 * other values - error
8281 **/
8282static uint32_t
8283lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8284{
8285 uint32_t intr_mode = LPFC_INTR_ERROR;
8286 int retval;
8287
8288 if (cfg_mode == 2) {
8289 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8290 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8291 if (!retval) {
8292 /* Now, try to enable MSI-X interrupt mode */
8293 retval = lpfc_sli_enable_msix(phba);
8294 if (!retval) {
8295 /* Indicate initialization to MSI-X mode */
8296 phba->intr_type = MSIX;
8297 intr_mode = 2;
8298 }
8299 }
8300 }
8301
8302 /* Fallback to MSI if MSI-X initialization failed */
8303 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8304 retval = lpfc_sli_enable_msi(phba);
8305 if (!retval) {
8306 /* Indicate initialization to MSI mode */
8307 phba->intr_type = MSI;
8308 intr_mode = 1;
8309 }
8310 }
8311
8312 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8313 if (phba->intr_type == NONE) {
8314 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8315 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8316 if (!retval) {
8317 /* Indicate initialization to INTx mode */
8318 phba->intr_type = INTx;
8319 intr_mode = 0;
8320 }
8321 }
8322 return intr_mode;
8323}
8324
8325/**
8326 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8327 * @phba: pointer to lpfc hba data structure.
8328 *
8329 * This routine is invoked to disable device interrupt and disassociate the
8330 * driver's interrupt handler(s) from interrupt vector(s) to device with
8331 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8332 * release the interrupt vector(s) for the message signaled interrupt.
8333 **/
8334static void
8335lpfc_sli_disable_intr(struct lpfc_hba *phba)
8336{
8337 /* Disable the currently initialized interrupt mode */
8338 if (phba->intr_type == MSIX)
8339 lpfc_sli_disable_msix(phba);
8340 else if (phba->intr_type == MSI)
8341 lpfc_sli_disable_msi(phba);
8342 else if (phba->intr_type == INTx)
8343 free_irq(phba->pcidev->irq, phba);
8344
8345 /* Reset interrupt management states */
8346 phba->intr_type = NONE;
8347 phba->sli.slistat.sli_intr = 0;
8348
8349 return;
8350}
8351
7bb03bbf
JS
8352/**
8353 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8354 * @phba: pointer to lpfc hba data structure.
8355 *
8356 * Find next available CPU to use for IRQ to CPU affinity.
8357 */
8358static int
8359lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8360{
8361 struct lpfc_vector_map_info *cpup;
8362 int cpu;
8363
8364 cpup = phba->sli4_hba.cpu_map;
8365 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8366 /* CPU must be online */
8367 if (cpu_online(cpu)) {
8368 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8369 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8370 (cpup->phys_id == phys_id)) {
8371 return cpu;
8372 }
8373 }
8374 cpup++;
8375 }
8376
8377 /*
8378 * If we get here, we have used ALL CPUs for the specific
8379 * phys_id. Now we need to clear out lpfc_used_cpu and start
8380 * reusing CPUs.
8381 */
8382
8383 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8384 if (lpfc_used_cpu[cpu] == phys_id)
8385 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8386 }
8387
8388 cpup = phba->sli4_hba.cpu_map;
8389 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8390 /* CPU must be online */
8391 if (cpu_online(cpu)) {
8392 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8393 (cpup->phys_id == phys_id)) {
8394 return cpu;
8395 }
8396 }
8397 cpup++;
8398 }
8399 return LPFC_VECTOR_MAP_EMPTY;
8400}
8401
8402/**
8403 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8404 * @phba: pointer to lpfc hba data structure.
8405 * @vectors: number of HBA vectors
8406 *
8407 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8408 * affinization across multple physical CPUs (numa nodes).
8409 * In addition, this routine will assign an IO channel for each CPU
8410 * to use when issuing I/Os.
8411 */
8412static int
8413lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8414{
8415 int i, idx, saved_chann, used_chann, cpu, phys_id;
8416 int max_phys_id, num_io_channel, first_cpu;
8417 struct lpfc_vector_map_info *cpup;
8418#ifdef CONFIG_X86
8419 struct cpuinfo_x86 *cpuinfo;
8420#endif
8421 struct cpumask *mask;
8422 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8423
8424 /* If there is no mapping, just return */
8425 if (!phba->cfg_fcp_cpu_map)
8426 return 1;
8427
8428 /* Init cpu_map array */
8429 memset(phba->sli4_hba.cpu_map, 0xff,
8430 (sizeof(struct lpfc_vector_map_info) *
8431 phba->sli4_hba.num_present_cpu));
8432
8433 max_phys_id = 0;
8434 phys_id = 0;
8435 num_io_channel = 0;
8436 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8437
8438 /* Update CPU map with physical id and core id of each CPU */
8439 cpup = phba->sli4_hba.cpu_map;
8440 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8441#ifdef CONFIG_X86
8442 cpuinfo = &cpu_data(cpu);
8443 cpup->phys_id = cpuinfo->phys_proc_id;
8444 cpup->core_id = cpuinfo->cpu_core_id;
8445#else
8446 /* No distinction between CPUs for other platforms */
8447 cpup->phys_id = 0;
8448 cpup->core_id = 0;
8449#endif
8450
8451 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8452 "3328 CPU physid %d coreid %d\n",
8453 cpup->phys_id, cpup->core_id);
8454
8455 if (cpup->phys_id > max_phys_id)
8456 max_phys_id = cpup->phys_id;
8457 cpup++;
8458 }
8459
8460 /* Now associate the HBA vectors with specific CPUs */
8461 for (idx = 0; idx < vectors; idx++) {
8462 cpup = phba->sli4_hba.cpu_map;
8463 cpu = lpfc_find_next_cpu(phba, phys_id);
8464 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8465
8466 /* Try for all phys_id's */
8467 for (i = 1; i < max_phys_id; i++) {
8468 phys_id++;
8469 if (phys_id > max_phys_id)
8470 phys_id = 0;
8471 cpu = lpfc_find_next_cpu(phba, phys_id);
8472 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8473 continue;
8474 goto found;
8475 }
8476
8477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8478 "3329 Cannot set affinity:"
8479 "Error mapping vector %d (%d)\n",
8480 idx, vectors);
8481 return 0;
8482 }
8483found:
8484 cpup += cpu;
8485 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8486 lpfc_used_cpu[cpu] = phys_id;
8487
8488 /* Associate vector with selected CPU */
8489 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8490
8491 /* Associate IO channel with selected CPU */
8492 cpup->channel_id = idx;
8493 num_io_channel++;
8494
8495 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8496 first_cpu = cpu;
8497
8498 /* Now affinitize to the selected CPU */
8499 mask = &cpup->maskbits;
8500 cpumask_clear(mask);
8501 cpumask_set_cpu(cpu, mask);
8502 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8503 vector, mask);
8504
8505 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8506 "3330 Set Affinity: CPU %d channel %d "
8507 "irq %d (%x)\n",
8508 cpu, cpup->channel_id,
8509 phba->sli4_hba.msix_entries[idx].vector, i);
8510
8511 /* Spread vector mapping across multple physical CPU nodes */
8512 phys_id++;
8513 if (phys_id > max_phys_id)
8514 phys_id = 0;
8515 }
8516
8517 /*
8518 * Finally fill in the IO channel for any remaining CPUs.
8519 * At this point, all IO channels have been assigned to a specific
8520 * MSIx vector, mapped to a specific CPU.
8521 * Base the remaining IO channel assigned, to IO channels already
8522 * assigned to other CPUs on the same phys_id.
8523 */
8524 for (i = 0; i <= max_phys_id; i++) {
8525 /*
8526 * If there are no io channels already mapped to
8527 * this phys_id, just round robin thru the io_channels.
8528 * Setup chann[] for round robin.
8529 */
8530 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8531 chann[idx] = idx;
8532
8533 saved_chann = 0;
8534 used_chann = 0;
8535
8536 /*
8537 * First build a list of IO channels already assigned
8538 * to this phys_id before reassigning the same IO
8539 * channels to the remaining CPUs.
8540 */
8541 cpup = phba->sli4_hba.cpu_map;
8542 cpu = first_cpu;
8543 cpup += cpu;
8544 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8545 idx++) {
8546 if (cpup->phys_id == i) {
8547 /*
8548 * Save any IO channels that are
8549 * already mapped to this phys_id.
8550 */
8551 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8552 chann[saved_chann] =
8553 cpup->channel_id;
8554 saved_chann++;
8555 goto out;
8556 }
8557
8558 /* See if we are using round-robin */
8559 if (saved_chann == 0)
8560 saved_chann =
8561 phba->cfg_fcp_io_channel;
8562
8563 /* Associate next IO channel with CPU */
8564 cpup->channel_id = chann[used_chann];
8565 num_io_channel++;
8566 used_chann++;
8567 if (used_chann == saved_chann)
8568 used_chann = 0;
8569
8570 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8571 "3331 Set IO_CHANN "
8572 "CPU %d channel %d\n",
8573 idx, cpup->channel_id);
8574 }
8575out:
8576 cpu++;
8577 if (cpu >= phba->sli4_hba.num_present_cpu) {
8578 cpup = phba->sli4_hba.cpu_map;
8579 cpu = 0;
8580 } else {
8581 cpup++;
8582 }
8583 }
8584 }
8585
8586 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8587 cpup = phba->sli4_hba.cpu_map;
8588 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8589 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8590 cpup->channel_id = 0;
8591 num_io_channel++;
8592
8593 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8594 "3332 Assign IO_CHANN "
8595 "CPU %d channel %d\n",
8596 idx, cpup->channel_id);
8597 }
8598 cpup++;
8599 }
8600 }
8601
8602 /* Sanity check */
8603 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8605 "3333 Set affinity mismatch:"
8606 "%d chann != %d cpus: %d vactors\n",
8607 num_io_channel, phba->sli4_hba.num_present_cpu,
8608 vectors);
8609
8610 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8611 return 1;
8612}
8613
8614
da0436e9
JS
8615/**
8616 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8617 * @phba: pointer to lpfc hba data structure.
8618 *
8619 * This routine is invoked to enable the MSI-X interrupt vectors to device
8620 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8621 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8622 * enables either all or nothing, depending on the current availability of
8623 * PCI vector resources. The device driver is responsible for calling the
8624 * individual request_irq() to register each MSI-X vector with a interrupt
8625 * handler, which is done in this function. Note that later when device is
8626 * unloading, the driver should always call free_irq() on all MSI-X vectors
8627 * it has done request_irq() on before calling pci_disable_msix(). Failure
8628 * to do so results in a BUG_ON() and a device will be left with MSI-X
8629 * enabled and leaks its vectors.
8630 *
8631 * Return codes
af901ca1 8632 * 0 - successful
da0436e9
JS
8633 * other values - error
8634 **/
8635static int
8636lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8637{
75baf696 8638 int vectors, rc, index;
da0436e9
JS
8639
8640 /* Set up MSI-X multi-message vectors */
82c3e9ba 8641 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
da0436e9
JS
8642 phba->sli4_hba.msix_entries[index].entry = index;
8643
8644 /* Configure MSI-X capability structure */
82c3e9ba 8645 vectors = phba->cfg_fcp_io_channel;
75baf696 8646enable_msix_vectors:
da0436e9 8647 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
75baf696
JS
8648 vectors);
8649 if (rc > 1) {
8650 vectors = rc;
8651 goto enable_msix_vectors;
8652 } else if (rc) {
da0436e9
JS
8653 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8654 "0484 PCI enable MSI-X failed (%d)\n", rc);
8655 goto msi_fail_out;
8656 }
75baf696 8657
da0436e9 8658 /* Log MSI-X vector assignment */
75baf696 8659 for (index = 0; index < vectors; index++)
da0436e9
JS
8660 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8661 "0489 MSI-X entry[%d]: vector=x%x "
8662 "message=%d\n", index,
8663 phba->sli4_hba.msix_entries[index].vector,
8664 phba->sli4_hba.msix_entries[index].entry);
67d12733 8665
7bb03bbf 8666 /* Assign MSI-X vectors to interrupt handlers */
67d12733 8667 for (index = 0; index < vectors; index++) {
4305f183
JS
8668 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8669 sprintf((char *)&phba->sli4_hba.handler_name[index],
8670 LPFC_DRIVER_HANDLER_NAME"%d", index);
da0436e9 8671
67d12733
JS
8672 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8673 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
ba20c853 8674 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
da0436e9 8675 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
67d12733 8676 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
4305f183 8677 (char *)&phba->sli4_hba.handler_name[index],
67d12733 8678 &phba->sli4_hba.fcp_eq_hdl[index]);
da0436e9
JS
8679 if (rc) {
8680 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8681 "0486 MSI-X fast-path (%d) "
8682 "request_irq failed (%d)\n", index, rc);
8683 goto cfg_fail_out;
8684 }
8685 }
8686
82c3e9ba
JS
8687 if (vectors != phba->cfg_fcp_io_channel) {
8688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8689 "3238 Reducing IO channels to match number of "
8690 "MSI-X vectors, requested %d got %d\n",
8691 phba->cfg_fcp_io_channel, vectors);
8692 phba->cfg_fcp_io_channel = vectors;
8693 }
7bb03bbf
JS
8694
8695 lpfc_sli4_set_affinity(phba, vectors);
da0436e9
JS
8696 return rc;
8697
8698cfg_fail_out:
8699 /* free the irq already requested */
67d12733
JS
8700 for (--index; index >= 0; index--)
8701 free_irq(phba->sli4_hba.msix_entries[index].vector,
8702 &phba->sli4_hba.fcp_eq_hdl[index]);
da0436e9
JS
8703
8704msi_fail_out:
8705 /* Unconfigure MSI-X capability structure */
8706 pci_disable_msix(phba->pcidev);
8707 return rc;
8708}
8709
8710/**
8711 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8712 * @phba: pointer to lpfc hba data structure.
8713 *
8714 * This routine is invoked to release the MSI-X vectors and then disable the
8715 * MSI-X interrupt mode to device with SLI-4 interface spec.
8716 **/
8717static void
8718lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8719{
8720 int index;
8721
8722 /* Free up MSI-X multi-message vectors */
82c3e9ba 8723 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
da0436e9 8724 free_irq(phba->sli4_hba.msix_entries[index].vector,
67d12733 8725 &phba->sli4_hba.fcp_eq_hdl[index]);
75baf696 8726
da0436e9
JS
8727 /* Disable MSI-X */
8728 pci_disable_msix(phba->pcidev);
8729
8730 return;
8731}
8732
8733/**
8734 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8735 * @phba: pointer to lpfc hba data structure.
8736 *
8737 * This routine is invoked to enable the MSI interrupt mode to device with
8738 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8739 * to enable the MSI vector. The device driver is responsible for calling
8740 * the request_irq() to register MSI vector with a interrupt the handler,
8741 * which is done in this function.
8742 *
8743 * Return codes
af901ca1 8744 * 0 - successful
da0436e9
JS
8745 * other values - error
8746 **/
8747static int
8748lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8749{
8750 int rc, index;
8751
8752 rc = pci_enable_msi(phba->pcidev);
8753 if (!rc)
8754 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8755 "0487 PCI enable MSI mode success.\n");
8756 else {
8757 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8758 "0488 PCI enable MSI mode failed (%d)\n", rc);
8759 return rc;
8760 }
8761
8762 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8763 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8764 if (rc) {
8765 pci_disable_msi(phba->pcidev);
8766 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8767 "0490 MSI request_irq failed (%d)\n", rc);
75baf696 8768 return rc;
da0436e9
JS
8769 }
8770
67d12733 8771 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
da0436e9
JS
8772 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8773 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8774 }
8775
75baf696 8776 return 0;
da0436e9
JS
8777}
8778
8779/**
8780 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8781 * @phba: pointer to lpfc hba data structure.
8782 *
8783 * This routine is invoked to disable the MSI interrupt mode to device with
8784 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8785 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8786 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8787 * its vector.
8788 **/
8789static void
8790lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8791{
8792 free_irq(phba->pcidev->irq, phba);
8793 pci_disable_msi(phba->pcidev);
8794 return;
8795}
8796
8797/**
8798 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8799 * @phba: pointer to lpfc hba data structure.
8800 *
8801 * This routine is invoked to enable device interrupt and associate driver's
8802 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8803 * interface spec. Depends on the interrupt mode configured to the driver,
8804 * the driver will try to fallback from the configured interrupt mode to an
8805 * interrupt mode which is supported by the platform, kernel, and device in
8806 * the order of:
8807 * MSI-X -> MSI -> IRQ.
8808 *
8809 * Return codes
af901ca1 8810 * 0 - successful
da0436e9
JS
8811 * other values - error
8812 **/
8813static uint32_t
8814lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8815{
8816 uint32_t intr_mode = LPFC_INTR_ERROR;
8817 int retval, index;
8818
8819 if (cfg_mode == 2) {
8820 /* Preparation before conf_msi mbox cmd */
8821 retval = 0;
8822 if (!retval) {
8823 /* Now, try to enable MSI-X interrupt mode */
8824 retval = lpfc_sli4_enable_msix(phba);
8825 if (!retval) {
8826 /* Indicate initialization to MSI-X mode */
8827 phba->intr_type = MSIX;
8828 intr_mode = 2;
8829 }
8830 }
8831 }
8832
8833 /* Fallback to MSI if MSI-X initialization failed */
8834 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8835 retval = lpfc_sli4_enable_msi(phba);
8836 if (!retval) {
8837 /* Indicate initialization to MSI mode */
8838 phba->intr_type = MSI;
8839 intr_mode = 1;
8840 }
8841 }
8842
8843 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8844 if (phba->intr_type == NONE) {
8845 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8846 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8847 if (!retval) {
8848 /* Indicate initialization to INTx mode */
8849 phba->intr_type = INTx;
8850 intr_mode = 0;
67d12733 8851 for (index = 0; index < phba->cfg_fcp_io_channel;
da0436e9
JS
8852 index++) {
8853 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8854 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
ba20c853
JS
8855 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8856 fcp_eq_in_use, 1);
da0436e9
JS
8857 }
8858 }
8859 }
8860 return intr_mode;
8861}
8862
8863/**
8864 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8865 * @phba: pointer to lpfc hba data structure.
8866 *
8867 * This routine is invoked to disable device interrupt and disassociate
8868 * the driver's interrupt handler(s) from interrupt vector(s) to device
8869 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8870 * will release the interrupt vector(s) for the message signaled interrupt.
8871 **/
8872static void
8873lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8874{
8875 /* Disable the currently initialized interrupt mode */
8876 if (phba->intr_type == MSIX)
8877 lpfc_sli4_disable_msix(phba);
8878 else if (phba->intr_type == MSI)
8879 lpfc_sli4_disable_msi(phba);
8880 else if (phba->intr_type == INTx)
8881 free_irq(phba->pcidev->irq, phba);
8882
8883 /* Reset interrupt management states */
8884 phba->intr_type = NONE;
8885 phba->sli.slistat.sli_intr = 0;
8886
8887 return;
8888}
8889
8890/**
8891 * lpfc_unset_hba - Unset SLI3 hba device initialization
8892 * @phba: pointer to lpfc hba data structure.
8893 *
8894 * This routine is invoked to unset the HBA device initialization steps to
8895 * a device with SLI-3 interface spec.
8896 **/
8897static void
8898lpfc_unset_hba(struct lpfc_hba *phba)
8899{
8900 struct lpfc_vport *vport = phba->pport;
8901 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8902
8903 spin_lock_irq(shost->host_lock);
8904 vport->load_flag |= FC_UNLOADING;
8905 spin_unlock_irq(shost->host_lock);
8906
72859909
JS
8907 kfree(phba->vpi_bmask);
8908 kfree(phba->vpi_ids);
8909
da0436e9
JS
8910 lpfc_stop_hba_timers(phba);
8911
8912 phba->pport->work_port_events = 0;
8913
8914 lpfc_sli_hba_down(phba);
8915
8916 lpfc_sli_brdrestart(phba);
8917
8918 lpfc_sli_disable_intr(phba);
8919
8920 return;
8921}
8922
5af5eee7
JS
8923/**
8924 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8925 * @phba: Pointer to HBA context object.
8926 *
8927 * This function is called in the SLI4 code path to wait for completion
8928 * of device's XRIs exchange busy. It will check the XRI exchange busy
8929 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8930 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8931 * I/Os every 30 seconds, log error message, and wait forever. Only when
8932 * all XRI exchange busy complete, the driver unload shall proceed with
8933 * invoking the function reset ioctl mailbox command to the CNA and the
8934 * the rest of the driver unload resource release.
8935 **/
8936static void
8937lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8938{
8939 int wait_time = 0;
8940 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8941 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8942
8943 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8944 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8945 if (!fcp_xri_cmpl)
8946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8947 "2877 FCP XRI exchange busy "
8948 "wait time: %d seconds.\n",
8949 wait_time/1000);
8950 if (!els_xri_cmpl)
8951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8952 "2878 ELS XRI exchange busy "
8953 "wait time: %d seconds.\n",
8954 wait_time/1000);
8955 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8956 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8957 } else {
8958 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8959 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8960 }
8961 fcp_xri_cmpl =
8962 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8963 els_xri_cmpl =
8964 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8965 }
8966}
8967
da0436e9
JS
8968/**
8969 * lpfc_sli4_hba_unset - Unset the fcoe hba
8970 * @phba: Pointer to HBA context object.
8971 *
8972 * This function is called in the SLI4 code path to reset the HBA's FCoE
8973 * function. The caller is not required to hold any lock. This routine
8974 * issues PCI function reset mailbox command to reset the FCoE function.
8975 * At the end of the function, it calls lpfc_hba_down_post function to
8976 * free any pending commands.
8977 **/
8978static void
8979lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8980{
8981 int wait_cnt = 0;
8982 LPFC_MBOXQ_t *mboxq;
912e3acd 8983 struct pci_dev *pdev = phba->pcidev;
da0436e9
JS
8984
8985 lpfc_stop_hba_timers(phba);
8986 phba->sli4_hba.intr_enable = 0;
8987
8988 /*
8989 * Gracefully wait out the potential current outstanding asynchronous
8990 * mailbox command.
8991 */
8992
8993 /* First, block any pending async mailbox command from posted */
8994 spin_lock_irq(&phba->hbalock);
8995 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8996 spin_unlock_irq(&phba->hbalock);
8997 /* Now, trying to wait it out if we can */
8998 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8999 msleep(10);
9000 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9001 break;
9002 }
9003 /* Forcefully release the outstanding mailbox command if timed out */
9004 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9005 spin_lock_irq(&phba->hbalock);
9006 mboxq = phba->sli.mbox_active;
9007 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9008 __lpfc_mbox_cmpl_put(phba, mboxq);
9009 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9010 phba->sli.mbox_active = NULL;
9011 spin_unlock_irq(&phba->hbalock);
9012 }
9013
5af5eee7
JS
9014 /* Abort all iocbs associated with the hba */
9015 lpfc_sli_hba_iocb_abort(phba);
9016
9017 /* Wait for completion of device XRI exchange busy */
9018 lpfc_sli4_xri_exchange_busy_wait(phba);
9019
da0436e9
JS
9020 /* Disable PCI subsystem interrupt */
9021 lpfc_sli4_disable_intr(phba);
9022
912e3acd
JS
9023 /* Disable SR-IOV if enabled */
9024 if (phba->cfg_sriov_nr_virtfn)
9025 pci_disable_sriov(pdev);
9026
da0436e9
JS
9027 /* Stop kthread signal shall trigger work_done one more time */
9028 kthread_stop(phba->worker_thread);
9029
3677a3a7
JS
9030 /* Reset SLI4 HBA FCoE function */
9031 lpfc_pci_function_reset(phba);
5350d872 9032 lpfc_sli4_queue_destroy(phba);
3677a3a7 9033
da0436e9
JS
9034 /* Stop the SLI4 device port */
9035 phba->pport->work_port_events = 0;
9036}
9037
28baac74
JS
9038 /**
9039 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9040 * @phba: Pointer to HBA context object.
9041 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9042 *
9043 * This function is called in the SLI4 code path to read the port's
9044 * sli4 capabilities.
9045 *
9046 * This function may be be called from any context that can block-wait
9047 * for the completion. The expectation is that this routine is called
9048 * typically from probe_one or from the online routine.
9049 **/
9050int
9051lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9052{
9053 int rc;
9054 struct lpfc_mqe *mqe;
9055 struct lpfc_pc_sli4_params *sli4_params;
9056 uint32_t mbox_tmo;
9057
9058 rc = 0;
9059 mqe = &mboxq->u.mqe;
9060
9061 /* Read the port's SLI4 Parameters port capabilities */
fedd3b7b 9062 lpfc_pc_sli4_params(mboxq);
28baac74
JS
9063 if (!phba->sli4_hba.intr_enable)
9064 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9065 else {
a183a15f 9066 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
28baac74
JS
9067 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9068 }
9069
9070 if (unlikely(rc))
9071 return 1;
9072
9073 sli4_params = &phba->sli4_hba.pc_sli4_params;
9074 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9075 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9076 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9077 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9078 &mqe->un.sli4_params);
9079 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9080 &mqe->un.sli4_params);
9081 sli4_params->proto_types = mqe->un.sli4_params.word3;
9082 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9083 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9084 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9085 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9086 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9087 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9088 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9089 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9090 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9091 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9092 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9093 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9094 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9095 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9096 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9097 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9098 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9099 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9100 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9101 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
0558056c
JS
9102
9103 /* Make sure that sge_supp_len can be handled by the driver */
9104 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9105 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9106
28baac74
JS
9107 return rc;
9108}
9109
fedd3b7b
JS
9110/**
9111 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9112 * @phba: Pointer to HBA context object.
9113 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9114 *
9115 * This function is called in the SLI4 code path to read the port's
9116 * sli4 capabilities.
9117 *
9118 * This function may be be called from any context that can block-wait
9119 * for the completion. The expectation is that this routine is called
9120 * typically from probe_one or from the online routine.
9121 **/
9122int
9123lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9124{
9125 int rc;
9126 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9127 struct lpfc_pc_sli4_params *sli4_params;
a183a15f 9128 uint32_t mbox_tmo;
fedd3b7b
JS
9129 int length;
9130 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9131
6d368e53
JS
9132 /*
9133 * By default, the driver assumes the SLI4 port requires RPI
9134 * header postings. The SLI4_PARAM response will correct this
9135 * assumption.
9136 */
9137 phba->sli4_hba.rpi_hdrs_in_use = 1;
9138
fedd3b7b
JS
9139 /* Read the port's SLI4 Config Parameters */
9140 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9141 sizeof(struct lpfc_sli4_cfg_mhdr));
9142 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9143 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9144 length, LPFC_SLI4_MBX_EMBED);
9145 if (!phba->sli4_hba.intr_enable)
9146 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
a183a15f
JS
9147 else {
9148 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9149 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9150 }
fedd3b7b
JS
9151 if (unlikely(rc))
9152 return rc;
9153 sli4_params = &phba->sli4_hba.pc_sli4_params;
9154 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9155 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9156 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9157 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9158 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9159 mbx_sli4_parameters);
9160 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9161 mbx_sli4_parameters);
9162 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9163 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9164 else
9165 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9166 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9167 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
9168 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9169 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9170 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9171 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
9172 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9173 mbx_sli4_parameters);
9174 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9175 mbx_sli4_parameters);
6d368e53
JS
9176 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9177 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
0558056c
JS
9178
9179 /* Make sure that sge_supp_len can be handled by the driver */
9180 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9181 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9182
fedd3b7b
JS
9183 return 0;
9184}
9185
da0436e9
JS
9186/**
9187 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9188 * @pdev: pointer to PCI device
9189 * @pid: pointer to PCI device identifier
9190 *
9191 * This routine is to be called to attach a device with SLI-3 interface spec
9192 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9193 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9194 * information of the device and driver to see if the driver state that it can
9195 * support this kind of device. If the match is successful, the driver core
9196 * invokes this routine. If this routine determines it can claim the HBA, it
9197 * does all the initialization that it needs to do to handle the HBA properly.
9198 *
9199 * Return code
9200 * 0 - driver can claim the device
9201 * negative value - driver can not claim the device
9202 **/
6f039790 9203static int
da0436e9
JS
9204lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9205{
9206 struct lpfc_hba *phba;
9207 struct lpfc_vport *vport = NULL;
6669f9bb 9208 struct Scsi_Host *shost = NULL;
da0436e9
JS
9209 int error;
9210 uint32_t cfg_mode, intr_mode;
9211
9212 /* Allocate memory for HBA structure */
9213 phba = lpfc_hba_alloc(pdev);
9214 if (!phba)
9215 return -ENOMEM;
9216
9217 /* Perform generic PCI device enabling operation */
9218 error = lpfc_enable_pci_dev(phba);
079b5c91 9219 if (error)
da0436e9 9220 goto out_free_phba;
da0436e9
JS
9221
9222 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9223 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9224 if (error)
9225 goto out_disable_pci_dev;
9226
9227 /* Set up SLI-3 specific device PCI memory space */
9228 error = lpfc_sli_pci_mem_setup(phba);
9229 if (error) {
9230 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9231 "1402 Failed to set up pci memory space.\n");
9232 goto out_disable_pci_dev;
9233 }
9234
9235 /* Set up phase-1 common device driver resources */
9236 error = lpfc_setup_driver_resource_phase1(phba);
9237 if (error) {
9238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9239 "1403 Failed to set up driver resource.\n");
9240 goto out_unset_pci_mem_s3;
9241 }
9242
9243 /* Set up SLI-3 specific device driver resources */
9244 error = lpfc_sli_driver_resource_setup(phba);
9245 if (error) {
9246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9247 "1404 Failed to set up driver resource.\n");
9248 goto out_unset_pci_mem_s3;
9249 }
9250
9251 /* Initialize and populate the iocb list per host */
9252 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9253 if (error) {
9254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9255 "1405 Failed to initialize iocb list.\n");
9256 goto out_unset_driver_resource_s3;
9257 }
9258
9259 /* Set up common device driver resources */
9260 error = lpfc_setup_driver_resource_phase2(phba);
9261 if (error) {
9262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9263 "1406 Failed to set up driver resource.\n");
9264 goto out_free_iocb_list;
9265 }
9266
079b5c91
JS
9267 /* Get the default values for Model Name and Description */
9268 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9269
da0436e9
JS
9270 /* Create SCSI host to the physical port */
9271 error = lpfc_create_shost(phba);
9272 if (error) {
9273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9274 "1407 Failed to create scsi host.\n");
9275 goto out_unset_driver_resource;
9276 }
9277
9278 /* Configure sysfs attributes */
9279 vport = phba->pport;
9280 error = lpfc_alloc_sysfs_attr(vport);
9281 if (error) {
9282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9283 "1476 Failed to allocate sysfs attr\n");
9284 goto out_destroy_shost;
9285 }
9286
6669f9bb 9287 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
da0436e9
JS
9288 /* Now, trying to enable interrupt and bring up the device */
9289 cfg_mode = phba->cfg_use_msi;
9290 while (true) {
9291 /* Put device to a known state before enabling interrupt */
9292 lpfc_stop_port(phba);
9293 /* Configure and enable interrupt */
9294 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9295 if (intr_mode == LPFC_INTR_ERROR) {
9296 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9297 "0431 Failed to enable interrupt.\n");
9298 error = -ENODEV;
9299 goto out_free_sysfs_attr;
9300 }
9301 /* SLI-3 HBA setup */
9302 if (lpfc_sli_hba_setup(phba)) {
9303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9304 "1477 Failed to set up hba\n");
9305 error = -ENODEV;
9306 goto out_remove_device;
9307 }
9308
9309 /* Wait 50ms for the interrupts of previous mailbox commands */
9310 msleep(50);
9311 /* Check active interrupts on message signaled interrupts */
9312 if (intr_mode == 0 ||
9313 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9314 /* Log the current active interrupt mode */
9315 phba->intr_mode = intr_mode;
9316 lpfc_log_intr_mode(phba, intr_mode);
9317 break;
9318 } else {
9319 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9320 "0447 Configure interrupt mode (%d) "
9321 "failed active interrupt test.\n",
9322 intr_mode);
9323 /* Disable the current interrupt mode */
9324 lpfc_sli_disable_intr(phba);
9325 /* Try next level of interrupt mode */
9326 cfg_mode = --intr_mode;
9327 }
9328 }
9329
9330 /* Perform post initialization setup */
9331 lpfc_post_init_setup(phba);
9332
9333 /* Check if there are static vports to be created. */
9334 lpfc_create_static_vport(phba);
9335
9336 return 0;
9337
9338out_remove_device:
9339 lpfc_unset_hba(phba);
9340out_free_sysfs_attr:
9341 lpfc_free_sysfs_attr(vport);
9342out_destroy_shost:
9343 lpfc_destroy_shost(phba);
9344out_unset_driver_resource:
9345 lpfc_unset_driver_resource_phase2(phba);
9346out_free_iocb_list:
9347 lpfc_free_iocb_list(phba);
9348out_unset_driver_resource_s3:
9349 lpfc_sli_driver_resource_unset(phba);
9350out_unset_pci_mem_s3:
9351 lpfc_sli_pci_mem_unset(phba);
9352out_disable_pci_dev:
9353 lpfc_disable_pci_dev(phba);
6669f9bb
JS
9354 if (shost)
9355 scsi_host_put(shost);
da0436e9
JS
9356out_free_phba:
9357 lpfc_hba_free(phba);
9358 return error;
9359}
9360
9361/**
9362 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
9363 * @pdev: pointer to PCI device
9364 *
9365 * This routine is to be called to disattach a device with SLI-3 interface
9366 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9367 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9368 * device to be removed from the PCI subsystem properly.
9369 **/
6f039790 9370static void
da0436e9
JS
9371lpfc_pci_remove_one_s3(struct pci_dev *pdev)
9372{
9373 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9374 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9375 struct lpfc_vport **vports;
9376 struct lpfc_hba *phba = vport->phba;
9377 int i;
9378 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9379
9380 spin_lock_irq(&phba->hbalock);
9381 vport->load_flag |= FC_UNLOADING;
9382 spin_unlock_irq(&phba->hbalock);
9383
9384 lpfc_free_sysfs_attr(vport);
9385
9386 /* Release all the vports against this physical port */
9387 vports = lpfc_create_vport_work_array(phba);
9388 if (vports != NULL)
587a37f6
JS
9389 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9390 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9391 continue;
da0436e9 9392 fc_vport_terminate(vports[i]->fc_vport);
587a37f6 9393 }
da0436e9
JS
9394 lpfc_destroy_vport_work_array(phba, vports);
9395
9396 /* Remove FC host and then SCSI host with the physical port */
9397 fc_remove_host(shost);
9398 scsi_remove_host(shost);
9399 lpfc_cleanup(vport);
9400
9401 /*
9402 * Bring down the SLI Layer. This step disable all interrupts,
9403 * clears the rings, discards all mailbox commands, and resets
9404 * the HBA.
9405 */
9406
48e34d0f 9407 /* HBA interrupt will be disabled after this call */
da0436e9
JS
9408 lpfc_sli_hba_down(phba);
9409 /* Stop kthread signal shall trigger work_done one more time */
9410 kthread_stop(phba->worker_thread);
9411 /* Final cleanup of txcmplq and reset the HBA */
9412 lpfc_sli_brdrestart(phba);
9413
72859909
JS
9414 kfree(phba->vpi_bmask);
9415 kfree(phba->vpi_ids);
9416
da0436e9
JS
9417 lpfc_stop_hba_timers(phba);
9418 spin_lock_irq(&phba->hbalock);
9419 list_del_init(&vport->listentry);
9420 spin_unlock_irq(&phba->hbalock);
9421
9422 lpfc_debugfs_terminate(vport);
9423
912e3acd
JS
9424 /* Disable SR-IOV if enabled */
9425 if (phba->cfg_sriov_nr_virtfn)
9426 pci_disable_sriov(pdev);
9427
da0436e9
JS
9428 /* Disable interrupt */
9429 lpfc_sli_disable_intr(phba);
9430
9431 pci_set_drvdata(pdev, NULL);
9432 scsi_host_put(shost);
9433
9434 /*
9435 * Call scsi_free before mem_free since scsi bufs are released to their
9436 * corresponding pools here.
9437 */
9438 lpfc_scsi_free(phba);
9439 lpfc_mem_free_all(phba);
9440
9441 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9442 phba->hbqslimp.virt, phba->hbqslimp.phys);
9443
9444 /* Free resources associated with SLI2 interface */
9445 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
9446 phba->slim2p.virt, phba->slim2p.phys);
9447
9448 /* unmap adapter SLIM and Control Registers */
9449 iounmap(phba->ctrl_regs_memmap_p);
9450 iounmap(phba->slim_memmap_p);
9451
9452 lpfc_hba_free(phba);
9453
9454 pci_release_selected_regions(pdev, bars);
9455 pci_disable_device(pdev);
9456}
9457
9458/**
9459 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
9460 * @pdev: pointer to PCI device
9461 * @msg: power management message
9462 *
9463 * This routine is to be called from the kernel's PCI subsystem to support
9464 * system Power Management (PM) to device with SLI-3 interface spec. When
9465 * PM invokes this method, it quiesces the device by stopping the driver's
9466 * worker thread for the device, turning off device's interrupt and DMA,
9467 * and bring the device offline. Note that as the driver implements the
9468 * minimum PM requirements to a power-aware driver's PM support for the
9469 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9470 * to the suspend() method call will be treated as SUSPEND and the driver will
9471 * fully reinitialize its device during resume() method call, the driver will
9472 * set device to PCI_D3hot state in PCI config space instead of setting it
9473 * according to the @msg provided by the PM.
9474 *
9475 * Return code
9476 * 0 - driver suspended the device
9477 * Error otherwise
9478 **/
9479static int
9480lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
9481{
9482 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9483 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9484
9485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9486 "0473 PCI device Power Management suspend.\n");
9487
9488 /* Bring down the device */
618a5230 9489 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
da0436e9
JS
9490 lpfc_offline(phba);
9491 kthread_stop(phba->worker_thread);
9492
9493 /* Disable interrupt from device */
9494 lpfc_sli_disable_intr(phba);
9495
9496 /* Save device state to PCI config space */
9497 pci_save_state(pdev);
9498 pci_set_power_state(pdev, PCI_D3hot);
9499
9500 return 0;
9501}
9502
9503/**
9504 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
9505 * @pdev: pointer to PCI device
9506 *
9507 * This routine is to be called from the kernel's PCI subsystem to support
9508 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9509 * invokes this method, it restores the device's PCI config space state and
9510 * fully reinitializes the device and brings it online. Note that as the
9511 * driver implements the minimum PM requirements to a power-aware driver's
9512 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9513 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9514 * driver will fully reinitialize its device during resume() method call,
9515 * the device will be set to PCI_D0 directly in PCI config space before
9516 * restoring the state.
9517 *
9518 * Return code
9519 * 0 - driver suspended the device
9520 * Error otherwise
9521 **/
9522static int
9523lpfc_pci_resume_one_s3(struct pci_dev *pdev)
9524{
9525 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9526 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9527 uint32_t intr_mode;
9528 int error;
9529
9530 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9531 "0452 PCI device Power Management resume.\n");
9532
9533 /* Restore device state from PCI config space */
9534 pci_set_power_state(pdev, PCI_D0);
9535 pci_restore_state(pdev);
0d878419 9536
1dfb5a47
JS
9537 /*
9538 * As the new kernel behavior of pci_restore_state() API call clears
9539 * device saved_state flag, need to save the restored state again.
9540 */
9541 pci_save_state(pdev);
9542
da0436e9
JS
9543 if (pdev->is_busmaster)
9544 pci_set_master(pdev);
9545
9546 /* Startup the kernel thread for this host adapter. */
9547 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9548 "lpfc_worker_%d", phba->brd_no);
9549 if (IS_ERR(phba->worker_thread)) {
9550 error = PTR_ERR(phba->worker_thread);
9551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9552 "0434 PM resume failed to start worker "
9553 "thread: error=x%x.\n", error);
9554 return error;
9555 }
9556
9557 /* Configure and enable interrupt */
9558 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
9559 if (intr_mode == LPFC_INTR_ERROR) {
9560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9561 "0430 PM resume Failed to enable interrupt\n");
9562 return -EIO;
9563 } else
9564 phba->intr_mode = intr_mode;
9565
9566 /* Restart HBA and bring it online */
9567 lpfc_sli_brdrestart(phba);
9568 lpfc_online(phba);
9569
9570 /* Log the current active interrupt mode */
9571 lpfc_log_intr_mode(phba, phba->intr_mode);
9572
9573 return 0;
9574}
9575
891478a2
JS
9576/**
9577 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9578 * @phba: pointer to lpfc hba data structure.
9579 *
9580 * This routine is called to prepare the SLI3 device for PCI slot recover. It
e2af0d2e 9581 * aborts all the outstanding SCSI I/Os to the pci device.
891478a2
JS
9582 **/
9583static void
9584lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9585{
e2af0d2e
JS
9586 struct lpfc_sli *psli = &phba->sli;
9587 struct lpfc_sli_ring *pring;
9588
891478a2
JS
9589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9590 "2723 PCI channel I/O abort preparing for recovery\n");
e2af0d2e
JS
9591
9592 /*
9593 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9594 * and let the SCSI mid-layer to retry them to recover.
9595 */
9596 pring = &psli->ring[psli->fcp_ring];
9597 lpfc_sli_abort_iocb_ring(phba, pring);
891478a2
JS
9598}
9599
0d878419
JS
9600/**
9601 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9602 * @phba: pointer to lpfc hba data structure.
9603 *
9604 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9605 * disables the device interrupt and pci device, and aborts the internal FCP
9606 * pending I/Os.
9607 **/
9608static void
9609lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9610{
0d878419 9611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
891478a2 9612 "2710 PCI channel disable preparing for reset\n");
e2af0d2e 9613
75baf696 9614 /* Block any management I/Os to the device */
618a5230 9615 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
75baf696 9616
e2af0d2e
JS
9617 /* Block all SCSI devices' I/Os on the host */
9618 lpfc_scsi_dev_block(phba);
9619
ea714f3d
JS
9620 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9621 lpfc_sli_flush_fcp_rings(phba);
9622
e2af0d2e
JS
9623 /* stop all timers */
9624 lpfc_stop_hba_timers(phba);
9625
0d878419
JS
9626 /* Disable interrupt and pci device */
9627 lpfc_sli_disable_intr(phba);
9628 pci_disable_device(phba->pcidev);
0d878419
JS
9629}
9630
9631/**
9632 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9633 * @phba: pointer to lpfc hba data structure.
9634 *
9635 * This routine is called to prepare the SLI3 device for PCI slot permanently
9636 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9637 * pending I/Os.
9638 **/
9639static void
75baf696 9640lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
0d878419
JS
9641{
9642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
891478a2 9643 "2711 PCI channel permanent disable for failure\n");
e2af0d2e
JS
9644 /* Block all SCSI devices' I/Os on the host */
9645 lpfc_scsi_dev_block(phba);
9646
9647 /* stop all timers */
9648 lpfc_stop_hba_timers(phba);
9649
0d878419
JS
9650 /* Clean up all driver's outstanding SCSI I/Os */
9651 lpfc_sli_flush_fcp_rings(phba);
9652}
9653
da0436e9
JS
9654/**
9655 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
9656 * @pdev: pointer to PCI device.
9657 * @state: the current PCI connection state.
9658 *
9659 * This routine is called from the PCI subsystem for I/O error handling to
9660 * device with SLI-3 interface spec. This function is called by the PCI
9661 * subsystem after a PCI bus error affecting this device has been detected.
9662 * When this function is invoked, it will need to stop all the I/Os and
9663 * interrupt(s) to the device. Once that is done, it will return
9664 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9665 * as desired.
9666 *
9667 * Return codes
0d878419 9668 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
da0436e9
JS
9669 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9670 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9671 **/
9672static pci_ers_result_t
9673lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
9674{
9675 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9676 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
da0436e9 9677
0d878419
JS
9678 switch (state) {
9679 case pci_channel_io_normal:
891478a2
JS
9680 /* Non-fatal error, prepare for recovery */
9681 lpfc_sli_prep_dev_for_recover(phba);
0d878419
JS
9682 return PCI_ERS_RESULT_CAN_RECOVER;
9683 case pci_channel_io_frozen:
9684 /* Fatal error, prepare for slot reset */
9685 lpfc_sli_prep_dev_for_reset(phba);
9686 return PCI_ERS_RESULT_NEED_RESET;
9687 case pci_channel_io_perm_failure:
9688 /* Permanent failure, prepare for device down */
75baf696 9689 lpfc_sli_prep_dev_for_perm_failure(phba);
da0436e9 9690 return PCI_ERS_RESULT_DISCONNECT;
0d878419
JS
9691 default:
9692 /* Unknown state, prepare and request slot reset */
9693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9694 "0472 Unknown PCI error state: x%x\n", state);
9695 lpfc_sli_prep_dev_for_reset(phba);
9696 return PCI_ERS_RESULT_NEED_RESET;
da0436e9 9697 }
da0436e9
JS
9698}
9699
9700/**
9701 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
9702 * @pdev: pointer to PCI device.
9703 *
9704 * This routine is called from the PCI subsystem for error handling to
9705 * device with SLI-3 interface spec. This is called after PCI bus has been
9706 * reset to restart the PCI card from scratch, as if from a cold-boot.
9707 * During the PCI subsystem error recovery, after driver returns
9708 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9709 * recovery and then call this routine before calling the .resume method
9710 * to recover the device. This function will initialize the HBA device,
9711 * enable the interrupt, but it will just put the HBA to offline state
9712 * without passing any I/O traffic.
9713 *
9714 * Return codes
9715 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9716 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9717 */
9718static pci_ers_result_t
9719lpfc_io_slot_reset_s3(struct pci_dev *pdev)
9720{
9721 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9722 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9723 struct lpfc_sli *psli = &phba->sli;
9724 uint32_t intr_mode;
9725
9726 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9727 if (pci_enable_device_mem(pdev)) {
9728 printk(KERN_ERR "lpfc: Cannot re-enable "
9729 "PCI device after reset.\n");
9730 return PCI_ERS_RESULT_DISCONNECT;
9731 }
9732
9733 pci_restore_state(pdev);
1dfb5a47
JS
9734
9735 /*
9736 * As the new kernel behavior of pci_restore_state() API call clears
9737 * device saved_state flag, need to save the restored state again.
9738 */
9739 pci_save_state(pdev);
9740
da0436e9
JS
9741 if (pdev->is_busmaster)
9742 pci_set_master(pdev);
9743
9744 spin_lock_irq(&phba->hbalock);
9745 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9746 spin_unlock_irq(&phba->hbalock);
9747
9748 /* Configure and enable interrupt */
9749 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
9750 if (intr_mode == LPFC_INTR_ERROR) {
9751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9752 "0427 Cannot re-enable interrupt after "
9753 "slot reset.\n");
9754 return PCI_ERS_RESULT_DISCONNECT;
9755 } else
9756 phba->intr_mode = intr_mode;
9757
75baf696 9758 /* Take device offline, it will perform cleanup */
618a5230 9759 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
da0436e9
JS
9760 lpfc_offline(phba);
9761 lpfc_sli_brdrestart(phba);
9762
9763 /* Log the current active interrupt mode */
9764 lpfc_log_intr_mode(phba, phba->intr_mode);
9765
9766 return PCI_ERS_RESULT_RECOVERED;
9767}
9768
9769/**
9770 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
9771 * @pdev: pointer to PCI device
9772 *
9773 * This routine is called from the PCI subsystem for error handling to device
9774 * with SLI-3 interface spec. It is called when kernel error recovery tells
9775 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9776 * error recovery. After this call, traffic can start to flow from this device
9777 * again.
9778 */
9779static void
9780lpfc_io_resume_s3(struct pci_dev *pdev)
9781{
9782 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9783 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3772a991 9784
e2af0d2e 9785 /* Bring device online, it will be no-op for non-fatal error resume */
da0436e9 9786 lpfc_online(phba);
0d878419
JS
9787
9788 /* Clean up Advanced Error Reporting (AER) if needed */
9789 if (phba->hba_flag & HBA_AER_ENABLED)
9790 pci_cleanup_aer_uncorrect_error_status(pdev);
da0436e9 9791}
3772a991 9792
da0436e9
JS
9793/**
9794 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9795 * @phba: pointer to lpfc hba data structure.
9796 *
9797 * returns the number of ELS/CT IOCBs to reserve
9798 **/
9799int
9800lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9801{
9802 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9803
f1126688
JS
9804 if (phba->sli_rev == LPFC_SLI_REV4) {
9805 if (max_xri <= 100)
6a9c52cf 9806 return 10;
f1126688 9807 else if (max_xri <= 256)
6a9c52cf 9808 return 25;
f1126688 9809 else if (max_xri <= 512)
6a9c52cf 9810 return 50;
f1126688 9811 else if (max_xri <= 1024)
6a9c52cf 9812 return 100;
8a9d2e80 9813 else if (max_xri <= 1536)
6a9c52cf 9814 return 150;
8a9d2e80
JS
9815 else if (max_xri <= 2048)
9816 return 200;
9817 else
9818 return 250;
f1126688
JS
9819 } else
9820 return 0;
3772a991
JS
9821}
9822
52d52440
JS
9823/**
9824 * lpfc_write_firmware - attempt to write a firmware image to the port
52d52440 9825 * @fw: pointer to firmware image returned from request_firmware.
ce396282 9826 * @phba: pointer to lpfc hba data structure.
52d52440 9827 *
52d52440 9828 **/
ce396282
JS
9829static void
9830lpfc_write_firmware(const struct firmware *fw, void *context)
52d52440 9831{
ce396282 9832 struct lpfc_hba *phba = (struct lpfc_hba *)context;
6b5151fd 9833 char fwrev[FW_REV_STR_SIZE];
ce396282 9834 struct lpfc_grp_hdr *image;
52d52440
JS
9835 struct list_head dma_buffer_list;
9836 int i, rc = 0;
9837 struct lpfc_dmabuf *dmabuf, *next;
9838 uint32_t offset = 0, temp_offset = 0;
9839
c71ab861 9840 /* It can be null in no-wait mode, sanity check */
ce396282
JS
9841 if (!fw) {
9842 rc = -ENXIO;
9843 goto out;
9844 }
9845 image = (struct lpfc_grp_hdr *)fw->data;
9846
52d52440 9847 INIT_LIST_HEAD(&dma_buffer_list);
079b5c91
JS
9848 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9849 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9850 LPFC_FILE_TYPE_GROUP) ||
9851 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9852 (be32_to_cpu(image->size) != fw->size)) {
52d52440
JS
9853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9854 "3022 Invalid FW image found. "
079b5c91
JS
9855 "Magic:%x Type:%x ID:%x\n",
9856 be32_to_cpu(image->magic_number),
9857 bf_get_be32(lpfc_grp_hdr_file_type, image),
9858 bf_get_be32(lpfc_grp_hdr_id, image));
ce396282
JS
9859 rc = -EINVAL;
9860 goto release_out;
52d52440
JS
9861 }
9862 lpfc_decode_firmware_rev(phba, fwrev, 1);
88a2cfbb 9863 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
52d52440 9864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
ce396282 9865 "3023 Updating Firmware, Current Version:%s "
52d52440 9866 "New Version:%s\n",
88a2cfbb 9867 fwrev, image->revision);
52d52440
JS
9868 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9869 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9870 GFP_KERNEL);
9871 if (!dmabuf) {
9872 rc = -ENOMEM;
ce396282 9873 goto release_out;
52d52440
JS
9874 }
9875 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9876 SLI4_PAGE_SIZE,
9877 &dmabuf->phys,
9878 GFP_KERNEL);
9879 if (!dmabuf->virt) {
9880 kfree(dmabuf);
9881 rc = -ENOMEM;
ce396282 9882 goto release_out;
52d52440
JS
9883 }
9884 list_add_tail(&dmabuf->list, &dma_buffer_list);
9885 }
9886 while (offset < fw->size) {
9887 temp_offset = offset;
9888 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
079b5c91 9889 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
52d52440
JS
9890 memcpy(dmabuf->virt,
9891 fw->data + temp_offset,
079b5c91
JS
9892 fw->size - temp_offset);
9893 temp_offset = fw->size;
52d52440
JS
9894 break;
9895 }
52d52440
JS
9896 memcpy(dmabuf->virt, fw->data + temp_offset,
9897 SLI4_PAGE_SIZE);
88a2cfbb 9898 temp_offset += SLI4_PAGE_SIZE;
52d52440
JS
9899 }
9900 rc = lpfc_wr_object(phba, &dma_buffer_list,
9901 (fw->size - offset), &offset);
ce396282
JS
9902 if (rc)
9903 goto release_out;
52d52440
JS
9904 }
9905 rc = offset;
9906 }
ce396282
JS
9907
9908release_out:
52d52440
JS
9909 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9910 list_del(&dmabuf->list);
9911 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9912 dmabuf->virt, dmabuf->phys);
9913 kfree(dmabuf);
9914 }
ce396282
JS
9915 release_firmware(fw);
9916out:
9917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
c71ab861 9918 "3024 Firmware update done: %d.\n", rc);
ce396282 9919 return;
52d52440
JS
9920}
9921
c71ab861
JS
9922/**
9923 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
9924 * @phba: pointer to lpfc hba data structure.
9925 *
9926 * This routine is called to perform Linux generic firmware upgrade on device
9927 * that supports such feature.
9928 **/
9929int
9930lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
9931{
9932 uint8_t file_name[ELX_MODEL_NAME_SIZE];
9933 int ret;
9934 const struct firmware *fw;
9935
9936 /* Only supported on SLI4 interface type 2 for now */
9937 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
9938 LPFC_SLI_INTF_IF_TYPE_2)
9939 return -EPERM;
9940
9941 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
9942
9943 if (fw_upgrade == INT_FW_UPGRADE) {
9944 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
9945 file_name, &phba->pcidev->dev,
9946 GFP_KERNEL, (void *)phba,
9947 lpfc_write_firmware);
9948 } else if (fw_upgrade == RUN_FW_UPGRADE) {
9949 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
9950 if (!ret)
9951 lpfc_write_firmware(fw, (void *)phba);
9952 } else {
9953 ret = -EINVAL;
9954 }
9955
9956 return ret;
9957}
9958
3772a991 9959/**
da0436e9 9960 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
3772a991
JS
9961 * @pdev: pointer to PCI device
9962 * @pid: pointer to PCI device identifier
9963 *
da0436e9
JS
9964 * This routine is called from the kernel's PCI subsystem to device with
9965 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
3772a991 9966 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
da0436e9
JS
9967 * information of the device and driver to see if the driver state that it
9968 * can support this kind of device. If the match is successful, the driver
9969 * core invokes this routine. If this routine determines it can claim the HBA,
9970 * it does all the initialization that it needs to do to handle the HBA
9971 * properly.
3772a991
JS
9972 *
9973 * Return code
9974 * 0 - driver can claim the device
9975 * negative value - driver can not claim the device
9976 **/
6f039790 9977static int
da0436e9 9978lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
3772a991
JS
9979{
9980 struct lpfc_hba *phba;
9981 struct lpfc_vport *vport = NULL;
6669f9bb 9982 struct Scsi_Host *shost = NULL;
ce396282 9983 int error, ret;
3772a991 9984 uint32_t cfg_mode, intr_mode;
67d12733 9985 int adjusted_fcp_io_channel;
3772a991
JS
9986
9987 /* Allocate memory for HBA structure */
9988 phba = lpfc_hba_alloc(pdev);
9989 if (!phba)
9990 return -ENOMEM;
9991
9992 /* Perform generic PCI device enabling operation */
9993 error = lpfc_enable_pci_dev(phba);
079b5c91 9994 if (error)
3772a991 9995 goto out_free_phba;
3772a991 9996
da0436e9
JS
9997 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9998 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
3772a991
JS
9999 if (error)
10000 goto out_disable_pci_dev;
10001
da0436e9
JS
10002 /* Set up SLI-4 specific device PCI memory space */
10003 error = lpfc_sli4_pci_mem_setup(phba);
3772a991
JS
10004 if (error) {
10005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 10006 "1410 Failed to set up pci memory space.\n");
3772a991
JS
10007 goto out_disable_pci_dev;
10008 }
10009
10010 /* Set up phase-1 common device driver resources */
10011 error = lpfc_setup_driver_resource_phase1(phba);
10012 if (error) {
10013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
10014 "1411 Failed to set up driver resource.\n");
10015 goto out_unset_pci_mem_s4;
3772a991
JS
10016 }
10017
da0436e9
JS
10018 /* Set up SLI-4 Specific device driver resources */
10019 error = lpfc_sli4_driver_resource_setup(phba);
3772a991
JS
10020 if (error) {
10021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
10022 "1412 Failed to set up driver resource.\n");
10023 goto out_unset_pci_mem_s4;
3772a991
JS
10024 }
10025
10026 /* Initialize and populate the iocb list per host */
2a9bf3d0
JS
10027
10028 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10029 "2821 initialize iocb list %d.\n",
10030 phba->cfg_iocb_cnt*1024);
10031 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10032
3772a991
JS
10033 if (error) {
10034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
10035 "1413 Failed to initialize iocb list.\n");
10036 goto out_unset_driver_resource_s4;
3772a991
JS
10037 }
10038
19ca7609 10039 INIT_LIST_HEAD(&phba->active_rrq_list);
7d791df7 10040 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
19ca7609 10041
3772a991
JS
10042 /* Set up common device driver resources */
10043 error = lpfc_setup_driver_resource_phase2(phba);
10044 if (error) {
10045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 10046 "1414 Failed to set up driver resource.\n");
3772a991
JS
10047 goto out_free_iocb_list;
10048 }
10049
079b5c91
JS
10050 /* Get the default values for Model Name and Description */
10051 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10052
3772a991
JS
10053 /* Create SCSI host to the physical port */
10054 error = lpfc_create_shost(phba);
10055 if (error) {
10056 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 10057 "1415 Failed to create scsi host.\n");
3772a991
JS
10058 goto out_unset_driver_resource;
10059 }
9399627f 10060
5b75da2f 10061 /* Configure sysfs attributes */
3772a991
JS
10062 vport = phba->pport;
10063 error = lpfc_alloc_sysfs_attr(vport);
10064 if (error) {
9399627f 10065 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 10066 "1416 Failed to allocate sysfs attr\n");
3772a991 10067 goto out_destroy_shost;
98c9ea5c 10068 }
875fbdfe 10069
6669f9bb 10070 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
3772a991 10071 /* Now, trying to enable interrupt and bring up the device */
5b75da2f 10072 cfg_mode = phba->cfg_use_msi;
5b75da2f 10073
7b15db32
JS
10074 /* Put device to a known state before enabling interrupt */
10075 lpfc_stop_port(phba);
10076 /* Configure and enable interrupt */
10077 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10078 if (intr_mode == LPFC_INTR_ERROR) {
10079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10080 "0426 Failed to enable interrupt.\n");
10081 error = -ENODEV;
10082 goto out_free_sysfs_attr;
10083 }
10084 /* Default to single EQ for non-MSI-X */
10085 if (phba->intr_type != MSIX)
10086 adjusted_fcp_io_channel = 1;
10087 else
10088 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10089 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10090 /* Set up SLI-4 HBA */
10091 if (lpfc_sli4_hba_setup(phba)) {
10092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10093 "1421 Failed to set up hba\n");
10094 error = -ENODEV;
10095 goto out_disable_intr;
98c9ea5c 10096 }
858c9f6c 10097
7b15db32
JS
10098 /* Log the current active interrupt mode */
10099 phba->intr_mode = intr_mode;
10100 lpfc_log_intr_mode(phba, intr_mode);
10101
3772a991
JS
10102 /* Perform post initialization setup */
10103 lpfc_post_init_setup(phba);
dea3101e 10104
c71ab861
JS
10105 /* check for firmware upgrade or downgrade */
10106 if (phba->cfg_request_firmware_upgrade)
10107 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
52d52440 10108
1c6834a7
JS
10109 /* Check if there are static vports to be created. */
10110 lpfc_create_static_vport(phba);
dea3101e
JB
10111 return 0;
10112
da0436e9
JS
10113out_disable_intr:
10114 lpfc_sli4_disable_intr(phba);
5b75da2f
JS
10115out_free_sysfs_attr:
10116 lpfc_free_sysfs_attr(vport);
3772a991
JS
10117out_destroy_shost:
10118 lpfc_destroy_shost(phba);
10119out_unset_driver_resource:
10120 lpfc_unset_driver_resource_phase2(phba);
10121out_free_iocb_list:
10122 lpfc_free_iocb_list(phba);
da0436e9
JS
10123out_unset_driver_resource_s4:
10124 lpfc_sli4_driver_resource_unset(phba);
10125out_unset_pci_mem_s4:
10126 lpfc_sli4_pci_mem_unset(phba);
3772a991
JS
10127out_disable_pci_dev:
10128 lpfc_disable_pci_dev(phba);
6669f9bb
JS
10129 if (shost)
10130 scsi_host_put(shost);
2e0fef85 10131out_free_phba:
3772a991 10132 lpfc_hba_free(phba);
dea3101e
JB
10133 return error;
10134}
10135
e59058c4 10136/**
da0436e9 10137 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
e59058c4
JS
10138 * @pdev: pointer to PCI device
10139 *
da0436e9
JS
10140 * This routine is called from the kernel's PCI subsystem to device with
10141 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
3772a991
JS
10142 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10143 * device to be removed from the PCI subsystem properly.
e59058c4 10144 **/
6f039790 10145static void
da0436e9 10146lpfc_pci_remove_one_s4(struct pci_dev *pdev)
dea3101e 10147{
da0436e9 10148 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2e0fef85 10149 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
eada272d 10150 struct lpfc_vport **vports;
da0436e9 10151 struct lpfc_hba *phba = vport->phba;
eada272d 10152 int i;
8a4df120 10153
da0436e9 10154 /* Mark the device unloading flag */
549e55cd 10155 spin_lock_irq(&phba->hbalock);
51ef4c26 10156 vport->load_flag |= FC_UNLOADING;
549e55cd 10157 spin_unlock_irq(&phba->hbalock);
2e0fef85 10158
da0436e9 10159 /* Free the HBA sysfs attributes */
858c9f6c
JS
10160 lpfc_free_sysfs_attr(vport);
10161
eada272d
JS
10162 /* Release all the vports against this physical port */
10163 vports = lpfc_create_vport_work_array(phba);
10164 if (vports != NULL)
587a37f6
JS
10165 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10166 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10167 continue;
eada272d 10168 fc_vport_terminate(vports[i]->fc_vport);
587a37f6 10169 }
eada272d
JS
10170 lpfc_destroy_vport_work_array(phba, vports);
10171
10172 /* Remove FC host and then SCSI host with the physical port */
858c9f6c
JS
10173 fc_remove_host(shost);
10174 scsi_remove_host(shost);
da0436e9
JS
10175
10176 /* Perform cleanup on the physical port */
87af33fe
JS
10177 lpfc_cleanup(vport);
10178
2e0fef85 10179 /*
da0436e9 10180 * Bring down the SLI Layer. This step disables all interrupts,
2e0fef85 10181 * clears the rings, discards all mailbox commands, and resets
da0436e9 10182 * the HBA FCoE function.
2e0fef85 10183 */
da0436e9
JS
10184 lpfc_debugfs_terminate(vport);
10185 lpfc_sli4_hba_unset(phba);
a257bf90 10186
858c9f6c
JS
10187 spin_lock_irq(&phba->hbalock);
10188 list_del_init(&vport->listentry);
10189 spin_unlock_irq(&phba->hbalock);
10190
3677a3a7 10191 /* Perform scsi free before driver resource_unset since scsi
da0436e9 10192 * buffers are released to their corresponding pools here.
2e0fef85
JS
10193 */
10194 lpfc_scsi_free(phba);
67d12733 10195
da0436e9 10196 lpfc_sli4_driver_resource_unset(phba);
ed957684 10197
da0436e9
JS
10198 /* Unmap adapter Control and Doorbell registers */
10199 lpfc_sli4_pci_mem_unset(phba);
2e0fef85 10200
da0436e9
JS
10201 /* Release PCI resources and disable device's PCI function */
10202 scsi_host_put(shost);
10203 lpfc_disable_pci_dev(phba);
2e0fef85 10204
da0436e9 10205 /* Finally, free the driver's device data structure */
3772a991 10206 lpfc_hba_free(phba);
2e0fef85 10207
da0436e9 10208 return;
dea3101e
JB
10209}
10210
3a55b532 10211/**
da0436e9 10212 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
3a55b532
JS
10213 * @pdev: pointer to PCI device
10214 * @msg: power management message
10215 *
da0436e9
JS
10216 * This routine is called from the kernel's PCI subsystem to support system
10217 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10218 * this method, it quiesces the device by stopping the driver's worker
10219 * thread for the device, turning off device's interrupt and DMA, and bring
10220 * the device offline. Note that as the driver implements the minimum PM
10221 * requirements to a power-aware driver's PM support for suspend/resume -- all
10222 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10223 * method call will be treated as SUSPEND and the driver will fully
10224 * reinitialize its device during resume() method call, the driver will set
10225 * device to PCI_D3hot state in PCI config space instead of setting it
3772a991 10226 * according to the @msg provided by the PM.
3a55b532
JS
10227 *
10228 * Return code
3772a991
JS
10229 * 0 - driver suspended the device
10230 * Error otherwise
3a55b532
JS
10231 **/
10232static int
da0436e9 10233lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
3a55b532
JS
10234{
10235 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10236 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10237
10238 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
75baf696 10239 "2843 PCI device Power Management suspend.\n");
3a55b532
JS
10240
10241 /* Bring down the device */
618a5230 10242 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
3a55b532
JS
10243 lpfc_offline(phba);
10244 kthread_stop(phba->worker_thread);
10245
10246 /* Disable interrupt from device */
da0436e9 10247 lpfc_sli4_disable_intr(phba);
5350d872 10248 lpfc_sli4_queue_destroy(phba);
3a55b532
JS
10249
10250 /* Save device state to PCI config space */
10251 pci_save_state(pdev);
10252 pci_set_power_state(pdev, PCI_D3hot);
10253
10254 return 0;
10255}
10256
10257/**
da0436e9 10258 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
3a55b532
JS
10259 * @pdev: pointer to PCI device
10260 *
da0436e9
JS
10261 * This routine is called from the kernel's PCI subsystem to support system
10262 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10263 * this method, it restores the device's PCI config space state and fully
10264 * reinitializes the device and brings it online. Note that as the driver
10265 * implements the minimum PM requirements to a power-aware driver's PM for
10266 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10267 * to the suspend() method call will be treated as SUSPEND and the driver
10268 * will fully reinitialize its device during resume() method call, the device
10269 * will be set to PCI_D0 directly in PCI config space before restoring the
10270 * state.
3a55b532
JS
10271 *
10272 * Return code
3772a991
JS
10273 * 0 - driver suspended the device
10274 * Error otherwise
3a55b532
JS
10275 **/
10276static int
da0436e9 10277lpfc_pci_resume_one_s4(struct pci_dev *pdev)
3a55b532
JS
10278{
10279 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10280 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
5b75da2f 10281 uint32_t intr_mode;
3a55b532
JS
10282 int error;
10283
10284 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
da0436e9 10285 "0292 PCI device Power Management resume.\n");
3a55b532
JS
10286
10287 /* Restore device state from PCI config space */
10288 pci_set_power_state(pdev, PCI_D0);
10289 pci_restore_state(pdev);
1dfb5a47
JS
10290
10291 /*
10292 * As the new kernel behavior of pci_restore_state() API call clears
10293 * device saved_state flag, need to save the restored state again.
10294 */
10295 pci_save_state(pdev);
10296
3a55b532
JS
10297 if (pdev->is_busmaster)
10298 pci_set_master(pdev);
10299
da0436e9 10300 /* Startup the kernel thread for this host adapter. */
3a55b532
JS
10301 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10302 "lpfc_worker_%d", phba->brd_no);
10303 if (IS_ERR(phba->worker_thread)) {
10304 error = PTR_ERR(phba->worker_thread);
10305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 10306 "0293 PM resume failed to start worker "
3a55b532
JS
10307 "thread: error=x%x.\n", error);
10308 return error;
10309 }
10310
5b75da2f 10311 /* Configure and enable interrupt */
da0436e9 10312 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
5b75da2f 10313 if (intr_mode == LPFC_INTR_ERROR) {
3a55b532 10314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 10315 "0294 PM resume Failed to enable interrupt\n");
5b75da2f
JS
10316 return -EIO;
10317 } else
10318 phba->intr_mode = intr_mode;
3a55b532
JS
10319
10320 /* Restart HBA and bring it online */
10321 lpfc_sli_brdrestart(phba);
10322 lpfc_online(phba);
10323
5b75da2f
JS
10324 /* Log the current active interrupt mode */
10325 lpfc_log_intr_mode(phba, phba->intr_mode);
10326
3a55b532
JS
10327 return 0;
10328}
10329
75baf696
JS
10330/**
10331 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10332 * @phba: pointer to lpfc hba data structure.
10333 *
10334 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10335 * aborts all the outstanding SCSI I/Os to the pci device.
10336 **/
10337static void
10338lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10339{
10340 struct lpfc_sli *psli = &phba->sli;
10341 struct lpfc_sli_ring *pring;
10342
10343 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10344 "2828 PCI channel I/O abort preparing for recovery\n");
10345 /*
10346 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10347 * and let the SCSI mid-layer to retry them to recover.
10348 */
10349 pring = &psli->ring[psli->fcp_ring];
10350 lpfc_sli_abort_iocb_ring(phba, pring);
10351}
10352
10353/**
10354 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10355 * @phba: pointer to lpfc hba data structure.
10356 *
10357 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10358 * disables the device interrupt and pci device, and aborts the internal FCP
10359 * pending I/Os.
10360 **/
10361static void
10362lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10363{
10364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10365 "2826 PCI channel disable preparing for reset\n");
10366
10367 /* Block any management I/Os to the device */
618a5230 10368 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
75baf696
JS
10369
10370 /* Block all SCSI devices' I/Os on the host */
10371 lpfc_scsi_dev_block(phba);
10372
ea714f3d
JS
10373 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10374 lpfc_sli_flush_fcp_rings(phba);
10375
75baf696
JS
10376 /* stop all timers */
10377 lpfc_stop_hba_timers(phba);
10378
10379 /* Disable interrupt and pci device */
10380 lpfc_sli4_disable_intr(phba);
5350d872 10381 lpfc_sli4_queue_destroy(phba);
75baf696 10382 pci_disable_device(phba->pcidev);
75baf696
JS
10383}
10384
10385/**
10386 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10387 * @phba: pointer to lpfc hba data structure.
10388 *
10389 * This routine is called to prepare the SLI4 device for PCI slot permanently
10390 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10391 * pending I/Os.
10392 **/
10393static void
10394lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10395{
10396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10397 "2827 PCI channel permanent disable for failure\n");
10398
10399 /* Block all SCSI devices' I/Os on the host */
10400 lpfc_scsi_dev_block(phba);
10401
10402 /* stop all timers */
10403 lpfc_stop_hba_timers(phba);
10404
10405 /* Clean up all driver's outstanding SCSI I/Os */
10406 lpfc_sli_flush_fcp_rings(phba);
10407}
10408
8d63f375 10409/**
da0436e9 10410 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
e59058c4
JS
10411 * @pdev: pointer to PCI device.
10412 * @state: the current PCI connection state.
8d63f375 10413 *
da0436e9
JS
10414 * This routine is called from the PCI subsystem for error handling to device
10415 * with SLI-4 interface spec. This function is called by the PCI subsystem
10416 * after a PCI bus error affecting this device has been detected. When this
10417 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10418 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10419 * for the PCI subsystem to perform proper recovery as desired.
e59058c4
JS
10420 *
10421 * Return codes
3772a991
JS
10422 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10423 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
e59058c4 10424 **/
3772a991 10425static pci_ers_result_t
da0436e9 10426lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8d63f375 10427{
75baf696
JS
10428 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10429 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10430
10431 switch (state) {
10432 case pci_channel_io_normal:
10433 /* Non-fatal error, prepare for recovery */
10434 lpfc_sli4_prep_dev_for_recover(phba);
10435 return PCI_ERS_RESULT_CAN_RECOVER;
10436 case pci_channel_io_frozen:
10437 /* Fatal error, prepare for slot reset */
10438 lpfc_sli4_prep_dev_for_reset(phba);
10439 return PCI_ERS_RESULT_NEED_RESET;
10440 case pci_channel_io_perm_failure:
10441 /* Permanent failure, prepare for device down */
10442 lpfc_sli4_prep_dev_for_perm_failure(phba);
10443 return PCI_ERS_RESULT_DISCONNECT;
10444 default:
10445 /* Unknown state, prepare and request slot reset */
10446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10447 "2825 Unknown PCI error state: x%x\n", state);
10448 lpfc_sli4_prep_dev_for_reset(phba);
10449 return PCI_ERS_RESULT_NEED_RESET;
10450 }
8d63f375
LV
10451}
10452
10453/**
da0436e9 10454 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
e59058c4
JS
10455 * @pdev: pointer to PCI device.
10456 *
da0436e9
JS
10457 * This routine is called from the PCI subsystem for error handling to device
10458 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10459 * restart the PCI card from scratch, as if from a cold-boot. During the
10460 * PCI subsystem error recovery, after the driver returns
3772a991 10461 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
da0436e9
JS
10462 * recovery and then call this routine before calling the .resume method to
10463 * recover the device. This function will initialize the HBA device, enable
10464 * the interrupt, but it will just put the HBA to offline state without
10465 * passing any I/O traffic.
8d63f375 10466 *
e59058c4 10467 * Return codes
3772a991
JS
10468 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10469 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8d63f375 10470 */
3772a991 10471static pci_ers_result_t
da0436e9 10472lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8d63f375 10473{
75baf696
JS
10474 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10475 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10476 struct lpfc_sli *psli = &phba->sli;
10477 uint32_t intr_mode;
10478
10479 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10480 if (pci_enable_device_mem(pdev)) {
10481 printk(KERN_ERR "lpfc: Cannot re-enable "
10482 "PCI device after reset.\n");
10483 return PCI_ERS_RESULT_DISCONNECT;
10484 }
10485
10486 pci_restore_state(pdev);
0a96e975
JS
10487
10488 /*
10489 * As the new kernel behavior of pci_restore_state() API call clears
10490 * device saved_state flag, need to save the restored state again.
10491 */
10492 pci_save_state(pdev);
10493
75baf696
JS
10494 if (pdev->is_busmaster)
10495 pci_set_master(pdev);
10496
10497 spin_lock_irq(&phba->hbalock);
10498 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10499 spin_unlock_irq(&phba->hbalock);
10500
10501 /* Configure and enable interrupt */
10502 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10503 if (intr_mode == LPFC_INTR_ERROR) {
10504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10505 "2824 Cannot re-enable interrupt after "
10506 "slot reset.\n");
10507 return PCI_ERS_RESULT_DISCONNECT;
10508 } else
10509 phba->intr_mode = intr_mode;
10510
10511 /* Log the current active interrupt mode */
10512 lpfc_log_intr_mode(phba, phba->intr_mode);
10513
8d63f375
LV
10514 return PCI_ERS_RESULT_RECOVERED;
10515}
10516
10517/**
da0436e9 10518 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
e59058c4 10519 * @pdev: pointer to PCI device
8d63f375 10520 *
3772a991 10521 * This routine is called from the PCI subsystem for error handling to device
da0436e9 10522 * with SLI-4 interface spec. It is called when kernel error recovery tells
3772a991
JS
10523 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10524 * error recovery. After this call, traffic can start to flow from this device
10525 * again.
da0436e9 10526 **/
3772a991 10527static void
da0436e9 10528lpfc_io_resume_s4(struct pci_dev *pdev)
8d63f375 10529{
75baf696
JS
10530 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10531 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10532
10533 /*
10534 * In case of slot reset, as function reset is performed through
10535 * mailbox command which needs DMA to be enabled, this operation
10536 * has to be moved to the io resume phase. Taking device offline
10537 * will perform the necessary cleanup.
10538 */
10539 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10540 /* Perform device reset */
618a5230 10541 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
75baf696
JS
10542 lpfc_offline(phba);
10543 lpfc_sli_brdrestart(phba);
10544 /* Bring the device back online */
10545 lpfc_online(phba);
10546 }
10547
10548 /* Clean up Advanced Error Reporting (AER) if needed */
10549 if (phba->hba_flag & HBA_AER_ENABLED)
10550 pci_cleanup_aer_uncorrect_error_status(pdev);
8d63f375
LV
10551}
10552
3772a991
JS
10553/**
10554 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10555 * @pdev: pointer to PCI device
10556 * @pid: pointer to PCI device identifier
10557 *
10558 * This routine is to be registered to the kernel's PCI subsystem. When an
10559 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10560 * at PCI device-specific information of the device and driver to see if the
10561 * driver state that it can support this kind of device. If the match is
10562 * successful, the driver core invokes this routine. This routine dispatches
10563 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10564 * do all the initialization that it needs to do to handle the HBA device
10565 * properly.
10566 *
10567 * Return code
10568 * 0 - driver can claim the device
10569 * negative value - driver can not claim the device
10570 **/
6f039790 10571static int
3772a991
JS
10572lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10573{
10574 int rc;
8fa38513 10575 struct lpfc_sli_intf intf;
3772a991 10576
28baac74 10577 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
3772a991
JS
10578 return -ENODEV;
10579
8fa38513 10580 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
28baac74 10581 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
da0436e9 10582 rc = lpfc_pci_probe_one_s4(pdev, pid);
8fa38513 10583 else
3772a991 10584 rc = lpfc_pci_probe_one_s3(pdev, pid);
8fa38513 10585
3772a991
JS
10586 return rc;
10587}
10588
10589/**
10590 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10591 * @pdev: pointer to PCI device
10592 *
10593 * This routine is to be registered to the kernel's PCI subsystem. When an
10594 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10595 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10596 * remove routine, which will perform all the necessary cleanup for the
10597 * device to be removed from the PCI subsystem properly.
10598 **/
6f039790 10599static void
3772a991
JS
10600lpfc_pci_remove_one(struct pci_dev *pdev)
10601{
10602 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10603 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10604
10605 switch (phba->pci_dev_grp) {
10606 case LPFC_PCI_DEV_LP:
10607 lpfc_pci_remove_one_s3(pdev);
10608 break;
da0436e9
JS
10609 case LPFC_PCI_DEV_OC:
10610 lpfc_pci_remove_one_s4(pdev);
10611 break;
3772a991
JS
10612 default:
10613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10614 "1424 Invalid PCI device group: 0x%x\n",
10615 phba->pci_dev_grp);
10616 break;
10617 }
10618 return;
10619}
10620
10621/**
10622 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10623 * @pdev: pointer to PCI device
10624 * @msg: power management message
10625 *
10626 * This routine is to be registered to the kernel's PCI subsystem to support
10627 * system Power Management (PM). When PM invokes this method, it dispatches
10628 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10629 * suspend the device.
10630 *
10631 * Return code
10632 * 0 - driver suspended the device
10633 * Error otherwise
10634 **/
10635static int
10636lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10637{
10638 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10639 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10640 int rc = -ENODEV;
10641
10642 switch (phba->pci_dev_grp) {
10643 case LPFC_PCI_DEV_LP:
10644 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10645 break;
da0436e9
JS
10646 case LPFC_PCI_DEV_OC:
10647 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10648 break;
3772a991
JS
10649 default:
10650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10651 "1425 Invalid PCI device group: 0x%x\n",
10652 phba->pci_dev_grp);
10653 break;
10654 }
10655 return rc;
10656}
10657
10658/**
10659 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10660 * @pdev: pointer to PCI device
10661 *
10662 * This routine is to be registered to the kernel's PCI subsystem to support
10663 * system Power Management (PM). When PM invokes this method, it dispatches
10664 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10665 * resume the device.
10666 *
10667 * Return code
10668 * 0 - driver suspended the device
10669 * Error otherwise
10670 **/
10671static int
10672lpfc_pci_resume_one(struct pci_dev *pdev)
10673{
10674 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10675 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10676 int rc = -ENODEV;
10677
10678 switch (phba->pci_dev_grp) {
10679 case LPFC_PCI_DEV_LP:
10680 rc = lpfc_pci_resume_one_s3(pdev);
10681 break;
da0436e9
JS
10682 case LPFC_PCI_DEV_OC:
10683 rc = lpfc_pci_resume_one_s4(pdev);
10684 break;
3772a991
JS
10685 default:
10686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10687 "1426 Invalid PCI device group: 0x%x\n",
10688 phba->pci_dev_grp);
10689 break;
10690 }
10691 return rc;
10692}
10693
10694/**
10695 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10696 * @pdev: pointer to PCI device.
10697 * @state: the current PCI connection state.
10698 *
10699 * This routine is registered to the PCI subsystem for error handling. This
10700 * function is called by the PCI subsystem after a PCI bus error affecting
10701 * this device has been detected. When this routine is invoked, it dispatches
10702 * the action to the proper SLI-3 or SLI-4 device error detected handling
10703 * routine, which will perform the proper error detected operation.
10704 *
10705 * Return codes
10706 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10707 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10708 **/
10709static pci_ers_result_t
10710lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10711{
10712 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10713 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10714 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10715
10716 switch (phba->pci_dev_grp) {
10717 case LPFC_PCI_DEV_LP:
10718 rc = lpfc_io_error_detected_s3(pdev, state);
10719 break;
da0436e9
JS
10720 case LPFC_PCI_DEV_OC:
10721 rc = lpfc_io_error_detected_s4(pdev, state);
10722 break;
3772a991
JS
10723 default:
10724 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10725 "1427 Invalid PCI device group: 0x%x\n",
10726 phba->pci_dev_grp);
10727 break;
10728 }
10729 return rc;
10730}
10731
10732/**
10733 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10734 * @pdev: pointer to PCI device.
10735 *
10736 * This routine is registered to the PCI subsystem for error handling. This
10737 * function is called after PCI bus has been reset to restart the PCI card
10738 * from scratch, as if from a cold-boot. When this routine is invoked, it
10739 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10740 * routine, which will perform the proper device reset.
10741 *
10742 * Return codes
10743 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10744 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10745 **/
10746static pci_ers_result_t
10747lpfc_io_slot_reset(struct pci_dev *pdev)
10748{
10749 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10750 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10751 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10752
10753 switch (phba->pci_dev_grp) {
10754 case LPFC_PCI_DEV_LP:
10755 rc = lpfc_io_slot_reset_s3(pdev);
10756 break;
da0436e9
JS
10757 case LPFC_PCI_DEV_OC:
10758 rc = lpfc_io_slot_reset_s4(pdev);
10759 break;
3772a991
JS
10760 default:
10761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10762 "1428 Invalid PCI device group: 0x%x\n",
10763 phba->pci_dev_grp);
10764 break;
10765 }
10766 return rc;
10767}
10768
10769/**
10770 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10771 * @pdev: pointer to PCI device
10772 *
10773 * This routine is registered to the PCI subsystem for error handling. It
10774 * is called when kernel error recovery tells the lpfc driver that it is
10775 * OK to resume normal PCI operation after PCI bus error recovery. When
10776 * this routine is invoked, it dispatches the action to the proper SLI-3
10777 * or SLI-4 device io_resume routine, which will resume the device operation.
10778 **/
10779static void
10780lpfc_io_resume(struct pci_dev *pdev)
10781{
10782 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10783 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10784
10785 switch (phba->pci_dev_grp) {
10786 case LPFC_PCI_DEV_LP:
10787 lpfc_io_resume_s3(pdev);
10788 break;
da0436e9
JS
10789 case LPFC_PCI_DEV_OC:
10790 lpfc_io_resume_s4(pdev);
10791 break;
3772a991
JS
10792 default:
10793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10794 "1429 Invalid PCI device group: 0x%x\n",
10795 phba->pci_dev_grp);
10796 break;
10797 }
10798 return;
10799}
10800
dea3101e
JB
10801static struct pci_device_id lpfc_id_table[] = {
10802 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10803 PCI_ANY_ID, PCI_ANY_ID, },
06325e74
JSEC
10804 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10805 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
10806 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10807 PCI_ANY_ID, PCI_ANY_ID, },
10808 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10809 PCI_ANY_ID, PCI_ANY_ID, },
10810 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10811 PCI_ANY_ID, PCI_ANY_ID, },
10812 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10813 PCI_ANY_ID, PCI_ANY_ID, },
10814 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10815 PCI_ANY_ID, PCI_ANY_ID, },
10816 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10817 PCI_ANY_ID, PCI_ANY_ID, },
10818 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10819 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
10820 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10821 PCI_ANY_ID, PCI_ANY_ID, },
10822 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10823 PCI_ANY_ID, PCI_ANY_ID, },
10824 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10825 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
10826 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10827 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
10828 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10829 PCI_ANY_ID, PCI_ANY_ID, },
10830 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10831 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
10832 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10833 PCI_ANY_ID, PCI_ANY_ID, },
10834 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10835 PCI_ANY_ID, PCI_ANY_ID, },
10836 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10837 PCI_ANY_ID, PCI_ANY_ID, },
84774a4d
JS
10838 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10839 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
10840 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10841 PCI_ANY_ID, PCI_ANY_ID, },
10842 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10843 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
10844 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10845 PCI_ANY_ID, PCI_ANY_ID, },
10846 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10847 PCI_ANY_ID, PCI_ANY_ID, },
10848 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10849 PCI_ANY_ID, PCI_ANY_ID, },
10850 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10851 PCI_ANY_ID, PCI_ANY_ID, },
10852 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10853 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
10854 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10855 PCI_ANY_ID, PCI_ANY_ID, },
10856 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10857 PCI_ANY_ID, PCI_ANY_ID, },
b87eab38
JS
10858 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10859 PCI_ANY_ID, PCI_ANY_ID, },
10860 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10861 PCI_ANY_ID, PCI_ANY_ID, },
10862 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10863 PCI_ANY_ID, PCI_ANY_ID, },
10864 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10865 PCI_ANY_ID, PCI_ANY_ID, },
10866 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10867 PCI_ANY_ID, PCI_ANY_ID, },
10868 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10869 PCI_ANY_ID, PCI_ANY_ID, },
84774a4d
JS
10870 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10871 PCI_ANY_ID, PCI_ANY_ID, },
10872 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10873 PCI_ANY_ID, PCI_ANY_ID, },
10874 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10875 PCI_ANY_ID, PCI_ANY_ID, },
3772a991
JS
10876 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10877 PCI_ANY_ID, PCI_ANY_ID, },
a747c9ce
JS
10878 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10879 PCI_ANY_ID, PCI_ANY_ID, },
10880 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
6669f9bb 10881 PCI_ANY_ID, PCI_ANY_ID, },
98fc5dd9
JS
10882 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10883 PCI_ANY_ID, PCI_ANY_ID, },
085c647c
JS
10884 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10885 PCI_ANY_ID, PCI_ANY_ID, },
10886 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10887 PCI_ANY_ID, PCI_ANY_ID, },
c0c11512
JS
10888 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10889 PCI_ANY_ID, PCI_ANY_ID, },
10890 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10891 PCI_ANY_ID, PCI_ANY_ID, },
f8cafd38
JS
10892 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
10893 PCI_ANY_ID, PCI_ANY_ID, },
10894 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
10895 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e
JB
10896 { 0 }
10897};
10898
10899MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10900
a55b2d21 10901static const struct pci_error_handlers lpfc_err_handler = {
8d63f375
LV
10902 .error_detected = lpfc_io_error_detected,
10903 .slot_reset = lpfc_io_slot_reset,
10904 .resume = lpfc_io_resume,
10905};
10906
dea3101e
JB
10907static struct pci_driver lpfc_driver = {
10908 .name = LPFC_DRIVER_NAME,
10909 .id_table = lpfc_id_table,
10910 .probe = lpfc_pci_probe_one,
6f039790 10911 .remove = lpfc_pci_remove_one,
dfda822c 10912 .shutdown = lpfc_pci_remove_one,
3a55b532 10913 .suspend = lpfc_pci_suspend_one,
3772a991 10914 .resume = lpfc_pci_resume_one,
2e0fef85 10915 .err_handler = &lpfc_err_handler,
dea3101e
JB
10916};
10917
3ef6d24c 10918static const struct file_operations lpfc_mgmt_fop = {
858feacd 10919 .owner = THIS_MODULE,
3ef6d24c
JS
10920};
10921
10922static struct miscdevice lpfc_mgmt_dev = {
10923 .minor = MISC_DYNAMIC_MINOR,
10924 .name = "lpfcmgmt",
10925 .fops = &lpfc_mgmt_fop,
10926};
10927
e59058c4 10928/**
3621a710 10929 * lpfc_init - lpfc module initialization routine
e59058c4
JS
10930 *
10931 * This routine is to be invoked when the lpfc module is loaded into the
10932 * kernel. The special kernel macro module_init() is used to indicate the
10933 * role of this routine to the kernel as lpfc module entry point.
10934 *
10935 * Return codes
10936 * 0 - successful
10937 * -ENOMEM - FC attach transport failed
10938 * all others - failed
10939 */
dea3101e
JB
10940static int __init
10941lpfc_init(void)
10942{
7bb03bbf 10943 int cpu;
dea3101e
JB
10944 int error = 0;
10945
10946 printk(LPFC_MODULE_DESC "\n");
c44ce173 10947 printk(LPFC_COPYRIGHT "\n");
dea3101e 10948
3ef6d24c
JS
10949 error = misc_register(&lpfc_mgmt_dev);
10950 if (error)
10951 printk(KERN_ERR "Could not register lpfcmgmt device, "
10952 "misc_register returned with status %d", error);
10953
7ee5d43e
JS
10954 if (lpfc_enable_npiv) {
10955 lpfc_transport_functions.vport_create = lpfc_vport_create;
10956 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10957 }
dea3101e
JB
10958 lpfc_transport_template =
10959 fc_attach_transport(&lpfc_transport_functions);
7ee5d43e 10960 if (lpfc_transport_template == NULL)
dea3101e 10961 return -ENOMEM;
7ee5d43e 10962 if (lpfc_enable_npiv) {
7ee5d43e 10963 lpfc_vport_transport_template =
98c9ea5c
JS
10964 fc_attach_transport(&lpfc_vport_transport_functions);
10965 if (lpfc_vport_transport_template == NULL) {
10966 fc_release_transport(lpfc_transport_template);
7ee5d43e 10967 return -ENOMEM;
98c9ea5c 10968 }
7ee5d43e 10969 }
7bb03bbf
JS
10970
10971 /* Initialize in case vector mapping is needed */
10972 for (cpu = 0; cpu < LPFC_MAX_CPU; cpu++)
10973 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
10974
dea3101e 10975 error = pci_register_driver(&lpfc_driver);
92d7f7b0 10976 if (error) {
dea3101e 10977 fc_release_transport(lpfc_transport_template);
d7c255b2
JS
10978 if (lpfc_enable_npiv)
10979 fc_release_transport(lpfc_vport_transport_template);
92d7f7b0 10980 }
dea3101e
JB
10981
10982 return error;
10983}
10984
e59058c4 10985/**
3621a710 10986 * lpfc_exit - lpfc module removal routine
e59058c4
JS
10987 *
10988 * This routine is invoked when the lpfc module is removed from the kernel.
10989 * The special kernel macro module_exit() is used to indicate the role of
10990 * this routine to the kernel as lpfc module exit point.
10991 */
dea3101e
JB
10992static void __exit
10993lpfc_exit(void)
10994{
3ef6d24c 10995 misc_deregister(&lpfc_mgmt_dev);
dea3101e
JB
10996 pci_unregister_driver(&lpfc_driver);
10997 fc_release_transport(lpfc_transport_template);
7ee5d43e
JS
10998 if (lpfc_enable_npiv)
10999 fc_release_transport(lpfc_vport_transport_template);
81301a9b 11000 if (_dump_buf_data) {
6a9c52cf
JS
11001 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11002 "_dump_buf_data at 0x%p\n",
81301a9b
JS
11003 (1L << _dump_buf_data_order), _dump_buf_data);
11004 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11005 }
11006
11007 if (_dump_buf_dif) {
6a9c52cf
JS
11008 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11009 "_dump_buf_dif at 0x%p\n",
81301a9b
JS
11010 (1L << _dump_buf_dif_order), _dump_buf_dif);
11011 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11012 }
dea3101e
JB
11013}
11014
11015module_init(lpfc_init);
11016module_exit(lpfc_exit);
11017MODULE_LICENSE("GPL");
11018MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11019MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11020MODULE_VERSION("0:" LPFC_DRIVER_VERSION);