[SCSI] lpfc 8.3.12: T10-PI/DIF changes
[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;
e2aed29f
JS
2603 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
2604 phba->menlo_flag |= HBA_MENLO_SUPPORT;
2605 /* check for menlo minimum sg count */
2606 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT) {
2607 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
2608 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2609 }
2610 }
47a8617c 2611
d139b9bd 2612 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2e0fef85
JS
2613 if (error)
2614 goto out_put_shost;
47a8617c 2615
549e55cd 2616 spin_lock_irq(&phba->hbalock);
2e0fef85 2617 list_add_tail(&vport->listentry, &phba->port_list);
549e55cd 2618 spin_unlock_irq(&phba->hbalock);
2e0fef85 2619 return vport;
47a8617c 2620
2e0fef85
JS
2621out_put_shost:
2622 scsi_host_put(shost);
2623out:
2624 return NULL;
47a8617c
JS
2625}
2626
e59058c4 2627/**
3621a710 2628 * destroy_port - destroy an FC port
e59058c4
JS
2629 * @vport: pointer to an lpfc virtual N_Port data structure.
2630 *
2631 * This routine destroys a FC port from the upper layer protocol. All the
2632 * resources associated with the port are released.
2633 **/
2e0fef85
JS
2634void
2635destroy_port(struct lpfc_vport *vport)
47a8617c 2636{
92d7f7b0
JS
2637 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2638 struct lpfc_hba *phba = vport->phba;
47a8617c 2639
858c9f6c 2640 lpfc_debugfs_terminate(vport);
92d7f7b0
JS
2641 fc_remove_host(shost);
2642 scsi_remove_host(shost);
47a8617c 2643
92d7f7b0
JS
2644 spin_lock_irq(&phba->hbalock);
2645 list_del_init(&vport->listentry);
2646 spin_unlock_irq(&phba->hbalock);
47a8617c 2647
92d7f7b0 2648 lpfc_cleanup(vport);
47a8617c 2649 return;
47a8617c
JS
2650}
2651
e59058c4 2652/**
3621a710 2653 * lpfc_get_instance - Get a unique integer ID
e59058c4
JS
2654 *
2655 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2656 * uses the kernel idr facility to perform the task.
2657 *
2658 * Return codes:
2659 * instance - a unique integer ID allocated as the new instance.
2660 * -1 - lpfc get instance failed.
2661 **/
92d7f7b0
JS
2662int
2663lpfc_get_instance(void)
2664{
2665 int instance = 0;
47a8617c 2666
92d7f7b0
JS
2667 /* Assign an unused number */
2668 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2669 return -1;
2670 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2671 return -1;
2672 return instance;
47a8617c
JS
2673}
2674
e59058c4 2675/**
3621a710 2676 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
e59058c4
JS
2677 * @shost: pointer to SCSI host data structure.
2678 * @time: elapsed time of the scan in jiffies.
2679 *
2680 * This routine is called by the SCSI layer with a SCSI host to determine
2681 * whether the scan host is finished.
2682 *
2683 * Note: there is no scan_start function as adapter initialization will have
2684 * asynchronously kicked off the link initialization.
2685 *
2686 * Return codes
2687 * 0 - SCSI host scan is not over yet.
2688 * 1 - SCSI host scan is over.
2689 **/
47a8617c
JS
2690int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2691{
2e0fef85
JS
2692 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2693 struct lpfc_hba *phba = vport->phba;
858c9f6c 2694 int stat = 0;
47a8617c 2695
858c9f6c
JS
2696 spin_lock_irq(shost->host_lock);
2697
51ef4c26 2698 if (vport->load_flag & FC_UNLOADING) {
858c9f6c
JS
2699 stat = 1;
2700 goto finished;
2701 }
2e0fef85
JS
2702 if (time >= 30 * HZ) {
2703 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
2704 "0461 Scanning longer than 30 "
2705 "seconds. Continuing initialization\n");
858c9f6c 2706 stat = 1;
47a8617c 2707 goto finished;
2e0fef85
JS
2708 }
2709 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2710 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
2711 "0465 Link down longer than 15 "
2712 "seconds. Continuing initialization\n");
858c9f6c 2713 stat = 1;
47a8617c 2714 goto finished;
2e0fef85 2715 }
47a8617c 2716
2e0fef85 2717 if (vport->port_state != LPFC_VPORT_READY)
858c9f6c 2718 goto finished;
2e0fef85 2719 if (vport->num_disc_nodes || vport->fc_prli_sent)
858c9f6c 2720 goto finished;
2e0fef85 2721 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
858c9f6c 2722 goto finished;
2e0fef85 2723 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
858c9f6c
JS
2724 goto finished;
2725
2726 stat = 1;
47a8617c
JS
2727
2728finished:
858c9f6c
JS
2729 spin_unlock_irq(shost->host_lock);
2730 return stat;
92d7f7b0 2731}
47a8617c 2732
e59058c4 2733/**
3621a710 2734 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
e59058c4
JS
2735 * @shost: pointer to SCSI host data structure.
2736 *
2737 * This routine initializes a given SCSI host attributes on a FC port. The
2738 * SCSI host can be either on top of a physical port or a virtual port.
2739 **/
92d7f7b0
JS
2740void lpfc_host_attrib_init(struct Scsi_Host *shost)
2741{
2742 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2743 struct lpfc_hba *phba = vport->phba;
47a8617c 2744 /*
2e0fef85 2745 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
47a8617c
JS
2746 */
2747
2e0fef85
JS
2748 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2749 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
47a8617c
JS
2750 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2751
2752 memset(fc_host_supported_fc4s(shost), 0,
2e0fef85 2753 sizeof(fc_host_supported_fc4s(shost)));
47a8617c
JS
2754 fc_host_supported_fc4s(shost)[2] = 1;
2755 fc_host_supported_fc4s(shost)[7] = 1;
2756
92d7f7b0
JS
2757 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2758 sizeof fc_host_symbolic_name(shost));
47a8617c
JS
2759
2760 fc_host_supported_speeds(shost) = 0;
2761 if (phba->lmt & LMT_10Gb)
2762 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
a8adb832
JS
2763 if (phba->lmt & LMT_8Gb)
2764 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
47a8617c
JS
2765 if (phba->lmt & LMT_4Gb)
2766 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2767 if (phba->lmt & LMT_2Gb)
2768 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2769 if (phba->lmt & LMT_1Gb)
2770 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2771
2772 fc_host_maxframe_size(shost) =
2e0fef85
JS
2773 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2774 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
47a8617c
JS
2775
2776 /* This value is also unchanging */
2777 memset(fc_host_active_fc4s(shost), 0,
2e0fef85 2778 sizeof(fc_host_active_fc4s(shost)));
47a8617c
JS
2779 fc_host_active_fc4s(shost)[2] = 1;
2780 fc_host_active_fc4s(shost)[7] = 1;
2781
92d7f7b0 2782 fc_host_max_npiv_vports(shost) = phba->max_vpi;
47a8617c 2783 spin_lock_irq(shost->host_lock);
51ef4c26 2784 vport->load_flag &= ~FC_LOADING;
47a8617c 2785 spin_unlock_irq(shost->host_lock);
47a8617c 2786}
dea3101e 2787
e59058c4 2788/**
da0436e9 2789 * lpfc_stop_port_s3 - Stop SLI3 device port
e59058c4
JS
2790 * @phba: pointer to lpfc hba data structure.
2791 *
da0436e9
JS
2792 * This routine is invoked to stop an SLI3 device port, it stops the device
2793 * from generating interrupts and stops the device driver's timers for the
2794 * device.
e59058c4 2795 **/
da0436e9
JS
2796static void
2797lpfc_stop_port_s3(struct lpfc_hba *phba)
db2378e0 2798{
da0436e9
JS
2799 /* Clear all interrupt enable conditions */
2800 writel(0, phba->HCregaddr);
2801 readl(phba->HCregaddr); /* flush */
2802 /* Clear all pending interrupts */
2803 writel(0xffffffff, phba->HAregaddr);
2804 readl(phba->HAregaddr); /* flush */
db2378e0 2805
da0436e9
JS
2806 /* Reset some HBA SLI setup states */
2807 lpfc_stop_hba_timers(phba);
2808 phba->pport->work_port_events = 0;
2809}
db2378e0 2810
da0436e9
JS
2811/**
2812 * lpfc_stop_port_s4 - Stop SLI4 device port
2813 * @phba: pointer to lpfc hba data structure.
2814 *
2815 * This routine is invoked to stop an SLI4 device port, it stops the device
2816 * from generating interrupts and stops the device driver's timers for the
2817 * device.
2818 **/
2819static void
2820lpfc_stop_port_s4(struct lpfc_hba *phba)
2821{
2822 /* Reset some HBA SLI4 setup states */
2823 lpfc_stop_hba_timers(phba);
2824 phba->pport->work_port_events = 0;
2825 phba->sli4_hba.intr_enable = 0;
da0436e9 2826}
9399627f 2827
da0436e9
JS
2828/**
2829 * lpfc_stop_port - Wrapper function for stopping hba port
2830 * @phba: Pointer to HBA context object.
2831 *
2832 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2833 * the API jump table function pointer from the lpfc_hba struct.
2834 **/
2835void
2836lpfc_stop_port(struct lpfc_hba *phba)
2837{
2838 phba->lpfc_stop_port(phba);
2839}
db2378e0 2840
da0436e9
JS
2841/**
2842 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2843 * @phba: pointer to lpfc hba data structure.
2844 *
2845 * This routine is invoked to remove the driver default fcf record from
2846 * the port. This routine currently acts on FCF Index 0.
2847 *
2848 **/
2849void
2850lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2851{
2852 int rc = 0;
2853 LPFC_MBOXQ_t *mboxq;
2854 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2855 uint32_t mbox_tmo, req_len;
2856 uint32_t shdr_status, shdr_add_status;
9399627f 2857
da0436e9
JS
2858 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2859 if (!mboxq) {
2860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2861 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2862 return;
db2378e0 2863 }
9399627f 2864
da0436e9
JS
2865 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2866 sizeof(struct lpfc_sli4_cfg_mhdr);
2867 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2868 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2869 req_len, LPFC_SLI4_MBX_EMBED);
9399627f 2870 /*
da0436e9
JS
2871 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2872 * supports multiple FCF indices.
9399627f 2873 */
da0436e9
JS
2874 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2875 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2876 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
ecfd03c6 2877 phba->fcf.current_rec.fcf_indx);
9399627f 2878
da0436e9
JS
2879 if (!phba->sli4_hba.intr_enable)
2880 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2881 else {
2882 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2883 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9399627f 2884 }
da0436e9
JS
2885 /* The IOCTL status is embedded in the mailbox subheader. */
2886 shdr_status = bf_get(lpfc_mbox_hdr_status,
2887 &del_fcf_record->header.cfg_shdr.response);
2888 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2889 &del_fcf_record->header.cfg_shdr.response);
2890 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2892 "2516 DEL FCF of default FCF Index failed "
2893 "mbx status x%x, status x%x add_status x%x\n",
2894 rc, shdr_status, shdr_add_status);
9399627f 2895 }
da0436e9
JS
2896 if (rc != MBX_TIMEOUT)
2897 mempool_free(mboxq, phba->mbox_mem_pool);
db2378e0
JS
2898}
2899
ecfd03c6
JS
2900/**
2901 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2902 * @phba: Pointer to hba for which this call is being executed.
2903 *
2904 * This routine starts the timer waiting for the FCF rediscovery to complete.
2905 **/
2906void
2907lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2908{
2909 unsigned long fcf_redisc_wait_tmo =
2910 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2911 /* Start fcf rediscovery wait period timer */
2912 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2913 spin_lock_irq(&phba->hbalock);
2914 /* Allow action to new fcf asynchronous event */
2915 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2916 /* Mark the FCF rediscovery pending state */
2917 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2918 spin_unlock_irq(&phba->hbalock);
2919}
2920
2921/**
2922 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2923 * @ptr: Map to lpfc_hba data structure pointer.
2924 *
2925 * This routine is invoked when waiting for FCF table rediscover has been
2926 * timed out. If new FCF record(s) has (have) been discovered during the
2927 * wait period, a new FCF event shall be added to the FCOE async event
2928 * list, and then worker thread shall be waked up for processing from the
2929 * worker thread context.
2930 **/
2931void
2932lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2933{
2934 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2935
2936 /* Don't send FCF rediscovery event if timer cancelled */
2937 spin_lock_irq(&phba->hbalock);
2938 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2939 spin_unlock_irq(&phba->hbalock);
2940 return;
2941 }
2942 /* Clear FCF rediscovery timer pending flag */
2943 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2944 /* FCF rediscovery event to worker thread */
2945 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2946 spin_unlock_irq(&phba->hbalock);
0c9ab6f5
JS
2947 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2948 "2776 FCF rediscover wait timer expired, post "
2949 "a worker thread event for FCF table scan\n");
ecfd03c6
JS
2950 /* wake up worker thread */
2951 lpfc_worker_wake_up(phba);
2952}
2953
6669f9bb
JS
2954/**
2955 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2956 * @phba: pointer to lpfc hba data structure.
2957 *
2958 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2959 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2960 * was successful and the firmware supports FCoE. Any other return indicates
2961 * a error. It is assumed that this function will be called before interrupts
2962 * are enabled.
2963 **/
2964static int
2965lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2966{
2967 int rc = 0;
2968 LPFC_MBOXQ_t *mboxq;
2969 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2970 uint32_t length;
2971 uint32_t shdr_status, shdr_add_status;
2972
2973 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2974 if (!mboxq) {
2975 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2976 "2621 Failed to allocate mbox for "
2977 "query firmware config cmd\n");
2978 return -ENOMEM;
2979 }
2980 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2981 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2982 sizeof(struct lpfc_sli4_cfg_mhdr));
2983 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2984 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2985 length, LPFC_SLI4_MBX_EMBED);
2986 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2987 /* The IOCTL status is embedded in the mailbox subheader. */
2988 shdr_status = bf_get(lpfc_mbox_hdr_status,
2989 &query_fw_cfg->header.cfg_shdr.response);
2990 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2991 &query_fw_cfg->header.cfg_shdr.response);
2992 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2994 "2622 Query Firmware Config failed "
2995 "mbx status x%x, status x%x add_status x%x\n",
2996 rc, shdr_status, shdr_add_status);
2997 return -EINVAL;
2998 }
2999 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
3000 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3001 "2623 FCoE Function not supported by firmware. "
3002 "Function mode = %08x\n",
3003 query_fw_cfg->function_mode);
3004 return -EINVAL;
3005 }
3006 if (rc != MBX_TIMEOUT)
3007 mempool_free(mboxq, phba->mbox_mem_pool);
3008 return 0;
3009}
3010
e59058c4 3011/**
da0436e9 3012 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
e59058c4 3013 * @phba: pointer to lpfc hba data structure.
da0436e9 3014 * @acqe_link: pointer to the async link completion queue entry.
e59058c4 3015 *
da0436e9
JS
3016 * This routine is to parse the SLI4 link-attention link fault code and
3017 * translate it into the base driver's read link attention mailbox command
3018 * status.
3019 *
3020 * Return: Link-attention status in terms of base driver's coding.
e59058c4 3021 **/
da0436e9
JS
3022static uint16_t
3023lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3024 struct lpfc_acqe_link *acqe_link)
db2378e0 3025{
da0436e9 3026 uint16_t latt_fault;
9399627f 3027
da0436e9
JS
3028 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3029 case LPFC_ASYNC_LINK_FAULT_NONE:
3030 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3031 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3032 latt_fault = 0;
3033 break;
3034 default:
3035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3036 "0398 Invalid link fault code: x%x\n",
3037 bf_get(lpfc_acqe_link_fault, acqe_link));
3038 latt_fault = MBXERR_ERROR;
3039 break;
3040 }
3041 return latt_fault;
db2378e0
JS
3042}
3043
5b75da2f 3044/**
da0436e9 3045 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
5b75da2f 3046 * @phba: pointer to lpfc hba data structure.
da0436e9 3047 * @acqe_link: pointer to the async link completion queue entry.
5b75da2f 3048 *
da0436e9
JS
3049 * This routine is to parse the SLI4 link attention type and translate it
3050 * into the base driver's link attention type coding.
5b75da2f 3051 *
da0436e9
JS
3052 * Return: Link attention type in terms of base driver's coding.
3053 **/
3054static uint8_t
3055lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3056 struct lpfc_acqe_link *acqe_link)
5b75da2f 3057{
da0436e9 3058 uint8_t att_type;
5b75da2f 3059
da0436e9
JS
3060 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3061 case LPFC_ASYNC_LINK_STATUS_DOWN:
3062 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3063 att_type = AT_LINK_DOWN;
3064 break;
3065 case LPFC_ASYNC_LINK_STATUS_UP:
3066 /* Ignore physical link up events - wait for logical link up */
3067 att_type = AT_RESERVED;
3068 break;
3069 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3070 att_type = AT_LINK_UP;
3071 break;
3072 default:
3073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3074 "0399 Invalid link attention type: x%x\n",
3075 bf_get(lpfc_acqe_link_status, acqe_link));
3076 att_type = AT_RESERVED;
3077 break;
5b75da2f 3078 }
da0436e9 3079 return att_type;
5b75da2f
JS
3080}
3081
3082/**
da0436e9 3083 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
5b75da2f 3084 * @phba: pointer to lpfc hba data structure.
da0436e9 3085 * @acqe_link: pointer to the async link completion queue entry.
5b75da2f 3086 *
da0436e9
JS
3087 * This routine is to parse the SLI4 link-attention link speed and translate
3088 * it into the base driver's link-attention link speed coding.
3089 *
3090 * Return: Link-attention link speed in terms of base driver's coding.
3091 **/
3092static uint8_t
3093lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3094 struct lpfc_acqe_link *acqe_link)
5b75da2f 3095{
da0436e9
JS
3096 uint8_t link_speed;
3097
3098 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3099 case LPFC_ASYNC_LINK_SPEED_ZERO:
3100 link_speed = LA_UNKNW_LINK;
3101 break;
3102 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3103 link_speed = LA_UNKNW_LINK;
3104 break;
3105 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3106 link_speed = LA_UNKNW_LINK;
3107 break;
3108 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3109 link_speed = LA_1GHZ_LINK;
3110 break;
3111 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3112 link_speed = LA_10GHZ_LINK;
3113 break;
3114 default:
3115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3116 "0483 Invalid link-attention link speed: x%x\n",
3117 bf_get(lpfc_acqe_link_speed, acqe_link));
3118 link_speed = LA_UNKNW_LINK;
3119 break;
3120 }
3121 return link_speed;
3122}
3123
3124/**
3125 * lpfc_sli4_async_link_evt - Process the asynchronous link event
3126 * @phba: pointer to lpfc hba data structure.
3127 * @acqe_link: pointer to the async link completion queue entry.
3128 *
3129 * This routine is to handle the SLI4 asynchronous link event.
3130 **/
3131static void
3132lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3133 struct lpfc_acqe_link *acqe_link)
3134{
3135 struct lpfc_dmabuf *mp;
3136 LPFC_MBOXQ_t *pmb;
3137 MAILBOX_t *mb;
3138 READ_LA_VAR *la;
3139 uint8_t att_type;
3140
3141 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3142 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3143 return;
32b9793f 3144 phba->fcoe_eventtag = acqe_link->event_tag;
da0436e9
JS
3145 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3146 if (!pmb) {
3147 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3148 "0395 The mboxq allocation failed\n");
3149 return;
3150 }
3151 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3152 if (!mp) {
3153 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3154 "0396 The lpfc_dmabuf allocation failed\n");
3155 goto out_free_pmb;
3156 }
3157 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3158 if (!mp->virt) {
3159 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3160 "0397 The mbuf allocation failed\n");
3161 goto out_free_dmabuf;
3162 }
3163
3164 /* Cleanup any outstanding ELS commands */
3165 lpfc_els_flush_all_cmd(phba);
3166
3167 /* Block ELS IOCBs until we have done process link event */
3168 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3169
3170 /* Update link event statistics */
3171 phba->sli.slistat.link_event++;
3172
3173 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3174 lpfc_read_la(phba, pmb, mp);
3175 pmb->vport = phba->pport;
3176
3177 /* Parse and translate status field */
3178 mb = &pmb->u.mb;
3179 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3180
3181 /* Parse and translate link attention fields */
3182 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3183 la->eventTag = acqe_link->event_tag;
3184 la->attType = att_type;
3185 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3186
3187 /* Fake the the following irrelvant fields */
3188 la->topology = TOPOLOGY_PT_PT;
3189 la->granted_AL_PA = 0;
3190 la->il = 0;
3191 la->pb = 0;
3192 la->fa = 0;
3193 la->mm = 0;
3194
3195 /* Keep the link status for extra SLI4 state machine reference */
3196 phba->sli4_hba.link_state.speed =
3197 bf_get(lpfc_acqe_link_speed, acqe_link);
3198 phba->sli4_hba.link_state.duplex =
3199 bf_get(lpfc_acqe_link_duplex, acqe_link);
3200 phba->sli4_hba.link_state.status =
3201 bf_get(lpfc_acqe_link_status, acqe_link);
3202 phba->sli4_hba.link_state.physical =
3203 bf_get(lpfc_acqe_link_physical, acqe_link);
3204 phba->sli4_hba.link_state.fault =
3205 bf_get(lpfc_acqe_link_fault, acqe_link);
65467b6b
JS
3206 phba->sli4_hba.link_state.logical_speed =
3207 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
da0436e9
JS
3208
3209 /* Invoke the lpfc_handle_latt mailbox command callback function */
3210 lpfc_mbx_cmpl_read_la(phba, pmb);
3211
5b75da2f 3212 return;
da0436e9
JS
3213
3214out_free_dmabuf:
3215 kfree(mp);
3216out_free_pmb:
3217 mempool_free(pmb, phba->mbox_mem_pool);
3218}
3219
fc2b989b
JS
3220/**
3221 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3222 * @vport: pointer to vport data structure.
3223 *
3224 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3225 * response to a CVL event.
3226 *
3227 * Return the pointer to the ndlp with the vport if successful, otherwise
3228 * return NULL.
3229 **/
3230static struct lpfc_nodelist *
3231lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3232{
3233 struct lpfc_nodelist *ndlp;
3234 struct Scsi_Host *shost;
3235 struct lpfc_hba *phba;
3236
3237 if (!vport)
3238 return NULL;
3239 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3240 if (!ndlp)
3241 return NULL;
3242 phba = vport->phba;
3243 if (!phba)
3244 return NULL;
3245 if (phba->pport->port_state <= LPFC_FLOGI)
3246 return NULL;
3247 /* If virtual link is not yet instantiated ignore CVL */
3248 if (vport->port_state <= LPFC_FDISC)
3249 return NULL;
3250 shost = lpfc_shost_from_vport(vport);
3251 if (!shost)
3252 return NULL;
3253 lpfc_linkdown_port(vport);
3254 lpfc_cleanup_pending_mbox(vport);
3255 spin_lock_irq(shost->host_lock);
3256 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3257 spin_unlock_irq(shost->host_lock);
3258
3259 return ndlp;
3260}
3261
3262/**
3263 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3264 * @vport: pointer to lpfc hba data structure.
3265 *
3266 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3267 * response to a FCF dead event.
3268 **/
3269static void
3270lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3271{
3272 struct lpfc_vport **vports;
3273 int i;
3274
3275 vports = lpfc_create_vport_work_array(phba);
3276 if (vports)
3277 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3278 lpfc_sli4_perform_vport_cvl(vports[i]);
3279 lpfc_destroy_vport_work_array(phba, vports);
3280}
3281
da0436e9
JS
3282/**
3283 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3284 * @phba: pointer to lpfc hba data structure.
3285 * @acqe_link: pointer to the async fcoe completion queue entry.
3286 *
3287 * This routine is to handle the SLI4 asynchronous fcoe event.
3288 **/
3289static void
3290lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3291 struct lpfc_acqe_fcoe *acqe_fcoe)
3292{
3293 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3294 int rc;
6669f9bb
JS
3295 struct lpfc_vport *vport;
3296 struct lpfc_nodelist *ndlp;
3297 struct Scsi_Host *shost;
695a814e
JS
3298 int active_vlink_present;
3299 struct lpfc_vport **vports;
3300 int i;
da0436e9 3301
4d9ab994 3302 phba->fc_eventTag = acqe_fcoe->event_tag;
32b9793f 3303 phba->fcoe_eventtag = acqe_fcoe->event_tag;
da0436e9
JS
3304 switch (event_type) {
3305 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
ecfd03c6 3306 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
999d813f
JS
3307 if (event_type == LPFC_FCOE_EVENT_TYPE_NEW_FCF)
3308 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3309 LOG_DISCOVERY,
3310 "2546 New FCF found event: "
3311 "evt_tag:x%x, fcf_index:x%x\n",
3312 acqe_fcoe->event_tag,
3313 acqe_fcoe->index);
3314 else
3315 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3316 LOG_DISCOVERY,
3317 "2788 FCF parameter modified event: "
3318 "evt_tag:x%x, fcf_index:x%x\n",
3319 acqe_fcoe->event_tag,
3320 acqe_fcoe->index);
da0436e9 3321 spin_lock_irq(&phba->hbalock);
ecfd03c6
JS
3322 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3323 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3324 /*
3325 * If the current FCF is in discovered state or
3326 * FCF discovery is in progress, do nothing.
3327 */
3328 spin_unlock_irq(&phba->hbalock);
3329 break;
3330 }
0c9ab6f5 3331
ecfd03c6
JS
3332 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3333 /*
3334 * If fast FCF failover rescan event is pending,
3335 * do nothing.
3336 */
da0436e9
JS
3337 spin_unlock_irq(&phba->hbalock);
3338 break;
3339 }
3340 spin_unlock_irq(&phba->hbalock);
3341
0c9ab6f5
JS
3342 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
3343 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
3344 /*
3345 * During period of FCF discovery, read the FCF
3346 * table record indexed by the event to update
3347 * FCF round robin failover eligible FCF bmask.
3348 */
3349 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3350 LOG_DISCOVERY,
3351 "2779 Read new FCF record with "
3352 "fcf_index:x%x for updating FCF "
3353 "round robin failover bmask\n",
3354 acqe_fcoe->index);
3355 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fcoe->index);
3356 }
3357
3358 /* Otherwise, scan the entire FCF table and re-discover SAN */
3359 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3360 "2770 Start FCF table scan due to new FCF "
3361 "event: evt_tag:x%x, fcf_index:x%x\n",
3362 acqe_fcoe->event_tag, acqe_fcoe->index);
3363 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3364 LPFC_FCOE_FCF_GET_FIRST);
da0436e9 3365 if (rc)
0c9ab6f5
JS
3366 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3367 "2547 Issue FCF scan read FCF mailbox "
3368 "command failed 0x%x\n", rc);
da0436e9
JS
3369 break;
3370
3371 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3372 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e4e74273 3373 "2548 FCF Table full count 0x%x tag 0x%x\n",
da0436e9
JS
3374 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3375 acqe_fcoe->event_tag);
3376 break;
3377
3378 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
0c9ab6f5 3379 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
6a9c52cf 3380 "2549 FCF disconnected from network index 0x%x"
6669f9bb 3381 " tag 0x%x\n", acqe_fcoe->index,
da0436e9
JS
3382 acqe_fcoe->event_tag);
3383 /* If the event is not for currently used fcf do nothing */
ecfd03c6 3384 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
da0436e9 3385 break;
fc2b989b
JS
3386 /* We request port to rediscover the entire FCF table for
3387 * a fast recovery from case that the current FCF record
0c9ab6f5
JS
3388 * is no longer valid if we are not in the middle of FCF
3389 * failover process already.
da0436e9 3390 */
fc2b989b 3391 spin_lock_irq(&phba->hbalock);
0c9ab6f5 3392 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
fc2b989b 3393 spin_unlock_irq(&phba->hbalock);
0c9ab6f5
JS
3394 /* Update FLOGI FCF failover eligible FCF bmask */
3395 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fcoe->index);
fc2b989b
JS
3396 break;
3397 }
3398 /* Mark the fast failover process in progress */
0c9ab6f5 3399 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
fc2b989b 3400 spin_unlock_irq(&phba->hbalock);
0c9ab6f5
JS
3401 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3402 "2771 Start FCF fast failover process due to "
3403 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3404 "\n", acqe_fcoe->event_tag, acqe_fcoe->index);
fc2b989b
JS
3405 rc = lpfc_sli4_redisc_fcf_table(phba);
3406 if (rc) {
0c9ab6f5
JS
3407 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3408 LOG_DISCOVERY,
3409 "2772 Issue FCF rediscover mabilbox "
3410 "command failed, fail through to FCF "
3411 "dead event\n");
fc2b989b 3412 spin_lock_irq(&phba->hbalock);
0c9ab6f5 3413 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
3414 spin_unlock_irq(&phba->hbalock);
3415 /*
3416 * Last resort will fail over by treating this
3417 * as a link down to FCF registration.
3418 */
3419 lpfc_sli4_fcf_dead_failthrough(phba);
3420 } else
3421 /* Handling fast FCF failover to a DEAD FCF event
3422 * is considered equalivant to receiving CVL to all
3423 * vports.
3424 */
3425 lpfc_sli4_perform_all_vport_cvl(phba);
da0436e9 3426 break;
6669f9bb 3427 case LPFC_FCOE_EVENT_TYPE_CVL:
0c9ab6f5 3428 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
6669f9bb
JS
3429 "2718 Clear Virtual Link Received for VPI 0x%x"
3430 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3431 vport = lpfc_find_vport_by_vpid(phba,
c868595d 3432 acqe_fcoe->index - phba->vpi_base);
fc2b989b 3433 ndlp = lpfc_sli4_perform_vport_cvl(vport);
6669f9bb
JS
3434 if (!ndlp)
3435 break;
695a814e
JS
3436 active_vlink_present = 0;
3437
3438 vports = lpfc_create_vport_work_array(phba);
3439 if (vports) {
3440 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3441 i++) {
3442 if ((!(vports[i]->fc_flag &
3443 FC_VPORT_CVL_RCVD)) &&
3444 (vports[i]->port_state > LPFC_FDISC)) {
3445 active_vlink_present = 1;
3446 break;
3447 }
3448 }
3449 lpfc_destroy_vport_work_array(phba, vports);
3450 }
3451
3452 if (active_vlink_present) {
3453 /*
3454 * If there are other active VLinks present,
3455 * re-instantiate the Vlink using FDISC.
3456 */
6669f9bb 3457 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
fc2b989b 3458 shost = lpfc_shost_from_vport(vport);
6669f9bb
JS
3459 spin_lock_irq(shost->host_lock);
3460 ndlp->nlp_flag |= NLP_DELAY_TMO;
3461 spin_unlock_irq(shost->host_lock);
695a814e
JS
3462 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3463 vport->port_state = LPFC_FDISC;
3464 } else {
ecfd03c6
JS
3465 /*
3466 * Otherwise, we request port to rediscover
3467 * the entire FCF table for a fast recovery
3468 * from possible case that the current FCF
0c9ab6f5
JS
3469 * is no longer valid if we are not already
3470 * in the FCF failover process.
ecfd03c6 3471 */
fc2b989b 3472 spin_lock_irq(&phba->hbalock);
0c9ab6f5 3473 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
fc2b989b
JS
3474 spin_unlock_irq(&phba->hbalock);
3475 break;
3476 }
3477 /* Mark the fast failover process in progress */
0c9ab6f5 3478 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
fc2b989b 3479 spin_unlock_irq(&phba->hbalock);
0c9ab6f5
JS
3480 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3481 LOG_DISCOVERY,
3482 "2773 Start FCF fast failover due "
3483 "to CVL event: evt_tag:x%x\n",
3484 acqe_fcoe->event_tag);
ecfd03c6 3485 rc = lpfc_sli4_redisc_fcf_table(phba);
fc2b989b 3486 if (rc) {
0c9ab6f5
JS
3487 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3488 LOG_DISCOVERY,
3489 "2774 Issue FCF rediscover "
3490 "mabilbox command failed, "
3491 "through to CVL event\n");
fc2b989b 3492 spin_lock_irq(&phba->hbalock);
0c9ab6f5 3493 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b 3494 spin_unlock_irq(&phba->hbalock);
ecfd03c6
JS
3495 /*
3496 * Last resort will be re-try on the
3497 * the current registered FCF entry.
3498 */
3499 lpfc_retry_pport_discovery(phba);
fc2b989b 3500 }
6669f9bb
JS
3501 }
3502 break;
da0436e9
JS
3503 default:
3504 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3505 "0288 Unknown FCoE event type 0x%x event tag "
3506 "0x%x\n", event_type, acqe_fcoe->event_tag);
3507 break;
3508 }
3509}
3510
3511/**
3512 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3513 * @phba: pointer to lpfc hba data structure.
3514 * @acqe_link: pointer to the async dcbx completion queue entry.
3515 *
3516 * This routine is to handle the SLI4 asynchronous dcbx event.
3517 **/
3518static void
3519lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3520 struct lpfc_acqe_dcbx *acqe_dcbx)
3521{
4d9ab994 3522 phba->fc_eventTag = acqe_dcbx->event_tag;
da0436e9
JS
3523 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3524 "0290 The SLI4 DCBX asynchronous event is not "
3525 "handled yet\n");
3526}
3527
b19a061a
JS
3528/**
3529 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3530 * @phba: pointer to lpfc hba data structure.
3531 * @acqe_link: pointer to the async grp5 completion queue entry.
3532 *
3533 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3534 * is an asynchronous notified of a logical link speed change. The Port
3535 * reports the logical link speed in units of 10Mbps.
3536 **/
3537static void
3538lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3539 struct lpfc_acqe_grp5 *acqe_grp5)
3540{
3541 uint16_t prev_ll_spd;
3542
3543 phba->fc_eventTag = acqe_grp5->event_tag;
3544 phba->fcoe_eventtag = acqe_grp5->event_tag;
3545 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3546 phba->sli4_hba.link_state.logical_speed =
3547 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3548 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3549 "2789 GRP5 Async Event: Updating logical link speed "
3550 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3551 (phba->sli4_hba.link_state.logical_speed*10));
3552}
3553
da0436e9
JS
3554/**
3555 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3556 * @phba: pointer to lpfc hba data structure.
3557 *
3558 * This routine is invoked by the worker thread to process all the pending
3559 * SLI4 asynchronous events.
3560 **/
3561void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3562{
3563 struct lpfc_cq_event *cq_event;
3564
3565 /* First, declare the async event has been handled */
3566 spin_lock_irq(&phba->hbalock);
3567 phba->hba_flag &= ~ASYNC_EVENT;
3568 spin_unlock_irq(&phba->hbalock);
3569 /* Now, handle all the async events */
3570 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3571 /* Get the first event from the head of the event queue */
3572 spin_lock_irq(&phba->hbalock);
3573 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3574 cq_event, struct lpfc_cq_event, list);
3575 spin_unlock_irq(&phba->hbalock);
3576 /* Process the asynchronous event */
3577 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3578 case LPFC_TRAILER_CODE_LINK:
3579 lpfc_sli4_async_link_evt(phba,
3580 &cq_event->cqe.acqe_link);
3581 break;
3582 case LPFC_TRAILER_CODE_FCOE:
3583 lpfc_sli4_async_fcoe_evt(phba,
3584 &cq_event->cqe.acqe_fcoe);
3585 break;
3586 case LPFC_TRAILER_CODE_DCBX:
3587 lpfc_sli4_async_dcbx_evt(phba,
3588 &cq_event->cqe.acqe_dcbx);
3589 break;
b19a061a
JS
3590 case LPFC_TRAILER_CODE_GRP5:
3591 lpfc_sli4_async_grp5_evt(phba,
3592 &cq_event->cqe.acqe_grp5);
3593 break;
da0436e9
JS
3594 default:
3595 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3596 "1804 Invalid asynchrous event code: "
3597 "x%x\n", bf_get(lpfc_trailer_code,
3598 &cq_event->cqe.mcqe_cmpl));
3599 break;
3600 }
3601 /* Free the completion event processed to the free pool */
3602 lpfc_sli4_cq_event_release(phba, cq_event);
3603 }
3604}
3605
ecfd03c6
JS
3606/**
3607 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3608 * @phba: pointer to lpfc hba data structure.
3609 *
3610 * This routine is invoked by the worker thread to process FCF table
3611 * rediscovery pending completion event.
3612 **/
3613void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3614{
3615 int rc;
3616
3617 spin_lock_irq(&phba->hbalock);
3618 /* Clear FCF rediscovery timeout event */
3619 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3620 /* Clear driver fast failover FCF record flag */
3621 phba->fcf.failover_rec.flag = 0;
3622 /* Set state for FCF fast failover */
3623 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3624 spin_unlock_irq(&phba->hbalock);
3625
3626 /* Scan FCF table from the first entry to re-discover SAN */
0c9ab6f5
JS
3627 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3628 "2777 Start FCF table scan after FCF "
3629 "rediscovery quiescent period over\n");
3630 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
ecfd03c6 3631 if (rc)
0c9ab6f5
JS
3632 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3633 "2747 Issue FCF scan read FCF mailbox "
3634 "command failed 0x%x\n", rc);
ecfd03c6
JS
3635}
3636
da0436e9
JS
3637/**
3638 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3639 * @phba: pointer to lpfc hba data structure.
3640 * @dev_grp: The HBA PCI-Device group number.
3641 *
3642 * This routine is invoked to set up the per HBA PCI-Device group function
3643 * API jump table entries.
3644 *
3645 * Return: 0 if success, otherwise -ENODEV
3646 **/
3647int
3648lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3649{
3650 int rc;
3651
3652 /* Set up lpfc PCI-device group */
3653 phba->pci_dev_grp = dev_grp;
3654
3655 /* The LPFC_PCI_DEV_OC uses SLI4 */
3656 if (dev_grp == LPFC_PCI_DEV_OC)
3657 phba->sli_rev = LPFC_SLI_REV4;
3658
3659 /* Set up device INIT API function jump table */
3660 rc = lpfc_init_api_table_setup(phba, dev_grp);
3661 if (rc)
3662 return -ENODEV;
3663 /* Set up SCSI API function jump table */
3664 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3665 if (rc)
3666 return -ENODEV;
3667 /* Set up SLI API function jump table */
3668 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3669 if (rc)
3670 return -ENODEV;
3671 /* Set up MBOX API function jump table */
3672 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3673 if (rc)
3674 return -ENODEV;
3675
3676 return 0;
5b75da2f
JS
3677}
3678
3679/**
3621a710 3680 * lpfc_log_intr_mode - Log the active interrupt mode
5b75da2f
JS
3681 * @phba: pointer to lpfc hba data structure.
3682 * @intr_mode: active interrupt mode adopted.
3683 *
3684 * This routine it invoked to log the currently used active interrupt mode
3685 * to the device.
3772a991
JS
3686 **/
3687static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5b75da2f
JS
3688{
3689 switch (intr_mode) {
3690 case 0:
3691 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3692 "0470 Enable INTx interrupt mode.\n");
3693 break;
3694 case 1:
3695 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3696 "0481 Enabled MSI interrupt mode.\n");
3697 break;
3698 case 2:
3699 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3700 "0480 Enabled MSI-X interrupt mode.\n");
3701 break;
3702 default:
3703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3704 "0482 Illegal interrupt mode.\n");
3705 break;
3706 }
3707 return;
3708}
3709
5b75da2f 3710/**
3772a991 3711 * lpfc_enable_pci_dev - Enable a generic PCI device.
5b75da2f
JS
3712 * @phba: pointer to lpfc hba data structure.
3713 *
3772a991
JS
3714 * This routine is invoked to enable the PCI device that is common to all
3715 * PCI devices.
5b75da2f
JS
3716 *
3717 * Return codes
af901ca1 3718 * 0 - successful
3772a991 3719 * other values - error
5b75da2f 3720 **/
3772a991
JS
3721static int
3722lpfc_enable_pci_dev(struct lpfc_hba *phba)
5b75da2f 3723{
3772a991
JS
3724 struct pci_dev *pdev;
3725 int bars;
5b75da2f 3726
3772a991
JS
3727 /* Obtain PCI device reference */
3728 if (!phba->pcidev)
3729 goto out_error;
3730 else
3731 pdev = phba->pcidev;
3732 /* Select PCI BARs */
3733 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3734 /* Enable PCI device */
3735 if (pci_enable_device_mem(pdev))
3736 goto out_error;
3737 /* Request PCI resource for the device */
3738 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3739 goto out_disable_device;
3740 /* Set up device as PCI master and save state for EEH */
3741 pci_set_master(pdev);
3742 pci_try_set_mwi(pdev);
3743 pci_save_state(pdev);
5b75da2f 3744
3772a991 3745 return 0;
5b75da2f 3746
3772a991
JS
3747out_disable_device:
3748 pci_disable_device(pdev);
3749out_error:
3750 return -ENODEV;
5b75da2f
JS
3751}
3752
3753/**
3772a991 3754 * lpfc_disable_pci_dev - Disable a generic PCI device.
5b75da2f
JS
3755 * @phba: pointer to lpfc hba data structure.
3756 *
3772a991
JS
3757 * This routine is invoked to disable the PCI device that is common to all
3758 * PCI devices.
5b75da2f
JS
3759 **/
3760static void
3772a991 3761lpfc_disable_pci_dev(struct lpfc_hba *phba)
5b75da2f 3762{
3772a991
JS
3763 struct pci_dev *pdev;
3764 int bars;
5b75da2f 3765
3772a991
JS
3766 /* Obtain PCI device reference */
3767 if (!phba->pcidev)
3768 return;
3769 else
3770 pdev = phba->pcidev;
3771 /* Select PCI BARs */
3772 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3773 /* Release PCI resource and disable PCI device */
3774 pci_release_selected_regions(pdev, bars);
3775 pci_disable_device(pdev);
3776 /* Null out PCI private reference to driver */
3777 pci_set_drvdata(pdev, NULL);
5b75da2f
JS
3778
3779 return;
3780}
3781
e59058c4 3782/**
3772a991
JS
3783 * lpfc_reset_hba - Reset a hba
3784 * @phba: pointer to lpfc hba data structure.
e59058c4 3785 *
3772a991
JS
3786 * This routine is invoked to reset a hba device. It brings the HBA
3787 * offline, performs a board restart, and then brings the board back
3788 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3789 * on outstanding mailbox commands.
e59058c4 3790 **/
3772a991
JS
3791void
3792lpfc_reset_hba(struct lpfc_hba *phba)
dea3101e 3793{
3772a991
JS
3794 /* If resets are disabled then set error state and return. */
3795 if (!phba->cfg_enable_hba_reset) {
3796 phba->link_state = LPFC_HBA_ERROR;
3797 return;
3798 }
3799 lpfc_offline_prep(phba);
3800 lpfc_offline(phba);
3801 lpfc_sli_brdrestart(phba);
3802 lpfc_online(phba);
3803 lpfc_unblock_mgmt_io(phba);
3804}
dea3101e 3805
3772a991
JS
3806/**
3807 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3808 * @phba: pointer to lpfc hba data structure.
3809 *
3810 * This routine is invoked to set up the driver internal resources specific to
3811 * support the SLI-3 HBA device it attached to.
3812 *
3813 * Return codes
af901ca1 3814 * 0 - successful
3772a991
JS
3815 * other values - error
3816 **/
3817static int
3818lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3819{
3820 struct lpfc_sli *psli;
dea3101e 3821
2e0fef85 3822 /*
3772a991 3823 * Initialize timers used by driver
2e0fef85 3824 */
dea3101e 3825
3772a991 3826 /* Heartbeat timer */
858c9f6c
JS
3827 init_timer(&phba->hb_tmofunc);
3828 phba->hb_tmofunc.function = lpfc_hb_timeout;
3829 phba->hb_tmofunc.data = (unsigned long)phba;
3830
dea3101e 3831 psli = &phba->sli;
3772a991 3832 /* MBOX heartbeat timer */
dea3101e 3833 init_timer(&psli->mbox_tmo);
3834 psli->mbox_tmo.function = lpfc_mbox_timeout;
2e0fef85 3835 psli->mbox_tmo.data = (unsigned long) phba;
3772a991 3836 /* FCP polling mode timer */
875fbdfe
JSEC
3837 init_timer(&phba->fcp_poll_timer);
3838 phba->fcp_poll_timer.function = lpfc_poll_timeout;
2e0fef85 3839 phba->fcp_poll_timer.data = (unsigned long) phba;
3772a991 3840 /* Fabric block timer */
92d7f7b0
JS
3841 init_timer(&phba->fabric_block_timer);
3842 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3843 phba->fabric_block_timer.data = (unsigned long) phba;
3772a991 3844 /* EA polling mode timer */
9399627f
JS
3845 init_timer(&phba->eratt_poll);
3846 phba->eratt_poll.function = lpfc_poll_eratt;
3847 phba->eratt_poll.data = (unsigned long) phba;
dea3101e 3848
3772a991
JS
3849 /* Host attention work mask setup */
3850 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3851 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
dea3101e 3852
3772a991
JS
3853 /* Get all the module params for configuring this host */
3854 lpfc_get_cfgparam(phba);
dea3101e 3855 /*
3772a991
JS
3856 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3857 * used to create the sg_dma_buf_pool must be dynamically calculated.
3858 * 2 segments are added since the IOCB needs a command and response bde.
dea3101e 3859 */
3772a991
JS
3860 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3861 sizeof(struct fcp_rsp) +
3862 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3863
3864 if (phba->cfg_enable_bg) {
3865 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3866 phba->cfg_sg_dma_buf_size +=
3867 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
901a920f 3868 }
dea3101e 3869
3772a991
JS
3870 /* Also reinitialize the host templates with new values. */
3871 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3872 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
dea3101e 3873
3772a991
JS
3874 phba->max_vpi = LPFC_MAX_VPI;
3875 /* This will be set to correct value after config_port mbox */
3876 phba->max_vports = 0;
dea3101e 3877
3772a991
JS
3878 /*
3879 * Initialize the SLI Layer to run with lpfc HBAs.
3880 */
3881 lpfc_sli_setup(phba);
3882 lpfc_sli_queue_setup(phba);
ed957684 3883
3772a991
JS
3884 /* Allocate device driver memory */
3885 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3886 return -ENOMEM;
51ef4c26 3887
3772a991
JS
3888 return 0;
3889}
ed957684 3890
3772a991
JS
3891/**
3892 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3893 * @phba: pointer to lpfc hba data structure.
3894 *
3895 * This routine is invoked to unset the driver internal resources set up
3896 * specific for supporting the SLI-3 HBA device it attached to.
3897 **/
3898static void
3899lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3900{
3901 /* Free device driver memory allocated */
3902 lpfc_mem_free_all(phba);
3163f725 3903
3772a991
JS
3904 return;
3905}
dea3101e 3906
3772a991 3907/**
da0436e9 3908 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3772a991
JS
3909 * @phba: pointer to lpfc hba data structure.
3910 *
da0436e9
JS
3911 * This routine is invoked to set up the driver internal resources specific to
3912 * support the SLI-4 HBA device it attached to.
3772a991
JS
3913 *
3914 * Return codes
af901ca1 3915 * 0 - successful
da0436e9 3916 * other values - error
3772a991
JS
3917 **/
3918static int
da0436e9 3919lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3772a991 3920{
da0436e9 3921 struct lpfc_sli *psli;
28baac74
JS
3922 LPFC_MBOXQ_t *mboxq;
3923 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3924 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3925 struct lpfc_mqe *mqe;
0c9ab6f5 3926 int longs;
da0436e9
JS
3927
3928 /* Before proceed, wait for POST done and device ready */
3929 rc = lpfc_sli4_post_status_check(phba);
3930 if (rc)
3931 return -ENODEV;
3932
3772a991 3933 /*
da0436e9 3934 * Initialize timers used by driver
3772a991 3935 */
3772a991 3936
da0436e9
JS
3937 /* Heartbeat timer */
3938 init_timer(&phba->hb_tmofunc);
3939 phba->hb_tmofunc.function = lpfc_hb_timeout;
3940 phba->hb_tmofunc.data = (unsigned long)phba;
3772a991 3941
da0436e9
JS
3942 psli = &phba->sli;
3943 /* MBOX heartbeat timer */
3944 init_timer(&psli->mbox_tmo);
3945 psli->mbox_tmo.function = lpfc_mbox_timeout;
3946 psli->mbox_tmo.data = (unsigned long) phba;
3947 /* Fabric block timer */
3948 init_timer(&phba->fabric_block_timer);
3949 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3950 phba->fabric_block_timer.data = (unsigned long) phba;
3951 /* EA polling mode timer */
3952 init_timer(&phba->eratt_poll);
3953 phba->eratt_poll.function = lpfc_poll_eratt;
3954 phba->eratt_poll.data = (unsigned long) phba;
ecfd03c6
JS
3955 /* FCF rediscover timer */
3956 init_timer(&phba->fcf.redisc_wait);
3957 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3958 phba->fcf.redisc_wait.data = (unsigned long)phba;
3959
da0436e9
JS
3960 /*
3961 * We need to do a READ_CONFIG mailbox command here before
3962 * calling lpfc_get_cfgparam. For VFs this will report the
3963 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3964 * All of the resources allocated
3965 * for this Port are tied to these values.
3966 */
3967 /* Get all the module params for configuring this host */
3968 lpfc_get_cfgparam(phba);
3969 phba->max_vpi = LPFC_MAX_VPI;
3970 /* This will be set to correct value after the read_config mbox */
3971 phba->max_vports = 0;
3772a991 3972
da0436e9
JS
3973 /* Program the default value of vlan_id and fc_map */
3974 phba->valid_vlan = 0;
3975 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3976 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3977 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3772a991 3978
da0436e9
JS
3979 /*
3980 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3981 * used to create the sg_dma_buf_pool must be dynamically calculated.
3982 * 2 segments are added since the IOCB needs a command and response bde.
3983 * To insure that the scsi sgl does not cross a 4k page boundary only
28baac74 3984 * sgl sizes of must be a power of 2.
da0436e9 3985 */
28baac74
JS
3986 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
3987 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
3988 /* Feature Level 1 hardware is limited to 2 pages */
3989 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
3990 LPFC_SLI_INTF_FEATURELEVEL1_1))
3991 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
da0436e9 3992 else
28baac74
JS
3993 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
3994 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
3995 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
3996 dma_buf_size = dma_buf_size << 1)
3997 ;
3998 if (dma_buf_size == max_buf_size)
3999 phba->cfg_sg_seg_cnt = (dma_buf_size -
4000 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4001 (2 * sizeof(struct sli4_sge))) /
4002 sizeof(struct sli4_sge);
4003 phba->cfg_sg_dma_buf_size = dma_buf_size;
3772a991 4004
da0436e9
JS
4005 /* Initialize buffer queue management fields */
4006 hbq_count = lpfc_sli_hbq_count();
4007 for (i = 0; i < hbq_count; ++i)
4008 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4009 INIT_LIST_HEAD(&phba->rb_pend_list);
4010 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4011 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3772a991 4012
da0436e9
JS
4013 /*
4014 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4015 */
4016 /* Initialize the Abort scsi buffer list used by driver */
4017 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4018 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4019 /* This abort list used by worker thread */
4020 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3772a991 4021
da0436e9
JS
4022 /*
4023 * Initialize dirver internal slow-path work queues
4024 */
3772a991 4025
da0436e9
JS
4026 /* Driver internel slow-path CQ Event pool */
4027 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4028 /* Response IOCB work queue list */
45ed1190 4029 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
da0436e9
JS
4030 /* Asynchronous event CQ Event work queue list */
4031 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4032 /* Fast-path XRI aborted CQ Event work queue list */
4033 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4034 /* Slow-path XRI aborted CQ Event work queue list */
4035 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4036 /* Receive queue CQ Event work queue list */
4037 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4038
4039 /* Initialize the driver internal SLI layer lists. */
4040 lpfc_sli_setup(phba);
4041 lpfc_sli_queue_setup(phba);
3772a991 4042
da0436e9
JS
4043 /* Allocate device driver memory */
4044 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4045 if (rc)
4046 return -ENOMEM;
4047
4048 /* Create the bootstrap mailbox command */
4049 rc = lpfc_create_bootstrap_mbox(phba);
4050 if (unlikely(rc))
4051 goto out_free_mem;
4052
4053 /* Set up the host's endian order with the device. */
4054 rc = lpfc_setup_endian_order(phba);
4055 if (unlikely(rc))
4056 goto out_free_bsmbx;
4057
6669f9bb
JS
4058 rc = lpfc_sli4_fw_cfg_check(phba);
4059 if (unlikely(rc))
4060 goto out_free_bsmbx;
4061
da0436e9
JS
4062 /* Set up the hba's configuration parameters. */
4063 rc = lpfc_sli4_read_config(phba);
4064 if (unlikely(rc))
4065 goto out_free_bsmbx;
4066
4067 /* Perform a function reset */
4068 rc = lpfc_pci_function_reset(phba);
4069 if (unlikely(rc))
4070 goto out_free_bsmbx;
4071
cb5172ea
JS
4072 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4073 GFP_KERNEL);
4074 if (!mboxq) {
4075 rc = -ENOMEM;
4076 goto out_free_bsmbx;
4077 }
4078
4079 /* Get the Supported Pages. It is always available. */
4080 lpfc_supported_pages(mboxq);
4081 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4082 if (unlikely(rc)) {
4083 rc = -EIO;
4084 mempool_free(mboxq, phba->mbox_mem_pool);
4085 goto out_free_bsmbx;
4086 }
4087
4088 mqe = &mboxq->u.mqe;
4089 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4090 LPFC_MAX_SUPPORTED_PAGES);
4091 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4092 switch (pn_page[i]) {
4093 case LPFC_SLI4_PARAMETERS:
4094 phba->sli4_hba.pc_sli4_params.supported = 1;
4095 break;
4096 default:
4097 break;
4098 }
4099 }
4100
4101 /* Read the port's SLI4 Parameters capabilities if supported. */
4102 if (phba->sli4_hba.pc_sli4_params.supported)
4103 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4104 mempool_free(mboxq, phba->mbox_mem_pool);
4105 if (rc) {
4106 rc = -EIO;
4107 goto out_free_bsmbx;
4108 }
da0436e9
JS
4109 /* Create all the SLI4 queues */
4110 rc = lpfc_sli4_queue_create(phba);
4111 if (rc)
4112 goto out_free_bsmbx;
4113
4114 /* Create driver internal CQE event pool */
4115 rc = lpfc_sli4_cq_event_pool_create(phba);
4116 if (rc)
4117 goto out_destroy_queue;
4118
4119 /* Initialize and populate the iocb list per host */
4120 rc = lpfc_init_sgl_list(phba);
4121 if (rc) {
4122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4123 "1400 Failed to initialize sgl list.\n");
4124 goto out_destroy_cq_event_pool;
4125 }
4126 rc = lpfc_init_active_sgl_array(phba);
4127 if (rc) {
4128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4129 "1430 Failed to initialize sgl list.\n");
4130 goto out_free_sgl_list;
4131 }
4132
4133 rc = lpfc_sli4_init_rpi_hdrs(phba);
4134 if (rc) {
4135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4136 "1432 Failed to initialize rpi headers.\n");
4137 goto out_free_active_sgl;
4138 }
4139
0c9ab6f5
JS
4140 /* Allocate eligible FCF bmask memory for FCF round robin failover */
4141 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4142 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4143 GFP_KERNEL);
4144 if (!phba->fcf.fcf_rr_bmask) {
4145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4146 "2759 Failed allocate memory for FCF round "
4147 "robin failover bmask\n");
4148 goto out_remove_rpi_hdrs;
4149 }
4150
da0436e9
JS
4151 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4152 phba->cfg_fcp_eq_count), GFP_KERNEL);
4153 if (!phba->sli4_hba.fcp_eq_hdl) {
4154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4155 "2572 Failed allocate memory for fast-path "
4156 "per-EQ handle array\n");
0c9ab6f5 4157 goto out_free_fcf_rr_bmask;
da0436e9
JS
4158 }
4159
4160 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4161 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4162 if (!phba->sli4_hba.msix_entries) {
4163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4164 "2573 Failed allocate memory for msi-x "
4165 "interrupt vector entries\n");
4166 goto out_free_fcp_eq_hdl;
4167 }
4168
4169 return rc;
4170
4171out_free_fcp_eq_hdl:
4172 kfree(phba->sli4_hba.fcp_eq_hdl);
0c9ab6f5
JS
4173out_free_fcf_rr_bmask:
4174 kfree(phba->fcf.fcf_rr_bmask);
da0436e9
JS
4175out_remove_rpi_hdrs:
4176 lpfc_sli4_remove_rpi_hdrs(phba);
4177out_free_active_sgl:
4178 lpfc_free_active_sgl(phba);
4179out_free_sgl_list:
4180 lpfc_free_sgl_list(phba);
4181out_destroy_cq_event_pool:
4182 lpfc_sli4_cq_event_pool_destroy(phba);
4183out_destroy_queue:
4184 lpfc_sli4_queue_destroy(phba);
4185out_free_bsmbx:
4186 lpfc_destroy_bootstrap_mbox(phba);
4187out_free_mem:
4188 lpfc_mem_free(phba);
4189 return rc;
4190}
4191
4192/**
4193 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4194 * @phba: pointer to lpfc hba data structure.
4195 *
4196 * This routine is invoked to unset the driver internal resources set up
4197 * specific for supporting the SLI-4 HBA device it attached to.
4198 **/
4199static void
4200lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4201{
4202 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4203
4204 /* unregister default FCFI from the HBA */
4205 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4206
4207 /* Free the default FCR table */
4208 lpfc_sli_remove_dflt_fcf(phba);
4209
4210 /* Free memory allocated for msi-x interrupt vector entries */
4211 kfree(phba->sli4_hba.msix_entries);
4212
4213 /* Free memory allocated for fast-path work queue handles */
4214 kfree(phba->sli4_hba.fcp_eq_hdl);
4215
4216 /* Free the allocated rpi headers. */
4217 lpfc_sli4_remove_rpi_hdrs(phba);
d11e31dd 4218 lpfc_sli4_remove_rpis(phba);
da0436e9 4219
0c9ab6f5
JS
4220 /* Free eligible FCF index bmask */
4221 kfree(phba->fcf.fcf_rr_bmask);
4222
da0436e9
JS
4223 /* Free the ELS sgl list */
4224 lpfc_free_active_sgl(phba);
4225 lpfc_free_sgl_list(phba);
4226
4227 /* Free the SCSI sgl management array */
4228 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4229
4230 /* Free the SLI4 queues */
4231 lpfc_sli4_queue_destroy(phba);
4232
4233 /* Free the completion queue EQ event pool */
4234 lpfc_sli4_cq_event_release_all(phba);
4235 lpfc_sli4_cq_event_pool_destroy(phba);
4236
4237 /* Reset SLI4 HBA FCoE function */
4238 lpfc_pci_function_reset(phba);
4239
4240 /* Free the bsmbx region. */
4241 lpfc_destroy_bootstrap_mbox(phba);
4242
4243 /* Free the SLI Layer memory with SLI4 HBAs */
4244 lpfc_mem_free_all(phba);
4245
4246 /* Free the current connect table */
4247 list_for_each_entry_safe(conn_entry, next_conn_entry,
4d9ab994
JS
4248 &phba->fcf_conn_rec_list, list) {
4249 list_del_init(&conn_entry->list);
da0436e9 4250 kfree(conn_entry);
4d9ab994 4251 }
da0436e9
JS
4252
4253 return;
4254}
4255
4256/**
4257 * lpfc_init_api_table_setup - Set up init api fucntion jump table
4258 * @phba: The hba struct for which this call is being executed.
4259 * @dev_grp: The HBA PCI-Device group number.
4260 *
4261 * This routine sets up the device INIT interface API function jump table
4262 * in @phba struct.
4263 *
4264 * Returns: 0 - success, -ENODEV - failure.
4265 **/
4266int
4267lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4268{
84d1b006
JS
4269 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4270 phba->lpfc_hba_down_link = lpfc_hba_down_link;
da0436e9
JS
4271 switch (dev_grp) {
4272 case LPFC_PCI_DEV_LP:
4273 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4274 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4275 phba->lpfc_stop_port = lpfc_stop_port_s3;
4276 break;
4277 case LPFC_PCI_DEV_OC:
4278 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4279 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4280 phba->lpfc_stop_port = lpfc_stop_port_s4;
4281 break;
4282 default:
4283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4284 "1431 Invalid HBA PCI-device group: 0x%x\n",
4285 dev_grp);
4286 return -ENODEV;
4287 break;
4288 }
4289 return 0;
4290}
4291
4292/**
4293 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4294 * @phba: pointer to lpfc hba data structure.
4295 *
4296 * This routine is invoked to set up the driver internal resources before the
4297 * device specific resource setup to support the HBA device it attached to.
4298 *
4299 * Return codes
af901ca1 4300 * 0 - successful
da0436e9
JS
4301 * other values - error
4302 **/
4303static int
4304lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4305{
4306 /*
4307 * Driver resources common to all SLI revisions
4308 */
4309 atomic_set(&phba->fast_event_count, 0);
4310 spin_lock_init(&phba->hbalock);
4311
4312 /* Initialize ndlp management spinlock */
4313 spin_lock_init(&phba->ndlp_lock);
4314
4315 INIT_LIST_HEAD(&phba->port_list);
4316 INIT_LIST_HEAD(&phba->work_list);
4317 init_waitqueue_head(&phba->wait_4_mlo_m_q);
4318
4319 /* Initialize the wait queue head for the kernel thread */
4320 init_waitqueue_head(&phba->work_waitq);
4321
4322 /* Initialize the scsi buffer list used by driver for scsi IO */
4323 spin_lock_init(&phba->scsi_buf_list_lock);
4324 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
4325
4326 /* Initialize the fabric iocb list */
4327 INIT_LIST_HEAD(&phba->fabric_iocb_list);
4328
4329 /* Initialize list to save ELS buffers */
4330 INIT_LIST_HEAD(&phba->elsbuf);
4331
4332 /* Initialize FCF connection rec list */
4333 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
4334
4335 return 0;
4336}
4337
4338/**
4339 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4340 * @phba: pointer to lpfc hba data structure.
4341 *
4342 * This routine is invoked to set up the driver internal resources after the
4343 * device specific resource setup to support the HBA device it attached to.
4344 *
4345 * Return codes
af901ca1 4346 * 0 - successful
da0436e9
JS
4347 * other values - error
4348 **/
4349static int
4350lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4351{
4352 int error;
4353
4354 /* Startup the kernel thread for this host adapter. */
4355 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4356 "lpfc_worker_%d", phba->brd_no);
4357 if (IS_ERR(phba->worker_thread)) {
4358 error = PTR_ERR(phba->worker_thread);
4359 return error;
3772a991
JS
4360 }
4361
4362 return 0;
4363}
4364
4365/**
4366 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4367 * @phba: pointer to lpfc hba data structure.
4368 *
4369 * This routine is invoked to unset the driver internal resources set up after
4370 * the device specific resource setup for supporting the HBA device it
4371 * attached to.
4372 **/
4373static void
4374lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4375{
4376 /* Stop kernel worker thread */
4377 kthread_stop(phba->worker_thread);
4378}
4379
4380/**
4381 * lpfc_free_iocb_list - Free iocb list.
4382 * @phba: pointer to lpfc hba data structure.
4383 *
4384 * This routine is invoked to free the driver's IOCB list and memory.
4385 **/
4386static void
4387lpfc_free_iocb_list(struct lpfc_hba *phba)
4388{
4389 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4390
4391 spin_lock_irq(&phba->hbalock);
4392 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4393 &phba->lpfc_iocb_list, list) {
4394 list_del(&iocbq_entry->list);
4395 kfree(iocbq_entry);
4396 phba->total_iocbq_bufs--;
98c9ea5c 4397 }
3772a991
JS
4398 spin_unlock_irq(&phba->hbalock);
4399
4400 return;
4401}
4402
4403/**
4404 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4405 * @phba: pointer to lpfc hba data structure.
4406 *
4407 * This routine is invoked to allocate and initizlize the driver's IOCB
4408 * list and set up the IOCB tag array accordingly.
4409 *
4410 * Return codes
af901ca1 4411 * 0 - successful
3772a991
JS
4412 * other values - error
4413 **/
4414static int
4415lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4416{
4417 struct lpfc_iocbq *iocbq_entry = NULL;
4418 uint16_t iotag;
4419 int i;
dea3101e 4420
4421 /* Initialize and populate the iocb list per host. */
4422 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
3772a991 4423 for (i = 0; i < iocb_count; i++) {
dd00cc48 4424 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea3101e 4425 if (iocbq_entry == NULL) {
4426 printk(KERN_ERR "%s: only allocated %d iocbs of "
4427 "expected %d count. Unloading driver.\n",
cadbd4a5 4428 __func__, i, LPFC_IOCB_LIST_CNT);
dea3101e 4429 goto out_free_iocbq;
4430 }
4431
604a3e30
JB
4432 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4433 if (iotag == 0) {
3772a991 4434 kfree(iocbq_entry);
604a3e30 4435 printk(KERN_ERR "%s: failed to allocate IOTAG. "
3772a991 4436 "Unloading driver.\n", __func__);
604a3e30
JB
4437 goto out_free_iocbq;
4438 }
3772a991 4439 iocbq_entry->sli4_xritag = NO_XRI;
2e0fef85
JS
4440
4441 spin_lock_irq(&phba->hbalock);
dea3101e 4442 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4443 phba->total_iocbq_bufs++;
2e0fef85 4444 spin_unlock_irq(&phba->hbalock);
dea3101e 4445 }
4446
3772a991 4447 return 0;
dea3101e 4448
3772a991
JS
4449out_free_iocbq:
4450 lpfc_free_iocb_list(phba);
dea3101e 4451
3772a991
JS
4452 return -ENOMEM;
4453}
5e9d9b82 4454
3772a991 4455/**
da0436e9
JS
4456 * lpfc_free_sgl_list - Free sgl list.
4457 * @phba: pointer to lpfc hba data structure.
3772a991 4458 *
da0436e9 4459 * This routine is invoked to free the driver's sgl list and memory.
3772a991 4460 **/
da0436e9
JS
4461static void
4462lpfc_free_sgl_list(struct lpfc_hba *phba)
3772a991 4463{
da0436e9
JS
4464 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4465 LIST_HEAD(sglq_list);
4466 int rc = 0;
dea3101e 4467
da0436e9
JS
4468 spin_lock_irq(&phba->hbalock);
4469 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4470 spin_unlock_irq(&phba->hbalock);
dea3101e 4471
da0436e9
JS
4472 list_for_each_entry_safe(sglq_entry, sglq_next,
4473 &sglq_list, list) {
4474 list_del(&sglq_entry->list);
4475 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4476 kfree(sglq_entry);
4477 phba->sli4_hba.total_sglq_bufs--;
4478 }
4479 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4480 if (rc) {
4481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6a9c52cf 4482 "2005 Unable to deregister pages from HBA: %x\n", rc);
3772a991 4483 }
da0436e9
JS
4484 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4485}
92d7f7b0 4486
da0436e9
JS
4487/**
4488 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4489 * @phba: pointer to lpfc hba data structure.
4490 *
4491 * This routine is invoked to allocate the driver's active sgl memory.
4492 * This array will hold the sglq_entry's for active IOs.
4493 **/
4494static int
4495lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4496{
4497 int size;
4498 size = sizeof(struct lpfc_sglq *);
4499 size *= phba->sli4_hba.max_cfg_param.max_xri;
4500
4501 phba->sli4_hba.lpfc_sglq_active_list =
4502 kzalloc(size, GFP_KERNEL);
4503 if (!phba->sli4_hba.lpfc_sglq_active_list)
4504 return -ENOMEM;
4505 return 0;
3772a991
JS
4506}
4507
4508/**
da0436e9 4509 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3772a991
JS
4510 * @phba: pointer to lpfc hba data structure.
4511 *
da0436e9
JS
4512 * This routine is invoked to walk through the array of active sglq entries
4513 * and free all of the resources.
4514 * This is just a place holder for now.
3772a991
JS
4515 **/
4516static void
da0436e9 4517lpfc_free_active_sgl(struct lpfc_hba *phba)
3772a991 4518{
da0436e9 4519 kfree(phba->sli4_hba.lpfc_sglq_active_list);
3772a991
JS
4520}
4521
4522/**
da0436e9 4523 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3772a991
JS
4524 * @phba: pointer to lpfc hba data structure.
4525 *
da0436e9
JS
4526 * This routine is invoked to allocate and initizlize the driver's sgl
4527 * list and set up the sgl xritag tag array accordingly.
3772a991
JS
4528 *
4529 * Return codes
af901ca1 4530 * 0 - successful
da0436e9 4531 * other values - error
3772a991
JS
4532 **/
4533static int
da0436e9 4534lpfc_init_sgl_list(struct lpfc_hba *phba)
3772a991 4535{
da0436e9
JS
4536 struct lpfc_sglq *sglq_entry = NULL;
4537 int i;
4538 int els_xri_cnt;
4539
4540 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4541 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4542 "2400 lpfc_init_sgl_list els %d.\n",
4543 els_xri_cnt);
4544 /* Initialize and populate the sglq list per host/VF. */
4545 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4546 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4547
4548 /* Sanity check on XRI management */
4549 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4550 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4551 "2562 No room left for SCSI XRI allocation: "
4552 "max_xri=%d, els_xri=%d\n",
4553 phba->sli4_hba.max_cfg_param.max_xri,
4554 els_xri_cnt);
4555 return -ENOMEM;
4556 }
3772a991 4557
da0436e9
JS
4558 /* Allocate memory for the ELS XRI management array */
4559 phba->sli4_hba.lpfc_els_sgl_array =
4560 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4561 GFP_KERNEL);
0ff10d46 4562
da0436e9
JS
4563 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4565 "2401 Failed to allocate memory for ELS "
4566 "XRI management array of size %d.\n",
4567 els_xri_cnt);
4568 return -ENOMEM;
4569 }
2e0fef85 4570
da0436e9
JS
4571 /* Keep the SCSI XRI into the XRI management array */
4572 phba->sli4_hba.scsi_xri_max =
4573 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4574 phba->sli4_hba.scsi_xri_cnt = 0;
4575
4576 phba->sli4_hba.lpfc_scsi_psb_array =
4577 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4578 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4579
4580 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4581 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4582 "2563 Failed to allocate memory for SCSI "
4583 "XRI management array of size %d.\n",
4584 phba->sli4_hba.scsi_xri_max);
4585 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4586 return -ENOMEM;
4587 }
4588
4589 for (i = 0; i < els_xri_cnt; i++) {
4590 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4591 if (sglq_entry == NULL) {
4592 printk(KERN_ERR "%s: only allocated %d sgls of "
4593 "expected %d count. Unloading driver.\n",
4594 __func__, i, els_xri_cnt);
4595 goto out_free_mem;
4596 }
4597
4598 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4599 if (sglq_entry->sli4_xritag == NO_XRI) {
4600 kfree(sglq_entry);
4601 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4602 "Unloading driver.\n", __func__);
4603 goto out_free_mem;
4604 }
4605 sglq_entry->buff_type = GEN_BUFF_TYPE;
4606 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4607 if (sglq_entry->virt == NULL) {
4608 kfree(sglq_entry);
4609 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4610 "Unloading driver.\n", __func__);
4611 goto out_free_mem;
4612 }
4613 sglq_entry->sgl = sglq_entry->virt;
4614 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4615
4616 /* The list order is used by later block SGL registraton */
4617 spin_lock_irq(&phba->hbalock);
0f65ff68 4618 sglq_entry->state = SGL_FREED;
da0436e9
JS
4619 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4620 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4621 phba->sli4_hba.total_sglq_bufs++;
4622 spin_unlock_irq(&phba->hbalock);
4623 }
4624 return 0;
4625
4626out_free_mem:
4627 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4628 lpfc_free_sgl_list(phba);
4629 return -ENOMEM;
4630}
4631
4632/**
4633 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4634 * @phba: pointer to lpfc hba data structure.
4635 *
4636 * This routine is invoked to post rpi header templates to the
4637 * HBA consistent with the SLI-4 interface spec. This routine
4638 * posts a PAGE_SIZE memory region to the port to hold up to
4639 * PAGE_SIZE modulo 64 rpi context headers.
4640 * No locks are held here because this is an initialization routine
4641 * called only from probe or lpfc_online when interrupts are not
4642 * enabled and the driver is reinitializing the device.
4643 *
4644 * Return codes
af901ca1 4645 * 0 - successful
da0436e9
JS
4646 * ENOMEM - No availble memory
4647 * EIO - The mailbox failed to complete successfully.
4648 **/
4649int
4650lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4651{
4652 int rc = 0;
4653 int longs;
4654 uint16_t rpi_count;
4655 struct lpfc_rpi_hdr *rpi_hdr;
4656
4657 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4658
4659 /*
4660 * Provision an rpi bitmask range for discovery. The total count
4661 * is the difference between max and base + 1.
4662 */
4663 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4664 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4665
4666 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4667 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4668 GFP_KERNEL);
4669 if (!phba->sli4_hba.rpi_bmask)
4670 return -ENOMEM;
4671
4672 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4673 if (!rpi_hdr) {
4674 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4675 "0391 Error during rpi post operation\n");
4676 lpfc_sli4_remove_rpis(phba);
4677 rc = -ENODEV;
4678 }
4679
4680 return rc;
4681}
4682
4683/**
4684 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4685 * @phba: pointer to lpfc hba data structure.
4686 *
4687 * This routine is invoked to allocate a single 4KB memory region to
4688 * support rpis and stores them in the phba. This single region
4689 * provides support for up to 64 rpis. The region is used globally
4690 * by the device.
4691 *
4692 * Returns:
4693 * A valid rpi hdr on success.
4694 * A NULL pointer on any failure.
4695 **/
4696struct lpfc_rpi_hdr *
4697lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4698{
4699 uint16_t rpi_limit, curr_rpi_range;
4700 struct lpfc_dmabuf *dmabuf;
4701 struct lpfc_rpi_hdr *rpi_hdr;
4702
4703 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4704 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4705
4706 spin_lock_irq(&phba->hbalock);
4707 curr_rpi_range = phba->sli4_hba.next_rpi;
4708 spin_unlock_irq(&phba->hbalock);
4709
4710 /*
4711 * The port has a limited number of rpis. The increment here
4712 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4713 * and to allow the full max_rpi range per port.
4714 */
4715 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4716 return NULL;
4717
4718 /*
4719 * First allocate the protocol header region for the port. The
4720 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4721 */
4722 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4723 if (!dmabuf)
4724 return NULL;
4725
4726 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4727 LPFC_HDR_TEMPLATE_SIZE,
4728 &dmabuf->phys,
4729 GFP_KERNEL);
4730 if (!dmabuf->virt) {
4731 rpi_hdr = NULL;
4732 goto err_free_dmabuf;
4733 }
4734
4735 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4736 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4737 rpi_hdr = NULL;
4738 goto err_free_coherent;
4739 }
4740
4741 /* Save the rpi header data for cleanup later. */
4742 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4743 if (!rpi_hdr)
4744 goto err_free_coherent;
4745
4746 rpi_hdr->dmabuf = dmabuf;
4747 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4748 rpi_hdr->page_count = 1;
4749 spin_lock_irq(&phba->hbalock);
4750 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4751 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4752
4753 /*
4754 * The next_rpi stores the next module-64 rpi value to post
4755 * in any subsequent rpi memory region postings.
4756 */
4757 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4758 spin_unlock_irq(&phba->hbalock);
4759 return rpi_hdr;
4760
4761 err_free_coherent:
4762 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4763 dmabuf->virt, dmabuf->phys);
4764 err_free_dmabuf:
4765 kfree(dmabuf);
4766 return NULL;
4767}
4768
4769/**
4770 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4771 * @phba: pointer to lpfc hba data structure.
4772 *
4773 * This routine is invoked to remove all memory resources allocated
4774 * to support rpis. This routine presumes the caller has released all
4775 * rpis consumed by fabric or port logins and is prepared to have
4776 * the header pages removed.
4777 **/
4778void
4779lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4780{
4781 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4782
4783 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4784 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4785 list_del(&rpi_hdr->list);
4786 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4787 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4788 kfree(rpi_hdr->dmabuf);
4789 kfree(rpi_hdr);
4790 }
4791
4792 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4793 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4794}
4795
4796/**
4797 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4798 * @pdev: pointer to pci device data structure.
4799 *
4800 * This routine is invoked to allocate the driver hba data structure for an
4801 * HBA device. If the allocation is successful, the phba reference to the
4802 * PCI device data structure is set.
4803 *
4804 * Return codes
af901ca1 4805 * pointer to @phba - successful
da0436e9
JS
4806 * NULL - error
4807 **/
4808static struct lpfc_hba *
4809lpfc_hba_alloc(struct pci_dev *pdev)
4810{
4811 struct lpfc_hba *phba;
4812
4813 /* Allocate memory for HBA structure */
4814 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4815 if (!phba) {
e34ccdfe 4816 dev_err(&pdev->dev, "failed to allocate hba struct\n");
da0436e9
JS
4817 return NULL;
4818 }
4819
4820 /* Set reference to PCI device in HBA structure */
4821 phba->pcidev = pdev;
4822
4823 /* Assign an unused board number */
4824 phba->brd_no = lpfc_get_instance();
4825 if (phba->brd_no < 0) {
4826 kfree(phba);
4827 return NULL;
4828 }
4829
4fede78f 4830 spin_lock_init(&phba->ct_ev_lock);
f1c3b0fc
JS
4831 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4832
da0436e9
JS
4833 return phba;
4834}
4835
4836/**
4837 * lpfc_hba_free - Free driver hba data structure with a device.
4838 * @phba: pointer to lpfc hba data structure.
4839 *
4840 * This routine is invoked to free the driver hba data structure with an
4841 * HBA device.
4842 **/
4843static void
4844lpfc_hba_free(struct lpfc_hba *phba)
4845{
4846 /* Release the driver assigned board number */
4847 idr_remove(&lpfc_hba_index, phba->brd_no);
4848
4849 kfree(phba);
4850 return;
4851}
4852
4853/**
4854 * lpfc_create_shost - Create hba physical port with associated scsi host.
4855 * @phba: pointer to lpfc hba data structure.
4856 *
4857 * This routine is invoked to create HBA physical port and associate a SCSI
4858 * host with it.
4859 *
4860 * Return codes
af901ca1 4861 * 0 - successful
da0436e9
JS
4862 * other values - error
4863 **/
4864static int
4865lpfc_create_shost(struct lpfc_hba *phba)
4866{
4867 struct lpfc_vport *vport;
4868 struct Scsi_Host *shost;
4869
4870 /* Initialize HBA FC structure */
4871 phba->fc_edtov = FF_DEF_EDTOV;
4872 phba->fc_ratov = FF_DEF_RATOV;
4873 phba->fc_altov = FF_DEF_ALTOV;
4874 phba->fc_arbtov = FF_DEF_ARBTOV;
4875
4876 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
4877 if (!vport)
4878 return -ENODEV;
4879
4880 shost = lpfc_shost_from_vport(vport);
4881 phba->pport = vport;
4882 lpfc_debugfs_initialize(vport);
4883 /* Put reference to SCSI host to driver's device private data */
4884 pci_set_drvdata(phba->pcidev, shost);
2e0fef85 4885
3772a991
JS
4886 return 0;
4887}
db2378e0 4888
3772a991
JS
4889/**
4890 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4891 * @phba: pointer to lpfc hba data structure.
4892 *
4893 * This routine is invoked to destroy HBA physical port and the associated
4894 * SCSI host.
4895 **/
4896static void
4897lpfc_destroy_shost(struct lpfc_hba *phba)
4898{
4899 struct lpfc_vport *vport = phba->pport;
4900
4901 /* Destroy physical port that associated with the SCSI host */
4902 destroy_port(vport);
4903
4904 return;
4905}
4906
4907/**
4908 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4909 * @phba: pointer to lpfc hba data structure.
4910 * @shost: the shost to be used to detect Block guard settings.
4911 *
4912 * This routine sets up the local Block guard protocol settings for @shost.
4913 * This routine also allocates memory for debugging bg buffers.
4914 **/
4915static void
4916lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4917{
4918 int pagecnt = 10;
4919 if (lpfc_prot_mask && lpfc_prot_guard) {
4920 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4921 "1478 Registering BlockGuard with the "
4922 "SCSI layer\n");
4923 scsi_host_set_prot(shost, lpfc_prot_mask);
4924 scsi_host_set_guard(shost, lpfc_prot_guard);
4925 }
4926 if (!_dump_buf_data) {
4927 while (pagecnt) {
4928 spin_lock_init(&_dump_buf_lock);
4929 _dump_buf_data =
4930 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4931 if (_dump_buf_data) {
6a9c52cf
JS
4932 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4933 "9043 BLKGRD: allocated %d pages for "
3772a991
JS
4934 "_dump_buf_data at 0x%p\n",
4935 (1 << pagecnt), _dump_buf_data);
4936 _dump_buf_data_order = pagecnt;
4937 memset(_dump_buf_data, 0,
4938 ((1 << PAGE_SHIFT) << pagecnt));
4939 break;
4940 } else
4941 --pagecnt;
4942 }
4943 if (!_dump_buf_data_order)
6a9c52cf
JS
4944 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4945 "9044 BLKGRD: ERROR unable to allocate "
3772a991
JS
4946 "memory for hexdump\n");
4947 } else
6a9c52cf
JS
4948 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4949 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
3772a991
JS
4950 "\n", _dump_buf_data);
4951 if (!_dump_buf_dif) {
4952 while (pagecnt) {
4953 _dump_buf_dif =
4954 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4955 if (_dump_buf_dif) {
6a9c52cf
JS
4956 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4957 "9046 BLKGRD: allocated %d pages for "
3772a991
JS
4958 "_dump_buf_dif at 0x%p\n",
4959 (1 << pagecnt), _dump_buf_dif);
4960 _dump_buf_dif_order = pagecnt;
4961 memset(_dump_buf_dif, 0,
4962 ((1 << PAGE_SHIFT) << pagecnt));
4963 break;
4964 } else
4965 --pagecnt;
4966 }
4967 if (!_dump_buf_dif_order)
6a9c52cf
JS
4968 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4969 "9047 BLKGRD: ERROR unable to allocate "
3772a991
JS
4970 "memory for hexdump\n");
4971 } else
6a9c52cf
JS
4972 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4973 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
3772a991
JS
4974 _dump_buf_dif);
4975}
4976
4977/**
4978 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4979 * @phba: pointer to lpfc hba data structure.
4980 *
4981 * This routine is invoked to perform all the necessary post initialization
4982 * setup for the device.
4983 **/
4984static void
4985lpfc_post_init_setup(struct lpfc_hba *phba)
4986{
4987 struct Scsi_Host *shost;
4988 struct lpfc_adapter_event_header adapter_event;
4989
4990 /* Get the default values for Model Name and Description */
4991 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4992
4993 /*
4994 * hba setup may have changed the hba_queue_depth so we need to
4995 * adjust the value of can_queue.
4996 */
4997 shost = pci_get_drvdata(phba->pcidev);
4998 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4999 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5000 lpfc_setup_bg(phba, shost);
5001
5002 lpfc_host_attrib_init(shost);
5003
5004 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5005 spin_lock_irq(shost->host_lock);
5006 lpfc_poll_start_timer(phba);
5007 spin_unlock_irq(shost->host_lock);
5008 }
5009
5010 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5011 "0428 Perform SCSI scan\n");
5012 /* Send board arrival event to upper layer */
5013 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5014 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5015 fc_host_post_vendor_event(shost, fc_get_event_number(),
5016 sizeof(adapter_event),
5017 (char *) &adapter_event,
5018 LPFC_NL_VENDOR_ID);
5019 return;
5020}
5021
5022/**
5023 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5024 * @phba: pointer to lpfc hba data structure.
5025 *
5026 * This routine is invoked to set up the PCI device memory space for device
5027 * with SLI-3 interface spec.
5028 *
5029 * Return codes
af901ca1 5030 * 0 - successful
3772a991
JS
5031 * other values - error
5032 **/
5033static int
5034lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5035{
5036 struct pci_dev *pdev;
5037 unsigned long bar0map_len, bar2map_len;
5038 int i, hbq_count;
5039 void *ptr;
5040 int error = -ENODEV;
5041
5042 /* Obtain PCI device reference */
5043 if (!phba->pcidev)
5044 return error;
5045 else
5046 pdev = phba->pcidev;
5047
5048 /* Set the device DMA mask size */
8e68597d
MR
5049 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5050 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5051 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5052 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
3772a991 5053 return error;
8e68597d
MR
5054 }
5055 }
3772a991
JS
5056
5057 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5058 * required by each mapping.
5059 */
5060 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5061 bar0map_len = pci_resource_len(pdev, 0);
5062
5063 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5064 bar2map_len = pci_resource_len(pdev, 2);
5065
5066 /* Map HBA SLIM to a kernel virtual address. */
5067 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5068 if (!phba->slim_memmap_p) {
5069 dev_printk(KERN_ERR, &pdev->dev,
5070 "ioremap failed for SLIM memory.\n");
5071 goto out;
5072 }
5073
5074 /* Map HBA Control Registers to a kernel virtual address. */
5075 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5076 if (!phba->ctrl_regs_memmap_p) {
5077 dev_printk(KERN_ERR, &pdev->dev,
5078 "ioremap failed for HBA control registers.\n");
5079 goto out_iounmap_slim;
5080 }
5081
5082 /* Allocate memory for SLI-2 structures */
5083 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5084 SLI2_SLIM_SIZE,
5085 &phba->slim2p.phys,
5086 GFP_KERNEL);
5087 if (!phba->slim2p.virt)
5088 goto out_iounmap;
5089
5090 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5091 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
7a470277
JS
5092 phba->mbox_ext = (phba->slim2p.virt +
5093 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
3772a991
JS
5094 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5095 phba->IOCBs = (phba->slim2p.virt +
5096 offsetof(struct lpfc_sli2_slim, IOCBs));
5097
5098 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5099 lpfc_sli_hbq_size(),
5100 &phba->hbqslimp.phys,
5101 GFP_KERNEL);
5102 if (!phba->hbqslimp.virt)
5103 goto out_free_slim;
5104
5105 hbq_count = lpfc_sli_hbq_count();
5106 ptr = phba->hbqslimp.virt;
5107 for (i = 0; i < hbq_count; ++i) {
5108 phba->hbqs[i].hbq_virt = ptr;
5109 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5110 ptr += (lpfc_hbq_defs[i]->entry_count *
5111 sizeof(struct lpfc_hbq_entry));
5112 }
5113 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5114 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5115
5116 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5117
5118 INIT_LIST_HEAD(&phba->rb_pend_list);
5119
5120 phba->MBslimaddr = phba->slim_memmap_p;
5121 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5122 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5123 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5124 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
5125
5126 return 0;
5127
5128out_free_slim:
5129 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5130 phba->slim2p.virt, phba->slim2p.phys);
5131out_iounmap:
5132 iounmap(phba->ctrl_regs_memmap_p);
5133out_iounmap_slim:
5134 iounmap(phba->slim_memmap_p);
5135out:
5136 return error;
5137}
5138
5139/**
5140 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5141 * @phba: pointer to lpfc hba data structure.
5142 *
5143 * This routine is invoked to unset the PCI device memory space for device
5144 * with SLI-3 interface spec.
5145 **/
5146static void
5147lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5148{
5149 struct pci_dev *pdev;
5150
5151 /* Obtain PCI device reference */
5152 if (!phba->pcidev)
5153 return;
5154 else
5155 pdev = phba->pcidev;
5156
5157 /* Free coherent DMA memory allocated */
5158 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5159 phba->hbqslimp.virt, phba->hbqslimp.phys);
5160 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5161 phba->slim2p.virt, phba->slim2p.phys);
5162
5163 /* I/O memory unmap */
5164 iounmap(phba->ctrl_regs_memmap_p);
5165 iounmap(phba->slim_memmap_p);
5166
5167 return;
5168}
5169
5170/**
da0436e9 5171 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
3772a991
JS
5172 * @phba: pointer to lpfc hba data structure.
5173 *
da0436e9
JS
5174 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5175 * done and check status.
3772a991 5176 *
da0436e9 5177 * Return 0 if successful, otherwise -ENODEV.
3772a991 5178 **/
da0436e9
JS
5179int
5180lpfc_sli4_post_status_check(struct lpfc_hba *phba)
3772a991 5181{
28baac74 5182 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
da0436e9 5183 int i, port_error = -ENODEV;
3772a991 5184
da0436e9
JS
5185 if (!phba->sli4_hba.STAregaddr)
5186 return -ENODEV;
3772a991 5187
da0436e9
JS
5188 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5189 for (i = 0; i < 3000; i++) {
5190 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
5191 /* Encounter fatal POST error, break out */
5192 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
5193 port_error = -ENODEV;
5194 break;
5195 }
5196 if (LPFC_POST_STAGE_ARMFW_READY ==
5197 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
5198 port_error = 0;
5199 break;
5200 }
5201 msleep(10);
3772a991
JS
5202 }
5203
da0436e9
JS
5204 if (port_error)
5205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5206 "1408 Failure HBA POST Status: sta_reg=0x%x, "
5207 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5208 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5209 bf_get(lpfc_hst_state_perr, &sta_reg),
5210 bf_get(lpfc_hst_state_sfi, &sta_reg),
5211 bf_get(lpfc_hst_state_nip, &sta_reg),
5212 bf_get(lpfc_hst_state_ipc, &sta_reg),
5213 bf_get(lpfc_hst_state_xrom, &sta_reg),
5214 bf_get(lpfc_hst_state_dl, &sta_reg),
5215 bf_get(lpfc_hst_state_port_status, &sta_reg));
5216
5217 /* Log device information */
28baac74
JS
5218 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5219 if (bf_get(lpfc_sli_intf_valid,
5220 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5221 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5222 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5223 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5224 bf_get(lpfc_sli_intf_sli_family,
5225 &phba->sli4_hba.sli_intf),
5226 bf_get(lpfc_sli_intf_slirev,
5227 &phba->sli4_hba.sli_intf),
5228 bf_get(lpfc_sli_intf_featurelevel1,
5229 &phba->sli4_hba.sli_intf),
5230 bf_get(lpfc_sli_intf_featurelevel2,
5231 &phba->sli4_hba.sli_intf));
5232 }
a747c9ce
JS
5233 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5234 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
8fa38513 5235 /* With uncoverable error, log the error message and return error */
a747c9ce
JS
5236 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5237 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5238 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5239 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5241 "1422 HBA Unrecoverable error: "
5242 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5243 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5244 uerrlo_reg.word0, uerrhi_reg.word0,
5245 phba->sli4_hba.ue_mask_lo,
5246 phba->sli4_hba.ue_mask_hi);
8fa38513
JS
5247 return -ENODEV;
5248 }
5249
da0436e9
JS
5250 return port_error;
5251}
3772a991 5252
da0436e9
JS
5253/**
5254 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5255 * @phba: pointer to lpfc hba data structure.
5256 *
5257 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5258 * memory map.
5259 **/
5260static void
5261lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5262{
5263 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5264 LPFC_UERR_STATUS_LO;
5265 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5266 LPFC_UERR_STATUS_HI;
a747c9ce
JS
5267 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5268 LPFC_UE_MASK_LO;
5269 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5270 LPFC_UE_MASK_HI;
28baac74
JS
5271 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5272 LPFC_SLI_INTF;
da0436e9 5273}
3772a991 5274
da0436e9
JS
5275/**
5276 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5277 * @phba: pointer to lpfc hba data structure.
5278 *
5279 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5280 * memory map.
5281 **/
5282static void
5283lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5284{
3772a991 5285
da0436e9
JS
5286 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5287 LPFC_HST_STATE;
5288 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5289 LPFC_HST_ISR0;
5290 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5291 LPFC_HST_IMR0;
5292 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5293 LPFC_HST_ISCR0;
5294 return;
3772a991
JS
5295}
5296
5297/**
da0436e9 5298 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
3772a991 5299 * @phba: pointer to lpfc hba data structure.
da0436e9 5300 * @vf: virtual function number
3772a991 5301 *
da0436e9
JS
5302 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5303 * based on the given viftual function number, @vf.
5304 *
5305 * Return 0 if successful, otherwise -ENODEV.
3772a991 5306 **/
da0436e9
JS
5307static int
5308lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
3772a991 5309{
da0436e9
JS
5310 if (vf > LPFC_VIR_FUNC_MAX)
5311 return -ENODEV;
3772a991 5312
da0436e9
JS
5313 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5314 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5315 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5316 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5317 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5318 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5319 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5320 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5321 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5322 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5323 return 0;
3772a991
JS
5324}
5325
5326/**
da0436e9 5327 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
3772a991
JS
5328 * @phba: pointer to lpfc hba data structure.
5329 *
da0436e9
JS
5330 * This routine is invoked to create the bootstrap mailbox
5331 * region consistent with the SLI-4 interface spec. This
5332 * routine allocates all memory necessary to communicate
5333 * mailbox commands to the port and sets up all alignment
5334 * needs. No locks are expected to be held when calling
5335 * this routine.
3772a991
JS
5336 *
5337 * Return codes
af901ca1 5338 * 0 - successful
da0436e9
JS
5339 * ENOMEM - could not allocated memory.
5340 **/
3772a991 5341static int
da0436e9 5342lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
3772a991 5343{
da0436e9
JS
5344 uint32_t bmbx_size;
5345 struct lpfc_dmabuf *dmabuf;
5346 struct dma_address *dma_address;
5347 uint32_t pa_addr;
5348 uint64_t phys_addr;
5349
5350 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5351 if (!dmabuf)
5352 return -ENOMEM;
3772a991 5353
da0436e9
JS
5354 /*
5355 * The bootstrap mailbox region is comprised of 2 parts
5356 * plus an alignment restriction of 16 bytes.
5357 */
5358 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5359 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5360 bmbx_size,
5361 &dmabuf->phys,
5362 GFP_KERNEL);
5363 if (!dmabuf->virt) {
5364 kfree(dmabuf);
5365 return -ENOMEM;
3772a991 5366 }
da0436e9 5367 memset(dmabuf->virt, 0, bmbx_size);
3772a991 5368
da0436e9
JS
5369 /*
5370 * Initialize the bootstrap mailbox pointers now so that the register
5371 * operations are simple later. The mailbox dma address is required
5372 * to be 16-byte aligned. Also align the virtual memory as each
5373 * maibox is copied into the bmbx mailbox region before issuing the
5374 * command to the port.
5375 */
5376 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5377 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5378
5379 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5380 LPFC_ALIGN_16_BYTE);
5381 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5382 LPFC_ALIGN_16_BYTE);
5383
5384 /*
5385 * Set the high and low physical addresses now. The SLI4 alignment
5386 * requirement is 16 bytes and the mailbox is posted to the port
5387 * as two 30-bit addresses. The other data is a bit marking whether
5388 * the 30-bit address is the high or low address.
5389 * Upcast bmbx aphys to 64bits so shift instruction compiles
5390 * clean on 32 bit machines.
5391 */
5392 dma_address = &phba->sli4_hba.bmbx.dma_address;
5393 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5394 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5395 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5396 LPFC_BMBX_BIT1_ADDR_HI);
5397
5398 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5399 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5400 LPFC_BMBX_BIT1_ADDR_LO);
5401 return 0;
3772a991
JS
5402}
5403
5404/**
da0436e9 5405 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
3772a991
JS
5406 * @phba: pointer to lpfc hba data structure.
5407 *
da0436e9
JS
5408 * This routine is invoked to teardown the bootstrap mailbox
5409 * region and release all host resources. This routine requires
5410 * the caller to ensure all mailbox commands recovered, no
5411 * additional mailbox comands are sent, and interrupts are disabled
5412 * before calling this routine.
5413 *
5414 **/
3772a991 5415static void
da0436e9 5416lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
3772a991 5417{
da0436e9
JS
5418 dma_free_coherent(&phba->pcidev->dev,
5419 phba->sli4_hba.bmbx.bmbx_size,
5420 phba->sli4_hba.bmbx.dmabuf->virt,
5421 phba->sli4_hba.bmbx.dmabuf->phys);
5422
5423 kfree(phba->sli4_hba.bmbx.dmabuf);
5424 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
3772a991
JS
5425}
5426
5427/**
da0436e9 5428 * lpfc_sli4_read_config - Get the config parameters.
3772a991
JS
5429 * @phba: pointer to lpfc hba data structure.
5430 *
da0436e9
JS
5431 * This routine is invoked to read the configuration parameters from the HBA.
5432 * The configuration parameters are used to set the base and maximum values
5433 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5434 * allocation for the port.
3772a991
JS
5435 *
5436 * Return codes
af901ca1 5437 * 0 - successful
da0436e9
JS
5438 * ENOMEM - No availble memory
5439 * EIO - The mailbox failed to complete successfully.
3772a991 5440 **/
da0436e9
JS
5441static int
5442lpfc_sli4_read_config(struct lpfc_hba *phba)
3772a991 5443{
da0436e9
JS
5444 LPFC_MBOXQ_t *pmb;
5445 struct lpfc_mbx_read_config *rd_config;
5446 uint32_t rc = 0;
3772a991 5447
da0436e9
JS
5448 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5449 if (!pmb) {
5450 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5451 "2011 Unable to allocate memory for issuing "
5452 "SLI_CONFIG_SPECIAL mailbox command\n");
5453 return -ENOMEM;
3772a991
JS
5454 }
5455
da0436e9 5456 lpfc_read_config(phba, pmb);
3772a991 5457
da0436e9
JS
5458 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5459 if (rc != MBX_SUCCESS) {
5460 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5461 "2012 Mailbox failed , mbxCmd x%x "
5462 "READ_CONFIG, mbxStatus x%x\n",
5463 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5464 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5465 rc = -EIO;
5466 } else {
5467 rd_config = &pmb->u.mqe.un.rd_config;
5468 phba->sli4_hba.max_cfg_param.max_xri =
5469 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5470 phba->sli4_hba.max_cfg_param.xri_base =
5471 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5472 phba->sli4_hba.max_cfg_param.max_vpi =
5473 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5474 phba->sli4_hba.max_cfg_param.vpi_base =
5475 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5476 phba->sli4_hba.max_cfg_param.max_rpi =
5477 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5478 phba->sli4_hba.max_cfg_param.rpi_base =
5479 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5480 phba->sli4_hba.max_cfg_param.max_vfi =
5481 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5482 phba->sli4_hba.max_cfg_param.vfi_base =
5483 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5484 phba->sli4_hba.max_cfg_param.max_fcfi =
5485 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5486 phba->sli4_hba.max_cfg_param.fcfi_base =
5487 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5488 phba->sli4_hba.max_cfg_param.max_eq =
5489 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5490 phba->sli4_hba.max_cfg_param.max_rq =
5491 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5492 phba->sli4_hba.max_cfg_param.max_wq =
5493 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5494 phba->sli4_hba.max_cfg_param.max_cq =
5495 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5496 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5497 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5498 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5499 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5500 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
5ffc266e
JS
5501 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5502 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
da0436e9
JS
5503 phba->max_vports = phba->max_vpi;
5504 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5505 "2003 cfg params XRI(B:%d M:%d), "
5506 "VPI(B:%d M:%d) "
5507 "VFI(B:%d M:%d) "
5508 "RPI(B:%d M:%d) "
5509 "FCFI(B:%d M:%d)\n",
5510 phba->sli4_hba.max_cfg_param.xri_base,
5511 phba->sli4_hba.max_cfg_param.max_xri,
5512 phba->sli4_hba.max_cfg_param.vpi_base,
5513 phba->sli4_hba.max_cfg_param.max_vpi,
5514 phba->sli4_hba.max_cfg_param.vfi_base,
5515 phba->sli4_hba.max_cfg_param.max_vfi,
5516 phba->sli4_hba.max_cfg_param.rpi_base,
5517 phba->sli4_hba.max_cfg_param.max_rpi,
5518 phba->sli4_hba.max_cfg_param.fcfi_base,
5519 phba->sli4_hba.max_cfg_param.max_fcfi);
3772a991 5520 }
da0436e9
JS
5521 mempool_free(pmb, phba->mbox_mem_pool);
5522
5523 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5524 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5525 phba->cfg_hba_queue_depth =
5526 phba->sli4_hba.max_cfg_param.max_xri;
5527 return rc;
3772a991
JS
5528}
5529
5530/**
da0436e9 5531 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
3772a991
JS
5532 * @phba: pointer to lpfc hba data structure.
5533 *
da0436e9
JS
5534 * This routine is invoked to setup the host-side endian order to the
5535 * HBA consistent with the SLI-4 interface spec.
5536 *
5537 * Return codes
af901ca1 5538 * 0 - successful
da0436e9
JS
5539 * ENOMEM - No availble memory
5540 * EIO - The mailbox failed to complete successfully.
3772a991 5541 **/
da0436e9
JS
5542static int
5543lpfc_setup_endian_order(struct lpfc_hba *phba)
3772a991 5544{
da0436e9
JS
5545 LPFC_MBOXQ_t *mboxq;
5546 uint32_t rc = 0;
5547 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5548 HOST_ENDIAN_HIGH_WORD1};
3772a991 5549
da0436e9
JS
5550 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5551 if (!mboxq) {
5552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5553 "0492 Unable to allocate memory for issuing "
5554 "SLI_CONFIG_SPECIAL mailbox command\n");
5555 return -ENOMEM;
5556 }
3772a991 5557
da0436e9
JS
5558 /*
5559 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5560 * words to contain special data values and no other data.
5561 */
5562 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5563 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5564 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5565 if (rc != MBX_SUCCESS) {
5566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5567 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5568 "status x%x\n",
5569 rc);
5570 rc = -EIO;
5571 }
5572
5573 mempool_free(mboxq, phba->mbox_mem_pool);
5574 return rc;
3772a991
JS
5575}
5576
5577/**
da0436e9 5578 * lpfc_sli4_queue_create - Create all the SLI4 queues
3772a991
JS
5579 * @phba: pointer to lpfc hba data structure.
5580 *
da0436e9
JS
5581 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5582 * operation. For each SLI4 queue type, the parameters such as queue entry
5583 * count (queue depth) shall be taken from the module parameter. For now,
5584 * we just use some constant number as place holder.
5585 *
5586 * Return codes
af901ca1 5587 * 0 - successful
da0436e9
JS
5588 * ENOMEM - No availble memory
5589 * EIO - The mailbox failed to complete successfully.
3772a991 5590 **/
da0436e9
JS
5591static int
5592lpfc_sli4_queue_create(struct lpfc_hba *phba)
3772a991 5593{
da0436e9
JS
5594 struct lpfc_queue *qdesc;
5595 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5596 int cfg_fcp_wq_count;
5597 int cfg_fcp_eq_count;
3772a991 5598
da0436e9
JS
5599 /*
5600 * Sanity check for confiugred queue parameters against the run-time
5601 * device parameters
5602 */
3772a991 5603
da0436e9
JS
5604 /* Sanity check on FCP fast-path WQ parameters */
5605 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5606 if (cfg_fcp_wq_count >
5607 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5608 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5609 LPFC_SP_WQN_DEF;
5610 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5612 "2581 Not enough WQs (%d) from "
5613 "the pci function for supporting "
5614 "FCP WQs (%d)\n",
5615 phba->sli4_hba.max_cfg_param.max_wq,
5616 phba->cfg_fcp_wq_count);
5617 goto out_error;
5618 }
5619 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5620 "2582 Not enough WQs (%d) from the pci "
5621 "function for supporting the requested "
5622 "FCP WQs (%d), the actual FCP WQs can "
5623 "be supported: %d\n",
5624 phba->sli4_hba.max_cfg_param.max_wq,
5625 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5626 }
5627 /* The actual number of FCP work queues adopted */
5628 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5629
5630 /* Sanity check on FCP fast-path EQ parameters */
5631 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5632 if (cfg_fcp_eq_count >
5633 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5634 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5635 LPFC_SP_EQN_DEF;
5636 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5638 "2574 Not enough EQs (%d) from the "
5639 "pci function for supporting FCP "
5640 "EQs (%d)\n",
5641 phba->sli4_hba.max_cfg_param.max_eq,
5642 phba->cfg_fcp_eq_count);
5643 goto out_error;
5644 }
5645 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5646 "2575 Not enough EQs (%d) from the pci "
5647 "function for supporting the requested "
5648 "FCP EQs (%d), the actual FCP EQs can "
5649 "be supported: %d\n",
5650 phba->sli4_hba.max_cfg_param.max_eq,
5651 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5652 }
5653 /* It does not make sense to have more EQs than WQs */
5654 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5655 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6a9c52cf
JS
5656 "2593 The FCP EQ count(%d) cannot be greater "
5657 "than the FCP WQ count(%d), limiting the "
5658 "FCP EQ count to %d\n", cfg_fcp_eq_count,
da0436e9
JS
5659 phba->cfg_fcp_wq_count,
5660 phba->cfg_fcp_wq_count);
5661 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5662 }
5663 /* The actual number of FCP event queues adopted */
5664 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5665 /* The overall number of event queues used */
5666 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
3772a991 5667
da0436e9
JS
5668 /*
5669 * Create Event Queues (EQs)
5670 */
3772a991 5671
da0436e9
JS
5672 /* Get EQ depth from module parameter, fake the default for now */
5673 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5674 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
3772a991 5675
da0436e9
JS
5676 /* Create slow path event queue */
5677 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5678 phba->sli4_hba.eq_ecount);
5679 if (!qdesc) {
5680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5681 "0496 Failed allocate slow-path EQ\n");
5682 goto out_error;
5683 }
5684 phba->sli4_hba.sp_eq = qdesc;
5685
5686 /* Create fast-path FCP Event Queue(s) */
5687 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5688 phba->cfg_fcp_eq_count), GFP_KERNEL);
5689 if (!phba->sli4_hba.fp_eq) {
5690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5691 "2576 Failed allocate memory for fast-path "
5692 "EQ record array\n");
5693 goto out_free_sp_eq;
5694 }
5695 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5696 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5697 phba->sli4_hba.eq_ecount);
5698 if (!qdesc) {
5699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5700 "0497 Failed allocate fast-path EQ\n");
5701 goto out_free_fp_eq;
5702 }
5703 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5704 }
5705
5706 /*
5707 * Create Complete Queues (CQs)
5708 */
5709
5710 /* Get CQ depth from module parameter, fake the default for now */
5711 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5712 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5713
5714 /* Create slow-path Mailbox Command Complete Queue */
5715 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5716 phba->sli4_hba.cq_ecount);
5717 if (!qdesc) {
5718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5719 "0500 Failed allocate slow-path mailbox CQ\n");
5720 goto out_free_fp_eq;
5721 }
5722 phba->sli4_hba.mbx_cq = qdesc;
5723
5724 /* Create slow-path ELS Complete Queue */
5725 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5726 phba->sli4_hba.cq_ecount);
5727 if (!qdesc) {
5728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5729 "0501 Failed allocate slow-path ELS CQ\n");
5730 goto out_free_mbx_cq;
5731 }
5732 phba->sli4_hba.els_cq = qdesc;
5733
da0436e9
JS
5734
5735 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5736 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5737 phba->cfg_fcp_eq_count), GFP_KERNEL);
5738 if (!phba->sli4_hba.fcp_cq) {
5739 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5740 "2577 Failed allocate memory for fast-path "
5741 "CQ record array\n");
4d9ab994 5742 goto out_free_els_cq;
da0436e9
JS
5743 }
5744 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5745 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5746 phba->sli4_hba.cq_ecount);
5747 if (!qdesc) {
5748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5749 "0499 Failed allocate fast-path FCP "
5750 "CQ (%d)\n", fcp_cqidx);
5751 goto out_free_fcp_cq;
5752 }
5753 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5754 }
5755
5756 /* Create Mailbox Command Queue */
5757 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5758 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5759
5760 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5761 phba->sli4_hba.mq_ecount);
5762 if (!qdesc) {
5763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5764 "0505 Failed allocate slow-path MQ\n");
5765 goto out_free_fcp_cq;
5766 }
5767 phba->sli4_hba.mbx_wq = qdesc;
5768
5769 /*
5770 * Create all the Work Queues (WQs)
5771 */
5772 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5773 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5774
5775 /* Create slow-path ELS Work Queue */
5776 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5777 phba->sli4_hba.wq_ecount);
5778 if (!qdesc) {
5779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5780 "0504 Failed allocate slow-path ELS WQ\n");
5781 goto out_free_mbx_wq;
5782 }
5783 phba->sli4_hba.els_wq = qdesc;
5784
5785 /* Create fast-path FCP Work Queue(s) */
5786 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5787 phba->cfg_fcp_wq_count), GFP_KERNEL);
5788 if (!phba->sli4_hba.fcp_wq) {
5789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5790 "2578 Failed allocate memory for fast-path "
5791 "WQ record array\n");
5792 goto out_free_els_wq;
5793 }
5794 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5795 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5796 phba->sli4_hba.wq_ecount);
5797 if (!qdesc) {
5798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5799 "0503 Failed allocate fast-path FCP "
5800 "WQ (%d)\n", fcp_wqidx);
5801 goto out_free_fcp_wq;
5802 }
5803 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5804 }
5805
5806 /*
5807 * Create Receive Queue (RQ)
5808 */
5809 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5810 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5811
5812 /* Create Receive Queue for header */
5813 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5814 phba->sli4_hba.rq_ecount);
5815 if (!qdesc) {
5816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5817 "0506 Failed allocate receive HRQ\n");
5818 goto out_free_fcp_wq;
5819 }
5820 phba->sli4_hba.hdr_rq = qdesc;
5821
5822 /* Create Receive Queue for data */
5823 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5824 phba->sli4_hba.rq_ecount);
5825 if (!qdesc) {
5826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5827 "0507 Failed allocate receive DRQ\n");
5828 goto out_free_hdr_rq;
5829 }
5830 phba->sli4_hba.dat_rq = qdesc;
5831
5832 return 0;
5833
5834out_free_hdr_rq:
5835 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5836 phba->sli4_hba.hdr_rq = NULL;
5837out_free_fcp_wq:
5838 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5839 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5840 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5841 }
5842 kfree(phba->sli4_hba.fcp_wq);
5843out_free_els_wq:
5844 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5845 phba->sli4_hba.els_wq = NULL;
5846out_free_mbx_wq:
5847 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5848 phba->sli4_hba.mbx_wq = NULL;
5849out_free_fcp_cq:
5850 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5851 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5852 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5853 }
5854 kfree(phba->sli4_hba.fcp_cq);
da0436e9
JS
5855out_free_els_cq:
5856 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5857 phba->sli4_hba.els_cq = NULL;
5858out_free_mbx_cq:
5859 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5860 phba->sli4_hba.mbx_cq = NULL;
5861out_free_fp_eq:
5862 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5863 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5864 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5865 }
5866 kfree(phba->sli4_hba.fp_eq);
5867out_free_sp_eq:
5868 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5869 phba->sli4_hba.sp_eq = NULL;
5870out_error:
5871 return -ENOMEM;
5872}
5873
5874/**
5875 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5876 * @phba: pointer to lpfc hba data structure.
5877 *
5878 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5879 * operation.
5880 *
5881 * Return codes
af901ca1 5882 * 0 - successful
da0436e9
JS
5883 * ENOMEM - No availble memory
5884 * EIO - The mailbox failed to complete successfully.
5885 **/
5886static void
5887lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5888{
5889 int fcp_qidx;
5890
5891 /* Release mailbox command work queue */
5892 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5893 phba->sli4_hba.mbx_wq = NULL;
5894
5895 /* Release ELS work queue */
5896 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5897 phba->sli4_hba.els_wq = NULL;
5898
5899 /* Release FCP work queue */
5900 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5901 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5902 kfree(phba->sli4_hba.fcp_wq);
5903 phba->sli4_hba.fcp_wq = NULL;
5904
5905 /* Release unsolicited receive queue */
5906 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5907 phba->sli4_hba.hdr_rq = NULL;
5908 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5909 phba->sli4_hba.dat_rq = NULL;
5910
da0436e9
JS
5911 /* Release ELS complete queue */
5912 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5913 phba->sli4_hba.els_cq = NULL;
5914
5915 /* Release mailbox command complete queue */
5916 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5917 phba->sli4_hba.mbx_cq = NULL;
5918
5919 /* Release FCP response complete queue */
5920 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5921 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5922 kfree(phba->sli4_hba.fcp_cq);
5923 phba->sli4_hba.fcp_cq = NULL;
5924
5925 /* Release fast-path event queue */
5926 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5927 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5928 kfree(phba->sli4_hba.fp_eq);
5929 phba->sli4_hba.fp_eq = NULL;
5930
5931 /* Release slow-path event queue */
5932 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5933 phba->sli4_hba.sp_eq = NULL;
5934
5935 return;
5936}
5937
5938/**
5939 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5940 * @phba: pointer to lpfc hba data structure.
5941 *
5942 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5943 * operation.
5944 *
5945 * Return codes
af901ca1 5946 * 0 - successful
da0436e9
JS
5947 * ENOMEM - No availble memory
5948 * EIO - The mailbox failed to complete successfully.
5949 **/
5950int
5951lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5952{
5953 int rc = -ENOMEM;
5954 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5955 int fcp_cq_index = 0;
5956
5957 /*
5958 * Set up Event Queues (EQs)
5959 */
5960
5961 /* Set up slow-path event queue */
5962 if (!phba->sli4_hba.sp_eq) {
5963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5964 "0520 Slow-path EQ not allocated\n");
5965 goto out_error;
5966 }
5967 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5968 LPFC_SP_DEF_IMAX);
5969 if (rc) {
5970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5971 "0521 Failed setup of slow-path EQ: "
5972 "rc = 0x%x\n", rc);
5973 goto out_error;
5974 }
5975 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5976 "2583 Slow-path EQ setup: queue-id=%d\n",
5977 phba->sli4_hba.sp_eq->queue_id);
5978
5979 /* Set up fast-path event queue */
5980 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5981 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5983 "0522 Fast-path EQ (%d) not "
5984 "allocated\n", fcp_eqidx);
5985 goto out_destroy_fp_eq;
5986 }
5987 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5988 phba->cfg_fcp_imax);
5989 if (rc) {
5990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5991 "0523 Failed setup of fast-path EQ "
5992 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5993 goto out_destroy_fp_eq;
5994 }
5995 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5996 "2584 Fast-path EQ setup: "
5997 "queue[%d]-id=%d\n", fcp_eqidx,
5998 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5999 }
6000
6001 /*
6002 * Set up Complete Queues (CQs)
6003 */
6004
6005 /* Set up slow-path MBOX Complete Queue as the first CQ */
6006 if (!phba->sli4_hba.mbx_cq) {
6007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6008 "0528 Mailbox CQ not allocated\n");
6009 goto out_destroy_fp_eq;
6010 }
6011 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6012 LPFC_MCQ, LPFC_MBOX);
6013 if (rc) {
6014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6015 "0529 Failed setup of slow-path mailbox CQ: "
6016 "rc = 0x%x\n", rc);
6017 goto out_destroy_fp_eq;
6018 }
6019 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6020 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6021 phba->sli4_hba.mbx_cq->queue_id,
6022 phba->sli4_hba.sp_eq->queue_id);
6023
6024 /* Set up slow-path ELS Complete Queue */
6025 if (!phba->sli4_hba.els_cq) {
6026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6027 "0530 ELS CQ not allocated\n");
6028 goto out_destroy_mbx_cq;
6029 }
6030 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6031 LPFC_WCQ, LPFC_ELS);
6032 if (rc) {
6033 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6034 "0531 Failed setup of slow-path ELS CQ: "
6035 "rc = 0x%x\n", rc);
6036 goto out_destroy_mbx_cq;
6037 }
6038 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6039 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6040 phba->sli4_hba.els_cq->queue_id,
6041 phba->sli4_hba.sp_eq->queue_id);
6042
da0436e9
JS
6043 /* Set up fast-path FCP Response Complete Queue */
6044 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6045 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6046 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6047 "0526 Fast-path FCP CQ (%d) not "
6048 "allocated\n", fcp_cqidx);
6049 goto out_destroy_fcp_cq;
6050 }
6051 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
6052 phba->sli4_hba.fp_eq[fcp_cqidx],
6053 LPFC_WCQ, LPFC_FCP);
6054 if (rc) {
6055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6056 "0527 Failed setup of fast-path FCP "
6057 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6058 goto out_destroy_fcp_cq;
6059 }
6060 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6061 "2588 FCP CQ setup: cq[%d]-id=%d, "
6062 "parent eq[%d]-id=%d\n",
6063 fcp_cqidx,
6064 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6065 fcp_cqidx,
6066 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
6067 }
6068
6069 /*
6070 * Set up all the Work Queues (WQs)
6071 */
6072
6073 /* Set up Mailbox Command Queue */
6074 if (!phba->sli4_hba.mbx_wq) {
6075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6076 "0538 Slow-path MQ not allocated\n");
6077 goto out_destroy_fcp_cq;
6078 }
6079 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6080 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6081 if (rc) {
6082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6083 "0539 Failed setup of slow-path MQ: "
6084 "rc = 0x%x\n", rc);
6085 goto out_destroy_fcp_cq;
6086 }
6087 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6088 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6089 phba->sli4_hba.mbx_wq->queue_id,
6090 phba->sli4_hba.mbx_cq->queue_id);
6091
6092 /* Set up slow-path ELS Work Queue */
6093 if (!phba->sli4_hba.els_wq) {
6094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6095 "0536 Slow-path ELS WQ not allocated\n");
6096 goto out_destroy_mbx_wq;
6097 }
6098 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6099 phba->sli4_hba.els_cq, LPFC_ELS);
6100 if (rc) {
6101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6102 "0537 Failed setup of slow-path ELS WQ: "
6103 "rc = 0x%x\n", rc);
6104 goto out_destroy_mbx_wq;
6105 }
6106 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6107 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6108 phba->sli4_hba.els_wq->queue_id,
6109 phba->sli4_hba.els_cq->queue_id);
6110
6111 /* Set up fast-path FCP Work Queue */
6112 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6113 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6115 "0534 Fast-path FCP WQ (%d) not "
6116 "allocated\n", fcp_wqidx);
6117 goto out_destroy_fcp_wq;
6118 }
6119 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6120 phba->sli4_hba.fcp_cq[fcp_cq_index],
6121 LPFC_FCP);
6122 if (rc) {
6123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6124 "0535 Failed setup of fast-path FCP "
6125 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6126 goto out_destroy_fcp_wq;
6127 }
6128 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6129 "2591 FCP WQ setup: wq[%d]-id=%d, "
6130 "parent cq[%d]-id=%d\n",
6131 fcp_wqidx,
6132 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6133 fcp_cq_index,
6134 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6135 /* Round robin FCP Work Queue's Completion Queue assignment */
6136 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
6137 }
6138
6139 /*
6140 * Create Receive Queue (RQ)
6141 */
6142 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6144 "0540 Receive Queue not allocated\n");
6145 goto out_destroy_fcp_wq;
6146 }
6147 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
4d9ab994 6148 phba->sli4_hba.els_cq, LPFC_USOL);
da0436e9
JS
6149 if (rc) {
6150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6151 "0541 Failed setup of Receive Queue: "
6152 "rc = 0x%x\n", rc);
6153 goto out_destroy_fcp_wq;
6154 }
6155 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6156 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6157 "parent cq-id=%d\n",
6158 phba->sli4_hba.hdr_rq->queue_id,
6159 phba->sli4_hba.dat_rq->queue_id,
4d9ab994 6160 phba->sli4_hba.els_cq->queue_id);
da0436e9
JS
6161 return 0;
6162
6163out_destroy_fcp_wq:
6164 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6165 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6166 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6167out_destroy_mbx_wq:
6168 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6169out_destroy_fcp_cq:
6170 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6171 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
da0436e9
JS
6172 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6173out_destroy_mbx_cq:
6174 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6175out_destroy_fp_eq:
6176 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6177 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6178 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6179out_error:
6180 return rc;
6181}
6182
6183/**
6184 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6185 * @phba: pointer to lpfc hba data structure.
6186 *
6187 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6188 * operation.
6189 *
6190 * Return codes
af901ca1 6191 * 0 - successful
da0436e9
JS
6192 * ENOMEM - No availble memory
6193 * EIO - The mailbox failed to complete successfully.
6194 **/
6195void
6196lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6197{
6198 int fcp_qidx;
6199
6200 /* Unset mailbox command work queue */
6201 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6202 /* Unset ELS work queue */
6203 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6204 /* Unset unsolicited receive queue */
6205 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6206 /* Unset FCP work queue */
6207 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6208 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6209 /* Unset mailbox command complete queue */
6210 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6211 /* Unset ELS complete queue */
6212 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
da0436e9
JS
6213 /* Unset FCP response complete queue */
6214 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6215 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6216 /* Unset fast-path event queue */
6217 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6218 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6219 /* Unset slow-path event queue */
6220 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6221}
6222
6223/**
6224 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6225 * @phba: pointer to lpfc hba data structure.
6226 *
6227 * This routine is invoked to allocate and set up a pool of completion queue
6228 * events. The body of the completion queue event is a completion queue entry
6229 * CQE. For now, this pool is used for the interrupt service routine to queue
6230 * the following HBA completion queue events for the worker thread to process:
6231 * - Mailbox asynchronous events
6232 * - Receive queue completion unsolicited events
6233 * Later, this can be used for all the slow-path events.
6234 *
6235 * Return codes
af901ca1 6236 * 0 - successful
da0436e9
JS
6237 * -ENOMEM - No availble memory
6238 **/
6239static int
6240lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6241{
6242 struct lpfc_cq_event *cq_event;
6243 int i;
6244
6245 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6246 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6247 if (!cq_event)
6248 goto out_pool_create_fail;
6249 list_add_tail(&cq_event->list,
6250 &phba->sli4_hba.sp_cqe_event_pool);
6251 }
6252 return 0;
6253
6254out_pool_create_fail:
6255 lpfc_sli4_cq_event_pool_destroy(phba);
6256 return -ENOMEM;
6257}
6258
6259/**
6260 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6261 * @phba: pointer to lpfc hba data structure.
6262 *
6263 * This routine is invoked to free the pool of completion queue events at
6264 * driver unload time. Note that, it is the responsibility of the driver
6265 * cleanup routine to free all the outstanding completion-queue events
6266 * allocated from this pool back into the pool before invoking this routine
6267 * to destroy the pool.
6268 **/
6269static void
6270lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6271{
6272 struct lpfc_cq_event *cq_event, *next_cq_event;
6273
6274 list_for_each_entry_safe(cq_event, next_cq_event,
6275 &phba->sli4_hba.sp_cqe_event_pool, list) {
6276 list_del(&cq_event->list);
6277 kfree(cq_event);
6278 }
6279}
6280
6281/**
6282 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6283 * @phba: pointer to lpfc hba data structure.
6284 *
6285 * This routine is the lock free version of the API invoked to allocate a
6286 * completion-queue event from the free pool.
6287 *
6288 * Return: Pointer to the newly allocated completion-queue event if successful
6289 * NULL otherwise.
6290 **/
6291struct lpfc_cq_event *
6292__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6293{
6294 struct lpfc_cq_event *cq_event = NULL;
6295
6296 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6297 struct lpfc_cq_event, list);
6298 return cq_event;
6299}
6300
6301/**
6302 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6303 * @phba: pointer to lpfc hba data structure.
6304 *
6305 * This routine is the lock version of the API invoked to allocate a
6306 * completion-queue event from the free pool.
6307 *
6308 * Return: Pointer to the newly allocated completion-queue event if successful
6309 * NULL otherwise.
6310 **/
6311struct lpfc_cq_event *
6312lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6313{
6314 struct lpfc_cq_event *cq_event;
6315 unsigned long iflags;
6316
6317 spin_lock_irqsave(&phba->hbalock, iflags);
6318 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6319 spin_unlock_irqrestore(&phba->hbalock, iflags);
6320 return cq_event;
6321}
6322
6323/**
6324 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6325 * @phba: pointer to lpfc hba data structure.
6326 * @cq_event: pointer to the completion queue event to be freed.
6327 *
6328 * This routine is the lock free version of the API invoked to release a
6329 * completion-queue event back into the free pool.
6330 **/
6331void
6332__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6333 struct lpfc_cq_event *cq_event)
6334{
6335 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6336}
6337
6338/**
6339 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6340 * @phba: pointer to lpfc hba data structure.
6341 * @cq_event: pointer to the completion queue event to be freed.
6342 *
6343 * This routine is the lock version of the API invoked to release a
6344 * completion-queue event back into the free pool.
6345 **/
6346void
6347lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6348 struct lpfc_cq_event *cq_event)
6349{
6350 unsigned long iflags;
6351 spin_lock_irqsave(&phba->hbalock, iflags);
6352 __lpfc_sli4_cq_event_release(phba, cq_event);
6353 spin_unlock_irqrestore(&phba->hbalock, iflags);
6354}
6355
6356/**
6357 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6358 * @phba: pointer to lpfc hba data structure.
6359 *
6360 * This routine is to free all the pending completion-queue events to the
6361 * back into the free pool for device reset.
6362 **/
6363static void
6364lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6365{
6366 LIST_HEAD(cqelist);
6367 struct lpfc_cq_event *cqe;
6368 unsigned long iflags;
6369
6370 /* Retrieve all the pending WCQEs from pending WCQE lists */
6371 spin_lock_irqsave(&phba->hbalock, iflags);
6372 /* Pending FCP XRI abort events */
6373 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6374 &cqelist);
6375 /* Pending ELS XRI abort events */
6376 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6377 &cqelist);
6378 /* Pending asynnc events */
6379 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6380 &cqelist);
6381 spin_unlock_irqrestore(&phba->hbalock, iflags);
6382
6383 while (!list_empty(&cqelist)) {
6384 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6385 lpfc_sli4_cq_event_release(phba, cqe);
6386 }
6387}
6388
6389/**
6390 * lpfc_pci_function_reset - Reset pci function.
6391 * @phba: pointer to lpfc hba data structure.
6392 *
6393 * This routine is invoked to request a PCI function reset. It will destroys
6394 * all resources assigned to the PCI function which originates this request.
6395 *
6396 * Return codes
af901ca1 6397 * 0 - successful
da0436e9
JS
6398 * ENOMEM - No availble memory
6399 * EIO - The mailbox failed to complete successfully.
6400 **/
6401int
6402lpfc_pci_function_reset(struct lpfc_hba *phba)
6403{
6404 LPFC_MBOXQ_t *mboxq;
6405 uint32_t rc = 0;
6406 uint32_t shdr_status, shdr_add_status;
6407 union lpfc_sli4_cfg_shdr *shdr;
6408
6409 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6410 if (!mboxq) {
6411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6412 "0494 Unable to allocate memory for issuing "
6413 "SLI_FUNCTION_RESET mailbox command\n");
6414 return -ENOMEM;
6415 }
6416
6417 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6418 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6419 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6420 LPFC_SLI4_MBX_EMBED);
6421 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6422 shdr = (union lpfc_sli4_cfg_shdr *)
6423 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6424 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6425 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6426 if (rc != MBX_TIMEOUT)
6427 mempool_free(mboxq, phba->mbox_mem_pool);
6428 if (shdr_status || shdr_add_status || rc) {
6429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6430 "0495 SLI_FUNCTION_RESET mailbox failed with "
6431 "status x%x add_status x%x, mbx status x%x\n",
6432 shdr_status, shdr_add_status, rc);
6433 rc = -ENXIO;
6434 }
6435 return rc;
6436}
6437
6438/**
6439 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6440 * @phba: pointer to lpfc hba data structure.
6441 * @cnt: number of nop mailbox commands to send.
6442 *
6443 * This routine is invoked to send a number @cnt of NOP mailbox command and
6444 * wait for each command to complete.
6445 *
6446 * Return: the number of NOP mailbox command completed.
6447 **/
6448static int
6449lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6450{
6451 LPFC_MBOXQ_t *mboxq;
6452 int length, cmdsent;
6453 uint32_t mbox_tmo;
6454 uint32_t rc = 0;
6455 uint32_t shdr_status, shdr_add_status;
6456 union lpfc_sli4_cfg_shdr *shdr;
6457
6458 if (cnt == 0) {
6459 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6460 "2518 Requested to send 0 NOP mailbox cmd\n");
6461 return cnt;
6462 }
6463
6464 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6465 if (!mboxq) {
6466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6467 "2519 Unable to allocate memory for issuing "
6468 "NOP mailbox command\n");
6469 return 0;
6470 }
6471
6472 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6473 length = (sizeof(struct lpfc_mbx_nop) -
6474 sizeof(struct lpfc_sli4_cfg_mhdr));
6475 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6476 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6477
6478 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6479 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6480 if (!phba->sli4_hba.intr_enable)
6481 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6482 else
6483 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6484 if (rc == MBX_TIMEOUT)
6485 break;
6486 /* Check return status */
6487 shdr = (union lpfc_sli4_cfg_shdr *)
6488 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6489 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6490 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6491 &shdr->response);
6492 if (shdr_status || shdr_add_status || rc) {
6493 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6494 "2520 NOP mailbox command failed "
6495 "status x%x add_status x%x mbx "
6496 "status x%x\n", shdr_status,
6497 shdr_add_status, rc);
6498 break;
6499 }
6500 }
6501
6502 if (rc != MBX_TIMEOUT)
6503 mempool_free(mboxq, phba->mbox_mem_pool);
6504
6505 return cmdsent;
6506}
6507
6508/**
6509 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6510 * @phba: pointer to lpfc hba data structure.
6511 * @fcfi: fcf index.
6512 *
6513 * This routine is invoked to unregister a FCFI from device.
6514 **/
6515void
6516lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6517{
6518 LPFC_MBOXQ_t *mbox;
6519 uint32_t mbox_tmo;
6520 int rc;
6521 unsigned long flags;
6522
6523 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6524
6525 if (!mbox)
6526 return;
6527
6528 lpfc_unreg_fcfi(mbox, fcfi);
6529
6530 if (!phba->sli4_hba.intr_enable)
6531 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6532 else {
6533 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6534 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6535 }
6536 if (rc != MBX_TIMEOUT)
6537 mempool_free(mbox, phba->mbox_mem_pool);
6538 if (rc != MBX_SUCCESS)
6539 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6540 "2517 Unregister FCFI command failed "
6541 "status %d, mbxStatus x%x\n", rc,
6542 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6543 else {
6544 spin_lock_irqsave(&phba->hbalock, flags);
6545 /* Mark the FCFI is no longer registered */
6546 phba->fcf.fcf_flag &=
ecfd03c6 6547 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
da0436e9
JS
6548 spin_unlock_irqrestore(&phba->hbalock, flags);
6549 }
6550}
6551
6552/**
6553 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6554 * @phba: pointer to lpfc hba data structure.
6555 *
6556 * This routine is invoked to set up the PCI device memory space for device
6557 * with SLI-4 interface spec.
6558 *
6559 * Return codes
af901ca1 6560 * 0 - successful
da0436e9
JS
6561 * other values - error
6562 **/
6563static int
6564lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6565{
6566 struct pci_dev *pdev;
6567 unsigned long bar0map_len, bar1map_len, bar2map_len;
6568 int error = -ENODEV;
6569
6570 /* Obtain PCI device reference */
6571 if (!phba->pcidev)
6572 return error;
6573 else
6574 pdev = phba->pcidev;
6575
6576 /* Set the device DMA mask size */
8e68597d
MR
6577 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6578 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6579 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6580 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
da0436e9 6581 return error;
8e68597d
MR
6582 }
6583 }
da0436e9
JS
6584
6585 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6586 * number of bytes required by each mapping. They are actually
1dfb5a47 6587 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
da0436e9 6588 */
1dfb5a47
JS
6589 if (pci_resource_start(pdev, 0)) {
6590 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6591 bar0map_len = pci_resource_len(pdev, 0);
6592 } else {
6593 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6594 bar0map_len = pci_resource_len(pdev, 1);
6595 }
6596 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6597 bar1map_len = pci_resource_len(pdev, 2);
da0436e9 6598
1dfb5a47
JS
6599 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6600 bar2map_len = pci_resource_len(pdev, 4);
da0436e9
JS
6601
6602 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6603 phba->sli4_hba.conf_regs_memmap_p =
6604 ioremap(phba->pci_bar0_map, bar0map_len);
6605 if (!phba->sli4_hba.conf_regs_memmap_p) {
6606 dev_printk(KERN_ERR, &pdev->dev,
6607 "ioremap failed for SLI4 PCI config registers.\n");
6608 goto out;
6609 }
6610
6611 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6612 phba->sli4_hba.ctrl_regs_memmap_p =
6613 ioremap(phba->pci_bar1_map, bar1map_len);
6614 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6615 dev_printk(KERN_ERR, &pdev->dev,
6616 "ioremap failed for SLI4 HBA control registers.\n");
6617 goto out_iounmap_conf;
6618 }
6619
6620 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6621 phba->sli4_hba.drbl_regs_memmap_p =
6622 ioremap(phba->pci_bar2_map, bar2map_len);
6623 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6624 dev_printk(KERN_ERR, &pdev->dev,
6625 "ioremap failed for SLI4 HBA doorbell registers.\n");
6626 goto out_iounmap_ctrl;
6627 }
6628
6629 /* Set up BAR0 PCI config space register memory map */
6630 lpfc_sli4_bar0_register_memmap(phba);
6631
6632 /* Set up BAR1 register memory map */
6633 lpfc_sli4_bar1_register_memmap(phba);
6634
6635 /* Set up BAR2 register memory map */
6636 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6637 if (error)
6638 goto out_iounmap_all;
6639
6640 return 0;
6641
6642out_iounmap_all:
6643 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6644out_iounmap_ctrl:
6645 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6646out_iounmap_conf:
6647 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6648out:
6649 return error;
6650}
6651
6652/**
6653 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6654 * @phba: pointer to lpfc hba data structure.
6655 *
6656 * This routine is invoked to unset the PCI device memory space for device
6657 * with SLI-4 interface spec.
6658 **/
6659static void
6660lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6661{
6662 struct pci_dev *pdev;
6663
6664 /* Obtain PCI device reference */
6665 if (!phba->pcidev)
6666 return;
6667 else
6668 pdev = phba->pcidev;
6669
6670 /* Free coherent DMA memory allocated */
6671
6672 /* Unmap I/O memory space */
6673 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6674 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6675 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6676
6677 return;
6678}
6679
6680/**
6681 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6682 * @phba: pointer to lpfc hba data structure.
6683 *
6684 * This routine is invoked to enable the MSI-X interrupt vectors to device
6685 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6686 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6687 * invoked, enables either all or nothing, depending on the current
6688 * availability of PCI vector resources. The device driver is responsible
6689 * for calling the individual request_irq() to register each MSI-X vector
6690 * with a interrupt handler, which is done in this function. Note that
6691 * later when device is unloading, the driver should always call free_irq()
6692 * on all MSI-X vectors it has done request_irq() on before calling
6693 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6694 * will be left with MSI-X enabled and leaks its vectors.
6695 *
6696 * Return codes
af901ca1 6697 * 0 - successful
da0436e9
JS
6698 * other values - error
6699 **/
6700static int
6701lpfc_sli_enable_msix(struct lpfc_hba *phba)
6702{
6703 int rc, i;
6704 LPFC_MBOXQ_t *pmb;
6705
6706 /* Set up MSI-X multi-message vectors */
6707 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6708 phba->msix_entries[i].entry = i;
6709
6710 /* Configure MSI-X capability structure */
6711 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6712 ARRAY_SIZE(phba->msix_entries));
6713 if (rc) {
6714 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6715 "0420 PCI enable MSI-X failed (%d)\n", rc);
6716 goto msi_fail_out;
6717 }
6718 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6719 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6720 "0477 MSI-X entry[%d]: vector=x%x "
6721 "message=%d\n", i,
6722 phba->msix_entries[i].vector,
6723 phba->msix_entries[i].entry);
6724 /*
6725 * Assign MSI-X vectors to interrupt handlers
6726 */
6727
6728 /* vector-0 is associated to slow-path handler */
6729 rc = request_irq(phba->msix_entries[0].vector,
6730 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6731 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6732 if (rc) {
6733 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6734 "0421 MSI-X slow-path request_irq failed "
6735 "(%d)\n", rc);
6736 goto msi_fail_out;
6737 }
6738
6739 /* vector-1 is associated to fast-path handler */
6740 rc = request_irq(phba->msix_entries[1].vector,
6741 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6742 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6743
6744 if (rc) {
6745 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6746 "0429 MSI-X fast-path request_irq failed "
6747 "(%d)\n", rc);
6748 goto irq_fail_out;
6749 }
6750
6751 /*
6752 * Configure HBA MSI-X attention conditions to messages
6753 */
6754 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6755
6756 if (!pmb) {
6757 rc = -ENOMEM;
6758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6759 "0474 Unable to allocate memory for issuing "
6760 "MBOX_CONFIG_MSI command\n");
6761 goto mem_fail_out;
6762 }
6763 rc = lpfc_config_msi(phba, pmb);
6764 if (rc)
6765 goto mbx_fail_out;
6766 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6767 if (rc != MBX_SUCCESS) {
6768 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6769 "0351 Config MSI mailbox command failed, "
6770 "mbxCmd x%x, mbxStatus x%x\n",
6771 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6772 goto mbx_fail_out;
6773 }
6774
6775 /* Free memory allocated for mailbox command */
6776 mempool_free(pmb, phba->mbox_mem_pool);
6777 return rc;
6778
6779mbx_fail_out:
6780 /* Free memory allocated for mailbox command */
6781 mempool_free(pmb, phba->mbox_mem_pool);
6782
6783mem_fail_out:
6784 /* free the irq already requested */
6785 free_irq(phba->msix_entries[1].vector, phba);
6786
6787irq_fail_out:
6788 /* free the irq already requested */
6789 free_irq(phba->msix_entries[0].vector, phba);
6790
6791msi_fail_out:
6792 /* Unconfigure MSI-X capability structure */
6793 pci_disable_msix(phba->pcidev);
6794 return rc;
6795}
6796
6797/**
6798 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6799 * @phba: pointer to lpfc hba data structure.
6800 *
6801 * This routine is invoked to release the MSI-X vectors and then disable the
6802 * MSI-X interrupt mode to device with SLI-3 interface spec.
6803 **/
6804static void
6805lpfc_sli_disable_msix(struct lpfc_hba *phba)
6806{
6807 int i;
6808
6809 /* Free up MSI-X multi-message vectors */
6810 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6811 free_irq(phba->msix_entries[i].vector, phba);
6812 /* Disable MSI-X */
6813 pci_disable_msix(phba->pcidev);
6814
6815 return;
6816}
6817
6818/**
6819 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6820 * @phba: pointer to lpfc hba data structure.
6821 *
6822 * This routine is invoked to enable the MSI interrupt mode to device with
6823 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6824 * enable the MSI vector. The device driver is responsible for calling the
6825 * request_irq() to register MSI vector with a interrupt the handler, which
6826 * is done in this function.
6827 *
6828 * Return codes
af901ca1 6829 * 0 - successful
da0436e9
JS
6830 * other values - error
6831 */
6832static int
6833lpfc_sli_enable_msi(struct lpfc_hba *phba)
6834{
6835 int rc;
6836
6837 rc = pci_enable_msi(phba->pcidev);
6838 if (!rc)
6839 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6840 "0462 PCI enable MSI mode success.\n");
6841 else {
6842 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6843 "0471 PCI enable MSI mode failed (%d)\n", rc);
6844 return rc;
6845 }
6846
6847 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6848 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6849 if (rc) {
6850 pci_disable_msi(phba->pcidev);
6851 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6852 "0478 MSI request_irq failed (%d)\n", rc);
6853 }
6854 return rc;
6855}
6856
6857/**
6858 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6859 * @phba: pointer to lpfc hba data structure.
6860 *
6861 * This routine is invoked to disable the MSI interrupt mode to device with
6862 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6863 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6864 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6865 * its vector.
6866 */
6867static void
6868lpfc_sli_disable_msi(struct lpfc_hba *phba)
6869{
6870 free_irq(phba->pcidev->irq, phba);
6871 pci_disable_msi(phba->pcidev);
6872 return;
6873}
6874
6875/**
6876 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6877 * @phba: pointer to lpfc hba data structure.
6878 *
6879 * This routine is invoked to enable device interrupt and associate driver's
6880 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6881 * spec. Depends on the interrupt mode configured to the driver, the driver
6882 * will try to fallback from the configured interrupt mode to an interrupt
6883 * mode which is supported by the platform, kernel, and device in the order
6884 * of:
6885 * MSI-X -> MSI -> IRQ.
6886 *
6887 * Return codes
af901ca1 6888 * 0 - successful
da0436e9
JS
6889 * other values - error
6890 **/
6891static uint32_t
6892lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6893{
6894 uint32_t intr_mode = LPFC_INTR_ERROR;
6895 int retval;
6896
6897 if (cfg_mode == 2) {
6898 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6899 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6900 if (!retval) {
6901 /* Now, try to enable MSI-X interrupt mode */
6902 retval = lpfc_sli_enable_msix(phba);
6903 if (!retval) {
6904 /* Indicate initialization to MSI-X mode */
6905 phba->intr_type = MSIX;
6906 intr_mode = 2;
6907 }
6908 }
6909 }
6910
6911 /* Fallback to MSI if MSI-X initialization failed */
6912 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6913 retval = lpfc_sli_enable_msi(phba);
6914 if (!retval) {
6915 /* Indicate initialization to MSI mode */
6916 phba->intr_type = MSI;
6917 intr_mode = 1;
6918 }
6919 }
6920
6921 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6922 if (phba->intr_type == NONE) {
6923 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6924 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6925 if (!retval) {
6926 /* Indicate initialization to INTx mode */
6927 phba->intr_type = INTx;
6928 intr_mode = 0;
6929 }
6930 }
6931 return intr_mode;
6932}
6933
6934/**
6935 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6936 * @phba: pointer to lpfc hba data structure.
6937 *
6938 * This routine is invoked to disable device interrupt and disassociate the
6939 * driver's interrupt handler(s) from interrupt vector(s) to device with
6940 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6941 * release the interrupt vector(s) for the message signaled interrupt.
6942 **/
6943static void
6944lpfc_sli_disable_intr(struct lpfc_hba *phba)
6945{
6946 /* Disable the currently initialized interrupt mode */
6947 if (phba->intr_type == MSIX)
6948 lpfc_sli_disable_msix(phba);
6949 else if (phba->intr_type == MSI)
6950 lpfc_sli_disable_msi(phba);
6951 else if (phba->intr_type == INTx)
6952 free_irq(phba->pcidev->irq, phba);
6953
6954 /* Reset interrupt management states */
6955 phba->intr_type = NONE;
6956 phba->sli.slistat.sli_intr = 0;
6957
6958 return;
6959}
6960
6961/**
6962 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6963 * @phba: pointer to lpfc hba data structure.
6964 *
6965 * This routine is invoked to enable the MSI-X interrupt vectors to device
6966 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6967 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6968 * enables either all or nothing, depending on the current availability of
6969 * PCI vector resources. The device driver is responsible for calling the
6970 * individual request_irq() to register each MSI-X vector with a interrupt
6971 * handler, which is done in this function. Note that later when device is
6972 * unloading, the driver should always call free_irq() on all MSI-X vectors
6973 * it has done request_irq() on before calling pci_disable_msix(). Failure
6974 * to do so results in a BUG_ON() and a device will be left with MSI-X
6975 * enabled and leaks its vectors.
6976 *
6977 * Return codes
af901ca1 6978 * 0 - successful
da0436e9
JS
6979 * other values - error
6980 **/
6981static int
6982lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6983{
6984 int rc, index;
6985
6986 /* Set up MSI-X multi-message vectors */
6987 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6988 phba->sli4_hba.msix_entries[index].entry = index;
6989
6990 /* Configure MSI-X capability structure */
6991 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6992 phba->sli4_hba.cfg_eqn);
6993 if (rc) {
6994 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6995 "0484 PCI enable MSI-X failed (%d)\n", rc);
6996 goto msi_fail_out;
6997 }
6998 /* Log MSI-X vector assignment */
6999 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7000 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7001 "0489 MSI-X entry[%d]: vector=x%x "
7002 "message=%d\n", index,
7003 phba->sli4_hba.msix_entries[index].vector,
7004 phba->sli4_hba.msix_entries[index].entry);
7005 /*
7006 * Assign MSI-X vectors to interrupt handlers
7007 */
7008
7009 /* The first vector must associated to slow-path handler for MQ */
7010 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7011 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7012 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7013 if (rc) {
7014 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7015 "0485 MSI-X slow-path request_irq failed "
7016 "(%d)\n", rc);
7017 goto msi_fail_out;
7018 }
7019
7020 /* The rest of the vector(s) are associated to fast-path handler(s) */
7021 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
7022 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7023 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7024 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7025 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7026 LPFC_FP_DRIVER_HANDLER_NAME,
7027 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7028 if (rc) {
7029 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7030 "0486 MSI-X fast-path (%d) "
7031 "request_irq failed (%d)\n", index, rc);
7032 goto cfg_fail_out;
7033 }
7034 }
7035
7036 return rc;
7037
7038cfg_fail_out:
7039 /* free the irq already requested */
7040 for (--index; index >= 1; index--)
7041 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7042 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7043
7044 /* free the irq already requested */
7045 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7046
7047msi_fail_out:
7048 /* Unconfigure MSI-X capability structure */
7049 pci_disable_msix(phba->pcidev);
7050 return rc;
7051}
7052
7053/**
7054 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7055 * @phba: pointer to lpfc hba data structure.
7056 *
7057 * This routine is invoked to release the MSI-X vectors and then disable the
7058 * MSI-X interrupt mode to device with SLI-4 interface spec.
7059 **/
7060static void
7061lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7062{
7063 int index;
7064
7065 /* Free up MSI-X multi-message vectors */
7066 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7067
7068 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
7069 free_irq(phba->sli4_hba.msix_entries[index].vector,
7070 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7071 /* Disable MSI-X */
7072 pci_disable_msix(phba->pcidev);
7073
7074 return;
7075}
7076
7077/**
7078 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7079 * @phba: pointer to lpfc hba data structure.
7080 *
7081 * This routine is invoked to enable the MSI interrupt mode to device with
7082 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7083 * to enable the MSI vector. The device driver is responsible for calling
7084 * the request_irq() to register MSI vector with a interrupt the handler,
7085 * which is done in this function.
7086 *
7087 * Return codes
af901ca1 7088 * 0 - successful
da0436e9
JS
7089 * other values - error
7090 **/
7091static int
7092lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7093{
7094 int rc, index;
7095
7096 rc = pci_enable_msi(phba->pcidev);
7097 if (!rc)
7098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7099 "0487 PCI enable MSI mode success.\n");
7100 else {
7101 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7102 "0488 PCI enable MSI mode failed (%d)\n", rc);
7103 return rc;
7104 }
7105
7106 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7107 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7108 if (rc) {
7109 pci_disable_msi(phba->pcidev);
7110 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7111 "0490 MSI request_irq failed (%d)\n", rc);
7112 }
7113
7114 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7115 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7116 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7117 }
7118
7119 return rc;
7120}
7121
7122/**
7123 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7124 * @phba: pointer to lpfc hba data structure.
7125 *
7126 * This routine is invoked to disable the MSI interrupt mode to device with
7127 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7128 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7129 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7130 * its vector.
7131 **/
7132static void
7133lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7134{
7135 free_irq(phba->pcidev->irq, phba);
7136 pci_disable_msi(phba->pcidev);
7137 return;
7138}
7139
7140/**
7141 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7142 * @phba: pointer to lpfc hba data structure.
7143 *
7144 * This routine is invoked to enable device interrupt and associate driver's
7145 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7146 * interface spec. Depends on the interrupt mode configured to the driver,
7147 * the driver will try to fallback from the configured interrupt mode to an
7148 * interrupt mode which is supported by the platform, kernel, and device in
7149 * the order of:
7150 * MSI-X -> MSI -> IRQ.
7151 *
7152 * Return codes
af901ca1 7153 * 0 - successful
da0436e9
JS
7154 * other values - error
7155 **/
7156static uint32_t
7157lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7158{
7159 uint32_t intr_mode = LPFC_INTR_ERROR;
7160 int retval, index;
7161
7162 if (cfg_mode == 2) {
7163 /* Preparation before conf_msi mbox cmd */
7164 retval = 0;
7165 if (!retval) {
7166 /* Now, try to enable MSI-X interrupt mode */
7167 retval = lpfc_sli4_enable_msix(phba);
7168 if (!retval) {
7169 /* Indicate initialization to MSI-X mode */
7170 phba->intr_type = MSIX;
7171 intr_mode = 2;
7172 }
7173 }
7174 }
7175
7176 /* Fallback to MSI if MSI-X initialization failed */
7177 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7178 retval = lpfc_sli4_enable_msi(phba);
7179 if (!retval) {
7180 /* Indicate initialization to MSI mode */
7181 phba->intr_type = MSI;
7182 intr_mode = 1;
7183 }
7184 }
7185
7186 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7187 if (phba->intr_type == NONE) {
7188 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7189 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7190 if (!retval) {
7191 /* Indicate initialization to INTx mode */
7192 phba->intr_type = INTx;
7193 intr_mode = 0;
7194 for (index = 0; index < phba->cfg_fcp_eq_count;
7195 index++) {
7196 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7197 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7198 }
7199 }
7200 }
7201 return intr_mode;
7202}
7203
7204/**
7205 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7206 * @phba: pointer to lpfc hba data structure.
7207 *
7208 * This routine is invoked to disable device interrupt and disassociate
7209 * the driver's interrupt handler(s) from interrupt vector(s) to device
7210 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7211 * will release the interrupt vector(s) for the message signaled interrupt.
7212 **/
7213static void
7214lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7215{
7216 /* Disable the currently initialized interrupt mode */
7217 if (phba->intr_type == MSIX)
7218 lpfc_sli4_disable_msix(phba);
7219 else if (phba->intr_type == MSI)
7220 lpfc_sli4_disable_msi(phba);
7221 else if (phba->intr_type == INTx)
7222 free_irq(phba->pcidev->irq, phba);
7223
7224 /* Reset interrupt management states */
7225 phba->intr_type = NONE;
7226 phba->sli.slistat.sli_intr = 0;
7227
7228 return;
7229}
7230
7231/**
7232 * lpfc_unset_hba - Unset SLI3 hba device initialization
7233 * @phba: pointer to lpfc hba data structure.
7234 *
7235 * This routine is invoked to unset the HBA device initialization steps to
7236 * a device with SLI-3 interface spec.
7237 **/
7238static void
7239lpfc_unset_hba(struct lpfc_hba *phba)
7240{
7241 struct lpfc_vport *vport = phba->pport;
7242 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7243
7244 spin_lock_irq(shost->host_lock);
7245 vport->load_flag |= FC_UNLOADING;
7246 spin_unlock_irq(shost->host_lock);
7247
7248 lpfc_stop_hba_timers(phba);
7249
7250 phba->pport->work_port_events = 0;
7251
7252 lpfc_sli_hba_down(phba);
7253
7254 lpfc_sli_brdrestart(phba);
7255
7256 lpfc_sli_disable_intr(phba);
7257
7258 return;
7259}
7260
7261/**
7262 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7263 * @phba: pointer to lpfc hba data structure.
7264 *
7265 * This routine is invoked to unset the HBA device initialization steps to
7266 * a device with SLI-4 interface spec.
7267 **/
7268static void
7269lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7270{
7271 struct lpfc_vport *vport = phba->pport;
7272 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7273
7274 spin_lock_irq(shost->host_lock);
7275 vport->load_flag |= FC_UNLOADING;
7276 spin_unlock_irq(shost->host_lock);
7277
7278 phba->pport->work_port_events = 0;
7279
7280 lpfc_sli4_hba_down(phba);
7281
7282 lpfc_sli4_disable_intr(phba);
7283
7284 return;
7285}
7286
7287/**
7288 * lpfc_sli4_hba_unset - Unset the fcoe hba
7289 * @phba: Pointer to HBA context object.
7290 *
7291 * This function is called in the SLI4 code path to reset the HBA's FCoE
7292 * function. The caller is not required to hold any lock. This routine
7293 * issues PCI function reset mailbox command to reset the FCoE function.
7294 * At the end of the function, it calls lpfc_hba_down_post function to
7295 * free any pending commands.
7296 **/
7297static void
7298lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7299{
7300 int wait_cnt = 0;
7301 LPFC_MBOXQ_t *mboxq;
7302
7303 lpfc_stop_hba_timers(phba);
7304 phba->sli4_hba.intr_enable = 0;
7305
7306 /*
7307 * Gracefully wait out the potential current outstanding asynchronous
7308 * mailbox command.
7309 */
7310
7311 /* First, block any pending async mailbox command from posted */
7312 spin_lock_irq(&phba->hbalock);
7313 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7314 spin_unlock_irq(&phba->hbalock);
7315 /* Now, trying to wait it out if we can */
7316 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7317 msleep(10);
7318 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7319 break;
7320 }
7321 /* Forcefully release the outstanding mailbox command if timed out */
7322 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7323 spin_lock_irq(&phba->hbalock);
7324 mboxq = phba->sli.mbox_active;
7325 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7326 __lpfc_mbox_cmpl_put(phba, mboxq);
7327 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7328 phba->sli.mbox_active = NULL;
7329 spin_unlock_irq(&phba->hbalock);
7330 }
7331
7332 /* Tear down the queues in the HBA */
7333 lpfc_sli4_queue_unset(phba);
7334
7335 /* Disable PCI subsystem interrupt */
7336 lpfc_sli4_disable_intr(phba);
7337
7338 /* Stop kthread signal shall trigger work_done one more time */
7339 kthread_stop(phba->worker_thread);
7340
7341 /* Stop the SLI4 device port */
7342 phba->pport->work_port_events = 0;
7343}
7344
28baac74
JS
7345 /**
7346 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7347 * @phba: Pointer to HBA context object.
7348 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7349 *
7350 * This function is called in the SLI4 code path to read the port's
7351 * sli4 capabilities.
7352 *
7353 * This function may be be called from any context that can block-wait
7354 * for the completion. The expectation is that this routine is called
7355 * typically from probe_one or from the online routine.
7356 **/
7357int
7358lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7359{
7360 int rc;
7361 struct lpfc_mqe *mqe;
7362 struct lpfc_pc_sli4_params *sli4_params;
7363 uint32_t mbox_tmo;
7364
7365 rc = 0;
7366 mqe = &mboxq->u.mqe;
7367
7368 /* Read the port's SLI4 Parameters port capabilities */
7369 lpfc_sli4_params(mboxq);
7370 if (!phba->sli4_hba.intr_enable)
7371 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7372 else {
7373 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7374 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7375 }
7376
7377 if (unlikely(rc))
7378 return 1;
7379
7380 sli4_params = &phba->sli4_hba.pc_sli4_params;
7381 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7382 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7383 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7384 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7385 &mqe->un.sli4_params);
7386 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7387 &mqe->un.sli4_params);
7388 sli4_params->proto_types = mqe->un.sli4_params.word3;
7389 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7390 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7391 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7392 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7393 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7394 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7395 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7396 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7397 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7398 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7399 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7400 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7401 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7402 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7403 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7404 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7405 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7406 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7407 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7408 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7409 return rc;
7410}
7411
da0436e9
JS
7412/**
7413 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7414 * @pdev: pointer to PCI device
7415 * @pid: pointer to PCI device identifier
7416 *
7417 * This routine is to be called to attach a device with SLI-3 interface spec
7418 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7419 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7420 * information of the device and driver to see if the driver state that it can
7421 * support this kind of device. If the match is successful, the driver core
7422 * invokes this routine. If this routine determines it can claim the HBA, it
7423 * does all the initialization that it needs to do to handle the HBA properly.
7424 *
7425 * Return code
7426 * 0 - driver can claim the device
7427 * negative value - driver can not claim the device
7428 **/
7429static int __devinit
7430lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7431{
7432 struct lpfc_hba *phba;
7433 struct lpfc_vport *vport = NULL;
6669f9bb 7434 struct Scsi_Host *shost = NULL;
da0436e9
JS
7435 int error;
7436 uint32_t cfg_mode, intr_mode;
7437
7438 /* Allocate memory for HBA structure */
7439 phba = lpfc_hba_alloc(pdev);
7440 if (!phba)
7441 return -ENOMEM;
7442
7443 /* Perform generic PCI device enabling operation */
7444 error = lpfc_enable_pci_dev(phba);
7445 if (error) {
7446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7447 "1401 Failed to enable pci device.\n");
7448 goto out_free_phba;
7449 }
7450
7451 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7452 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7453 if (error)
7454 goto out_disable_pci_dev;
7455
7456 /* Set up SLI-3 specific device PCI memory space */
7457 error = lpfc_sli_pci_mem_setup(phba);
7458 if (error) {
7459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7460 "1402 Failed to set up pci memory space.\n");
7461 goto out_disable_pci_dev;
7462 }
7463
7464 /* Set up phase-1 common device driver resources */
7465 error = lpfc_setup_driver_resource_phase1(phba);
7466 if (error) {
7467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7468 "1403 Failed to set up driver resource.\n");
7469 goto out_unset_pci_mem_s3;
7470 }
7471
7472 /* Set up SLI-3 specific device driver resources */
7473 error = lpfc_sli_driver_resource_setup(phba);
7474 if (error) {
7475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7476 "1404 Failed to set up driver resource.\n");
7477 goto out_unset_pci_mem_s3;
7478 }
7479
7480 /* Initialize and populate the iocb list per host */
7481 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7482 if (error) {
7483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7484 "1405 Failed to initialize iocb list.\n");
7485 goto out_unset_driver_resource_s3;
7486 }
7487
7488 /* Set up common device driver resources */
7489 error = lpfc_setup_driver_resource_phase2(phba);
7490 if (error) {
7491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7492 "1406 Failed to set up driver resource.\n");
7493 goto out_free_iocb_list;
7494 }
7495
7496 /* Create SCSI host to the physical port */
7497 error = lpfc_create_shost(phba);
7498 if (error) {
7499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7500 "1407 Failed to create scsi host.\n");
7501 goto out_unset_driver_resource;
7502 }
7503
7504 /* Configure sysfs attributes */
7505 vport = phba->pport;
7506 error = lpfc_alloc_sysfs_attr(vport);
7507 if (error) {
7508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7509 "1476 Failed to allocate sysfs attr\n");
7510 goto out_destroy_shost;
7511 }
7512
6669f9bb 7513 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
da0436e9
JS
7514 /* Now, trying to enable interrupt and bring up the device */
7515 cfg_mode = phba->cfg_use_msi;
7516 while (true) {
7517 /* Put device to a known state before enabling interrupt */
7518 lpfc_stop_port(phba);
7519 /* Configure and enable interrupt */
7520 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7521 if (intr_mode == LPFC_INTR_ERROR) {
7522 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7523 "0431 Failed to enable interrupt.\n");
7524 error = -ENODEV;
7525 goto out_free_sysfs_attr;
7526 }
7527 /* SLI-3 HBA setup */
7528 if (lpfc_sli_hba_setup(phba)) {
7529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7530 "1477 Failed to set up hba\n");
7531 error = -ENODEV;
7532 goto out_remove_device;
7533 }
7534
7535 /* Wait 50ms for the interrupts of previous mailbox commands */
7536 msleep(50);
7537 /* Check active interrupts on message signaled interrupts */
7538 if (intr_mode == 0 ||
7539 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7540 /* Log the current active interrupt mode */
7541 phba->intr_mode = intr_mode;
7542 lpfc_log_intr_mode(phba, intr_mode);
7543 break;
7544 } else {
7545 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7546 "0447 Configure interrupt mode (%d) "
7547 "failed active interrupt test.\n",
7548 intr_mode);
7549 /* Disable the current interrupt mode */
7550 lpfc_sli_disable_intr(phba);
7551 /* Try next level of interrupt mode */
7552 cfg_mode = --intr_mode;
7553 }
7554 }
7555
7556 /* Perform post initialization setup */
7557 lpfc_post_init_setup(phba);
7558
7559 /* Check if there are static vports to be created. */
7560 lpfc_create_static_vport(phba);
7561
7562 return 0;
7563
7564out_remove_device:
7565 lpfc_unset_hba(phba);
7566out_free_sysfs_attr:
7567 lpfc_free_sysfs_attr(vport);
7568out_destroy_shost:
7569 lpfc_destroy_shost(phba);
7570out_unset_driver_resource:
7571 lpfc_unset_driver_resource_phase2(phba);
7572out_free_iocb_list:
7573 lpfc_free_iocb_list(phba);
7574out_unset_driver_resource_s3:
7575 lpfc_sli_driver_resource_unset(phba);
7576out_unset_pci_mem_s3:
7577 lpfc_sli_pci_mem_unset(phba);
7578out_disable_pci_dev:
7579 lpfc_disable_pci_dev(phba);
6669f9bb
JS
7580 if (shost)
7581 scsi_host_put(shost);
da0436e9
JS
7582out_free_phba:
7583 lpfc_hba_free(phba);
7584 return error;
7585}
7586
7587/**
7588 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
7589 * @pdev: pointer to PCI device
7590 *
7591 * This routine is to be called to disattach a device with SLI-3 interface
7592 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7593 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7594 * device to be removed from the PCI subsystem properly.
7595 **/
7596static void __devexit
7597lpfc_pci_remove_one_s3(struct pci_dev *pdev)
7598{
7599 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7600 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7601 struct lpfc_vport **vports;
7602 struct lpfc_hba *phba = vport->phba;
7603 int i;
7604 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7605
7606 spin_lock_irq(&phba->hbalock);
7607 vport->load_flag |= FC_UNLOADING;
7608 spin_unlock_irq(&phba->hbalock);
7609
7610 lpfc_free_sysfs_attr(vport);
7611
7612 /* Release all the vports against this physical port */
7613 vports = lpfc_create_vport_work_array(phba);
7614 if (vports != NULL)
7615 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7616 fc_vport_terminate(vports[i]->fc_vport);
7617 lpfc_destroy_vport_work_array(phba, vports);
7618
7619 /* Remove FC host and then SCSI host with the physical port */
7620 fc_remove_host(shost);
7621 scsi_remove_host(shost);
7622 lpfc_cleanup(vport);
7623
7624 /*
7625 * Bring down the SLI Layer. This step disable all interrupts,
7626 * clears the rings, discards all mailbox commands, and resets
7627 * the HBA.
7628 */
7629
7630 /* HBA interrupt will be diabled after this call */
7631 lpfc_sli_hba_down(phba);
7632 /* Stop kthread signal shall trigger work_done one more time */
7633 kthread_stop(phba->worker_thread);
7634 /* Final cleanup of txcmplq and reset the HBA */
7635 lpfc_sli_brdrestart(phba);
7636
7637 lpfc_stop_hba_timers(phba);
7638 spin_lock_irq(&phba->hbalock);
7639 list_del_init(&vport->listentry);
7640 spin_unlock_irq(&phba->hbalock);
7641
7642 lpfc_debugfs_terminate(vport);
7643
7644 /* Disable interrupt */
7645 lpfc_sli_disable_intr(phba);
7646
7647 pci_set_drvdata(pdev, NULL);
7648 scsi_host_put(shost);
7649
7650 /*
7651 * Call scsi_free before mem_free since scsi bufs are released to their
7652 * corresponding pools here.
7653 */
7654 lpfc_scsi_free(phba);
7655 lpfc_mem_free_all(phba);
7656
7657 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7658 phba->hbqslimp.virt, phba->hbqslimp.phys);
7659
7660 /* Free resources associated with SLI2 interface */
7661 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7662 phba->slim2p.virt, phba->slim2p.phys);
7663
7664 /* unmap adapter SLIM and Control Registers */
7665 iounmap(phba->ctrl_regs_memmap_p);
7666 iounmap(phba->slim_memmap_p);
7667
7668 lpfc_hba_free(phba);
7669
7670 pci_release_selected_regions(pdev, bars);
7671 pci_disable_device(pdev);
7672}
7673
7674/**
7675 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
7676 * @pdev: pointer to PCI device
7677 * @msg: power management message
7678 *
7679 * This routine is to be called from the kernel's PCI subsystem to support
7680 * system Power Management (PM) to device with SLI-3 interface spec. When
7681 * PM invokes this method, it quiesces the device by stopping the driver's
7682 * worker thread for the device, turning off device's interrupt and DMA,
7683 * and bring the device offline. Note that as the driver implements the
7684 * minimum PM requirements to a power-aware driver's PM support for the
7685 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7686 * to the suspend() method call will be treated as SUSPEND and the driver will
7687 * fully reinitialize its device during resume() method call, the driver will
7688 * set device to PCI_D3hot state in PCI config space instead of setting it
7689 * according to the @msg provided by the PM.
7690 *
7691 * Return code
7692 * 0 - driver suspended the device
7693 * Error otherwise
7694 **/
7695static int
7696lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
7697{
7698 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7699 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7700
7701 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7702 "0473 PCI device Power Management suspend.\n");
7703
7704 /* Bring down the device */
7705 lpfc_offline_prep(phba);
7706 lpfc_offline(phba);
7707 kthread_stop(phba->worker_thread);
7708
7709 /* Disable interrupt from device */
7710 lpfc_sli_disable_intr(phba);
7711
7712 /* Save device state to PCI config space */
7713 pci_save_state(pdev);
7714 pci_set_power_state(pdev, PCI_D3hot);
7715
7716 return 0;
7717}
7718
7719/**
7720 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
7721 * @pdev: pointer to PCI device
7722 *
7723 * This routine is to be called from the kernel's PCI subsystem to support
7724 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7725 * invokes this method, it restores the device's PCI config space state and
7726 * fully reinitializes the device and brings it online. Note that as the
7727 * driver implements the minimum PM requirements to a power-aware driver's
7728 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7729 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7730 * driver will fully reinitialize its device during resume() method call,
7731 * the device will be set to PCI_D0 directly in PCI config space before
7732 * restoring the state.
7733 *
7734 * Return code
7735 * 0 - driver suspended the device
7736 * Error otherwise
7737 **/
7738static int
7739lpfc_pci_resume_one_s3(struct pci_dev *pdev)
7740{
7741 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7742 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7743 uint32_t intr_mode;
7744 int error;
7745
7746 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7747 "0452 PCI device Power Management resume.\n");
7748
7749 /* Restore device state from PCI config space */
7750 pci_set_power_state(pdev, PCI_D0);
7751 pci_restore_state(pdev);
0d878419 7752
1dfb5a47
JS
7753 /*
7754 * As the new kernel behavior of pci_restore_state() API call clears
7755 * device saved_state flag, need to save the restored state again.
7756 */
7757 pci_save_state(pdev);
7758
da0436e9
JS
7759 if (pdev->is_busmaster)
7760 pci_set_master(pdev);
7761
7762 /* Startup the kernel thread for this host adapter. */
7763 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7764 "lpfc_worker_%d", phba->brd_no);
7765 if (IS_ERR(phba->worker_thread)) {
7766 error = PTR_ERR(phba->worker_thread);
7767 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7768 "0434 PM resume failed to start worker "
7769 "thread: error=x%x.\n", error);
7770 return error;
7771 }
7772
7773 /* Configure and enable interrupt */
7774 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7775 if (intr_mode == LPFC_INTR_ERROR) {
7776 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7777 "0430 PM resume Failed to enable interrupt\n");
7778 return -EIO;
7779 } else
7780 phba->intr_mode = intr_mode;
7781
7782 /* Restart HBA and bring it online */
7783 lpfc_sli_brdrestart(phba);
7784 lpfc_online(phba);
7785
7786 /* Log the current active interrupt mode */
7787 lpfc_log_intr_mode(phba, phba->intr_mode);
7788
7789 return 0;
7790}
7791
891478a2
JS
7792/**
7793 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7794 * @phba: pointer to lpfc hba data structure.
7795 *
7796 * This routine is called to prepare the SLI3 device for PCI slot recover. It
e2af0d2e 7797 * aborts all the outstanding SCSI I/Os to the pci device.
891478a2
JS
7798 **/
7799static void
7800lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7801{
e2af0d2e
JS
7802 struct lpfc_sli *psli = &phba->sli;
7803 struct lpfc_sli_ring *pring;
7804
891478a2
JS
7805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7806 "2723 PCI channel I/O abort preparing for recovery\n");
e2af0d2e
JS
7807
7808 /*
7809 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
7810 * and let the SCSI mid-layer to retry them to recover.
7811 */
7812 pring = &psli->ring[psli->fcp_ring];
7813 lpfc_sli_abort_iocb_ring(phba, pring);
891478a2
JS
7814}
7815
0d878419
JS
7816/**
7817 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7818 * @phba: pointer to lpfc hba data structure.
7819 *
7820 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7821 * disables the device interrupt and pci device, and aborts the internal FCP
7822 * pending I/Os.
7823 **/
7824static void
7825lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7826{
0d878419 7827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
891478a2 7828 "2710 PCI channel disable preparing for reset\n");
e2af0d2e
JS
7829
7830 /* Block all SCSI devices' I/Os on the host */
7831 lpfc_scsi_dev_block(phba);
7832
7833 /* stop all timers */
7834 lpfc_stop_hba_timers(phba);
7835
0d878419
JS
7836 /* Disable interrupt and pci device */
7837 lpfc_sli_disable_intr(phba);
7838 pci_disable_device(phba->pcidev);
e2af0d2e
JS
7839 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
7840 lpfc_sli_flush_fcp_rings(phba);
0d878419
JS
7841}
7842
7843/**
7844 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7845 * @phba: pointer to lpfc hba data structure.
7846 *
7847 * This routine is called to prepare the SLI3 device for PCI slot permanently
7848 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7849 * pending I/Os.
7850 **/
7851static void
7852lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7853{
7854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
891478a2 7855 "2711 PCI channel permanent disable for failure\n");
e2af0d2e
JS
7856 /* Block all SCSI devices' I/Os on the host */
7857 lpfc_scsi_dev_block(phba);
7858
7859 /* stop all timers */
7860 lpfc_stop_hba_timers(phba);
7861
0d878419
JS
7862 /* Clean up all driver's outstanding SCSI I/Os */
7863 lpfc_sli_flush_fcp_rings(phba);
7864}
7865
da0436e9
JS
7866/**
7867 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
7868 * @pdev: pointer to PCI device.
7869 * @state: the current PCI connection state.
7870 *
7871 * This routine is called from the PCI subsystem for I/O error handling to
7872 * device with SLI-3 interface spec. This function is called by the PCI
7873 * subsystem after a PCI bus error affecting this device has been detected.
7874 * When this function is invoked, it will need to stop all the I/Os and
7875 * interrupt(s) to the device. Once that is done, it will return
7876 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7877 * as desired.
7878 *
7879 * Return codes
0d878419 7880 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
da0436e9
JS
7881 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7882 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7883 **/
7884static pci_ers_result_t
7885lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
7886{
7887 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7888 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
da0436e9 7889
0d878419
JS
7890 switch (state) {
7891 case pci_channel_io_normal:
891478a2
JS
7892 /* Non-fatal error, prepare for recovery */
7893 lpfc_sli_prep_dev_for_recover(phba);
0d878419
JS
7894 return PCI_ERS_RESULT_CAN_RECOVER;
7895 case pci_channel_io_frozen:
7896 /* Fatal error, prepare for slot reset */
7897 lpfc_sli_prep_dev_for_reset(phba);
7898 return PCI_ERS_RESULT_NEED_RESET;
7899 case pci_channel_io_perm_failure:
7900 /* Permanent failure, prepare for device down */
7901 lpfc_prep_dev_for_perm_failure(phba);
da0436e9 7902 return PCI_ERS_RESULT_DISCONNECT;
0d878419
JS
7903 default:
7904 /* Unknown state, prepare and request slot reset */
7905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7906 "0472 Unknown PCI error state: x%x\n", state);
7907 lpfc_sli_prep_dev_for_reset(phba);
7908 return PCI_ERS_RESULT_NEED_RESET;
da0436e9 7909 }
da0436e9
JS
7910}
7911
7912/**
7913 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
7914 * @pdev: pointer to PCI device.
7915 *
7916 * This routine is called from the PCI subsystem for error handling to
7917 * device with SLI-3 interface spec. This is called after PCI bus has been
7918 * reset to restart the PCI card from scratch, as if from a cold-boot.
7919 * During the PCI subsystem error recovery, after driver returns
7920 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7921 * recovery and then call this routine before calling the .resume method
7922 * to recover the device. This function will initialize the HBA device,
7923 * enable the interrupt, but it will just put the HBA to offline state
7924 * without passing any I/O traffic.
7925 *
7926 * Return codes
7927 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7928 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7929 */
7930static pci_ers_result_t
7931lpfc_io_slot_reset_s3(struct pci_dev *pdev)
7932{
7933 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7934 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7935 struct lpfc_sli *psli = &phba->sli;
7936 uint32_t intr_mode;
7937
7938 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
7939 if (pci_enable_device_mem(pdev)) {
7940 printk(KERN_ERR "lpfc: Cannot re-enable "
7941 "PCI device after reset.\n");
7942 return PCI_ERS_RESULT_DISCONNECT;
7943 }
7944
7945 pci_restore_state(pdev);
1dfb5a47
JS
7946
7947 /*
7948 * As the new kernel behavior of pci_restore_state() API call clears
7949 * device saved_state flag, need to save the restored state again.
7950 */
7951 pci_save_state(pdev);
7952
da0436e9
JS
7953 if (pdev->is_busmaster)
7954 pci_set_master(pdev);
7955
7956 spin_lock_irq(&phba->hbalock);
7957 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7958 spin_unlock_irq(&phba->hbalock);
7959
7960 /* Configure and enable interrupt */
7961 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7962 if (intr_mode == LPFC_INTR_ERROR) {
7963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7964 "0427 Cannot re-enable interrupt after "
7965 "slot reset.\n");
7966 return PCI_ERS_RESULT_DISCONNECT;
7967 } else
7968 phba->intr_mode = intr_mode;
7969
7970 /* Take device offline; this will perform cleanup */
7971 lpfc_offline(phba);
7972 lpfc_sli_brdrestart(phba);
7973
7974 /* Log the current active interrupt mode */
7975 lpfc_log_intr_mode(phba, phba->intr_mode);
7976
7977 return PCI_ERS_RESULT_RECOVERED;
7978}
7979
7980/**
7981 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
7982 * @pdev: pointer to PCI device
7983 *
7984 * This routine is called from the PCI subsystem for error handling to device
7985 * with SLI-3 interface spec. It is called when kernel error recovery tells
7986 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7987 * error recovery. After this call, traffic can start to flow from this device
7988 * again.
7989 */
7990static void
7991lpfc_io_resume_s3(struct pci_dev *pdev)
7992{
7993 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7994 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3772a991 7995
e2af0d2e 7996 /* Bring device online, it will be no-op for non-fatal error resume */
da0436e9 7997 lpfc_online(phba);
0d878419
JS
7998
7999 /* Clean up Advanced Error Reporting (AER) if needed */
8000 if (phba->hba_flag & HBA_AER_ENABLED)
8001 pci_cleanup_aer_uncorrect_error_status(pdev);
da0436e9 8002}
3772a991 8003
da0436e9
JS
8004/**
8005 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8006 * @phba: pointer to lpfc hba data structure.
8007 *
8008 * returns the number of ELS/CT IOCBs to reserve
8009 **/
8010int
8011lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8012{
8013 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8014
f1126688
JS
8015 if (phba->sli_rev == LPFC_SLI_REV4) {
8016 if (max_xri <= 100)
6a9c52cf 8017 return 10;
f1126688 8018 else if (max_xri <= 256)
6a9c52cf 8019 return 25;
f1126688 8020 else if (max_xri <= 512)
6a9c52cf 8021 return 50;
f1126688 8022 else if (max_xri <= 1024)
6a9c52cf 8023 return 100;
f1126688 8024 else
6a9c52cf 8025 return 150;
f1126688
JS
8026 } else
8027 return 0;
3772a991
JS
8028}
8029
8030/**
da0436e9 8031 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
3772a991
JS
8032 * @pdev: pointer to PCI device
8033 * @pid: pointer to PCI device identifier
8034 *
da0436e9
JS
8035 * This routine is called from the kernel's PCI subsystem to device with
8036 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
3772a991 8037 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
da0436e9
JS
8038 * information of the device and driver to see if the driver state that it
8039 * can support this kind of device. If the match is successful, the driver
8040 * core invokes this routine. If this routine determines it can claim the HBA,
8041 * it does all the initialization that it needs to do to handle the HBA
8042 * properly.
3772a991
JS
8043 *
8044 * Return code
8045 * 0 - driver can claim the device
8046 * negative value - driver can not claim the device
8047 **/
8048static int __devinit
da0436e9 8049lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
3772a991
JS
8050{
8051 struct lpfc_hba *phba;
8052 struct lpfc_vport *vport = NULL;
6669f9bb 8053 struct Scsi_Host *shost = NULL;
3772a991
JS
8054 int error;
8055 uint32_t cfg_mode, intr_mode;
da0436e9 8056 int mcnt;
3772a991
JS
8057
8058 /* Allocate memory for HBA structure */
8059 phba = lpfc_hba_alloc(pdev);
8060 if (!phba)
8061 return -ENOMEM;
8062
8063 /* Perform generic PCI device enabling operation */
8064 error = lpfc_enable_pci_dev(phba);
8065 if (error) {
8066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8067 "1409 Failed to enable pci device.\n");
3772a991
JS
8068 goto out_free_phba;
8069 }
8070
da0436e9
JS
8071 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
8072 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
3772a991
JS
8073 if (error)
8074 goto out_disable_pci_dev;
8075
da0436e9
JS
8076 /* Set up SLI-4 specific device PCI memory space */
8077 error = lpfc_sli4_pci_mem_setup(phba);
3772a991
JS
8078 if (error) {
8079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8080 "1410 Failed to set up pci memory space.\n");
3772a991
JS
8081 goto out_disable_pci_dev;
8082 }
8083
8084 /* Set up phase-1 common device driver resources */
8085 error = lpfc_setup_driver_resource_phase1(phba);
8086 if (error) {
8087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
8088 "1411 Failed to set up driver resource.\n");
8089 goto out_unset_pci_mem_s4;
3772a991
JS
8090 }
8091
da0436e9
JS
8092 /* Set up SLI-4 Specific device driver resources */
8093 error = lpfc_sli4_driver_resource_setup(phba);
3772a991
JS
8094 if (error) {
8095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
8096 "1412 Failed to set up driver resource.\n");
8097 goto out_unset_pci_mem_s4;
3772a991
JS
8098 }
8099
8100 /* Initialize and populate the iocb list per host */
da0436e9
JS
8101 error = lpfc_init_iocb_list(phba,
8102 phba->sli4_hba.max_cfg_param.max_xri);
3772a991
JS
8103 if (error) {
8104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
8105 "1413 Failed to initialize iocb list.\n");
8106 goto out_unset_driver_resource_s4;
3772a991
JS
8107 }
8108
8109 /* Set up common device driver resources */
8110 error = lpfc_setup_driver_resource_phase2(phba);
8111 if (error) {
8112 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8113 "1414 Failed to set up driver resource.\n");
3772a991
JS
8114 goto out_free_iocb_list;
8115 }
8116
8117 /* Create SCSI host to the physical port */
8118 error = lpfc_create_shost(phba);
8119 if (error) {
8120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8121 "1415 Failed to create scsi host.\n");
3772a991
JS
8122 goto out_unset_driver_resource;
8123 }
9399627f 8124
5b75da2f 8125 /* Configure sysfs attributes */
3772a991
JS
8126 vport = phba->pport;
8127 error = lpfc_alloc_sysfs_attr(vport);
8128 if (error) {
9399627f 8129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8130 "1416 Failed to allocate sysfs attr\n");
3772a991 8131 goto out_destroy_shost;
98c9ea5c 8132 }
875fbdfe 8133
6669f9bb 8134 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
3772a991 8135 /* Now, trying to enable interrupt and bring up the device */
5b75da2f
JS
8136 cfg_mode = phba->cfg_use_msi;
8137 while (true) {
3772a991
JS
8138 /* Put device to a known state before enabling interrupt */
8139 lpfc_stop_port(phba);
5b75da2f 8140 /* Configure and enable interrupt */
da0436e9 8141 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
5b75da2f
JS
8142 if (intr_mode == LPFC_INTR_ERROR) {
8143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8144 "0426 Failed to enable interrupt.\n");
3772a991 8145 error = -ENODEV;
5b75da2f
JS
8146 goto out_free_sysfs_attr;
8147 }
def9c7a9
JS
8148 /* Default to single FCP EQ for non-MSI-X */
8149 if (phba->intr_type != MSIX)
8150 phba->cfg_fcp_eq_count = 1;
da0436e9
JS
8151 /* Set up SLI-4 HBA */
8152 if (lpfc_sli4_hba_setup(phba)) {
5b75da2f 8153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8154 "1421 Failed to set up hba\n");
5b75da2f 8155 error = -ENODEV;
da0436e9 8156 goto out_disable_intr;
5b75da2f
JS
8157 }
8158
da0436e9
JS
8159 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
8160 if (intr_mode != 0)
8161 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
8162 LPFC_ACT_INTR_CNT);
8163
8164 /* Check active interrupts received only for MSI/MSI-X */
3772a991 8165 if (intr_mode == 0 ||
da0436e9 8166 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
5b75da2f
JS
8167 /* Log the current active interrupt mode */
8168 phba->intr_mode = intr_mode;
8169 lpfc_log_intr_mode(phba, intr_mode);
8170 break;
5b75da2f 8171 }
da0436e9
JS
8172 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8173 "0451 Configure interrupt mode (%d) "
8174 "failed active interrupt test.\n",
8175 intr_mode);
8176 /* Unset the preivous SLI-4 HBA setup */
8177 lpfc_sli4_unset_hba(phba);
8178 /* Try next level of interrupt mode */
8179 cfg_mode = --intr_mode;
98c9ea5c 8180 }
858c9f6c 8181
3772a991
JS
8182 /* Perform post initialization setup */
8183 lpfc_post_init_setup(phba);
dea3101e 8184
1c6834a7
JS
8185 /* Check if there are static vports to be created. */
8186 lpfc_create_static_vport(phba);
8187
dea3101e 8188 return 0;
8189
da0436e9
JS
8190out_disable_intr:
8191 lpfc_sli4_disable_intr(phba);
5b75da2f
JS
8192out_free_sysfs_attr:
8193 lpfc_free_sysfs_attr(vport);
3772a991
JS
8194out_destroy_shost:
8195 lpfc_destroy_shost(phba);
8196out_unset_driver_resource:
8197 lpfc_unset_driver_resource_phase2(phba);
8198out_free_iocb_list:
8199 lpfc_free_iocb_list(phba);
da0436e9
JS
8200out_unset_driver_resource_s4:
8201 lpfc_sli4_driver_resource_unset(phba);
8202out_unset_pci_mem_s4:
8203 lpfc_sli4_pci_mem_unset(phba);
3772a991
JS
8204out_disable_pci_dev:
8205 lpfc_disable_pci_dev(phba);
6669f9bb
JS
8206 if (shost)
8207 scsi_host_put(shost);
2e0fef85 8208out_free_phba:
3772a991 8209 lpfc_hba_free(phba);
dea3101e 8210 return error;
8211}
8212
e59058c4 8213/**
da0436e9 8214 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
e59058c4
JS
8215 * @pdev: pointer to PCI device
8216 *
da0436e9
JS
8217 * This routine is called from the kernel's PCI subsystem to device with
8218 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
3772a991
JS
8219 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8220 * device to be removed from the PCI subsystem properly.
e59058c4 8221 **/
dea3101e 8222static void __devexit
da0436e9 8223lpfc_pci_remove_one_s4(struct pci_dev *pdev)
dea3101e 8224{
da0436e9 8225 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2e0fef85 8226 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
eada272d 8227 struct lpfc_vport **vports;
da0436e9 8228 struct lpfc_hba *phba = vport->phba;
eada272d 8229 int i;
8a4df120 8230
da0436e9 8231 /* Mark the device unloading flag */
549e55cd 8232 spin_lock_irq(&phba->hbalock);
51ef4c26 8233 vport->load_flag |= FC_UNLOADING;
549e55cd 8234 spin_unlock_irq(&phba->hbalock);
2e0fef85 8235
da0436e9 8236 /* Free the HBA sysfs attributes */
858c9f6c
JS
8237 lpfc_free_sysfs_attr(vport);
8238
eada272d
JS
8239 /* Release all the vports against this physical port */
8240 vports = lpfc_create_vport_work_array(phba);
8241 if (vports != NULL)
3772a991 8242 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
eada272d
JS
8243 fc_vport_terminate(vports[i]->fc_vport);
8244 lpfc_destroy_vport_work_array(phba, vports);
8245
8246 /* Remove FC host and then SCSI host with the physical port */
858c9f6c
JS
8247 fc_remove_host(shost);
8248 scsi_remove_host(shost);
da0436e9
JS
8249
8250 /* Perform cleanup on the physical port */
87af33fe
JS
8251 lpfc_cleanup(vport);
8252
2e0fef85 8253 /*
da0436e9 8254 * Bring down the SLI Layer. This step disables all interrupts,
2e0fef85 8255 * clears the rings, discards all mailbox commands, and resets
da0436e9 8256 * the HBA FCoE function.
2e0fef85 8257 */
da0436e9
JS
8258 lpfc_debugfs_terminate(vport);
8259 lpfc_sli4_hba_unset(phba);
a257bf90 8260
858c9f6c
JS
8261 spin_lock_irq(&phba->hbalock);
8262 list_del_init(&vport->listentry);
8263 spin_unlock_irq(&phba->hbalock);
8264
da0436e9
JS
8265 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
8266 * buffers are released to their corresponding pools here.
2e0fef85
JS
8267 */
8268 lpfc_scsi_free(phba);
da0436e9 8269 lpfc_sli4_driver_resource_unset(phba);
ed957684 8270
da0436e9
JS
8271 /* Unmap adapter Control and Doorbell registers */
8272 lpfc_sli4_pci_mem_unset(phba);
2e0fef85 8273
da0436e9
JS
8274 /* Release PCI resources and disable device's PCI function */
8275 scsi_host_put(shost);
8276 lpfc_disable_pci_dev(phba);
2e0fef85 8277
da0436e9 8278 /* Finally, free the driver's device data structure */
3772a991 8279 lpfc_hba_free(phba);
2e0fef85 8280
da0436e9 8281 return;
dea3101e 8282}
8283
3a55b532 8284/**
da0436e9 8285 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
3a55b532
JS
8286 * @pdev: pointer to PCI device
8287 * @msg: power management message
8288 *
da0436e9
JS
8289 * This routine is called from the kernel's PCI subsystem to support system
8290 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8291 * this method, it quiesces the device by stopping the driver's worker
8292 * thread for the device, turning off device's interrupt and DMA, and bring
8293 * the device offline. Note that as the driver implements the minimum PM
8294 * requirements to a power-aware driver's PM support for suspend/resume -- all
8295 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8296 * method call will be treated as SUSPEND and the driver will fully
8297 * reinitialize its device during resume() method call, the driver will set
8298 * device to PCI_D3hot state in PCI config space instead of setting it
3772a991 8299 * according to the @msg provided by the PM.
3a55b532
JS
8300 *
8301 * Return code
3772a991
JS
8302 * 0 - driver suspended the device
8303 * Error otherwise
3a55b532
JS
8304 **/
8305static int
da0436e9 8306lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
3a55b532
JS
8307{
8308 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8309 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8310
8311 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
da0436e9 8312 "0298 PCI device Power Management suspend.\n");
3a55b532
JS
8313
8314 /* Bring down the device */
8315 lpfc_offline_prep(phba);
8316 lpfc_offline(phba);
8317 kthread_stop(phba->worker_thread);
8318
8319 /* Disable interrupt from device */
da0436e9 8320 lpfc_sli4_disable_intr(phba);
3a55b532
JS
8321
8322 /* Save device state to PCI config space */
8323 pci_save_state(pdev);
8324 pci_set_power_state(pdev, PCI_D3hot);
8325
8326 return 0;
8327}
8328
8329/**
da0436e9 8330 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
3a55b532
JS
8331 * @pdev: pointer to PCI device
8332 *
da0436e9
JS
8333 * This routine is called from the kernel's PCI subsystem to support system
8334 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8335 * this method, it restores the device's PCI config space state and fully
8336 * reinitializes the device and brings it online. Note that as the driver
8337 * implements the minimum PM requirements to a power-aware driver's PM for
8338 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8339 * to the suspend() method call will be treated as SUSPEND and the driver
8340 * will fully reinitialize its device during resume() method call, the device
8341 * will be set to PCI_D0 directly in PCI config space before restoring the
8342 * state.
3a55b532
JS
8343 *
8344 * Return code
3772a991
JS
8345 * 0 - driver suspended the device
8346 * Error otherwise
3a55b532
JS
8347 **/
8348static int
da0436e9 8349lpfc_pci_resume_one_s4(struct pci_dev *pdev)
3a55b532
JS
8350{
8351 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8352 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
5b75da2f 8353 uint32_t intr_mode;
3a55b532
JS
8354 int error;
8355
8356 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
da0436e9 8357 "0292 PCI device Power Management resume.\n");
3a55b532
JS
8358
8359 /* Restore device state from PCI config space */
8360 pci_set_power_state(pdev, PCI_D0);
8361 pci_restore_state(pdev);
1dfb5a47
JS
8362
8363 /*
8364 * As the new kernel behavior of pci_restore_state() API call clears
8365 * device saved_state flag, need to save the restored state again.
8366 */
8367 pci_save_state(pdev);
8368
3a55b532
JS
8369 if (pdev->is_busmaster)
8370 pci_set_master(pdev);
8371
da0436e9 8372 /* Startup the kernel thread for this host adapter. */
3a55b532
JS
8373 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8374 "lpfc_worker_%d", phba->brd_no);
8375 if (IS_ERR(phba->worker_thread)) {
8376 error = PTR_ERR(phba->worker_thread);
8377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8378 "0293 PM resume failed to start worker "
3a55b532
JS
8379 "thread: error=x%x.\n", error);
8380 return error;
8381 }
8382
5b75da2f 8383 /* Configure and enable interrupt */
da0436e9 8384 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
5b75da2f 8385 if (intr_mode == LPFC_INTR_ERROR) {
3a55b532 8386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8387 "0294 PM resume Failed to enable interrupt\n");
5b75da2f
JS
8388 return -EIO;
8389 } else
8390 phba->intr_mode = intr_mode;
3a55b532
JS
8391
8392 /* Restart HBA and bring it online */
8393 lpfc_sli_brdrestart(phba);
8394 lpfc_online(phba);
8395
5b75da2f
JS
8396 /* Log the current active interrupt mode */
8397 lpfc_log_intr_mode(phba, phba->intr_mode);
8398
3a55b532
JS
8399 return 0;
8400}
8401
8d63f375 8402/**
da0436e9 8403 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
e59058c4
JS
8404 * @pdev: pointer to PCI device.
8405 * @state: the current PCI connection state.
8d63f375 8406 *
da0436e9
JS
8407 * This routine is called from the PCI subsystem for error handling to device
8408 * with SLI-4 interface spec. This function is called by the PCI subsystem
8409 * after a PCI bus error affecting this device has been detected. When this
8410 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8411 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8412 * for the PCI subsystem to perform proper recovery as desired.
e59058c4
JS
8413 *
8414 * Return codes
3772a991
JS
8415 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8416 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
e59058c4 8417 **/
3772a991 8418static pci_ers_result_t
da0436e9 8419lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8d63f375 8420{
8d63f375
LV
8421 return PCI_ERS_RESULT_NEED_RESET;
8422}
8423
8424/**
da0436e9 8425 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
e59058c4
JS
8426 * @pdev: pointer to PCI device.
8427 *
da0436e9
JS
8428 * This routine is called from the PCI subsystem for error handling to device
8429 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8430 * restart the PCI card from scratch, as if from a cold-boot. During the
8431 * PCI subsystem error recovery, after the driver returns
3772a991 8432 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
da0436e9
JS
8433 * recovery and then call this routine before calling the .resume method to
8434 * recover the device. This function will initialize the HBA device, enable
8435 * the interrupt, but it will just put the HBA to offline state without
8436 * passing any I/O traffic.
8d63f375 8437 *
e59058c4 8438 * Return codes
3772a991
JS
8439 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8440 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8d63f375 8441 */
3772a991 8442static pci_ers_result_t
da0436e9 8443lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8d63f375 8444{
8d63f375
LV
8445 return PCI_ERS_RESULT_RECOVERED;
8446}
8447
8448/**
da0436e9 8449 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
e59058c4 8450 * @pdev: pointer to PCI device
8d63f375 8451 *
3772a991 8452 * This routine is called from the PCI subsystem for error handling to device
da0436e9 8453 * with SLI-4 interface spec. It is called when kernel error recovery tells
3772a991
JS
8454 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8455 * error recovery. After this call, traffic can start to flow from this device
8456 * again.
da0436e9 8457 **/
3772a991 8458static void
da0436e9 8459lpfc_io_resume_s4(struct pci_dev *pdev)
8d63f375 8460{
da0436e9 8461 return;
8d63f375
LV
8462}
8463
3772a991
JS
8464/**
8465 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8466 * @pdev: pointer to PCI device
8467 * @pid: pointer to PCI device identifier
8468 *
8469 * This routine is to be registered to the kernel's PCI subsystem. When an
8470 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8471 * at PCI device-specific information of the device and driver to see if the
8472 * driver state that it can support this kind of device. If the match is
8473 * successful, the driver core invokes this routine. This routine dispatches
8474 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8475 * do all the initialization that it needs to do to handle the HBA device
8476 * properly.
8477 *
8478 * Return code
8479 * 0 - driver can claim the device
8480 * negative value - driver can not claim the device
8481 **/
8482static int __devinit
8483lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8484{
8485 int rc;
8fa38513 8486 struct lpfc_sli_intf intf;
3772a991 8487
28baac74 8488 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
3772a991
JS
8489 return -ENODEV;
8490
8fa38513 8491 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
28baac74 8492 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
da0436e9 8493 rc = lpfc_pci_probe_one_s4(pdev, pid);
8fa38513 8494 else
3772a991 8495 rc = lpfc_pci_probe_one_s3(pdev, pid);
8fa38513 8496
3772a991
JS
8497 return rc;
8498}
8499
8500/**
8501 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8502 * @pdev: pointer to PCI device
8503 *
8504 * This routine is to be registered to the kernel's PCI subsystem. When an
8505 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8506 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8507 * remove routine, which will perform all the necessary cleanup for the
8508 * device to be removed from the PCI subsystem properly.
8509 **/
8510static void __devexit
8511lpfc_pci_remove_one(struct pci_dev *pdev)
8512{
8513 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8514 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8515
8516 switch (phba->pci_dev_grp) {
8517 case LPFC_PCI_DEV_LP:
8518 lpfc_pci_remove_one_s3(pdev);
8519 break;
da0436e9
JS
8520 case LPFC_PCI_DEV_OC:
8521 lpfc_pci_remove_one_s4(pdev);
8522 break;
3772a991
JS
8523 default:
8524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8525 "1424 Invalid PCI device group: 0x%x\n",
8526 phba->pci_dev_grp);
8527 break;
8528 }
8529 return;
8530}
8531
8532/**
8533 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8534 * @pdev: pointer to PCI device
8535 * @msg: power management message
8536 *
8537 * This routine is to be registered to the kernel's PCI subsystem to support
8538 * system Power Management (PM). When PM invokes this method, it dispatches
8539 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8540 * suspend the device.
8541 *
8542 * Return code
8543 * 0 - driver suspended the device
8544 * Error otherwise
8545 **/
8546static int
8547lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8548{
8549 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8550 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8551 int rc = -ENODEV;
8552
8553 switch (phba->pci_dev_grp) {
8554 case LPFC_PCI_DEV_LP:
8555 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8556 break;
da0436e9
JS
8557 case LPFC_PCI_DEV_OC:
8558 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8559 break;
3772a991
JS
8560 default:
8561 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8562 "1425 Invalid PCI device group: 0x%x\n",
8563 phba->pci_dev_grp);
8564 break;
8565 }
8566 return rc;
8567}
8568
8569/**
8570 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8571 * @pdev: pointer to PCI device
8572 *
8573 * This routine is to be registered to the kernel's PCI subsystem to support
8574 * system Power Management (PM). When PM invokes this method, it dispatches
8575 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8576 * resume the device.
8577 *
8578 * Return code
8579 * 0 - driver suspended the device
8580 * Error otherwise
8581 **/
8582static int
8583lpfc_pci_resume_one(struct pci_dev *pdev)
8584{
8585 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8586 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8587 int rc = -ENODEV;
8588
8589 switch (phba->pci_dev_grp) {
8590 case LPFC_PCI_DEV_LP:
8591 rc = lpfc_pci_resume_one_s3(pdev);
8592 break;
da0436e9
JS
8593 case LPFC_PCI_DEV_OC:
8594 rc = lpfc_pci_resume_one_s4(pdev);
8595 break;
3772a991
JS
8596 default:
8597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8598 "1426 Invalid PCI device group: 0x%x\n",
8599 phba->pci_dev_grp);
8600 break;
8601 }
8602 return rc;
8603}
8604
8605/**
8606 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8607 * @pdev: pointer to PCI device.
8608 * @state: the current PCI connection state.
8609 *
8610 * This routine is registered to the PCI subsystem for error handling. This
8611 * function is called by the PCI subsystem after a PCI bus error affecting
8612 * this device has been detected. When this routine is invoked, it dispatches
8613 * the action to the proper SLI-3 or SLI-4 device error detected handling
8614 * routine, which will perform the proper error detected operation.
8615 *
8616 * Return codes
8617 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8618 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8619 **/
8620static pci_ers_result_t
8621lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8622{
8623 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8624 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8625 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8626
8627 switch (phba->pci_dev_grp) {
8628 case LPFC_PCI_DEV_LP:
8629 rc = lpfc_io_error_detected_s3(pdev, state);
8630 break;
da0436e9
JS
8631 case LPFC_PCI_DEV_OC:
8632 rc = lpfc_io_error_detected_s4(pdev, state);
8633 break;
3772a991
JS
8634 default:
8635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8636 "1427 Invalid PCI device group: 0x%x\n",
8637 phba->pci_dev_grp);
8638 break;
8639 }
8640 return rc;
8641}
8642
8643/**
8644 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8645 * @pdev: pointer to PCI device.
8646 *
8647 * This routine is registered to the PCI subsystem for error handling. This
8648 * function is called after PCI bus has been reset to restart the PCI card
8649 * from scratch, as if from a cold-boot. When this routine is invoked, it
8650 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8651 * routine, which will perform the proper device reset.
8652 *
8653 * Return codes
8654 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8655 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8656 **/
8657static pci_ers_result_t
8658lpfc_io_slot_reset(struct pci_dev *pdev)
8659{
8660 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8661 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8662 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8663
8664 switch (phba->pci_dev_grp) {
8665 case LPFC_PCI_DEV_LP:
8666 rc = lpfc_io_slot_reset_s3(pdev);
8667 break;
da0436e9
JS
8668 case LPFC_PCI_DEV_OC:
8669 rc = lpfc_io_slot_reset_s4(pdev);
8670 break;
3772a991
JS
8671 default:
8672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8673 "1428 Invalid PCI device group: 0x%x\n",
8674 phba->pci_dev_grp);
8675 break;
8676 }
8677 return rc;
8678}
8679
8680/**
8681 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8682 * @pdev: pointer to PCI device
8683 *
8684 * This routine is registered to the PCI subsystem for error handling. It
8685 * is called when kernel error recovery tells the lpfc driver that it is
8686 * OK to resume normal PCI operation after PCI bus error recovery. When
8687 * this routine is invoked, it dispatches the action to the proper SLI-3
8688 * or SLI-4 device io_resume routine, which will resume the device operation.
8689 **/
8690static void
8691lpfc_io_resume(struct pci_dev *pdev)
8692{
8693 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8694 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8695
8696 switch (phba->pci_dev_grp) {
8697 case LPFC_PCI_DEV_LP:
8698 lpfc_io_resume_s3(pdev);
8699 break;
da0436e9
JS
8700 case LPFC_PCI_DEV_OC:
8701 lpfc_io_resume_s4(pdev);
8702 break;
3772a991
JS
8703 default:
8704 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8705 "1429 Invalid PCI device group: 0x%x\n",
8706 phba->pci_dev_grp);
8707 break;
8708 }
8709 return;
8710}
8711
dea3101e 8712static struct pci_device_id lpfc_id_table[] = {
8713 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8714 PCI_ANY_ID, PCI_ANY_ID, },
06325e74
JSEC
8715 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8716 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8717 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8718 PCI_ANY_ID, PCI_ANY_ID, },
8719 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8720 PCI_ANY_ID, PCI_ANY_ID, },
8721 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8722 PCI_ANY_ID, PCI_ANY_ID, },
8723 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8724 PCI_ANY_ID, PCI_ANY_ID, },
8725 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8726 PCI_ANY_ID, PCI_ANY_ID, },
8727 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8728 PCI_ANY_ID, PCI_ANY_ID, },
8729 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8730 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
8731 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8732 PCI_ANY_ID, PCI_ANY_ID, },
8733 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8734 PCI_ANY_ID, PCI_ANY_ID, },
8735 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8736 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8737 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8738 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
8739 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8740 PCI_ANY_ID, PCI_ANY_ID, },
8741 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8742 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8743 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8744 PCI_ANY_ID, PCI_ANY_ID, },
8745 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8746 PCI_ANY_ID, PCI_ANY_ID, },
8747 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8748 PCI_ANY_ID, PCI_ANY_ID, },
84774a4d
JS
8749 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8750 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
8751 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8752 PCI_ANY_ID, PCI_ANY_ID, },
8753 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8754 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8755 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8756 PCI_ANY_ID, PCI_ANY_ID, },
8757 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8758 PCI_ANY_ID, PCI_ANY_ID, },
8759 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8760 PCI_ANY_ID, PCI_ANY_ID, },
8761 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8762 PCI_ANY_ID, PCI_ANY_ID, },
8763 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8764 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
8765 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8766 PCI_ANY_ID, PCI_ANY_ID, },
8767 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8768 PCI_ANY_ID, PCI_ANY_ID, },
b87eab38
JS
8769 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8770 PCI_ANY_ID, PCI_ANY_ID, },
8771 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8772 PCI_ANY_ID, PCI_ANY_ID, },
8773 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8774 PCI_ANY_ID, PCI_ANY_ID, },
8775 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8776 PCI_ANY_ID, PCI_ANY_ID, },
8777 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8778 PCI_ANY_ID, PCI_ANY_ID, },
8779 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8780 PCI_ANY_ID, PCI_ANY_ID, },
84774a4d
JS
8781 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8782 PCI_ANY_ID, PCI_ANY_ID, },
8783 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8784 PCI_ANY_ID, PCI_ANY_ID, },
8785 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8786 PCI_ANY_ID, PCI_ANY_ID, },
3772a991
JS
8787 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8788 PCI_ANY_ID, PCI_ANY_ID, },
a747c9ce
JS
8789 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8790 PCI_ANY_ID, PCI_ANY_ID, },
8791 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
6669f9bb 8792 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8793 { 0 }
8794};
8795
8796MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8797
8d63f375
LV
8798static struct pci_error_handlers lpfc_err_handler = {
8799 .error_detected = lpfc_io_error_detected,
8800 .slot_reset = lpfc_io_slot_reset,
8801 .resume = lpfc_io_resume,
8802};
8803
dea3101e 8804static struct pci_driver lpfc_driver = {
8805 .name = LPFC_DRIVER_NAME,
8806 .id_table = lpfc_id_table,
8807 .probe = lpfc_pci_probe_one,
8808 .remove = __devexit_p(lpfc_pci_remove_one),
3a55b532 8809 .suspend = lpfc_pci_suspend_one,
3772a991 8810 .resume = lpfc_pci_resume_one,
2e0fef85 8811 .err_handler = &lpfc_err_handler,
dea3101e 8812};
8813
e59058c4 8814/**
3621a710 8815 * lpfc_init - lpfc module initialization routine
e59058c4
JS
8816 *
8817 * This routine is to be invoked when the lpfc module is loaded into the
8818 * kernel. The special kernel macro module_init() is used to indicate the
8819 * role of this routine to the kernel as lpfc module entry point.
8820 *
8821 * Return codes
8822 * 0 - successful
8823 * -ENOMEM - FC attach transport failed
8824 * all others - failed
8825 */
dea3101e 8826static int __init
8827lpfc_init(void)
8828{
8829 int error = 0;
8830
8831 printk(LPFC_MODULE_DESC "\n");
c44ce173 8832 printk(LPFC_COPYRIGHT "\n");
dea3101e 8833
7ee5d43e
JS
8834 if (lpfc_enable_npiv) {
8835 lpfc_transport_functions.vport_create = lpfc_vport_create;
8836 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8837 }
dea3101e 8838 lpfc_transport_template =
8839 fc_attach_transport(&lpfc_transport_functions);
7ee5d43e 8840 if (lpfc_transport_template == NULL)
dea3101e 8841 return -ENOMEM;
7ee5d43e 8842 if (lpfc_enable_npiv) {
7ee5d43e 8843 lpfc_vport_transport_template =
98c9ea5c
JS
8844 fc_attach_transport(&lpfc_vport_transport_functions);
8845 if (lpfc_vport_transport_template == NULL) {
8846 fc_release_transport(lpfc_transport_template);
7ee5d43e 8847 return -ENOMEM;
98c9ea5c 8848 }
7ee5d43e 8849 }
dea3101e 8850 error = pci_register_driver(&lpfc_driver);
92d7f7b0 8851 if (error) {
dea3101e 8852 fc_release_transport(lpfc_transport_template);
d7c255b2
JS
8853 if (lpfc_enable_npiv)
8854 fc_release_transport(lpfc_vport_transport_template);
92d7f7b0 8855 }
dea3101e 8856
8857 return error;
8858}
8859
e59058c4 8860/**
3621a710 8861 * lpfc_exit - lpfc module removal routine
e59058c4
JS
8862 *
8863 * This routine is invoked when the lpfc module is removed from the kernel.
8864 * The special kernel macro module_exit() is used to indicate the role of
8865 * this routine to the kernel as lpfc module exit point.
8866 */
dea3101e 8867static void __exit
8868lpfc_exit(void)
8869{
8870 pci_unregister_driver(&lpfc_driver);
8871 fc_release_transport(lpfc_transport_template);
7ee5d43e
JS
8872 if (lpfc_enable_npiv)
8873 fc_release_transport(lpfc_vport_transport_template);
81301a9b 8874 if (_dump_buf_data) {
6a9c52cf
JS
8875 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8876 "_dump_buf_data at 0x%p\n",
81301a9b
JS
8877 (1L << _dump_buf_data_order), _dump_buf_data);
8878 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8879 }
8880
8881 if (_dump_buf_dif) {
6a9c52cf
JS
8882 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8883 "_dump_buf_dif at 0x%p\n",
81301a9b
JS
8884 (1L << _dump_buf_dif_order), _dump_buf_dif);
8885 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8886 }
dea3101e 8887}
8888
8889module_init(lpfc_init);
8890module_exit(lpfc_exit);
8891MODULE_LICENSE("GPL");
8892MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8893MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8894MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
This page took 1.010826 seconds and 5 git commands to generate.