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