Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / scsi / lpfc / lpfc_init.c
blob4ab3d2252313ee0a55750c86e5b099a5ec1efed8
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2011 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
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. *
20 *******************************************************************/
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/module.h>
28 #include <linux/kthread.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
31 #include <linux/ctype.h>
32 #include <linux/aer.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_device.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_transport_fc.h>
41 #include "lpfc_hw4.h"
42 #include "lpfc_hw.h"
43 #include "lpfc_sli.h"
44 #include "lpfc_sli4.h"
45 #include "lpfc_nl.h"
46 #include "lpfc_disc.h"
47 #include "lpfc_scsi.h"
48 #include "lpfc.h"
49 #include "lpfc_logmsg.h"
50 #include "lpfc_crtn.h"
51 #include "lpfc_vport.h"
52 #include "lpfc_version.h"
54 char *_dump_buf_data;
55 unsigned long _dump_buf_data_order;
56 char *_dump_buf_dif;
57 unsigned long _dump_buf_dif_order;
58 spinlock_t _dump_buf_lock;
60 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
61 static int lpfc_post_rcv_buf(struct lpfc_hba *);
62 static int lpfc_sli4_queue_create(struct lpfc_hba *);
63 static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
64 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
65 static int lpfc_setup_endian_order(struct lpfc_hba *);
66 static int lpfc_sli4_read_config(struct lpfc_hba *);
67 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
68 static void lpfc_free_sgl_list(struct lpfc_hba *);
69 static int lpfc_init_sgl_list(struct lpfc_hba *);
70 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
71 static void lpfc_free_active_sgl(struct lpfc_hba *);
72 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
73 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
74 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
75 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
76 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
78 static struct scsi_transport_template *lpfc_transport_template = NULL;
79 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
80 static DEFINE_IDR(lpfc_hba_index);
82 /**
83 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
84 * @phba: pointer to lpfc hba data structure.
86 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
87 * mailbox command. It retrieves the revision information from the HBA and
88 * collects the Vital Product Data (VPD) about the HBA for preparing the
89 * configuration of the HBA.
91 * Return codes:
92 * 0 - success.
93 * -ERESTART - requests the SLI layer to reset the HBA and try again.
94 * Any other value - indicates an error.
95 **/
96 int
97 lpfc_config_port_prep(struct lpfc_hba *phba)
99 lpfc_vpd_t *vp = &phba->vpd;
100 int i = 0, rc;
101 LPFC_MBOXQ_t *pmb;
102 MAILBOX_t *mb;
103 char *lpfc_vpd_data = NULL;
104 uint16_t offset = 0;
105 static char licensed[56] =
106 "key unlock for use with gnu public licensed code only\0";
107 static int init_key = 1;
109 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
110 if (!pmb) {
111 phba->link_state = LPFC_HBA_ERROR;
112 return -ENOMEM;
115 mb = &pmb->u.mb;
116 phba->link_state = LPFC_INIT_MBX_CMDS;
118 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
119 if (init_key) {
120 uint32_t *ptext = (uint32_t *) licensed;
122 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
123 *ptext = cpu_to_be32(*ptext);
124 init_key = 0;
127 lpfc_read_nv(phba, pmb);
128 memset((char*)mb->un.varRDnvp.rsvd3, 0,
129 sizeof (mb->un.varRDnvp.rsvd3));
130 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
131 sizeof (licensed));
133 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
135 if (rc != MBX_SUCCESS) {
136 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
137 "0324 Config Port initialization "
138 "error, mbxCmd x%x READ_NVPARM, "
139 "mbxStatus x%x\n",
140 mb->mbxCommand, mb->mbxStatus);
141 mempool_free(pmb, phba->mbox_mem_pool);
142 return -ERESTART;
144 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
145 sizeof(phba->wwnn));
146 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
147 sizeof(phba->wwpn));
150 phba->sli3_options = 0x0;
152 /* Setup and issue mailbox READ REV command */
153 lpfc_read_rev(phba, pmb);
154 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
155 if (rc != MBX_SUCCESS) {
156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
157 "0439 Adapter failed to init, mbxCmd x%x "
158 "READ_REV, mbxStatus x%x\n",
159 mb->mbxCommand, mb->mbxStatus);
160 mempool_free( pmb, phba->mbox_mem_pool);
161 return -ERESTART;
166 * The value of rr must be 1 since the driver set the cv field to 1.
167 * This setting requires the FW to set all revision fields.
169 if (mb->un.varRdRev.rr == 0) {
170 vp->rev.rBit = 0;
171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
172 "0440 Adapter failed to init, READ_REV has "
173 "missing revision information.\n");
174 mempool_free(pmb, phba->mbox_mem_pool);
175 return -ERESTART;
178 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
179 mempool_free(pmb, phba->mbox_mem_pool);
180 return -EINVAL;
183 /* Save information as VPD data */
184 vp->rev.rBit = 1;
185 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
186 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
187 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
188 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
189 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
190 vp->rev.biuRev = mb->un.varRdRev.biuRev;
191 vp->rev.smRev = mb->un.varRdRev.smRev;
192 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
193 vp->rev.endecRev = mb->un.varRdRev.endecRev;
194 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
195 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
196 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
197 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
198 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
199 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
201 /* If the sli feature level is less then 9, we must
202 * tear down all RPIs and VPIs on link down if NPIV
203 * is enabled.
205 if (vp->rev.feaLevelHigh < 9)
206 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
208 if (lpfc_is_LC_HBA(phba->pcidev->device))
209 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
210 sizeof (phba->RandomData));
212 /* Get adapter VPD information */
213 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
214 if (!lpfc_vpd_data)
215 goto out_free_mbox;
216 do {
217 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
218 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
220 if (rc != MBX_SUCCESS) {
221 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
222 "0441 VPD not present on adapter, "
223 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
224 mb->mbxCommand, mb->mbxStatus);
225 mb->un.varDmp.word_cnt = 0;
227 /* dump mem may return a zero when finished or we got a
228 * mailbox error, either way we are done.
230 if (mb->un.varDmp.word_cnt == 0)
231 break;
232 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
233 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
234 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
235 lpfc_vpd_data + offset,
236 mb->un.varDmp.word_cnt);
237 offset += mb->un.varDmp.word_cnt;
238 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
239 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
241 kfree(lpfc_vpd_data);
242 out_free_mbox:
243 mempool_free(pmb, phba->mbox_mem_pool);
244 return 0;
248 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
249 * @phba: pointer to lpfc hba data structure.
250 * @pmboxq: pointer to the driver internal queue element for mailbox command.
252 * This is the completion handler for driver's configuring asynchronous event
253 * mailbox command to the device. If the mailbox command returns successfully,
254 * it will set internal async event support flag to 1; otherwise, it will
255 * set internal async event support flag to 0.
257 static void
258 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
260 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
261 phba->temp_sensor_support = 1;
262 else
263 phba->temp_sensor_support = 0;
264 mempool_free(pmboxq, phba->mbox_mem_pool);
265 return;
269 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
270 * @phba: pointer to lpfc hba data structure.
271 * @pmboxq: pointer to the driver internal queue element for mailbox command.
273 * This is the completion handler for dump mailbox command for getting
274 * wake up parameters. When this command complete, the response contain
275 * Option rom version of the HBA. This function translate the version number
276 * into a human readable string and store it in OptionROMVersion.
278 static void
279 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
281 struct prog_id *prg;
282 uint32_t prog_id_word;
283 char dist = ' ';
284 /* character array used for decoding dist type. */
285 char dist_char[] = "nabx";
287 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
288 mempool_free(pmboxq, phba->mbox_mem_pool);
289 return;
292 prg = (struct prog_id *) &prog_id_word;
294 /* word 7 contain option rom version */
295 prog_id_word = pmboxq->u.mb.un.varWords[7];
297 /* Decode the Option rom version word to a readable string */
298 if (prg->dist < 4)
299 dist = dist_char[prg->dist];
301 if ((prg->dist == 3) && (prg->num == 0))
302 sprintf(phba->OptionROMVersion, "%d.%d%d",
303 prg->ver, prg->rev, prg->lev);
304 else
305 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
306 prg->ver, prg->rev, prg->lev,
307 dist, prg->num);
308 mempool_free(pmboxq, phba->mbox_mem_pool);
309 return;
313 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
314 * cfg_soft_wwnn, cfg_soft_wwpn
315 * @vport: pointer to lpfc vport data structure.
318 * Return codes
319 * None.
321 void
322 lpfc_update_vport_wwn(struct lpfc_vport *vport)
324 /* If the soft name exists then update it using the service params */
325 if (vport->phba->cfg_soft_wwnn)
326 u64_to_wwn(vport->phba->cfg_soft_wwnn,
327 vport->fc_sparam.nodeName.u.wwn);
328 if (vport->phba->cfg_soft_wwpn)
329 u64_to_wwn(vport->phba->cfg_soft_wwpn,
330 vport->fc_sparam.portName.u.wwn);
333 * If the name is empty or there exists a soft name
334 * then copy the service params name, otherwise use the fc name
336 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
337 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
338 sizeof(struct lpfc_name));
339 else
340 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
341 sizeof(struct lpfc_name));
343 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
344 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
345 sizeof(struct lpfc_name));
346 else
347 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
348 sizeof(struct lpfc_name));
352 * lpfc_config_port_post - Perform lpfc initialization after config port
353 * @phba: pointer to lpfc hba data structure.
355 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
356 * command call. It performs all internal resource and state setups on the
357 * port: post IOCB buffers, enable appropriate host interrupt attentions,
358 * ELS ring timers, etc.
360 * Return codes
361 * 0 - success.
362 * Any other value - error.
365 lpfc_config_port_post(struct lpfc_hba *phba)
367 struct lpfc_vport *vport = phba->pport;
368 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
369 LPFC_MBOXQ_t *pmb;
370 MAILBOX_t *mb;
371 struct lpfc_dmabuf *mp;
372 struct lpfc_sli *psli = &phba->sli;
373 uint32_t status, timeout;
374 int i, j;
375 int rc;
377 spin_lock_irq(&phba->hbalock);
379 * If the Config port completed correctly the HBA is not
380 * over heated any more.
382 if (phba->over_temp_state == HBA_OVER_TEMP)
383 phba->over_temp_state = HBA_NORMAL_TEMP;
384 spin_unlock_irq(&phba->hbalock);
386 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
387 if (!pmb) {
388 phba->link_state = LPFC_HBA_ERROR;
389 return -ENOMEM;
391 mb = &pmb->u.mb;
393 /* Get login parameters for NID. */
394 rc = lpfc_read_sparam(phba, pmb, 0);
395 if (rc) {
396 mempool_free(pmb, phba->mbox_mem_pool);
397 return -ENOMEM;
400 pmb->vport = vport;
401 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
403 "0448 Adapter failed init, mbxCmd x%x "
404 "READ_SPARM mbxStatus x%x\n",
405 mb->mbxCommand, mb->mbxStatus);
406 phba->link_state = LPFC_HBA_ERROR;
407 mp = (struct lpfc_dmabuf *) pmb->context1;
408 mempool_free(pmb, phba->mbox_mem_pool);
409 lpfc_mbuf_free(phba, mp->virt, mp->phys);
410 kfree(mp);
411 return -EIO;
414 mp = (struct lpfc_dmabuf *) pmb->context1;
416 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
417 lpfc_mbuf_free(phba, mp->virt, mp->phys);
418 kfree(mp);
419 pmb->context1 = NULL;
420 lpfc_update_vport_wwn(vport);
422 /* Update the fc_host data structures with new wwn. */
423 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
424 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
425 fc_host_max_npiv_vports(shost) = phba->max_vpi;
427 /* If no serial number in VPD data, use low 6 bytes of WWNN */
428 /* This should be consolidated into parse_vpd ? - mr */
429 if (phba->SerialNumber[0] == 0) {
430 uint8_t *outptr;
432 outptr = &vport->fc_nodename.u.s.IEEE[0];
433 for (i = 0; i < 12; i++) {
434 status = *outptr++;
435 j = ((status & 0xf0) >> 4);
436 if (j <= 9)
437 phba->SerialNumber[i] =
438 (char)((uint8_t) 0x30 + (uint8_t) j);
439 else
440 phba->SerialNumber[i] =
441 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
442 i++;
443 j = (status & 0xf);
444 if (j <= 9)
445 phba->SerialNumber[i] =
446 (char)((uint8_t) 0x30 + (uint8_t) j);
447 else
448 phba->SerialNumber[i] =
449 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
453 lpfc_read_config(phba, pmb);
454 pmb->vport = vport;
455 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
457 "0453 Adapter failed to init, mbxCmd x%x "
458 "READ_CONFIG, mbxStatus x%x\n",
459 mb->mbxCommand, mb->mbxStatus);
460 phba->link_state = LPFC_HBA_ERROR;
461 mempool_free( pmb, phba->mbox_mem_pool);
462 return -EIO;
465 /* Check if the port is disabled */
466 lpfc_sli_read_link_ste(phba);
468 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
469 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
470 phba->cfg_hba_queue_depth =
471 (mb->un.varRdConfig.max_xri + 1) -
472 lpfc_sli4_get_els_iocb_cnt(phba);
474 phba->lmt = mb->un.varRdConfig.lmt;
476 /* Get the default values for Model Name and Description */
477 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
479 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_16G)
480 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G)
481 && !(phba->lmt & LMT_1Gb))
482 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G)
483 && !(phba->lmt & LMT_2Gb))
484 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G)
485 && !(phba->lmt & LMT_4Gb))
486 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G)
487 && !(phba->lmt & LMT_8Gb))
488 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G)
489 && !(phba->lmt & LMT_10Gb))
490 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G)
491 && !(phba->lmt & LMT_16Gb))) {
492 /* Reset link speed to auto */
493 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
494 "1302 Invalid speed for this board: "
495 "Reset link speed to auto: x%x\n",
496 phba->cfg_link_speed);
497 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
500 phba->link_state = LPFC_LINK_DOWN;
502 /* Only process IOCBs on ELS ring till hba_state is READY */
503 if (psli->ring[psli->extra_ring].cmdringaddr)
504 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
505 if (psli->ring[psli->fcp_ring].cmdringaddr)
506 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
507 if (psli->ring[psli->next_ring].cmdringaddr)
508 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
510 /* Post receive buffers for desired rings */
511 if (phba->sli_rev != 3)
512 lpfc_post_rcv_buf(phba);
515 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
517 if (phba->intr_type == MSIX) {
518 rc = lpfc_config_msi(phba, pmb);
519 if (rc) {
520 mempool_free(pmb, phba->mbox_mem_pool);
521 return -EIO;
523 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
524 if (rc != MBX_SUCCESS) {
525 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
526 "0352 Config MSI mailbox command "
527 "failed, mbxCmd x%x, mbxStatus x%x\n",
528 pmb->u.mb.mbxCommand,
529 pmb->u.mb.mbxStatus);
530 mempool_free(pmb, phba->mbox_mem_pool);
531 return -EIO;
535 spin_lock_irq(&phba->hbalock);
536 /* Initialize ERATT handling flag */
537 phba->hba_flag &= ~HBA_ERATT_HANDLED;
539 /* Enable appropriate host interrupts */
540 if (lpfc_readl(phba->HCregaddr, &status)) {
541 spin_unlock_irq(&phba->hbalock);
542 return -EIO;
544 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
545 if (psli->num_rings > 0)
546 status |= HC_R0INT_ENA;
547 if (psli->num_rings > 1)
548 status |= HC_R1INT_ENA;
549 if (psli->num_rings > 2)
550 status |= HC_R2INT_ENA;
551 if (psli->num_rings > 3)
552 status |= HC_R3INT_ENA;
554 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
555 (phba->cfg_poll & DISABLE_FCP_RING_INT))
556 status &= ~(HC_R0INT_ENA);
558 writel(status, phba->HCregaddr);
559 readl(phba->HCregaddr); /* flush */
560 spin_unlock_irq(&phba->hbalock);
562 /* Set up ring-0 (ELS) timer */
563 timeout = phba->fc_ratov * 2;
564 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
565 /* Set up heart beat (HB) timer */
566 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
567 phba->hb_outstanding = 0;
568 phba->last_completion_time = jiffies;
569 /* Set up error attention (ERATT) polling timer */
570 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
572 if (phba->hba_flag & LINK_DISABLED) {
573 lpfc_printf_log(phba,
574 KERN_ERR, LOG_INIT,
575 "2598 Adapter Link is disabled.\n");
576 lpfc_down_link(phba, pmb);
577 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
578 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
579 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
580 lpfc_printf_log(phba,
581 KERN_ERR, LOG_INIT,
582 "2599 Adapter failed to issue DOWN_LINK"
583 " mbox command rc 0x%x\n", rc);
585 mempool_free(pmb, phba->mbox_mem_pool);
586 return -EIO;
588 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
589 lpfc_init_link(phba, pmb, phba->cfg_topology,
590 phba->cfg_link_speed);
591 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
592 lpfc_set_loopback_flag(phba);
593 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
594 if (rc != MBX_SUCCESS) {
595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
596 "0454 Adapter failed to init, mbxCmd x%x "
597 "INIT_LINK, mbxStatus x%x\n",
598 mb->mbxCommand, mb->mbxStatus);
600 /* Clear all interrupt enable conditions */
601 writel(0, phba->HCregaddr);
602 readl(phba->HCregaddr); /* flush */
603 /* Clear all pending interrupts */
604 writel(0xffffffff, phba->HAregaddr);
605 readl(phba->HAregaddr); /* flush */
606 phba->link_state = LPFC_HBA_ERROR;
607 if (rc != MBX_BUSY)
608 mempool_free(pmb, phba->mbox_mem_pool);
609 return -EIO;
612 /* MBOX buffer will be freed in mbox compl */
613 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
614 if (!pmb) {
615 phba->link_state = LPFC_HBA_ERROR;
616 return -ENOMEM;
619 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
620 pmb->mbox_cmpl = lpfc_config_async_cmpl;
621 pmb->vport = phba->pport;
622 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
624 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
625 lpfc_printf_log(phba,
626 KERN_ERR,
627 LOG_INIT,
628 "0456 Adapter failed to issue "
629 "ASYNCEVT_ENABLE mbox status x%x\n",
630 rc);
631 mempool_free(pmb, phba->mbox_mem_pool);
634 /* Get Option rom version */
635 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
636 if (!pmb) {
637 phba->link_state = LPFC_HBA_ERROR;
638 return -ENOMEM;
641 lpfc_dump_wakeup_param(phba, pmb);
642 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
643 pmb->vport = phba->pport;
644 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
646 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
648 "to get Option ROM version status x%x\n", rc);
649 mempool_free(pmb, phba->mbox_mem_pool);
652 return 0;
656 * lpfc_hba_init_link - Initialize the FC link
657 * @phba: pointer to lpfc hba data structure.
658 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
660 * This routine will issue the INIT_LINK mailbox command call.
661 * It is available to other drivers through the lpfc_hba data
662 * structure for use as a delayed link up mechanism with the
663 * module parameter lpfc_suppress_link_up.
665 * Return code
666 * 0 - success
667 * Any other value - error
670 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
672 struct lpfc_vport *vport = phba->pport;
673 LPFC_MBOXQ_t *pmb;
674 MAILBOX_t *mb;
675 int rc;
677 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
678 if (!pmb) {
679 phba->link_state = LPFC_HBA_ERROR;
680 return -ENOMEM;
682 mb = &pmb->u.mb;
683 pmb->vport = vport;
685 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
686 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
687 lpfc_set_loopback_flag(phba);
688 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
689 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
691 "0498 Adapter failed to init, mbxCmd x%x "
692 "INIT_LINK, mbxStatus x%x\n",
693 mb->mbxCommand, mb->mbxStatus);
694 if (phba->sli_rev <= LPFC_SLI_REV3) {
695 /* Clear all interrupt enable conditions */
696 writel(0, phba->HCregaddr);
697 readl(phba->HCregaddr); /* flush */
698 /* Clear all pending interrupts */
699 writel(0xffffffff, phba->HAregaddr);
700 readl(phba->HAregaddr); /* flush */
702 phba->link_state = LPFC_HBA_ERROR;
703 if (rc != MBX_BUSY || flag == MBX_POLL)
704 mempool_free(pmb, phba->mbox_mem_pool);
705 return -EIO;
707 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
708 if (flag == MBX_POLL)
709 mempool_free(pmb, phba->mbox_mem_pool);
711 return 0;
715 * lpfc_hba_down_link - this routine downs the FC link
716 * @phba: pointer to lpfc hba data structure.
717 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
719 * This routine will issue the DOWN_LINK mailbox command call.
720 * It is available to other drivers through the lpfc_hba data
721 * structure for use to stop the link.
723 * Return code
724 * 0 - success
725 * Any other value - error
728 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
730 LPFC_MBOXQ_t *pmb;
731 int rc;
733 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
734 if (!pmb) {
735 phba->link_state = LPFC_HBA_ERROR;
736 return -ENOMEM;
739 lpfc_printf_log(phba,
740 KERN_ERR, LOG_INIT,
741 "0491 Adapter Link is disabled.\n");
742 lpfc_down_link(phba, pmb);
743 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
744 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
745 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
746 lpfc_printf_log(phba,
747 KERN_ERR, LOG_INIT,
748 "2522 Adapter failed to issue DOWN_LINK"
749 " mbox command rc 0x%x\n", rc);
751 mempool_free(pmb, phba->mbox_mem_pool);
752 return -EIO;
754 if (flag == MBX_POLL)
755 mempool_free(pmb, phba->mbox_mem_pool);
757 return 0;
761 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
762 * @phba: pointer to lpfc HBA data structure.
764 * This routine will do LPFC uninitialization before the HBA is reset when
765 * bringing down the SLI Layer.
767 * Return codes
768 * 0 - success.
769 * Any other value - error.
772 lpfc_hba_down_prep(struct lpfc_hba *phba)
774 struct lpfc_vport **vports;
775 int i;
777 if (phba->sli_rev <= LPFC_SLI_REV3) {
778 /* Disable interrupts */
779 writel(0, phba->HCregaddr);
780 readl(phba->HCregaddr); /* flush */
783 if (phba->pport->load_flag & FC_UNLOADING)
784 lpfc_cleanup_discovery_resources(phba->pport);
785 else {
786 vports = lpfc_create_vport_work_array(phba);
787 if (vports != NULL)
788 for (i = 0; i <= phba->max_vports &&
789 vports[i] != NULL; i++)
790 lpfc_cleanup_discovery_resources(vports[i]);
791 lpfc_destroy_vport_work_array(phba, vports);
793 return 0;
797 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
798 * @phba: pointer to lpfc HBA data structure.
800 * This routine will do uninitialization after the HBA is reset when bring
801 * down the SLI Layer.
803 * Return codes
804 * 0 - success.
805 * Any other value - error.
807 static int
808 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
810 struct lpfc_sli *psli = &phba->sli;
811 struct lpfc_sli_ring *pring;
812 struct lpfc_dmabuf *mp, *next_mp;
813 LIST_HEAD(completions);
814 int i;
816 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
817 lpfc_sli_hbqbuf_free_all(phba);
818 else {
819 /* Cleanup preposted buffers on the ELS ring */
820 pring = &psli->ring[LPFC_ELS_RING];
821 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
822 list_del(&mp->list);
823 pring->postbufq_cnt--;
824 lpfc_mbuf_free(phba, mp->virt, mp->phys);
825 kfree(mp);
829 spin_lock_irq(&phba->hbalock);
830 for (i = 0; i < psli->num_rings; i++) {
831 pring = &psli->ring[i];
833 /* At this point in time the HBA is either reset or DOA. Either
834 * way, nothing should be on txcmplq as it will NEVER complete.
836 list_splice_init(&pring->txcmplq, &completions);
837 pring->txcmplq_cnt = 0;
838 spin_unlock_irq(&phba->hbalock);
840 /* Cancel all the IOCBs from the completions list */
841 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
842 IOERR_SLI_ABORTED);
844 lpfc_sli_abort_iocb_ring(phba, pring);
845 spin_lock_irq(&phba->hbalock);
847 spin_unlock_irq(&phba->hbalock);
849 return 0;
853 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
854 * @phba: pointer to lpfc HBA data structure.
856 * This routine will do uninitialization after the HBA is reset when bring
857 * down the SLI Layer.
859 * Return codes
860 * 0 - success.
861 * Any other value - error.
863 static int
864 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
866 struct lpfc_scsi_buf *psb, *psb_next;
867 LIST_HEAD(aborts);
868 int ret;
869 unsigned long iflag = 0;
870 struct lpfc_sglq *sglq_entry = NULL;
872 ret = lpfc_hba_down_post_s3(phba);
873 if (ret)
874 return ret;
875 /* At this point in time the HBA is either reset or DOA. Either
876 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
877 * on the lpfc_sgl_list so that it can either be freed if the
878 * driver is unloading or reposted if the driver is restarting
879 * the port.
881 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
882 /* scsl_buf_list */
883 /* abts_sgl_list_lock required because worker thread uses this
884 * list.
886 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
887 list_for_each_entry(sglq_entry,
888 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
889 sglq_entry->state = SGL_FREED;
891 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
892 &phba->sli4_hba.lpfc_sgl_list);
893 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
894 /* abts_scsi_buf_list_lock required because worker thread uses this
895 * list.
897 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
898 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
899 &aborts);
900 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
901 spin_unlock_irq(&phba->hbalock);
903 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
904 psb->pCmd = NULL;
905 psb->status = IOSTAT_SUCCESS;
907 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
908 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
909 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
910 return 0;
914 * lpfc_hba_down_post - Wrapper func for hba down post routine
915 * @phba: pointer to lpfc HBA data structure.
917 * This routine wraps the actual SLI3 or SLI4 routine for performing
918 * uninitialization after the HBA is reset when bring down the SLI Layer.
920 * Return codes
921 * 0 - success.
922 * Any other value - error.
925 lpfc_hba_down_post(struct lpfc_hba *phba)
927 return (*phba->lpfc_hba_down_post)(phba);
931 * lpfc_hb_timeout - The HBA-timer timeout handler
932 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
934 * This is the HBA-timer timeout handler registered to the lpfc driver. When
935 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
936 * work-port-events bitmap and the worker thread is notified. This timeout
937 * event will be used by the worker thread to invoke the actual timeout
938 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
939 * be performed in the timeout handler and the HBA timeout event bit shall
940 * be cleared by the worker thread after it has taken the event bitmap out.
942 static void
943 lpfc_hb_timeout(unsigned long ptr)
945 struct lpfc_hba *phba;
946 uint32_t tmo_posted;
947 unsigned long iflag;
949 phba = (struct lpfc_hba *)ptr;
951 /* Check for heart beat timeout conditions */
952 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
953 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
954 if (!tmo_posted)
955 phba->pport->work_port_events |= WORKER_HB_TMO;
956 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
958 /* Tell the worker thread there is work to do */
959 if (!tmo_posted)
960 lpfc_worker_wake_up(phba);
961 return;
965 * lpfc_rrq_timeout - The RRQ-timer timeout handler
966 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
968 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
969 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
970 * work-port-events bitmap and the worker thread is notified. This timeout
971 * event will be used by the worker thread to invoke the actual timeout
972 * handler routine, lpfc_rrq_handler. Any periodical operations will
973 * be performed in the timeout handler and the RRQ timeout event bit shall
974 * be cleared by the worker thread after it has taken the event bitmap out.
976 static void
977 lpfc_rrq_timeout(unsigned long ptr)
979 struct lpfc_hba *phba;
980 unsigned long iflag;
982 phba = (struct lpfc_hba *)ptr;
983 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
984 phba->hba_flag |= HBA_RRQ_ACTIVE;
985 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
986 lpfc_worker_wake_up(phba);
990 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
991 * @phba: pointer to lpfc hba data structure.
992 * @pmboxq: pointer to the driver internal queue element for mailbox command.
994 * This is the callback function to the lpfc heart-beat mailbox command.
995 * If configured, the lpfc driver issues the heart-beat mailbox command to
996 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
997 * heart-beat mailbox command is issued, the driver shall set up heart-beat
998 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
999 * heart-beat outstanding state. Once the mailbox command comes back and
1000 * no error conditions detected, the heart-beat mailbox command timer is
1001 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1002 * state is cleared for the next heart-beat. If the timer expired with the
1003 * heart-beat outstanding state set, the driver will put the HBA offline.
1005 static void
1006 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1008 unsigned long drvr_flag;
1010 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1011 phba->hb_outstanding = 0;
1012 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1014 /* Check and reset heart-beat timer is necessary */
1015 mempool_free(pmboxq, phba->mbox_mem_pool);
1016 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1017 !(phba->link_state == LPFC_HBA_ERROR) &&
1018 !(phba->pport->load_flag & FC_UNLOADING))
1019 mod_timer(&phba->hb_tmofunc,
1020 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1021 return;
1025 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1026 * @phba: pointer to lpfc hba data structure.
1028 * This is the actual HBA-timer timeout handler to be invoked by the worker
1029 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1030 * handler performs any periodic operations needed for the device. If such
1031 * periodic event has already been attended to either in the interrupt handler
1032 * or by processing slow-ring or fast-ring events within the HBA-timer
1033 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1034 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1035 * is configured and there is no heart-beat mailbox command outstanding, a
1036 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1037 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1038 * to offline.
1040 void
1041 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1043 struct lpfc_vport **vports;
1044 LPFC_MBOXQ_t *pmboxq;
1045 struct lpfc_dmabuf *buf_ptr;
1046 int retval, i;
1047 struct lpfc_sli *psli = &phba->sli;
1048 LIST_HEAD(completions);
1050 vports = lpfc_create_vport_work_array(phba);
1051 if (vports != NULL)
1052 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1053 lpfc_rcv_seq_check_edtov(vports[i]);
1054 lpfc_destroy_vport_work_array(phba, vports);
1056 if ((phba->link_state == LPFC_HBA_ERROR) ||
1057 (phba->pport->load_flag & FC_UNLOADING) ||
1058 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1059 return;
1061 spin_lock_irq(&phba->pport->work_port_lock);
1063 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1064 jiffies)) {
1065 spin_unlock_irq(&phba->pport->work_port_lock);
1066 if (!phba->hb_outstanding)
1067 mod_timer(&phba->hb_tmofunc,
1068 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1069 else
1070 mod_timer(&phba->hb_tmofunc,
1071 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1072 return;
1074 spin_unlock_irq(&phba->pport->work_port_lock);
1076 if (phba->elsbuf_cnt &&
1077 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1078 spin_lock_irq(&phba->hbalock);
1079 list_splice_init(&phba->elsbuf, &completions);
1080 phba->elsbuf_cnt = 0;
1081 phba->elsbuf_prev_cnt = 0;
1082 spin_unlock_irq(&phba->hbalock);
1084 while (!list_empty(&completions)) {
1085 list_remove_head(&completions, buf_ptr,
1086 struct lpfc_dmabuf, list);
1087 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1088 kfree(buf_ptr);
1091 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1093 /* If there is no heart beat outstanding, issue a heartbeat command */
1094 if (phba->cfg_enable_hba_heartbeat) {
1095 if (!phba->hb_outstanding) {
1096 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1097 (list_empty(&psli->mboxq))) {
1098 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1099 GFP_KERNEL);
1100 if (!pmboxq) {
1101 mod_timer(&phba->hb_tmofunc,
1102 jiffies +
1103 HZ * LPFC_HB_MBOX_INTERVAL);
1104 return;
1107 lpfc_heart_beat(phba, pmboxq);
1108 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1109 pmboxq->vport = phba->pport;
1110 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1111 MBX_NOWAIT);
1113 if (retval != MBX_BUSY &&
1114 retval != MBX_SUCCESS) {
1115 mempool_free(pmboxq,
1116 phba->mbox_mem_pool);
1117 mod_timer(&phba->hb_tmofunc,
1118 jiffies +
1119 HZ * LPFC_HB_MBOX_INTERVAL);
1120 return;
1122 phba->skipped_hb = 0;
1123 phba->hb_outstanding = 1;
1124 } else if (time_before_eq(phba->last_completion_time,
1125 phba->skipped_hb)) {
1126 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1127 "2857 Last completion time not "
1128 " updated in %d ms\n",
1129 jiffies_to_msecs(jiffies
1130 - phba->last_completion_time));
1131 } else
1132 phba->skipped_hb = jiffies;
1134 mod_timer(&phba->hb_tmofunc,
1135 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1136 return;
1137 } else {
1139 * If heart beat timeout called with hb_outstanding set
1140 * we need to give the hb mailbox cmd a chance to
1141 * complete or TMO.
1143 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1144 "0459 Adapter heartbeat still out"
1145 "standing:last compl time was %d ms.\n",
1146 jiffies_to_msecs(jiffies
1147 - phba->last_completion_time));
1148 mod_timer(&phba->hb_tmofunc,
1149 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1155 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1156 * @phba: pointer to lpfc hba data structure.
1158 * This routine is called to bring the HBA offline when HBA hardware error
1159 * other than Port Error 6 has been detected.
1161 static void
1162 lpfc_offline_eratt(struct lpfc_hba *phba)
1164 struct lpfc_sli *psli = &phba->sli;
1166 spin_lock_irq(&phba->hbalock);
1167 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1168 spin_unlock_irq(&phba->hbalock);
1169 lpfc_offline_prep(phba);
1171 lpfc_offline(phba);
1172 lpfc_reset_barrier(phba);
1173 spin_lock_irq(&phba->hbalock);
1174 lpfc_sli_brdreset(phba);
1175 spin_unlock_irq(&phba->hbalock);
1176 lpfc_hba_down_post(phba);
1177 lpfc_sli_brdready(phba, HS_MBRDY);
1178 lpfc_unblock_mgmt_io(phba);
1179 phba->link_state = LPFC_HBA_ERROR;
1180 return;
1184 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1185 * @phba: pointer to lpfc hba data structure.
1187 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1188 * other than Port Error 6 has been detected.
1190 static void
1191 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1193 lpfc_offline_prep(phba);
1194 lpfc_offline(phba);
1195 lpfc_sli4_brdreset(phba);
1196 lpfc_hba_down_post(phba);
1197 lpfc_sli4_post_status_check(phba);
1198 lpfc_unblock_mgmt_io(phba);
1199 phba->link_state = LPFC_HBA_ERROR;
1203 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1204 * @phba: pointer to lpfc hba data structure.
1206 * This routine is invoked to handle the deferred HBA hardware error
1207 * conditions. This type of error is indicated by HBA by setting ER1
1208 * and another ER bit in the host status register. The driver will
1209 * wait until the ER1 bit clears before handling the error condition.
1211 static void
1212 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1214 uint32_t old_host_status = phba->work_hs;
1215 struct lpfc_sli_ring *pring;
1216 struct lpfc_sli *psli = &phba->sli;
1218 /* If the pci channel is offline, ignore possible errors,
1219 * since we cannot communicate with the pci card anyway.
1221 if (pci_channel_offline(phba->pcidev)) {
1222 spin_lock_irq(&phba->hbalock);
1223 phba->hba_flag &= ~DEFER_ERATT;
1224 spin_unlock_irq(&phba->hbalock);
1225 return;
1228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1229 "0479 Deferred Adapter Hardware Error "
1230 "Data: x%x x%x x%x\n",
1231 phba->work_hs,
1232 phba->work_status[0], phba->work_status[1]);
1234 spin_lock_irq(&phba->hbalock);
1235 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1236 spin_unlock_irq(&phba->hbalock);
1240 * Firmware stops when it triggred erratt. That could cause the I/Os
1241 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1242 * SCSI layer retry it after re-establishing link.
1244 pring = &psli->ring[psli->fcp_ring];
1245 lpfc_sli_abort_iocb_ring(phba, pring);
1248 * There was a firmware error. Take the hba offline and then
1249 * attempt to restart it.
1251 lpfc_offline_prep(phba);
1252 lpfc_offline(phba);
1254 /* Wait for the ER1 bit to clear.*/
1255 while (phba->work_hs & HS_FFER1) {
1256 msleep(100);
1257 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1258 phba->work_hs = UNPLUG_ERR ;
1259 break;
1261 /* If driver is unloading let the worker thread continue */
1262 if (phba->pport->load_flag & FC_UNLOADING) {
1263 phba->work_hs = 0;
1264 break;
1269 * This is to ptrotect against a race condition in which
1270 * first write to the host attention register clear the
1271 * host status register.
1273 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1274 phba->work_hs = old_host_status & ~HS_FFER1;
1276 spin_lock_irq(&phba->hbalock);
1277 phba->hba_flag &= ~DEFER_ERATT;
1278 spin_unlock_irq(&phba->hbalock);
1279 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1280 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1283 static void
1284 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1286 struct lpfc_board_event_header board_event;
1287 struct Scsi_Host *shost;
1289 board_event.event_type = FC_REG_BOARD_EVENT;
1290 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1291 shost = lpfc_shost_from_vport(phba->pport);
1292 fc_host_post_vendor_event(shost, fc_get_event_number(),
1293 sizeof(board_event),
1294 (char *) &board_event,
1295 LPFC_NL_VENDOR_ID);
1299 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1300 * @phba: pointer to lpfc hba data structure.
1302 * This routine is invoked to handle the following HBA hardware error
1303 * conditions:
1304 * 1 - HBA error attention interrupt
1305 * 2 - DMA ring index out of range
1306 * 3 - Mailbox command came back as unknown
1308 static void
1309 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1311 struct lpfc_vport *vport = phba->pport;
1312 struct lpfc_sli *psli = &phba->sli;
1313 struct lpfc_sli_ring *pring;
1314 uint32_t event_data;
1315 unsigned long temperature;
1316 struct temp_event temp_event_data;
1317 struct Scsi_Host *shost;
1319 /* If the pci channel is offline, ignore possible errors,
1320 * since we cannot communicate with the pci card anyway.
1322 if (pci_channel_offline(phba->pcidev)) {
1323 spin_lock_irq(&phba->hbalock);
1324 phba->hba_flag &= ~DEFER_ERATT;
1325 spin_unlock_irq(&phba->hbalock);
1326 return;
1329 /* If resets are disabled then leave the HBA alone and return */
1330 if (!phba->cfg_enable_hba_reset)
1331 return;
1333 /* Send an internal error event to mgmt application */
1334 lpfc_board_errevt_to_mgmt(phba);
1336 if (phba->hba_flag & DEFER_ERATT)
1337 lpfc_handle_deferred_eratt(phba);
1339 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1340 if (phba->work_hs & HS_FFER6)
1341 /* Re-establishing Link */
1342 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1343 "1301 Re-establishing Link "
1344 "Data: x%x x%x x%x\n",
1345 phba->work_hs, phba->work_status[0],
1346 phba->work_status[1]);
1347 if (phba->work_hs & HS_FFER8)
1348 /* Device Zeroization */
1349 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1350 "2861 Host Authentication device "
1351 "zeroization Data:x%x x%x x%x\n",
1352 phba->work_hs, phba->work_status[0],
1353 phba->work_status[1]);
1355 spin_lock_irq(&phba->hbalock);
1356 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1357 spin_unlock_irq(&phba->hbalock);
1360 * Firmware stops when it triggled erratt with HS_FFER6.
1361 * That could cause the I/Os dropped by the firmware.
1362 * Error iocb (I/O) on txcmplq and let the SCSI layer
1363 * retry it after re-establishing link.
1365 pring = &psli->ring[psli->fcp_ring];
1366 lpfc_sli_abort_iocb_ring(phba, pring);
1369 * There was a firmware error. Take the hba offline and then
1370 * attempt to restart it.
1372 lpfc_offline_prep(phba);
1373 lpfc_offline(phba);
1374 lpfc_sli_brdrestart(phba);
1375 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
1376 lpfc_unblock_mgmt_io(phba);
1377 return;
1379 lpfc_unblock_mgmt_io(phba);
1380 } else if (phba->work_hs & HS_CRIT_TEMP) {
1381 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1382 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1383 temp_event_data.event_code = LPFC_CRIT_TEMP;
1384 temp_event_data.data = (uint32_t)temperature;
1386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1387 "0406 Adapter maximum temperature exceeded "
1388 "(%ld), taking this port offline "
1389 "Data: x%x x%x x%x\n",
1390 temperature, phba->work_hs,
1391 phba->work_status[0], phba->work_status[1]);
1393 shost = lpfc_shost_from_vport(phba->pport);
1394 fc_host_post_vendor_event(shost, fc_get_event_number(),
1395 sizeof(temp_event_data),
1396 (char *) &temp_event_data,
1397 SCSI_NL_VID_TYPE_PCI
1398 | PCI_VENDOR_ID_EMULEX);
1400 spin_lock_irq(&phba->hbalock);
1401 phba->over_temp_state = HBA_OVER_TEMP;
1402 spin_unlock_irq(&phba->hbalock);
1403 lpfc_offline_eratt(phba);
1405 } else {
1406 /* The if clause above forces this code path when the status
1407 * failure is a value other than FFER6. Do not call the offline
1408 * twice. This is the adapter hardware error path.
1410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1411 "0457 Adapter Hardware Error "
1412 "Data: x%x x%x x%x\n",
1413 phba->work_hs,
1414 phba->work_status[0], phba->work_status[1]);
1416 event_data = FC_REG_DUMP_EVENT;
1417 shost = lpfc_shost_from_vport(vport);
1418 fc_host_post_vendor_event(shost, fc_get_event_number(),
1419 sizeof(event_data), (char *) &event_data,
1420 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1422 lpfc_offline_eratt(phba);
1424 return;
1428 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1429 * @phba: pointer to lpfc hba data structure.
1431 * This routine is invoked to handle the SLI4 HBA hardware error attention
1432 * conditions.
1434 static void
1435 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1437 struct lpfc_vport *vport = phba->pport;
1438 uint32_t event_data;
1439 struct Scsi_Host *shost;
1440 uint32_t if_type;
1441 struct lpfc_register portstat_reg;
1443 /* If the pci channel is offline, ignore possible errors, since
1444 * we cannot communicate with the pci card anyway.
1446 if (pci_channel_offline(phba->pcidev))
1447 return;
1448 /* If resets are disabled then leave the HBA alone and return */
1449 if (!phba->cfg_enable_hba_reset)
1450 return;
1452 /* Send an internal error event to mgmt application */
1453 lpfc_board_errevt_to_mgmt(phba);
1455 /* For now, the actual action for SLI4 device handling is not
1456 * specified yet, just treated it as adaptor hardware failure
1458 event_data = FC_REG_DUMP_EVENT;
1459 shost = lpfc_shost_from_vport(vport);
1460 fc_host_post_vendor_event(shost, fc_get_event_number(),
1461 sizeof(event_data), (char *) &event_data,
1462 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1464 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1465 switch (if_type) {
1466 case LPFC_SLI_INTF_IF_TYPE_0:
1467 lpfc_sli4_offline_eratt(phba);
1468 break;
1469 case LPFC_SLI_INTF_IF_TYPE_2:
1470 portstat_reg.word0 =
1471 readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
1473 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1474 /* TODO: Register for Overtemp async events. */
1475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1476 "2889 Port Overtemperature event, "
1477 "taking port\n");
1478 spin_lock_irq(&phba->hbalock);
1479 phba->over_temp_state = HBA_OVER_TEMP;
1480 spin_unlock_irq(&phba->hbalock);
1481 lpfc_sli4_offline_eratt(phba);
1482 return;
1484 if (bf_get(lpfc_sliport_status_rn, &portstat_reg)) {
1485 /* need reset: attempt for port recovery */
1486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1487 "2887 Port Error: Attempting "
1488 "Port Recovery\n");
1489 lpfc_offline_prep(phba);
1490 lpfc_offline(phba);
1491 lpfc_sli_brdrestart(phba);
1492 if (lpfc_online(phba) == 0) {
1493 lpfc_unblock_mgmt_io(phba);
1494 return;
1496 /* fall through for not able to recover */
1498 lpfc_sli4_offline_eratt(phba);
1499 break;
1500 case LPFC_SLI_INTF_IF_TYPE_1:
1501 default:
1502 break;
1507 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1508 * @phba: pointer to lpfc HBA data structure.
1510 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1511 * routine from the API jump table function pointer from the lpfc_hba struct.
1513 * Return codes
1514 * 0 - success.
1515 * Any other value - error.
1517 void
1518 lpfc_handle_eratt(struct lpfc_hba *phba)
1520 (*phba->lpfc_handle_eratt)(phba);
1524 * lpfc_handle_latt - The HBA link event handler
1525 * @phba: pointer to lpfc hba data structure.
1527 * This routine is invoked from the worker thread to handle a HBA host
1528 * attention link event.
1530 void
1531 lpfc_handle_latt(struct lpfc_hba *phba)
1533 struct lpfc_vport *vport = phba->pport;
1534 struct lpfc_sli *psli = &phba->sli;
1535 LPFC_MBOXQ_t *pmb;
1536 volatile uint32_t control;
1537 struct lpfc_dmabuf *mp;
1538 int rc = 0;
1540 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1541 if (!pmb) {
1542 rc = 1;
1543 goto lpfc_handle_latt_err_exit;
1546 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1547 if (!mp) {
1548 rc = 2;
1549 goto lpfc_handle_latt_free_pmb;
1552 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1553 if (!mp->virt) {
1554 rc = 3;
1555 goto lpfc_handle_latt_free_mp;
1558 /* Cleanup any outstanding ELS commands */
1559 lpfc_els_flush_all_cmd(phba);
1561 psli->slistat.link_event++;
1562 lpfc_read_topology(phba, pmb, mp);
1563 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
1564 pmb->vport = vport;
1565 /* Block ELS IOCBs until we have processed this mbox command */
1566 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1567 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1568 if (rc == MBX_NOT_FINISHED) {
1569 rc = 4;
1570 goto lpfc_handle_latt_free_mbuf;
1573 /* Clear Link Attention in HA REG */
1574 spin_lock_irq(&phba->hbalock);
1575 writel(HA_LATT, phba->HAregaddr);
1576 readl(phba->HAregaddr); /* flush */
1577 spin_unlock_irq(&phba->hbalock);
1579 return;
1581 lpfc_handle_latt_free_mbuf:
1582 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1583 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1584 lpfc_handle_latt_free_mp:
1585 kfree(mp);
1586 lpfc_handle_latt_free_pmb:
1587 mempool_free(pmb, phba->mbox_mem_pool);
1588 lpfc_handle_latt_err_exit:
1589 /* Enable Link attention interrupts */
1590 spin_lock_irq(&phba->hbalock);
1591 psli->sli_flag |= LPFC_PROCESS_LA;
1592 control = readl(phba->HCregaddr);
1593 control |= HC_LAINT_ENA;
1594 writel(control, phba->HCregaddr);
1595 readl(phba->HCregaddr); /* flush */
1597 /* Clear Link Attention in HA REG */
1598 writel(HA_LATT, phba->HAregaddr);
1599 readl(phba->HAregaddr); /* flush */
1600 spin_unlock_irq(&phba->hbalock);
1601 lpfc_linkdown(phba);
1602 phba->link_state = LPFC_HBA_ERROR;
1604 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1605 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1607 return;
1611 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1612 * @phba: pointer to lpfc hba data structure.
1613 * @vpd: pointer to the vital product data.
1614 * @len: length of the vital product data in bytes.
1616 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1617 * an array of characters. In this routine, the ModelName, ProgramType, and
1618 * ModelDesc, etc. fields of the phba data structure will be populated.
1620 * Return codes
1621 * 0 - pointer to the VPD passed in is NULL
1622 * 1 - success
1625 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1627 uint8_t lenlo, lenhi;
1628 int Length;
1629 int i, j;
1630 int finished = 0;
1631 int index = 0;
1633 if (!vpd)
1634 return 0;
1636 /* Vital Product */
1637 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1638 "0455 Vital Product Data: x%x x%x x%x x%x\n",
1639 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1640 (uint32_t) vpd[3]);
1641 while (!finished && (index < (len - 4))) {
1642 switch (vpd[index]) {
1643 case 0x82:
1644 case 0x91:
1645 index += 1;
1646 lenlo = vpd[index];
1647 index += 1;
1648 lenhi = vpd[index];
1649 index += 1;
1650 i = ((((unsigned short)lenhi) << 8) + lenlo);
1651 index += i;
1652 break;
1653 case 0x90:
1654 index += 1;
1655 lenlo = vpd[index];
1656 index += 1;
1657 lenhi = vpd[index];
1658 index += 1;
1659 Length = ((((unsigned short)lenhi) << 8) + lenlo);
1660 if (Length > len - index)
1661 Length = len - index;
1662 while (Length > 0) {
1663 /* Look for Serial Number */
1664 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1665 index += 2;
1666 i = vpd[index];
1667 index += 1;
1668 j = 0;
1669 Length -= (3+i);
1670 while(i--) {
1671 phba->SerialNumber[j++] = vpd[index++];
1672 if (j == 31)
1673 break;
1675 phba->SerialNumber[j] = 0;
1676 continue;
1678 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1679 phba->vpd_flag |= VPD_MODEL_DESC;
1680 index += 2;
1681 i = vpd[index];
1682 index += 1;
1683 j = 0;
1684 Length -= (3+i);
1685 while(i--) {
1686 phba->ModelDesc[j++] = vpd[index++];
1687 if (j == 255)
1688 break;
1690 phba->ModelDesc[j] = 0;
1691 continue;
1693 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1694 phba->vpd_flag |= VPD_MODEL_NAME;
1695 index += 2;
1696 i = vpd[index];
1697 index += 1;
1698 j = 0;
1699 Length -= (3+i);
1700 while(i--) {
1701 phba->ModelName[j++] = vpd[index++];
1702 if (j == 79)
1703 break;
1705 phba->ModelName[j] = 0;
1706 continue;
1708 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1709 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1710 index += 2;
1711 i = vpd[index];
1712 index += 1;
1713 j = 0;
1714 Length -= (3+i);
1715 while(i--) {
1716 phba->ProgramType[j++] = vpd[index++];
1717 if (j == 255)
1718 break;
1720 phba->ProgramType[j] = 0;
1721 continue;
1723 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1724 phba->vpd_flag |= VPD_PORT;
1725 index += 2;
1726 i = vpd[index];
1727 index += 1;
1728 j = 0;
1729 Length -= (3+i);
1730 while(i--) {
1731 phba->Port[j++] = vpd[index++];
1732 if (j == 19)
1733 break;
1735 phba->Port[j] = 0;
1736 continue;
1738 else {
1739 index += 2;
1740 i = vpd[index];
1741 index += 1;
1742 index += i;
1743 Length -= (3 + i);
1746 finished = 0;
1747 break;
1748 case 0x78:
1749 finished = 1;
1750 break;
1751 default:
1752 index ++;
1753 break;
1757 return(1);
1761 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1762 * @phba: pointer to lpfc hba data structure.
1763 * @mdp: pointer to the data structure to hold the derived model name.
1764 * @descp: pointer to the data structure to hold the derived description.
1766 * This routine retrieves HBA's description based on its registered PCI device
1767 * ID. The @descp passed into this function points to an array of 256 chars. It
1768 * shall be returned with the model name, maximum speed, and the host bus type.
1769 * The @mdp passed into this function points to an array of 80 chars. When the
1770 * function returns, the @mdp will be filled with the model name.
1772 static void
1773 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1775 lpfc_vpd_t *vp;
1776 uint16_t dev_id = phba->pcidev->device;
1777 int max_speed;
1778 int GE = 0;
1779 int oneConnect = 0; /* default is not a oneConnect */
1780 struct {
1781 char *name;
1782 char *bus;
1783 char *function;
1784 } m = {"<Unknown>", "", ""};
1786 if (mdp && mdp[0] != '\0'
1787 && descp && descp[0] != '\0')
1788 return;
1790 if (phba->lmt & LMT_16Gb)
1791 max_speed = 16;
1792 else if (phba->lmt & LMT_10Gb)
1793 max_speed = 10;
1794 else if (phba->lmt & LMT_8Gb)
1795 max_speed = 8;
1796 else if (phba->lmt & LMT_4Gb)
1797 max_speed = 4;
1798 else if (phba->lmt & LMT_2Gb)
1799 max_speed = 2;
1800 else
1801 max_speed = 1;
1803 vp = &phba->vpd;
1805 switch (dev_id) {
1806 case PCI_DEVICE_ID_FIREFLY:
1807 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1808 break;
1809 case PCI_DEVICE_ID_SUPERFLY:
1810 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1811 m = (typeof(m)){"LP7000", "PCI",
1812 "Fibre Channel Adapter"};
1813 else
1814 m = (typeof(m)){"LP7000E", "PCI",
1815 "Fibre Channel Adapter"};
1816 break;
1817 case PCI_DEVICE_ID_DRAGONFLY:
1818 m = (typeof(m)){"LP8000", "PCI",
1819 "Fibre Channel Adapter"};
1820 break;
1821 case PCI_DEVICE_ID_CENTAUR:
1822 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1823 m = (typeof(m)){"LP9002", "PCI",
1824 "Fibre Channel Adapter"};
1825 else
1826 m = (typeof(m)){"LP9000", "PCI",
1827 "Fibre Channel Adapter"};
1828 break;
1829 case PCI_DEVICE_ID_RFLY:
1830 m = (typeof(m)){"LP952", "PCI",
1831 "Fibre Channel Adapter"};
1832 break;
1833 case PCI_DEVICE_ID_PEGASUS:
1834 m = (typeof(m)){"LP9802", "PCI-X",
1835 "Fibre Channel Adapter"};
1836 break;
1837 case PCI_DEVICE_ID_THOR:
1838 m = (typeof(m)){"LP10000", "PCI-X",
1839 "Fibre Channel Adapter"};
1840 break;
1841 case PCI_DEVICE_ID_VIPER:
1842 m = (typeof(m)){"LPX1000", "PCI-X",
1843 "Fibre Channel Adapter"};
1844 break;
1845 case PCI_DEVICE_ID_PFLY:
1846 m = (typeof(m)){"LP982", "PCI-X",
1847 "Fibre Channel Adapter"};
1848 break;
1849 case PCI_DEVICE_ID_TFLY:
1850 m = (typeof(m)){"LP1050", "PCI-X",
1851 "Fibre Channel Adapter"};
1852 break;
1853 case PCI_DEVICE_ID_HELIOS:
1854 m = (typeof(m)){"LP11000", "PCI-X2",
1855 "Fibre Channel Adapter"};
1856 break;
1857 case PCI_DEVICE_ID_HELIOS_SCSP:
1858 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1859 "Fibre Channel Adapter"};
1860 break;
1861 case PCI_DEVICE_ID_HELIOS_DCSP:
1862 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1863 "Fibre Channel Adapter"};
1864 break;
1865 case PCI_DEVICE_ID_NEPTUNE:
1866 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1867 break;
1868 case PCI_DEVICE_ID_NEPTUNE_SCSP:
1869 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1870 break;
1871 case PCI_DEVICE_ID_NEPTUNE_DCSP:
1872 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1873 break;
1874 case PCI_DEVICE_ID_BMID:
1875 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1876 break;
1877 case PCI_DEVICE_ID_BSMB:
1878 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1879 break;
1880 case PCI_DEVICE_ID_ZEPHYR:
1881 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1882 break;
1883 case PCI_DEVICE_ID_ZEPHYR_SCSP:
1884 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1885 break;
1886 case PCI_DEVICE_ID_ZEPHYR_DCSP:
1887 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1888 GE = 1;
1889 break;
1890 case PCI_DEVICE_ID_ZMID:
1891 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1892 break;
1893 case PCI_DEVICE_ID_ZSMB:
1894 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1895 break;
1896 case PCI_DEVICE_ID_LP101:
1897 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1898 break;
1899 case PCI_DEVICE_ID_LP10000S:
1900 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1901 break;
1902 case PCI_DEVICE_ID_LP11000S:
1903 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
1904 break;
1905 case PCI_DEVICE_ID_LPE11000S:
1906 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
1907 break;
1908 case PCI_DEVICE_ID_SAT:
1909 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
1910 break;
1911 case PCI_DEVICE_ID_SAT_MID:
1912 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
1913 break;
1914 case PCI_DEVICE_ID_SAT_SMB:
1915 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
1916 break;
1917 case PCI_DEVICE_ID_SAT_DCSP:
1918 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
1919 break;
1920 case PCI_DEVICE_ID_SAT_SCSP:
1921 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
1922 break;
1923 case PCI_DEVICE_ID_SAT_S:
1924 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
1925 break;
1926 case PCI_DEVICE_ID_HORNET:
1927 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
1928 GE = 1;
1929 break;
1930 case PCI_DEVICE_ID_PROTEUS_VF:
1931 m = (typeof(m)){"LPev12000", "PCIe IOV",
1932 "Fibre Channel Adapter"};
1933 break;
1934 case PCI_DEVICE_ID_PROTEUS_PF:
1935 m = (typeof(m)){"LPev12000", "PCIe IOV",
1936 "Fibre Channel Adapter"};
1937 break;
1938 case PCI_DEVICE_ID_PROTEUS_S:
1939 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1940 "Fibre Channel Adapter"};
1941 break;
1942 case PCI_DEVICE_ID_TIGERSHARK:
1943 oneConnect = 1;
1944 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
1945 break;
1946 case PCI_DEVICE_ID_TOMCAT:
1947 oneConnect = 1;
1948 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1949 break;
1950 case PCI_DEVICE_ID_FALCON:
1951 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1952 "EmulexSecure Fibre"};
1953 break;
1954 case PCI_DEVICE_ID_BALIUS:
1955 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
1956 "Fibre Channel Adapter"};
1957 break;
1958 case PCI_DEVICE_ID_LANCER_FC:
1959 case PCI_DEVICE_ID_LANCER_FC_VF:
1960 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
1961 break;
1962 case PCI_DEVICE_ID_LANCER_FCOE:
1963 case PCI_DEVICE_ID_LANCER_FCOE_VF:
1964 oneConnect = 1;
1965 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
1966 break;
1967 default:
1968 m = (typeof(m)){"Unknown", "", ""};
1969 break;
1972 if (mdp && mdp[0] == '\0')
1973 snprintf(mdp, 79,"%s", m.name);
1975 * oneConnect hba requires special processing, they are all initiators
1976 * and we put the port number on the end
1978 if (descp && descp[0] == '\0') {
1979 if (oneConnect)
1980 snprintf(descp, 255,
1981 "Emulex OneConnect %s, %s Initiator, Port %s",
1982 m.name, m.function,
1983 phba->Port);
1984 else
1985 snprintf(descp, 255,
1986 "Emulex %s %d%s %s %s",
1987 m.name, max_speed, (GE) ? "GE" : "Gb",
1988 m.bus, m.function);
1993 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
1994 * @phba: pointer to lpfc hba data structure.
1995 * @pring: pointer to a IOCB ring.
1996 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1998 * This routine posts a given number of IOCBs with the associated DMA buffer
1999 * descriptors specified by the cnt argument to the given IOCB ring.
2001 * Return codes
2002 * The number of IOCBs NOT able to be posted to the IOCB ring.
2005 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2007 IOCB_t *icmd;
2008 struct lpfc_iocbq *iocb;
2009 struct lpfc_dmabuf *mp1, *mp2;
2011 cnt += pring->missbufcnt;
2013 /* While there are buffers to post */
2014 while (cnt > 0) {
2015 /* Allocate buffer for command iocb */
2016 iocb = lpfc_sli_get_iocbq(phba);
2017 if (iocb == NULL) {
2018 pring->missbufcnt = cnt;
2019 return cnt;
2021 icmd = &iocb->iocb;
2023 /* 2 buffers can be posted per command */
2024 /* Allocate buffer to post */
2025 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2026 if (mp1)
2027 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2028 if (!mp1 || !mp1->virt) {
2029 kfree(mp1);
2030 lpfc_sli_release_iocbq(phba, iocb);
2031 pring->missbufcnt = cnt;
2032 return cnt;
2035 INIT_LIST_HEAD(&mp1->list);
2036 /* Allocate buffer to post */
2037 if (cnt > 1) {
2038 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2039 if (mp2)
2040 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2041 &mp2->phys);
2042 if (!mp2 || !mp2->virt) {
2043 kfree(mp2);
2044 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2045 kfree(mp1);
2046 lpfc_sli_release_iocbq(phba, iocb);
2047 pring->missbufcnt = cnt;
2048 return cnt;
2051 INIT_LIST_HEAD(&mp2->list);
2052 } else {
2053 mp2 = NULL;
2056 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2057 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2058 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2059 icmd->ulpBdeCount = 1;
2060 cnt--;
2061 if (mp2) {
2062 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2063 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2064 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2065 cnt--;
2066 icmd->ulpBdeCount = 2;
2069 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2070 icmd->ulpLe = 1;
2072 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2073 IOCB_ERROR) {
2074 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2075 kfree(mp1);
2076 cnt++;
2077 if (mp2) {
2078 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2079 kfree(mp2);
2080 cnt++;
2082 lpfc_sli_release_iocbq(phba, iocb);
2083 pring->missbufcnt = cnt;
2084 return cnt;
2086 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2087 if (mp2)
2088 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2090 pring->missbufcnt = 0;
2091 return 0;
2095 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2096 * @phba: pointer to lpfc hba data structure.
2098 * This routine posts initial receive IOCB buffers to the ELS ring. The
2099 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2100 * set to 64 IOCBs.
2102 * Return codes
2103 * 0 - success (currently always success)
2105 static int
2106 lpfc_post_rcv_buf(struct lpfc_hba *phba)
2108 struct lpfc_sli *psli = &phba->sli;
2110 /* Ring 0, ELS / CT buffers */
2111 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2112 /* Ring 2 - FCP no buffers needed */
2114 return 0;
2117 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2120 * lpfc_sha_init - Set up initial array of hash table entries
2121 * @HashResultPointer: pointer to an array as hash table.
2123 * This routine sets up the initial values to the array of hash table entries
2124 * for the LC HBAs.
2126 static void
2127 lpfc_sha_init(uint32_t * HashResultPointer)
2129 HashResultPointer[0] = 0x67452301;
2130 HashResultPointer[1] = 0xEFCDAB89;
2131 HashResultPointer[2] = 0x98BADCFE;
2132 HashResultPointer[3] = 0x10325476;
2133 HashResultPointer[4] = 0xC3D2E1F0;
2137 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2138 * @HashResultPointer: pointer to an initial/result hash table.
2139 * @HashWorkingPointer: pointer to an working hash table.
2141 * This routine iterates an initial hash table pointed by @HashResultPointer
2142 * with the values from the working hash table pointeed by @HashWorkingPointer.
2143 * The results are putting back to the initial hash table, returned through
2144 * the @HashResultPointer as the result hash table.
2146 static void
2147 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2149 int t;
2150 uint32_t TEMP;
2151 uint32_t A, B, C, D, E;
2152 t = 16;
2153 do {
2154 HashWorkingPointer[t] =
2155 S(1,
2156 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2157 8] ^
2158 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2159 } while (++t <= 79);
2160 t = 0;
2161 A = HashResultPointer[0];
2162 B = HashResultPointer[1];
2163 C = HashResultPointer[2];
2164 D = HashResultPointer[3];
2165 E = HashResultPointer[4];
2167 do {
2168 if (t < 20) {
2169 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2170 } else if (t < 40) {
2171 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2172 } else if (t < 60) {
2173 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2174 } else {
2175 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2177 TEMP += S(5, A) + E + HashWorkingPointer[t];
2178 E = D;
2179 D = C;
2180 C = S(30, B);
2181 B = A;
2182 A = TEMP;
2183 } while (++t <= 79);
2185 HashResultPointer[0] += A;
2186 HashResultPointer[1] += B;
2187 HashResultPointer[2] += C;
2188 HashResultPointer[3] += D;
2189 HashResultPointer[4] += E;
2194 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2195 * @RandomChallenge: pointer to the entry of host challenge random number array.
2196 * @HashWorking: pointer to the entry of the working hash array.
2198 * This routine calculates the working hash array referred by @HashWorking
2199 * from the challenge random numbers associated with the host, referred by
2200 * @RandomChallenge. The result is put into the entry of the working hash
2201 * array and returned by reference through @HashWorking.
2203 static void
2204 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2206 *HashWorking = (*RandomChallenge ^ *HashWorking);
2210 * lpfc_hba_init - Perform special handling for LC HBA initialization
2211 * @phba: pointer to lpfc hba data structure.
2212 * @hbainit: pointer to an array of unsigned 32-bit integers.
2214 * This routine performs the special handling for LC HBA initialization.
2216 void
2217 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2219 int t;
2220 uint32_t *HashWorking;
2221 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2223 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2224 if (!HashWorking)
2225 return;
2227 HashWorking[0] = HashWorking[78] = *pwwnn++;
2228 HashWorking[1] = HashWorking[79] = *pwwnn;
2230 for (t = 0; t < 7; t++)
2231 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2233 lpfc_sha_init(hbainit);
2234 lpfc_sha_iterate(hbainit, HashWorking);
2235 kfree(HashWorking);
2239 * lpfc_cleanup - Performs vport cleanups before deleting a vport
2240 * @vport: pointer to a virtual N_Port data structure.
2242 * This routine performs the necessary cleanups before deleting the @vport.
2243 * It invokes the discovery state machine to perform necessary state
2244 * transitions and to release the ndlps associated with the @vport. Note,
2245 * the physical port is treated as @vport 0.
2247 void
2248 lpfc_cleanup(struct lpfc_vport *vport)
2250 struct lpfc_hba *phba = vport->phba;
2251 struct lpfc_nodelist *ndlp, *next_ndlp;
2252 int i = 0;
2254 if (phba->link_state > LPFC_LINK_DOWN)
2255 lpfc_port_link_failure(vport);
2257 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2258 if (!NLP_CHK_NODE_ACT(ndlp)) {
2259 ndlp = lpfc_enable_node(vport, ndlp,
2260 NLP_STE_UNUSED_NODE);
2261 if (!ndlp)
2262 continue;
2263 spin_lock_irq(&phba->ndlp_lock);
2264 NLP_SET_FREE_REQ(ndlp);
2265 spin_unlock_irq(&phba->ndlp_lock);
2266 /* Trigger the release of the ndlp memory */
2267 lpfc_nlp_put(ndlp);
2268 continue;
2270 spin_lock_irq(&phba->ndlp_lock);
2271 if (NLP_CHK_FREE_REQ(ndlp)) {
2272 /* The ndlp should not be in memory free mode already */
2273 spin_unlock_irq(&phba->ndlp_lock);
2274 continue;
2275 } else
2276 /* Indicate request for freeing ndlp memory */
2277 NLP_SET_FREE_REQ(ndlp);
2278 spin_unlock_irq(&phba->ndlp_lock);
2280 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2281 ndlp->nlp_DID == Fabric_DID) {
2282 /* Just free up ndlp with Fabric_DID for vports */
2283 lpfc_nlp_put(ndlp);
2284 continue;
2287 if (ndlp->nlp_type & NLP_FABRIC)
2288 lpfc_disc_state_machine(vport, ndlp, NULL,
2289 NLP_EVT_DEVICE_RECOVERY);
2291 lpfc_disc_state_machine(vport, ndlp, NULL,
2292 NLP_EVT_DEVICE_RM);
2296 /* At this point, ALL ndlp's should be gone
2297 * because of the previous NLP_EVT_DEVICE_RM.
2298 * Lets wait for this to happen, if needed.
2300 while (!list_empty(&vport->fc_nodes)) {
2301 if (i++ > 3000) {
2302 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2303 "0233 Nodelist not empty\n");
2304 list_for_each_entry_safe(ndlp, next_ndlp,
2305 &vport->fc_nodes, nlp_listp) {
2306 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2307 LOG_NODE,
2308 "0282 did:x%x ndlp:x%p "
2309 "usgmap:x%x refcnt:%d\n",
2310 ndlp->nlp_DID, (void *)ndlp,
2311 ndlp->nlp_usg_map,
2312 atomic_read(
2313 &ndlp->kref.refcount));
2315 break;
2318 /* Wait for any activity on ndlps to settle */
2319 msleep(10);
2321 lpfc_cleanup_vports_rrqs(vport, NULL);
2325 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2326 * @vport: pointer to a virtual N_Port data structure.
2328 * This routine stops all the timers associated with a @vport. This function
2329 * is invoked before disabling or deleting a @vport. Note that the physical
2330 * port is treated as @vport 0.
2332 void
2333 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2335 del_timer_sync(&vport->els_tmofunc);
2336 del_timer_sync(&vport->fc_fdmitmo);
2337 del_timer_sync(&vport->delayed_disc_tmo);
2338 lpfc_can_disctmo(vport);
2339 return;
2343 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2344 * @phba: pointer to lpfc hba data structure.
2346 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2347 * caller of this routine should already hold the host lock.
2349 void
2350 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2352 /* Clear pending FCF rediscovery wait flag */
2353 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2355 /* Now, try to stop the timer */
2356 del_timer(&phba->fcf.redisc_wait);
2360 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2361 * @phba: pointer to lpfc hba data structure.
2363 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2364 * checks whether the FCF rediscovery wait timer is pending with the host
2365 * lock held before proceeding with disabling the timer and clearing the
2366 * wait timer pendig flag.
2368 void
2369 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2371 spin_lock_irq(&phba->hbalock);
2372 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2373 /* FCF rediscovery timer already fired or stopped */
2374 spin_unlock_irq(&phba->hbalock);
2375 return;
2377 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2378 /* Clear failover in progress flags */
2379 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2380 spin_unlock_irq(&phba->hbalock);
2384 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2385 * @phba: pointer to lpfc hba data structure.
2387 * This routine stops all the timers associated with a HBA. This function is
2388 * invoked before either putting a HBA offline or unloading the driver.
2390 void
2391 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2393 lpfc_stop_vport_timers(phba->pport);
2394 del_timer_sync(&phba->sli.mbox_tmo);
2395 del_timer_sync(&phba->fabric_block_timer);
2396 del_timer_sync(&phba->eratt_poll);
2397 del_timer_sync(&phba->hb_tmofunc);
2398 if (phba->sli_rev == LPFC_SLI_REV4) {
2399 del_timer_sync(&phba->rrq_tmr);
2400 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2402 phba->hb_outstanding = 0;
2404 switch (phba->pci_dev_grp) {
2405 case LPFC_PCI_DEV_LP:
2406 /* Stop any LightPulse device specific driver timers */
2407 del_timer_sync(&phba->fcp_poll_timer);
2408 break;
2409 case LPFC_PCI_DEV_OC:
2410 /* Stop any OneConnect device sepcific driver timers */
2411 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2412 break;
2413 default:
2414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2415 "0297 Invalid device group (x%x)\n",
2416 phba->pci_dev_grp);
2417 break;
2419 return;
2423 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2424 * @phba: pointer to lpfc hba data structure.
2426 * This routine marks a HBA's management interface as blocked. Once the HBA's
2427 * management interface is marked as blocked, all the user space access to
2428 * the HBA, whether they are from sysfs interface or libdfc interface will
2429 * all be blocked. The HBA is set to block the management interface when the
2430 * driver prepares the HBA interface for online or offline.
2432 static void
2433 lpfc_block_mgmt_io(struct lpfc_hba * phba)
2435 unsigned long iflag;
2436 uint8_t actcmd = MBX_HEARTBEAT;
2437 unsigned long timeout;
2440 spin_lock_irqsave(&phba->hbalock, iflag);
2441 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2442 if (phba->sli.mbox_active)
2443 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2444 spin_unlock_irqrestore(&phba->hbalock, iflag);
2445 /* Determine how long we might wait for the active mailbox
2446 * command to be gracefully completed by firmware.
2448 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
2449 jiffies;
2450 /* Wait for the outstnading mailbox command to complete */
2451 while (phba->sli.mbox_active) {
2452 /* Check active mailbox complete status every 2ms */
2453 msleep(2);
2454 if (time_after(jiffies, timeout)) {
2455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2456 "2813 Mgmt IO is Blocked %x "
2457 "- mbox cmd %x still active\n",
2458 phba->sli.sli_flag, actcmd);
2459 break;
2465 * lpfc_online - Initialize and bring a HBA online
2466 * @phba: pointer to lpfc hba data structure.
2468 * This routine initializes the HBA and brings a HBA online. During this
2469 * process, the management interface is blocked to prevent user space access
2470 * to the HBA interfering with the driver initialization.
2472 * Return codes
2473 * 0 - successful
2474 * 1 - failed
2477 lpfc_online(struct lpfc_hba *phba)
2479 struct lpfc_vport *vport;
2480 struct lpfc_vport **vports;
2481 int i;
2483 if (!phba)
2484 return 0;
2485 vport = phba->pport;
2487 if (!(vport->fc_flag & FC_OFFLINE_MODE))
2488 return 0;
2490 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2491 "0458 Bring Adapter online\n");
2493 lpfc_block_mgmt_io(phba);
2495 if (!lpfc_sli_queue_setup(phba)) {
2496 lpfc_unblock_mgmt_io(phba);
2497 return 1;
2500 if (phba->sli_rev == LPFC_SLI_REV4) {
2501 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2502 lpfc_unblock_mgmt_io(phba);
2503 return 1;
2505 } else {
2506 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2507 lpfc_unblock_mgmt_io(phba);
2508 return 1;
2512 vports = lpfc_create_vport_work_array(phba);
2513 if (vports != NULL)
2514 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2515 struct Scsi_Host *shost;
2516 shost = lpfc_shost_from_vport(vports[i]);
2517 spin_lock_irq(shost->host_lock);
2518 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2519 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2520 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2521 if (phba->sli_rev == LPFC_SLI_REV4)
2522 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2523 spin_unlock_irq(shost->host_lock);
2525 lpfc_destroy_vport_work_array(phba, vports);
2527 lpfc_unblock_mgmt_io(phba);
2528 return 0;
2532 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2533 * @phba: pointer to lpfc hba data structure.
2535 * This routine marks a HBA's management interface as not blocked. Once the
2536 * HBA's management interface is marked as not blocked, all the user space
2537 * access to the HBA, whether they are from sysfs interface or libdfc
2538 * interface will be allowed. The HBA is set to block the management interface
2539 * when the driver prepares the HBA interface for online or offline and then
2540 * set to unblock the management interface afterwards.
2542 void
2543 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2545 unsigned long iflag;
2547 spin_lock_irqsave(&phba->hbalock, iflag);
2548 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2549 spin_unlock_irqrestore(&phba->hbalock, iflag);
2553 * lpfc_offline_prep - Prepare a HBA to be brought offline
2554 * @phba: pointer to lpfc hba data structure.
2556 * This routine is invoked to prepare a HBA to be brought offline. It performs
2557 * unregistration login to all the nodes on all vports and flushes the mailbox
2558 * queue to make it ready to be brought offline.
2560 void
2561 lpfc_offline_prep(struct lpfc_hba * phba)
2563 struct lpfc_vport *vport = phba->pport;
2564 struct lpfc_nodelist *ndlp, *next_ndlp;
2565 struct lpfc_vport **vports;
2566 struct Scsi_Host *shost;
2567 int i;
2569 if (vport->fc_flag & FC_OFFLINE_MODE)
2570 return;
2572 lpfc_block_mgmt_io(phba);
2574 lpfc_linkdown(phba);
2576 /* Issue an unreg_login to all nodes on all vports */
2577 vports = lpfc_create_vport_work_array(phba);
2578 if (vports != NULL) {
2579 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2580 if (vports[i]->load_flag & FC_UNLOADING)
2581 continue;
2582 shost = lpfc_shost_from_vport(vports[i]);
2583 spin_lock_irq(shost->host_lock);
2584 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2585 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2586 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2587 spin_unlock_irq(shost->host_lock);
2589 shost = lpfc_shost_from_vport(vports[i]);
2590 list_for_each_entry_safe(ndlp, next_ndlp,
2591 &vports[i]->fc_nodes,
2592 nlp_listp) {
2593 if (!NLP_CHK_NODE_ACT(ndlp))
2594 continue;
2595 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2596 continue;
2597 if (ndlp->nlp_type & NLP_FABRIC) {
2598 lpfc_disc_state_machine(vports[i], ndlp,
2599 NULL, NLP_EVT_DEVICE_RECOVERY);
2600 lpfc_disc_state_machine(vports[i], ndlp,
2601 NULL, NLP_EVT_DEVICE_RM);
2603 spin_lock_irq(shost->host_lock);
2604 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2605 spin_unlock_irq(shost->host_lock);
2606 lpfc_unreg_rpi(vports[i], ndlp);
2610 lpfc_destroy_vport_work_array(phba, vports);
2612 lpfc_sli_mbox_sys_shutdown(phba);
2616 * lpfc_offline - Bring a HBA offline
2617 * @phba: pointer to lpfc hba data structure.
2619 * This routine actually brings a HBA offline. It stops all the timers
2620 * associated with the HBA, brings down the SLI layer, and eventually
2621 * marks the HBA as in offline state for the upper layer protocol.
2623 void
2624 lpfc_offline(struct lpfc_hba *phba)
2626 struct Scsi_Host *shost;
2627 struct lpfc_vport **vports;
2628 int i;
2630 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2631 return;
2633 /* stop port and all timers associated with this hba */
2634 lpfc_stop_port(phba);
2635 vports = lpfc_create_vport_work_array(phba);
2636 if (vports != NULL)
2637 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2638 lpfc_stop_vport_timers(vports[i]);
2639 lpfc_destroy_vport_work_array(phba, vports);
2640 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2641 "0460 Bring Adapter offline\n");
2642 /* Bring down the SLI Layer and cleanup. The HBA is offline
2643 now. */
2644 lpfc_sli_hba_down(phba);
2645 spin_lock_irq(&phba->hbalock);
2646 phba->work_ha = 0;
2647 spin_unlock_irq(&phba->hbalock);
2648 vports = lpfc_create_vport_work_array(phba);
2649 if (vports != NULL)
2650 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2651 shost = lpfc_shost_from_vport(vports[i]);
2652 spin_lock_irq(shost->host_lock);
2653 vports[i]->work_port_events = 0;
2654 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2655 spin_unlock_irq(shost->host_lock);
2657 lpfc_destroy_vport_work_array(phba, vports);
2661 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2662 * @phba: pointer to lpfc hba data structure.
2664 * This routine is to free all the SCSI buffers and IOCBs from the driver
2665 * list back to kernel. It is called from lpfc_pci_remove_one to free
2666 * the internal resources before the device is removed from the system.
2668 * Return codes
2669 * 0 - successful (for now, it always returns 0)
2671 static int
2672 lpfc_scsi_free(struct lpfc_hba *phba)
2674 struct lpfc_scsi_buf *sb, *sb_next;
2675 struct lpfc_iocbq *io, *io_next;
2677 spin_lock_irq(&phba->hbalock);
2678 /* Release all the lpfc_scsi_bufs maintained by this host. */
2679 spin_lock(&phba->scsi_buf_list_lock);
2680 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2681 list_del(&sb->list);
2682 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2683 sb->dma_handle);
2684 kfree(sb);
2685 phba->total_scsi_bufs--;
2687 spin_unlock(&phba->scsi_buf_list_lock);
2689 /* Release all the lpfc_iocbq entries maintained by this host. */
2690 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2691 list_del(&io->list);
2692 kfree(io);
2693 phba->total_iocbq_bufs--;
2696 spin_unlock_irq(&phba->hbalock);
2697 return 0;
2701 * lpfc_create_port - Create an FC port
2702 * @phba: pointer to lpfc hba data structure.
2703 * @instance: a unique integer ID to this FC port.
2704 * @dev: pointer to the device data structure.
2706 * This routine creates a FC port for the upper layer protocol. The FC port
2707 * can be created on top of either a physical port or a virtual port provided
2708 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2709 * and associates the FC port created before adding the shost into the SCSI
2710 * layer.
2712 * Return codes
2713 * @vport - pointer to the virtual N_Port data structure.
2714 * NULL - port create failed.
2716 struct lpfc_vport *
2717 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
2719 struct lpfc_vport *vport;
2720 struct Scsi_Host *shost;
2721 int error = 0;
2723 if (dev != &phba->pcidev->dev)
2724 shost = scsi_host_alloc(&lpfc_vport_template,
2725 sizeof(struct lpfc_vport));
2726 else
2727 shost = scsi_host_alloc(&lpfc_template,
2728 sizeof(struct lpfc_vport));
2729 if (!shost)
2730 goto out;
2732 vport = (struct lpfc_vport *) shost->hostdata;
2733 vport->phba = phba;
2734 vport->load_flag |= FC_LOADING;
2735 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2736 vport->fc_rscn_flush = 0;
2738 lpfc_get_vport_cfgparam(vport);
2739 shost->unique_id = instance;
2740 shost->max_id = LPFC_MAX_TARGET;
2741 shost->max_lun = vport->cfg_max_luns;
2742 shost->this_id = -1;
2743 shost->max_cmd_len = 16;
2744 if (phba->sli_rev == LPFC_SLI_REV4) {
2745 shost->dma_boundary =
2746 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
2747 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2751 * Set initial can_queue value since 0 is no longer supported and
2752 * scsi_add_host will fail. This will be adjusted later based on the
2753 * max xri value determined in hba setup.
2755 shost->can_queue = phba->cfg_hba_queue_depth - 10;
2756 if (dev != &phba->pcidev->dev) {
2757 shost->transportt = lpfc_vport_transport_template;
2758 vport->port_type = LPFC_NPIV_PORT;
2759 } else {
2760 shost->transportt = lpfc_transport_template;
2761 vport->port_type = LPFC_PHYSICAL_PORT;
2764 /* Initialize all internally managed lists. */
2765 INIT_LIST_HEAD(&vport->fc_nodes);
2766 INIT_LIST_HEAD(&vport->rcv_buffer_list);
2767 spin_lock_init(&vport->work_port_lock);
2769 init_timer(&vport->fc_disctmo);
2770 vport->fc_disctmo.function = lpfc_disc_timeout;
2771 vport->fc_disctmo.data = (unsigned long)vport;
2773 init_timer(&vport->fc_fdmitmo);
2774 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
2775 vport->fc_fdmitmo.data = (unsigned long)vport;
2777 init_timer(&vport->els_tmofunc);
2778 vport->els_tmofunc.function = lpfc_els_timeout;
2779 vport->els_tmofunc.data = (unsigned long)vport;
2781 init_timer(&vport->delayed_disc_tmo);
2782 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
2783 vport->delayed_disc_tmo.data = (unsigned long)vport;
2785 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2786 if (error)
2787 goto out_put_shost;
2789 spin_lock_irq(&phba->hbalock);
2790 list_add_tail(&vport->listentry, &phba->port_list);
2791 spin_unlock_irq(&phba->hbalock);
2792 return vport;
2794 out_put_shost:
2795 scsi_host_put(shost);
2796 out:
2797 return NULL;
2801 * destroy_port - destroy an FC port
2802 * @vport: pointer to an lpfc virtual N_Port data structure.
2804 * This routine destroys a FC port from the upper layer protocol. All the
2805 * resources associated with the port are released.
2807 void
2808 destroy_port(struct lpfc_vport *vport)
2810 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2811 struct lpfc_hba *phba = vport->phba;
2813 lpfc_debugfs_terminate(vport);
2814 fc_remove_host(shost);
2815 scsi_remove_host(shost);
2817 spin_lock_irq(&phba->hbalock);
2818 list_del_init(&vport->listentry);
2819 spin_unlock_irq(&phba->hbalock);
2821 lpfc_cleanup(vport);
2822 return;
2826 * lpfc_get_instance - Get a unique integer ID
2828 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2829 * uses the kernel idr facility to perform the task.
2831 * Return codes:
2832 * instance - a unique integer ID allocated as the new instance.
2833 * -1 - lpfc get instance failed.
2836 lpfc_get_instance(void)
2838 int instance = 0;
2840 /* Assign an unused number */
2841 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2842 return -1;
2843 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2844 return -1;
2845 return instance;
2849 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
2850 * @shost: pointer to SCSI host data structure.
2851 * @time: elapsed time of the scan in jiffies.
2853 * This routine is called by the SCSI layer with a SCSI host to determine
2854 * whether the scan host is finished.
2856 * Note: there is no scan_start function as adapter initialization will have
2857 * asynchronously kicked off the link initialization.
2859 * Return codes
2860 * 0 - SCSI host scan is not over yet.
2861 * 1 - SCSI host scan is over.
2863 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2865 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2866 struct lpfc_hba *phba = vport->phba;
2867 int stat = 0;
2869 spin_lock_irq(shost->host_lock);
2871 if (vport->load_flag & FC_UNLOADING) {
2872 stat = 1;
2873 goto finished;
2875 if (time >= 30 * HZ) {
2876 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2877 "0461 Scanning longer than 30 "
2878 "seconds. Continuing initialization\n");
2879 stat = 1;
2880 goto finished;
2882 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2883 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2884 "0465 Link down longer than 15 "
2885 "seconds. Continuing initialization\n");
2886 stat = 1;
2887 goto finished;
2890 if (vport->port_state != LPFC_VPORT_READY)
2891 goto finished;
2892 if (vport->num_disc_nodes || vport->fc_prli_sent)
2893 goto finished;
2894 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2895 goto finished;
2896 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2897 goto finished;
2899 stat = 1;
2901 finished:
2902 spin_unlock_irq(shost->host_lock);
2903 return stat;
2907 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
2908 * @shost: pointer to SCSI host data structure.
2910 * This routine initializes a given SCSI host attributes on a FC port. The
2911 * SCSI host can be either on top of a physical port or a virtual port.
2913 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2915 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2916 struct lpfc_hba *phba = vport->phba;
2918 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
2921 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2922 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2923 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2925 memset(fc_host_supported_fc4s(shost), 0,
2926 sizeof(fc_host_supported_fc4s(shost)));
2927 fc_host_supported_fc4s(shost)[2] = 1;
2928 fc_host_supported_fc4s(shost)[7] = 1;
2930 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2931 sizeof fc_host_symbolic_name(shost));
2933 fc_host_supported_speeds(shost) = 0;
2934 if (phba->lmt & LMT_16Gb)
2935 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
2936 if (phba->lmt & LMT_10Gb)
2937 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2938 if (phba->lmt & LMT_8Gb)
2939 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2940 if (phba->lmt & LMT_4Gb)
2941 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2942 if (phba->lmt & LMT_2Gb)
2943 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2944 if (phba->lmt & LMT_1Gb)
2945 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2947 fc_host_maxframe_size(shost) =
2948 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2949 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2951 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
2953 /* This value is also unchanging */
2954 memset(fc_host_active_fc4s(shost), 0,
2955 sizeof(fc_host_active_fc4s(shost)));
2956 fc_host_active_fc4s(shost)[2] = 1;
2957 fc_host_active_fc4s(shost)[7] = 1;
2959 fc_host_max_npiv_vports(shost) = phba->max_vpi;
2960 spin_lock_irq(shost->host_lock);
2961 vport->load_flag &= ~FC_LOADING;
2962 spin_unlock_irq(shost->host_lock);
2966 * lpfc_stop_port_s3 - Stop SLI3 device port
2967 * @phba: pointer to lpfc hba data structure.
2969 * This routine is invoked to stop an SLI3 device port, it stops the device
2970 * from generating interrupts and stops the device driver's timers for the
2971 * device.
2973 static void
2974 lpfc_stop_port_s3(struct lpfc_hba *phba)
2976 /* Clear all interrupt enable conditions */
2977 writel(0, phba->HCregaddr);
2978 readl(phba->HCregaddr); /* flush */
2979 /* Clear all pending interrupts */
2980 writel(0xffffffff, phba->HAregaddr);
2981 readl(phba->HAregaddr); /* flush */
2983 /* Reset some HBA SLI setup states */
2984 lpfc_stop_hba_timers(phba);
2985 phba->pport->work_port_events = 0;
2989 * lpfc_stop_port_s4 - Stop SLI4 device port
2990 * @phba: pointer to lpfc hba data structure.
2992 * This routine is invoked to stop an SLI4 device port, it stops the device
2993 * from generating interrupts and stops the device driver's timers for the
2994 * device.
2996 static void
2997 lpfc_stop_port_s4(struct lpfc_hba *phba)
2999 /* Reset some HBA SLI4 setup states */
3000 lpfc_stop_hba_timers(phba);
3001 phba->pport->work_port_events = 0;
3002 phba->sli4_hba.intr_enable = 0;
3006 * lpfc_stop_port - Wrapper function for stopping hba port
3007 * @phba: Pointer to HBA context object.
3009 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3010 * the API jump table function pointer from the lpfc_hba struct.
3012 void
3013 lpfc_stop_port(struct lpfc_hba *phba)
3015 phba->lpfc_stop_port(phba);
3019 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3020 * @phba: Pointer to hba for which this call is being executed.
3022 * This routine starts the timer waiting for the FCF rediscovery to complete.
3024 void
3025 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3027 unsigned long fcf_redisc_wait_tmo =
3028 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3029 /* Start fcf rediscovery wait period timer */
3030 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3031 spin_lock_irq(&phba->hbalock);
3032 /* Allow action to new fcf asynchronous event */
3033 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3034 /* Mark the FCF rediscovery pending state */
3035 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3036 spin_unlock_irq(&phba->hbalock);
3040 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3041 * @ptr: Map to lpfc_hba data structure pointer.
3043 * This routine is invoked when waiting for FCF table rediscover has been
3044 * timed out. If new FCF record(s) has (have) been discovered during the
3045 * wait period, a new FCF event shall be added to the FCOE async event
3046 * list, and then worker thread shall be waked up for processing from the
3047 * worker thread context.
3049 void
3050 lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3052 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3054 /* Don't send FCF rediscovery event if timer cancelled */
3055 spin_lock_irq(&phba->hbalock);
3056 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3057 spin_unlock_irq(&phba->hbalock);
3058 return;
3060 /* Clear FCF rediscovery timer pending flag */
3061 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3062 /* FCF rediscovery event to worker thread */
3063 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3064 spin_unlock_irq(&phba->hbalock);
3065 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3066 "2776 FCF rediscover quiescent timer expired\n");
3067 /* wake up worker thread */
3068 lpfc_worker_wake_up(phba);
3072 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3073 * @phba: pointer to lpfc hba data structure.
3074 * @acqe_link: pointer to the async link completion queue entry.
3076 * This routine is to parse the SLI4 link-attention link fault code and
3077 * translate it into the base driver's read link attention mailbox command
3078 * status.
3080 * Return: Link-attention status in terms of base driver's coding.
3082 static uint16_t
3083 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3084 struct lpfc_acqe_link *acqe_link)
3086 uint16_t latt_fault;
3088 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3089 case LPFC_ASYNC_LINK_FAULT_NONE:
3090 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3091 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3092 latt_fault = 0;
3093 break;
3094 default:
3095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3096 "0398 Invalid link fault code: x%x\n",
3097 bf_get(lpfc_acqe_link_fault, acqe_link));
3098 latt_fault = MBXERR_ERROR;
3099 break;
3101 return latt_fault;
3105 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3106 * @phba: pointer to lpfc hba data structure.
3107 * @acqe_link: pointer to the async link completion queue entry.
3109 * This routine is to parse the SLI4 link attention type and translate it
3110 * into the base driver's link attention type coding.
3112 * Return: Link attention type in terms of base driver's coding.
3114 static uint8_t
3115 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3116 struct lpfc_acqe_link *acqe_link)
3118 uint8_t att_type;
3120 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3121 case LPFC_ASYNC_LINK_STATUS_DOWN:
3122 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3123 att_type = LPFC_ATT_LINK_DOWN;
3124 break;
3125 case LPFC_ASYNC_LINK_STATUS_UP:
3126 /* Ignore physical link up events - wait for logical link up */
3127 att_type = LPFC_ATT_RESERVED;
3128 break;
3129 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3130 att_type = LPFC_ATT_LINK_UP;
3131 break;
3132 default:
3133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3134 "0399 Invalid link attention type: x%x\n",
3135 bf_get(lpfc_acqe_link_status, acqe_link));
3136 att_type = LPFC_ATT_RESERVED;
3137 break;
3139 return att_type;
3143 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3144 * @phba: pointer to lpfc hba data structure.
3145 * @acqe_link: pointer to the async link completion queue entry.
3147 * This routine is to parse the SLI4 link-attention link speed and translate
3148 * it into the base driver's link-attention link speed coding.
3150 * Return: Link-attention link speed in terms of base driver's coding.
3152 static uint8_t
3153 lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3154 struct lpfc_acqe_link *acqe_link)
3156 uint8_t link_speed;
3158 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3159 case LPFC_ASYNC_LINK_SPEED_ZERO:
3160 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3161 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3162 link_speed = LPFC_LINK_SPEED_UNKNOWN;
3163 break;
3164 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3165 link_speed = LPFC_LINK_SPEED_1GHZ;
3166 break;
3167 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3168 link_speed = LPFC_LINK_SPEED_10GHZ;
3169 break;
3170 default:
3171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3172 "0483 Invalid link-attention link speed: x%x\n",
3173 bf_get(lpfc_acqe_link_speed, acqe_link));
3174 link_speed = LPFC_LINK_SPEED_UNKNOWN;
3175 break;
3177 return link_speed;
3181 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
3182 * @phba: pointer to lpfc hba data structure.
3183 * @acqe_link: pointer to the async link completion queue entry.
3185 * This routine is to handle the SLI4 asynchronous FCoE link event.
3187 static void
3188 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3189 struct lpfc_acqe_link *acqe_link)
3191 struct lpfc_dmabuf *mp;
3192 LPFC_MBOXQ_t *pmb;
3193 MAILBOX_t *mb;
3194 struct lpfc_mbx_read_top *la;
3195 uint8_t att_type;
3196 int rc;
3198 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3199 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
3200 return;
3201 phba->fcoe_eventtag = acqe_link->event_tag;
3202 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3203 if (!pmb) {
3204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3205 "0395 The mboxq allocation failed\n");
3206 return;
3208 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3209 if (!mp) {
3210 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3211 "0396 The lpfc_dmabuf allocation failed\n");
3212 goto out_free_pmb;
3214 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3215 if (!mp->virt) {
3216 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3217 "0397 The mbuf allocation failed\n");
3218 goto out_free_dmabuf;
3221 /* Cleanup any outstanding ELS commands */
3222 lpfc_els_flush_all_cmd(phba);
3224 /* Block ELS IOCBs until we have done process link event */
3225 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3227 /* Update link event statistics */
3228 phba->sli.slistat.link_event++;
3230 /* Create lpfc_handle_latt mailbox command from link ACQE */
3231 lpfc_read_topology(phba, pmb, mp);
3232 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3233 pmb->vport = phba->pport;
3235 /* Keep the link status for extra SLI4 state machine reference */
3236 phba->sli4_hba.link_state.speed =
3237 bf_get(lpfc_acqe_link_speed, acqe_link);
3238 phba->sli4_hba.link_state.duplex =
3239 bf_get(lpfc_acqe_link_duplex, acqe_link);
3240 phba->sli4_hba.link_state.status =
3241 bf_get(lpfc_acqe_link_status, acqe_link);
3242 phba->sli4_hba.link_state.type =
3243 bf_get(lpfc_acqe_link_type, acqe_link);
3244 phba->sli4_hba.link_state.number =
3245 bf_get(lpfc_acqe_link_number, acqe_link);
3246 phba->sli4_hba.link_state.fault =
3247 bf_get(lpfc_acqe_link_fault, acqe_link);
3248 phba->sli4_hba.link_state.logical_speed =
3249 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3250 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3251 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3252 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3253 "Logical speed:%dMbps Fault:%d\n",
3254 phba->sli4_hba.link_state.speed,
3255 phba->sli4_hba.link_state.topology,
3256 phba->sli4_hba.link_state.status,
3257 phba->sli4_hba.link_state.type,
3258 phba->sli4_hba.link_state.number,
3259 phba->sli4_hba.link_state.logical_speed * 10,
3260 phba->sli4_hba.link_state.fault);
3262 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3263 * topology info. Note: Optional for non FC-AL ports.
3265 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3266 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3267 if (rc == MBX_NOT_FINISHED)
3268 goto out_free_dmabuf;
3269 return;
3272 * For FCoE Mode: fill in all the topology information we need and call
3273 * the READ_TOPOLOGY completion routine to continue without actually
3274 * sending the READ_TOPOLOGY mailbox command to the port.
3276 /* Parse and translate status field */
3277 mb = &pmb->u.mb;
3278 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3280 /* Parse and translate link attention fields */
3281 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3282 la->eventTag = acqe_link->event_tag;
3283 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3284 bf_set(lpfc_mbx_read_top_link_spd, la,
3285 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3287 /* Fake the the following irrelvant fields */
3288 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3289 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3290 bf_set(lpfc_mbx_read_top_il, la, 0);
3291 bf_set(lpfc_mbx_read_top_pb, la, 0);
3292 bf_set(lpfc_mbx_read_top_fa, la, 0);
3293 bf_set(lpfc_mbx_read_top_mm, la, 0);
3295 /* Invoke the lpfc_handle_latt mailbox command callback function */
3296 lpfc_mbx_cmpl_read_topology(phba, pmb);
3298 return;
3300 out_free_dmabuf:
3301 kfree(mp);
3302 out_free_pmb:
3303 mempool_free(pmb, phba->mbox_mem_pool);
3307 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3308 * @phba: pointer to lpfc hba data structure.
3309 * @acqe_fc: pointer to the async fc completion queue entry.
3311 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3312 * that the event was received and then issue a read_topology mailbox command so
3313 * that the rest of the driver will treat it the same as SLI3.
3315 static void
3316 lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3318 struct lpfc_dmabuf *mp;
3319 LPFC_MBOXQ_t *pmb;
3320 int rc;
3322 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3323 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3324 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3325 "2895 Non FC link Event detected.(%d)\n",
3326 bf_get(lpfc_trailer_type, acqe_fc));
3327 return;
3329 /* Keep the link status for extra SLI4 state machine reference */
3330 phba->sli4_hba.link_state.speed =
3331 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3332 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3333 phba->sli4_hba.link_state.topology =
3334 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3335 phba->sli4_hba.link_state.status =
3336 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3337 phba->sli4_hba.link_state.type =
3338 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3339 phba->sli4_hba.link_state.number =
3340 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3341 phba->sli4_hba.link_state.fault =
3342 bf_get(lpfc_acqe_link_fault, acqe_fc);
3343 phba->sli4_hba.link_state.logical_speed =
3344 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3345 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3346 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3347 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3348 "%dMbps Fault:%d\n",
3349 phba->sli4_hba.link_state.speed,
3350 phba->sli4_hba.link_state.topology,
3351 phba->sli4_hba.link_state.status,
3352 phba->sli4_hba.link_state.type,
3353 phba->sli4_hba.link_state.number,
3354 phba->sli4_hba.link_state.logical_speed * 10,
3355 phba->sli4_hba.link_state.fault);
3356 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3357 if (!pmb) {
3358 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3359 "2897 The mboxq allocation failed\n");
3360 return;
3362 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3363 if (!mp) {
3364 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3365 "2898 The lpfc_dmabuf allocation failed\n");
3366 goto out_free_pmb;
3368 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3369 if (!mp->virt) {
3370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3371 "2899 The mbuf allocation failed\n");
3372 goto out_free_dmabuf;
3375 /* Cleanup any outstanding ELS commands */
3376 lpfc_els_flush_all_cmd(phba);
3378 /* Block ELS IOCBs until we have done process link event */
3379 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3381 /* Update link event statistics */
3382 phba->sli.slistat.link_event++;
3384 /* Create lpfc_handle_latt mailbox command from link ACQE */
3385 lpfc_read_topology(phba, pmb, mp);
3386 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3387 pmb->vport = phba->pport;
3389 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3390 if (rc == MBX_NOT_FINISHED)
3391 goto out_free_dmabuf;
3392 return;
3394 out_free_dmabuf:
3395 kfree(mp);
3396 out_free_pmb:
3397 mempool_free(pmb, phba->mbox_mem_pool);
3401 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3402 * @phba: pointer to lpfc hba data structure.
3403 * @acqe_fc: pointer to the async SLI completion queue entry.
3405 * This routine is to handle the SLI4 asynchronous SLI events.
3407 static void
3408 lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3410 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3411 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3412 "x%08x SLI Event Type:%d",
3413 acqe_sli->event_data1, acqe_sli->event_data2,
3414 bf_get(lpfc_trailer_type, acqe_sli));
3415 return;
3419 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3420 * @vport: pointer to vport data structure.
3422 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3423 * response to a CVL event.
3425 * Return the pointer to the ndlp with the vport if successful, otherwise
3426 * return NULL.
3428 static struct lpfc_nodelist *
3429 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3431 struct lpfc_nodelist *ndlp;
3432 struct Scsi_Host *shost;
3433 struct lpfc_hba *phba;
3435 if (!vport)
3436 return NULL;
3437 phba = vport->phba;
3438 if (!phba)
3439 return NULL;
3440 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3441 if (!ndlp) {
3442 /* Cannot find existing Fabric ndlp, so allocate a new one */
3443 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3444 if (!ndlp)
3445 return 0;
3446 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3447 /* Set the node type */
3448 ndlp->nlp_type |= NLP_FABRIC;
3449 /* Put ndlp onto node list */
3450 lpfc_enqueue_node(vport, ndlp);
3451 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3452 /* re-setup ndlp without removing from node list */
3453 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3454 if (!ndlp)
3455 return 0;
3457 if ((phba->pport->port_state < LPFC_FLOGI) &&
3458 (phba->pport->port_state != LPFC_VPORT_FAILED))
3459 return NULL;
3460 /* If virtual link is not yet instantiated ignore CVL */
3461 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3462 && (vport->port_state != LPFC_VPORT_FAILED))
3463 return NULL;
3464 shost = lpfc_shost_from_vport(vport);
3465 if (!shost)
3466 return NULL;
3467 lpfc_linkdown_port(vport);
3468 lpfc_cleanup_pending_mbox(vport);
3469 spin_lock_irq(shost->host_lock);
3470 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3471 spin_unlock_irq(shost->host_lock);
3473 return ndlp;
3477 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3478 * @vport: pointer to lpfc hba data structure.
3480 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3481 * response to a FCF dead event.
3483 static void
3484 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3486 struct lpfc_vport **vports;
3487 int i;
3489 vports = lpfc_create_vport_work_array(phba);
3490 if (vports)
3491 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3492 lpfc_sli4_perform_vport_cvl(vports[i]);
3493 lpfc_destroy_vport_work_array(phba, vports);
3497 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
3498 * @phba: pointer to lpfc hba data structure.
3499 * @acqe_link: pointer to the async fcoe completion queue entry.
3501 * This routine is to handle the SLI4 asynchronous fcoe event.
3503 static void
3504 lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
3505 struct lpfc_acqe_fip *acqe_fip)
3507 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
3508 int rc;
3509 struct lpfc_vport *vport;
3510 struct lpfc_nodelist *ndlp;
3511 struct Scsi_Host *shost;
3512 int active_vlink_present;
3513 struct lpfc_vport **vports;
3514 int i;
3516 phba->fc_eventTag = acqe_fip->event_tag;
3517 phba->fcoe_eventtag = acqe_fip->event_tag;
3518 switch (event_type) {
3519 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3520 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3521 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
3522 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3523 LOG_DISCOVERY,
3524 "2546 New FCF event, evt_tag:x%x, "
3525 "index:x%x\n",
3526 acqe_fip->event_tag,
3527 acqe_fip->index);
3528 else
3529 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3530 LOG_DISCOVERY,
3531 "2788 FCF param modified event, "
3532 "evt_tag:x%x, index:x%x\n",
3533 acqe_fip->event_tag,
3534 acqe_fip->index);
3535 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3537 * During period of FCF discovery, read the FCF
3538 * table record indexed by the event to update
3539 * FCF roundrobin failover eligible FCF bmask.
3541 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3542 LOG_DISCOVERY,
3543 "2779 Read FCF (x%x) for updating "
3544 "roundrobin FCF failover bmask\n",
3545 acqe_fip->index);
3546 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
3549 /* If the FCF discovery is in progress, do nothing. */
3550 spin_lock_irq(&phba->hbalock);
3551 if (phba->hba_flag & FCF_TS_INPROG) {
3552 spin_unlock_irq(&phba->hbalock);
3553 break;
3555 /* If fast FCF failover rescan event is pending, do nothing */
3556 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3557 spin_unlock_irq(&phba->hbalock);
3558 break;
3561 /* If the FCF has been in discovered state, do nothing. */
3562 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3563 spin_unlock_irq(&phba->hbalock);
3564 break;
3566 spin_unlock_irq(&phba->hbalock);
3568 /* Otherwise, scan the entire FCF table and re-discover SAN */
3569 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3570 "2770 Start FCF table scan per async FCF "
3571 "event, evt_tag:x%x, index:x%x\n",
3572 acqe_fip->event_tag, acqe_fip->index);
3573 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3574 LPFC_FCOE_FCF_GET_FIRST);
3575 if (rc)
3576 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3577 "2547 Issue FCF scan read FCF mailbox "
3578 "command failed (x%x)\n", rc);
3579 break;
3581 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
3582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3583 "2548 FCF Table full count 0x%x tag 0x%x\n",
3584 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3585 acqe_fip->event_tag);
3586 break;
3588 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
3589 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3590 "2549 FCF (x%x) disconnected from network, "
3591 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
3593 * If we are in the middle of FCF failover process, clear
3594 * the corresponding FCF bit in the roundrobin bitmap.
3596 spin_lock_irq(&phba->hbalock);
3597 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3598 spin_unlock_irq(&phba->hbalock);
3599 /* Update FLOGI FCF failover eligible FCF bmask */
3600 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
3601 break;
3603 spin_unlock_irq(&phba->hbalock);
3605 /* If the event is not for currently used fcf do nothing */
3606 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
3607 break;
3610 * Otherwise, request the port to rediscover the entire FCF
3611 * table for a fast recovery from case that the current FCF
3612 * is no longer valid as we are not in the middle of FCF
3613 * failover process already.
3615 spin_lock_irq(&phba->hbalock);
3616 /* Mark the fast failover process in progress */
3617 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
3618 spin_unlock_irq(&phba->hbalock);
3620 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3621 "2771 Start FCF fast failover process due to "
3622 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3623 "\n", acqe_fip->event_tag, acqe_fip->index);
3624 rc = lpfc_sli4_redisc_fcf_table(phba);
3625 if (rc) {
3626 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3627 LOG_DISCOVERY,
3628 "2772 Issue FCF rediscover mabilbox "
3629 "command failed, fail through to FCF "
3630 "dead event\n");
3631 spin_lock_irq(&phba->hbalock);
3632 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
3633 spin_unlock_irq(&phba->hbalock);
3635 * Last resort will fail over by treating this
3636 * as a link down to FCF registration.
3638 lpfc_sli4_fcf_dead_failthrough(phba);
3639 } else {
3640 /* Reset FCF roundrobin bmask for new discovery */
3641 lpfc_sli4_clear_fcf_rr_bmask(phba);
3643 * Handling fast FCF failover to a DEAD FCF event is
3644 * considered equalivant to receiving CVL to all vports.
3646 lpfc_sli4_perform_all_vport_cvl(phba);
3648 break;
3649 case LPFC_FIP_EVENT_TYPE_CVL:
3650 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3651 "2718 Clear Virtual Link Received for VPI 0x%x"
3652 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
3654 vport = lpfc_find_vport_by_vpid(phba,
3655 acqe_fip->index);
3656 ndlp = lpfc_sli4_perform_vport_cvl(vport);
3657 if (!ndlp)
3658 break;
3659 active_vlink_present = 0;
3661 vports = lpfc_create_vport_work_array(phba);
3662 if (vports) {
3663 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3664 i++) {
3665 if ((!(vports[i]->fc_flag &
3666 FC_VPORT_CVL_RCVD)) &&
3667 (vports[i]->port_state > LPFC_FDISC)) {
3668 active_vlink_present = 1;
3669 break;
3672 lpfc_destroy_vport_work_array(phba, vports);
3675 if (active_vlink_present) {
3677 * If there are other active VLinks present,
3678 * re-instantiate the Vlink using FDISC.
3680 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3681 shost = lpfc_shost_from_vport(vport);
3682 spin_lock_irq(shost->host_lock);
3683 ndlp->nlp_flag |= NLP_DELAY_TMO;
3684 spin_unlock_irq(shost->host_lock);
3685 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3686 vport->port_state = LPFC_FDISC;
3687 } else {
3689 * Otherwise, we request port to rediscover
3690 * the entire FCF table for a fast recovery
3691 * from possible case that the current FCF
3692 * is no longer valid if we are not already
3693 * in the FCF failover process.
3695 spin_lock_irq(&phba->hbalock);
3696 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3697 spin_unlock_irq(&phba->hbalock);
3698 break;
3700 /* Mark the fast failover process in progress */
3701 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
3702 spin_unlock_irq(&phba->hbalock);
3703 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3704 LOG_DISCOVERY,
3705 "2773 Start FCF failover per CVL, "
3706 "evt_tag:x%x\n", acqe_fip->event_tag);
3707 rc = lpfc_sli4_redisc_fcf_table(phba);
3708 if (rc) {
3709 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3710 LOG_DISCOVERY,
3711 "2774 Issue FCF rediscover "
3712 "mabilbox command failed, "
3713 "through to CVL event\n");
3714 spin_lock_irq(&phba->hbalock);
3715 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
3716 spin_unlock_irq(&phba->hbalock);
3718 * Last resort will be re-try on the
3719 * the current registered FCF entry.
3721 lpfc_retry_pport_discovery(phba);
3722 } else
3724 * Reset FCF roundrobin bmask for new
3725 * discovery.
3727 lpfc_sli4_clear_fcf_rr_bmask(phba);
3729 break;
3730 default:
3731 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3732 "0288 Unknown FCoE event type 0x%x event tag "
3733 "0x%x\n", event_type, acqe_fip->event_tag);
3734 break;
3739 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3740 * @phba: pointer to lpfc hba data structure.
3741 * @acqe_link: pointer to the async dcbx completion queue entry.
3743 * This routine is to handle the SLI4 asynchronous dcbx event.
3745 static void
3746 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3747 struct lpfc_acqe_dcbx *acqe_dcbx)
3749 phba->fc_eventTag = acqe_dcbx->event_tag;
3750 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3751 "0290 The SLI4 DCBX asynchronous event is not "
3752 "handled yet\n");
3756 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3757 * @phba: pointer to lpfc hba data structure.
3758 * @acqe_link: pointer to the async grp5 completion queue entry.
3760 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3761 * is an asynchronous notified of a logical link speed change. The Port
3762 * reports the logical link speed in units of 10Mbps.
3764 static void
3765 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3766 struct lpfc_acqe_grp5 *acqe_grp5)
3768 uint16_t prev_ll_spd;
3770 phba->fc_eventTag = acqe_grp5->event_tag;
3771 phba->fcoe_eventtag = acqe_grp5->event_tag;
3772 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3773 phba->sli4_hba.link_state.logical_speed =
3774 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3775 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3776 "2789 GRP5 Async Event: Updating logical link speed "
3777 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3778 (phba->sli4_hba.link_state.logical_speed*10));
3782 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3783 * @phba: pointer to lpfc hba data structure.
3785 * This routine is invoked by the worker thread to process all the pending
3786 * SLI4 asynchronous events.
3788 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3790 struct lpfc_cq_event *cq_event;
3792 /* First, declare the async event has been handled */
3793 spin_lock_irq(&phba->hbalock);
3794 phba->hba_flag &= ~ASYNC_EVENT;
3795 spin_unlock_irq(&phba->hbalock);
3796 /* Now, handle all the async events */
3797 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3798 /* Get the first event from the head of the event queue */
3799 spin_lock_irq(&phba->hbalock);
3800 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3801 cq_event, struct lpfc_cq_event, list);
3802 spin_unlock_irq(&phba->hbalock);
3803 /* Process the asynchronous event */
3804 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3805 case LPFC_TRAILER_CODE_LINK:
3806 lpfc_sli4_async_link_evt(phba,
3807 &cq_event->cqe.acqe_link);
3808 break;
3809 case LPFC_TRAILER_CODE_FCOE:
3810 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
3811 break;
3812 case LPFC_TRAILER_CODE_DCBX:
3813 lpfc_sli4_async_dcbx_evt(phba,
3814 &cq_event->cqe.acqe_dcbx);
3815 break;
3816 case LPFC_TRAILER_CODE_GRP5:
3817 lpfc_sli4_async_grp5_evt(phba,
3818 &cq_event->cqe.acqe_grp5);
3819 break;
3820 case LPFC_TRAILER_CODE_FC:
3821 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
3822 break;
3823 case LPFC_TRAILER_CODE_SLI:
3824 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
3825 break;
3826 default:
3827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3828 "1804 Invalid asynchrous event code: "
3829 "x%x\n", bf_get(lpfc_trailer_code,
3830 &cq_event->cqe.mcqe_cmpl));
3831 break;
3833 /* Free the completion event processed to the free pool */
3834 lpfc_sli4_cq_event_release(phba, cq_event);
3839 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3840 * @phba: pointer to lpfc hba data structure.
3842 * This routine is invoked by the worker thread to process FCF table
3843 * rediscovery pending completion event.
3845 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3847 int rc;
3849 spin_lock_irq(&phba->hbalock);
3850 /* Clear FCF rediscovery timeout event */
3851 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3852 /* Clear driver fast failover FCF record flag */
3853 phba->fcf.failover_rec.flag = 0;
3854 /* Set state for FCF fast failover */
3855 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3856 spin_unlock_irq(&phba->hbalock);
3858 /* Scan FCF table from the first entry to re-discover SAN */
3859 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3860 "2777 Start post-quiescent FCF table scan\n");
3861 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
3862 if (rc)
3863 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3864 "2747 Issue FCF scan read FCF mailbox "
3865 "command failed 0x%x\n", rc);
3869 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3870 * @phba: pointer to lpfc hba data structure.
3871 * @dev_grp: The HBA PCI-Device group number.
3873 * This routine is invoked to set up the per HBA PCI-Device group function
3874 * API jump table entries.
3876 * Return: 0 if success, otherwise -ENODEV
3879 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3881 int rc;
3883 /* Set up lpfc PCI-device group */
3884 phba->pci_dev_grp = dev_grp;
3886 /* The LPFC_PCI_DEV_OC uses SLI4 */
3887 if (dev_grp == LPFC_PCI_DEV_OC)
3888 phba->sli_rev = LPFC_SLI_REV4;
3890 /* Set up device INIT API function jump table */
3891 rc = lpfc_init_api_table_setup(phba, dev_grp);
3892 if (rc)
3893 return -ENODEV;
3894 /* Set up SCSI API function jump table */
3895 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3896 if (rc)
3897 return -ENODEV;
3898 /* Set up SLI API function jump table */
3899 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3900 if (rc)
3901 return -ENODEV;
3902 /* Set up MBOX API function jump table */
3903 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3904 if (rc)
3905 return -ENODEV;
3907 return 0;
3911 * lpfc_log_intr_mode - Log the active interrupt mode
3912 * @phba: pointer to lpfc hba data structure.
3913 * @intr_mode: active interrupt mode adopted.
3915 * This routine it invoked to log the currently used active interrupt mode
3916 * to the device.
3918 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
3920 switch (intr_mode) {
3921 case 0:
3922 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3923 "0470 Enable INTx interrupt mode.\n");
3924 break;
3925 case 1:
3926 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3927 "0481 Enabled MSI interrupt mode.\n");
3928 break;
3929 case 2:
3930 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3931 "0480 Enabled MSI-X interrupt mode.\n");
3932 break;
3933 default:
3934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3935 "0482 Illegal interrupt mode.\n");
3936 break;
3938 return;
3942 * lpfc_enable_pci_dev - Enable a generic PCI device.
3943 * @phba: pointer to lpfc hba data structure.
3945 * This routine is invoked to enable the PCI device that is common to all
3946 * PCI devices.
3948 * Return codes
3949 * 0 - successful
3950 * other values - error
3952 static int
3953 lpfc_enable_pci_dev(struct lpfc_hba *phba)
3955 struct pci_dev *pdev;
3956 int bars = 0;
3958 /* Obtain PCI device reference */
3959 if (!phba->pcidev)
3960 goto out_error;
3961 else
3962 pdev = phba->pcidev;
3963 /* Select PCI BARs */
3964 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3965 /* Enable PCI device */
3966 if (pci_enable_device_mem(pdev))
3967 goto out_error;
3968 /* Request PCI resource for the device */
3969 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3970 goto out_disable_device;
3971 /* Set up device as PCI master and save state for EEH */
3972 pci_set_master(pdev);
3973 pci_try_set_mwi(pdev);
3974 pci_save_state(pdev);
3976 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
3977 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
3978 pdev->needs_freset = 1;
3980 return 0;
3982 out_disable_device:
3983 pci_disable_device(pdev);
3984 out_error:
3985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3986 "1401 Failed to enable pci device, bars:x%x\n", bars);
3987 return -ENODEV;
3991 * lpfc_disable_pci_dev - Disable a generic PCI device.
3992 * @phba: pointer to lpfc hba data structure.
3994 * This routine is invoked to disable the PCI device that is common to all
3995 * PCI devices.
3997 static void
3998 lpfc_disable_pci_dev(struct lpfc_hba *phba)
4000 struct pci_dev *pdev;
4001 int bars;
4003 /* Obtain PCI device reference */
4004 if (!phba->pcidev)
4005 return;
4006 else
4007 pdev = phba->pcidev;
4008 /* Select PCI BARs */
4009 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4010 /* Release PCI resource and disable PCI device */
4011 pci_release_selected_regions(pdev, bars);
4012 pci_disable_device(pdev);
4013 /* Null out PCI private reference to driver */
4014 pci_set_drvdata(pdev, NULL);
4016 return;
4020 * lpfc_reset_hba - Reset a hba
4021 * @phba: pointer to lpfc hba data structure.
4023 * This routine is invoked to reset a hba device. It brings the HBA
4024 * offline, performs a board restart, and then brings the board back
4025 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4026 * on outstanding mailbox commands.
4028 void
4029 lpfc_reset_hba(struct lpfc_hba *phba)
4031 /* If resets are disabled then set error state and return. */
4032 if (!phba->cfg_enable_hba_reset) {
4033 phba->link_state = LPFC_HBA_ERROR;
4034 return;
4036 lpfc_offline_prep(phba);
4037 lpfc_offline(phba);
4038 lpfc_sli_brdrestart(phba);
4039 lpfc_online(phba);
4040 lpfc_unblock_mgmt_io(phba);
4044 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4045 * @phba: pointer to lpfc hba data structure.
4047 * This function enables the PCI SR-IOV virtual functions to a physical
4048 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4049 * enable the number of virtual functions to the physical function. As
4050 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4051 * API call does not considered as an error condition for most of the device.
4053 uint16_t
4054 lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4056 struct pci_dev *pdev = phba->pcidev;
4057 uint16_t nr_virtfn;
4058 int pos;
4060 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4061 if (pos == 0)
4062 return 0;
4064 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4065 return nr_virtfn;
4069 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4070 * @phba: pointer to lpfc hba data structure.
4071 * @nr_vfn: number of virtual functions to be enabled.
4073 * This function enables the PCI SR-IOV virtual functions to a physical
4074 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4075 * enable the number of virtual functions to the physical function. As
4076 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4077 * API call does not considered as an error condition for most of the device.
4080 lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4082 struct pci_dev *pdev = phba->pcidev;
4083 uint16_t max_nr_vfn;
4084 int rc;
4086 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4087 if (nr_vfn > max_nr_vfn) {
4088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4089 "3057 Requested vfs (%d) greater than "
4090 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4091 return -EINVAL;
4094 rc = pci_enable_sriov(pdev, nr_vfn);
4095 if (rc) {
4096 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4097 "2806 Failed to enable sriov on this device "
4098 "with vfn number nr_vf:%d, rc:%d\n",
4099 nr_vfn, rc);
4100 } else
4101 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4102 "2807 Successful enable sriov on this device "
4103 "with vfn number nr_vf:%d\n", nr_vfn);
4104 return rc;
4108 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4109 * @phba: pointer to lpfc hba data structure.
4111 * This routine is invoked to set up the driver internal resources specific to
4112 * support the SLI-3 HBA device it attached to.
4114 * Return codes
4115 * 0 - successful
4116 * other values - error
4118 static int
4119 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4121 struct lpfc_sli *psli;
4122 int rc;
4125 * Initialize timers used by driver
4128 /* Heartbeat timer */
4129 init_timer(&phba->hb_tmofunc);
4130 phba->hb_tmofunc.function = lpfc_hb_timeout;
4131 phba->hb_tmofunc.data = (unsigned long)phba;
4133 psli = &phba->sli;
4134 /* MBOX heartbeat timer */
4135 init_timer(&psli->mbox_tmo);
4136 psli->mbox_tmo.function = lpfc_mbox_timeout;
4137 psli->mbox_tmo.data = (unsigned long) phba;
4138 /* FCP polling mode timer */
4139 init_timer(&phba->fcp_poll_timer);
4140 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4141 phba->fcp_poll_timer.data = (unsigned long) phba;
4142 /* Fabric block timer */
4143 init_timer(&phba->fabric_block_timer);
4144 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4145 phba->fabric_block_timer.data = (unsigned long) phba;
4146 /* EA polling mode timer */
4147 init_timer(&phba->eratt_poll);
4148 phba->eratt_poll.function = lpfc_poll_eratt;
4149 phba->eratt_poll.data = (unsigned long) phba;
4151 /* Host attention work mask setup */
4152 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4153 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4155 /* Get all the module params for configuring this host */
4156 lpfc_get_cfgparam(phba);
4157 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4158 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4159 /* check for menlo minimum sg count */
4160 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4161 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4165 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4166 * used to create the sg_dma_buf_pool must be dynamically calculated.
4167 * 2 segments are added since the IOCB needs a command and response bde.
4169 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4170 sizeof(struct fcp_rsp) +
4171 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4173 if (phba->cfg_enable_bg) {
4174 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4175 phba->cfg_sg_dma_buf_size +=
4176 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4179 /* Also reinitialize the host templates with new values. */
4180 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4181 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4183 phba->max_vpi = LPFC_MAX_VPI;
4184 /* This will be set to correct value after config_port mbox */
4185 phba->max_vports = 0;
4188 * Initialize the SLI Layer to run with lpfc HBAs.
4190 lpfc_sli_setup(phba);
4191 lpfc_sli_queue_setup(phba);
4193 /* Allocate device driver memory */
4194 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4195 return -ENOMEM;
4198 * Enable sr-iov virtual functions if supported and configured
4199 * through the module parameter.
4201 if (phba->cfg_sriov_nr_virtfn > 0) {
4202 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4203 phba->cfg_sriov_nr_virtfn);
4204 if (rc) {
4205 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4206 "2808 Requested number of SR-IOV "
4207 "virtual functions (%d) is not "
4208 "supported\n",
4209 phba->cfg_sriov_nr_virtfn);
4210 phba->cfg_sriov_nr_virtfn = 0;
4214 return 0;
4218 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4219 * @phba: pointer to lpfc hba data structure.
4221 * This routine is invoked to unset the driver internal resources set up
4222 * specific for supporting the SLI-3 HBA device it attached to.
4224 static void
4225 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4227 /* Free device driver memory allocated */
4228 lpfc_mem_free_all(phba);
4230 return;
4234 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4235 * @phba: pointer to lpfc hba data structure.
4237 * This routine is invoked to set up the driver internal resources specific to
4238 * support the SLI-4 HBA device it attached to.
4240 * Return codes
4241 * 0 - successful
4242 * other values - error
4244 static int
4245 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4247 struct lpfc_sli *psli;
4248 LPFC_MBOXQ_t *mboxq;
4249 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4250 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4251 struct lpfc_mqe *mqe;
4252 int longs, sli_family;
4254 /* Before proceed, wait for POST done and device ready */
4255 rc = lpfc_sli4_post_status_check(phba);
4256 if (rc)
4257 return -ENODEV;
4260 * Initialize timers used by driver
4263 /* Heartbeat timer */
4264 init_timer(&phba->hb_tmofunc);
4265 phba->hb_tmofunc.function = lpfc_hb_timeout;
4266 phba->hb_tmofunc.data = (unsigned long)phba;
4267 init_timer(&phba->rrq_tmr);
4268 phba->rrq_tmr.function = lpfc_rrq_timeout;
4269 phba->rrq_tmr.data = (unsigned long)phba;
4271 psli = &phba->sli;
4272 /* MBOX heartbeat timer */
4273 init_timer(&psli->mbox_tmo);
4274 psli->mbox_tmo.function = lpfc_mbox_timeout;
4275 psli->mbox_tmo.data = (unsigned long) phba;
4276 /* Fabric block timer */
4277 init_timer(&phba->fabric_block_timer);
4278 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4279 phba->fabric_block_timer.data = (unsigned long) phba;
4280 /* EA polling mode timer */
4281 init_timer(&phba->eratt_poll);
4282 phba->eratt_poll.function = lpfc_poll_eratt;
4283 phba->eratt_poll.data = (unsigned long) phba;
4284 /* FCF rediscover timer */
4285 init_timer(&phba->fcf.redisc_wait);
4286 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4287 phba->fcf.redisc_wait.data = (unsigned long)phba;
4290 * Control structure for handling external multi-buffer mailbox
4291 * command pass-through.
4293 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4294 sizeof(struct lpfc_mbox_ext_buf_ctx));
4295 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4298 * We need to do a READ_CONFIG mailbox command here before
4299 * calling lpfc_get_cfgparam. For VFs this will report the
4300 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4301 * All of the resources allocated
4302 * for this Port are tied to these values.
4304 /* Get all the module params for configuring this host */
4305 lpfc_get_cfgparam(phba);
4306 phba->max_vpi = LPFC_MAX_VPI;
4307 /* This will be set to correct value after the read_config mbox */
4308 phba->max_vports = 0;
4310 /* Program the default value of vlan_id and fc_map */
4311 phba->valid_vlan = 0;
4312 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4313 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4314 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4317 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4318 * used to create the sg_dma_buf_pool must be dynamically calculated.
4319 * 2 segments are added since the IOCB needs a command and response bde.
4320 * To insure that the scsi sgl does not cross a 4k page boundary only
4321 * sgl sizes of must be a power of 2.
4323 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4324 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
4326 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4327 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4328 switch (sli_family) {
4329 case LPFC_SLI_INTF_FAMILY_BE2:
4330 case LPFC_SLI_INTF_FAMILY_BE3:
4331 /* There is a single hint for BE - 2 pages per BPL. */
4332 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4333 LPFC_SLI_INTF_SLI_HINT1_1)
4334 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4335 break;
4336 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4337 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4338 default:
4339 break;
4341 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4342 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4343 dma_buf_size = dma_buf_size << 1)
4345 if (dma_buf_size == max_buf_size)
4346 phba->cfg_sg_seg_cnt = (dma_buf_size -
4347 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4348 (2 * sizeof(struct sli4_sge))) /
4349 sizeof(struct sli4_sge);
4350 phba->cfg_sg_dma_buf_size = dma_buf_size;
4352 /* Initialize buffer queue management fields */
4353 hbq_count = lpfc_sli_hbq_count();
4354 for (i = 0; i < hbq_count; ++i)
4355 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4356 INIT_LIST_HEAD(&phba->rb_pend_list);
4357 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4358 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4361 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4363 /* Initialize the Abort scsi buffer list used by driver */
4364 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4365 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4366 /* This abort list used by worker thread */
4367 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4370 * Initialize driver internal slow-path work queues
4373 /* Driver internel slow-path CQ Event pool */
4374 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4375 /* Response IOCB work queue list */
4376 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
4377 /* Asynchronous event CQ Event work queue list */
4378 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4379 /* Fast-path XRI aborted CQ Event work queue list */
4380 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4381 /* Slow-path XRI aborted CQ Event work queue list */
4382 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4383 /* Receive queue CQ Event work queue list */
4384 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4386 /* Initialize extent block lists. */
4387 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4388 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4389 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4390 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4392 /* Initialize the driver internal SLI layer lists. */
4393 lpfc_sli_setup(phba);
4394 lpfc_sli_queue_setup(phba);
4396 /* Allocate device driver memory */
4397 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4398 if (rc)
4399 return -ENOMEM;
4401 /* IF Type 2 ports get initialized now. */
4402 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4403 LPFC_SLI_INTF_IF_TYPE_2) {
4404 rc = lpfc_pci_function_reset(phba);
4405 if (unlikely(rc))
4406 return -ENODEV;
4409 /* Create the bootstrap mailbox command */
4410 rc = lpfc_create_bootstrap_mbox(phba);
4411 if (unlikely(rc))
4412 goto out_free_mem;
4414 /* Set up the host's endian order with the device. */
4415 rc = lpfc_setup_endian_order(phba);
4416 if (unlikely(rc))
4417 goto out_free_bsmbx;
4419 /* Set up the hba's configuration parameters. */
4420 rc = lpfc_sli4_read_config(phba);
4421 if (unlikely(rc))
4422 goto out_free_bsmbx;
4424 /* IF Type 0 ports get initialized now. */
4425 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4426 LPFC_SLI_INTF_IF_TYPE_0) {
4427 rc = lpfc_pci_function_reset(phba);
4428 if (unlikely(rc))
4429 goto out_free_bsmbx;
4432 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4433 GFP_KERNEL);
4434 if (!mboxq) {
4435 rc = -ENOMEM;
4436 goto out_free_bsmbx;
4439 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
4440 lpfc_supported_pages(mboxq);
4441 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4442 if (!rc) {
4443 mqe = &mboxq->u.mqe;
4444 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4445 LPFC_MAX_SUPPORTED_PAGES);
4446 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4447 switch (pn_page[i]) {
4448 case LPFC_SLI4_PARAMETERS:
4449 phba->sli4_hba.pc_sli4_params.supported = 1;
4450 break;
4451 default:
4452 break;
4455 /* Read the port's SLI4 Parameters capabilities if supported. */
4456 if (phba->sli4_hba.pc_sli4_params.supported)
4457 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4458 if (rc) {
4459 mempool_free(mboxq, phba->mbox_mem_pool);
4460 rc = -EIO;
4461 goto out_free_bsmbx;
4465 * Get sli4 parameters that override parameters from Port capabilities.
4466 * If this call fails, it isn't critical unless the SLI4 parameters come
4467 * back in conflict.
4469 rc = lpfc_get_sli4_parameters(phba, mboxq);
4470 if (rc) {
4471 if (phba->sli4_hba.extents_in_use &&
4472 phba->sli4_hba.rpi_hdrs_in_use) {
4473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4474 "2999 Unsupported SLI4 Parameters "
4475 "Extents and RPI headers enabled.\n");
4476 goto out_free_bsmbx;
4479 mempool_free(mboxq, phba->mbox_mem_pool);
4480 /* Create all the SLI4 queues */
4481 rc = lpfc_sli4_queue_create(phba);
4482 if (rc)
4483 goto out_free_bsmbx;
4485 /* Create driver internal CQE event pool */
4486 rc = lpfc_sli4_cq_event_pool_create(phba);
4487 if (rc)
4488 goto out_destroy_queue;
4490 /* Initialize and populate the iocb list per host */
4491 rc = lpfc_init_sgl_list(phba);
4492 if (rc) {
4493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4494 "1400 Failed to initialize sgl list.\n");
4495 goto out_destroy_cq_event_pool;
4497 rc = lpfc_init_active_sgl_array(phba);
4498 if (rc) {
4499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4500 "1430 Failed to initialize sgl list.\n");
4501 goto out_free_sgl_list;
4503 rc = lpfc_sli4_init_rpi_hdrs(phba);
4504 if (rc) {
4505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4506 "1432 Failed to initialize rpi headers.\n");
4507 goto out_free_active_sgl;
4510 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
4511 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4512 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4513 GFP_KERNEL);
4514 if (!phba->fcf.fcf_rr_bmask) {
4515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4516 "2759 Failed allocate memory for FCF round "
4517 "robin failover bmask\n");
4518 rc = -ENOMEM;
4519 goto out_remove_rpi_hdrs;
4522 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4523 phba->cfg_fcp_eq_count), GFP_KERNEL);
4524 if (!phba->sli4_hba.fcp_eq_hdl) {
4525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4526 "2572 Failed allocate memory for fast-path "
4527 "per-EQ handle array\n");
4528 rc = -ENOMEM;
4529 goto out_free_fcf_rr_bmask;
4532 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4533 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4534 if (!phba->sli4_hba.msix_entries) {
4535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4536 "2573 Failed allocate memory for msi-x "
4537 "interrupt vector entries\n");
4538 rc = -ENOMEM;
4539 goto out_free_fcp_eq_hdl;
4543 * Enable sr-iov virtual functions if supported and configured
4544 * through the module parameter.
4546 if (phba->cfg_sriov_nr_virtfn > 0) {
4547 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4548 phba->cfg_sriov_nr_virtfn);
4549 if (rc) {
4550 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4551 "3020 Requested number of SR-IOV "
4552 "virtual functions (%d) is not "
4553 "supported\n",
4554 phba->cfg_sriov_nr_virtfn);
4555 phba->cfg_sriov_nr_virtfn = 0;
4559 return 0;
4561 out_free_fcp_eq_hdl:
4562 kfree(phba->sli4_hba.fcp_eq_hdl);
4563 out_free_fcf_rr_bmask:
4564 kfree(phba->fcf.fcf_rr_bmask);
4565 out_remove_rpi_hdrs:
4566 lpfc_sli4_remove_rpi_hdrs(phba);
4567 out_free_active_sgl:
4568 lpfc_free_active_sgl(phba);
4569 out_free_sgl_list:
4570 lpfc_free_sgl_list(phba);
4571 out_destroy_cq_event_pool:
4572 lpfc_sli4_cq_event_pool_destroy(phba);
4573 out_destroy_queue:
4574 lpfc_sli4_queue_destroy(phba);
4575 out_free_bsmbx:
4576 lpfc_destroy_bootstrap_mbox(phba);
4577 out_free_mem:
4578 lpfc_mem_free(phba);
4579 return rc;
4583 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4584 * @phba: pointer to lpfc hba data structure.
4586 * This routine is invoked to unset the driver internal resources set up
4587 * specific for supporting the SLI-4 HBA device it attached to.
4589 static void
4590 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4592 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4594 /* Free memory allocated for msi-x interrupt vector entries */
4595 kfree(phba->sli4_hba.msix_entries);
4597 /* Free memory allocated for fast-path work queue handles */
4598 kfree(phba->sli4_hba.fcp_eq_hdl);
4600 /* Free the allocated rpi headers. */
4601 lpfc_sli4_remove_rpi_hdrs(phba);
4602 lpfc_sli4_remove_rpis(phba);
4604 /* Free eligible FCF index bmask */
4605 kfree(phba->fcf.fcf_rr_bmask);
4607 /* Free the ELS sgl list */
4608 lpfc_free_active_sgl(phba);
4609 lpfc_free_sgl_list(phba);
4611 /* Free the SCSI sgl management array */
4612 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4614 /* Free the SLI4 queues */
4615 lpfc_sli4_queue_destroy(phba);
4617 /* Free the completion queue EQ event pool */
4618 lpfc_sli4_cq_event_release_all(phba);
4619 lpfc_sli4_cq_event_pool_destroy(phba);
4621 /* Release resource identifiers. */
4622 lpfc_sli4_dealloc_resource_identifiers(phba);
4624 /* Free the bsmbx region. */
4625 lpfc_destroy_bootstrap_mbox(phba);
4627 /* Free the SLI Layer memory with SLI4 HBAs */
4628 lpfc_mem_free_all(phba);
4630 /* Free the current connect table */
4631 list_for_each_entry_safe(conn_entry, next_conn_entry,
4632 &phba->fcf_conn_rec_list, list) {
4633 list_del_init(&conn_entry->list);
4634 kfree(conn_entry);
4637 return;
4641 * lpfc_init_api_table_setup - Set up init api function jump table
4642 * @phba: The hba struct for which this call is being executed.
4643 * @dev_grp: The HBA PCI-Device group number.
4645 * This routine sets up the device INIT interface API function jump table
4646 * in @phba struct.
4648 * Returns: 0 - success, -ENODEV - failure.
4651 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4653 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4654 phba->lpfc_hba_down_link = lpfc_hba_down_link;
4655 phba->lpfc_selective_reset = lpfc_selective_reset;
4656 switch (dev_grp) {
4657 case LPFC_PCI_DEV_LP:
4658 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4659 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4660 phba->lpfc_stop_port = lpfc_stop_port_s3;
4661 break;
4662 case LPFC_PCI_DEV_OC:
4663 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4664 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4665 phba->lpfc_stop_port = lpfc_stop_port_s4;
4666 break;
4667 default:
4668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4669 "1431 Invalid HBA PCI-device group: 0x%x\n",
4670 dev_grp);
4671 return -ENODEV;
4672 break;
4674 return 0;
4678 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4679 * @phba: pointer to lpfc hba data structure.
4681 * This routine is invoked to set up the driver internal resources before the
4682 * device specific resource setup to support the HBA device it attached to.
4684 * Return codes
4685 * 0 - successful
4686 * other values - error
4688 static int
4689 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4692 * Driver resources common to all SLI revisions
4694 atomic_set(&phba->fast_event_count, 0);
4695 spin_lock_init(&phba->hbalock);
4697 /* Initialize ndlp management spinlock */
4698 spin_lock_init(&phba->ndlp_lock);
4700 INIT_LIST_HEAD(&phba->port_list);
4701 INIT_LIST_HEAD(&phba->work_list);
4702 init_waitqueue_head(&phba->wait_4_mlo_m_q);
4704 /* Initialize the wait queue head for the kernel thread */
4705 init_waitqueue_head(&phba->work_waitq);
4707 /* Initialize the scsi buffer list used by driver for scsi IO */
4708 spin_lock_init(&phba->scsi_buf_list_lock);
4709 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
4711 /* Initialize the fabric iocb list */
4712 INIT_LIST_HEAD(&phba->fabric_iocb_list);
4714 /* Initialize list to save ELS buffers */
4715 INIT_LIST_HEAD(&phba->elsbuf);
4717 /* Initialize FCF connection rec list */
4718 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
4720 return 0;
4724 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4725 * @phba: pointer to lpfc hba data structure.
4727 * This routine is invoked to set up the driver internal resources after the
4728 * device specific resource setup to support the HBA device it attached to.
4730 * Return codes
4731 * 0 - successful
4732 * other values - error
4734 static int
4735 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4737 int error;
4739 /* Startup the kernel thread for this host adapter. */
4740 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4741 "lpfc_worker_%d", phba->brd_no);
4742 if (IS_ERR(phba->worker_thread)) {
4743 error = PTR_ERR(phba->worker_thread);
4744 return error;
4747 return 0;
4751 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4752 * @phba: pointer to lpfc hba data structure.
4754 * This routine is invoked to unset the driver internal resources set up after
4755 * the device specific resource setup for supporting the HBA device it
4756 * attached to.
4758 static void
4759 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4761 /* Stop kernel worker thread */
4762 kthread_stop(phba->worker_thread);
4766 * lpfc_free_iocb_list - Free iocb list.
4767 * @phba: pointer to lpfc hba data structure.
4769 * This routine is invoked to free the driver's IOCB list and memory.
4771 static void
4772 lpfc_free_iocb_list(struct lpfc_hba *phba)
4774 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4776 spin_lock_irq(&phba->hbalock);
4777 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4778 &phba->lpfc_iocb_list, list) {
4779 list_del(&iocbq_entry->list);
4780 kfree(iocbq_entry);
4781 phba->total_iocbq_bufs--;
4783 spin_unlock_irq(&phba->hbalock);
4785 return;
4789 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4790 * @phba: pointer to lpfc hba data structure.
4792 * This routine is invoked to allocate and initizlize the driver's IOCB
4793 * list and set up the IOCB tag array accordingly.
4795 * Return codes
4796 * 0 - successful
4797 * other values - error
4799 static int
4800 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4802 struct lpfc_iocbq *iocbq_entry = NULL;
4803 uint16_t iotag;
4804 int i;
4806 /* Initialize and populate the iocb list per host. */
4807 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
4808 for (i = 0; i < iocb_count; i++) {
4809 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
4810 if (iocbq_entry == NULL) {
4811 printk(KERN_ERR "%s: only allocated %d iocbs of "
4812 "expected %d count. Unloading driver.\n",
4813 __func__, i, LPFC_IOCB_LIST_CNT);
4814 goto out_free_iocbq;
4817 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4818 if (iotag == 0) {
4819 kfree(iocbq_entry);
4820 printk(KERN_ERR "%s: failed to allocate IOTAG. "
4821 "Unloading driver.\n", __func__);
4822 goto out_free_iocbq;
4824 iocbq_entry->sli4_lxritag = NO_XRI;
4825 iocbq_entry->sli4_xritag = NO_XRI;
4827 spin_lock_irq(&phba->hbalock);
4828 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4829 phba->total_iocbq_bufs++;
4830 spin_unlock_irq(&phba->hbalock);
4833 return 0;
4835 out_free_iocbq:
4836 lpfc_free_iocb_list(phba);
4838 return -ENOMEM;
4842 * lpfc_free_sgl_list - Free sgl list.
4843 * @phba: pointer to lpfc hba data structure.
4845 * This routine is invoked to free the driver's sgl list and memory.
4847 static void
4848 lpfc_free_sgl_list(struct lpfc_hba *phba)
4850 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4851 LIST_HEAD(sglq_list);
4853 spin_lock_irq(&phba->hbalock);
4854 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4855 spin_unlock_irq(&phba->hbalock);
4857 list_for_each_entry_safe(sglq_entry, sglq_next,
4858 &sglq_list, list) {
4859 list_del(&sglq_entry->list);
4860 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4861 kfree(sglq_entry);
4862 phba->sli4_hba.total_sglq_bufs--;
4864 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4868 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4869 * @phba: pointer to lpfc hba data structure.
4871 * This routine is invoked to allocate the driver's active sgl memory.
4872 * This array will hold the sglq_entry's for active IOs.
4874 static int
4875 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4877 int size;
4878 size = sizeof(struct lpfc_sglq *);
4879 size *= phba->sli4_hba.max_cfg_param.max_xri;
4881 phba->sli4_hba.lpfc_sglq_active_list =
4882 kzalloc(size, GFP_KERNEL);
4883 if (!phba->sli4_hba.lpfc_sglq_active_list)
4884 return -ENOMEM;
4885 return 0;
4889 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4890 * @phba: pointer to lpfc hba data structure.
4892 * This routine is invoked to walk through the array of active sglq entries
4893 * and free all of the resources.
4894 * This is just a place holder for now.
4896 static void
4897 lpfc_free_active_sgl(struct lpfc_hba *phba)
4899 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4903 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4904 * @phba: pointer to lpfc hba data structure.
4906 * This routine is invoked to allocate and initizlize the driver's sgl
4907 * list and set up the sgl xritag tag array accordingly.
4909 * Return codes
4910 * 0 - successful
4911 * other values - error
4913 static int
4914 lpfc_init_sgl_list(struct lpfc_hba *phba)
4916 struct lpfc_sglq *sglq_entry = NULL;
4917 int i;
4918 int els_xri_cnt;
4920 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4921 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4922 "2400 ELS XRI count %d.\n",
4923 els_xri_cnt);
4924 /* Initialize and populate the sglq list per host/VF. */
4925 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4926 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4928 /* Sanity check on XRI management */
4929 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4930 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4931 "2562 No room left for SCSI XRI allocation: "
4932 "max_xri=%d, els_xri=%d\n",
4933 phba->sli4_hba.max_cfg_param.max_xri,
4934 els_xri_cnt);
4935 return -ENOMEM;
4938 /* Allocate memory for the ELS XRI management array */
4939 phba->sli4_hba.lpfc_els_sgl_array =
4940 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4941 GFP_KERNEL);
4943 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4945 "2401 Failed to allocate memory for ELS "
4946 "XRI management array of size %d.\n",
4947 els_xri_cnt);
4948 return -ENOMEM;
4951 /* Keep the SCSI XRI into the XRI management array */
4952 phba->sli4_hba.scsi_xri_max =
4953 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4954 phba->sli4_hba.scsi_xri_cnt = 0;
4955 phba->sli4_hba.lpfc_scsi_psb_array =
4956 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4957 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4959 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4960 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4961 "2563 Failed to allocate memory for SCSI "
4962 "XRI management array of size %d.\n",
4963 phba->sli4_hba.scsi_xri_max);
4964 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4965 return -ENOMEM;
4968 for (i = 0; i < els_xri_cnt; i++) {
4969 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4970 if (sglq_entry == NULL) {
4971 printk(KERN_ERR "%s: only allocated %d sgls of "
4972 "expected %d count. Unloading driver.\n",
4973 __func__, i, els_xri_cnt);
4974 goto out_free_mem;
4977 sglq_entry->buff_type = GEN_BUFF_TYPE;
4978 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4979 if (sglq_entry->virt == NULL) {
4980 kfree(sglq_entry);
4981 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4982 "Unloading driver.\n", __func__);
4983 goto out_free_mem;
4985 sglq_entry->sgl = sglq_entry->virt;
4986 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4988 /* The list order is used by later block SGL registraton */
4989 spin_lock_irq(&phba->hbalock);
4990 sglq_entry->state = SGL_FREED;
4991 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4992 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4993 phba->sli4_hba.total_sglq_bufs++;
4994 spin_unlock_irq(&phba->hbalock);
4996 return 0;
4998 out_free_mem:
4999 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
5000 lpfc_free_sgl_list(phba);
5001 return -ENOMEM;
5005 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5006 * @phba: pointer to lpfc hba data structure.
5008 * This routine is invoked to post rpi header templates to the
5009 * port for those SLI4 ports that do not support extents. This routine
5010 * posts a PAGE_SIZE memory region to the port to hold up to
5011 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5012 * and should be called only when interrupts are disabled.
5014 * Return codes
5015 * 0 - successful
5016 * -ERROR - otherwise.
5019 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5021 int rc = 0;
5022 struct lpfc_rpi_hdr *rpi_hdr;
5024 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
5026 * If the SLI4 port supports extents, posting the rpi header isn't
5027 * required. Set the expected maximum count and let the actual value
5028 * get set when extents are fully allocated.
5030 if (!phba->sli4_hba.rpi_hdrs_in_use) {
5031 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5032 return rc;
5034 if (phba->sli4_hba.extents_in_use)
5035 return -EIO;
5037 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5038 if (!rpi_hdr) {
5039 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5040 "0391 Error during rpi post operation\n");
5041 lpfc_sli4_remove_rpis(phba);
5042 rc = -ENODEV;
5045 return rc;
5049 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5050 * @phba: pointer to lpfc hba data structure.
5052 * This routine is invoked to allocate a single 4KB memory region to
5053 * support rpis and stores them in the phba. This single region
5054 * provides support for up to 64 rpis. The region is used globally
5055 * by the device.
5057 * Returns:
5058 * A valid rpi hdr on success.
5059 * A NULL pointer on any failure.
5061 struct lpfc_rpi_hdr *
5062 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5064 uint16_t rpi_limit, curr_rpi_range;
5065 struct lpfc_dmabuf *dmabuf;
5066 struct lpfc_rpi_hdr *rpi_hdr;
5067 uint32_t rpi_count;
5070 * If the SLI4 port supports extents, posting the rpi header isn't
5071 * required. Set the expected maximum count and let the actual value
5072 * get set when extents are fully allocated.
5074 if (!phba->sli4_hba.rpi_hdrs_in_use)
5075 return NULL;
5076 if (phba->sli4_hba.extents_in_use)
5077 return NULL;
5079 /* The limit on the logical index is just the max_rpi count. */
5080 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
5081 phba->sli4_hba.max_cfg_param.max_rpi - 1;
5083 spin_lock_irq(&phba->hbalock);
5085 * Establish the starting RPI in this header block. The starting
5086 * rpi is normalized to a zero base because the physical rpi is
5087 * port based.
5089 curr_rpi_range = phba->sli4_hba.next_rpi -
5090 phba->sli4_hba.max_cfg_param.rpi_base;
5091 spin_unlock_irq(&phba->hbalock);
5094 * The port has a limited number of rpis. The increment here
5095 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5096 * and to allow the full max_rpi range per port.
5098 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
5099 rpi_count = rpi_limit - curr_rpi_range;
5100 else
5101 rpi_count = LPFC_RPI_HDR_COUNT;
5103 if (!rpi_count)
5104 return NULL;
5106 * First allocate the protocol header region for the port. The
5107 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5109 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5110 if (!dmabuf)
5111 return NULL;
5113 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5114 LPFC_HDR_TEMPLATE_SIZE,
5115 &dmabuf->phys,
5116 GFP_KERNEL);
5117 if (!dmabuf->virt) {
5118 rpi_hdr = NULL;
5119 goto err_free_dmabuf;
5122 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5123 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5124 rpi_hdr = NULL;
5125 goto err_free_coherent;
5128 /* Save the rpi header data for cleanup later. */
5129 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5130 if (!rpi_hdr)
5131 goto err_free_coherent;
5133 rpi_hdr->dmabuf = dmabuf;
5134 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5135 rpi_hdr->page_count = 1;
5136 spin_lock_irq(&phba->hbalock);
5138 /* The rpi_hdr stores the logical index only. */
5139 rpi_hdr->start_rpi = curr_rpi_range;
5140 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5143 * The next_rpi stores the next logical module-64 rpi value used
5144 * to post physical rpis in subsequent rpi postings.
5146 phba->sli4_hba.next_rpi += rpi_count;
5147 spin_unlock_irq(&phba->hbalock);
5148 return rpi_hdr;
5150 err_free_coherent:
5151 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5152 dmabuf->virt, dmabuf->phys);
5153 err_free_dmabuf:
5154 kfree(dmabuf);
5155 return NULL;
5159 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5160 * @phba: pointer to lpfc hba data structure.
5162 * This routine is invoked to remove all memory resources allocated
5163 * to support rpis for SLI4 ports not supporting extents. This routine
5164 * presumes the caller has released all rpis consumed by fabric or port
5165 * logins and is prepared to have the header pages removed.
5167 void
5168 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5170 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5172 if (!phba->sli4_hba.rpi_hdrs_in_use)
5173 goto exit;
5175 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5176 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5177 list_del(&rpi_hdr->list);
5178 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5179 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5180 kfree(rpi_hdr->dmabuf);
5181 kfree(rpi_hdr);
5183 exit:
5184 /* There are no rpis available to the port now. */
5185 phba->sli4_hba.next_rpi = 0;
5189 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5190 * @pdev: pointer to pci device data structure.
5192 * This routine is invoked to allocate the driver hba data structure for an
5193 * HBA device. If the allocation is successful, the phba reference to the
5194 * PCI device data structure is set.
5196 * Return codes
5197 * pointer to @phba - successful
5198 * NULL - error
5200 static struct lpfc_hba *
5201 lpfc_hba_alloc(struct pci_dev *pdev)
5203 struct lpfc_hba *phba;
5205 /* Allocate memory for HBA structure */
5206 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5207 if (!phba) {
5208 dev_err(&pdev->dev, "failed to allocate hba struct\n");
5209 return NULL;
5212 /* Set reference to PCI device in HBA structure */
5213 phba->pcidev = pdev;
5215 /* Assign an unused board number */
5216 phba->brd_no = lpfc_get_instance();
5217 if (phba->brd_no < 0) {
5218 kfree(phba);
5219 return NULL;
5222 spin_lock_init(&phba->ct_ev_lock);
5223 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5225 return phba;
5229 * lpfc_hba_free - Free driver hba data structure with a device.
5230 * @phba: pointer to lpfc hba data structure.
5232 * This routine is invoked to free the driver hba data structure with an
5233 * HBA device.
5235 static void
5236 lpfc_hba_free(struct lpfc_hba *phba)
5238 /* Release the driver assigned board number */
5239 idr_remove(&lpfc_hba_index, phba->brd_no);
5241 kfree(phba);
5242 return;
5246 * lpfc_create_shost - Create hba physical port with associated scsi host.
5247 * @phba: pointer to lpfc hba data structure.
5249 * This routine is invoked to create HBA physical port and associate a SCSI
5250 * host with it.
5252 * Return codes
5253 * 0 - successful
5254 * other values - error
5256 static int
5257 lpfc_create_shost(struct lpfc_hba *phba)
5259 struct lpfc_vport *vport;
5260 struct Scsi_Host *shost;
5262 /* Initialize HBA FC structure */
5263 phba->fc_edtov = FF_DEF_EDTOV;
5264 phba->fc_ratov = FF_DEF_RATOV;
5265 phba->fc_altov = FF_DEF_ALTOV;
5266 phba->fc_arbtov = FF_DEF_ARBTOV;
5268 atomic_set(&phba->sdev_cnt, 0);
5269 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
5270 if (!vport)
5271 return -ENODEV;
5273 shost = lpfc_shost_from_vport(vport);
5274 phba->pport = vport;
5275 lpfc_debugfs_initialize(vport);
5276 /* Put reference to SCSI host to driver's device private data */
5277 pci_set_drvdata(phba->pcidev, shost);
5279 return 0;
5283 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5284 * @phba: pointer to lpfc hba data structure.
5286 * This routine is invoked to destroy HBA physical port and the associated
5287 * SCSI host.
5289 static void
5290 lpfc_destroy_shost(struct lpfc_hba *phba)
5292 struct lpfc_vport *vport = phba->pport;
5294 /* Destroy physical port that associated with the SCSI host */
5295 destroy_port(vport);
5297 return;
5301 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5302 * @phba: pointer to lpfc hba data structure.
5303 * @shost: the shost to be used to detect Block guard settings.
5305 * This routine sets up the local Block guard protocol settings for @shost.
5306 * This routine also allocates memory for debugging bg buffers.
5308 static void
5309 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5311 int pagecnt = 10;
5312 if (lpfc_prot_mask && lpfc_prot_guard) {
5313 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5314 "1478 Registering BlockGuard with the "
5315 "SCSI layer\n");
5316 scsi_host_set_prot(shost, lpfc_prot_mask);
5317 scsi_host_set_guard(shost, lpfc_prot_guard);
5319 if (!_dump_buf_data) {
5320 while (pagecnt) {
5321 spin_lock_init(&_dump_buf_lock);
5322 _dump_buf_data =
5323 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5324 if (_dump_buf_data) {
5325 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5326 "9043 BLKGRD: allocated %d pages for "
5327 "_dump_buf_data at 0x%p\n",
5328 (1 << pagecnt), _dump_buf_data);
5329 _dump_buf_data_order = pagecnt;
5330 memset(_dump_buf_data, 0,
5331 ((1 << PAGE_SHIFT) << pagecnt));
5332 break;
5333 } else
5334 --pagecnt;
5336 if (!_dump_buf_data_order)
5337 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5338 "9044 BLKGRD: ERROR unable to allocate "
5339 "memory for hexdump\n");
5340 } else
5341 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5342 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
5343 "\n", _dump_buf_data);
5344 if (!_dump_buf_dif) {
5345 while (pagecnt) {
5346 _dump_buf_dif =
5347 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5348 if (_dump_buf_dif) {
5349 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5350 "9046 BLKGRD: allocated %d pages for "
5351 "_dump_buf_dif at 0x%p\n",
5352 (1 << pagecnt), _dump_buf_dif);
5353 _dump_buf_dif_order = pagecnt;
5354 memset(_dump_buf_dif, 0,
5355 ((1 << PAGE_SHIFT) << pagecnt));
5356 break;
5357 } else
5358 --pagecnt;
5360 if (!_dump_buf_dif_order)
5361 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5362 "9047 BLKGRD: ERROR unable to allocate "
5363 "memory for hexdump\n");
5364 } else
5365 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5366 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
5367 _dump_buf_dif);
5371 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5372 * @phba: pointer to lpfc hba data structure.
5374 * This routine is invoked to perform all the necessary post initialization
5375 * setup for the device.
5377 static void
5378 lpfc_post_init_setup(struct lpfc_hba *phba)
5380 struct Scsi_Host *shost;
5381 struct lpfc_adapter_event_header adapter_event;
5383 /* Get the default values for Model Name and Description */
5384 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5387 * hba setup may have changed the hba_queue_depth so we need to
5388 * adjust the value of can_queue.
5390 shost = pci_get_drvdata(phba->pcidev);
5391 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5392 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5393 lpfc_setup_bg(phba, shost);
5395 lpfc_host_attrib_init(shost);
5397 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5398 spin_lock_irq(shost->host_lock);
5399 lpfc_poll_start_timer(phba);
5400 spin_unlock_irq(shost->host_lock);
5403 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5404 "0428 Perform SCSI scan\n");
5405 /* Send board arrival event to upper layer */
5406 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5407 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5408 fc_host_post_vendor_event(shost, fc_get_event_number(),
5409 sizeof(adapter_event),
5410 (char *) &adapter_event,
5411 LPFC_NL_VENDOR_ID);
5412 return;
5416 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5417 * @phba: pointer to lpfc hba data structure.
5419 * This routine is invoked to set up the PCI device memory space for device
5420 * with SLI-3 interface spec.
5422 * Return codes
5423 * 0 - successful
5424 * other values - error
5426 static int
5427 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5429 struct pci_dev *pdev;
5430 unsigned long bar0map_len, bar2map_len;
5431 int i, hbq_count;
5432 void *ptr;
5433 int error = -ENODEV;
5435 /* Obtain PCI device reference */
5436 if (!phba->pcidev)
5437 return error;
5438 else
5439 pdev = phba->pcidev;
5441 /* Set the device DMA mask size */
5442 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5443 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5444 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5445 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
5446 return error;
5450 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5451 * required by each mapping.
5453 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5454 bar0map_len = pci_resource_len(pdev, 0);
5456 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5457 bar2map_len = pci_resource_len(pdev, 2);
5459 /* Map HBA SLIM to a kernel virtual address. */
5460 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5461 if (!phba->slim_memmap_p) {
5462 dev_printk(KERN_ERR, &pdev->dev,
5463 "ioremap failed for SLIM memory.\n");
5464 goto out;
5467 /* Map HBA Control Registers to a kernel virtual address. */
5468 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5469 if (!phba->ctrl_regs_memmap_p) {
5470 dev_printk(KERN_ERR, &pdev->dev,
5471 "ioremap failed for HBA control registers.\n");
5472 goto out_iounmap_slim;
5475 /* Allocate memory for SLI-2 structures */
5476 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5477 SLI2_SLIM_SIZE,
5478 &phba->slim2p.phys,
5479 GFP_KERNEL);
5480 if (!phba->slim2p.virt)
5481 goto out_iounmap;
5483 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5484 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
5485 phba->mbox_ext = (phba->slim2p.virt +
5486 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
5487 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5488 phba->IOCBs = (phba->slim2p.virt +
5489 offsetof(struct lpfc_sli2_slim, IOCBs));
5491 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5492 lpfc_sli_hbq_size(),
5493 &phba->hbqslimp.phys,
5494 GFP_KERNEL);
5495 if (!phba->hbqslimp.virt)
5496 goto out_free_slim;
5498 hbq_count = lpfc_sli_hbq_count();
5499 ptr = phba->hbqslimp.virt;
5500 for (i = 0; i < hbq_count; ++i) {
5501 phba->hbqs[i].hbq_virt = ptr;
5502 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5503 ptr += (lpfc_hbq_defs[i]->entry_count *
5504 sizeof(struct lpfc_hbq_entry));
5506 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5507 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5509 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5511 INIT_LIST_HEAD(&phba->rb_pend_list);
5513 phba->MBslimaddr = phba->slim_memmap_p;
5514 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5515 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5516 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5517 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
5519 return 0;
5521 out_free_slim:
5522 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5523 phba->slim2p.virt, phba->slim2p.phys);
5524 out_iounmap:
5525 iounmap(phba->ctrl_regs_memmap_p);
5526 out_iounmap_slim:
5527 iounmap(phba->slim_memmap_p);
5528 out:
5529 return error;
5533 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5534 * @phba: pointer to lpfc hba data structure.
5536 * This routine is invoked to unset the PCI device memory space for device
5537 * with SLI-3 interface spec.
5539 static void
5540 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5542 struct pci_dev *pdev;
5544 /* Obtain PCI device reference */
5545 if (!phba->pcidev)
5546 return;
5547 else
5548 pdev = phba->pcidev;
5550 /* Free coherent DMA memory allocated */
5551 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5552 phba->hbqslimp.virt, phba->hbqslimp.phys);
5553 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5554 phba->slim2p.virt, phba->slim2p.phys);
5556 /* I/O memory unmap */
5557 iounmap(phba->ctrl_regs_memmap_p);
5558 iounmap(phba->slim_memmap_p);
5560 return;
5564 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5565 * @phba: pointer to lpfc hba data structure.
5567 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5568 * done and check status.
5570 * Return 0 if successful, otherwise -ENODEV.
5573 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5575 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5576 struct lpfc_register reg_data;
5577 int i, port_error = 0;
5578 uint32_t if_type;
5580 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
5581 memset(&reg_data, 0, sizeof(reg_data));
5582 if (!phba->sli4_hba.PSMPHRregaddr)
5583 return -ENODEV;
5585 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5586 for (i = 0; i < 3000; i++) {
5587 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
5588 &portsmphr_reg.word0) ||
5589 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
5590 /* Port has a fatal POST error, break out */
5591 port_error = -ENODEV;
5592 break;
5594 if (LPFC_POST_STAGE_PORT_READY ==
5595 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
5596 break;
5597 msleep(10);
5601 * If there was a port error during POST, then don't proceed with
5602 * other register reads as the data may not be valid. Just exit.
5604 if (port_error) {
5605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5606 "1408 Port Failed POST - portsmphr=0x%x, "
5607 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5608 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5609 portsmphr_reg.word0,
5610 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5611 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5612 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5613 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5614 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5615 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5616 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5617 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5618 } else {
5619 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5620 "2534 Device Info: SLIFamily=0x%x, "
5621 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5622 "SLIHint_2=0x%x, FT=0x%x\n",
5623 bf_get(lpfc_sli_intf_sli_family,
5624 &phba->sli4_hba.sli_intf),
5625 bf_get(lpfc_sli_intf_slirev,
5626 &phba->sli4_hba.sli_intf),
5627 bf_get(lpfc_sli_intf_if_type,
5628 &phba->sli4_hba.sli_intf),
5629 bf_get(lpfc_sli_intf_sli_hint1,
5630 &phba->sli4_hba.sli_intf),
5631 bf_get(lpfc_sli_intf_sli_hint2,
5632 &phba->sli4_hba.sli_intf),
5633 bf_get(lpfc_sli_intf_func_type,
5634 &phba->sli4_hba.sli_intf));
5636 * Check for other Port errors during the initialization
5637 * process. Fail the load if the port did not come up
5638 * correctly.
5640 if_type = bf_get(lpfc_sli_intf_if_type,
5641 &phba->sli4_hba.sli_intf);
5642 switch (if_type) {
5643 case LPFC_SLI_INTF_IF_TYPE_0:
5644 phba->sli4_hba.ue_mask_lo =
5645 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5646 phba->sli4_hba.ue_mask_hi =
5647 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5648 uerrlo_reg.word0 =
5649 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5650 uerrhi_reg.word0 =
5651 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
5652 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5653 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5655 "1422 Unrecoverable Error "
5656 "Detected during POST "
5657 "uerr_lo_reg=0x%x, "
5658 "uerr_hi_reg=0x%x, "
5659 "ue_mask_lo_reg=0x%x, "
5660 "ue_mask_hi_reg=0x%x\n",
5661 uerrlo_reg.word0,
5662 uerrhi_reg.word0,
5663 phba->sli4_hba.ue_mask_lo,
5664 phba->sli4_hba.ue_mask_hi);
5665 port_error = -ENODEV;
5667 break;
5668 case LPFC_SLI_INTF_IF_TYPE_2:
5669 /* Final checks. The port status should be clean. */
5670 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
5671 &reg_data.word0) ||
5672 (bf_get(lpfc_sliport_status_err, &reg_data) &&
5673 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
5674 phba->work_status[0] =
5675 readl(phba->sli4_hba.u.if_type2.
5676 ERR1regaddr);
5677 phba->work_status[1] =
5678 readl(phba->sli4_hba.u.if_type2.
5679 ERR2regaddr);
5680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5681 "2888 Port Error Detected "
5682 "during POST: "
5683 "port status reg 0x%x, "
5684 "port_smphr reg 0x%x, "
5685 "error 1=0x%x, error 2=0x%x\n",
5686 reg_data.word0,
5687 portsmphr_reg.word0,
5688 phba->work_status[0],
5689 phba->work_status[1]);
5690 port_error = -ENODEV;
5692 break;
5693 case LPFC_SLI_INTF_IF_TYPE_1:
5694 default:
5695 break;
5698 return port_error;
5702 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5703 * @phba: pointer to lpfc hba data structure.
5704 * @if_type: The SLI4 interface type getting configured.
5706 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5707 * memory map.
5709 static void
5710 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
5712 switch (if_type) {
5713 case LPFC_SLI_INTF_IF_TYPE_0:
5714 phba->sli4_hba.u.if_type0.UERRLOregaddr =
5715 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
5716 phba->sli4_hba.u.if_type0.UERRHIregaddr =
5717 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
5718 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
5719 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
5720 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
5721 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
5722 phba->sli4_hba.SLIINTFregaddr =
5723 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5724 break;
5725 case LPFC_SLI_INTF_IF_TYPE_2:
5726 phba->sli4_hba.u.if_type2.ERR1regaddr =
5727 phba->sli4_hba.conf_regs_memmap_p +
5728 LPFC_CTL_PORT_ER1_OFFSET;
5729 phba->sli4_hba.u.if_type2.ERR2regaddr =
5730 phba->sli4_hba.conf_regs_memmap_p +
5731 LPFC_CTL_PORT_ER2_OFFSET;
5732 phba->sli4_hba.u.if_type2.CTRLregaddr =
5733 phba->sli4_hba.conf_regs_memmap_p +
5734 LPFC_CTL_PORT_CTL_OFFSET;
5735 phba->sli4_hba.u.if_type2.STATUSregaddr =
5736 phba->sli4_hba.conf_regs_memmap_p +
5737 LPFC_CTL_PORT_STA_OFFSET;
5738 phba->sli4_hba.SLIINTFregaddr =
5739 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5740 phba->sli4_hba.PSMPHRregaddr =
5741 phba->sli4_hba.conf_regs_memmap_p +
5742 LPFC_CTL_PORT_SEM_OFFSET;
5743 phba->sli4_hba.RQDBregaddr =
5744 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
5745 phba->sli4_hba.WQDBregaddr =
5746 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
5747 phba->sli4_hba.EQCQDBregaddr =
5748 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
5749 phba->sli4_hba.MQDBregaddr =
5750 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
5751 phba->sli4_hba.BMBXregaddr =
5752 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
5753 break;
5754 case LPFC_SLI_INTF_IF_TYPE_1:
5755 default:
5756 dev_printk(KERN_ERR, &phba->pcidev->dev,
5757 "FATAL - unsupported SLI4 interface type - %d\n",
5758 if_type);
5759 break;
5764 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5765 * @phba: pointer to lpfc hba data structure.
5767 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5768 * memory map.
5770 static void
5771 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5773 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5774 LPFC_SLIPORT_IF0_SMPHR;
5775 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5776 LPFC_HST_ISR0;
5777 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5778 LPFC_HST_IMR0;
5779 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5780 LPFC_HST_ISCR0;
5784 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5785 * @phba: pointer to lpfc hba data structure.
5786 * @vf: virtual function number
5788 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5789 * based on the given viftual function number, @vf.
5791 * Return 0 if successful, otherwise -ENODEV.
5793 static int
5794 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5796 if (vf > LPFC_VIR_FUNC_MAX)
5797 return -ENODEV;
5799 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5800 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5801 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5802 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5803 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5804 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5805 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5806 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5807 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5808 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5809 return 0;
5813 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5814 * @phba: pointer to lpfc hba data structure.
5816 * This routine is invoked to create the bootstrap mailbox
5817 * region consistent with the SLI-4 interface spec. This
5818 * routine allocates all memory necessary to communicate
5819 * mailbox commands to the port and sets up all alignment
5820 * needs. No locks are expected to be held when calling
5821 * this routine.
5823 * Return codes
5824 * 0 - successful
5825 * -ENOMEM - could not allocated memory.
5827 static int
5828 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5830 uint32_t bmbx_size;
5831 struct lpfc_dmabuf *dmabuf;
5832 struct dma_address *dma_address;
5833 uint32_t pa_addr;
5834 uint64_t phys_addr;
5836 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5837 if (!dmabuf)
5838 return -ENOMEM;
5841 * The bootstrap mailbox region is comprised of 2 parts
5842 * plus an alignment restriction of 16 bytes.
5844 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5845 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5846 bmbx_size,
5847 &dmabuf->phys,
5848 GFP_KERNEL);
5849 if (!dmabuf->virt) {
5850 kfree(dmabuf);
5851 return -ENOMEM;
5853 memset(dmabuf->virt, 0, bmbx_size);
5856 * Initialize the bootstrap mailbox pointers now so that the register
5857 * operations are simple later. The mailbox dma address is required
5858 * to be 16-byte aligned. Also align the virtual memory as each
5859 * maibox is copied into the bmbx mailbox region before issuing the
5860 * command to the port.
5862 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5863 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5865 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5866 LPFC_ALIGN_16_BYTE);
5867 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5868 LPFC_ALIGN_16_BYTE);
5871 * Set the high and low physical addresses now. The SLI4 alignment
5872 * requirement is 16 bytes and the mailbox is posted to the port
5873 * as two 30-bit addresses. The other data is a bit marking whether
5874 * the 30-bit address is the high or low address.
5875 * Upcast bmbx aphys to 64bits so shift instruction compiles
5876 * clean on 32 bit machines.
5878 dma_address = &phba->sli4_hba.bmbx.dma_address;
5879 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5880 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5881 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5882 LPFC_BMBX_BIT1_ADDR_HI);
5884 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5885 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5886 LPFC_BMBX_BIT1_ADDR_LO);
5887 return 0;
5891 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5892 * @phba: pointer to lpfc hba data structure.
5894 * This routine is invoked to teardown the bootstrap mailbox
5895 * region and release all host resources. This routine requires
5896 * the caller to ensure all mailbox commands recovered, no
5897 * additional mailbox comands are sent, and interrupts are disabled
5898 * before calling this routine.
5901 static void
5902 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5904 dma_free_coherent(&phba->pcidev->dev,
5905 phba->sli4_hba.bmbx.bmbx_size,
5906 phba->sli4_hba.bmbx.dmabuf->virt,
5907 phba->sli4_hba.bmbx.dmabuf->phys);
5909 kfree(phba->sli4_hba.bmbx.dmabuf);
5910 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5914 * lpfc_sli4_read_config - Get the config parameters.
5915 * @phba: pointer to lpfc hba data structure.
5917 * This routine is invoked to read the configuration parameters from the HBA.
5918 * The configuration parameters are used to set the base and maximum values
5919 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5920 * allocation for the port.
5922 * Return codes
5923 * 0 - successful
5924 * -ENOMEM - No available memory
5925 * -EIO - The mailbox failed to complete successfully.
5927 static int
5928 lpfc_sli4_read_config(struct lpfc_hba *phba)
5930 LPFC_MBOXQ_t *pmb;
5931 struct lpfc_mbx_read_config *rd_config;
5932 union lpfc_sli4_cfg_shdr *shdr;
5933 uint32_t shdr_status, shdr_add_status;
5934 struct lpfc_mbx_get_func_cfg *get_func_cfg;
5935 struct lpfc_rsrc_desc_fcfcoe *desc;
5936 uint32_t desc_count;
5937 int length, i, rc = 0;
5939 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5940 if (!pmb) {
5941 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5942 "2011 Unable to allocate memory for issuing "
5943 "SLI_CONFIG_SPECIAL mailbox command\n");
5944 return -ENOMEM;
5947 lpfc_read_config(phba, pmb);
5949 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5950 if (rc != MBX_SUCCESS) {
5951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5952 "2012 Mailbox failed , mbxCmd x%x "
5953 "READ_CONFIG, mbxStatus x%x\n",
5954 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5955 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5956 rc = -EIO;
5957 } else {
5958 rd_config = &pmb->u.mqe.un.rd_config;
5959 phba->sli4_hba.extents_in_use =
5960 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
5961 phba->sli4_hba.max_cfg_param.max_xri =
5962 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5963 phba->sli4_hba.max_cfg_param.xri_base =
5964 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5965 phba->sli4_hba.max_cfg_param.max_vpi =
5966 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5967 phba->sli4_hba.max_cfg_param.vpi_base =
5968 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5969 phba->sli4_hba.max_cfg_param.max_rpi =
5970 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5971 phba->sli4_hba.max_cfg_param.rpi_base =
5972 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5973 phba->sli4_hba.max_cfg_param.max_vfi =
5974 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5975 phba->sli4_hba.max_cfg_param.vfi_base =
5976 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5977 phba->sli4_hba.max_cfg_param.max_fcfi =
5978 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5979 phba->sli4_hba.max_cfg_param.max_eq =
5980 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5981 phba->sli4_hba.max_cfg_param.max_rq =
5982 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5983 phba->sli4_hba.max_cfg_param.max_wq =
5984 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5985 phba->sli4_hba.max_cfg_param.max_cq =
5986 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5987 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5988 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5989 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5990 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5991 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
5992 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5993 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
5994 phba->max_vports = phba->max_vpi;
5995 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5996 "2003 cfg params Extents? %d "
5997 "XRI(B:%d M:%d), "
5998 "VPI(B:%d M:%d) "
5999 "VFI(B:%d M:%d) "
6000 "RPI(B:%d M:%d) "
6001 "FCFI(Count:%d)\n",
6002 phba->sli4_hba.extents_in_use,
6003 phba->sli4_hba.max_cfg_param.xri_base,
6004 phba->sli4_hba.max_cfg_param.max_xri,
6005 phba->sli4_hba.max_cfg_param.vpi_base,
6006 phba->sli4_hba.max_cfg_param.max_vpi,
6007 phba->sli4_hba.max_cfg_param.vfi_base,
6008 phba->sli4_hba.max_cfg_param.max_vfi,
6009 phba->sli4_hba.max_cfg_param.rpi_base,
6010 phba->sli4_hba.max_cfg_param.max_rpi,
6011 phba->sli4_hba.max_cfg_param.max_fcfi);
6014 if (rc)
6015 goto read_cfg_out;
6017 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
6018 if (phba->cfg_hba_queue_depth >
6019 (phba->sli4_hba.max_cfg_param.max_xri -
6020 lpfc_sli4_get_els_iocb_cnt(phba)))
6021 phba->cfg_hba_queue_depth =
6022 phba->sli4_hba.max_cfg_param.max_xri -
6023 lpfc_sli4_get_els_iocb_cnt(phba);
6025 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6026 LPFC_SLI_INTF_IF_TYPE_2)
6027 goto read_cfg_out;
6029 /* get the pf# and vf# for SLI4 if_type 2 port */
6030 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6031 sizeof(struct lpfc_sli4_cfg_mhdr));
6032 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6033 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6034 length, LPFC_SLI4_MBX_EMBED);
6036 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6037 shdr = (union lpfc_sli4_cfg_shdr *)
6038 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6039 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6040 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6041 if (rc || shdr_status || shdr_add_status) {
6042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6043 "3026 Mailbox failed , mbxCmd x%x "
6044 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6045 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6046 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6047 rc = -EIO;
6048 goto read_cfg_out;
6051 /* search for fc_fcoe resrouce descriptor */
6052 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6053 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6055 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
6056 desc = (struct lpfc_rsrc_desc_fcfcoe *)
6057 &get_func_cfg->func_cfg.desc[i];
6058 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
6059 bf_get(lpfc_rsrc_desc_pcie_type, desc)) {
6060 phba->sli4_hba.iov.pf_number =
6061 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6062 phba->sli4_hba.iov.vf_number =
6063 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6064 break;
6068 if (i < LPFC_RSRC_DESC_MAX_NUM)
6069 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6070 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6071 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6072 phba->sli4_hba.iov.vf_number);
6073 else {
6074 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6075 "3028 GET_FUNCTION_CONFIG: failed to find "
6076 "Resrouce Descriptor:x%x\n",
6077 LPFC_RSRC_DESC_TYPE_FCFCOE);
6078 rc = -EIO;
6081 read_cfg_out:
6082 mempool_free(pmb, phba->mbox_mem_pool);
6083 return rc;
6087 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
6088 * @phba: pointer to lpfc hba data structure.
6090 * This routine is invoked to setup the port-side endian order when
6091 * the port if_type is 0. This routine has no function for other
6092 * if_types.
6094 * Return codes
6095 * 0 - successful
6096 * -ENOMEM - No available memory
6097 * -EIO - The mailbox failed to complete successfully.
6099 static int
6100 lpfc_setup_endian_order(struct lpfc_hba *phba)
6102 LPFC_MBOXQ_t *mboxq;
6103 uint32_t if_type, rc = 0;
6104 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6105 HOST_ENDIAN_HIGH_WORD1};
6107 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6108 switch (if_type) {
6109 case LPFC_SLI_INTF_IF_TYPE_0:
6110 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6111 GFP_KERNEL);
6112 if (!mboxq) {
6113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6114 "0492 Unable to allocate memory for "
6115 "issuing SLI_CONFIG_SPECIAL mailbox "
6116 "command\n");
6117 return -ENOMEM;
6121 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6122 * two words to contain special data values and no other data.
6124 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6125 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6126 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6127 if (rc != MBX_SUCCESS) {
6128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6129 "0493 SLI_CONFIG_SPECIAL mailbox "
6130 "failed with status x%x\n",
6131 rc);
6132 rc = -EIO;
6134 mempool_free(mboxq, phba->mbox_mem_pool);
6135 break;
6136 case LPFC_SLI_INTF_IF_TYPE_2:
6137 case LPFC_SLI_INTF_IF_TYPE_1:
6138 default:
6139 break;
6141 return rc;
6145 * lpfc_sli4_queue_create - Create all the SLI4 queues
6146 * @phba: pointer to lpfc hba data structure.
6148 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6149 * operation. For each SLI4 queue type, the parameters such as queue entry
6150 * count (queue depth) shall be taken from the module parameter. For now,
6151 * we just use some constant number as place holder.
6153 * Return codes
6154 * 0 - successful
6155 * -ENOMEM - No available memory
6156 * -EIO - The mailbox failed to complete successfully.
6158 static int
6159 lpfc_sli4_queue_create(struct lpfc_hba *phba)
6161 struct lpfc_queue *qdesc;
6162 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6163 int cfg_fcp_wq_count;
6164 int cfg_fcp_eq_count;
6167 * Sanity check for confiugred queue parameters against the run-time
6168 * device parameters
6171 /* Sanity check on FCP fast-path WQ parameters */
6172 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
6173 if (cfg_fcp_wq_count >
6174 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
6175 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
6176 LPFC_SP_WQN_DEF;
6177 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
6178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6179 "2581 Not enough WQs (%d) from "
6180 "the pci function for supporting "
6181 "FCP WQs (%d)\n",
6182 phba->sli4_hba.max_cfg_param.max_wq,
6183 phba->cfg_fcp_wq_count);
6184 goto out_error;
6186 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6187 "2582 Not enough WQs (%d) from the pci "
6188 "function for supporting the requested "
6189 "FCP WQs (%d), the actual FCP WQs can "
6190 "be supported: %d\n",
6191 phba->sli4_hba.max_cfg_param.max_wq,
6192 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
6194 /* The actual number of FCP work queues adopted */
6195 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
6197 /* Sanity check on FCP fast-path EQ parameters */
6198 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
6199 if (cfg_fcp_eq_count >
6200 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
6201 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
6202 LPFC_SP_EQN_DEF;
6203 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
6204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6205 "2574 Not enough EQs (%d) from the "
6206 "pci function for supporting FCP "
6207 "EQs (%d)\n",
6208 phba->sli4_hba.max_cfg_param.max_eq,
6209 phba->cfg_fcp_eq_count);
6210 goto out_error;
6212 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6213 "2575 Not enough EQs (%d) from the pci "
6214 "function for supporting the requested "
6215 "FCP EQs (%d), the actual FCP EQs can "
6216 "be supported: %d\n",
6217 phba->sli4_hba.max_cfg_param.max_eq,
6218 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
6220 /* It does not make sense to have more EQs than WQs */
6221 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
6222 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6223 "2593 The FCP EQ count(%d) cannot be greater "
6224 "than the FCP WQ count(%d), limiting the "
6225 "FCP EQ count to %d\n", cfg_fcp_eq_count,
6226 phba->cfg_fcp_wq_count,
6227 phba->cfg_fcp_wq_count);
6228 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
6230 /* The actual number of FCP event queues adopted */
6231 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
6232 /* The overall number of event queues used */
6233 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
6236 * Create Event Queues (EQs)
6239 /* Get EQ depth from module parameter, fake the default for now */
6240 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6241 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6243 /* Create slow path event queue */
6244 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6245 phba->sli4_hba.eq_ecount);
6246 if (!qdesc) {
6247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6248 "0496 Failed allocate slow-path EQ\n");
6249 goto out_error;
6251 phba->sli4_hba.sp_eq = qdesc;
6253 /* Create fast-path FCP Event Queue(s) */
6254 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
6255 phba->cfg_fcp_eq_count), GFP_KERNEL);
6256 if (!phba->sli4_hba.fp_eq) {
6257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6258 "2576 Failed allocate memory for fast-path "
6259 "EQ record array\n");
6260 goto out_free_sp_eq;
6262 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6263 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6264 phba->sli4_hba.eq_ecount);
6265 if (!qdesc) {
6266 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6267 "0497 Failed allocate fast-path EQ\n");
6268 goto out_free_fp_eq;
6270 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
6274 * Create Complete Queues (CQs)
6277 /* Get CQ depth from module parameter, fake the default for now */
6278 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6279 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6281 /* Create slow-path Mailbox Command Complete Queue */
6282 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6283 phba->sli4_hba.cq_ecount);
6284 if (!qdesc) {
6285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6286 "0500 Failed allocate slow-path mailbox CQ\n");
6287 goto out_free_fp_eq;
6289 phba->sli4_hba.mbx_cq = qdesc;
6291 /* Create slow-path ELS Complete Queue */
6292 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6293 phba->sli4_hba.cq_ecount);
6294 if (!qdesc) {
6295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6296 "0501 Failed allocate slow-path ELS CQ\n");
6297 goto out_free_mbx_cq;
6299 phba->sli4_hba.els_cq = qdesc;
6302 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
6303 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6304 phba->cfg_fcp_eq_count), GFP_KERNEL);
6305 if (!phba->sli4_hba.fcp_cq) {
6306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6307 "2577 Failed allocate memory for fast-path "
6308 "CQ record array\n");
6309 goto out_free_els_cq;
6311 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6312 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6313 phba->sli4_hba.cq_ecount);
6314 if (!qdesc) {
6315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6316 "0499 Failed allocate fast-path FCP "
6317 "CQ (%d)\n", fcp_cqidx);
6318 goto out_free_fcp_cq;
6320 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
6323 /* Create Mailbox Command Queue */
6324 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6325 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6327 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6328 phba->sli4_hba.mq_ecount);
6329 if (!qdesc) {
6330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6331 "0505 Failed allocate slow-path MQ\n");
6332 goto out_free_fcp_cq;
6334 phba->sli4_hba.mbx_wq = qdesc;
6337 * Create all the Work Queues (WQs)
6339 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6340 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6342 /* Create slow-path ELS Work Queue */
6343 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6344 phba->sli4_hba.wq_ecount);
6345 if (!qdesc) {
6346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6347 "0504 Failed allocate slow-path ELS WQ\n");
6348 goto out_free_mbx_wq;
6350 phba->sli4_hba.els_wq = qdesc;
6352 /* Create fast-path FCP Work Queue(s) */
6353 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6354 phba->cfg_fcp_wq_count), GFP_KERNEL);
6355 if (!phba->sli4_hba.fcp_wq) {
6356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6357 "2578 Failed allocate memory for fast-path "
6358 "WQ record array\n");
6359 goto out_free_els_wq;
6361 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6362 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6363 phba->sli4_hba.wq_ecount);
6364 if (!qdesc) {
6365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6366 "0503 Failed allocate fast-path FCP "
6367 "WQ (%d)\n", fcp_wqidx);
6368 goto out_free_fcp_wq;
6370 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6374 * Create Receive Queue (RQ)
6376 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6377 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6379 /* Create Receive Queue for header */
6380 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6381 phba->sli4_hba.rq_ecount);
6382 if (!qdesc) {
6383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6384 "0506 Failed allocate receive HRQ\n");
6385 goto out_free_fcp_wq;
6387 phba->sli4_hba.hdr_rq = qdesc;
6389 /* Create Receive Queue for data */
6390 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6391 phba->sli4_hba.rq_ecount);
6392 if (!qdesc) {
6393 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6394 "0507 Failed allocate receive DRQ\n");
6395 goto out_free_hdr_rq;
6397 phba->sli4_hba.dat_rq = qdesc;
6399 return 0;
6401 out_free_hdr_rq:
6402 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6403 phba->sli4_hba.hdr_rq = NULL;
6404 out_free_fcp_wq:
6405 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6406 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6407 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6409 kfree(phba->sli4_hba.fcp_wq);
6410 out_free_els_wq:
6411 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6412 phba->sli4_hba.els_wq = NULL;
6413 out_free_mbx_wq:
6414 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6415 phba->sli4_hba.mbx_wq = NULL;
6416 out_free_fcp_cq:
6417 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6418 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6419 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6421 kfree(phba->sli4_hba.fcp_cq);
6422 out_free_els_cq:
6423 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6424 phba->sli4_hba.els_cq = NULL;
6425 out_free_mbx_cq:
6426 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6427 phba->sli4_hba.mbx_cq = NULL;
6428 out_free_fp_eq:
6429 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6430 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6431 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6433 kfree(phba->sli4_hba.fp_eq);
6434 out_free_sp_eq:
6435 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6436 phba->sli4_hba.sp_eq = NULL;
6437 out_error:
6438 return -ENOMEM;
6442 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6443 * @phba: pointer to lpfc hba data structure.
6445 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6446 * operation.
6448 * Return codes
6449 * 0 - successful
6450 * -ENOMEM - No available memory
6451 * -EIO - The mailbox failed to complete successfully.
6453 static void
6454 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6456 int fcp_qidx;
6458 /* Release mailbox command work queue */
6459 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6460 phba->sli4_hba.mbx_wq = NULL;
6462 /* Release ELS work queue */
6463 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6464 phba->sli4_hba.els_wq = NULL;
6466 /* Release FCP work queue */
6467 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6468 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
6469 kfree(phba->sli4_hba.fcp_wq);
6470 phba->sli4_hba.fcp_wq = NULL;
6472 /* Release unsolicited receive queue */
6473 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6474 phba->sli4_hba.hdr_rq = NULL;
6475 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6476 phba->sli4_hba.dat_rq = NULL;
6478 /* Release ELS complete queue */
6479 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6480 phba->sli4_hba.els_cq = NULL;
6482 /* Release mailbox command complete queue */
6483 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6484 phba->sli4_hba.mbx_cq = NULL;
6486 /* Release FCP response complete queue */
6487 fcp_qidx = 0;
6489 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
6490 while (++fcp_qidx < phba->cfg_fcp_eq_count);
6491 kfree(phba->sli4_hba.fcp_cq);
6492 phba->sli4_hba.fcp_cq = NULL;
6494 /* Release fast-path event queue */
6495 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6496 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
6497 kfree(phba->sli4_hba.fp_eq);
6498 phba->sli4_hba.fp_eq = NULL;
6500 /* Release slow-path event queue */
6501 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6502 phba->sli4_hba.sp_eq = NULL;
6504 return;
6508 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6509 * @phba: pointer to lpfc hba data structure.
6511 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6512 * operation.
6514 * Return codes
6515 * 0 - successful
6516 * -ENOMEM - No available memory
6517 * -EIO - The mailbox failed to complete successfully.
6520 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6522 int rc = -ENOMEM;
6523 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6524 int fcp_cq_index = 0;
6527 * Set up Event Queues (EQs)
6530 /* Set up slow-path event queue */
6531 if (!phba->sli4_hba.sp_eq) {
6532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6533 "0520 Slow-path EQ not allocated\n");
6534 goto out_error;
6536 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6537 LPFC_SP_DEF_IMAX);
6538 if (rc) {
6539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6540 "0521 Failed setup of slow-path EQ: "
6541 "rc = 0x%x\n", rc);
6542 goto out_error;
6544 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6545 "2583 Slow-path EQ setup: queue-id=%d\n",
6546 phba->sli4_hba.sp_eq->queue_id);
6548 /* Set up fast-path event queue */
6549 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6550 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6552 "0522 Fast-path EQ (%d) not "
6553 "allocated\n", fcp_eqidx);
6554 goto out_destroy_fp_eq;
6556 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6557 phba->cfg_fcp_imax);
6558 if (rc) {
6559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6560 "0523 Failed setup of fast-path EQ "
6561 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6562 goto out_destroy_fp_eq;
6564 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6565 "2584 Fast-path EQ setup: "
6566 "queue[%d]-id=%d\n", fcp_eqidx,
6567 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6571 * Set up Complete Queues (CQs)
6574 /* Set up slow-path MBOX Complete Queue as the first CQ */
6575 if (!phba->sli4_hba.mbx_cq) {
6576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6577 "0528 Mailbox CQ not allocated\n");
6578 goto out_destroy_fp_eq;
6580 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6581 LPFC_MCQ, LPFC_MBOX);
6582 if (rc) {
6583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6584 "0529 Failed setup of slow-path mailbox CQ: "
6585 "rc = 0x%x\n", rc);
6586 goto out_destroy_fp_eq;
6588 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6589 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6590 phba->sli4_hba.mbx_cq->queue_id,
6591 phba->sli4_hba.sp_eq->queue_id);
6593 /* Set up slow-path ELS Complete Queue */
6594 if (!phba->sli4_hba.els_cq) {
6595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6596 "0530 ELS CQ not allocated\n");
6597 goto out_destroy_mbx_cq;
6599 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6600 LPFC_WCQ, LPFC_ELS);
6601 if (rc) {
6602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6603 "0531 Failed setup of slow-path ELS CQ: "
6604 "rc = 0x%x\n", rc);
6605 goto out_destroy_mbx_cq;
6607 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6608 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6609 phba->sli4_hba.els_cq->queue_id,
6610 phba->sli4_hba.sp_eq->queue_id);
6612 /* Set up fast-path FCP Response Complete Queue */
6613 fcp_cqidx = 0;
6614 do {
6615 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6617 "0526 Fast-path FCP CQ (%d) not "
6618 "allocated\n", fcp_cqidx);
6619 goto out_destroy_fcp_cq;
6621 if (phba->cfg_fcp_eq_count)
6622 rc = lpfc_cq_create(phba,
6623 phba->sli4_hba.fcp_cq[fcp_cqidx],
6624 phba->sli4_hba.fp_eq[fcp_cqidx],
6625 LPFC_WCQ, LPFC_FCP);
6626 else
6627 rc = lpfc_cq_create(phba,
6628 phba->sli4_hba.fcp_cq[fcp_cqidx],
6629 phba->sli4_hba.sp_eq,
6630 LPFC_WCQ, LPFC_FCP);
6631 if (rc) {
6632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6633 "0527 Failed setup of fast-path FCP "
6634 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6635 goto out_destroy_fcp_cq;
6637 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6638 "2588 FCP CQ setup: cq[%d]-id=%d, "
6639 "parent %seq[%d]-id=%d\n",
6640 fcp_cqidx,
6641 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6642 (phba->cfg_fcp_eq_count) ? "" : "sp_",
6643 fcp_cqidx,
6644 (phba->cfg_fcp_eq_count) ?
6645 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id :
6646 phba->sli4_hba.sp_eq->queue_id);
6647 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
6650 * Set up all the Work Queues (WQs)
6653 /* Set up Mailbox Command Queue */
6654 if (!phba->sli4_hba.mbx_wq) {
6655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6656 "0538 Slow-path MQ not allocated\n");
6657 goto out_destroy_fcp_cq;
6659 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6660 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6661 if (rc) {
6662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6663 "0539 Failed setup of slow-path MQ: "
6664 "rc = 0x%x\n", rc);
6665 goto out_destroy_fcp_cq;
6667 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6668 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6669 phba->sli4_hba.mbx_wq->queue_id,
6670 phba->sli4_hba.mbx_cq->queue_id);
6672 /* Set up slow-path ELS Work Queue */
6673 if (!phba->sli4_hba.els_wq) {
6674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6675 "0536 Slow-path ELS WQ not allocated\n");
6676 goto out_destroy_mbx_wq;
6678 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6679 phba->sli4_hba.els_cq, LPFC_ELS);
6680 if (rc) {
6681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6682 "0537 Failed setup of slow-path ELS WQ: "
6683 "rc = 0x%x\n", rc);
6684 goto out_destroy_mbx_wq;
6686 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6687 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6688 phba->sli4_hba.els_wq->queue_id,
6689 phba->sli4_hba.els_cq->queue_id);
6691 /* Set up fast-path FCP Work Queue */
6692 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6693 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6695 "0534 Fast-path FCP WQ (%d) not "
6696 "allocated\n", fcp_wqidx);
6697 goto out_destroy_fcp_wq;
6699 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6700 phba->sli4_hba.fcp_cq[fcp_cq_index],
6701 LPFC_FCP);
6702 if (rc) {
6703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6704 "0535 Failed setup of fast-path FCP "
6705 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6706 goto out_destroy_fcp_wq;
6708 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6709 "2591 FCP WQ setup: wq[%d]-id=%d, "
6710 "parent cq[%d]-id=%d\n",
6711 fcp_wqidx,
6712 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6713 fcp_cq_index,
6714 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6715 /* Round robin FCP Work Queue's Completion Queue assignment */
6716 if (phba->cfg_fcp_eq_count)
6717 fcp_cq_index = ((fcp_cq_index + 1) %
6718 phba->cfg_fcp_eq_count);
6722 * Create Receive Queue (RQ)
6724 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6726 "0540 Receive Queue not allocated\n");
6727 goto out_destroy_fcp_wq;
6729 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
6730 phba->sli4_hba.els_cq, LPFC_USOL);
6731 if (rc) {
6732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6733 "0541 Failed setup of Receive Queue: "
6734 "rc = 0x%x\n", rc);
6735 goto out_destroy_fcp_wq;
6737 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6738 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6739 "parent cq-id=%d\n",
6740 phba->sli4_hba.hdr_rq->queue_id,
6741 phba->sli4_hba.dat_rq->queue_id,
6742 phba->sli4_hba.els_cq->queue_id);
6743 return 0;
6745 out_destroy_fcp_wq:
6746 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6747 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6748 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6749 out_destroy_mbx_wq:
6750 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6751 out_destroy_fcp_cq:
6752 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6753 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
6754 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6755 out_destroy_mbx_cq:
6756 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6757 out_destroy_fp_eq:
6758 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6759 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6760 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6761 out_error:
6762 return rc;
6766 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6767 * @phba: pointer to lpfc hba data structure.
6769 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6770 * operation.
6772 * Return codes
6773 * 0 - successful
6774 * -ENOMEM - No available memory
6775 * -EIO - The mailbox failed to complete successfully.
6777 void
6778 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6780 int fcp_qidx;
6782 /* Unset mailbox command work queue */
6783 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6784 /* Unset ELS work queue */
6785 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6786 /* Unset unsolicited receive queue */
6787 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6788 /* Unset FCP work queue */
6789 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6790 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6791 /* Unset mailbox command complete queue */
6792 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6793 /* Unset ELS complete queue */
6794 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6795 /* Unset FCP response complete queue */
6796 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6797 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6798 /* Unset fast-path event queue */
6799 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6800 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6801 /* Unset slow-path event queue */
6802 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6806 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6807 * @phba: pointer to lpfc hba data structure.
6809 * This routine is invoked to allocate and set up a pool of completion queue
6810 * events. The body of the completion queue event is a completion queue entry
6811 * CQE. For now, this pool is used for the interrupt service routine to queue
6812 * the following HBA completion queue events for the worker thread to process:
6813 * - Mailbox asynchronous events
6814 * - Receive queue completion unsolicited events
6815 * Later, this can be used for all the slow-path events.
6817 * Return codes
6818 * 0 - successful
6819 * -ENOMEM - No available memory
6821 static int
6822 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6824 struct lpfc_cq_event *cq_event;
6825 int i;
6827 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6828 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6829 if (!cq_event)
6830 goto out_pool_create_fail;
6831 list_add_tail(&cq_event->list,
6832 &phba->sli4_hba.sp_cqe_event_pool);
6834 return 0;
6836 out_pool_create_fail:
6837 lpfc_sli4_cq_event_pool_destroy(phba);
6838 return -ENOMEM;
6842 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6843 * @phba: pointer to lpfc hba data structure.
6845 * This routine is invoked to free the pool of completion queue events at
6846 * driver unload time. Note that, it is the responsibility of the driver
6847 * cleanup routine to free all the outstanding completion-queue events
6848 * allocated from this pool back into the pool before invoking this routine
6849 * to destroy the pool.
6851 static void
6852 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6854 struct lpfc_cq_event *cq_event, *next_cq_event;
6856 list_for_each_entry_safe(cq_event, next_cq_event,
6857 &phba->sli4_hba.sp_cqe_event_pool, list) {
6858 list_del(&cq_event->list);
6859 kfree(cq_event);
6864 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6865 * @phba: pointer to lpfc hba data structure.
6867 * This routine is the lock free version of the API invoked to allocate a
6868 * completion-queue event from the free pool.
6870 * Return: Pointer to the newly allocated completion-queue event if successful
6871 * NULL otherwise.
6873 struct lpfc_cq_event *
6874 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6876 struct lpfc_cq_event *cq_event = NULL;
6878 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6879 struct lpfc_cq_event, list);
6880 return cq_event;
6884 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6885 * @phba: pointer to lpfc hba data structure.
6887 * This routine is the lock version of the API invoked to allocate a
6888 * completion-queue event from the free pool.
6890 * Return: Pointer to the newly allocated completion-queue event if successful
6891 * NULL otherwise.
6893 struct lpfc_cq_event *
6894 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6896 struct lpfc_cq_event *cq_event;
6897 unsigned long iflags;
6899 spin_lock_irqsave(&phba->hbalock, iflags);
6900 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6901 spin_unlock_irqrestore(&phba->hbalock, iflags);
6902 return cq_event;
6906 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6907 * @phba: pointer to lpfc hba data structure.
6908 * @cq_event: pointer to the completion queue event to be freed.
6910 * This routine is the lock free version of the API invoked to release a
6911 * completion-queue event back into the free pool.
6913 void
6914 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6915 struct lpfc_cq_event *cq_event)
6917 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6921 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6922 * @phba: pointer to lpfc hba data structure.
6923 * @cq_event: pointer to the completion queue event to be freed.
6925 * This routine is the lock version of the API invoked to release a
6926 * completion-queue event back into the free pool.
6928 void
6929 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6930 struct lpfc_cq_event *cq_event)
6932 unsigned long iflags;
6933 spin_lock_irqsave(&phba->hbalock, iflags);
6934 __lpfc_sli4_cq_event_release(phba, cq_event);
6935 spin_unlock_irqrestore(&phba->hbalock, iflags);
6939 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6940 * @phba: pointer to lpfc hba data structure.
6942 * This routine is to free all the pending completion-queue events to the
6943 * back into the free pool for device reset.
6945 static void
6946 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6948 LIST_HEAD(cqelist);
6949 struct lpfc_cq_event *cqe;
6950 unsigned long iflags;
6952 /* Retrieve all the pending WCQEs from pending WCQE lists */
6953 spin_lock_irqsave(&phba->hbalock, iflags);
6954 /* Pending FCP XRI abort events */
6955 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6956 &cqelist);
6957 /* Pending ELS XRI abort events */
6958 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6959 &cqelist);
6960 /* Pending asynnc events */
6961 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6962 &cqelist);
6963 spin_unlock_irqrestore(&phba->hbalock, iflags);
6965 while (!list_empty(&cqelist)) {
6966 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6967 lpfc_sli4_cq_event_release(phba, cqe);
6972 * lpfc_pci_function_reset - Reset pci function.
6973 * @phba: pointer to lpfc hba data structure.
6975 * This routine is invoked to request a PCI function reset. It will destroys
6976 * all resources assigned to the PCI function which originates this request.
6978 * Return codes
6979 * 0 - successful
6980 * -ENOMEM - No available memory
6981 * -EIO - The mailbox failed to complete successfully.
6984 lpfc_pci_function_reset(struct lpfc_hba *phba)
6986 LPFC_MBOXQ_t *mboxq;
6987 uint32_t rc = 0, if_type;
6988 uint32_t shdr_status, shdr_add_status;
6989 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
6990 union lpfc_sli4_cfg_shdr *shdr;
6991 struct lpfc_register reg_data;
6993 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6994 switch (if_type) {
6995 case LPFC_SLI_INTF_IF_TYPE_0:
6996 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6997 GFP_KERNEL);
6998 if (!mboxq) {
6999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7000 "0494 Unable to allocate memory for "
7001 "issuing SLI_FUNCTION_RESET mailbox "
7002 "command\n");
7003 return -ENOMEM;
7006 /* Setup PCI function reset mailbox-ioctl command */
7007 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7008 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7009 LPFC_SLI4_MBX_EMBED);
7010 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7011 shdr = (union lpfc_sli4_cfg_shdr *)
7012 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7013 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7014 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7015 &shdr->response);
7016 if (rc != MBX_TIMEOUT)
7017 mempool_free(mboxq, phba->mbox_mem_pool);
7018 if (shdr_status || shdr_add_status || rc) {
7019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7020 "0495 SLI_FUNCTION_RESET mailbox "
7021 "failed with status x%x add_status x%x,"
7022 " mbx status x%x\n",
7023 shdr_status, shdr_add_status, rc);
7024 rc = -ENXIO;
7026 break;
7027 case LPFC_SLI_INTF_IF_TYPE_2:
7028 for (num_resets = 0;
7029 num_resets < MAX_IF_TYPE_2_RESETS;
7030 num_resets++) {
7031 reg_data.word0 = 0;
7032 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7033 LPFC_SLIPORT_LITTLE_ENDIAN);
7034 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7035 LPFC_SLIPORT_INIT_PORT);
7036 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7037 CTRLregaddr);
7040 * Poll the Port Status Register and wait for RDY for
7041 * up to 10 seconds. If the port doesn't respond, treat
7042 * it as an error. If the port responds with RN, start
7043 * the loop again.
7045 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
7046 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7047 STATUSregaddr, &reg_data.word0)) {
7048 rc = -ENODEV;
7049 break;
7051 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7052 break;
7053 if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
7054 reset_again++;
7055 break;
7057 msleep(10);
7061 * If the port responds to the init request with
7062 * reset needed, delay for a bit and restart the loop.
7064 if (reset_again) {
7065 msleep(10);
7066 reset_again = 0;
7067 continue;
7070 /* Detect any port errors. */
7071 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7072 &reg_data.word0)) {
7073 rc = -ENODEV;
7074 break;
7076 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7077 (rdy_chk >= 1000)) {
7078 phba->work_status[0] = readl(
7079 phba->sli4_hba.u.if_type2.ERR1regaddr);
7080 phba->work_status[1] = readl(
7081 phba->sli4_hba.u.if_type2.ERR2regaddr);
7082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7083 "2890 Port Error Detected "
7084 "during Port Reset: "
7085 "port status reg 0x%x, "
7086 "error 1=0x%x, error 2=0x%x\n",
7087 reg_data.word0,
7088 phba->work_status[0],
7089 phba->work_status[1]);
7090 rc = -ENODEV;
7094 * Terminate the outer loop provided the Port indicated
7095 * ready within 10 seconds.
7097 if (rdy_chk < 1000)
7098 break;
7100 /* delay driver action following IF_TYPE_2 function reset */
7101 msleep(100);
7102 break;
7103 case LPFC_SLI_INTF_IF_TYPE_1:
7104 default:
7105 break;
7108 /* Catch the not-ready port failure after a port reset. */
7109 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7110 rc = -ENODEV;
7112 return rc;
7116 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
7117 * @phba: pointer to lpfc hba data structure.
7118 * @cnt: number of nop mailbox commands to send.
7120 * This routine is invoked to send a number @cnt of NOP mailbox command and
7121 * wait for each command to complete.
7123 * Return: the number of NOP mailbox command completed.
7125 static int
7126 lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
7128 LPFC_MBOXQ_t *mboxq;
7129 int length, cmdsent;
7130 uint32_t mbox_tmo;
7131 uint32_t rc = 0;
7132 uint32_t shdr_status, shdr_add_status;
7133 union lpfc_sli4_cfg_shdr *shdr;
7135 if (cnt == 0) {
7136 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7137 "2518 Requested to send 0 NOP mailbox cmd\n");
7138 return cnt;
7141 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7142 if (!mboxq) {
7143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7144 "2519 Unable to allocate memory for issuing "
7145 "NOP mailbox command\n");
7146 return 0;
7149 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
7150 length = (sizeof(struct lpfc_mbx_nop) -
7151 sizeof(struct lpfc_sli4_cfg_mhdr));
7152 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7153 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
7155 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
7156 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
7157 if (!phba->sli4_hba.intr_enable)
7158 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7159 else
7160 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7161 if (rc == MBX_TIMEOUT)
7162 break;
7163 /* Check return status */
7164 shdr = (union lpfc_sli4_cfg_shdr *)
7165 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7166 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7167 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7168 &shdr->response);
7169 if (shdr_status || shdr_add_status || rc) {
7170 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7171 "2520 NOP mailbox command failed "
7172 "status x%x add_status x%x mbx "
7173 "status x%x\n", shdr_status,
7174 shdr_add_status, rc);
7175 break;
7179 if (rc != MBX_TIMEOUT)
7180 mempool_free(mboxq, phba->mbox_mem_pool);
7182 return cmdsent;
7186 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7187 * @phba: pointer to lpfc hba data structure.
7189 * This routine is invoked to set up the PCI device memory space for device
7190 * with SLI-4 interface spec.
7192 * Return codes
7193 * 0 - successful
7194 * other values - error
7196 static int
7197 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7199 struct pci_dev *pdev;
7200 unsigned long bar0map_len, bar1map_len, bar2map_len;
7201 int error = -ENODEV;
7202 uint32_t if_type;
7204 /* Obtain PCI device reference */
7205 if (!phba->pcidev)
7206 return error;
7207 else
7208 pdev = phba->pcidev;
7210 /* Set the device DMA mask size */
7211 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7212 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7213 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7214 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7215 return error;
7220 * The BARs and register set definitions and offset locations are
7221 * dependent on the if_type.
7223 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7224 &phba->sli4_hba.sli_intf.word0)) {
7225 return error;
7228 /* There is no SLI3 failback for SLI4 devices. */
7229 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7230 LPFC_SLI_INTF_VALID) {
7231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7232 "2894 SLI_INTF reg contents invalid "
7233 "sli_intf reg 0x%x\n",
7234 phba->sli4_hba.sli_intf.word0);
7235 return error;
7238 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7240 * Get the bus address of SLI4 device Bar regions and the
7241 * number of bytes required by each mapping. The mapping of the
7242 * particular PCI BARs regions is dependent on the type of
7243 * SLI4 device.
7245 if (pci_resource_start(pdev, 0)) {
7246 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7247 bar0map_len = pci_resource_len(pdev, 0);
7250 * Map SLI4 PCI Config Space Register base to a kernel virtual
7251 * addr
7253 phba->sli4_hba.conf_regs_memmap_p =
7254 ioremap(phba->pci_bar0_map, bar0map_len);
7255 if (!phba->sli4_hba.conf_regs_memmap_p) {
7256 dev_printk(KERN_ERR, &pdev->dev,
7257 "ioremap failed for SLI4 PCI config "
7258 "registers.\n");
7259 goto out;
7261 /* Set up BAR0 PCI config space register memory map */
7262 lpfc_sli4_bar0_register_memmap(phba, if_type);
7263 } else {
7264 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7265 bar0map_len = pci_resource_len(pdev, 1);
7266 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7267 dev_printk(KERN_ERR, &pdev->dev,
7268 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7269 goto out;
7271 phba->sli4_hba.conf_regs_memmap_p =
7272 ioremap(phba->pci_bar0_map, bar0map_len);
7273 if (!phba->sli4_hba.conf_regs_memmap_p) {
7274 dev_printk(KERN_ERR, &pdev->dev,
7275 "ioremap failed for SLI4 PCI config "
7276 "registers.\n");
7277 goto out;
7279 lpfc_sli4_bar0_register_memmap(phba, if_type);
7282 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7283 (pci_resource_start(pdev, 2))) {
7285 * Map SLI4 if type 0 HBA Control Register base to a kernel
7286 * virtual address and setup the registers.
7288 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7289 bar1map_len = pci_resource_len(pdev, 2);
7290 phba->sli4_hba.ctrl_regs_memmap_p =
7291 ioremap(phba->pci_bar1_map, bar1map_len);
7292 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7293 dev_printk(KERN_ERR, &pdev->dev,
7294 "ioremap failed for SLI4 HBA control registers.\n");
7295 goto out_iounmap_conf;
7297 lpfc_sli4_bar1_register_memmap(phba);
7300 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7301 (pci_resource_start(pdev, 4))) {
7303 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7304 * virtual address and setup the registers.
7306 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7307 bar2map_len = pci_resource_len(pdev, 4);
7308 phba->sli4_hba.drbl_regs_memmap_p =
7309 ioremap(phba->pci_bar2_map, bar2map_len);
7310 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7311 dev_printk(KERN_ERR, &pdev->dev,
7312 "ioremap failed for SLI4 HBA doorbell registers.\n");
7313 goto out_iounmap_ctrl;
7315 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7316 if (error)
7317 goto out_iounmap_all;
7320 return 0;
7322 out_iounmap_all:
7323 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7324 out_iounmap_ctrl:
7325 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7326 out_iounmap_conf:
7327 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7328 out:
7329 return error;
7333 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7334 * @phba: pointer to lpfc hba data structure.
7336 * This routine is invoked to unset the PCI device memory space for device
7337 * with SLI-4 interface spec.
7339 static void
7340 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7342 struct pci_dev *pdev;
7344 /* Obtain PCI device reference */
7345 if (!phba->pcidev)
7346 return;
7347 else
7348 pdev = phba->pcidev;
7350 /* Free coherent DMA memory allocated */
7352 /* Unmap I/O memory space */
7353 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7354 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7355 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7357 return;
7361 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7362 * @phba: pointer to lpfc hba data structure.
7364 * This routine is invoked to enable the MSI-X interrupt vectors to device
7365 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7366 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7367 * invoked, enables either all or nothing, depending on the current
7368 * availability of PCI vector resources. The device driver is responsible
7369 * for calling the individual request_irq() to register each MSI-X vector
7370 * with a interrupt handler, which is done in this function. Note that
7371 * later when device is unloading, the driver should always call free_irq()
7372 * on all MSI-X vectors it has done request_irq() on before calling
7373 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7374 * will be left with MSI-X enabled and leaks its vectors.
7376 * Return codes
7377 * 0 - successful
7378 * other values - error
7380 static int
7381 lpfc_sli_enable_msix(struct lpfc_hba *phba)
7383 int rc, i;
7384 LPFC_MBOXQ_t *pmb;
7386 /* Set up MSI-X multi-message vectors */
7387 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7388 phba->msix_entries[i].entry = i;
7390 /* Configure MSI-X capability structure */
7391 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7392 ARRAY_SIZE(phba->msix_entries));
7393 if (rc) {
7394 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7395 "0420 PCI enable MSI-X failed (%d)\n", rc);
7396 goto msi_fail_out;
7398 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7399 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7400 "0477 MSI-X entry[%d]: vector=x%x "
7401 "message=%d\n", i,
7402 phba->msix_entries[i].vector,
7403 phba->msix_entries[i].entry);
7405 * Assign MSI-X vectors to interrupt handlers
7408 /* vector-0 is associated to slow-path handler */
7409 rc = request_irq(phba->msix_entries[0].vector,
7410 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7411 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7412 if (rc) {
7413 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7414 "0421 MSI-X slow-path request_irq failed "
7415 "(%d)\n", rc);
7416 goto msi_fail_out;
7419 /* vector-1 is associated to fast-path handler */
7420 rc = request_irq(phba->msix_entries[1].vector,
7421 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7422 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7424 if (rc) {
7425 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7426 "0429 MSI-X fast-path request_irq failed "
7427 "(%d)\n", rc);
7428 goto irq_fail_out;
7432 * Configure HBA MSI-X attention conditions to messages
7434 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7436 if (!pmb) {
7437 rc = -ENOMEM;
7438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7439 "0474 Unable to allocate memory for issuing "
7440 "MBOX_CONFIG_MSI command\n");
7441 goto mem_fail_out;
7443 rc = lpfc_config_msi(phba, pmb);
7444 if (rc)
7445 goto mbx_fail_out;
7446 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7447 if (rc != MBX_SUCCESS) {
7448 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7449 "0351 Config MSI mailbox command failed, "
7450 "mbxCmd x%x, mbxStatus x%x\n",
7451 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7452 goto mbx_fail_out;
7455 /* Free memory allocated for mailbox command */
7456 mempool_free(pmb, phba->mbox_mem_pool);
7457 return rc;
7459 mbx_fail_out:
7460 /* Free memory allocated for mailbox command */
7461 mempool_free(pmb, phba->mbox_mem_pool);
7463 mem_fail_out:
7464 /* free the irq already requested */
7465 free_irq(phba->msix_entries[1].vector, phba);
7467 irq_fail_out:
7468 /* free the irq already requested */
7469 free_irq(phba->msix_entries[0].vector, phba);
7471 msi_fail_out:
7472 /* Unconfigure MSI-X capability structure */
7473 pci_disable_msix(phba->pcidev);
7474 return rc;
7478 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7479 * @phba: pointer to lpfc hba data structure.
7481 * This routine is invoked to release the MSI-X vectors and then disable the
7482 * MSI-X interrupt mode to device with SLI-3 interface spec.
7484 static void
7485 lpfc_sli_disable_msix(struct lpfc_hba *phba)
7487 int i;
7489 /* Free up MSI-X multi-message vectors */
7490 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7491 free_irq(phba->msix_entries[i].vector, phba);
7492 /* Disable MSI-X */
7493 pci_disable_msix(phba->pcidev);
7495 return;
7499 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7500 * @phba: pointer to lpfc hba data structure.
7502 * This routine is invoked to enable the MSI interrupt mode to device with
7503 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7504 * enable the MSI vector. The device driver is responsible for calling the
7505 * request_irq() to register MSI vector with a interrupt the handler, which
7506 * is done in this function.
7508 * Return codes
7509 * 0 - successful
7510 * other values - error
7512 static int
7513 lpfc_sli_enable_msi(struct lpfc_hba *phba)
7515 int rc;
7517 rc = pci_enable_msi(phba->pcidev);
7518 if (!rc)
7519 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7520 "0462 PCI enable MSI mode success.\n");
7521 else {
7522 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7523 "0471 PCI enable MSI mode failed (%d)\n", rc);
7524 return rc;
7527 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7528 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7529 if (rc) {
7530 pci_disable_msi(phba->pcidev);
7531 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7532 "0478 MSI request_irq failed (%d)\n", rc);
7534 return rc;
7538 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7539 * @phba: pointer to lpfc hba data structure.
7541 * This routine is invoked to disable the MSI interrupt mode to device with
7542 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7543 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7544 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7545 * its vector.
7547 static void
7548 lpfc_sli_disable_msi(struct lpfc_hba *phba)
7550 free_irq(phba->pcidev->irq, phba);
7551 pci_disable_msi(phba->pcidev);
7552 return;
7556 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
7557 * @phba: pointer to lpfc hba data structure.
7559 * This routine is invoked to enable device interrupt and associate driver's
7560 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
7561 * spec. Depends on the interrupt mode configured to the driver, the driver
7562 * will try to fallback from the configured interrupt mode to an interrupt
7563 * mode which is supported by the platform, kernel, and device in the order
7564 * of:
7565 * MSI-X -> MSI -> IRQ.
7567 * Return codes
7568 * 0 - successful
7569 * other values - error
7571 static uint32_t
7572 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7574 uint32_t intr_mode = LPFC_INTR_ERROR;
7575 int retval;
7577 if (cfg_mode == 2) {
7578 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
7579 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
7580 if (!retval) {
7581 /* Now, try to enable MSI-X interrupt mode */
7582 retval = lpfc_sli_enable_msix(phba);
7583 if (!retval) {
7584 /* Indicate initialization to MSI-X mode */
7585 phba->intr_type = MSIX;
7586 intr_mode = 2;
7591 /* Fallback to MSI if MSI-X initialization failed */
7592 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7593 retval = lpfc_sli_enable_msi(phba);
7594 if (!retval) {
7595 /* Indicate initialization to MSI mode */
7596 phba->intr_type = MSI;
7597 intr_mode = 1;
7601 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7602 if (phba->intr_type == NONE) {
7603 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7604 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7605 if (!retval) {
7606 /* Indicate initialization to INTx mode */
7607 phba->intr_type = INTx;
7608 intr_mode = 0;
7611 return intr_mode;
7615 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7616 * @phba: pointer to lpfc hba data structure.
7618 * This routine is invoked to disable device interrupt and disassociate the
7619 * driver's interrupt handler(s) from interrupt vector(s) to device with
7620 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7621 * release the interrupt vector(s) for the message signaled interrupt.
7623 static void
7624 lpfc_sli_disable_intr(struct lpfc_hba *phba)
7626 /* Disable the currently initialized interrupt mode */
7627 if (phba->intr_type == MSIX)
7628 lpfc_sli_disable_msix(phba);
7629 else if (phba->intr_type == MSI)
7630 lpfc_sli_disable_msi(phba);
7631 else if (phba->intr_type == INTx)
7632 free_irq(phba->pcidev->irq, phba);
7634 /* Reset interrupt management states */
7635 phba->intr_type = NONE;
7636 phba->sli.slistat.sli_intr = 0;
7638 return;
7642 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7643 * @phba: pointer to lpfc hba data structure.
7645 * This routine is invoked to enable the MSI-X interrupt vectors to device
7646 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7647 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7648 * enables either all or nothing, depending on the current availability of
7649 * PCI vector resources. The device driver is responsible for calling the
7650 * individual request_irq() to register each MSI-X vector with a interrupt
7651 * handler, which is done in this function. Note that later when device is
7652 * unloading, the driver should always call free_irq() on all MSI-X vectors
7653 * it has done request_irq() on before calling pci_disable_msix(). Failure
7654 * to do so results in a BUG_ON() and a device will be left with MSI-X
7655 * enabled and leaks its vectors.
7657 * Return codes
7658 * 0 - successful
7659 * other values - error
7661 static int
7662 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7664 int vectors, rc, index;
7666 /* Set up MSI-X multi-message vectors */
7667 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7668 phba->sli4_hba.msix_entries[index].entry = index;
7670 /* Configure MSI-X capability structure */
7671 vectors = phba->sli4_hba.cfg_eqn;
7672 enable_msix_vectors:
7673 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
7674 vectors);
7675 if (rc > 1) {
7676 vectors = rc;
7677 goto enable_msix_vectors;
7678 } else if (rc) {
7679 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7680 "0484 PCI enable MSI-X failed (%d)\n", rc);
7681 goto msi_fail_out;
7684 /* Log MSI-X vector assignment */
7685 for (index = 0; index < vectors; index++)
7686 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7687 "0489 MSI-X entry[%d]: vector=x%x "
7688 "message=%d\n", index,
7689 phba->sli4_hba.msix_entries[index].vector,
7690 phba->sli4_hba.msix_entries[index].entry);
7692 * Assign MSI-X vectors to interrupt handlers
7694 if (vectors > 1)
7695 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7696 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7697 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7698 else
7699 /* All Interrupts need to be handled by one EQ */
7700 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7701 &lpfc_sli4_intr_handler, IRQF_SHARED,
7702 LPFC_DRIVER_NAME, phba);
7703 if (rc) {
7704 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7705 "0485 MSI-X slow-path request_irq failed "
7706 "(%d)\n", rc);
7707 goto msi_fail_out;
7710 /* The rest of the vector(s) are associated to fast-path handler(s) */
7711 for (index = 1; index < vectors; index++) {
7712 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7713 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7714 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7715 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7716 LPFC_FP_DRIVER_HANDLER_NAME,
7717 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7718 if (rc) {
7719 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7720 "0486 MSI-X fast-path (%d) "
7721 "request_irq failed (%d)\n", index, rc);
7722 goto cfg_fail_out;
7725 phba->sli4_hba.msix_vec_nr = vectors;
7727 return rc;
7729 cfg_fail_out:
7730 /* free the irq already requested */
7731 for (--index; index >= 1; index--)
7732 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7733 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7735 /* free the irq already requested */
7736 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7738 msi_fail_out:
7739 /* Unconfigure MSI-X capability structure */
7740 pci_disable_msix(phba->pcidev);
7741 return rc;
7745 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7746 * @phba: pointer to lpfc hba data structure.
7748 * This routine is invoked to release the MSI-X vectors and then disable the
7749 * MSI-X interrupt mode to device with SLI-4 interface spec.
7751 static void
7752 lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7754 int index;
7756 /* Free up MSI-X multi-message vectors */
7757 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7759 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
7760 free_irq(phba->sli4_hba.msix_entries[index].vector,
7761 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7763 /* Disable MSI-X */
7764 pci_disable_msix(phba->pcidev);
7766 return;
7770 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7771 * @phba: pointer to lpfc hba data structure.
7773 * This routine is invoked to enable the MSI interrupt mode to device with
7774 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7775 * to enable the MSI vector. The device driver is responsible for calling
7776 * the request_irq() to register MSI vector with a interrupt the handler,
7777 * which is done in this function.
7779 * Return codes
7780 * 0 - successful
7781 * other values - error
7783 static int
7784 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7786 int rc, index;
7788 rc = pci_enable_msi(phba->pcidev);
7789 if (!rc)
7790 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7791 "0487 PCI enable MSI mode success.\n");
7792 else {
7793 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7794 "0488 PCI enable MSI mode failed (%d)\n", rc);
7795 return rc;
7798 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7799 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7800 if (rc) {
7801 pci_disable_msi(phba->pcidev);
7802 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7803 "0490 MSI request_irq failed (%d)\n", rc);
7804 return rc;
7807 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7808 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7809 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7812 return 0;
7816 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7817 * @phba: pointer to lpfc hba data structure.
7819 * This routine is invoked to disable the MSI interrupt mode to device with
7820 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7821 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7822 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7823 * its vector.
7825 static void
7826 lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7828 free_irq(phba->pcidev->irq, phba);
7829 pci_disable_msi(phba->pcidev);
7830 return;
7834 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7835 * @phba: pointer to lpfc hba data structure.
7837 * This routine is invoked to enable device interrupt and associate driver's
7838 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7839 * interface spec. Depends on the interrupt mode configured to the driver,
7840 * the driver will try to fallback from the configured interrupt mode to an
7841 * interrupt mode which is supported by the platform, kernel, and device in
7842 * the order of:
7843 * MSI-X -> MSI -> IRQ.
7845 * Return codes
7846 * 0 - successful
7847 * other values - error
7849 static uint32_t
7850 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7852 uint32_t intr_mode = LPFC_INTR_ERROR;
7853 int retval, index;
7855 if (cfg_mode == 2) {
7856 /* Preparation before conf_msi mbox cmd */
7857 retval = 0;
7858 if (!retval) {
7859 /* Now, try to enable MSI-X interrupt mode */
7860 retval = lpfc_sli4_enable_msix(phba);
7861 if (!retval) {
7862 /* Indicate initialization to MSI-X mode */
7863 phba->intr_type = MSIX;
7864 intr_mode = 2;
7869 /* Fallback to MSI if MSI-X initialization failed */
7870 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7871 retval = lpfc_sli4_enable_msi(phba);
7872 if (!retval) {
7873 /* Indicate initialization to MSI mode */
7874 phba->intr_type = MSI;
7875 intr_mode = 1;
7879 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7880 if (phba->intr_type == NONE) {
7881 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7882 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7883 if (!retval) {
7884 /* Indicate initialization to INTx mode */
7885 phba->intr_type = INTx;
7886 intr_mode = 0;
7887 for (index = 0; index < phba->cfg_fcp_eq_count;
7888 index++) {
7889 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7890 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7894 return intr_mode;
7898 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7899 * @phba: pointer to lpfc hba data structure.
7901 * This routine is invoked to disable device interrupt and disassociate
7902 * the driver's interrupt handler(s) from interrupt vector(s) to device
7903 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7904 * will release the interrupt vector(s) for the message signaled interrupt.
7906 static void
7907 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7909 /* Disable the currently initialized interrupt mode */
7910 if (phba->intr_type == MSIX)
7911 lpfc_sli4_disable_msix(phba);
7912 else if (phba->intr_type == MSI)
7913 lpfc_sli4_disable_msi(phba);
7914 else if (phba->intr_type == INTx)
7915 free_irq(phba->pcidev->irq, phba);
7917 /* Reset interrupt management states */
7918 phba->intr_type = NONE;
7919 phba->sli.slistat.sli_intr = 0;
7921 return;
7925 * lpfc_unset_hba - Unset SLI3 hba device initialization
7926 * @phba: pointer to lpfc hba data structure.
7928 * This routine is invoked to unset the HBA device initialization steps to
7929 * a device with SLI-3 interface spec.
7931 static void
7932 lpfc_unset_hba(struct lpfc_hba *phba)
7934 struct lpfc_vport *vport = phba->pport;
7935 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7937 spin_lock_irq(shost->host_lock);
7938 vport->load_flag |= FC_UNLOADING;
7939 spin_unlock_irq(shost->host_lock);
7941 lpfc_stop_hba_timers(phba);
7943 phba->pport->work_port_events = 0;
7945 lpfc_sli_hba_down(phba);
7947 lpfc_sli_brdrestart(phba);
7949 lpfc_sli_disable_intr(phba);
7951 return;
7955 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7956 * @phba: pointer to lpfc hba data structure.
7958 * This routine is invoked to unset the HBA device initialization steps to
7959 * a device with SLI-4 interface spec.
7961 static void
7962 lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7964 struct lpfc_vport *vport = phba->pport;
7965 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7967 spin_lock_irq(shost->host_lock);
7968 vport->load_flag |= FC_UNLOADING;
7969 spin_unlock_irq(shost->host_lock);
7971 phba->pport->work_port_events = 0;
7973 /* Stop the SLI4 device port */
7974 lpfc_stop_port(phba);
7976 lpfc_sli4_disable_intr(phba);
7978 /* Reset SLI4 HBA FCoE function */
7979 lpfc_pci_function_reset(phba);
7981 return;
7985 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
7986 * @phba: Pointer to HBA context object.
7988 * This function is called in the SLI4 code path to wait for completion
7989 * of device's XRIs exchange busy. It will check the XRI exchange busy
7990 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
7991 * that, it will check the XRI exchange busy on outstanding FCP and ELS
7992 * I/Os every 30 seconds, log error message, and wait forever. Only when
7993 * all XRI exchange busy complete, the driver unload shall proceed with
7994 * invoking the function reset ioctl mailbox command to the CNA and the
7995 * the rest of the driver unload resource release.
7997 static void
7998 lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8000 int wait_time = 0;
8001 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8002 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8004 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8005 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8006 if (!fcp_xri_cmpl)
8007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8008 "2877 FCP XRI exchange busy "
8009 "wait time: %d seconds.\n",
8010 wait_time/1000);
8011 if (!els_xri_cmpl)
8012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8013 "2878 ELS XRI exchange busy "
8014 "wait time: %d seconds.\n",
8015 wait_time/1000);
8016 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8017 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8018 } else {
8019 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8020 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8022 fcp_xri_cmpl =
8023 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8024 els_xri_cmpl =
8025 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8030 * lpfc_sli4_hba_unset - Unset the fcoe hba
8031 * @phba: Pointer to HBA context object.
8033 * This function is called in the SLI4 code path to reset the HBA's FCoE
8034 * function. The caller is not required to hold any lock. This routine
8035 * issues PCI function reset mailbox command to reset the FCoE function.
8036 * At the end of the function, it calls lpfc_hba_down_post function to
8037 * free any pending commands.
8039 static void
8040 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8042 int wait_cnt = 0;
8043 LPFC_MBOXQ_t *mboxq;
8044 struct pci_dev *pdev = phba->pcidev;
8046 lpfc_stop_hba_timers(phba);
8047 phba->sli4_hba.intr_enable = 0;
8050 * Gracefully wait out the potential current outstanding asynchronous
8051 * mailbox command.
8054 /* First, block any pending async mailbox command from posted */
8055 spin_lock_irq(&phba->hbalock);
8056 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8057 spin_unlock_irq(&phba->hbalock);
8058 /* Now, trying to wait it out if we can */
8059 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8060 msleep(10);
8061 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8062 break;
8064 /* Forcefully release the outstanding mailbox command if timed out */
8065 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8066 spin_lock_irq(&phba->hbalock);
8067 mboxq = phba->sli.mbox_active;
8068 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8069 __lpfc_mbox_cmpl_put(phba, mboxq);
8070 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8071 phba->sli.mbox_active = NULL;
8072 spin_unlock_irq(&phba->hbalock);
8075 /* Abort all iocbs associated with the hba */
8076 lpfc_sli_hba_iocb_abort(phba);
8078 /* Wait for completion of device XRI exchange busy */
8079 lpfc_sli4_xri_exchange_busy_wait(phba);
8081 /* Disable PCI subsystem interrupt */
8082 lpfc_sli4_disable_intr(phba);
8084 /* Disable SR-IOV if enabled */
8085 if (phba->cfg_sriov_nr_virtfn)
8086 pci_disable_sriov(pdev);
8088 /* Stop kthread signal shall trigger work_done one more time */
8089 kthread_stop(phba->worker_thread);
8091 /* Reset SLI4 HBA FCoE function */
8092 lpfc_pci_function_reset(phba);
8094 /* Stop the SLI4 device port */
8095 phba->pport->work_port_events = 0;
8099 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8100 * @phba: Pointer to HBA context object.
8101 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8103 * This function is called in the SLI4 code path to read the port's
8104 * sli4 capabilities.
8106 * This function may be be called from any context that can block-wait
8107 * for the completion. The expectation is that this routine is called
8108 * typically from probe_one or from the online routine.
8111 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8113 int rc;
8114 struct lpfc_mqe *mqe;
8115 struct lpfc_pc_sli4_params *sli4_params;
8116 uint32_t mbox_tmo;
8118 rc = 0;
8119 mqe = &mboxq->u.mqe;
8121 /* Read the port's SLI4 Parameters port capabilities */
8122 lpfc_pc_sli4_params(mboxq);
8123 if (!phba->sli4_hba.intr_enable)
8124 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8125 else {
8126 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
8127 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8130 if (unlikely(rc))
8131 return 1;
8133 sli4_params = &phba->sli4_hba.pc_sli4_params;
8134 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8135 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8136 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8137 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8138 &mqe->un.sli4_params);
8139 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8140 &mqe->un.sli4_params);
8141 sli4_params->proto_types = mqe->un.sli4_params.word3;
8142 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8143 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8144 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8145 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8146 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8147 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8148 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8149 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8150 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8151 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8152 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8153 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8154 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8155 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8156 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8157 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8158 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8159 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8160 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8161 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
8163 /* Make sure that sge_supp_len can be handled by the driver */
8164 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8165 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8167 return rc;
8171 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8172 * @phba: Pointer to HBA context object.
8173 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8175 * This function is called in the SLI4 code path to read the port's
8176 * sli4 capabilities.
8178 * This function may be be called from any context that can block-wait
8179 * for the completion. The expectation is that this routine is called
8180 * typically from probe_one or from the online routine.
8183 lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8185 int rc;
8186 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8187 struct lpfc_pc_sli4_params *sli4_params;
8188 int length;
8189 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8192 * By default, the driver assumes the SLI4 port requires RPI
8193 * header postings. The SLI4_PARAM response will correct this
8194 * assumption.
8196 phba->sli4_hba.rpi_hdrs_in_use = 1;
8198 /* Read the port's SLI4 Config Parameters */
8199 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8200 sizeof(struct lpfc_sli4_cfg_mhdr));
8201 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8202 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8203 length, LPFC_SLI4_MBX_EMBED);
8204 if (!phba->sli4_hba.intr_enable)
8205 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8206 else
8207 rc = lpfc_sli_issue_mbox_wait(phba, mboxq,
8208 lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG));
8209 if (unlikely(rc))
8210 return rc;
8211 sli4_params = &phba->sli4_hba.pc_sli4_params;
8212 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8213 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8214 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8215 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8216 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8217 mbx_sli4_parameters);
8218 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8219 mbx_sli4_parameters);
8220 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8221 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8222 else
8223 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8224 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8225 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8226 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8227 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8228 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8229 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8230 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8231 mbx_sli4_parameters);
8232 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8233 mbx_sli4_parameters);
8234 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8235 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
8237 /* Make sure that sge_supp_len can be handled by the driver */
8238 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8239 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8241 return 0;
8245 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8246 * @pdev: pointer to PCI device
8247 * @pid: pointer to PCI device identifier
8249 * This routine is to be called to attach a device with SLI-3 interface spec
8250 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8251 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8252 * information of the device and driver to see if the driver state that it can
8253 * support this kind of device. If the match is successful, the driver core
8254 * invokes this routine. If this routine determines it can claim the HBA, it
8255 * does all the initialization that it needs to do to handle the HBA properly.
8257 * Return code
8258 * 0 - driver can claim the device
8259 * negative value - driver can not claim the device
8261 static int __devinit
8262 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8264 struct lpfc_hba *phba;
8265 struct lpfc_vport *vport = NULL;
8266 struct Scsi_Host *shost = NULL;
8267 int error;
8268 uint32_t cfg_mode, intr_mode;
8270 /* Allocate memory for HBA structure */
8271 phba = lpfc_hba_alloc(pdev);
8272 if (!phba)
8273 return -ENOMEM;
8275 /* Perform generic PCI device enabling operation */
8276 error = lpfc_enable_pci_dev(phba);
8277 if (error)
8278 goto out_free_phba;
8280 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8281 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8282 if (error)
8283 goto out_disable_pci_dev;
8285 /* Set up SLI-3 specific device PCI memory space */
8286 error = lpfc_sli_pci_mem_setup(phba);
8287 if (error) {
8288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8289 "1402 Failed to set up pci memory space.\n");
8290 goto out_disable_pci_dev;
8293 /* Set up phase-1 common device driver resources */
8294 error = lpfc_setup_driver_resource_phase1(phba);
8295 if (error) {
8296 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8297 "1403 Failed to set up driver resource.\n");
8298 goto out_unset_pci_mem_s3;
8301 /* Set up SLI-3 specific device driver resources */
8302 error = lpfc_sli_driver_resource_setup(phba);
8303 if (error) {
8304 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8305 "1404 Failed to set up driver resource.\n");
8306 goto out_unset_pci_mem_s3;
8309 /* Initialize and populate the iocb list per host */
8310 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8311 if (error) {
8312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8313 "1405 Failed to initialize iocb list.\n");
8314 goto out_unset_driver_resource_s3;
8317 /* Set up common device driver resources */
8318 error = lpfc_setup_driver_resource_phase2(phba);
8319 if (error) {
8320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8321 "1406 Failed to set up driver resource.\n");
8322 goto out_free_iocb_list;
8325 /* Get the default values for Model Name and Description */
8326 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8328 /* Create SCSI host to the physical port */
8329 error = lpfc_create_shost(phba);
8330 if (error) {
8331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8332 "1407 Failed to create scsi host.\n");
8333 goto out_unset_driver_resource;
8336 /* Configure sysfs attributes */
8337 vport = phba->pport;
8338 error = lpfc_alloc_sysfs_attr(vport);
8339 if (error) {
8340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8341 "1476 Failed to allocate sysfs attr\n");
8342 goto out_destroy_shost;
8345 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
8346 /* Now, trying to enable interrupt and bring up the device */
8347 cfg_mode = phba->cfg_use_msi;
8348 while (true) {
8349 /* Put device to a known state before enabling interrupt */
8350 lpfc_stop_port(phba);
8351 /* Configure and enable interrupt */
8352 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8353 if (intr_mode == LPFC_INTR_ERROR) {
8354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8355 "0431 Failed to enable interrupt.\n");
8356 error = -ENODEV;
8357 goto out_free_sysfs_attr;
8359 /* SLI-3 HBA setup */
8360 if (lpfc_sli_hba_setup(phba)) {
8361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8362 "1477 Failed to set up hba\n");
8363 error = -ENODEV;
8364 goto out_remove_device;
8367 /* Wait 50ms for the interrupts of previous mailbox commands */
8368 msleep(50);
8369 /* Check active interrupts on message signaled interrupts */
8370 if (intr_mode == 0 ||
8371 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8372 /* Log the current active interrupt mode */
8373 phba->intr_mode = intr_mode;
8374 lpfc_log_intr_mode(phba, intr_mode);
8375 break;
8376 } else {
8377 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8378 "0447 Configure interrupt mode (%d) "
8379 "failed active interrupt test.\n",
8380 intr_mode);
8381 /* Disable the current interrupt mode */
8382 lpfc_sli_disable_intr(phba);
8383 /* Try next level of interrupt mode */
8384 cfg_mode = --intr_mode;
8388 /* Perform post initialization setup */
8389 lpfc_post_init_setup(phba);
8391 /* Check if there are static vports to be created. */
8392 lpfc_create_static_vport(phba);
8394 return 0;
8396 out_remove_device:
8397 lpfc_unset_hba(phba);
8398 out_free_sysfs_attr:
8399 lpfc_free_sysfs_attr(vport);
8400 out_destroy_shost:
8401 lpfc_destroy_shost(phba);
8402 out_unset_driver_resource:
8403 lpfc_unset_driver_resource_phase2(phba);
8404 out_free_iocb_list:
8405 lpfc_free_iocb_list(phba);
8406 out_unset_driver_resource_s3:
8407 lpfc_sli_driver_resource_unset(phba);
8408 out_unset_pci_mem_s3:
8409 lpfc_sli_pci_mem_unset(phba);
8410 out_disable_pci_dev:
8411 lpfc_disable_pci_dev(phba);
8412 if (shost)
8413 scsi_host_put(shost);
8414 out_free_phba:
8415 lpfc_hba_free(phba);
8416 return error;
8420 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
8421 * @pdev: pointer to PCI device
8423 * This routine is to be called to disattach a device with SLI-3 interface
8424 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8425 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8426 * device to be removed from the PCI subsystem properly.
8428 static void __devexit
8429 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
8431 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8432 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8433 struct lpfc_vport **vports;
8434 struct lpfc_hba *phba = vport->phba;
8435 int i;
8436 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8438 spin_lock_irq(&phba->hbalock);
8439 vport->load_flag |= FC_UNLOADING;
8440 spin_unlock_irq(&phba->hbalock);
8442 lpfc_free_sysfs_attr(vport);
8444 /* Release all the vports against this physical port */
8445 vports = lpfc_create_vport_work_array(phba);
8446 if (vports != NULL)
8447 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8448 fc_vport_terminate(vports[i]->fc_vport);
8449 lpfc_destroy_vport_work_array(phba, vports);
8451 /* Remove FC host and then SCSI host with the physical port */
8452 fc_remove_host(shost);
8453 scsi_remove_host(shost);
8454 lpfc_cleanup(vport);
8457 * Bring down the SLI Layer. This step disable all interrupts,
8458 * clears the rings, discards all mailbox commands, and resets
8459 * the HBA.
8462 /* HBA interrupt will be disabled after this call */
8463 lpfc_sli_hba_down(phba);
8464 /* Stop kthread signal shall trigger work_done one more time */
8465 kthread_stop(phba->worker_thread);
8466 /* Final cleanup of txcmplq and reset the HBA */
8467 lpfc_sli_brdrestart(phba);
8469 lpfc_stop_hba_timers(phba);
8470 spin_lock_irq(&phba->hbalock);
8471 list_del_init(&vport->listentry);
8472 spin_unlock_irq(&phba->hbalock);
8474 lpfc_debugfs_terminate(vport);
8476 /* Disable SR-IOV if enabled */
8477 if (phba->cfg_sriov_nr_virtfn)
8478 pci_disable_sriov(pdev);
8480 /* Disable interrupt */
8481 lpfc_sli_disable_intr(phba);
8483 pci_set_drvdata(pdev, NULL);
8484 scsi_host_put(shost);
8487 * Call scsi_free before mem_free since scsi bufs are released to their
8488 * corresponding pools here.
8490 lpfc_scsi_free(phba);
8491 lpfc_mem_free_all(phba);
8493 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8494 phba->hbqslimp.virt, phba->hbqslimp.phys);
8496 /* Free resources associated with SLI2 interface */
8497 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
8498 phba->slim2p.virt, phba->slim2p.phys);
8500 /* unmap adapter SLIM and Control Registers */
8501 iounmap(phba->ctrl_regs_memmap_p);
8502 iounmap(phba->slim_memmap_p);
8504 lpfc_hba_free(phba);
8506 pci_release_selected_regions(pdev, bars);
8507 pci_disable_device(pdev);
8511 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
8512 * @pdev: pointer to PCI device
8513 * @msg: power management message
8515 * This routine is to be called from the kernel's PCI subsystem to support
8516 * system Power Management (PM) to device with SLI-3 interface spec. When
8517 * PM invokes this method, it quiesces the device by stopping the driver's
8518 * worker thread for the device, turning off device's interrupt and DMA,
8519 * and bring the device offline. Note that as the driver implements the
8520 * minimum PM requirements to a power-aware driver's PM support for the
8521 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8522 * to the suspend() method call will be treated as SUSPEND and the driver will
8523 * fully reinitialize its device during resume() method call, the driver will
8524 * set device to PCI_D3hot state in PCI config space instead of setting it
8525 * according to the @msg provided by the PM.
8527 * Return code
8528 * 0 - driver suspended the device
8529 * Error otherwise
8531 static int
8532 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
8534 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8535 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8537 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8538 "0473 PCI device Power Management suspend.\n");
8540 /* Bring down the device */
8541 lpfc_offline_prep(phba);
8542 lpfc_offline(phba);
8543 kthread_stop(phba->worker_thread);
8545 /* Disable interrupt from device */
8546 lpfc_sli_disable_intr(phba);
8548 /* Save device state to PCI config space */
8549 pci_save_state(pdev);
8550 pci_set_power_state(pdev, PCI_D3hot);
8552 return 0;
8556 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
8557 * @pdev: pointer to PCI device
8559 * This routine is to be called from the kernel's PCI subsystem to support
8560 * system Power Management (PM) to device with SLI-3 interface spec. When PM
8561 * invokes this method, it restores the device's PCI config space state and
8562 * fully reinitializes the device and brings it online. Note that as the
8563 * driver implements the minimum PM requirements to a power-aware driver's
8564 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
8565 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
8566 * driver will fully reinitialize its device during resume() method call,
8567 * the device will be set to PCI_D0 directly in PCI config space before
8568 * restoring the state.
8570 * Return code
8571 * 0 - driver suspended the device
8572 * Error otherwise
8574 static int
8575 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
8577 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8578 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8579 uint32_t intr_mode;
8580 int error;
8582 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8583 "0452 PCI device Power Management resume.\n");
8585 /* Restore device state from PCI config space */
8586 pci_set_power_state(pdev, PCI_D0);
8587 pci_restore_state(pdev);
8590 * As the new kernel behavior of pci_restore_state() API call clears
8591 * device saved_state flag, need to save the restored state again.
8593 pci_save_state(pdev);
8595 if (pdev->is_busmaster)
8596 pci_set_master(pdev);
8598 /* Startup the kernel thread for this host adapter. */
8599 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8600 "lpfc_worker_%d", phba->brd_no);
8601 if (IS_ERR(phba->worker_thread)) {
8602 error = PTR_ERR(phba->worker_thread);
8603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8604 "0434 PM resume failed to start worker "
8605 "thread: error=x%x.\n", error);
8606 return error;
8609 /* Configure and enable interrupt */
8610 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
8611 if (intr_mode == LPFC_INTR_ERROR) {
8612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8613 "0430 PM resume Failed to enable interrupt\n");
8614 return -EIO;
8615 } else
8616 phba->intr_mode = intr_mode;
8618 /* Restart HBA and bring it online */
8619 lpfc_sli_brdrestart(phba);
8620 lpfc_online(phba);
8622 /* Log the current active interrupt mode */
8623 lpfc_log_intr_mode(phba, phba->intr_mode);
8625 return 0;
8629 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
8630 * @phba: pointer to lpfc hba data structure.
8632 * This routine is called to prepare the SLI3 device for PCI slot recover. It
8633 * aborts all the outstanding SCSI I/Os to the pci device.
8635 static void
8636 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
8638 struct lpfc_sli *psli = &phba->sli;
8639 struct lpfc_sli_ring *pring;
8641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8642 "2723 PCI channel I/O abort preparing for recovery\n");
8645 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8646 * and let the SCSI mid-layer to retry them to recover.
8648 pring = &psli->ring[psli->fcp_ring];
8649 lpfc_sli_abort_iocb_ring(phba, pring);
8653 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
8654 * @phba: pointer to lpfc hba data structure.
8656 * This routine is called to prepare the SLI3 device for PCI slot reset. It
8657 * disables the device interrupt and pci device, and aborts the internal FCP
8658 * pending I/Os.
8660 static void
8661 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
8663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8664 "2710 PCI channel disable preparing for reset\n");
8666 /* Block any management I/Os to the device */
8667 lpfc_block_mgmt_io(phba);
8669 /* Block all SCSI devices' I/Os on the host */
8670 lpfc_scsi_dev_block(phba);
8672 /* stop all timers */
8673 lpfc_stop_hba_timers(phba);
8675 /* Disable interrupt and pci device */
8676 lpfc_sli_disable_intr(phba);
8677 pci_disable_device(phba->pcidev);
8679 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8680 lpfc_sli_flush_fcp_rings(phba);
8684 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
8685 * @phba: pointer to lpfc hba data structure.
8687 * This routine is called to prepare the SLI3 device for PCI slot permanently
8688 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8689 * pending I/Os.
8691 static void
8692 lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
8694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8695 "2711 PCI channel permanent disable for failure\n");
8696 /* Block all SCSI devices' I/Os on the host */
8697 lpfc_scsi_dev_block(phba);
8699 /* stop all timers */
8700 lpfc_stop_hba_timers(phba);
8702 /* Clean up all driver's outstanding SCSI I/Os */
8703 lpfc_sli_flush_fcp_rings(phba);
8707 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
8708 * @pdev: pointer to PCI device.
8709 * @state: the current PCI connection state.
8711 * This routine is called from the PCI subsystem for I/O error handling to
8712 * device with SLI-3 interface spec. This function is called by the PCI
8713 * subsystem after a PCI bus error affecting this device has been detected.
8714 * When this function is invoked, it will need to stop all the I/Os and
8715 * interrupt(s) to the device. Once that is done, it will return
8716 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
8717 * as desired.
8719 * Return codes
8720 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
8721 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8722 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8724 static pci_ers_result_t
8725 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
8727 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8728 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8730 switch (state) {
8731 case pci_channel_io_normal:
8732 /* Non-fatal error, prepare for recovery */
8733 lpfc_sli_prep_dev_for_recover(phba);
8734 return PCI_ERS_RESULT_CAN_RECOVER;
8735 case pci_channel_io_frozen:
8736 /* Fatal error, prepare for slot reset */
8737 lpfc_sli_prep_dev_for_reset(phba);
8738 return PCI_ERS_RESULT_NEED_RESET;
8739 case pci_channel_io_perm_failure:
8740 /* Permanent failure, prepare for device down */
8741 lpfc_sli_prep_dev_for_perm_failure(phba);
8742 return PCI_ERS_RESULT_DISCONNECT;
8743 default:
8744 /* Unknown state, prepare and request slot reset */
8745 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8746 "0472 Unknown PCI error state: x%x\n", state);
8747 lpfc_sli_prep_dev_for_reset(phba);
8748 return PCI_ERS_RESULT_NEED_RESET;
8753 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
8754 * @pdev: pointer to PCI device.
8756 * This routine is called from the PCI subsystem for error handling to
8757 * device with SLI-3 interface spec. This is called after PCI bus has been
8758 * reset to restart the PCI card from scratch, as if from a cold-boot.
8759 * During the PCI subsystem error recovery, after driver returns
8760 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8761 * recovery and then call this routine before calling the .resume method
8762 * to recover the device. This function will initialize the HBA device,
8763 * enable the interrupt, but it will just put the HBA to offline state
8764 * without passing any I/O traffic.
8766 * Return codes
8767 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8768 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8770 static pci_ers_result_t
8771 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
8773 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8774 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8775 struct lpfc_sli *psli = &phba->sli;
8776 uint32_t intr_mode;
8778 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
8779 if (pci_enable_device_mem(pdev)) {
8780 printk(KERN_ERR "lpfc: Cannot re-enable "
8781 "PCI device after reset.\n");
8782 return PCI_ERS_RESULT_DISCONNECT;
8785 pci_restore_state(pdev);
8788 * As the new kernel behavior of pci_restore_state() API call clears
8789 * device saved_state flag, need to save the restored state again.
8791 pci_save_state(pdev);
8793 if (pdev->is_busmaster)
8794 pci_set_master(pdev);
8796 spin_lock_irq(&phba->hbalock);
8797 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
8798 spin_unlock_irq(&phba->hbalock);
8800 /* Configure and enable interrupt */
8801 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
8802 if (intr_mode == LPFC_INTR_ERROR) {
8803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8804 "0427 Cannot re-enable interrupt after "
8805 "slot reset.\n");
8806 return PCI_ERS_RESULT_DISCONNECT;
8807 } else
8808 phba->intr_mode = intr_mode;
8810 /* Take device offline, it will perform cleanup */
8811 lpfc_offline_prep(phba);
8812 lpfc_offline(phba);
8813 lpfc_sli_brdrestart(phba);
8815 /* Log the current active interrupt mode */
8816 lpfc_log_intr_mode(phba, phba->intr_mode);
8818 return PCI_ERS_RESULT_RECOVERED;
8822 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
8823 * @pdev: pointer to PCI device
8825 * This routine is called from the PCI subsystem for error handling to device
8826 * with SLI-3 interface spec. It is called when kernel error recovery tells
8827 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8828 * error recovery. After this call, traffic can start to flow from this device
8829 * again.
8831 static void
8832 lpfc_io_resume_s3(struct pci_dev *pdev)
8834 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8835 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8837 /* Bring device online, it will be no-op for non-fatal error resume */
8838 lpfc_online(phba);
8840 /* Clean up Advanced Error Reporting (AER) if needed */
8841 if (phba->hba_flag & HBA_AER_ENABLED)
8842 pci_cleanup_aer_uncorrect_error_status(pdev);
8846 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8847 * @phba: pointer to lpfc hba data structure.
8849 * returns the number of ELS/CT IOCBs to reserve
8852 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8854 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8856 if (phba->sli_rev == LPFC_SLI_REV4) {
8857 if (max_xri <= 100)
8858 return 10;
8859 else if (max_xri <= 256)
8860 return 25;
8861 else if (max_xri <= 512)
8862 return 50;
8863 else if (max_xri <= 1024)
8864 return 100;
8865 else
8866 return 150;
8867 } else
8868 return 0;
8872 * lpfc_write_firmware - attempt to write a firmware image to the port
8873 * @phba: pointer to lpfc hba data structure.
8874 * @fw: pointer to firmware image returned from request_firmware.
8876 * returns the number of bytes written if write is successful.
8877 * returns a negative error value if there were errors.
8878 * returns 0 if firmware matches currently active firmware on port.
8881 lpfc_write_firmware(struct lpfc_hba *phba, const struct firmware *fw)
8883 char fwrev[32];
8884 struct lpfc_grp_hdr *image = (struct lpfc_grp_hdr *)fw->data;
8885 struct list_head dma_buffer_list;
8886 int i, rc = 0;
8887 struct lpfc_dmabuf *dmabuf, *next;
8888 uint32_t offset = 0, temp_offset = 0;
8890 INIT_LIST_HEAD(&dma_buffer_list);
8891 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
8892 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
8893 LPFC_FILE_TYPE_GROUP) ||
8894 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
8895 (be32_to_cpu(image->size) != fw->size)) {
8896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8897 "3022 Invalid FW image found. "
8898 "Magic:%x Type:%x ID:%x\n",
8899 be32_to_cpu(image->magic_number),
8900 bf_get_be32(lpfc_grp_hdr_file_type, image),
8901 bf_get_be32(lpfc_grp_hdr_id, image));
8902 return -EINVAL;
8904 lpfc_decode_firmware_rev(phba, fwrev, 1);
8905 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
8906 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8907 "3023 Updating Firmware. Current Version:%s "
8908 "New Version:%s\n",
8909 fwrev, image->revision);
8910 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
8911 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
8912 GFP_KERNEL);
8913 if (!dmabuf) {
8914 rc = -ENOMEM;
8915 goto out;
8917 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
8918 SLI4_PAGE_SIZE,
8919 &dmabuf->phys,
8920 GFP_KERNEL);
8921 if (!dmabuf->virt) {
8922 kfree(dmabuf);
8923 rc = -ENOMEM;
8924 goto out;
8926 list_add_tail(&dmabuf->list, &dma_buffer_list);
8928 while (offset < fw->size) {
8929 temp_offset = offset;
8930 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
8931 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
8932 memcpy(dmabuf->virt,
8933 fw->data + temp_offset,
8934 fw->size - temp_offset);
8935 temp_offset = fw->size;
8936 break;
8938 memcpy(dmabuf->virt, fw->data + temp_offset,
8939 SLI4_PAGE_SIZE);
8940 temp_offset += SLI4_PAGE_SIZE;
8942 rc = lpfc_wr_object(phba, &dma_buffer_list,
8943 (fw->size - offset), &offset);
8944 if (rc) {
8945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8946 "3024 Firmware update failed. "
8947 "%d\n", rc);
8948 goto out;
8951 rc = offset;
8953 out:
8954 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
8955 list_del(&dmabuf->list);
8956 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
8957 dmabuf->virt, dmabuf->phys);
8958 kfree(dmabuf);
8960 return rc;
8964 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
8965 * @pdev: pointer to PCI device
8966 * @pid: pointer to PCI device identifier
8968 * This routine is called from the kernel's PCI subsystem to device with
8969 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8970 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8971 * information of the device and driver to see if the driver state that it
8972 * can support this kind of device. If the match is successful, the driver
8973 * core invokes this routine. If this routine determines it can claim the HBA,
8974 * it does all the initialization that it needs to do to handle the HBA
8975 * properly.
8977 * Return code
8978 * 0 - driver can claim the device
8979 * negative value - driver can not claim the device
8981 static int __devinit
8982 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8984 struct lpfc_hba *phba;
8985 struct lpfc_vport *vport = NULL;
8986 struct Scsi_Host *shost = NULL;
8987 int error;
8988 uint32_t cfg_mode, intr_mode;
8989 int mcnt;
8990 int adjusted_fcp_eq_count;
8991 int fcp_qidx;
8992 const struct firmware *fw;
8993 uint8_t file_name[16];
8995 /* Allocate memory for HBA structure */
8996 phba = lpfc_hba_alloc(pdev);
8997 if (!phba)
8998 return -ENOMEM;
9000 /* Perform generic PCI device enabling operation */
9001 error = lpfc_enable_pci_dev(phba);
9002 if (error)
9003 goto out_free_phba;
9005 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9006 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9007 if (error)
9008 goto out_disable_pci_dev;
9010 /* Set up SLI-4 specific device PCI memory space */
9011 error = lpfc_sli4_pci_mem_setup(phba);
9012 if (error) {
9013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9014 "1410 Failed to set up pci memory space.\n");
9015 goto out_disable_pci_dev;
9018 /* Set up phase-1 common device driver resources */
9019 error = lpfc_setup_driver_resource_phase1(phba);
9020 if (error) {
9021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9022 "1411 Failed to set up driver resource.\n");
9023 goto out_unset_pci_mem_s4;
9026 /* Set up SLI-4 Specific device driver resources */
9027 error = lpfc_sli4_driver_resource_setup(phba);
9028 if (error) {
9029 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9030 "1412 Failed to set up driver resource.\n");
9031 goto out_unset_pci_mem_s4;
9034 /* Initialize and populate the iocb list per host */
9036 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9037 "2821 initialize iocb list %d.\n",
9038 phba->cfg_iocb_cnt*1024);
9039 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9041 if (error) {
9042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9043 "1413 Failed to initialize iocb list.\n");
9044 goto out_unset_driver_resource_s4;
9047 INIT_LIST_HEAD(&phba->active_rrq_list);
9048 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
9050 /* Set up common device driver resources */
9051 error = lpfc_setup_driver_resource_phase2(phba);
9052 if (error) {
9053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9054 "1414 Failed to set up driver resource.\n");
9055 goto out_free_iocb_list;
9058 /* Get the default values for Model Name and Description */
9059 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9061 /* Create SCSI host to the physical port */
9062 error = lpfc_create_shost(phba);
9063 if (error) {
9064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9065 "1415 Failed to create scsi host.\n");
9066 goto out_unset_driver_resource;
9069 /* Configure sysfs attributes */
9070 vport = phba->pport;
9071 error = lpfc_alloc_sysfs_attr(vport);
9072 if (error) {
9073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9074 "1416 Failed to allocate sysfs attr\n");
9075 goto out_destroy_shost;
9078 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
9079 /* Now, trying to enable interrupt and bring up the device */
9080 cfg_mode = phba->cfg_use_msi;
9081 while (true) {
9082 /* Put device to a known state before enabling interrupt */
9083 lpfc_stop_port(phba);
9084 /* Configure and enable interrupt */
9085 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9086 if (intr_mode == LPFC_INTR_ERROR) {
9087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9088 "0426 Failed to enable interrupt.\n");
9089 error = -ENODEV;
9090 goto out_free_sysfs_attr;
9092 /* Default to single EQ for non-MSI-X */
9093 if (phba->intr_type != MSIX)
9094 adjusted_fcp_eq_count = 0;
9095 else if (phba->sli4_hba.msix_vec_nr <
9096 phba->cfg_fcp_eq_count + 1)
9097 adjusted_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
9098 else
9099 adjusted_fcp_eq_count = phba->cfg_fcp_eq_count;
9100 /* Free unused EQs */
9101 for (fcp_qidx = adjusted_fcp_eq_count;
9102 fcp_qidx < phba->cfg_fcp_eq_count;
9103 fcp_qidx++) {
9104 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
9105 /* do not delete the first fcp_cq */
9106 if (fcp_qidx)
9107 lpfc_sli4_queue_free(
9108 phba->sli4_hba.fcp_cq[fcp_qidx]);
9110 phba->cfg_fcp_eq_count = adjusted_fcp_eq_count;
9111 /* Set up SLI-4 HBA */
9112 if (lpfc_sli4_hba_setup(phba)) {
9113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9114 "1421 Failed to set up hba\n");
9115 error = -ENODEV;
9116 goto out_disable_intr;
9119 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
9120 if (intr_mode != 0)
9121 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
9122 LPFC_ACT_INTR_CNT);
9124 /* Check active interrupts received only for MSI/MSI-X */
9125 if (intr_mode == 0 ||
9126 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
9127 /* Log the current active interrupt mode */
9128 phba->intr_mode = intr_mode;
9129 lpfc_log_intr_mode(phba, intr_mode);
9130 break;
9132 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9133 "0451 Configure interrupt mode (%d) "
9134 "failed active interrupt test.\n",
9135 intr_mode);
9136 /* Unset the previous SLI-4 HBA setup. */
9138 * TODO: Is this operation compatible with IF TYPE 2
9139 * devices? All port state is deleted and cleared.
9141 lpfc_sli4_unset_hba(phba);
9142 /* Try next level of interrupt mode */
9143 cfg_mode = --intr_mode;
9146 /* Perform post initialization setup */
9147 lpfc_post_init_setup(phba);
9149 /* check for firmware upgrade or downgrade */
9150 snprintf(file_name, 16, "%s.grp", phba->ModelName);
9151 error = request_firmware(&fw, file_name, &phba->pcidev->dev);
9152 if (!error) {
9153 lpfc_write_firmware(phba, fw);
9154 release_firmware(fw);
9157 /* Check if there are static vports to be created. */
9158 lpfc_create_static_vport(phba);
9159 return 0;
9161 out_disable_intr:
9162 lpfc_sli4_disable_intr(phba);
9163 out_free_sysfs_attr:
9164 lpfc_free_sysfs_attr(vport);
9165 out_destroy_shost:
9166 lpfc_destroy_shost(phba);
9167 out_unset_driver_resource:
9168 lpfc_unset_driver_resource_phase2(phba);
9169 out_free_iocb_list:
9170 lpfc_free_iocb_list(phba);
9171 out_unset_driver_resource_s4:
9172 lpfc_sli4_driver_resource_unset(phba);
9173 out_unset_pci_mem_s4:
9174 lpfc_sli4_pci_mem_unset(phba);
9175 out_disable_pci_dev:
9176 lpfc_disable_pci_dev(phba);
9177 if (shost)
9178 scsi_host_put(shost);
9179 out_free_phba:
9180 lpfc_hba_free(phba);
9181 return error;
9185 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9186 * @pdev: pointer to PCI device
9188 * This routine is called from the kernel's PCI subsystem to device with
9189 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9190 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9191 * device to be removed from the PCI subsystem properly.
9193 static void __devexit
9194 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9196 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9197 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9198 struct lpfc_vport **vports;
9199 struct lpfc_hba *phba = vport->phba;
9200 int i;
9202 /* Mark the device unloading flag */
9203 spin_lock_irq(&phba->hbalock);
9204 vport->load_flag |= FC_UNLOADING;
9205 spin_unlock_irq(&phba->hbalock);
9207 /* Free the HBA sysfs attributes */
9208 lpfc_free_sysfs_attr(vport);
9210 /* Release all the vports against this physical port */
9211 vports = lpfc_create_vport_work_array(phba);
9212 if (vports != NULL)
9213 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
9214 fc_vport_terminate(vports[i]->fc_vport);
9215 lpfc_destroy_vport_work_array(phba, vports);
9217 /* Remove FC host and then SCSI host with the physical port */
9218 fc_remove_host(shost);
9219 scsi_remove_host(shost);
9221 /* Perform cleanup on the physical port */
9222 lpfc_cleanup(vport);
9225 * Bring down the SLI Layer. This step disables all interrupts,
9226 * clears the rings, discards all mailbox commands, and resets
9227 * the HBA FCoE function.
9229 lpfc_debugfs_terminate(vport);
9230 lpfc_sli4_hba_unset(phba);
9232 spin_lock_irq(&phba->hbalock);
9233 list_del_init(&vport->listentry);
9234 spin_unlock_irq(&phba->hbalock);
9236 /* Perform scsi free before driver resource_unset since scsi
9237 * buffers are released to their corresponding pools here.
9239 lpfc_scsi_free(phba);
9240 lpfc_sli4_driver_resource_unset(phba);
9242 /* Unmap adapter Control and Doorbell registers */
9243 lpfc_sli4_pci_mem_unset(phba);
9245 /* Release PCI resources and disable device's PCI function */
9246 scsi_host_put(shost);
9247 lpfc_disable_pci_dev(phba);
9249 /* Finally, free the driver's device data structure */
9250 lpfc_hba_free(phba);
9252 return;
9256 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9257 * @pdev: pointer to PCI device
9258 * @msg: power management message
9260 * This routine is called from the kernel's PCI subsystem to support system
9261 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9262 * this method, it quiesces the device by stopping the driver's worker
9263 * thread for the device, turning off device's interrupt and DMA, and bring
9264 * the device offline. Note that as the driver implements the minimum PM
9265 * requirements to a power-aware driver's PM support for suspend/resume -- all
9266 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9267 * method call will be treated as SUSPEND and the driver will fully
9268 * reinitialize its device during resume() method call, the driver will set
9269 * device to PCI_D3hot state in PCI config space instead of setting it
9270 * according to the @msg provided by the PM.
9272 * Return code
9273 * 0 - driver suspended the device
9274 * Error otherwise
9276 static int
9277 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9279 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9280 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9282 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9283 "2843 PCI device Power Management suspend.\n");
9285 /* Bring down the device */
9286 lpfc_offline_prep(phba);
9287 lpfc_offline(phba);
9288 kthread_stop(phba->worker_thread);
9290 /* Disable interrupt from device */
9291 lpfc_sli4_disable_intr(phba);
9293 /* Save device state to PCI config space */
9294 pci_save_state(pdev);
9295 pci_set_power_state(pdev, PCI_D3hot);
9297 return 0;
9301 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9302 * @pdev: pointer to PCI device
9304 * This routine is called from the kernel's PCI subsystem to support system
9305 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9306 * this method, it restores the device's PCI config space state and fully
9307 * reinitializes the device and brings it online. Note that as the driver
9308 * implements the minimum PM requirements to a power-aware driver's PM for
9309 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9310 * to the suspend() method call will be treated as SUSPEND and the driver
9311 * will fully reinitialize its device during resume() method call, the device
9312 * will be set to PCI_D0 directly in PCI config space before restoring the
9313 * state.
9315 * Return code
9316 * 0 - driver suspended the device
9317 * Error otherwise
9319 static int
9320 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9322 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9323 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9324 uint32_t intr_mode;
9325 int error;
9327 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9328 "0292 PCI device Power Management resume.\n");
9330 /* Restore device state from PCI config space */
9331 pci_set_power_state(pdev, PCI_D0);
9332 pci_restore_state(pdev);
9335 * As the new kernel behavior of pci_restore_state() API call clears
9336 * device saved_state flag, need to save the restored state again.
9338 pci_save_state(pdev);
9340 if (pdev->is_busmaster)
9341 pci_set_master(pdev);
9343 /* Startup the kernel thread for this host adapter. */
9344 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9345 "lpfc_worker_%d", phba->brd_no);
9346 if (IS_ERR(phba->worker_thread)) {
9347 error = PTR_ERR(phba->worker_thread);
9348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9349 "0293 PM resume failed to start worker "
9350 "thread: error=x%x.\n", error);
9351 return error;
9354 /* Configure and enable interrupt */
9355 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9356 if (intr_mode == LPFC_INTR_ERROR) {
9357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9358 "0294 PM resume Failed to enable interrupt\n");
9359 return -EIO;
9360 } else
9361 phba->intr_mode = intr_mode;
9363 /* Restart HBA and bring it online */
9364 lpfc_sli_brdrestart(phba);
9365 lpfc_online(phba);
9367 /* Log the current active interrupt mode */
9368 lpfc_log_intr_mode(phba, phba->intr_mode);
9370 return 0;
9374 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9375 * @phba: pointer to lpfc hba data structure.
9377 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9378 * aborts all the outstanding SCSI I/Os to the pci device.
9380 static void
9381 lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9383 struct lpfc_sli *psli = &phba->sli;
9384 struct lpfc_sli_ring *pring;
9386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9387 "2828 PCI channel I/O abort preparing for recovery\n");
9389 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9390 * and let the SCSI mid-layer to retry them to recover.
9392 pring = &psli->ring[psli->fcp_ring];
9393 lpfc_sli_abort_iocb_ring(phba, pring);
9397 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9398 * @phba: pointer to lpfc hba data structure.
9400 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9401 * disables the device interrupt and pci device, and aborts the internal FCP
9402 * pending I/Os.
9404 static void
9405 lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9408 "2826 PCI channel disable preparing for reset\n");
9410 /* Block any management I/Os to the device */
9411 lpfc_block_mgmt_io(phba);
9413 /* Block all SCSI devices' I/Os on the host */
9414 lpfc_scsi_dev_block(phba);
9416 /* stop all timers */
9417 lpfc_stop_hba_timers(phba);
9419 /* Disable interrupt and pci device */
9420 lpfc_sli4_disable_intr(phba);
9421 pci_disable_device(phba->pcidev);
9423 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9424 lpfc_sli_flush_fcp_rings(phba);
9428 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9429 * @phba: pointer to lpfc hba data structure.
9431 * This routine is called to prepare the SLI4 device for PCI slot permanently
9432 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9433 * pending I/Os.
9435 static void
9436 lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9439 "2827 PCI channel permanent disable for failure\n");
9441 /* Block all SCSI devices' I/Os on the host */
9442 lpfc_scsi_dev_block(phba);
9444 /* stop all timers */
9445 lpfc_stop_hba_timers(phba);
9447 /* Clean up all driver's outstanding SCSI I/Os */
9448 lpfc_sli_flush_fcp_rings(phba);
9452 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9453 * @pdev: pointer to PCI device.
9454 * @state: the current PCI connection state.
9456 * This routine is called from the PCI subsystem for error handling to device
9457 * with SLI-4 interface spec. This function is called by the PCI subsystem
9458 * after a PCI bus error affecting this device has been detected. When this
9459 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9460 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9461 * for the PCI subsystem to perform proper recovery as desired.
9463 * Return codes
9464 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9465 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9467 static pci_ers_result_t
9468 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9470 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9471 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9473 switch (state) {
9474 case pci_channel_io_normal:
9475 /* Non-fatal error, prepare for recovery */
9476 lpfc_sli4_prep_dev_for_recover(phba);
9477 return PCI_ERS_RESULT_CAN_RECOVER;
9478 case pci_channel_io_frozen:
9479 /* Fatal error, prepare for slot reset */
9480 lpfc_sli4_prep_dev_for_reset(phba);
9481 return PCI_ERS_RESULT_NEED_RESET;
9482 case pci_channel_io_perm_failure:
9483 /* Permanent failure, prepare for device down */
9484 lpfc_sli4_prep_dev_for_perm_failure(phba);
9485 return PCI_ERS_RESULT_DISCONNECT;
9486 default:
9487 /* Unknown state, prepare and request slot reset */
9488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9489 "2825 Unknown PCI error state: x%x\n", state);
9490 lpfc_sli4_prep_dev_for_reset(phba);
9491 return PCI_ERS_RESULT_NEED_RESET;
9496 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9497 * @pdev: pointer to PCI device.
9499 * This routine is called from the PCI subsystem for error handling to device
9500 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9501 * restart the PCI card from scratch, as if from a cold-boot. During the
9502 * PCI subsystem error recovery, after the driver returns
9503 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9504 * recovery and then call this routine before calling the .resume method to
9505 * recover the device. This function will initialize the HBA device, enable
9506 * the interrupt, but it will just put the HBA to offline state without
9507 * passing any I/O traffic.
9509 * Return codes
9510 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9511 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9513 static pci_ers_result_t
9514 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9516 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9517 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9518 struct lpfc_sli *psli = &phba->sli;
9519 uint32_t intr_mode;
9521 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9522 if (pci_enable_device_mem(pdev)) {
9523 printk(KERN_ERR "lpfc: Cannot re-enable "
9524 "PCI device after reset.\n");
9525 return PCI_ERS_RESULT_DISCONNECT;
9528 pci_restore_state(pdev);
9531 * As the new kernel behavior of pci_restore_state() API call clears
9532 * device saved_state flag, need to save the restored state again.
9534 pci_save_state(pdev);
9536 if (pdev->is_busmaster)
9537 pci_set_master(pdev);
9539 spin_lock_irq(&phba->hbalock);
9540 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9541 spin_unlock_irq(&phba->hbalock);
9543 /* Configure and enable interrupt */
9544 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9545 if (intr_mode == LPFC_INTR_ERROR) {
9546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9547 "2824 Cannot re-enable interrupt after "
9548 "slot reset.\n");
9549 return PCI_ERS_RESULT_DISCONNECT;
9550 } else
9551 phba->intr_mode = intr_mode;
9553 /* Log the current active interrupt mode */
9554 lpfc_log_intr_mode(phba, phba->intr_mode);
9556 return PCI_ERS_RESULT_RECOVERED;
9560 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
9561 * @pdev: pointer to PCI device
9563 * This routine is called from the PCI subsystem for error handling to device
9564 * with SLI-4 interface spec. It is called when kernel error recovery tells
9565 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9566 * error recovery. After this call, traffic can start to flow from this device
9567 * again.
9569 static void
9570 lpfc_io_resume_s4(struct pci_dev *pdev)
9572 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9573 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9576 * In case of slot reset, as function reset is performed through
9577 * mailbox command which needs DMA to be enabled, this operation
9578 * has to be moved to the io resume phase. Taking device offline
9579 * will perform the necessary cleanup.
9581 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
9582 /* Perform device reset */
9583 lpfc_offline_prep(phba);
9584 lpfc_offline(phba);
9585 lpfc_sli_brdrestart(phba);
9586 /* Bring the device back online */
9587 lpfc_online(phba);
9590 /* Clean up Advanced Error Reporting (AER) if needed */
9591 if (phba->hba_flag & HBA_AER_ENABLED)
9592 pci_cleanup_aer_uncorrect_error_status(pdev);
9596 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
9597 * @pdev: pointer to PCI device
9598 * @pid: pointer to PCI device identifier
9600 * This routine is to be registered to the kernel's PCI subsystem. When an
9601 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
9602 * at PCI device-specific information of the device and driver to see if the
9603 * driver state that it can support this kind of device. If the match is
9604 * successful, the driver core invokes this routine. This routine dispatches
9605 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
9606 * do all the initialization that it needs to do to handle the HBA device
9607 * properly.
9609 * Return code
9610 * 0 - driver can claim the device
9611 * negative value - driver can not claim the device
9613 static int __devinit
9614 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
9616 int rc;
9617 struct lpfc_sli_intf intf;
9619 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
9620 return -ENODEV;
9622 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
9623 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
9624 rc = lpfc_pci_probe_one_s4(pdev, pid);
9625 else
9626 rc = lpfc_pci_probe_one_s3(pdev, pid);
9628 return rc;
9632 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
9633 * @pdev: pointer to PCI device
9635 * This routine is to be registered to the kernel's PCI subsystem. When an
9636 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
9637 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
9638 * remove routine, which will perform all the necessary cleanup for the
9639 * device to be removed from the PCI subsystem properly.
9641 static void __devexit
9642 lpfc_pci_remove_one(struct pci_dev *pdev)
9644 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9645 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9647 switch (phba->pci_dev_grp) {
9648 case LPFC_PCI_DEV_LP:
9649 lpfc_pci_remove_one_s3(pdev);
9650 break;
9651 case LPFC_PCI_DEV_OC:
9652 lpfc_pci_remove_one_s4(pdev);
9653 break;
9654 default:
9655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9656 "1424 Invalid PCI device group: 0x%x\n",
9657 phba->pci_dev_grp);
9658 break;
9660 return;
9664 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
9665 * @pdev: pointer to PCI device
9666 * @msg: power management message
9668 * This routine is to be registered to the kernel's PCI subsystem to support
9669 * system Power Management (PM). When PM invokes this method, it dispatches
9670 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
9671 * suspend the device.
9673 * Return code
9674 * 0 - driver suspended the device
9675 * Error otherwise
9677 static int
9678 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
9680 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9681 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9682 int rc = -ENODEV;
9684 switch (phba->pci_dev_grp) {
9685 case LPFC_PCI_DEV_LP:
9686 rc = lpfc_pci_suspend_one_s3(pdev, msg);
9687 break;
9688 case LPFC_PCI_DEV_OC:
9689 rc = lpfc_pci_suspend_one_s4(pdev, msg);
9690 break;
9691 default:
9692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9693 "1425 Invalid PCI device group: 0x%x\n",
9694 phba->pci_dev_grp);
9695 break;
9697 return rc;
9701 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
9702 * @pdev: pointer to PCI device
9704 * This routine is to be registered to the kernel's PCI subsystem to support
9705 * system Power Management (PM). When PM invokes this method, it dispatches
9706 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
9707 * resume the device.
9709 * Return code
9710 * 0 - driver suspended the device
9711 * Error otherwise
9713 static int
9714 lpfc_pci_resume_one(struct pci_dev *pdev)
9716 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9717 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9718 int rc = -ENODEV;
9720 switch (phba->pci_dev_grp) {
9721 case LPFC_PCI_DEV_LP:
9722 rc = lpfc_pci_resume_one_s3(pdev);
9723 break;
9724 case LPFC_PCI_DEV_OC:
9725 rc = lpfc_pci_resume_one_s4(pdev);
9726 break;
9727 default:
9728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9729 "1426 Invalid PCI device group: 0x%x\n",
9730 phba->pci_dev_grp);
9731 break;
9733 return rc;
9737 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
9738 * @pdev: pointer to PCI device.
9739 * @state: the current PCI connection state.
9741 * This routine is registered to the PCI subsystem for error handling. This
9742 * function is called by the PCI subsystem after a PCI bus error affecting
9743 * this device has been detected. When this routine is invoked, it dispatches
9744 * the action to the proper SLI-3 or SLI-4 device error detected handling
9745 * routine, which will perform the proper error detected operation.
9747 * Return codes
9748 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9749 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9751 static pci_ers_result_t
9752 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9754 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9755 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9756 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9758 switch (phba->pci_dev_grp) {
9759 case LPFC_PCI_DEV_LP:
9760 rc = lpfc_io_error_detected_s3(pdev, state);
9761 break;
9762 case LPFC_PCI_DEV_OC:
9763 rc = lpfc_io_error_detected_s4(pdev, state);
9764 break;
9765 default:
9766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9767 "1427 Invalid PCI device group: 0x%x\n",
9768 phba->pci_dev_grp);
9769 break;
9771 return rc;
9775 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
9776 * @pdev: pointer to PCI device.
9778 * This routine is registered to the PCI subsystem for error handling. This
9779 * function is called after PCI bus has been reset to restart the PCI card
9780 * from scratch, as if from a cold-boot. When this routine is invoked, it
9781 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
9782 * routine, which will perform the proper device reset.
9784 * Return codes
9785 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9786 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9788 static pci_ers_result_t
9789 lpfc_io_slot_reset(struct pci_dev *pdev)
9791 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9792 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9793 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9795 switch (phba->pci_dev_grp) {
9796 case LPFC_PCI_DEV_LP:
9797 rc = lpfc_io_slot_reset_s3(pdev);
9798 break;
9799 case LPFC_PCI_DEV_OC:
9800 rc = lpfc_io_slot_reset_s4(pdev);
9801 break;
9802 default:
9803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9804 "1428 Invalid PCI device group: 0x%x\n",
9805 phba->pci_dev_grp);
9806 break;
9808 return rc;
9812 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
9813 * @pdev: pointer to PCI device
9815 * This routine is registered to the PCI subsystem for error handling. It
9816 * is called when kernel error recovery tells the lpfc driver that it is
9817 * OK to resume normal PCI operation after PCI bus error recovery. When
9818 * this routine is invoked, it dispatches the action to the proper SLI-3
9819 * or SLI-4 device io_resume routine, which will resume the device operation.
9821 static void
9822 lpfc_io_resume(struct pci_dev *pdev)
9824 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9825 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9827 switch (phba->pci_dev_grp) {
9828 case LPFC_PCI_DEV_LP:
9829 lpfc_io_resume_s3(pdev);
9830 break;
9831 case LPFC_PCI_DEV_OC:
9832 lpfc_io_resume_s4(pdev);
9833 break;
9834 default:
9835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9836 "1429 Invalid PCI device group: 0x%x\n",
9837 phba->pci_dev_grp);
9838 break;
9840 return;
9843 static struct pci_device_id lpfc_id_table[] = {
9844 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
9845 PCI_ANY_ID, PCI_ANY_ID, },
9846 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
9847 PCI_ANY_ID, PCI_ANY_ID, },
9848 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
9849 PCI_ANY_ID, PCI_ANY_ID, },
9850 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
9851 PCI_ANY_ID, PCI_ANY_ID, },
9852 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
9853 PCI_ANY_ID, PCI_ANY_ID, },
9854 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
9855 PCI_ANY_ID, PCI_ANY_ID, },
9856 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
9857 PCI_ANY_ID, PCI_ANY_ID, },
9858 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
9859 PCI_ANY_ID, PCI_ANY_ID, },
9860 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
9861 PCI_ANY_ID, PCI_ANY_ID, },
9862 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
9863 PCI_ANY_ID, PCI_ANY_ID, },
9864 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
9865 PCI_ANY_ID, PCI_ANY_ID, },
9866 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
9867 PCI_ANY_ID, PCI_ANY_ID, },
9868 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
9869 PCI_ANY_ID, PCI_ANY_ID, },
9870 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
9871 PCI_ANY_ID, PCI_ANY_ID, },
9872 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
9873 PCI_ANY_ID, PCI_ANY_ID, },
9874 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
9875 PCI_ANY_ID, PCI_ANY_ID, },
9876 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
9877 PCI_ANY_ID, PCI_ANY_ID, },
9878 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
9879 PCI_ANY_ID, PCI_ANY_ID, },
9880 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
9881 PCI_ANY_ID, PCI_ANY_ID, },
9882 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
9883 PCI_ANY_ID, PCI_ANY_ID, },
9884 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
9885 PCI_ANY_ID, PCI_ANY_ID, },
9886 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
9887 PCI_ANY_ID, PCI_ANY_ID, },
9888 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
9889 PCI_ANY_ID, PCI_ANY_ID, },
9890 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
9891 PCI_ANY_ID, PCI_ANY_ID, },
9892 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
9893 PCI_ANY_ID, PCI_ANY_ID, },
9894 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
9895 PCI_ANY_ID, PCI_ANY_ID, },
9896 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
9897 PCI_ANY_ID, PCI_ANY_ID, },
9898 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
9899 PCI_ANY_ID, PCI_ANY_ID, },
9900 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
9901 PCI_ANY_ID, PCI_ANY_ID, },
9902 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
9903 PCI_ANY_ID, PCI_ANY_ID, },
9904 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
9905 PCI_ANY_ID, PCI_ANY_ID, },
9906 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
9907 PCI_ANY_ID, PCI_ANY_ID, },
9908 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
9909 PCI_ANY_ID, PCI_ANY_ID, },
9910 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
9911 PCI_ANY_ID, PCI_ANY_ID, },
9912 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
9913 PCI_ANY_ID, PCI_ANY_ID, },
9914 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
9915 PCI_ANY_ID, PCI_ANY_ID, },
9916 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
9917 PCI_ANY_ID, PCI_ANY_ID, },
9918 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
9919 PCI_ANY_ID, PCI_ANY_ID, },
9920 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
9921 PCI_ANY_ID, PCI_ANY_ID, },
9922 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
9923 PCI_ANY_ID, PCI_ANY_ID, },
9924 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
9925 PCI_ANY_ID, PCI_ANY_ID, },
9926 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
9927 PCI_ANY_ID, PCI_ANY_ID, },
9928 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
9929 PCI_ANY_ID, PCI_ANY_ID, },
9930 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
9931 PCI_ANY_ID, PCI_ANY_ID, },
9932 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
9933 PCI_ANY_ID, PCI_ANY_ID, },
9934 { 0 }
9937 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
9939 static struct pci_error_handlers lpfc_err_handler = {
9940 .error_detected = lpfc_io_error_detected,
9941 .slot_reset = lpfc_io_slot_reset,
9942 .resume = lpfc_io_resume,
9945 static struct pci_driver lpfc_driver = {
9946 .name = LPFC_DRIVER_NAME,
9947 .id_table = lpfc_id_table,
9948 .probe = lpfc_pci_probe_one,
9949 .remove = __devexit_p(lpfc_pci_remove_one),
9950 .suspend = lpfc_pci_suspend_one,
9951 .resume = lpfc_pci_resume_one,
9952 .err_handler = &lpfc_err_handler,
9956 * lpfc_init - lpfc module initialization routine
9958 * This routine is to be invoked when the lpfc module is loaded into the
9959 * kernel. The special kernel macro module_init() is used to indicate the
9960 * role of this routine to the kernel as lpfc module entry point.
9962 * Return codes
9963 * 0 - successful
9964 * -ENOMEM - FC attach transport failed
9965 * all others - failed
9967 static int __init
9968 lpfc_init(void)
9970 int error = 0;
9972 printk(LPFC_MODULE_DESC "\n");
9973 printk(LPFC_COPYRIGHT "\n");
9975 if (lpfc_enable_npiv) {
9976 lpfc_transport_functions.vport_create = lpfc_vport_create;
9977 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
9979 lpfc_transport_template =
9980 fc_attach_transport(&lpfc_transport_functions);
9981 if (lpfc_transport_template == NULL)
9982 return -ENOMEM;
9983 if (lpfc_enable_npiv) {
9984 lpfc_vport_transport_template =
9985 fc_attach_transport(&lpfc_vport_transport_functions);
9986 if (lpfc_vport_transport_template == NULL) {
9987 fc_release_transport(lpfc_transport_template);
9988 return -ENOMEM;
9991 error = pci_register_driver(&lpfc_driver);
9992 if (error) {
9993 fc_release_transport(lpfc_transport_template);
9994 if (lpfc_enable_npiv)
9995 fc_release_transport(lpfc_vport_transport_template);
9998 return error;
10002 * lpfc_exit - lpfc module removal routine
10004 * This routine is invoked when the lpfc module is removed from the kernel.
10005 * The special kernel macro module_exit() is used to indicate the role of
10006 * this routine to the kernel as lpfc module exit point.
10008 static void __exit
10009 lpfc_exit(void)
10011 pci_unregister_driver(&lpfc_driver);
10012 fc_release_transport(lpfc_transport_template);
10013 if (lpfc_enable_npiv)
10014 fc_release_transport(lpfc_vport_transport_template);
10015 if (_dump_buf_data) {
10016 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10017 "_dump_buf_data at 0x%p\n",
10018 (1L << _dump_buf_data_order), _dump_buf_data);
10019 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10022 if (_dump_buf_dif) {
10023 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10024 "_dump_buf_dif at 0x%p\n",
10025 (1L << _dump_buf_dif_order), _dump_buf_dif);
10026 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10030 module_init(lpfc_init);
10031 module_exit(lpfc_exit);
10032 MODULE_LICENSE("GPL");
10033 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10034 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10035 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);