Back to home page

OSCL-LXR

 
 

    


0001 /*******************************************************************
0002  * This file is part of the Emulex Linux Device Driver for         *
0003  * Fibre Channel Host Bus Adapters.                                *
0004  * Copyright (C) 2017-2022 Broadcom. All Rights Reserved. The term *
0005  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
0006  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
0007  * EMULEX and SLI are trademarks of Emulex.                        *
0008  * www.broadcom.com                                                *
0009  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
0010  *                                                                 *
0011  * This program is free software; you can redistribute it and/or   *
0012  * modify it under the terms of version 2 of the GNU General       *
0013  * Public License as published by the Free Software Foundation.    *
0014  * This program is distributed in the hope that it will be useful. *
0015  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
0016  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
0017  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
0018  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
0019  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
0020  * more details, a copy of which can be found in the file COPYING  *
0021  * included with this package.                                     *
0022  *******************************************************************/
0023 
0024 #include <linux/blkdev.h>
0025 #include <linux/pci.h>
0026 #include <linux/slab.h>
0027 #include <linux/interrupt.h>
0028 
0029 #include <scsi/scsi_device.h>
0030 #include <scsi/scsi_transport_fc.h>
0031 #include <scsi/scsi.h>
0032 #include <scsi/fc/fc_fs.h>
0033 
0034 #include "lpfc_hw4.h"
0035 #include "lpfc_hw.h"
0036 #include "lpfc_sli.h"
0037 #include "lpfc_sli4.h"
0038 #include "lpfc_nl.h"
0039 #include "lpfc_disc.h"
0040 #include "lpfc_scsi.h"
0041 #include "lpfc.h"
0042 #include "lpfc_logmsg.h"
0043 #include "lpfc_crtn.h"
0044 #include "lpfc_compat.h"
0045 
0046 /**
0047  * lpfc_mbox_rsrc_prep - Prepare a mailbox with DMA buffer memory.
0048  * @phba: pointer to lpfc hba data structure.
0049  * @mbox: pointer to the driver internal queue element for mailbox command.
0050  *
0051  * A mailbox command consists of the pool memory for the command, @mbox, and
0052  * one or more DMA buffers for the data transfer.  This routine provides
0053  * a standard framework for allocating the dma buffer and assigning to the
0054  * @mbox.  Callers should cleanup the mbox with a call to
0055  * lpfc_mbox_rsrc_cleanup.
0056  *
0057  * The lpfc_mbuf_alloc routine acquires the hbalock so the caller is
0058  * responsible to ensure the hbalock is released.  Also note that the
0059  * driver design is a single dmabuf/mbuf per mbox in the ctx_buf.
0060  *
0061  **/
0062 int
0063 lpfc_mbox_rsrc_prep(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
0064 {
0065     struct lpfc_dmabuf *mp;
0066 
0067     mp = kmalloc(sizeof(*mp), GFP_KERNEL);
0068     if (!mp)
0069         return -ENOMEM;
0070 
0071     mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
0072     if (!mp->virt) {
0073         kfree(mp);
0074         return -ENOMEM;
0075     }
0076 
0077     memset(mp->virt, 0, LPFC_BPL_SIZE);
0078 
0079     /* Initialization only.  Driver does not use a list of dmabufs. */
0080     INIT_LIST_HEAD(&mp->list);
0081     mbox->ctx_buf = mp;
0082     return 0;
0083 }
0084 
0085 /**
0086  * lpfc_mbox_rsrc_cleanup - Free the mailbox DMA buffer and virtual memory.
0087  * @phba: pointer to lpfc hba data structure.
0088  * @mbox: pointer to the driver internal queue element for mailbox command.
0089  * @locked: value that indicates if the hbalock is held (1) or not (0).
0090  *
0091  * A mailbox command consists of the pool memory for the command, @mbox, and
0092  * possibly a DMA buffer for the data transfer.  This routine provides
0093  * a standard framework for releasing any dma buffers and freeing all
0094  * memory resources in it as well as releasing the @mbox back to the @phba pool.
0095  * Callers should use this routine for cleanup for all mailboxes prepped with
0096  * lpfc_mbox_rsrc_prep.
0097  *
0098  **/
0099 void
0100 lpfc_mbox_rsrc_cleanup(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
0101                enum lpfc_mbox_ctx locked)
0102 {
0103     struct lpfc_dmabuf *mp;
0104 
0105     mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
0106     mbox->ctx_buf = NULL;
0107 
0108     /* Release the generic BPL buffer memory.  */
0109     if (mp) {
0110         if (locked == MBOX_THD_LOCKED)
0111             __lpfc_mbuf_free(phba, mp->virt, mp->phys);
0112         else
0113             lpfc_mbuf_free(phba, mp->virt, mp->phys);
0114         kfree(mp);
0115     }
0116 
0117     mempool_free(mbox, phba->mbox_mem_pool);
0118 }
0119 
0120 /**
0121  * lpfc_dump_static_vport - Dump HBA's static vport information.
0122  * @phba: pointer to lpfc hba data structure.
0123  * @pmb: pointer to the driver internal queue element for mailbox command.
0124  * @offset: offset for dumping vport info.
0125  *
0126  * The dump mailbox command provides a method for the device driver to obtain
0127  * various types of information from the HBA device.
0128  *
0129  * This routine prepares the mailbox command for dumping list of static
0130  * vports to be created.
0131  **/
0132 int
0133 lpfc_dump_static_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
0134         uint16_t offset)
0135 {
0136     MAILBOX_t *mb;
0137     struct lpfc_dmabuf *mp;
0138     int rc;
0139 
0140     mb = &pmb->u.mb;
0141 
0142     /* Setup to dump vport info region */
0143     memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
0144     mb->mbxCommand = MBX_DUMP_MEMORY;
0145     mb->un.varDmp.type = DMP_NV_PARAMS;
0146     mb->un.varDmp.entry_index = offset;
0147     mb->un.varDmp.region_id = DMP_REGION_VPORT;
0148     mb->mbxOwner = OWN_HOST;
0149 
0150     /* For SLI3 HBAs data is embedded in mailbox */
0151     if (phba->sli_rev != LPFC_SLI_REV4) {
0152         mb->un.varDmp.cv = 1;
0153         mb->un.varDmp.word_cnt = DMP_RSP_SIZE/sizeof(uint32_t);
0154         return 0;
0155     }
0156 
0157     rc = lpfc_mbox_rsrc_prep(phba, pmb);
0158     if (rc) {
0159         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
0160                 "2605 %s: memory allocation failed\n",
0161                 __func__);
0162         return 1;
0163     }
0164 
0165     mp = pmb->ctx_buf;
0166     mb->un.varWords[3] = putPaddrLow(mp->phys);
0167     mb->un.varWords[4] = putPaddrHigh(mp->phys);
0168     mb->un.varDmp.sli4_length = sizeof(struct static_vport_info);
0169 
0170     return 0;
0171 }
0172 
0173 /**
0174  * lpfc_down_link - Bring down HBAs link.
0175  * @phba: pointer to lpfc hba data structure.
0176  * @pmb: pointer to the driver internal queue element for mailbox command.
0177  *
0178  * This routine prepares a mailbox command to bring down HBA link.
0179  **/
0180 void
0181 lpfc_down_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
0182 {
0183     MAILBOX_t *mb;
0184     memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
0185     mb = &pmb->u.mb;
0186     mb->mbxCommand = MBX_DOWN_LINK;
0187     mb->mbxOwner = OWN_HOST;
0188 }
0189 
0190 /**
0191  * lpfc_dump_mem - Prepare a mailbox command for reading a region.
0192  * @phba: pointer to lpfc hba data structure.
0193  * @pmb: pointer to the driver internal queue element for mailbox command.
0194  * @offset: offset into the region.
0195  * @region_id: config region id.
0196  *
0197  * The dump mailbox command provides a method for the device driver to obtain
0198  * various types of information from the HBA device.
0199  *
0200  * This routine prepares the mailbox command for dumping HBA's config region.
0201  **/
0202 void
0203 lpfc_dump_mem(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, uint16_t offset,
0204         uint16_t region_id)
0205 {
0206     MAILBOX_t *mb;
0207     void *ctx;
0208 
0209     mb = &pmb->u.mb;
0210     ctx = pmb->ctx_buf;
0211 
0212     /* Setup to dump VPD region */
0213     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0214     mb->mbxCommand = MBX_DUMP_MEMORY;
0215     mb->un.varDmp.cv = 1;
0216     mb->un.varDmp.type = DMP_NV_PARAMS;
0217     mb->un.varDmp.entry_index = offset;
0218     mb->un.varDmp.region_id = region_id;
0219     mb->un.varDmp.word_cnt = (DMP_RSP_SIZE / sizeof (uint32_t));
0220     mb->un.varDmp.co = 0;
0221     mb->un.varDmp.resp_offset = 0;
0222     pmb->ctx_buf = ctx;
0223     mb->mbxOwner = OWN_HOST;
0224     return;
0225 }
0226 
0227 /**
0228  * lpfc_dump_wakeup_param - Prepare mailbox command for retrieving wakeup params
0229  * @phba: pointer to lpfc hba data structure.
0230  * @pmb: pointer to the driver internal queue element for mailbox command.
0231  *
0232  * This function create a dump memory mailbox command to dump wake up
0233  * parameters.
0234  */
0235 void
0236 lpfc_dump_wakeup_param(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
0237 {
0238     MAILBOX_t *mb;
0239     void *ctx;
0240 
0241     mb = &pmb->u.mb;
0242     /* Save context so that we can restore after memset */
0243     ctx = pmb->ctx_buf;
0244 
0245     /* Setup to dump VPD region */
0246     memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
0247     mb->mbxCommand = MBX_DUMP_MEMORY;
0248     mb->mbxOwner = OWN_HOST;
0249     mb->un.varDmp.cv = 1;
0250     mb->un.varDmp.type = DMP_NV_PARAMS;
0251     if (phba->sli_rev < LPFC_SLI_REV4)
0252         mb->un.varDmp.entry_index = 0;
0253     mb->un.varDmp.region_id = WAKE_UP_PARMS_REGION_ID;
0254     mb->un.varDmp.word_cnt = WAKE_UP_PARMS_WORD_SIZE;
0255     mb->un.varDmp.co = 0;
0256     mb->un.varDmp.resp_offset = 0;
0257     pmb->ctx_buf = ctx;
0258     return;
0259 }
0260 
0261 /**
0262  * lpfc_read_nv - Prepare a mailbox command for reading HBA's NVRAM param
0263  * @phba: pointer to lpfc hba data structure.
0264  * @pmb: pointer to the driver internal queue element for mailbox command.
0265  *
0266  * The read NVRAM mailbox command returns the HBA's non-volatile parameters
0267  * that are used as defaults when the Fibre Channel link is brought on-line.
0268  *
0269  * This routine prepares the mailbox command for reading information stored
0270  * in the HBA's NVRAM. Specifically, the HBA's WWNN and WWPN.
0271  **/
0272 void
0273 lpfc_read_nv(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
0274 {
0275     MAILBOX_t *mb;
0276 
0277     mb = &pmb->u.mb;
0278     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0279     mb->mbxCommand = MBX_READ_NV;
0280     mb->mbxOwner = OWN_HOST;
0281     return;
0282 }
0283 
0284 /**
0285  * lpfc_config_async - Prepare a mailbox command for enabling HBA async event
0286  * @phba: pointer to lpfc hba data structure.
0287  * @pmb: pointer to the driver internal queue element for mailbox command.
0288  * @ring: ring number for the asynchronous event to be configured.
0289  *
0290  * The asynchronous event enable mailbox command is used to enable the
0291  * asynchronous event posting via the ASYNC_STATUS_CN IOCB response and
0292  * specifies the default ring to which events are posted.
0293  *
0294  * This routine prepares the mailbox command for enabling HBA asynchronous
0295  * event support on a IOCB ring.
0296  **/
0297 void
0298 lpfc_config_async(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
0299         uint32_t ring)
0300 {
0301     MAILBOX_t *mb;
0302 
0303     mb = &pmb->u.mb;
0304     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0305     mb->mbxCommand = MBX_ASYNCEVT_ENABLE;
0306     mb->un.varCfgAsyncEvent.ring = ring;
0307     mb->mbxOwner = OWN_HOST;
0308     return;
0309 }
0310 
0311 /**
0312  * lpfc_heart_beat - Prepare a mailbox command for heart beat
0313  * @phba: pointer to lpfc hba data structure.
0314  * @pmb: pointer to the driver internal queue element for mailbox command.
0315  *
0316  * The heart beat mailbox command is used to detect an unresponsive HBA, which
0317  * is defined as any device where no error attention is sent and both mailbox
0318  * and rings are not processed.
0319  *
0320  * This routine prepares the mailbox command for issuing a heart beat in the
0321  * form of mailbox command to the HBA. The timely completion of the heart
0322  * beat mailbox command indicates the health of the HBA.
0323  **/
0324 void
0325 lpfc_heart_beat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
0326 {
0327     MAILBOX_t *mb;
0328 
0329     mb = &pmb->u.mb;
0330     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0331     mb->mbxCommand = MBX_HEARTBEAT;
0332     mb->mbxOwner = OWN_HOST;
0333     return;
0334 }
0335 
0336 /**
0337  * lpfc_read_topology - Prepare a mailbox command for reading HBA topology
0338  * @phba: pointer to lpfc hba data structure.
0339  * @pmb: pointer to the driver internal queue element for mailbox command.
0340  * @mp: DMA buffer memory for reading the link attention information into.
0341  *
0342  * The read topology mailbox command is issued to read the link topology
0343  * information indicated by the HBA port when the Link Event bit of the Host
0344  * Attention (HSTATT) register is set to 1 (For SLI-3) or when an FC Link
0345  * Attention ACQE is received from the port (For SLI-4). A Link Event
0346  * Attention occurs based on an exception detected at the Fibre Channel link
0347  * interface.
0348  *
0349  * This routine prepares the mailbox command for reading HBA link topology
0350  * information. A DMA memory has been set aside and address passed to the
0351  * HBA through @mp for the HBA to DMA link attention information into the
0352  * memory as part of the execution of the mailbox command.
0353  *
0354  * Return codes
0355  *    0 - Success (currently always return 0)
0356  **/
0357 int
0358 lpfc_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
0359            struct lpfc_dmabuf *mp)
0360 {
0361     MAILBOX_t *mb;
0362 
0363     mb = &pmb->u.mb;
0364     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0365 
0366     INIT_LIST_HEAD(&mp->list);
0367     mb->mbxCommand = MBX_READ_TOPOLOGY;
0368     mb->un.varReadTop.lilpBde64.tus.f.bdeSize = LPFC_ALPA_MAP_SIZE;
0369     mb->un.varReadTop.lilpBde64.addrHigh = putPaddrHigh(mp->phys);
0370     mb->un.varReadTop.lilpBde64.addrLow = putPaddrLow(mp->phys);
0371 
0372     /* Save address for later completion and set the owner to host so that
0373      * the FW knows this mailbox is available for processing.
0374      */
0375     pmb->ctx_buf = (uint8_t *)mp;
0376     mb->mbxOwner = OWN_HOST;
0377     return (0);
0378 }
0379 
0380 /**
0381  * lpfc_clear_la - Prepare a mailbox command for clearing HBA link attention
0382  * @phba: pointer to lpfc hba data structure.
0383  * @pmb: pointer to the driver internal queue element for mailbox command.
0384  *
0385  * The clear link attention mailbox command is issued to clear the link event
0386  * attention condition indicated by the Link Event bit of the Host Attention
0387  * (HSTATT) register. The link event attention condition is cleared only if
0388  * the event tag specified matches that of the current link event counter.
0389  * The current event tag is read using the read link attention event mailbox
0390  * command.
0391  *
0392  * This routine prepares the mailbox command for clearing HBA link attention
0393  * information.
0394  **/
0395 void
0396 lpfc_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
0397 {
0398     MAILBOX_t *mb;
0399 
0400     mb = &pmb->u.mb;
0401     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0402 
0403     mb->un.varClearLA.eventTag = phba->fc_eventTag;
0404     mb->mbxCommand = MBX_CLEAR_LA;
0405     mb->mbxOwner = OWN_HOST;
0406     return;
0407 }
0408 
0409 /**
0410  * lpfc_config_link - Prepare a mailbox command for configuring link on a HBA
0411  * @phba: pointer to lpfc hba data structure.
0412  * @pmb: pointer to the driver internal queue element for mailbox command.
0413  *
0414  * The configure link mailbox command is used before the initialize link
0415  * mailbox command to override default value and to configure link-oriented
0416  * parameters such as DID address and various timers. Typically, this
0417  * command would be used after an F_Port login to set the returned DID address
0418  * and the fabric timeout values. This command is not valid before a configure
0419  * port command has configured the HBA port.
0420  *
0421  * This routine prepares the mailbox command for configuring link on a HBA.
0422  **/
0423 void
0424 lpfc_config_link(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
0425 {
0426     struct lpfc_vport  *vport = phba->pport;
0427     MAILBOX_t *mb = &pmb->u.mb;
0428     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0429 
0430     /* NEW_FEATURE
0431      * SLI-2, Coalescing Response Feature.
0432      */
0433     if (phba->cfg_cr_delay && (phba->sli_rev < LPFC_SLI_REV4)) {
0434         mb->un.varCfgLnk.cr = 1;
0435         mb->un.varCfgLnk.ci = 1;
0436         mb->un.varCfgLnk.cr_delay = phba->cfg_cr_delay;
0437         mb->un.varCfgLnk.cr_count = phba->cfg_cr_count;
0438     }
0439 
0440     mb->un.varCfgLnk.myId = vport->fc_myDID;
0441     mb->un.varCfgLnk.edtov = phba->fc_edtov;
0442     mb->un.varCfgLnk.arbtov = phba->fc_arbtov;
0443     mb->un.varCfgLnk.ratov = phba->fc_ratov;
0444     mb->un.varCfgLnk.rttov = phba->fc_rttov;
0445     mb->un.varCfgLnk.altov = phba->fc_altov;
0446     mb->un.varCfgLnk.crtov = phba->fc_crtov;
0447     mb->un.varCfgLnk.cscn = 0;
0448     if (phba->bbcredit_support && phba->cfg_enable_bbcr) {
0449         mb->un.varCfgLnk.cscn = 1;
0450         mb->un.varCfgLnk.bbscn = bf_get(lpfc_bbscn_def,
0451                          &phba->sli4_hba.bbscn_params);
0452     }
0453 
0454     if (phba->cfg_ack0 && (phba->sli_rev < LPFC_SLI_REV4))
0455         mb->un.varCfgLnk.ack0_enable = 1;
0456 
0457     mb->mbxCommand = MBX_CONFIG_LINK;
0458     mb->mbxOwner = OWN_HOST;
0459     return;
0460 }
0461 
0462 /**
0463  * lpfc_config_msi - Prepare a mailbox command for configuring msi-x
0464  * @phba: pointer to lpfc hba data structure.
0465  * @pmb: pointer to the driver internal queue element for mailbox command.
0466  *
0467  * The configure MSI-X mailbox command is used to configure the HBA's SLI-3
0468  * MSI-X multi-message interrupt vector association to interrupt attention
0469  * conditions.
0470  *
0471  * Return codes
0472  *    0 - Success
0473  *    -EINVAL - Failure
0474  **/
0475 int
0476 lpfc_config_msi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
0477 {
0478     MAILBOX_t *mb = &pmb->u.mb;
0479     uint32_t attentionConditions[2];
0480 
0481     /* Sanity check */
0482     if (phba->cfg_use_msi != 2) {
0483         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
0484                 "0475 Not configured for supporting MSI-X "
0485                 "cfg_use_msi: 0x%x\n", phba->cfg_use_msi);
0486         return -EINVAL;
0487     }
0488 
0489     if (phba->sli_rev < 3) {
0490         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
0491                 "0476 HBA not supporting SLI-3 or later "
0492                 "SLI Revision: 0x%x\n", phba->sli_rev);
0493         return -EINVAL;
0494     }
0495 
0496     /* Clear mailbox command fields */
0497     memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
0498 
0499     /*
0500      * SLI-3, Message Signaled Interrupt Feature.
0501      */
0502 
0503     /* Multi-message attention configuration */
0504     attentionConditions[0] = (HA_R0ATT | HA_R1ATT | HA_R2ATT | HA_ERATT |
0505                   HA_LATT | HA_MBATT);
0506     attentionConditions[1] = 0;
0507 
0508     mb->un.varCfgMSI.attentionConditions[0] = attentionConditions[0];
0509     mb->un.varCfgMSI.attentionConditions[1] = attentionConditions[1];
0510 
0511     /*
0512      * Set up message number to HA bit association
0513      */
0514 #ifdef __BIG_ENDIAN_BITFIELD
0515     /* RA0 (FCP Ring) */
0516     mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS] = 1;
0517     /* RA1 (Other Protocol Extra Ring) */
0518     mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS] = 1;
0519 #else   /*  __LITTLE_ENDIAN_BITFIELD */
0520     /* RA0 (FCP Ring) */
0521     mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS^3] = 1;
0522     /* RA1 (Other Protocol Extra Ring) */
0523     mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS^3] = 1;
0524 #endif
0525     /* Multi-message interrupt autoclear configuration*/
0526     mb->un.varCfgMSI.autoClearHA[0] = attentionConditions[0];
0527     mb->un.varCfgMSI.autoClearHA[1] = attentionConditions[1];
0528 
0529     /* For now, HBA autoclear does not work reliably, disable it */
0530     mb->un.varCfgMSI.autoClearHA[0] = 0;
0531     mb->un.varCfgMSI.autoClearHA[1] = 0;
0532 
0533     /* Set command and owner bit */
0534     mb->mbxCommand = MBX_CONFIG_MSI;
0535     mb->mbxOwner = OWN_HOST;
0536 
0537     return 0;
0538 }
0539 
0540 /**
0541  * lpfc_init_link - Prepare a mailbox command for initialize link on a HBA
0542  * @phba: pointer to lpfc hba data structure.
0543  * @pmb: pointer to the driver internal queue element for mailbox command.
0544  * @topology: the link topology for the link to be initialized to.
0545  * @linkspeed: the link speed for the link to be initialized to.
0546  *
0547  * The initialize link mailbox command is used to initialize the Fibre
0548  * Channel link. This command must follow a configure port command that
0549  * establishes the mode of operation.
0550  *
0551  * This routine prepares the mailbox command for initializing link on a HBA
0552  * with the specified link topology and speed.
0553  **/
0554 void
0555 lpfc_init_link(struct lpfc_hba * phba,
0556            LPFC_MBOXQ_t * pmb, uint32_t topology, uint32_t linkspeed)
0557 {
0558     lpfc_vpd_t *vpd;
0559     MAILBOX_t *mb;
0560 
0561     mb = &pmb->u.mb;
0562     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0563 
0564     switch (topology) {
0565     case FLAGS_TOPOLOGY_MODE_LOOP_PT:
0566         mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
0567         mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
0568         break;
0569     case FLAGS_TOPOLOGY_MODE_PT_PT:
0570         mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
0571         break;
0572     case FLAGS_TOPOLOGY_MODE_LOOP:
0573         mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
0574         break;
0575     case FLAGS_TOPOLOGY_MODE_PT_LOOP:
0576         mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
0577         mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
0578         break;
0579     case FLAGS_LOCAL_LB:
0580         mb->un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
0581         break;
0582     }
0583 
0584     /* Topology handling for ASIC_GEN_NUM 0xC and later */
0585     if ((phba->sli4_hba.pc_sli4_params.sli_family == LPFC_SLI_INTF_FAMILY_G6 ||
0586          phba->sli4_hba.pc_sli4_params.if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
0587         !(phba->sli4_hba.pc_sli4_params.pls) &&
0588         mb->un.varInitLnk.link_flags & FLAGS_TOPOLOGY_MODE_LOOP) {
0589         mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
0590         phba->cfg_topology = FLAGS_TOPOLOGY_MODE_PT_PT;
0591     }
0592 
0593     /* Enable asynchronous ABTS responses from firmware */
0594     if (phba->sli_rev == LPFC_SLI_REV3 && !phba->cfg_fcp_wait_abts_rsp)
0595         mb->un.varInitLnk.link_flags |= FLAGS_IMED_ABORT;
0596 
0597     /* NEW_FEATURE
0598      * Setting up the link speed
0599      */
0600     vpd = &phba->vpd;
0601     if (vpd->rev.feaLevelHigh >= 0x02){
0602         switch(linkspeed){
0603         case LPFC_USER_LINK_SPEED_1G:
0604             mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
0605             mb->un.varInitLnk.link_speed = LINK_SPEED_1G;
0606             break;
0607         case LPFC_USER_LINK_SPEED_2G:
0608             mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
0609             mb->un.varInitLnk.link_speed = LINK_SPEED_2G;
0610             break;
0611         case LPFC_USER_LINK_SPEED_4G:
0612             mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
0613             mb->un.varInitLnk.link_speed = LINK_SPEED_4G;
0614             break;
0615         case LPFC_USER_LINK_SPEED_8G:
0616             mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
0617             mb->un.varInitLnk.link_speed = LINK_SPEED_8G;
0618             break;
0619         case LPFC_USER_LINK_SPEED_10G:
0620             mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
0621             mb->un.varInitLnk.link_speed = LINK_SPEED_10G;
0622             break;
0623         case LPFC_USER_LINK_SPEED_16G:
0624             mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
0625             mb->un.varInitLnk.link_speed = LINK_SPEED_16G;
0626             break;
0627         case LPFC_USER_LINK_SPEED_32G:
0628             mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
0629             mb->un.varInitLnk.link_speed = LINK_SPEED_32G;
0630             break;
0631         case LPFC_USER_LINK_SPEED_64G:
0632             mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
0633             mb->un.varInitLnk.link_speed = LINK_SPEED_64G;
0634             break;
0635         case LPFC_USER_LINK_SPEED_AUTO:
0636         default:
0637             mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
0638             break;
0639         }
0640 
0641     }
0642     else
0643         mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
0644 
0645     mb->mbxCommand = (volatile uint8_t)MBX_INIT_LINK;
0646     mb->mbxOwner = OWN_HOST;
0647     mb->un.varInitLnk.fabric_AL_PA = phba->fc_pref_ALPA;
0648     return;
0649 }
0650 
0651 /**
0652  * lpfc_read_sparam - Prepare a mailbox command for reading HBA parameters
0653  * @phba: pointer to lpfc hba data structure.
0654  * @pmb: pointer to the driver internal queue element for mailbox command.
0655  * @vpi: virtual N_Port identifier.
0656  *
0657  * The read service parameter mailbox command is used to read the HBA port
0658  * service parameters. The service parameters are read into the buffer
0659  * specified directly by a BDE in the mailbox command. These service
0660  * parameters may then be used to build the payload of an N_Port/F_POrt
0661  * login request and reply (LOGI/ACC).
0662  *
0663  * This routine prepares the mailbox command for reading HBA port service
0664  * parameters. The DMA memory is allocated in this function and the addresses
0665  * are populated into the mailbox command for the HBA to DMA the service
0666  * parameters into.
0667  *
0668  * Return codes
0669  *    0 - Success
0670  *    1 - DMA memory allocation failed
0671  **/
0672 int
0673 lpfc_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, int vpi)
0674 {
0675     struct lpfc_dmabuf *mp;
0676     MAILBOX_t *mb;
0677     int rc;
0678 
0679     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0680 
0681     /* Get a buffer to hold the HBAs Service Parameters */
0682     rc = lpfc_mbox_rsrc_prep(phba, pmb);
0683     if (rc) {
0684         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
0685                     "0301 READ_SPARAM: no buffers\n");
0686         return 1;
0687     }
0688 
0689     mp = pmb->ctx_buf;
0690     mb = &pmb->u.mb;
0691     mb->mbxOwner = OWN_HOST;
0692     mb->mbxCommand = MBX_READ_SPARM64;
0693     mb->un.varRdSparm.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
0694     mb->un.varRdSparm.un.sp64.addrHigh = putPaddrHigh(mp->phys);
0695     mb->un.varRdSparm.un.sp64.addrLow = putPaddrLow(mp->phys);
0696     if (phba->sli_rev >= LPFC_SLI_REV3)
0697         mb->un.varRdSparm.vpi = phba->vpi_ids[vpi];
0698 
0699     return (0);
0700 }
0701 
0702 /**
0703  * lpfc_unreg_did - Prepare a mailbox command for unregistering DID
0704  * @phba: pointer to lpfc hba data structure.
0705  * @vpi: virtual N_Port identifier.
0706  * @did: remote port identifier.
0707  * @pmb: pointer to the driver internal queue element for mailbox command.
0708  *
0709  * The unregister DID mailbox command is used to unregister an N_Port/F_Port
0710  * login for an unknown RPI by specifying the DID of a remote port. This
0711  * command frees an RPI context in the HBA port. This has the effect of
0712  * performing an implicit N_Port/F_Port logout.
0713  *
0714  * This routine prepares the mailbox command for unregistering a remote
0715  * N_Port/F_Port (DID) login.
0716  **/
0717 void
0718 lpfc_unreg_did(struct lpfc_hba * phba, uint16_t vpi, uint32_t did,
0719            LPFC_MBOXQ_t * pmb)
0720 {
0721     MAILBOX_t *mb;
0722 
0723     mb = &pmb->u.mb;
0724     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0725 
0726     mb->un.varUnregDID.did = did;
0727     mb->un.varUnregDID.vpi = vpi;
0728     if ((vpi != 0xffff) &&
0729         (phba->sli_rev == LPFC_SLI_REV4))
0730         mb->un.varUnregDID.vpi = phba->vpi_ids[vpi];
0731 
0732     mb->mbxCommand = MBX_UNREG_D_ID;
0733     mb->mbxOwner = OWN_HOST;
0734     return;
0735 }
0736 
0737 /**
0738  * lpfc_read_config - Prepare a mailbox command for reading HBA configuration
0739  * @phba: pointer to lpfc hba data structure.
0740  * @pmb: pointer to the driver internal queue element for mailbox command.
0741  *
0742  * The read configuration mailbox command is used to read the HBA port
0743  * configuration parameters. This mailbox command provides a method for
0744  * seeing any parameters that may have changed via various configuration
0745  * mailbox commands.
0746  *
0747  * This routine prepares the mailbox command for reading out HBA configuration
0748  * parameters.
0749  **/
0750 void
0751 lpfc_read_config(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
0752 {
0753     MAILBOX_t *mb;
0754 
0755     mb = &pmb->u.mb;
0756     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0757 
0758     mb->mbxCommand = MBX_READ_CONFIG;
0759     mb->mbxOwner = OWN_HOST;
0760     return;
0761 }
0762 
0763 /**
0764  * lpfc_read_lnk_stat - Prepare a mailbox command for reading HBA link stats
0765  * @phba: pointer to lpfc hba data structure.
0766  * @pmb: pointer to the driver internal queue element for mailbox command.
0767  *
0768  * The read link status mailbox command is used to read the link status from
0769  * the HBA. Link status includes all link-related error counters. These
0770  * counters are maintained by the HBA and originated in the link hardware
0771  * unit. Note that all of these counters wrap.
0772  *
0773  * This routine prepares the mailbox command for reading out HBA link status.
0774  **/
0775 void
0776 lpfc_read_lnk_stat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
0777 {
0778     MAILBOX_t *mb;
0779 
0780     mb = &pmb->u.mb;
0781     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0782 
0783     mb->mbxCommand = MBX_READ_LNK_STAT;
0784     mb->mbxOwner = OWN_HOST;
0785     return;
0786 }
0787 
0788 /**
0789  * lpfc_reg_rpi - Prepare a mailbox command for registering remote login
0790  * @phba: pointer to lpfc hba data structure.
0791  * @vpi: virtual N_Port identifier.
0792  * @did: remote port identifier.
0793  * @param: pointer to memory holding the server parameters.
0794  * @pmb: pointer to the driver internal queue element for mailbox command.
0795  * @rpi: the rpi to use in the registration (usually only used for SLI4.
0796  *
0797  * The registration login mailbox command is used to register an N_Port or
0798  * F_Port login. This registration allows the HBA to cache the remote N_Port
0799  * service parameters internally and thereby make the appropriate FC-2
0800  * decisions. The remote port service parameters are handed off by the driver
0801  * to the HBA using a descriptor entry that directly identifies a buffer in
0802  * host memory. In exchange, the HBA returns an RPI identifier.
0803  *
0804  * This routine prepares the mailbox command for registering remote port login.
0805  * The function allocates DMA buffer for passing the service parameters to the
0806  * HBA with the mailbox command.
0807  *
0808  * Return codes
0809  *    0 - Success
0810  *    1 - DMA memory allocation failed
0811  **/
0812 int
0813 lpfc_reg_rpi(struct lpfc_hba *phba, uint16_t vpi, uint32_t did,
0814          uint8_t *param, LPFC_MBOXQ_t *pmb, uint16_t rpi)
0815 {
0816     MAILBOX_t *mb = &pmb->u.mb;
0817     uint8_t *sparam;
0818     struct lpfc_dmabuf *mp;
0819     int rc;
0820 
0821     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0822 
0823     mb->un.varRegLogin.rpi = 0;
0824     if (phba->sli_rev == LPFC_SLI_REV4)
0825         mb->un.varRegLogin.rpi = phba->sli4_hba.rpi_ids[rpi];
0826     if (phba->sli_rev >= LPFC_SLI_REV3)
0827         mb->un.varRegLogin.vpi = phba->vpi_ids[vpi];
0828     mb->un.varRegLogin.did = did;
0829     mb->mbxOwner = OWN_HOST;
0830 
0831     /* Get a buffer to hold NPorts Service Parameters */
0832     rc = lpfc_mbox_rsrc_prep(phba, pmb);
0833     if (rc) {
0834         mb->mbxCommand = MBX_REG_LOGIN64;
0835         /* REG_LOGIN: no buffers */
0836         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
0837                 "0302 REG_LOGIN: no buffers, VPI:%d DID:x%x, "
0838                 "rpi x%x\n", vpi, did, rpi);
0839         return 1;
0840     }
0841 
0842     /* Copy param's into a new buffer */
0843     mp = pmb->ctx_buf;
0844     sparam = mp->virt;
0845     memcpy(sparam, param, sizeof (struct serv_parm));
0846 
0847     /* Finish initializing the mailbox. */
0848     mb->mbxCommand = MBX_REG_LOGIN64;
0849     mb->un.varRegLogin.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
0850     mb->un.varRegLogin.un.sp64.addrHigh = putPaddrHigh(mp->phys);
0851     mb->un.varRegLogin.un.sp64.addrLow = putPaddrLow(mp->phys);
0852 
0853     return 0;
0854 }
0855 
0856 /**
0857  * lpfc_unreg_login - Prepare a mailbox command for unregistering remote login
0858  * @phba: pointer to lpfc hba data structure.
0859  * @vpi: virtual N_Port identifier.
0860  * @rpi: remote port identifier
0861  * @pmb: pointer to the driver internal queue element for mailbox command.
0862  *
0863  * The unregistration login mailbox command is used to unregister an N_Port
0864  * or F_Port login. This command frees an RPI context in the HBA. It has the
0865  * effect of performing an implicit N_Port/F_Port logout.
0866  *
0867  * This routine prepares the mailbox command for unregistering remote port
0868  * login.
0869  *
0870  * For SLI4 ports, the rpi passed to this function must be the physical
0871  * rpi value, not the logical index.
0872  **/
0873 void
0874 lpfc_unreg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t rpi,
0875          LPFC_MBOXQ_t * pmb)
0876 {
0877     MAILBOX_t *mb;
0878 
0879     mb = &pmb->u.mb;
0880     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0881 
0882     mb->un.varUnregLogin.rpi = rpi;
0883     mb->un.varUnregLogin.rsvd1 = 0;
0884     if (phba->sli_rev >= LPFC_SLI_REV3)
0885         mb->un.varUnregLogin.vpi = phba->vpi_ids[vpi];
0886 
0887     mb->mbxCommand = MBX_UNREG_LOGIN;
0888     mb->mbxOwner = OWN_HOST;
0889 
0890     return;
0891 }
0892 
0893 /**
0894  * lpfc_sli4_unreg_all_rpis - unregister all RPIs for a vport on SLI4 HBA.
0895  * @vport: pointer to a vport object.
0896  *
0897  * This routine sends mailbox command to unregister all active RPIs for
0898  * a vport.
0899  **/
0900 void
0901 lpfc_sli4_unreg_all_rpis(struct lpfc_vport *vport)
0902 {
0903     struct lpfc_hba  *phba  = vport->phba;
0904     LPFC_MBOXQ_t     *mbox;
0905     int rc;
0906 
0907     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
0908     if (mbox) {
0909         /*
0910          * For SLI4 functions, the rpi field is overloaded for
0911          * the vport context unreg all.  This routine passes
0912          * 0 for the rpi field in lpfc_unreg_login for compatibility
0913          * with SLI3 and then overrides the rpi field with the
0914          * expected value for SLI4.
0915          */
0916         lpfc_unreg_login(phba, vport->vpi, phba->vpi_ids[vport->vpi],
0917                  mbox);
0918         mbox->u.mb.un.varUnregLogin.rsvd1 = 0x4000;
0919         mbox->vport = vport;
0920         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
0921         mbox->ctx_ndlp = NULL;
0922         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
0923         if (rc == MBX_NOT_FINISHED)
0924             mempool_free(mbox, phba->mbox_mem_pool);
0925     }
0926 }
0927 
0928 /**
0929  * lpfc_reg_vpi - Prepare a mailbox command for registering vport identifier
0930  * @vport: pointer to a vport object.
0931  * @pmb: pointer to the driver internal queue element for mailbox command.
0932  *
0933  * The registration vport identifier mailbox command is used to activate a
0934  * virtual N_Port after it has acquired an N_Port_ID. The HBA validates the
0935  * N_Port_ID against the information in the selected virtual N_Port context
0936  * block and marks it active to allow normal processing of IOCB commands and
0937  * received unsolicited exchanges.
0938  *
0939  * This routine prepares the mailbox command for registering a virtual N_Port.
0940  **/
0941 void
0942 lpfc_reg_vpi(struct lpfc_vport *vport, LPFC_MBOXQ_t *pmb)
0943 {
0944     MAILBOX_t *mb = &pmb->u.mb;
0945     struct lpfc_hba *phba = vport->phba;
0946 
0947     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0948     /*
0949      * Set the re-reg VPI bit for f/w to update the MAC address.
0950      */
0951     if ((phba->sli_rev == LPFC_SLI_REV4) &&
0952         !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI))
0953         mb->un.varRegVpi.upd = 1;
0954 
0955     mb->un.varRegVpi.vpi = phba->vpi_ids[vport->vpi];
0956     mb->un.varRegVpi.sid = vport->fc_myDID;
0957     if (phba->sli_rev == LPFC_SLI_REV4)
0958         mb->un.varRegVpi.vfi = phba->sli4_hba.vfi_ids[vport->vfi];
0959     else
0960         mb->un.varRegVpi.vfi = vport->vfi + vport->phba->vfi_base;
0961     memcpy(mb->un.varRegVpi.wwn, &vport->fc_portname,
0962            sizeof(struct lpfc_name));
0963     mb->un.varRegVpi.wwn[0] = cpu_to_le32(mb->un.varRegVpi.wwn[0]);
0964     mb->un.varRegVpi.wwn[1] = cpu_to_le32(mb->un.varRegVpi.wwn[1]);
0965 
0966     mb->mbxCommand = MBX_REG_VPI;
0967     mb->mbxOwner = OWN_HOST;
0968     return;
0969 
0970 }
0971 
0972 /**
0973  * lpfc_unreg_vpi - Prepare a mailbox command for unregistering vport id
0974  * @phba: pointer to lpfc hba data structure.
0975  * @vpi: virtual N_Port identifier.
0976  * @pmb: pointer to the driver internal queue element for mailbox command.
0977  *
0978  * The unregistration vport identifier mailbox command is used to inactivate
0979  * a virtual N_Port. The driver must have logged out and unregistered all
0980  * remote N_Ports to abort any activity on the virtual N_Port. The HBA will
0981  * unregisters any default RPIs associated with the specified vpi, aborting
0982  * any active exchanges. The HBA will post the mailbox response after making
0983  * the virtual N_Port inactive.
0984  *
0985  * This routine prepares the mailbox command for unregistering a virtual
0986  * N_Port.
0987  **/
0988 void
0989 lpfc_unreg_vpi(struct lpfc_hba *phba, uint16_t vpi, LPFC_MBOXQ_t *pmb)
0990 {
0991     MAILBOX_t *mb = &pmb->u.mb;
0992     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
0993 
0994     if (phba->sli_rev == LPFC_SLI_REV3)
0995         mb->un.varUnregVpi.vpi = phba->vpi_ids[vpi];
0996     else if (phba->sli_rev >= LPFC_SLI_REV4)
0997         mb->un.varUnregVpi.sli4_vpi = phba->vpi_ids[vpi];
0998 
0999     mb->mbxCommand = MBX_UNREG_VPI;
1000     mb->mbxOwner = OWN_HOST;
1001     return;
1002 
1003 }
1004 
1005 /**
1006  * lpfc_config_pcb_setup - Set up IOCB rings in the Port Control Block (PCB)
1007  * @phba: pointer to lpfc hba data structure.
1008  *
1009  * This routine sets up and initializes the IOCB rings in the Port Control
1010  * Block (PCB).
1011  **/
1012 static void
1013 lpfc_config_pcb_setup(struct lpfc_hba * phba)
1014 {
1015     struct lpfc_sli *psli = &phba->sli;
1016     struct lpfc_sli_ring *pring;
1017     PCB_t *pcbp = phba->pcb;
1018     dma_addr_t pdma_addr;
1019     uint32_t offset;
1020     uint32_t iocbCnt = 0;
1021     int i;
1022 
1023     pcbp->maxRing = (psli->num_rings - 1);
1024 
1025     for (i = 0; i < psli->num_rings; i++) {
1026         pring = &psli->sli3_ring[i];
1027 
1028         pring->sli.sli3.sizeCiocb =
1029             phba->sli_rev == 3 ? SLI3_IOCB_CMD_SIZE :
1030                             SLI2_IOCB_CMD_SIZE;
1031         pring->sli.sli3.sizeRiocb =
1032             phba->sli_rev == 3 ? SLI3_IOCB_RSP_SIZE :
1033                             SLI2_IOCB_RSP_SIZE;
1034         /* A ring MUST have both cmd and rsp entries defined to be
1035            valid */
1036         if ((pring->sli.sli3.numCiocb == 0) ||
1037             (pring->sli.sli3.numRiocb == 0)) {
1038             pcbp->rdsc[i].cmdEntries = 0;
1039             pcbp->rdsc[i].rspEntries = 0;
1040             pcbp->rdsc[i].cmdAddrHigh = 0;
1041             pcbp->rdsc[i].rspAddrHigh = 0;
1042             pcbp->rdsc[i].cmdAddrLow = 0;
1043             pcbp->rdsc[i].rspAddrLow = 0;
1044             pring->sli.sli3.cmdringaddr = NULL;
1045             pring->sli.sli3.rspringaddr = NULL;
1046             continue;
1047         }
1048         /* Command ring setup for ring */
1049         pring->sli.sli3.cmdringaddr = (void *)&phba->IOCBs[iocbCnt];
1050         pcbp->rdsc[i].cmdEntries = pring->sli.sli3.numCiocb;
1051 
1052         offset = (uint8_t *) &phba->IOCBs[iocbCnt] -
1053              (uint8_t *) phba->slim2p.virt;
1054         pdma_addr = phba->slim2p.phys + offset;
1055         pcbp->rdsc[i].cmdAddrHigh = putPaddrHigh(pdma_addr);
1056         pcbp->rdsc[i].cmdAddrLow = putPaddrLow(pdma_addr);
1057         iocbCnt += pring->sli.sli3.numCiocb;
1058 
1059         /* Response ring setup for ring */
1060         pring->sli.sli3.rspringaddr = (void *) &phba->IOCBs[iocbCnt];
1061 
1062         pcbp->rdsc[i].rspEntries = pring->sli.sli3.numRiocb;
1063         offset = (uint8_t *)&phba->IOCBs[iocbCnt] -
1064              (uint8_t *)phba->slim2p.virt;
1065         pdma_addr = phba->slim2p.phys + offset;
1066         pcbp->rdsc[i].rspAddrHigh = putPaddrHigh(pdma_addr);
1067         pcbp->rdsc[i].rspAddrLow = putPaddrLow(pdma_addr);
1068         iocbCnt += pring->sli.sli3.numRiocb;
1069     }
1070 }
1071 
1072 /**
1073  * lpfc_read_rev - Prepare a mailbox command for reading HBA revision
1074  * @phba: pointer to lpfc hba data structure.
1075  * @pmb: pointer to the driver internal queue element for mailbox command.
1076  *
1077  * The read revision mailbox command is used to read the revision levels of
1078  * the HBA components. These components include hardware units, resident
1079  * firmware, and available firmware. HBAs that supports SLI-3 mode of
1080  * operation provide different response information depending on the version
1081  * requested by the driver.
1082  *
1083  * This routine prepares the mailbox command for reading HBA revision
1084  * information.
1085  **/
1086 void
1087 lpfc_read_rev(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1088 {
1089     MAILBOX_t *mb = &pmb->u.mb;
1090     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1091     mb->un.varRdRev.cv = 1;
1092     mb->un.varRdRev.v3req = 1; /* Request SLI3 info */
1093     mb->mbxCommand = MBX_READ_REV;
1094     mb->mbxOwner = OWN_HOST;
1095     return;
1096 }
1097 
1098 void
1099 lpfc_sli4_swap_str(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1100 {
1101     MAILBOX_t *mb = &pmb->u.mb;
1102     struct lpfc_mqe *mqe;
1103 
1104     switch (mb->mbxCommand) {
1105     case  MBX_READ_REV:
1106          mqe = &pmb->u.mqe;
1107         lpfc_sli_pcimem_bcopy(mqe->un.read_rev.fw_name,
1108                  mqe->un.read_rev.fw_name, 16);
1109         lpfc_sli_pcimem_bcopy(mqe->un.read_rev.ulp_fw_name,
1110                  mqe->un.read_rev.ulp_fw_name, 16);
1111         break;
1112     default:
1113         break;
1114     }
1115     return;
1116 }
1117 
1118 /**
1119  * lpfc_build_hbq_profile2 - Set up the HBQ Selection Profile 2
1120  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1121  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1122  *
1123  * The Host Buffer Queue (HBQ) Selection Profile 2 specifies that the HBA
1124  * tests the incoming frames' R_CTL/TYPE fields with works 10:15 and performs
1125  * the Sequence Length Test using the fields in the Selection Profile 2
1126  * extension in words 20:31.
1127  **/
1128 static void
1129 lpfc_build_hbq_profile2(struct config_hbq_var *hbqmb,
1130             struct lpfc_hbq_init  *hbq_desc)
1131 {
1132     hbqmb->profiles.profile2.seqlenbcnt = hbq_desc->seqlenbcnt;
1133     hbqmb->profiles.profile2.maxlen     = hbq_desc->maxlen;
1134     hbqmb->profiles.profile2.seqlenoff  = hbq_desc->seqlenoff;
1135 }
1136 
1137 /**
1138  * lpfc_build_hbq_profile3 - Set up the HBQ Selection Profile 3
1139  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1140  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1141  *
1142  * The Host Buffer Queue (HBQ) Selection Profile 3 specifies that the HBA
1143  * tests the incoming frame's R_CTL/TYPE fields with words 10:15 and performs
1144  * the Sequence Length Test and Byte Field Test using the fields in the
1145  * Selection Profile 3 extension in words 20:31.
1146  **/
1147 static void
1148 lpfc_build_hbq_profile3(struct config_hbq_var *hbqmb,
1149             struct lpfc_hbq_init  *hbq_desc)
1150 {
1151     hbqmb->profiles.profile3.seqlenbcnt = hbq_desc->seqlenbcnt;
1152     hbqmb->profiles.profile3.maxlen     = hbq_desc->maxlen;
1153     hbqmb->profiles.profile3.cmdcodeoff = hbq_desc->cmdcodeoff;
1154     hbqmb->profiles.profile3.seqlenoff  = hbq_desc->seqlenoff;
1155     memcpy(&hbqmb->profiles.profile3.cmdmatch, hbq_desc->cmdmatch,
1156            sizeof(hbqmb->profiles.profile3.cmdmatch));
1157 }
1158 
1159 /**
1160  * lpfc_build_hbq_profile5 - Set up the HBQ Selection Profile 5
1161  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1162  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1163  *
1164  * The Host Buffer Queue (HBQ) Selection Profile 5 specifies a header HBQ. The
1165  * HBA tests the initial frame of an incoming sequence using the frame's
1166  * R_CTL/TYPE fields with words 10:15 and performs the Sequence Length Test
1167  * and Byte Field Test using the fields in the Selection Profile 5 extension
1168  * words 20:31.
1169  **/
1170 static void
1171 lpfc_build_hbq_profile5(struct config_hbq_var *hbqmb,
1172             struct lpfc_hbq_init  *hbq_desc)
1173 {
1174     hbqmb->profiles.profile5.seqlenbcnt = hbq_desc->seqlenbcnt;
1175     hbqmb->profiles.profile5.maxlen     = hbq_desc->maxlen;
1176     hbqmb->profiles.profile5.cmdcodeoff = hbq_desc->cmdcodeoff;
1177     hbqmb->profiles.profile5.seqlenoff  = hbq_desc->seqlenoff;
1178     memcpy(&hbqmb->profiles.profile5.cmdmatch, hbq_desc->cmdmatch,
1179            sizeof(hbqmb->profiles.profile5.cmdmatch));
1180 }
1181 
1182 /**
1183  * lpfc_config_hbq - Prepare a mailbox command for configuring an HBQ
1184  * @phba: pointer to lpfc hba data structure.
1185  * @id: HBQ identifier.
1186  * @hbq_desc: pointer to the HBA descriptor data structure.
1187  * @hbq_entry_index: index of the HBQ entry data structures.
1188  * @pmb: pointer to the driver internal queue element for mailbox command.
1189  *
1190  * The configure HBQ (Host Buffer Queue) mailbox command is used to configure
1191  * an HBQ. The configuration binds events that require buffers to a particular
1192  * ring and HBQ based on a selection profile.
1193  *
1194  * This routine prepares the mailbox command for configuring an HBQ.
1195  **/
1196 void
1197 lpfc_config_hbq(struct lpfc_hba *phba, uint32_t id,
1198          struct lpfc_hbq_init *hbq_desc,
1199         uint32_t hbq_entry_index, LPFC_MBOXQ_t *pmb)
1200 {
1201     int i;
1202     MAILBOX_t *mb = &pmb->u.mb;
1203     struct config_hbq_var *hbqmb = &mb->un.varCfgHbq;
1204 
1205     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1206     hbqmb->hbqId = id;
1207     hbqmb->entry_count = hbq_desc->entry_count;   /* # entries in HBQ */
1208     hbqmb->recvNotify = hbq_desc->rn;             /* Receive
1209                                * Notification */
1210     hbqmb->numMask    = hbq_desc->mask_count;     /* # R_CTL/TYPE masks
1211                                * # in words 0-19 */
1212     hbqmb->profile    = hbq_desc->profile;        /* Selection profile:
1213                                * 0 = all,
1214                                * 7 = logentry */
1215     hbqmb->ringMask   = hbq_desc->ring_mask;      /* Binds HBQ to a ring
1216                                * e.g. Ring0=b0001,
1217                                * ring2=b0100 */
1218     hbqmb->headerLen  = hbq_desc->headerLen;      /* 0 if not profile 4
1219                                * or 5 */
1220     hbqmb->logEntry   = hbq_desc->logEntry;       /* Set to 1 if this
1221                                * HBQ will be used
1222                                * for LogEntry
1223                                * buffers */
1224     hbqmb->hbqaddrLow = putPaddrLow(phba->hbqslimp.phys) +
1225         hbq_entry_index * sizeof(struct lpfc_hbq_entry);
1226     hbqmb->hbqaddrHigh = putPaddrHigh(phba->hbqslimp.phys);
1227 
1228     mb->mbxCommand = MBX_CONFIG_HBQ;
1229     mb->mbxOwner = OWN_HOST;
1230 
1231                 /* Copy info for profiles 2,3,5. Other
1232                  * profiles this area is reserved
1233                  */
1234     if (hbq_desc->profile == 2)
1235         lpfc_build_hbq_profile2(hbqmb, hbq_desc);
1236     else if (hbq_desc->profile == 3)
1237         lpfc_build_hbq_profile3(hbqmb, hbq_desc);
1238     else if (hbq_desc->profile == 5)
1239         lpfc_build_hbq_profile5(hbqmb, hbq_desc);
1240 
1241     /* Return if no rctl / type masks for this HBQ */
1242     if (!hbq_desc->mask_count)
1243         return;
1244 
1245     /* Otherwise we setup specific rctl / type masks for this HBQ */
1246     for (i = 0; i < hbq_desc->mask_count; i++) {
1247         hbqmb->hbqMasks[i].tmatch = hbq_desc->hbqMasks[i].tmatch;
1248         hbqmb->hbqMasks[i].tmask  = hbq_desc->hbqMasks[i].tmask;
1249         hbqmb->hbqMasks[i].rctlmatch = hbq_desc->hbqMasks[i].rctlmatch;
1250         hbqmb->hbqMasks[i].rctlmask  = hbq_desc->hbqMasks[i].rctlmask;
1251     }
1252 
1253     return;
1254 }
1255 
1256 /**
1257  * lpfc_config_ring - Prepare a mailbox command for configuring an IOCB ring
1258  * @phba: pointer to lpfc hba data structure.
1259  * @ring: ring number/index
1260  * @pmb: pointer to the driver internal queue element for mailbox command.
1261  *
1262  * The configure ring mailbox command is used to configure an IOCB ring. This
1263  * configuration binds from one to six of HBA RC_CTL/TYPE mask entries to the
1264  * ring. This is used to map incoming sequences to a particular ring whose
1265  * RC_CTL/TYPE mask entry matches that of the sequence. The driver should not
1266  * attempt to configure a ring whose number is greater than the number
1267  * specified in the Port Control Block (PCB). It is an error to issue the
1268  * configure ring command more than once with the same ring number. The HBA
1269  * returns an error if the driver attempts this.
1270  *
1271  * This routine prepares the mailbox command for configuring IOCB ring.
1272  **/
1273 void
1274 lpfc_config_ring(struct lpfc_hba * phba, int ring, LPFC_MBOXQ_t * pmb)
1275 {
1276     int i;
1277     MAILBOX_t *mb = &pmb->u.mb;
1278     struct lpfc_sli *psli;
1279     struct lpfc_sli_ring *pring;
1280 
1281     memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1282 
1283     mb->un.varCfgRing.ring = ring;
1284     mb->un.varCfgRing.maxOrigXchg = 0;
1285     mb->un.varCfgRing.maxRespXchg = 0;
1286     mb->un.varCfgRing.recvNotify = 1;
1287 
1288     psli = &phba->sli;
1289     pring = &psli->sli3_ring[ring];
1290     mb->un.varCfgRing.numMask = pring->num_mask;
1291     mb->mbxCommand = MBX_CONFIG_RING;
1292     mb->mbxOwner = OWN_HOST;
1293 
1294     /* Is this ring configured for a specific profile */
1295     if (pring->prt[0].profile) {
1296         mb->un.varCfgRing.profile = pring->prt[0].profile;
1297         return;
1298     }
1299 
1300     /* Otherwise we setup specific rctl / type masks for this ring */
1301     for (i = 0; i < pring->num_mask; i++) {
1302         mb->un.varCfgRing.rrRegs[i].rval = pring->prt[i].rctl;
1303         if (mb->un.varCfgRing.rrRegs[i].rval != FC_RCTL_ELS_REQ)
1304             mb->un.varCfgRing.rrRegs[i].rmask = 0xff;
1305         else
1306             mb->un.varCfgRing.rrRegs[i].rmask = 0xfe;
1307         mb->un.varCfgRing.rrRegs[i].tval = pring->prt[i].type;
1308         mb->un.varCfgRing.rrRegs[i].tmask = 0xff;
1309     }
1310 
1311     return;
1312 }
1313 
1314 /**
1315  * lpfc_config_port - Prepare a mailbox command for configuring port
1316  * @phba: pointer to lpfc hba data structure.
1317  * @pmb: pointer to the driver internal queue element for mailbox command.
1318  *
1319  * The configure port mailbox command is used to identify the Port Control
1320  * Block (PCB) in the driver memory. After this command is issued, the
1321  * driver must not access the mailbox in the HBA without first resetting
1322  * the HBA. The HBA may copy the PCB information to internal storage for
1323  * subsequent use; the driver can not change the PCB information unless it
1324  * resets the HBA.
1325  *
1326  * This routine prepares the mailbox command for configuring port.
1327  **/
1328 void
1329 lpfc_config_port(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1330 {
1331     MAILBOX_t __iomem *mb_slim = (MAILBOX_t __iomem *) phba->MBslimaddr;
1332     MAILBOX_t *mb = &pmb->u.mb;
1333     dma_addr_t pdma_addr;
1334     uint32_t bar_low, bar_high;
1335     size_t offset;
1336     struct lpfc_hgp hgp;
1337     int i;
1338     uint32_t pgp_offset;
1339 
1340     memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1341     mb->mbxCommand = MBX_CONFIG_PORT;
1342     mb->mbxOwner = OWN_HOST;
1343 
1344     mb->un.varCfgPort.pcbLen = sizeof(PCB_t);
1345 
1346     offset = (uint8_t *)phba->pcb - (uint8_t *)phba->slim2p.virt;
1347     pdma_addr = phba->slim2p.phys + offset;
1348     mb->un.varCfgPort.pcbLow = putPaddrLow(pdma_addr);
1349     mb->un.varCfgPort.pcbHigh = putPaddrHigh(pdma_addr);
1350 
1351     /* Always Host Group Pointer is in SLIM */
1352     mb->un.varCfgPort.hps = 1;
1353 
1354     /* If HBA supports SLI=3 ask for it */
1355 
1356     if (phba->sli_rev == LPFC_SLI_REV3 && phba->vpd.sli3Feat.cerbm) {
1357         if (phba->cfg_enable_bg)
1358             mb->un.varCfgPort.cbg = 1; /* configure BlockGuard */
1359         mb->un.varCfgPort.cerbm = 1; /* Request HBQs */
1360         mb->un.varCfgPort.ccrp = 1; /* Command Ring Polling */
1361         mb->un.varCfgPort.max_hbq = lpfc_sli_hbq_count();
1362         if (phba->max_vpi && phba->cfg_enable_npiv &&
1363             phba->vpd.sli3Feat.cmv) {
1364             mb->un.varCfgPort.max_vpi = LPFC_MAX_VPI;
1365             mb->un.varCfgPort.cmv = 1;
1366         } else
1367             mb->un.varCfgPort.max_vpi = phba->max_vpi = 0;
1368     } else
1369         phba->sli_rev = LPFC_SLI_REV2;
1370     mb->un.varCfgPort.sli_mode = phba->sli_rev;
1371 
1372     /* If this is an SLI3 port, configure async status notification. */
1373     if (phba->sli_rev == LPFC_SLI_REV3)
1374         mb->un.varCfgPort.casabt = 1;
1375 
1376     /* Now setup pcb */
1377     phba->pcb->type = TYPE_NATIVE_SLI2;
1378     phba->pcb->feature = FEATURE_INITIAL_SLI2;
1379 
1380     /* Setup Mailbox pointers */
1381     phba->pcb->mailBoxSize = sizeof(MAILBOX_t) + MAILBOX_EXT_SIZE;
1382     offset = (uint8_t *)phba->mbox - (uint8_t *)phba->slim2p.virt;
1383     pdma_addr = phba->slim2p.phys + offset;
1384     phba->pcb->mbAddrHigh = putPaddrHigh(pdma_addr);
1385     phba->pcb->mbAddrLow = putPaddrLow(pdma_addr);
1386 
1387     /*
1388      * Setup Host Group ring pointer.
1389      *
1390      * For efficiency reasons, the ring get/put pointers can be
1391      * placed in adapter memory (SLIM) rather than in host memory.
1392      * This allows firmware to avoid PCI reads/writes when updating
1393      * and checking pointers.
1394      *
1395      * The firmware recognizes the use of SLIM memory by comparing
1396      * the address of the get/put pointers structure with that of
1397      * the SLIM BAR (BAR0).
1398      *
1399      * Caution: be sure to use the PCI config space value of BAR0/BAR1
1400      * (the hardware's view of the base address), not the OS's
1401      * value of pci_resource_start() as the OS value may be a cookie
1402      * for ioremap/iomap.
1403      */
1404 
1405 
1406     pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_0, &bar_low);
1407     pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_1, &bar_high);
1408 
1409     /*
1410      * Set up HGP - Port Memory
1411      *
1412      * The port expects the host get/put pointers to reside in memory
1413      * following the "non-diagnostic" mode mailbox (32 words, 0x80 bytes)
1414      * area of SLIM.  In SLI-2 mode, there's an additional 16 reserved
1415      * words (0x40 bytes).  This area is not reserved if HBQs are
1416      * configured in SLI-3.
1417      *
1418      * CR0Put    - SLI2(no HBQs) = 0xc0, With HBQs = 0x80
1419      * RR0Get                      0xc4              0x84
1420      * CR1Put                      0xc8              0x88
1421      * RR1Get                      0xcc              0x8c
1422      * CR2Put                      0xd0              0x90
1423      * RR2Get                      0xd4              0x94
1424      * CR3Put                      0xd8              0x98
1425      * RR3Get                      0xdc              0x9c
1426      *
1427      * Reserved                    0xa0-0xbf
1428      *    If HBQs configured:
1429      *                         HBQ 0 Put ptr  0xc0
1430      *                         HBQ 1 Put ptr  0xc4
1431      *                         HBQ 2 Put ptr  0xc8
1432      *                         ......
1433      *                         HBQ(M-1)Put Pointer 0xc0+(M-1)*4
1434      *
1435      */
1436 
1437     if (phba->cfg_hostmem_hgp && phba->sli_rev != 3) {
1438         phba->host_gp = (struct lpfc_hgp __iomem *)
1439                  &phba->mbox->us.s2.host[0];
1440         phba->hbq_put = NULL;
1441         offset = (uint8_t *)&phba->mbox->us.s2.host -
1442             (uint8_t *)phba->slim2p.virt;
1443         pdma_addr = phba->slim2p.phys + offset;
1444         phba->pcb->hgpAddrHigh = putPaddrHigh(pdma_addr);
1445         phba->pcb->hgpAddrLow = putPaddrLow(pdma_addr);
1446     } else {
1447         /* Always Host Group Pointer is in SLIM */
1448         mb->un.varCfgPort.hps = 1;
1449 
1450         if (phba->sli_rev == 3) {
1451             phba->host_gp = &mb_slim->us.s3.host[0];
1452             phba->hbq_put = &mb_slim->us.s3.hbq_put[0];
1453         } else {
1454             phba->host_gp = &mb_slim->us.s2.host[0];
1455             phba->hbq_put = NULL;
1456         }
1457 
1458         /* mask off BAR0's flag bits 0 - 3 */
1459         phba->pcb->hgpAddrLow = (bar_low & PCI_BASE_ADDRESS_MEM_MASK) +
1460             (void __iomem *)phba->host_gp -
1461             (void __iomem *)phba->MBslimaddr;
1462         if (bar_low & PCI_BASE_ADDRESS_MEM_TYPE_64)
1463             phba->pcb->hgpAddrHigh = bar_high;
1464         else
1465             phba->pcb->hgpAddrHigh = 0;
1466         /* write HGP data to SLIM at the required longword offset */
1467         memset(&hgp, 0, sizeof(struct lpfc_hgp));
1468 
1469         for (i = 0; i < phba->sli.num_rings; i++) {
1470             lpfc_memcpy_to_slim(phba->host_gp + i, &hgp,
1471                     sizeof(*phba->host_gp));
1472         }
1473     }
1474 
1475     /* Setup Port Group offset */
1476     if (phba->sli_rev == 3)
1477         pgp_offset = offsetof(struct lpfc_sli2_slim,
1478                       mbx.us.s3_pgp.port);
1479     else
1480         pgp_offset = offsetof(struct lpfc_sli2_slim, mbx.us.s2.port);
1481     pdma_addr = phba->slim2p.phys + pgp_offset;
1482     phba->pcb->pgpAddrHigh = putPaddrHigh(pdma_addr);
1483     phba->pcb->pgpAddrLow = putPaddrLow(pdma_addr);
1484 
1485     /* Use callback routine to setp rings in the pcb */
1486     lpfc_config_pcb_setup(phba);
1487 
1488     /* special handling for LC HBAs */
1489     if (lpfc_is_LC_HBA(phba->pcidev->device)) {
1490         uint32_t hbainit[5];
1491 
1492         lpfc_hba_init(phba, hbainit);
1493 
1494         memcpy(&mb->un.varCfgPort.hbainit, hbainit, 20);
1495     }
1496 
1497     /* Swap PCB if needed */
1498     lpfc_sli_pcimem_bcopy(phba->pcb, phba->pcb, sizeof(PCB_t));
1499 }
1500 
1501 /**
1502  * lpfc_kill_board - Prepare a mailbox command for killing board
1503  * @phba: pointer to lpfc hba data structure.
1504  * @pmb: pointer to the driver internal queue element for mailbox command.
1505  *
1506  * The kill board mailbox command is used to tell firmware to perform a
1507  * graceful shutdown of a channel on a specified board to prepare for reset.
1508  * When the kill board mailbox command is received, the ER3 bit is set to 1
1509  * in the Host Status register and the ER Attention bit is set to 1 in the
1510  * Host Attention register of the HBA function that received the kill board
1511  * command.
1512  *
1513  * This routine prepares the mailbox command for killing the board in
1514  * preparation for a graceful shutdown.
1515  **/
1516 void
1517 lpfc_kill_board(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1518 {
1519     MAILBOX_t *mb = &pmb->u.mb;
1520 
1521     memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1522     mb->mbxCommand = MBX_KILL_BOARD;
1523     mb->mbxOwner = OWN_HOST;
1524     return;
1525 }
1526 
1527 /**
1528  * lpfc_mbox_put - Put a mailbox cmd into the tail of driver's mailbox queue
1529  * @phba: pointer to lpfc hba data structure.
1530  * @mbq: pointer to the driver internal queue element for mailbox command.
1531  *
1532  * Driver maintains a internal mailbox command queue implemented as a linked
1533  * list. When a mailbox command is issued, it shall be put into the mailbox
1534  * command queue such that they shall be processed orderly as HBA can process
1535  * one mailbox command at a time.
1536  **/
1537 void
1538 lpfc_mbox_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
1539 {
1540     struct lpfc_sli *psli;
1541 
1542     psli = &phba->sli;
1543 
1544     list_add_tail(&mbq->list, &psli->mboxq);
1545 
1546     psli->mboxq_cnt++;
1547 
1548     return;
1549 }
1550 
1551 /**
1552  * lpfc_mbox_get - Remove a mailbox cmd from the head of driver's mailbox queue
1553  * @phba: pointer to lpfc hba data structure.
1554  *
1555  * Driver maintains a internal mailbox command queue implemented as a linked
1556  * list. When a mailbox command is issued, it shall be put into the mailbox
1557  * command queue such that they shall be processed orderly as HBA can process
1558  * one mailbox command at a time. After HBA finished processing a mailbox
1559  * command, the driver will remove a pending mailbox command from the head of
1560  * the mailbox command queue and send to the HBA for processing.
1561  *
1562  * Return codes
1563  *    pointer to the driver internal queue element for mailbox command.
1564  **/
1565 LPFC_MBOXQ_t *
1566 lpfc_mbox_get(struct lpfc_hba * phba)
1567 {
1568     LPFC_MBOXQ_t *mbq = NULL;
1569     struct lpfc_sli *psli = &phba->sli;
1570 
1571     list_remove_head((&psli->mboxq), mbq, LPFC_MBOXQ_t, list);
1572     if (mbq)
1573         psli->mboxq_cnt--;
1574 
1575     return mbq;
1576 }
1577 
1578 /**
1579  * __lpfc_mbox_cmpl_put - Put mailbox cmd into mailbox cmd complete list
1580  * @phba: pointer to lpfc hba data structure.
1581  * @mbq: pointer to the driver internal queue element for mailbox command.
1582  *
1583  * This routine put the completed mailbox command into the mailbox command
1584  * complete list. This is the unlocked version of the routine. The mailbox
1585  * complete list is used by the driver worker thread to process mailbox
1586  * complete callback functions outside the driver interrupt handler.
1587  **/
1588 void
1589 __lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1590 {
1591     list_add_tail(&mbq->list, &phba->sli.mboxq_cmpl);
1592 }
1593 
1594 /**
1595  * lpfc_mbox_cmpl_put - Put mailbox command into mailbox command complete list
1596  * @phba: pointer to lpfc hba data structure.
1597  * @mbq: pointer to the driver internal queue element for mailbox command.
1598  *
1599  * This routine put the completed mailbox command into the mailbox command
1600  * complete list. This is the locked version of the routine. The mailbox
1601  * complete list is used by the driver worker thread to process mailbox
1602  * complete callback functions outside the driver interrupt handler.
1603  **/
1604 void
1605 lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1606 {
1607     unsigned long iflag;
1608 
1609     /* This function expects to be called from interrupt context */
1610     spin_lock_irqsave(&phba->hbalock, iflag);
1611     __lpfc_mbox_cmpl_put(phba, mbq);
1612     spin_unlock_irqrestore(&phba->hbalock, iflag);
1613     return;
1614 }
1615 
1616 /**
1617  * lpfc_mbox_cmd_check - Check the validality of a mailbox command
1618  * @phba: pointer to lpfc hba data structure.
1619  * @mboxq: pointer to the driver internal queue element for mailbox command.
1620  *
1621  * This routine is to check whether a mailbox command is valid to be issued.
1622  * This check will be performed by both the mailbox issue API when a client
1623  * is to issue a mailbox command to the mailbox transport.
1624  *
1625  * Return 0 - pass the check, -ENODEV - fail the check
1626  **/
1627 int
1628 lpfc_mbox_cmd_check(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1629 {
1630     /* Mailbox command that have a completion handler must also have a
1631      * vport specified.
1632      */
1633     if (mboxq->mbox_cmpl && mboxq->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
1634         mboxq->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
1635         if (!mboxq->vport) {
1636             lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1637                     "1814 Mbox x%x failed, no vport\n",
1638                     mboxq->u.mb.mbxCommand);
1639             dump_stack();
1640             return -ENODEV;
1641         }
1642     }
1643     return 0;
1644 }
1645 
1646 /**
1647  * lpfc_mbox_dev_check - Check the device state for issuing a mailbox command
1648  * @phba: pointer to lpfc hba data structure.
1649  *
1650  * This routine is to check whether the HBA device is ready for posting a
1651  * mailbox command. It is used by the mailbox transport API at the time the
1652  * to post a mailbox command to the device.
1653  *
1654  * Return 0 - pass the check, -ENODEV - fail the check
1655  **/
1656 int
1657 lpfc_mbox_dev_check(struct lpfc_hba *phba)
1658 {
1659     /* If the PCI channel is in offline state, do not issue mbox */
1660     if (unlikely(pci_channel_offline(phba->pcidev)))
1661         return -ENODEV;
1662 
1663     /* If the HBA is in error state, do not issue mbox */
1664     if (phba->link_state == LPFC_HBA_ERROR)
1665         return -ENODEV;
1666 
1667     return 0;
1668 }
1669 
1670 /**
1671  * lpfc_mbox_tmo_val - Retrieve mailbox command timeout value
1672  * @phba: pointer to lpfc hba data structure.
1673  * @mboxq: pointer to the driver internal queue element for mailbox command.
1674  *
1675  * This routine retrieves the proper timeout value according to the mailbox
1676  * command code.
1677  *
1678  * Return codes
1679  *    Timeout value to be used for the given mailbox command
1680  **/
1681 int
1682 lpfc_mbox_tmo_val(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1683 {
1684     MAILBOX_t *mbox = &mboxq->u.mb;
1685     uint8_t subsys, opcode;
1686 
1687     switch (mbox->mbxCommand) {
1688     case MBX_WRITE_NV:  /* 0x03 */
1689     case MBX_DUMP_MEMORY:   /* 0x17 */
1690     case MBX_UPDATE_CFG:    /* 0x1B */
1691     case MBX_DOWN_LOAD: /* 0x1C */
1692     case MBX_DEL_LD_ENTRY:  /* 0x1D */
1693     case MBX_WRITE_VPARMS:  /* 0x32 */
1694     case MBX_LOAD_AREA: /* 0x81 */
1695     case MBX_WRITE_WWN:     /* 0x98 */
1696     case MBX_LOAD_EXP_ROM:  /* 0x9C */
1697     case MBX_ACCESS_VDATA:  /* 0xA5 */
1698         return LPFC_MBOX_TMO_FLASH_CMD;
1699     case MBX_SLI4_CONFIG:   /* 0x9b */
1700         subsys = lpfc_sli_config_mbox_subsys_get(phba, mboxq);
1701         opcode = lpfc_sli_config_mbox_opcode_get(phba, mboxq);
1702         if (subsys == LPFC_MBOX_SUBSYSTEM_COMMON) {
1703             switch (opcode) {
1704             case LPFC_MBOX_OPCODE_READ_OBJECT:
1705             case LPFC_MBOX_OPCODE_WRITE_OBJECT:
1706             case LPFC_MBOX_OPCODE_READ_OBJECT_LIST:
1707             case LPFC_MBOX_OPCODE_DELETE_OBJECT:
1708             case LPFC_MBOX_OPCODE_GET_PROFILE_LIST:
1709             case LPFC_MBOX_OPCODE_SET_ACT_PROFILE:
1710             case LPFC_MBOX_OPCODE_GET_PROFILE_CONFIG:
1711             case LPFC_MBOX_OPCODE_SET_PROFILE_CONFIG:
1712             case LPFC_MBOX_OPCODE_GET_FACTORY_PROFILE_CONFIG:
1713             case LPFC_MBOX_OPCODE_GET_PROFILE_CAPACITIES:
1714             case LPFC_MBOX_OPCODE_SEND_ACTIVATION:
1715             case LPFC_MBOX_OPCODE_RESET_LICENSES:
1716             case LPFC_MBOX_OPCODE_SET_BOOT_CONFIG:
1717             case LPFC_MBOX_OPCODE_GET_VPD_DATA:
1718             case LPFC_MBOX_OPCODE_SET_PHYSICAL_LINK_CONFIG:
1719                 return LPFC_MBOX_SLI4_CONFIG_EXTENDED_TMO;
1720             }
1721         }
1722         if (subsys == LPFC_MBOX_SUBSYSTEM_FCOE) {
1723             switch (opcode) {
1724             case LPFC_MBOX_OPCODE_FCOE_SET_FCLINK_SETTINGS:
1725                 return LPFC_MBOX_SLI4_CONFIG_EXTENDED_TMO;
1726             }
1727         }
1728         return LPFC_MBOX_SLI4_CONFIG_TMO;
1729     }
1730     return LPFC_MBOX_TMO;
1731 }
1732 
1733 /**
1734  * lpfc_sli4_mbx_sge_set - Set a sge entry in non-embedded mailbox command
1735  * @mbox: pointer to lpfc mbox command.
1736  * @sgentry: sge entry index.
1737  * @phyaddr: physical address for the sge
1738  * @length: Length of the sge.
1739  *
1740  * This routine sets up an entry in the non-embedded mailbox command at the sge
1741  * index location.
1742  **/
1743 void
1744 lpfc_sli4_mbx_sge_set(struct lpfcMboxq *mbox, uint32_t sgentry,
1745               dma_addr_t phyaddr, uint32_t length)
1746 {
1747     struct lpfc_mbx_nembed_cmd *nembed_sge;
1748 
1749     nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1750                 &mbox->u.mqe.un.nembed_cmd;
1751     nembed_sge->sge[sgentry].pa_lo = putPaddrLow(phyaddr);
1752     nembed_sge->sge[sgentry].pa_hi = putPaddrHigh(phyaddr);
1753     nembed_sge->sge[sgentry].length = length;
1754 }
1755 
1756 /**
1757  * lpfc_sli4_mbx_sge_get - Get a sge entry from non-embedded mailbox command
1758  * @mbox: pointer to lpfc mbox command.
1759  * @sgentry: sge entry index.
1760  * @sge: pointer to lpfc mailbox sge to load into.
1761  *
1762  * This routine gets an entry from the non-embedded mailbox command at the sge
1763  * index location.
1764  **/
1765 void
1766 lpfc_sli4_mbx_sge_get(struct lpfcMboxq *mbox, uint32_t sgentry,
1767               struct lpfc_mbx_sge *sge)
1768 {
1769     struct lpfc_mbx_nembed_cmd *nembed_sge;
1770 
1771     nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1772                 &mbox->u.mqe.un.nembed_cmd;
1773     sge->pa_lo = nembed_sge->sge[sgentry].pa_lo;
1774     sge->pa_hi = nembed_sge->sge[sgentry].pa_hi;
1775     sge->length = nembed_sge->sge[sgentry].length;
1776 }
1777 
1778 /**
1779  * lpfc_sli4_mbox_cmd_free - Free a sli4 mailbox command
1780  * @phba: pointer to lpfc hba data structure.
1781  * @mbox: pointer to lpfc mbox command.
1782  *
1783  * This routine cleans up and releases an SLI4 mailbox command that was
1784  * configured using lpfc_sli4_config.  It accounts for the embedded and
1785  * non-embedded config types.
1786  **/
1787 void
1788 lpfc_sli4_mbox_cmd_free(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
1789 {
1790     struct lpfc_mbx_sli4_config *sli4_cfg;
1791     struct lpfc_mbx_sge sge;
1792     dma_addr_t phyaddr;
1793     uint32_t sgecount, sgentry;
1794 
1795     sli4_cfg = &mbox->u.mqe.un.sli4_config;
1796 
1797     /* For embedded mbox command, just free the mbox command */
1798     if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
1799         mempool_free(mbox, phba->mbox_mem_pool);
1800         return;
1801     }
1802 
1803     /* For non-embedded mbox command, we need to free the pages first */
1804     sgecount = bf_get(lpfc_mbox_hdr_sge_cnt, &sli4_cfg->header.cfg_mhdr);
1805     /* There is nothing we can do if there is no sge address array */
1806     if (unlikely(!mbox->sge_array)) {
1807         mempool_free(mbox, phba->mbox_mem_pool);
1808         return;
1809     }
1810     /* Each non-embedded DMA memory was allocated in the length of a page */
1811     for (sgentry = 0; sgentry < sgecount; sgentry++) {
1812         lpfc_sli4_mbx_sge_get(mbox, sgentry, &sge);
1813         phyaddr = getPaddr(sge.pa_hi, sge.pa_lo);
1814         dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
1815                   mbox->sge_array->addr[sgentry], phyaddr);
1816     }
1817     /* Free the sge address array memory */
1818     kfree(mbox->sge_array);
1819     /* Finally, free the mailbox command itself */
1820     mempool_free(mbox, phba->mbox_mem_pool);
1821 }
1822 
1823 /**
1824  * lpfc_sli4_config - Initialize the  SLI4 Config Mailbox command
1825  * @phba: pointer to lpfc hba data structure.
1826  * @mbox: pointer to lpfc mbox command.
1827  * @subsystem: The sli4 config sub mailbox subsystem.
1828  * @opcode: The sli4 config sub mailbox command opcode.
1829  * @length: Length of the sli4 config mailbox command (including sub-header).
1830  * @emb: True if embedded mbox command should be setup.
1831  *
1832  * This routine sets up the header fields of SLI4 specific mailbox command
1833  * for sending IOCTL command.
1834  *
1835  * Return: the actual length of the mbox command allocated (mostly useful
1836  *         for none embedded mailbox command).
1837  **/
1838 int
1839 lpfc_sli4_config(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
1840          uint8_t subsystem, uint8_t opcode, uint32_t length, bool emb)
1841 {
1842     struct lpfc_mbx_sli4_config *sli4_config;
1843     union lpfc_sli4_cfg_shdr *cfg_shdr = NULL;
1844     uint32_t alloc_len;
1845     uint32_t resid_len;
1846     uint32_t pagen, pcount;
1847     void *viraddr;
1848     dma_addr_t phyaddr;
1849 
1850     /* Set up SLI4 mailbox command header fields */
1851     memset(mbox, 0, sizeof(*mbox));
1852     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_SLI4_CONFIG);
1853 
1854     /* Set up SLI4 ioctl command header fields */
1855     sli4_config = &mbox->u.mqe.un.sli4_config;
1856 
1857     /* Setup for the embedded mbox command */
1858     if (emb) {
1859         /* Set up main header fields */
1860         bf_set(lpfc_mbox_hdr_emb, &sli4_config->header.cfg_mhdr, 1);
1861         sli4_config->header.cfg_mhdr.payload_length = length;
1862         /* Set up sub-header fields following main header */
1863         bf_set(lpfc_mbox_hdr_opcode,
1864             &sli4_config->header.cfg_shdr.request, opcode);
1865         bf_set(lpfc_mbox_hdr_subsystem,
1866             &sli4_config->header.cfg_shdr.request, subsystem);
1867         sli4_config->header.cfg_shdr.request.request_length =
1868             length - LPFC_MBX_CMD_HDR_LENGTH;
1869         return length;
1870     }
1871 
1872     /* Setup for the non-embedded mbox command */
1873     pcount = (SLI4_PAGE_ALIGN(length))/SLI4_PAGE_SIZE;
1874     pcount = (pcount > LPFC_SLI4_MBX_SGE_MAX_PAGES) ?
1875                 LPFC_SLI4_MBX_SGE_MAX_PAGES : pcount;
1876     /* Allocate record for keeping SGE virtual addresses */
1877     mbox->sge_array = kzalloc(sizeof(struct lpfc_mbx_nembed_sge_virt),
1878                   GFP_KERNEL);
1879     if (!mbox->sge_array) {
1880         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1881                 "2527 Failed to allocate non-embedded SGE "
1882                 "array.\n");
1883         return 0;
1884     }
1885     for (pagen = 0, alloc_len = 0; pagen < pcount; pagen++) {
1886         /* The DMA memory is always allocated in the length of a
1887          * page even though the last SGE might not fill up to a
1888          * page, this is used as a priori size of SLI4_PAGE_SIZE for
1889          * the later DMA memory free.
1890          */
1891         viraddr = dma_alloc_coherent(&phba->pcidev->dev,
1892                          SLI4_PAGE_SIZE, &phyaddr,
1893                          GFP_KERNEL);
1894         /* In case of malloc fails, proceed with whatever we have */
1895         if (!viraddr)
1896             break;
1897         mbox->sge_array->addr[pagen] = viraddr;
1898         /* Keep the first page for later sub-header construction */
1899         if (pagen == 0)
1900             cfg_shdr = (union lpfc_sli4_cfg_shdr *)viraddr;
1901         resid_len = length - alloc_len;
1902         if (resid_len > SLI4_PAGE_SIZE) {
1903             lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1904                           SLI4_PAGE_SIZE);
1905             alloc_len += SLI4_PAGE_SIZE;
1906         } else {
1907             lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1908                           resid_len);
1909             alloc_len = length;
1910         }
1911     }
1912 
1913     /* Set up main header fields in mailbox command */
1914     sli4_config->header.cfg_mhdr.payload_length = alloc_len;
1915     bf_set(lpfc_mbox_hdr_sge_cnt, &sli4_config->header.cfg_mhdr, pagen);
1916 
1917     /* Set up sub-header fields into the first page */
1918     if (pagen > 0) {
1919         bf_set(lpfc_mbox_hdr_opcode, &cfg_shdr->request, opcode);
1920         bf_set(lpfc_mbox_hdr_subsystem, &cfg_shdr->request, subsystem);
1921         cfg_shdr->request.request_length =
1922                 alloc_len - sizeof(union  lpfc_sli4_cfg_shdr);
1923     }
1924     /* The sub-header is in DMA memory, which needs endian converstion */
1925     if (cfg_shdr)
1926         lpfc_sli_pcimem_bcopy(cfg_shdr, cfg_shdr,
1927                       sizeof(union  lpfc_sli4_cfg_shdr));
1928     return alloc_len;
1929 }
1930 
1931 /**
1932  * lpfc_sli4_mbox_rsrc_extent - Initialize the opcode resource extent.
1933  * @phba: pointer to lpfc hba data structure.
1934  * @mbox: pointer to an allocated lpfc mbox resource.
1935  * @exts_count: the number of extents, if required, to allocate.
1936  * @rsrc_type: the resource extent type.
1937  * @emb: true if LPFC_SLI4_MBX_EMBED. false if LPFC_SLI4_MBX_NEMBED.
1938  *
1939  * This routine completes the subcommand header for SLI4 resource extent
1940  * mailbox commands.  It is called after lpfc_sli4_config.  The caller must
1941  * pass an allocated mailbox and the attributes required to initialize the
1942  * mailbox correctly.
1943  *
1944  * Return: the actual length of the mbox command allocated.
1945  **/
1946 int
1947 lpfc_sli4_mbox_rsrc_extent(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
1948                uint16_t exts_count, uint16_t rsrc_type, bool emb)
1949 {
1950     uint8_t opcode = 0;
1951     struct lpfc_mbx_nembed_rsrc_extent *n_rsrc_extnt = NULL;
1952     void *virtaddr = NULL;
1953 
1954     /* Set up SLI4 ioctl command header fields */
1955     if (emb == LPFC_SLI4_MBX_NEMBED) {
1956         /* Get the first SGE entry from the non-embedded DMA memory */
1957         virtaddr = mbox->sge_array->addr[0];
1958         if (virtaddr == NULL)
1959             return 1;
1960         n_rsrc_extnt = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
1961     }
1962 
1963     /*
1964      * The resource type is common to all extent Opcodes and resides in the
1965      * same position.
1966      */
1967     if (emb == LPFC_SLI4_MBX_EMBED)
1968         bf_set(lpfc_mbx_alloc_rsrc_extents_type,
1969                &mbox->u.mqe.un.alloc_rsrc_extents.u.req,
1970                rsrc_type);
1971     else {
1972         /* This is DMA data.  Byteswap is required. */
1973         bf_set(lpfc_mbx_alloc_rsrc_extents_type,
1974                n_rsrc_extnt, rsrc_type);
1975         lpfc_sli_pcimem_bcopy(&n_rsrc_extnt->word4,
1976                       &n_rsrc_extnt->word4,
1977                       sizeof(uint32_t));
1978     }
1979 
1980     /* Complete the initialization for the particular Opcode. */
1981     opcode = lpfc_sli_config_mbox_opcode_get(phba, mbox);
1982     switch (opcode) {
1983     case LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT:
1984         if (emb == LPFC_SLI4_MBX_EMBED)
1985             bf_set(lpfc_mbx_alloc_rsrc_extents_cnt,
1986                    &mbox->u.mqe.un.alloc_rsrc_extents.u.req,
1987                    exts_count);
1988         else
1989             bf_set(lpfc_mbx_alloc_rsrc_extents_cnt,
1990                    n_rsrc_extnt, exts_count);
1991         break;
1992     case LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT:
1993     case LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO:
1994     case LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT:
1995         /* Initialization is complete.*/
1996         break;
1997     default:
1998         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1999                 "2929 Resource Extent Opcode x%x is "
2000                 "unsupported\n", opcode);
2001         return 1;
2002     }
2003 
2004     return 0;
2005 }
2006 
2007 /**
2008  * lpfc_sli_config_mbox_subsys_get - Get subsystem from a sli_config mbox cmd
2009  * @phba: pointer to lpfc hba data structure.
2010  * @mbox: pointer to lpfc mbox command queue entry.
2011  *
2012  * This routine gets the subsystem from a SLI4 specific SLI_CONFIG mailbox
2013  * command. If the mailbox command is not MBX_SLI4_CONFIG (0x9B) or if the
2014  * sub-header is not present, subsystem LPFC_MBOX_SUBSYSTEM_NA (0x0) shall
2015  * be returned.
2016  **/
2017 uint8_t
2018 lpfc_sli_config_mbox_subsys_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2019 {
2020     struct lpfc_mbx_sli4_config *sli4_cfg;
2021     union lpfc_sli4_cfg_shdr *cfg_shdr;
2022 
2023     if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
2024         return LPFC_MBOX_SUBSYSTEM_NA;
2025     sli4_cfg = &mbox->u.mqe.un.sli4_config;
2026 
2027     /* For embedded mbox command, get opcode from embedded sub-header*/
2028     if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
2029         cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
2030         return bf_get(lpfc_mbox_hdr_subsystem, &cfg_shdr->request);
2031     }
2032 
2033     /* For non-embedded mbox command, get opcode from first dma page */
2034     if (unlikely(!mbox->sge_array))
2035         return LPFC_MBOX_SUBSYSTEM_NA;
2036     cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
2037     return bf_get(lpfc_mbox_hdr_subsystem, &cfg_shdr->request);
2038 }
2039 
2040 /**
2041  * lpfc_sli_config_mbox_opcode_get - Get opcode from a sli_config mbox cmd
2042  * @phba: pointer to lpfc hba data structure.
2043  * @mbox: pointer to lpfc mbox command queue entry.
2044  *
2045  * This routine gets the opcode from a SLI4 specific SLI_CONFIG mailbox
2046  * command. If the mailbox command is not MBX_SLI4_CONFIG (0x9B) or if
2047  * the sub-header is not present, opcode LPFC_MBOX_OPCODE_NA (0x0) be
2048  * returned.
2049  **/
2050 uint8_t
2051 lpfc_sli_config_mbox_opcode_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2052 {
2053     struct lpfc_mbx_sli4_config *sli4_cfg;
2054     union lpfc_sli4_cfg_shdr *cfg_shdr;
2055 
2056     if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
2057         return LPFC_MBOX_OPCODE_NA;
2058     sli4_cfg = &mbox->u.mqe.un.sli4_config;
2059 
2060     /* For embedded mbox command, get opcode from embedded sub-header*/
2061     if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
2062         cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
2063         return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
2064     }
2065 
2066     /* For non-embedded mbox command, get opcode from first dma page */
2067     if (unlikely(!mbox->sge_array))
2068         return LPFC_MBOX_OPCODE_NA;
2069     cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
2070     return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
2071 }
2072 
2073 /**
2074  * lpfc_sli4_mbx_read_fcf_rec - Allocate and construct read fcf mbox cmd
2075  * @phba: pointer to lpfc hba data structure.
2076  * @mboxq: pointer to lpfc mbox command.
2077  * @fcf_index: index to fcf table.
2078  *
2079  * This routine routine allocates and constructs non-embedded mailbox command
2080  * for reading a FCF table entry referred by @fcf_index.
2081  *
2082  * Return: pointer to the mailbox command constructed if successful, otherwise
2083  * NULL.
2084  **/
2085 int
2086 lpfc_sli4_mbx_read_fcf_rec(struct lpfc_hba *phba,
2087                struct lpfcMboxq *mboxq,
2088                uint16_t fcf_index)
2089 {
2090     void *virt_addr;
2091     uint8_t *bytep;
2092     struct lpfc_mbx_sge sge;
2093     uint32_t alloc_len, req_len;
2094     struct lpfc_mbx_read_fcf_tbl *read_fcf;
2095 
2096     if (!mboxq)
2097         return -ENOMEM;
2098 
2099     req_len = sizeof(struct fcf_record) +
2100           sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
2101 
2102     /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
2103     alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2104             LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
2105             LPFC_SLI4_MBX_NEMBED);
2106 
2107     if (alloc_len < req_len) {
2108         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2109                 "0291 Allocated DMA memory size (x%x) is "
2110                 "less than the requested DMA memory "
2111                 "size (x%x)\n", alloc_len, req_len);
2112         return -ENOMEM;
2113     }
2114 
2115     /* Get the first SGE entry from the non-embedded DMA memory. This
2116      * routine only uses a single SGE.
2117      */
2118     lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
2119     virt_addr = mboxq->sge_array->addr[0];
2120     read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
2121 
2122     /* Set up command fields */
2123     bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
2124     /* Perform necessary endian conversion */
2125     bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
2126     lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
2127 
2128     return 0;
2129 }
2130 
2131 /**
2132  * lpfc_request_features: Configure SLI4 REQUEST_FEATURES mailbox
2133  * @phba: pointer to lpfc hba data structure.
2134  * @mboxq: pointer to lpfc mbox command.
2135  *
2136  * This routine sets up the mailbox for an SLI4 REQUEST_FEATURES
2137  * mailbox command.
2138  **/
2139 void
2140 lpfc_request_features(struct lpfc_hba *phba, struct lpfcMboxq *mboxq)
2141 {
2142     /* Set up SLI4 mailbox command header fields */
2143     memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
2144     bf_set(lpfc_mqe_command, &mboxq->u.mqe, MBX_SLI4_REQ_FTRS);
2145 
2146     /* Set up host requested features. */
2147     bf_set(lpfc_mbx_rq_ftr_rq_fcpi, &mboxq->u.mqe.un.req_ftrs, 1);
2148     bf_set(lpfc_mbx_rq_ftr_rq_perfh, &mboxq->u.mqe.un.req_ftrs, 1);
2149 
2150     /* Enable DIF (block guard) only if configured to do so. */
2151     if (phba->cfg_enable_bg)
2152         bf_set(lpfc_mbx_rq_ftr_rq_dif, &mboxq->u.mqe.un.req_ftrs, 1);
2153 
2154     /* Enable NPIV only if configured to do so. */
2155     if (phba->max_vpi && phba->cfg_enable_npiv)
2156         bf_set(lpfc_mbx_rq_ftr_rq_npiv, &mboxq->u.mqe.un.req_ftrs, 1);
2157 
2158     if (phba->nvmet_support) {
2159         bf_set(lpfc_mbx_rq_ftr_rq_mrqp, &mboxq->u.mqe.un.req_ftrs, 1);
2160         /* iaab/iaar NOT set for now */
2161         bf_set(lpfc_mbx_rq_ftr_rq_iaab, &mboxq->u.mqe.un.req_ftrs, 0);
2162         bf_set(lpfc_mbx_rq_ftr_rq_iaar, &mboxq->u.mqe.un.req_ftrs, 0);
2163     }
2164 
2165     /* Enable Application Services Header for appheader VMID */
2166     if (phba->cfg_vmid_app_header) {
2167         bf_set(lpfc_mbx_rq_ftr_rq_ashdr, &mboxq->u.mqe.un.req_ftrs, 1);
2168         bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 1);
2169     }
2170     return;
2171 }
2172 
2173 /**
2174  * lpfc_init_vfi - Initialize the INIT_VFI mailbox command
2175  * @mbox: pointer to lpfc mbox command to initialize.
2176  * @vport: Vport associated with the VF.
2177  *
2178  * This routine initializes @mbox to all zeros and then fills in the mailbox
2179  * fields from @vport. INIT_VFI configures virtual fabrics identified by VFI
2180  * in the context of an FCF. The driver issues this command to setup a VFI
2181  * before issuing a FLOGI to login to the VSAN. The driver should also issue a
2182  * REG_VFI after a successful VSAN login.
2183  **/
2184 void
2185 lpfc_init_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
2186 {
2187     struct lpfc_mbx_init_vfi *init_vfi;
2188 
2189     memset(mbox, 0, sizeof(*mbox));
2190     mbox->vport = vport;
2191     init_vfi = &mbox->u.mqe.un.init_vfi;
2192     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VFI);
2193     bf_set(lpfc_init_vfi_vr, init_vfi, 1);
2194     bf_set(lpfc_init_vfi_vt, init_vfi, 1);
2195     bf_set(lpfc_init_vfi_vp, init_vfi, 1);
2196     bf_set(lpfc_init_vfi_vfi, init_vfi,
2197            vport->phba->sli4_hba.vfi_ids[vport->vfi]);
2198     bf_set(lpfc_init_vfi_vpi, init_vfi,
2199            vport->phba->vpi_ids[vport->vpi]);
2200     bf_set(lpfc_init_vfi_fcfi, init_vfi,
2201            vport->phba->fcf.fcfi);
2202 }
2203 
2204 /**
2205  * lpfc_reg_vfi - Initialize the REG_VFI mailbox command
2206  * @mbox: pointer to lpfc mbox command to initialize.
2207  * @vport: vport associated with the VF.
2208  * @phys: BDE DMA bus address used to send the service parameters to the HBA.
2209  *
2210  * This routine initializes @mbox to all zeros and then fills in the mailbox
2211  * fields from @vport, and uses @buf as a DMAable buffer to send the vport's
2212  * fc service parameters to the HBA for this VFI. REG_VFI configures virtual
2213  * fabrics identified by VFI in the context of an FCF.
2214  **/
2215 void
2216 lpfc_reg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport, dma_addr_t phys)
2217 {
2218     struct lpfc_mbx_reg_vfi *reg_vfi;
2219     struct lpfc_hba *phba = vport->phba;
2220     uint8_t bbscn_fabric = 0, bbscn_max = 0, bbscn_def = 0;
2221 
2222     memset(mbox, 0, sizeof(*mbox));
2223     reg_vfi = &mbox->u.mqe.un.reg_vfi;
2224     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_VFI);
2225     bf_set(lpfc_reg_vfi_vp, reg_vfi, 1);
2226     bf_set(lpfc_reg_vfi_vfi, reg_vfi,
2227            phba->sli4_hba.vfi_ids[vport->vfi]);
2228     bf_set(lpfc_reg_vfi_fcfi, reg_vfi, phba->fcf.fcfi);
2229     bf_set(lpfc_reg_vfi_vpi, reg_vfi, phba->vpi_ids[vport->vpi]);
2230     memcpy(reg_vfi->wwn, &vport->fc_portname, sizeof(struct lpfc_name));
2231     reg_vfi->wwn[0] = cpu_to_le32(reg_vfi->wwn[0]);
2232     reg_vfi->wwn[1] = cpu_to_le32(reg_vfi->wwn[1]);
2233     reg_vfi->e_d_tov = phba->fc_edtov;
2234     reg_vfi->r_a_tov = phba->fc_ratov;
2235     if (phys) {
2236         reg_vfi->bde.addrHigh = putPaddrHigh(phys);
2237         reg_vfi->bde.addrLow = putPaddrLow(phys);
2238         reg_vfi->bde.tus.f.bdeSize = sizeof(vport->fc_sparam);
2239         reg_vfi->bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2240     }
2241     bf_set(lpfc_reg_vfi_nport_id, reg_vfi, vport->fc_myDID);
2242 
2243     /* Only FC supports upd bit */
2244     if ((phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC) &&
2245         (vport->fc_flag & FC_VFI_REGISTERED) &&
2246         (!phba->fc_topology_changed))
2247         bf_set(lpfc_reg_vfi_upd, reg_vfi, 1);
2248 
2249     bf_set(lpfc_reg_vfi_bbcr, reg_vfi, 0);
2250     bf_set(lpfc_reg_vfi_bbscn, reg_vfi, 0);
2251     bbscn_fabric = (phba->fc_fabparam.cmn.bbRcvSizeMsb >> 4) & 0xF;
2252 
2253     if (phba->bbcredit_support && phba->cfg_enable_bbcr  &&
2254         bbscn_fabric != 0) {
2255         bbscn_max = bf_get(lpfc_bbscn_max,
2256                    &phba->sli4_hba.bbscn_params);
2257         if (bbscn_fabric <= bbscn_max) {
2258             bbscn_def = bf_get(lpfc_bbscn_def,
2259                        &phba->sli4_hba.bbscn_params);
2260 
2261             if (bbscn_fabric > bbscn_def)
2262                 bf_set(lpfc_reg_vfi_bbscn, reg_vfi,
2263                        bbscn_fabric);
2264             else
2265                 bf_set(lpfc_reg_vfi_bbscn, reg_vfi, bbscn_def);
2266 
2267             bf_set(lpfc_reg_vfi_bbcr, reg_vfi, 1);
2268         }
2269     }
2270     lpfc_printf_vlog(vport, KERN_INFO, LOG_MBOX,
2271             "3134 Register VFI, mydid:x%x, fcfi:%d, "
2272             " vfi:%d, vpi:%d, fc_pname:%x%x fc_flag:x%x"
2273             " port_state:x%x topology chg:%d bbscn_fabric :%d\n",
2274             vport->fc_myDID,
2275             phba->fcf.fcfi,
2276             phba->sli4_hba.vfi_ids[vport->vfi],
2277             phba->vpi_ids[vport->vpi],
2278             reg_vfi->wwn[0], reg_vfi->wwn[1], vport->fc_flag,
2279             vport->port_state, phba->fc_topology_changed,
2280             bbscn_fabric);
2281 }
2282 
2283 /**
2284  * lpfc_init_vpi - Initialize the INIT_VPI mailbox command
2285  * @phba: pointer to the hba structure to init the VPI for.
2286  * @mbox: pointer to lpfc mbox command to initialize.
2287  * @vpi: VPI to be initialized.
2288  *
2289  * The INIT_VPI mailbox command supports virtual N_Ports. The driver uses the
2290  * command to activate a virtual N_Port. The HBA assigns a MAC address to use
2291  * with the virtual N Port.  The SLI Host issues this command before issuing a
2292  * FDISC to connect to the Fabric. The SLI Host should issue a REG_VPI after a
2293  * successful virtual NPort login.
2294  **/
2295 void
2296 lpfc_init_vpi(struct lpfc_hba *phba, struct lpfcMboxq *mbox, uint16_t vpi)
2297 {
2298     memset(mbox, 0, sizeof(*mbox));
2299     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VPI);
2300     bf_set(lpfc_init_vpi_vpi, &mbox->u.mqe.un.init_vpi,
2301            phba->vpi_ids[vpi]);
2302     bf_set(lpfc_init_vpi_vfi, &mbox->u.mqe.un.init_vpi,
2303            phba->sli4_hba.vfi_ids[phba->pport->vfi]);
2304 }
2305 
2306 /**
2307  * lpfc_unreg_vfi - Initialize the UNREG_VFI mailbox command
2308  * @mbox: pointer to lpfc mbox command to initialize.
2309  * @vport: vport associated with the VF.
2310  *
2311  * The UNREG_VFI mailbox command causes the SLI Host to put a virtual fabric
2312  * (logical NPort) into the inactive state. The SLI Host must have logged out
2313  * and unregistered all remote N_Ports to abort any activity on the virtual
2314  * fabric. The SLI Port posts the mailbox response after marking the virtual
2315  * fabric inactive.
2316  **/
2317 void
2318 lpfc_unreg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
2319 {
2320     memset(mbox, 0, sizeof(*mbox));
2321     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_VFI);
2322     bf_set(lpfc_unreg_vfi_vfi, &mbox->u.mqe.un.unreg_vfi,
2323            vport->phba->sli4_hba.vfi_ids[vport->vfi]);
2324 }
2325 
2326 /**
2327  * lpfc_sli4_dump_cfg_rg23 - Dump sli4 port config region 23
2328  * @phba: pointer to the hba structure containing.
2329  * @mbox: pointer to lpfc mbox command to initialize.
2330  *
2331  * This function create a SLI4 dump mailbox command to dump configure
2332  * region 23.
2333  **/
2334 int
2335 lpfc_sli4_dump_cfg_rg23(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2336 {
2337     struct lpfc_dmabuf *mp = NULL;
2338     MAILBOX_t *mb;
2339     int rc;
2340 
2341     memset(mbox, 0, sizeof(*mbox));
2342     mb = &mbox->u.mb;
2343 
2344     rc = lpfc_mbox_rsrc_prep(phba, mbox);
2345     if (rc) {
2346         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2347                 "2569 %s: memory allocation failed\n",
2348                 __func__);
2349         return 1;
2350     }
2351 
2352     mb->mbxCommand = MBX_DUMP_MEMORY;
2353     mb->un.varDmp.type = DMP_NV_PARAMS;
2354     mb->un.varDmp.region_id = DMP_REGION_23;
2355     mb->un.varDmp.sli4_length = DMP_RGN23_SIZE;
2356     mp = mbox->ctx_buf;
2357     mb->un.varWords[3] = putPaddrLow(mp->phys);
2358     mb->un.varWords[4] = putPaddrHigh(mp->phys);
2359     return 0;
2360 }
2361 
2362 static void
2363 lpfc_mbx_cmpl_rdp_link_stat(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2364 {
2365     MAILBOX_t *mb;
2366     int rc = FAILURE;
2367     struct lpfc_rdp_context *rdp_context =
2368             (struct lpfc_rdp_context *)(mboxq->ctx_ndlp);
2369 
2370     mb = &mboxq->u.mb;
2371     if (mb->mbxStatus)
2372         goto mbx_failed;
2373 
2374     memcpy(&rdp_context->link_stat, &mb->un.varRdLnk, sizeof(READ_LNK_VAR));
2375 
2376     rc = SUCCESS;
2377 
2378 mbx_failed:
2379     lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
2380     rdp_context->cmpl(phba, rdp_context, rc);
2381 }
2382 
2383 static void
2384 lpfc_mbx_cmpl_rdp_page_a2(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2385 {
2386     struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
2387     struct lpfc_rdp_context *rdp_context =
2388             (struct lpfc_rdp_context *)(mbox->ctx_ndlp);
2389 
2390     if (bf_get(lpfc_mqe_status, &mbox->u.mqe))
2391         goto error_mbox_free;
2392 
2393     lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a2,
2394                 DMP_SFF_PAGE_A2_SIZE);
2395 
2396     lpfc_read_lnk_stat(phba, mbox);
2397     mbox->vport = rdp_context->ndlp->vport;
2398 
2399     /* Save the dma buffer for cleanup in the final completion. */
2400     mbox->ctx_buf = mp;
2401     mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_link_stat;
2402     mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
2403     if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) == MBX_NOT_FINISHED)
2404         goto error_mbox_free;
2405 
2406     return;
2407 
2408 error_mbox_free:
2409     lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
2410     rdp_context->cmpl(phba, rdp_context, FAILURE);
2411 }
2412 
2413 void
2414 lpfc_mbx_cmpl_rdp_page_a0(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2415 {
2416     int rc;
2417     struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
2418     struct lpfc_rdp_context *rdp_context =
2419             (struct lpfc_rdp_context *)(mbox->ctx_ndlp);
2420 
2421     if (bf_get(lpfc_mqe_status, &mbox->u.mqe))
2422         goto error;
2423 
2424     lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a0,
2425                 DMP_SFF_PAGE_A0_SIZE);
2426 
2427     memset(mbox, 0, sizeof(*mbox));
2428 
2429     memset(mp->virt, 0, DMP_SFF_PAGE_A2_SIZE);
2430     INIT_LIST_HEAD(&mp->list);
2431 
2432     /* save address for completion */
2433     mbox->ctx_buf = mp;
2434     mbox->vport = rdp_context->ndlp->vport;
2435 
2436     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
2437     bf_set(lpfc_mbx_memory_dump_type3_type,
2438         &mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
2439     bf_set(lpfc_mbx_memory_dump_type3_link,
2440         &mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
2441     bf_set(lpfc_mbx_memory_dump_type3_page_no,
2442         &mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A2);
2443     bf_set(lpfc_mbx_memory_dump_type3_length,
2444         &mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A2_SIZE);
2445     mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
2446     mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
2447 
2448     mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a2;
2449     mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
2450     rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2451     if (rc == MBX_NOT_FINISHED)
2452         goto error;
2453 
2454     return;
2455 
2456 error:
2457     lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
2458     rdp_context->cmpl(phba, rdp_context, FAILURE);
2459 }
2460 
2461 
2462 /*
2463  * lpfc_sli4_dump_page_a0 - Dump sli4 read SFP Diagnostic.
2464  * @phba: pointer to the hba structure containing.
2465  * @mbox: pointer to lpfc mbox command to initialize.
2466  *
2467  * This function create a SLI4 dump mailbox command to dump configure
2468  * type 3 page 0xA0.
2469  */
2470 int
2471 lpfc_sli4_dump_page_a0(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2472 {
2473     int rc;
2474     struct lpfc_dmabuf *mp = NULL;
2475 
2476     memset(mbox, 0, sizeof(*mbox));
2477 
2478     rc = lpfc_mbox_rsrc_prep(phba, mbox);
2479     if (rc) {
2480         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2481             "3569 dump type 3 page 0xA0 allocation failed\n");
2482         return 1;
2483     }
2484 
2485     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
2486     bf_set(lpfc_mbx_memory_dump_type3_type,
2487         &mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
2488     bf_set(lpfc_mbx_memory_dump_type3_link,
2489         &mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
2490     bf_set(lpfc_mbx_memory_dump_type3_page_no,
2491         &mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A0);
2492     bf_set(lpfc_mbx_memory_dump_type3_length,
2493         &mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A0_SIZE);
2494 
2495     mp = mbox->ctx_buf;
2496     mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
2497     mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
2498 
2499     return 0;
2500 }
2501 
2502 /**
2503  * lpfc_reg_fcfi - Initialize the REG_FCFI mailbox command
2504  * @phba: pointer to the hba structure containing the FCF index and RQ ID.
2505  * @mbox: pointer to lpfc mbox command to initialize.
2506  *
2507  * The REG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs). The
2508  * SLI Host uses the command to activate an FCF after it has acquired FCF
2509  * information via a READ_FCF mailbox command. This mailbox command also is used
2510  * to indicate where received unsolicited frames from this FCF will be sent. By
2511  * default this routine will set up the FCF to forward all unsolicited frames
2512  * the the RQ ID passed in the @phba. This can be overridden by the caller for
2513  * more complicated setups.
2514  **/
2515 void
2516 lpfc_reg_fcfi(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2517 {
2518     struct lpfc_mbx_reg_fcfi *reg_fcfi;
2519 
2520     memset(mbox, 0, sizeof(*mbox));
2521     reg_fcfi = &mbox->u.mqe.un.reg_fcfi;
2522     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI);
2523     if (phba->nvmet_support == 0) {
2524         bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi,
2525                phba->sli4_hba.hdr_rq->queue_id);
2526         /* Match everything - rq_id0 */
2527         bf_set(lpfc_reg_fcfi_type_match0, reg_fcfi, 0);
2528         bf_set(lpfc_reg_fcfi_type_mask0, reg_fcfi, 0);
2529         bf_set(lpfc_reg_fcfi_rctl_match0, reg_fcfi, 0);
2530         bf_set(lpfc_reg_fcfi_rctl_mask0, reg_fcfi, 0);
2531 
2532         bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi, REG_FCF_INVALID_QID);
2533 
2534         /* addr mode is bit wise inverted value of fcf addr_mode */
2535         bf_set(lpfc_reg_fcfi_mam, reg_fcfi,
2536                (~phba->fcf.addr_mode) & 0x3);
2537     } else {
2538         /* This is ONLY for NVMET MRQ == 1 */
2539         if (phba->cfg_nvmet_mrq != 1)
2540             return;
2541 
2542         bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi,
2543                phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id);
2544         /* Match type FCP - rq_id0 */
2545         bf_set(lpfc_reg_fcfi_type_match0, reg_fcfi, FC_TYPE_FCP);
2546         bf_set(lpfc_reg_fcfi_type_mask0, reg_fcfi, 0xff);
2547         bf_set(lpfc_reg_fcfi_rctl_match0, reg_fcfi,
2548                FC_RCTL_DD_UNSOL_CMD);
2549 
2550         bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi,
2551                phba->sli4_hba.hdr_rq->queue_id);
2552         /* Match everything else - rq_id1 */
2553         bf_set(lpfc_reg_fcfi_type_match1, reg_fcfi, 0);
2554         bf_set(lpfc_reg_fcfi_type_mask1, reg_fcfi, 0);
2555         bf_set(lpfc_reg_fcfi_rctl_match1, reg_fcfi, 0);
2556         bf_set(lpfc_reg_fcfi_rctl_mask1, reg_fcfi, 0);
2557     }
2558     bf_set(lpfc_reg_fcfi_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
2559     bf_set(lpfc_reg_fcfi_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
2560     bf_set(lpfc_reg_fcfi_info_index, reg_fcfi,
2561            phba->fcf.current_rec.fcf_indx);
2562     if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
2563         bf_set(lpfc_reg_fcfi_vv, reg_fcfi, 1);
2564         bf_set(lpfc_reg_fcfi_vlan_tag, reg_fcfi,
2565                phba->fcf.current_rec.vlan_id);
2566     }
2567 }
2568 
2569 /**
2570  * lpfc_reg_fcfi_mrq - Initialize the REG_FCFI_MRQ mailbox command
2571  * @phba: pointer to the hba structure containing the FCF index and RQ ID.
2572  * @mbox: pointer to lpfc mbox command to initialize.
2573  * @mode: 0 to register FCFI, 1 to register MRQs
2574  *
2575  * The REG_FCFI_MRQ mailbox command supports Fibre Channel Forwarders (FCFs).
2576  * The SLI Host uses the command to activate an FCF after it has acquired FCF
2577  * information via a READ_FCF mailbox command. This mailbox command also is used
2578  * to indicate where received unsolicited frames from this FCF will be sent. By
2579  * default this routine will set up the FCF to forward all unsolicited frames
2580  * the the RQ ID passed in the @phba. This can be overridden by the caller for
2581  * more complicated setups.
2582  **/
2583 void
2584 lpfc_reg_fcfi_mrq(struct lpfc_hba *phba, struct lpfcMboxq *mbox, int mode)
2585 {
2586     struct lpfc_mbx_reg_fcfi_mrq *reg_fcfi;
2587 
2588     /* This is ONLY for MRQ */
2589     if (phba->cfg_nvmet_mrq <= 1)
2590         return;
2591 
2592     memset(mbox, 0, sizeof(*mbox));
2593     reg_fcfi = &mbox->u.mqe.un.reg_fcfi_mrq;
2594     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI_MRQ);
2595     if (mode == 0) {
2596         bf_set(lpfc_reg_fcfi_mrq_info_index, reg_fcfi,
2597                phba->fcf.current_rec.fcf_indx);
2598         if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
2599             bf_set(lpfc_reg_fcfi_mrq_vv, reg_fcfi, 1);
2600             bf_set(lpfc_reg_fcfi_mrq_vlan_tag, reg_fcfi,
2601                    phba->fcf.current_rec.vlan_id);
2602         }
2603         return;
2604     }
2605 
2606     bf_set(lpfc_reg_fcfi_mrq_rq_id0, reg_fcfi,
2607            phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id);
2608     /* Match NVME frames of type FCP (protocol NVME) - rq_id0 */
2609     bf_set(lpfc_reg_fcfi_mrq_type_match0, reg_fcfi, FC_TYPE_FCP);
2610     bf_set(lpfc_reg_fcfi_mrq_type_mask0, reg_fcfi, 0xff);
2611     bf_set(lpfc_reg_fcfi_mrq_rctl_match0, reg_fcfi, FC_RCTL_DD_UNSOL_CMD);
2612     bf_set(lpfc_reg_fcfi_mrq_rctl_mask0, reg_fcfi, 0xff);
2613     bf_set(lpfc_reg_fcfi_mrq_ptc0, reg_fcfi, 1);
2614     bf_set(lpfc_reg_fcfi_mrq_pt0, reg_fcfi, 1);
2615 
2616     bf_set(lpfc_reg_fcfi_mrq_policy, reg_fcfi, 3); /* NVME connection id */
2617     bf_set(lpfc_reg_fcfi_mrq_mode, reg_fcfi, 1);
2618     bf_set(lpfc_reg_fcfi_mrq_filter, reg_fcfi, 1); /* rq_id0 */
2619     bf_set(lpfc_reg_fcfi_mrq_npairs, reg_fcfi, phba->cfg_nvmet_mrq);
2620 
2621     bf_set(lpfc_reg_fcfi_mrq_rq_id1, reg_fcfi,
2622            phba->sli4_hba.hdr_rq->queue_id);
2623     /* Match everything - rq_id1 */
2624     bf_set(lpfc_reg_fcfi_mrq_type_match1, reg_fcfi, 0);
2625     bf_set(lpfc_reg_fcfi_mrq_type_mask1, reg_fcfi, 0);
2626     bf_set(lpfc_reg_fcfi_mrq_rctl_match1, reg_fcfi, 0);
2627     bf_set(lpfc_reg_fcfi_mrq_rctl_mask1, reg_fcfi, 0);
2628 
2629     bf_set(lpfc_reg_fcfi_mrq_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
2630     bf_set(lpfc_reg_fcfi_mrq_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
2631 }
2632 
2633 /**
2634  * lpfc_unreg_fcfi - Initialize the UNREG_FCFI mailbox command
2635  * @mbox: pointer to lpfc mbox command to initialize.
2636  * @fcfi: FCFI to be unregistered.
2637  *
2638  * The UNREG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs).
2639  * The SLI Host uses the command to inactivate an FCFI.
2640  **/
2641 void
2642 lpfc_unreg_fcfi(struct lpfcMboxq *mbox, uint16_t fcfi)
2643 {
2644     memset(mbox, 0, sizeof(*mbox));
2645     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_FCFI);
2646     bf_set(lpfc_unreg_fcfi, &mbox->u.mqe.un.unreg_fcfi, fcfi);
2647 }
2648 
2649 /**
2650  * lpfc_resume_rpi - Initialize the RESUME_RPI mailbox command
2651  * @mbox: pointer to lpfc mbox command to initialize.
2652  * @ndlp: The nodelist structure that describes the RPI to resume.
2653  *
2654  * The RESUME_RPI mailbox command is used to restart I/O to an RPI after a
2655  * link event.
2656  **/
2657 void
2658 lpfc_resume_rpi(struct lpfcMboxq *mbox, struct lpfc_nodelist *ndlp)
2659 {
2660     struct lpfc_hba *phba = ndlp->phba;
2661     struct lpfc_mbx_resume_rpi *resume_rpi;
2662 
2663     memset(mbox, 0, sizeof(*mbox));
2664     resume_rpi = &mbox->u.mqe.un.resume_rpi;
2665     bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_RESUME_RPI);
2666     bf_set(lpfc_resume_rpi_index, resume_rpi,
2667            phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2668     bf_set(lpfc_resume_rpi_ii, resume_rpi, RESUME_INDEX_RPI);
2669     resume_rpi->event_tag = ndlp->phba->fc_eventTag;
2670 }
2671