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