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