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