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