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