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