Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * QLogic Fibre Channel HBA Driver
0004  * Copyright (c)  2003-2014 QLogic Corporation
0005  */
0006 #include "qla_def.h"
0007 #include "qla_target.h"
0008 
0009 #include <linux/kthread.h>
0010 #include <linux/vmalloc.h>
0011 #include <linux/slab.h>
0012 #include <linux/delay.h>
0013 
0014 static int qla24xx_vport_disable(struct fc_vport *, bool);
0015 
0016 /* SYSFS attributes --------------------------------------------------------- */
0017 
0018 static ssize_t
0019 qla2x00_sysfs_read_fw_dump(struct file *filp, struct kobject *kobj,
0020                struct bin_attribute *bin_attr,
0021                char *buf, loff_t off, size_t count)
0022 {
0023     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0024         struct device, kobj)));
0025     struct qla_hw_data *ha = vha->hw;
0026     int rval = 0;
0027 
0028     if (!(ha->fw_dump_reading || ha->mctp_dump_reading ||
0029           ha->mpi_fw_dump_reading))
0030         return 0;
0031 
0032     mutex_lock(&ha->optrom_mutex);
0033     if (IS_P3P_TYPE(ha)) {
0034         if (off < ha->md_template_size) {
0035             rval = memory_read_from_buffer(buf, count,
0036                 &off, ha->md_tmplt_hdr, ha->md_template_size);
0037         } else {
0038             off -= ha->md_template_size;
0039             rval = memory_read_from_buffer(buf, count,
0040                 &off, ha->md_dump, ha->md_dump_size);
0041         }
0042     } else if (ha->mctp_dumped && ha->mctp_dump_reading) {
0043         rval = memory_read_from_buffer(buf, count, &off, ha->mctp_dump,
0044             MCTP_DUMP_SIZE);
0045     } else if (ha->mpi_fw_dumped && ha->mpi_fw_dump_reading) {
0046         rval = memory_read_from_buffer(buf, count, &off,
0047                            ha->mpi_fw_dump,
0048                            ha->mpi_fw_dump_len);
0049     } else if (ha->fw_dump_reading) {
0050         rval = memory_read_from_buffer(buf, count, &off, ha->fw_dump,
0051                     ha->fw_dump_len);
0052     } else {
0053         rval = 0;
0054     }
0055     mutex_unlock(&ha->optrom_mutex);
0056     return rval;
0057 }
0058 
0059 static ssize_t
0060 qla2x00_sysfs_write_fw_dump(struct file *filp, struct kobject *kobj,
0061                 struct bin_attribute *bin_attr,
0062                 char *buf, loff_t off, size_t count)
0063 {
0064     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0065         struct device, kobj)));
0066     struct qla_hw_data *ha = vha->hw;
0067     int reading;
0068 
0069     if (off != 0)
0070         return (0);
0071 
0072     reading = simple_strtol(buf, NULL, 10);
0073     switch (reading) {
0074     case 0:
0075         if (!ha->fw_dump_reading)
0076             break;
0077 
0078         ql_log(ql_log_info, vha, 0x705d,
0079             "Firmware dump cleared on (%ld).\n", vha->host_no);
0080 
0081         if (IS_P3P_TYPE(ha)) {
0082             qla82xx_md_free(vha);
0083             qla82xx_md_prep(vha);
0084         }
0085         ha->fw_dump_reading = 0;
0086         ha->fw_dumped = false;
0087         break;
0088     case 1:
0089         if (ha->fw_dumped && !ha->fw_dump_reading) {
0090             ha->fw_dump_reading = 1;
0091 
0092             ql_log(ql_log_info, vha, 0x705e,
0093                 "Raw firmware dump ready for read on (%ld).\n",
0094                 vha->host_no);
0095         }
0096         break;
0097     case 2:
0098         qla2x00_alloc_fw_dump(vha);
0099         break;
0100     case 3:
0101         if (IS_QLA82XX(ha)) {
0102             qla82xx_idc_lock(ha);
0103             qla82xx_set_reset_owner(vha);
0104             qla82xx_idc_unlock(ha);
0105         } else if (IS_QLA8044(ha)) {
0106             qla8044_idc_lock(ha);
0107             qla82xx_set_reset_owner(vha);
0108             qla8044_idc_unlock(ha);
0109         } else {
0110             qla2x00_system_error(vha);
0111         }
0112         break;
0113     case 4:
0114         if (IS_P3P_TYPE(ha)) {
0115             if (ha->md_tmplt_hdr)
0116                 ql_dbg(ql_dbg_user, vha, 0x705b,
0117                     "MiniDump supported with this firmware.\n");
0118             else
0119                 ql_dbg(ql_dbg_user, vha, 0x709d,
0120                     "MiniDump not supported with this firmware.\n");
0121         }
0122         break;
0123     case 5:
0124         if (IS_P3P_TYPE(ha))
0125             set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
0126         break;
0127     case 6:
0128         if (!ha->mctp_dump_reading)
0129             break;
0130         ql_log(ql_log_info, vha, 0x70c1,
0131             "MCTP dump cleared on (%ld).\n", vha->host_no);
0132         ha->mctp_dump_reading = 0;
0133         ha->mctp_dumped = 0;
0134         break;
0135     case 7:
0136         if (ha->mctp_dumped && !ha->mctp_dump_reading) {
0137             ha->mctp_dump_reading = 1;
0138             ql_log(ql_log_info, vha, 0x70c2,
0139                 "Raw mctp dump ready for read on (%ld).\n",
0140                 vha->host_no);
0141         }
0142         break;
0143     case 8:
0144         if (!ha->mpi_fw_dump_reading)
0145             break;
0146         ql_log(ql_log_info, vha, 0x70e7,
0147                "MPI firmware dump cleared on (%ld).\n", vha->host_no);
0148         ha->mpi_fw_dump_reading = 0;
0149         ha->mpi_fw_dumped = 0;
0150         break;
0151     case 9:
0152         if (ha->mpi_fw_dumped && !ha->mpi_fw_dump_reading) {
0153             ha->mpi_fw_dump_reading = 1;
0154             ql_log(ql_log_info, vha, 0x70e8,
0155                    "Raw MPI firmware dump ready for read on (%ld).\n",
0156                    vha->host_no);
0157         }
0158         break;
0159     case 10:
0160         if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
0161             ql_log(ql_log_info, vha, 0x70e9,
0162                    "Issuing MPI firmware dump on host#%ld.\n",
0163                    vha->host_no);
0164             ha->isp_ops->mpi_fw_dump(vha, 0);
0165         }
0166         break;
0167     }
0168     return count;
0169 }
0170 
0171 static struct bin_attribute sysfs_fw_dump_attr = {
0172     .attr = {
0173         .name = "fw_dump",
0174         .mode = S_IRUSR | S_IWUSR,
0175     },
0176     .size = 0,
0177     .read = qla2x00_sysfs_read_fw_dump,
0178     .write = qla2x00_sysfs_write_fw_dump,
0179 };
0180 
0181 static ssize_t
0182 qla2x00_sysfs_read_nvram(struct file *filp, struct kobject *kobj,
0183              struct bin_attribute *bin_attr,
0184              char *buf, loff_t off, size_t count)
0185 {
0186     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0187         struct device, kobj)));
0188     struct qla_hw_data *ha = vha->hw;
0189     uint32_t faddr;
0190     struct active_regions active_regions = { };
0191 
0192     if (!capable(CAP_SYS_ADMIN))
0193         return 0;
0194 
0195     mutex_lock(&ha->optrom_mutex);
0196     if (qla2x00_chip_is_down(vha)) {
0197         mutex_unlock(&ha->optrom_mutex);
0198         return -EAGAIN;
0199     }
0200 
0201     if (!IS_NOCACHE_VPD_TYPE(ha)) {
0202         mutex_unlock(&ha->optrom_mutex);
0203         goto skip;
0204     }
0205 
0206     faddr = ha->flt_region_nvram;
0207     if (IS_QLA28XX(ha)) {
0208         qla28xx_get_aux_images(vha, &active_regions);
0209         if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
0210             faddr = ha->flt_region_nvram_sec;
0211     }
0212     ha->isp_ops->read_optrom(vha, ha->nvram, faddr << 2, ha->nvram_size);
0213 
0214     mutex_unlock(&ha->optrom_mutex);
0215 
0216 skip:
0217     return memory_read_from_buffer(buf, count, &off, ha->nvram,
0218                     ha->nvram_size);
0219 }
0220 
0221 static ssize_t
0222 qla2x00_sysfs_write_nvram(struct file *filp, struct kobject *kobj,
0223               struct bin_attribute *bin_attr,
0224               char *buf, loff_t off, size_t count)
0225 {
0226     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0227         struct device, kobj)));
0228     struct qla_hw_data *ha = vha->hw;
0229     uint16_t    cnt;
0230 
0231     if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size ||
0232         !ha->isp_ops->write_nvram)
0233         return -EINVAL;
0234 
0235     /* Checksum NVRAM. */
0236     if (IS_FWI2_CAPABLE(ha)) {
0237         __le32 *iter = (__force __le32 *)buf;
0238         uint32_t chksum;
0239 
0240         chksum = 0;
0241         for (cnt = 0; cnt < ((count >> 2) - 1); cnt++, iter++)
0242             chksum += le32_to_cpu(*iter);
0243         chksum = ~chksum + 1;
0244         *iter = cpu_to_le32(chksum);
0245     } else {
0246         uint8_t *iter;
0247         uint8_t chksum;
0248 
0249         iter = (uint8_t *)buf;
0250         chksum = 0;
0251         for (cnt = 0; cnt < count - 1; cnt++)
0252             chksum += *iter++;
0253         chksum = ~chksum + 1;
0254         *iter = chksum;
0255     }
0256 
0257     if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
0258         ql_log(ql_log_warn, vha, 0x705f,
0259             "HBA not online, failing NVRAM update.\n");
0260         return -EAGAIN;
0261     }
0262 
0263     mutex_lock(&ha->optrom_mutex);
0264     if (qla2x00_chip_is_down(vha)) {
0265         mutex_unlock(&ha->optrom_mutex);
0266         return -EAGAIN;
0267     }
0268 
0269     /* Write NVRAM. */
0270     ha->isp_ops->write_nvram(vha, buf, ha->nvram_base, count);
0271     ha->isp_ops->read_nvram(vha, ha->nvram, ha->nvram_base,
0272         count);
0273     mutex_unlock(&ha->optrom_mutex);
0274 
0275     ql_dbg(ql_dbg_user, vha, 0x7060,
0276         "Setting ISP_ABORT_NEEDED\n");
0277     /* NVRAM settings take effect immediately. */
0278     set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
0279     qla2xxx_wake_dpc(vha);
0280     qla2x00_wait_for_chip_reset(vha);
0281 
0282     return count;
0283 }
0284 
0285 static struct bin_attribute sysfs_nvram_attr = {
0286     .attr = {
0287         .name = "nvram",
0288         .mode = S_IRUSR | S_IWUSR,
0289     },
0290     .size = 512,
0291     .read = qla2x00_sysfs_read_nvram,
0292     .write = qla2x00_sysfs_write_nvram,
0293 };
0294 
0295 static ssize_t
0296 qla2x00_sysfs_read_optrom(struct file *filp, struct kobject *kobj,
0297               struct bin_attribute *bin_attr,
0298               char *buf, loff_t off, size_t count)
0299 {
0300     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0301         struct device, kobj)));
0302     struct qla_hw_data *ha = vha->hw;
0303     ssize_t rval = 0;
0304 
0305     mutex_lock(&ha->optrom_mutex);
0306 
0307     if (ha->optrom_state != QLA_SREADING)
0308         goto out;
0309 
0310     rval = memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
0311         ha->optrom_region_size);
0312 
0313 out:
0314     mutex_unlock(&ha->optrom_mutex);
0315 
0316     return rval;
0317 }
0318 
0319 static ssize_t
0320 qla2x00_sysfs_write_optrom(struct file *filp, struct kobject *kobj,
0321                struct bin_attribute *bin_attr,
0322                char *buf, loff_t off, size_t count)
0323 {
0324     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0325         struct device, kobj)));
0326     struct qla_hw_data *ha = vha->hw;
0327 
0328     mutex_lock(&ha->optrom_mutex);
0329 
0330     if (ha->optrom_state != QLA_SWRITING) {
0331         mutex_unlock(&ha->optrom_mutex);
0332         return -EINVAL;
0333     }
0334     if (off > ha->optrom_region_size) {
0335         mutex_unlock(&ha->optrom_mutex);
0336         return -ERANGE;
0337     }
0338     if (off + count > ha->optrom_region_size)
0339         count = ha->optrom_region_size - off;
0340 
0341     memcpy(&ha->optrom_buffer[off], buf, count);
0342     mutex_unlock(&ha->optrom_mutex);
0343 
0344     return count;
0345 }
0346 
0347 static struct bin_attribute sysfs_optrom_attr = {
0348     .attr = {
0349         .name = "optrom",
0350         .mode = S_IRUSR | S_IWUSR,
0351     },
0352     .size = 0,
0353     .read = qla2x00_sysfs_read_optrom,
0354     .write = qla2x00_sysfs_write_optrom,
0355 };
0356 
0357 static ssize_t
0358 qla2x00_sysfs_write_optrom_ctl(struct file *filp, struct kobject *kobj,
0359                    struct bin_attribute *bin_attr,
0360                    char *buf, loff_t off, size_t count)
0361 {
0362     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0363         struct device, kobj)));
0364     struct qla_hw_data *ha = vha->hw;
0365     uint32_t start = 0;
0366     uint32_t size = ha->optrom_size;
0367     int val, valid;
0368     ssize_t rval = count;
0369 
0370     if (off)
0371         return -EINVAL;
0372 
0373     if (unlikely(pci_channel_offline(ha->pdev)))
0374         return -EAGAIN;
0375 
0376     if (sscanf(buf, "%d:%x:%x", &val, &start, &size) < 1)
0377         return -EINVAL;
0378     if (start > ha->optrom_size)
0379         return -EINVAL;
0380     if (size > ha->optrom_size - start)
0381         size = ha->optrom_size - start;
0382 
0383     mutex_lock(&ha->optrom_mutex);
0384     if (qla2x00_chip_is_down(vha)) {
0385         mutex_unlock(&ha->optrom_mutex);
0386         return -EAGAIN;
0387     }
0388     switch (val) {
0389     case 0:
0390         if (ha->optrom_state != QLA_SREADING &&
0391             ha->optrom_state != QLA_SWRITING) {
0392             rval =  -EINVAL;
0393             goto out;
0394         }
0395         ha->optrom_state = QLA_SWAITING;
0396 
0397         ql_dbg(ql_dbg_user, vha, 0x7061,
0398             "Freeing flash region allocation -- 0x%x bytes.\n",
0399             ha->optrom_region_size);
0400 
0401         vfree(ha->optrom_buffer);
0402         ha->optrom_buffer = NULL;
0403         break;
0404     case 1:
0405         if (ha->optrom_state != QLA_SWAITING) {
0406             rval = -EINVAL;
0407             goto out;
0408         }
0409 
0410         ha->optrom_region_start = start;
0411         ha->optrom_region_size = size;
0412 
0413         ha->optrom_state = QLA_SREADING;
0414         ha->optrom_buffer = vzalloc(ha->optrom_region_size);
0415         if (ha->optrom_buffer == NULL) {
0416             ql_log(ql_log_warn, vha, 0x7062,
0417                 "Unable to allocate memory for optrom retrieval "
0418                 "(%x).\n", ha->optrom_region_size);
0419 
0420             ha->optrom_state = QLA_SWAITING;
0421             rval = -ENOMEM;
0422             goto out;
0423         }
0424 
0425         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
0426             ql_log(ql_log_warn, vha, 0x7063,
0427                 "HBA not online, failing NVRAM update.\n");
0428             rval = -EAGAIN;
0429             goto out;
0430         }
0431 
0432         ql_dbg(ql_dbg_user, vha, 0x7064,
0433             "Reading flash region -- 0x%x/0x%x.\n",
0434             ha->optrom_region_start, ha->optrom_region_size);
0435 
0436         ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
0437             ha->optrom_region_start, ha->optrom_region_size);
0438         break;
0439     case 2:
0440         if (ha->optrom_state != QLA_SWAITING) {
0441             rval = -EINVAL;
0442             goto out;
0443         }
0444 
0445         /*
0446          * We need to be more restrictive on which FLASH regions are
0447          * allowed to be updated via user-space.  Regions accessible
0448          * via this method include:
0449          *
0450          * ISP21xx/ISP22xx/ISP23xx type boards:
0451          *
0452          *  0x000000 -> 0x020000 -- Boot code.
0453          *
0454          * ISP2322/ISP24xx type boards:
0455          *
0456          *  0x000000 -> 0x07ffff -- Boot code.
0457          *  0x080000 -> 0x0fffff -- Firmware.
0458          *
0459          * ISP25xx type boards:
0460          *
0461          *  0x000000 -> 0x07ffff -- Boot code.
0462          *  0x080000 -> 0x0fffff -- Firmware.
0463          *  0x120000 -> 0x12ffff -- VPD and HBA parameters.
0464          *
0465          * > ISP25xx type boards:
0466          *
0467          *      None -- should go through BSG.
0468          */
0469         valid = 0;
0470         if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
0471             valid = 1;
0472         else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
0473             valid = 1;
0474         if (!valid) {
0475             ql_log(ql_log_warn, vha, 0x7065,
0476                 "Invalid start region 0x%x/0x%x.\n", start, size);
0477             rval = -EINVAL;
0478             goto out;
0479         }
0480 
0481         ha->optrom_region_start = start;
0482         ha->optrom_region_size = size;
0483 
0484         ha->optrom_state = QLA_SWRITING;
0485         ha->optrom_buffer = vzalloc(ha->optrom_region_size);
0486         if (ha->optrom_buffer == NULL) {
0487             ql_log(ql_log_warn, vha, 0x7066,
0488                 "Unable to allocate memory for optrom update "
0489                 "(%x)\n", ha->optrom_region_size);
0490 
0491             ha->optrom_state = QLA_SWAITING;
0492             rval = -ENOMEM;
0493             goto out;
0494         }
0495 
0496         ql_dbg(ql_dbg_user, vha, 0x7067,
0497             "Staging flash region write -- 0x%x/0x%x.\n",
0498             ha->optrom_region_start, ha->optrom_region_size);
0499 
0500         break;
0501     case 3:
0502         if (ha->optrom_state != QLA_SWRITING) {
0503             rval = -EINVAL;
0504             goto out;
0505         }
0506 
0507         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
0508             ql_log(ql_log_warn, vha, 0x7068,
0509                 "HBA not online, failing flash update.\n");
0510             rval = -EAGAIN;
0511             goto out;
0512         }
0513 
0514         ql_dbg(ql_dbg_user, vha, 0x7069,
0515             "Writing flash region -- 0x%x/0x%x.\n",
0516             ha->optrom_region_start, ha->optrom_region_size);
0517 
0518         rval = ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
0519             ha->optrom_region_start, ha->optrom_region_size);
0520         if (rval)
0521             rval = -EIO;
0522         break;
0523     default:
0524         rval = -EINVAL;
0525     }
0526 
0527 out:
0528     mutex_unlock(&ha->optrom_mutex);
0529     return rval;
0530 }
0531 
0532 static struct bin_attribute sysfs_optrom_ctl_attr = {
0533     .attr = {
0534         .name = "optrom_ctl",
0535         .mode = S_IWUSR,
0536     },
0537     .size = 0,
0538     .write = qla2x00_sysfs_write_optrom_ctl,
0539 };
0540 
0541 static ssize_t
0542 qla2x00_sysfs_read_vpd(struct file *filp, struct kobject *kobj,
0543                struct bin_attribute *bin_attr,
0544                char *buf, loff_t off, size_t count)
0545 {
0546     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0547         struct device, kobj)));
0548     struct qla_hw_data *ha = vha->hw;
0549     uint32_t faddr;
0550     struct active_regions active_regions = { };
0551 
0552     if (unlikely(pci_channel_offline(ha->pdev)))
0553         return -EAGAIN;
0554 
0555     if (!capable(CAP_SYS_ADMIN))
0556         return -EINVAL;
0557 
0558     if (!IS_NOCACHE_VPD_TYPE(ha))
0559         goto skip;
0560 
0561     faddr = ha->flt_region_vpd << 2;
0562 
0563     if (IS_QLA28XX(ha)) {
0564         qla28xx_get_aux_images(vha, &active_regions);
0565         if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
0566             faddr = ha->flt_region_vpd_sec << 2;
0567 
0568         ql_dbg(ql_dbg_init, vha, 0x7070,
0569             "Loading %s nvram image.\n",
0570             active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
0571             "primary" : "secondary");
0572     }
0573 
0574     mutex_lock(&ha->optrom_mutex);
0575     if (qla2x00_chip_is_down(vha)) {
0576         mutex_unlock(&ha->optrom_mutex);
0577         return -EAGAIN;
0578     }
0579 
0580     ha->isp_ops->read_optrom(vha, ha->vpd, faddr, ha->vpd_size);
0581     mutex_unlock(&ha->optrom_mutex);
0582 
0583     ha->isp_ops->read_optrom(vha, ha->vpd, faddr, ha->vpd_size);
0584 skip:
0585     return memory_read_from_buffer(buf, count, &off, ha->vpd, ha->vpd_size);
0586 }
0587 
0588 static ssize_t
0589 qla2x00_sysfs_write_vpd(struct file *filp, struct kobject *kobj,
0590             struct bin_attribute *bin_attr,
0591             char *buf, loff_t off, size_t count)
0592 {
0593     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0594         struct device, kobj)));
0595     struct qla_hw_data *ha = vha->hw;
0596     uint8_t *tmp_data;
0597 
0598     if (unlikely(pci_channel_offline(ha->pdev)))
0599         return 0;
0600 
0601     if (qla2x00_chip_is_down(vha))
0602         return 0;
0603 
0604     if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size ||
0605         !ha->isp_ops->write_nvram)
0606         return 0;
0607 
0608     if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
0609         ql_log(ql_log_warn, vha, 0x706a,
0610             "HBA not online, failing VPD update.\n");
0611         return -EAGAIN;
0612     }
0613 
0614     mutex_lock(&ha->optrom_mutex);
0615     if (qla2x00_chip_is_down(vha)) {
0616         mutex_unlock(&ha->optrom_mutex);
0617         return -EAGAIN;
0618     }
0619 
0620     /* Write NVRAM. */
0621     ha->isp_ops->write_nvram(vha, buf, ha->vpd_base, count);
0622     ha->isp_ops->read_nvram(vha, ha->vpd, ha->vpd_base, count);
0623 
0624     /* Update flash version information for 4Gb & above. */
0625     if (!IS_FWI2_CAPABLE(ha)) {
0626         mutex_unlock(&ha->optrom_mutex);
0627         return -EINVAL;
0628     }
0629 
0630     tmp_data = vmalloc(256);
0631     if (!tmp_data) {
0632         mutex_unlock(&ha->optrom_mutex);
0633         ql_log(ql_log_warn, vha, 0x706b,
0634             "Unable to allocate memory for VPD information update.\n");
0635         return -ENOMEM;
0636     }
0637     ha->isp_ops->get_flash_version(vha, tmp_data);
0638     vfree(tmp_data);
0639 
0640     mutex_unlock(&ha->optrom_mutex);
0641 
0642     return count;
0643 }
0644 
0645 static struct bin_attribute sysfs_vpd_attr = {
0646     .attr = {
0647         .name = "vpd",
0648         .mode = S_IRUSR | S_IWUSR,
0649     },
0650     .size = 0,
0651     .read = qla2x00_sysfs_read_vpd,
0652     .write = qla2x00_sysfs_write_vpd,
0653 };
0654 
0655 static ssize_t
0656 qla2x00_sysfs_read_sfp(struct file *filp, struct kobject *kobj,
0657                struct bin_attribute *bin_attr,
0658                char *buf, loff_t off, size_t count)
0659 {
0660     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0661         struct device, kobj)));
0662     int rval;
0663 
0664     if (!capable(CAP_SYS_ADMIN) || off != 0 || count < SFP_DEV_SIZE)
0665         return 0;
0666 
0667     mutex_lock(&vha->hw->optrom_mutex);
0668     if (qla2x00_chip_is_down(vha)) {
0669         mutex_unlock(&vha->hw->optrom_mutex);
0670         return 0;
0671     }
0672 
0673     rval = qla2x00_read_sfp_dev(vha, buf, count);
0674     mutex_unlock(&vha->hw->optrom_mutex);
0675 
0676     if (rval)
0677         return -EIO;
0678 
0679     return count;
0680 }
0681 
0682 static struct bin_attribute sysfs_sfp_attr = {
0683     .attr = {
0684         .name = "sfp",
0685         .mode = S_IRUSR | S_IWUSR,
0686     },
0687     .size = SFP_DEV_SIZE,
0688     .read = qla2x00_sysfs_read_sfp,
0689 };
0690 
0691 static ssize_t
0692 qla2x00_sysfs_write_reset(struct file *filp, struct kobject *kobj,
0693             struct bin_attribute *bin_attr,
0694             char *buf, loff_t off, size_t count)
0695 {
0696     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0697         struct device, kobj)));
0698     struct qla_hw_data *ha = vha->hw;
0699     struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
0700     int type;
0701     uint32_t idc_control;
0702     uint8_t *tmp_data = NULL;
0703 
0704     if (off != 0)
0705         return -EINVAL;
0706 
0707     type = simple_strtol(buf, NULL, 10);
0708     switch (type) {
0709     case 0x2025c:
0710         ql_log(ql_log_info, vha, 0x706e,
0711             "Issuing ISP reset.\n");
0712 
0713         if (vha->hw->flags.port_isolated) {
0714             ql_log(ql_log_info, vha, 0x706e,
0715                    "Port is isolated, returning.\n");
0716             return -EINVAL;
0717         }
0718 
0719         scsi_block_requests(vha->host);
0720         if (IS_QLA82XX(ha)) {
0721             ha->flags.isp82xx_no_md_cap = 1;
0722             qla82xx_idc_lock(ha);
0723             qla82xx_set_reset_owner(vha);
0724             qla82xx_idc_unlock(ha);
0725         } else if (IS_QLA8044(ha)) {
0726             qla8044_idc_lock(ha);
0727             idc_control = qla8044_rd_reg(ha,
0728                 QLA8044_IDC_DRV_CTRL);
0729             qla8044_wr_reg(ha, QLA8044_IDC_DRV_CTRL,
0730                 (idc_control | GRACEFUL_RESET_BIT1));
0731             qla82xx_set_reset_owner(vha);
0732             qla8044_idc_unlock(ha);
0733         } else {
0734             set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
0735             qla2xxx_wake_dpc(vha);
0736         }
0737         qla2x00_wait_for_chip_reset(vha);
0738         scsi_unblock_requests(vha->host);
0739         break;
0740     case 0x2025d:
0741         if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
0742             !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
0743             return -EPERM;
0744 
0745         ql_log(ql_log_info, vha, 0x706f,
0746             "Issuing MPI reset.\n");
0747 
0748         if (IS_QLA83XX(ha)) {
0749             uint32_t idc_control;
0750 
0751             qla83xx_idc_lock(vha, 0);
0752             __qla83xx_get_idc_control(vha, &idc_control);
0753             idc_control |= QLA83XX_IDC_GRACEFUL_RESET;
0754             __qla83xx_set_idc_control(vha, idc_control);
0755             qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
0756                 QLA8XXX_DEV_NEED_RESET);
0757             qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
0758             qla83xx_idc_unlock(vha, 0);
0759             break;
0760         } else {
0761             /* Make sure FC side is not in reset */
0762             WARN_ON_ONCE(qla2x00_wait_for_hba_online(vha) !=
0763                      QLA_SUCCESS);
0764 
0765             /* Issue MPI reset */
0766             scsi_block_requests(vha->host);
0767             if (qla81xx_restart_mpi_firmware(vha) != QLA_SUCCESS)
0768                 ql_log(ql_log_warn, vha, 0x7070,
0769                     "MPI reset failed.\n");
0770             scsi_unblock_requests(vha->host);
0771             break;
0772         }
0773         break;
0774     case 0x2025e:
0775         if (!IS_P3P_TYPE(ha) || vha != base_vha) {
0776             ql_log(ql_log_info, vha, 0x7071,
0777                 "FCoE ctx reset not supported.\n");
0778             return -EPERM;
0779         }
0780 
0781         ql_log(ql_log_info, vha, 0x7072,
0782             "Issuing FCoE ctx reset.\n");
0783         set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
0784         qla2xxx_wake_dpc(vha);
0785         qla2x00_wait_for_fcoe_ctx_reset(vha);
0786         break;
0787     case 0x2025f:
0788         if (!IS_QLA8031(ha))
0789             return -EPERM;
0790         ql_log(ql_log_info, vha, 0x70bc,
0791             "Disabling Reset by IDC control\n");
0792         qla83xx_idc_lock(vha, 0);
0793         __qla83xx_get_idc_control(vha, &idc_control);
0794         idc_control |= QLA83XX_IDC_RESET_DISABLED;
0795         __qla83xx_set_idc_control(vha, idc_control);
0796         qla83xx_idc_unlock(vha, 0);
0797         break;
0798     case 0x20260:
0799         if (!IS_QLA8031(ha))
0800             return -EPERM;
0801         ql_log(ql_log_info, vha, 0x70bd,
0802             "Enabling Reset by IDC control\n");
0803         qla83xx_idc_lock(vha, 0);
0804         __qla83xx_get_idc_control(vha, &idc_control);
0805         idc_control &= ~QLA83XX_IDC_RESET_DISABLED;
0806         __qla83xx_set_idc_control(vha, idc_control);
0807         qla83xx_idc_unlock(vha, 0);
0808         break;
0809     case 0x20261:
0810         ql_dbg(ql_dbg_user, vha, 0x70e0,
0811             "Updating cache versions without reset ");
0812 
0813         tmp_data = vmalloc(256);
0814         if (!tmp_data) {
0815             ql_log(ql_log_warn, vha, 0x70e1,
0816                 "Unable to allocate memory for VPD information update.\n");
0817             return -ENOMEM;
0818         }
0819         ha->isp_ops->get_flash_version(vha, tmp_data);
0820         vfree(tmp_data);
0821         break;
0822     }
0823     return count;
0824 }
0825 
0826 static struct bin_attribute sysfs_reset_attr = {
0827     .attr = {
0828         .name = "reset",
0829         .mode = S_IWUSR,
0830     },
0831     .size = 0,
0832     .write = qla2x00_sysfs_write_reset,
0833 };
0834 
0835 static ssize_t
0836 qla2x00_issue_logo(struct file *filp, struct kobject *kobj,
0837             struct bin_attribute *bin_attr,
0838             char *buf, loff_t off, size_t count)
0839 {
0840     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0841         struct device, kobj)));
0842     int type;
0843     port_id_t did;
0844 
0845     if (!capable(CAP_SYS_ADMIN))
0846         return 0;
0847 
0848     if (unlikely(pci_channel_offline(vha->hw->pdev)))
0849         return 0;
0850 
0851     if (qla2x00_chip_is_down(vha))
0852         return 0;
0853 
0854     type = simple_strtol(buf, NULL, 10);
0855 
0856     did.b.domain = (type & 0x00ff0000) >> 16;
0857     did.b.area = (type & 0x0000ff00) >> 8;
0858     did.b.al_pa = (type & 0x000000ff);
0859 
0860     ql_log(ql_log_info, vha, 0xd04d, "portid=%02x%02x%02x done\n",
0861         did.b.domain, did.b.area, did.b.al_pa);
0862 
0863     ql_log(ql_log_info, vha, 0x70e4, "%s: %d\n", __func__, type);
0864 
0865     qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, did);
0866     return count;
0867 }
0868 
0869 static struct bin_attribute sysfs_issue_logo_attr = {
0870     .attr = {
0871         .name = "issue_logo",
0872         .mode = S_IWUSR,
0873     },
0874     .size = 0,
0875     .write = qla2x00_issue_logo,
0876 };
0877 
0878 static ssize_t
0879 qla2x00_sysfs_read_xgmac_stats(struct file *filp, struct kobject *kobj,
0880                struct bin_attribute *bin_attr,
0881                char *buf, loff_t off, size_t count)
0882 {
0883     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0884         struct device, kobj)));
0885     struct qla_hw_data *ha = vha->hw;
0886     int rval;
0887     uint16_t actual_size;
0888 
0889     if (!capable(CAP_SYS_ADMIN) || off != 0 || count > XGMAC_DATA_SIZE)
0890         return 0;
0891 
0892     if (unlikely(pci_channel_offline(ha->pdev)))
0893         return 0;
0894     mutex_lock(&vha->hw->optrom_mutex);
0895     if (qla2x00_chip_is_down(vha)) {
0896         mutex_unlock(&vha->hw->optrom_mutex);
0897         return 0;
0898     }
0899 
0900     if (ha->xgmac_data)
0901         goto do_read;
0902 
0903     ha->xgmac_data = dma_alloc_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
0904         &ha->xgmac_data_dma, GFP_KERNEL);
0905     if (!ha->xgmac_data) {
0906         mutex_unlock(&vha->hw->optrom_mutex);
0907         ql_log(ql_log_warn, vha, 0x7076,
0908             "Unable to allocate memory for XGMAC read-data.\n");
0909         return 0;
0910     }
0911 
0912 do_read:
0913     actual_size = 0;
0914     memset(ha->xgmac_data, 0, XGMAC_DATA_SIZE);
0915 
0916     rval = qla2x00_get_xgmac_stats(vha, ha->xgmac_data_dma,
0917         XGMAC_DATA_SIZE, &actual_size);
0918 
0919     mutex_unlock(&vha->hw->optrom_mutex);
0920     if (rval != QLA_SUCCESS) {
0921         ql_log(ql_log_warn, vha, 0x7077,
0922             "Unable to read XGMAC data (%x).\n", rval);
0923         count = 0;
0924     }
0925 
0926     count = actual_size > count ? count : actual_size;
0927     memcpy(buf, ha->xgmac_data, count);
0928 
0929     return count;
0930 }
0931 
0932 static struct bin_attribute sysfs_xgmac_stats_attr = {
0933     .attr = {
0934         .name = "xgmac_stats",
0935         .mode = S_IRUSR,
0936     },
0937     .size = 0,
0938     .read = qla2x00_sysfs_read_xgmac_stats,
0939 };
0940 
0941 static ssize_t
0942 qla2x00_sysfs_read_dcbx_tlv(struct file *filp, struct kobject *kobj,
0943                struct bin_attribute *bin_attr,
0944                char *buf, loff_t off, size_t count)
0945 {
0946     struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
0947         struct device, kobj)));
0948     struct qla_hw_data *ha = vha->hw;
0949     int rval;
0950 
0951     if (!capable(CAP_SYS_ADMIN) || off != 0 || count > DCBX_TLV_DATA_SIZE)
0952         return 0;
0953 
0954     if (ha->dcbx_tlv)
0955         goto do_read;
0956     mutex_lock(&vha->hw->optrom_mutex);
0957     if (qla2x00_chip_is_down(vha)) {
0958         mutex_unlock(&vha->hw->optrom_mutex);
0959         return 0;
0960     }
0961 
0962     ha->dcbx_tlv = dma_alloc_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
0963         &ha->dcbx_tlv_dma, GFP_KERNEL);
0964     if (!ha->dcbx_tlv) {
0965         mutex_unlock(&vha->hw->optrom_mutex);
0966         ql_log(ql_log_warn, vha, 0x7078,
0967             "Unable to allocate memory for DCBX TLV read-data.\n");
0968         return -ENOMEM;
0969     }
0970 
0971 do_read:
0972     memset(ha->dcbx_tlv, 0, DCBX_TLV_DATA_SIZE);
0973 
0974     rval = qla2x00_get_dcbx_params(vha, ha->dcbx_tlv_dma,
0975         DCBX_TLV_DATA_SIZE);
0976 
0977     mutex_unlock(&vha->hw->optrom_mutex);
0978 
0979     if (rval != QLA_SUCCESS) {
0980         ql_log(ql_log_warn, vha, 0x7079,
0981             "Unable to read DCBX TLV (%x).\n", rval);
0982         return -EIO;
0983     }
0984 
0985     memcpy(buf, ha->dcbx_tlv, count);
0986 
0987     return count;
0988 }
0989 
0990 static struct bin_attribute sysfs_dcbx_tlv_attr = {
0991     .attr = {
0992         .name = "dcbx_tlv",
0993         .mode = S_IRUSR,
0994     },
0995     .size = 0,
0996     .read = qla2x00_sysfs_read_dcbx_tlv,
0997 };
0998 
0999 static struct sysfs_entry {
1000     char *name;
1001     struct bin_attribute *attr;
1002     int type;
1003 } bin_file_entries[] = {
1004     { "fw_dump", &sysfs_fw_dump_attr, },
1005     { "nvram", &sysfs_nvram_attr, },
1006     { "optrom", &sysfs_optrom_attr, },
1007     { "optrom_ctl", &sysfs_optrom_ctl_attr, },
1008     { "vpd", &sysfs_vpd_attr, 1 },
1009     { "sfp", &sysfs_sfp_attr, 1 },
1010     { "reset", &sysfs_reset_attr, },
1011     { "issue_logo", &sysfs_issue_logo_attr, },
1012     { "xgmac_stats", &sysfs_xgmac_stats_attr, 3 },
1013     { "dcbx_tlv", &sysfs_dcbx_tlv_attr, 3 },
1014     { NULL },
1015 };
1016 
1017 void
1018 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *vha)
1019 {
1020     struct Scsi_Host *host = vha->host;
1021     struct sysfs_entry *iter;
1022     int ret;
1023 
1024     for (iter = bin_file_entries; iter->name; iter++) {
1025         if (iter->type && !IS_FWI2_CAPABLE(vha->hw))
1026             continue;
1027         if (iter->type == 2 && !IS_QLA25XX(vha->hw))
1028             continue;
1029         if (iter->type == 3 && !(IS_CNA_CAPABLE(vha->hw)))
1030             continue;
1031 
1032         ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
1033             iter->attr);
1034         if (ret)
1035             ql_log(ql_log_warn, vha, 0x00f3,
1036                 "Unable to create sysfs %s binary attribute (%d).\n",
1037                 iter->name, ret);
1038         else
1039             ql_dbg(ql_dbg_init, vha, 0x00f4,
1040                 "Successfully created sysfs %s binary attribute.\n",
1041                 iter->name);
1042     }
1043 }
1044 
1045 void
1046 qla2x00_free_sysfs_attr(scsi_qla_host_t *vha, bool stop_beacon)
1047 {
1048     struct Scsi_Host *host = vha->host;
1049     struct sysfs_entry *iter;
1050     struct qla_hw_data *ha = vha->hw;
1051 
1052     for (iter = bin_file_entries; iter->name; iter++) {
1053         if (iter->type && !IS_FWI2_CAPABLE(ha))
1054             continue;
1055         if (iter->type == 2 && !IS_QLA25XX(ha))
1056             continue;
1057         if (iter->type == 3 && !(IS_CNA_CAPABLE(ha)))
1058             continue;
1059 
1060         sysfs_remove_bin_file(&host->shost_gendev.kobj,
1061             iter->attr);
1062     }
1063 
1064     if (stop_beacon && ha->beacon_blink_led == 1)
1065         ha->isp_ops->beacon_off(vha);
1066 }
1067 
1068 /* Scsi_Host attributes. */
1069 
1070 static ssize_t
1071 qla2x00_driver_version_show(struct device *dev,
1072               struct device_attribute *attr, char *buf)
1073 {
1074     return scnprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
1075 }
1076 
1077 static ssize_t
1078 qla2x00_fw_version_show(struct device *dev,
1079             struct device_attribute *attr, char *buf)
1080 {
1081     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1082     struct qla_hw_data *ha = vha->hw;
1083     char fw_str[128];
1084 
1085     return scnprintf(buf, PAGE_SIZE, "%s\n",
1086         ha->isp_ops->fw_version_str(vha, fw_str, sizeof(fw_str)));
1087 }
1088 
1089 static ssize_t
1090 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
1091             char *buf)
1092 {
1093     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1094     struct qla_hw_data *ha = vha->hw;
1095     uint32_t sn;
1096 
1097     if (IS_QLAFX00(vha->hw)) {
1098         return scnprintf(buf, PAGE_SIZE, "%s\n",
1099             vha->hw->mr.serial_num);
1100     } else if (IS_FWI2_CAPABLE(ha)) {
1101         qla2xxx_get_vpd_field(vha, "SN", buf, PAGE_SIZE - 1);
1102         return strlen(strcat(buf, "\n"));
1103     }
1104 
1105     sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
1106     return scnprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
1107         sn % 100000);
1108 }
1109 
1110 static ssize_t
1111 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
1112               char *buf)
1113 {
1114     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1115 
1116     return scnprintf(buf, PAGE_SIZE, "ISP%04X\n", vha->hw->pdev->device);
1117 }
1118 
1119 static ssize_t
1120 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
1121             char *buf)
1122 {
1123     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1124     struct qla_hw_data *ha = vha->hw;
1125 
1126     if (IS_QLAFX00(vha->hw))
1127         return scnprintf(buf, PAGE_SIZE, "%s\n",
1128             vha->hw->mr.hw_version);
1129 
1130     return scnprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
1131         ha->product_id[0], ha->product_id[1], ha->product_id[2],
1132         ha->product_id[3]);
1133 }
1134 
1135 static ssize_t
1136 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
1137             char *buf)
1138 {
1139     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1140 
1141     return scnprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_number);
1142 }
1143 
1144 static ssize_t
1145 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
1146             char *buf)
1147 {
1148     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1149 
1150     return scnprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_desc);
1151 }
1152 
1153 static ssize_t
1154 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
1155               char *buf)
1156 {
1157     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1158     char pci_info[30];
1159 
1160     return scnprintf(buf, PAGE_SIZE, "%s\n",
1161              vha->hw->isp_ops->pci_info_str(vha, pci_info,
1162                             sizeof(pci_info)));
1163 }
1164 
1165 static ssize_t
1166 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
1167             char *buf)
1168 {
1169     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1170     struct qla_hw_data *ha = vha->hw;
1171     int len = 0;
1172 
1173     if (atomic_read(&vha->loop_state) == LOOP_DOWN ||
1174         atomic_read(&vha->loop_state) == LOOP_DEAD ||
1175         vha->device_flags & DFLG_NO_CABLE)
1176         len = scnprintf(buf, PAGE_SIZE, "Link Down\n");
1177     else if (atomic_read(&vha->loop_state) != LOOP_READY ||
1178         qla2x00_chip_is_down(vha))
1179         len = scnprintf(buf, PAGE_SIZE, "Unknown Link State\n");
1180     else {
1181         len = scnprintf(buf, PAGE_SIZE, "Link Up - ");
1182 
1183         switch (ha->current_topology) {
1184         case ISP_CFG_NL:
1185             len += scnprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1186             break;
1187         case ISP_CFG_FL:
1188             len += scnprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
1189             break;
1190         case ISP_CFG_N:
1191             len += scnprintf(buf + len, PAGE_SIZE-len,
1192                 "N_Port to N_Port\n");
1193             break;
1194         case ISP_CFG_F:
1195             len += scnprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
1196             break;
1197         default:
1198             len += scnprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1199             break;
1200         }
1201     }
1202     return len;
1203 }
1204 
1205 static ssize_t
1206 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
1207          char *buf)
1208 {
1209     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1210     int len = 0;
1211 
1212     switch (vha->hw->zio_mode) {
1213     case QLA_ZIO_MODE_6:
1214         len += scnprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
1215         break;
1216     case QLA_ZIO_DISABLED:
1217         len += scnprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1218         break;
1219     }
1220     return len;
1221 }
1222 
1223 static ssize_t
1224 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
1225           const char *buf, size_t count)
1226 {
1227     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1228     struct qla_hw_data *ha = vha->hw;
1229     int val = 0;
1230     uint16_t zio_mode;
1231 
1232     if (!IS_ZIO_SUPPORTED(ha))
1233         return -ENOTSUPP;
1234 
1235     if (sscanf(buf, "%d", &val) != 1)
1236         return -EINVAL;
1237 
1238     if (val)
1239         zio_mode = QLA_ZIO_MODE_6;
1240     else
1241         zio_mode = QLA_ZIO_DISABLED;
1242 
1243     /* Update per-hba values and queue a reset. */
1244     if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
1245         ha->zio_mode = zio_mode;
1246         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1247     }
1248     return strlen(buf);
1249 }
1250 
1251 static ssize_t
1252 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
1253                char *buf)
1254 {
1255     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1256 
1257     return scnprintf(buf, PAGE_SIZE, "%d us\n", vha->hw->zio_timer * 100);
1258 }
1259 
1260 static ssize_t
1261 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
1262             const char *buf, size_t count)
1263 {
1264     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1265     int val = 0;
1266     uint16_t zio_timer;
1267 
1268     if (sscanf(buf, "%d", &val) != 1)
1269         return -EINVAL;
1270     if (val > 25500 || val < 100)
1271         return -ERANGE;
1272 
1273     zio_timer = (uint16_t)(val / 100);
1274     vha->hw->zio_timer = zio_timer;
1275 
1276     return strlen(buf);
1277 }
1278 
1279 static ssize_t
1280 qla_zio_threshold_show(struct device *dev, struct device_attribute *attr,
1281                char *buf)
1282 {
1283     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1284 
1285     return scnprintf(buf, PAGE_SIZE, "%d exchanges\n",
1286         vha->hw->last_zio_threshold);
1287 }
1288 
1289 static ssize_t
1290 qla_zio_threshold_store(struct device *dev, struct device_attribute *attr,
1291     const char *buf, size_t count)
1292 {
1293     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1294     int val = 0;
1295 
1296     if (vha->hw->zio_mode != QLA_ZIO_MODE_6)
1297         return -EINVAL;
1298     if (sscanf(buf, "%d", &val) != 1)
1299         return -EINVAL;
1300     if (val < 0 || val > 256)
1301         return -ERANGE;
1302 
1303     atomic_set(&vha->hw->zio_threshold, val);
1304     return strlen(buf);
1305 }
1306 
1307 static ssize_t
1308 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
1309             char *buf)
1310 {
1311     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1312     int len = 0;
1313 
1314     if (vha->hw->beacon_blink_led)
1315         len += scnprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
1316     else
1317         len += scnprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1318     return len;
1319 }
1320 
1321 static ssize_t
1322 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
1323              const char *buf, size_t count)
1324 {
1325     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1326     struct qla_hw_data *ha = vha->hw;
1327     int val = 0;
1328     int rval;
1329 
1330     if (IS_QLA2100(ha) || IS_QLA2200(ha))
1331         return -EPERM;
1332 
1333     if (sscanf(buf, "%d", &val) != 1)
1334         return -EINVAL;
1335 
1336     mutex_lock(&vha->hw->optrom_mutex);
1337     if (qla2x00_chip_is_down(vha)) {
1338         mutex_unlock(&vha->hw->optrom_mutex);
1339         ql_log(ql_log_warn, vha, 0x707a,
1340             "Abort ISP active -- ignoring beacon request.\n");
1341         return -EBUSY;
1342     }
1343 
1344     if (val)
1345         rval = ha->isp_ops->beacon_on(vha);
1346     else
1347         rval = ha->isp_ops->beacon_off(vha);
1348 
1349     if (rval != QLA_SUCCESS)
1350         count = 0;
1351 
1352     mutex_unlock(&vha->hw->optrom_mutex);
1353 
1354     return count;
1355 }
1356 
1357 static ssize_t
1358 qla2x00_beacon_config_show(struct device *dev, struct device_attribute *attr,
1359     char *buf)
1360 {
1361     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1362     struct qla_hw_data *ha = vha->hw;
1363     uint16_t led[3] = { 0 };
1364 
1365     if (!IS_QLA2031(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1366         return -EPERM;
1367 
1368     if (ql26xx_led_config(vha, 0, led))
1369         return scnprintf(buf, PAGE_SIZE, "\n");
1370 
1371     return scnprintf(buf, PAGE_SIZE, "%#04hx %#04hx %#04hx\n",
1372         led[0], led[1], led[2]);
1373 }
1374 
1375 static ssize_t
1376 qla2x00_beacon_config_store(struct device *dev, struct device_attribute *attr,
1377     const char *buf, size_t count)
1378 {
1379     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1380     struct qla_hw_data *ha = vha->hw;
1381     uint16_t options = BIT_0;
1382     uint16_t led[3] = { 0 };
1383     uint16_t word[4];
1384     int n;
1385 
1386     if (!IS_QLA2031(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1387         return -EPERM;
1388 
1389     n = sscanf(buf, "%hx %hx %hx %hx", word+0, word+1, word+2, word+3);
1390     if (n == 4) {
1391         if (word[0] == 3) {
1392             options |= BIT_3|BIT_2|BIT_1;
1393             led[0] = word[1];
1394             led[1] = word[2];
1395             led[2] = word[3];
1396             goto write;
1397         }
1398         return -EINVAL;
1399     }
1400 
1401     if (n == 2) {
1402         /* check led index */
1403         if (word[0] == 0) {
1404             options |= BIT_2;
1405             led[0] = word[1];
1406             goto write;
1407         }
1408         if (word[0] == 1) {
1409             options |= BIT_3;
1410             led[1] = word[1];
1411             goto write;
1412         }
1413         if (word[0] == 2) {
1414             options |= BIT_1;
1415             led[2] = word[1];
1416             goto write;
1417         }
1418         return -EINVAL;
1419     }
1420 
1421     return -EINVAL;
1422 
1423 write:
1424     if (ql26xx_led_config(vha, options, led))
1425         return -EFAULT;
1426 
1427     return count;
1428 }
1429 
1430 static ssize_t
1431 qla2x00_optrom_bios_version_show(struct device *dev,
1432                  struct device_attribute *attr, char *buf)
1433 {
1434     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1435     struct qla_hw_data *ha = vha->hw;
1436 
1437     return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
1438         ha->bios_revision[0]);
1439 }
1440 
1441 static ssize_t
1442 qla2x00_optrom_efi_version_show(struct device *dev,
1443                 struct device_attribute *attr, char *buf)
1444 {
1445     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1446     struct qla_hw_data *ha = vha->hw;
1447 
1448     return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
1449         ha->efi_revision[0]);
1450 }
1451 
1452 static ssize_t
1453 qla2x00_optrom_fcode_version_show(struct device *dev,
1454                   struct device_attribute *attr, char *buf)
1455 {
1456     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1457     struct qla_hw_data *ha = vha->hw;
1458 
1459     return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
1460         ha->fcode_revision[0]);
1461 }
1462 
1463 static ssize_t
1464 qla2x00_optrom_fw_version_show(struct device *dev,
1465                    struct device_attribute *attr, char *buf)
1466 {
1467     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1468     struct qla_hw_data *ha = vha->hw;
1469 
1470     return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
1471         ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
1472         ha->fw_revision[3]);
1473 }
1474 
1475 static ssize_t
1476 qla2x00_optrom_gold_fw_version_show(struct device *dev,
1477     struct device_attribute *attr, char *buf)
1478 {
1479     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1480     struct qla_hw_data *ha = vha->hw;
1481 
1482     if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
1483         !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1484         return scnprintf(buf, PAGE_SIZE, "\n");
1485 
1486     return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%d)\n",
1487         ha->gold_fw_version[0], ha->gold_fw_version[1],
1488         ha->gold_fw_version[2], ha->gold_fw_version[3]);
1489 }
1490 
1491 static ssize_t
1492 qla2x00_total_isp_aborts_show(struct device *dev,
1493                   struct device_attribute *attr, char *buf)
1494 {
1495     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1496 
1497     return scnprintf(buf, PAGE_SIZE, "%d\n",
1498         vha->qla_stats.total_isp_aborts);
1499 }
1500 
1501 static ssize_t
1502 qla24xx_84xx_fw_version_show(struct device *dev,
1503     struct device_attribute *attr, char *buf)
1504 {
1505     int rval = QLA_SUCCESS;
1506     uint16_t status[2] = { 0 };
1507     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1508     struct qla_hw_data *ha = vha->hw;
1509 
1510     if (!IS_QLA84XX(ha))
1511         return scnprintf(buf, PAGE_SIZE, "\n");
1512 
1513     if (!ha->cs84xx->op_fw_version) {
1514         rval = qla84xx_verify_chip(vha, status);
1515 
1516         if (!rval && !status[0])
1517             return scnprintf(buf, PAGE_SIZE, "%u\n",
1518                 (uint32_t)ha->cs84xx->op_fw_version);
1519     }
1520 
1521     return scnprintf(buf, PAGE_SIZE, "\n");
1522 }
1523 
1524 static ssize_t
1525 qla2x00_serdes_version_show(struct device *dev, struct device_attribute *attr,
1526     char *buf)
1527 {
1528     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1529     struct qla_hw_data *ha = vha->hw;
1530 
1531     if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1532         return scnprintf(buf, PAGE_SIZE, "\n");
1533 
1534     return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1535         ha->serdes_version[0], ha->serdes_version[1],
1536         ha->serdes_version[2]);
1537 }
1538 
1539 static ssize_t
1540 qla2x00_mpi_version_show(struct device *dev, struct device_attribute *attr,
1541     char *buf)
1542 {
1543     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1544     struct qla_hw_data *ha = vha->hw;
1545 
1546     if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha) &&
1547         !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1548         return scnprintf(buf, PAGE_SIZE, "\n");
1549 
1550     return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%x)\n",
1551         ha->mpi_version[0], ha->mpi_version[1], ha->mpi_version[2],
1552         ha->mpi_capabilities);
1553 }
1554 
1555 static ssize_t
1556 qla2x00_phy_version_show(struct device *dev, struct device_attribute *attr,
1557     char *buf)
1558 {
1559     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1560     struct qla_hw_data *ha = vha->hw;
1561 
1562     if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
1563         return scnprintf(buf, PAGE_SIZE, "\n");
1564 
1565     return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1566         ha->phy_version[0], ha->phy_version[1], ha->phy_version[2]);
1567 }
1568 
1569 static ssize_t
1570 qla2x00_flash_block_size_show(struct device *dev,
1571                   struct device_attribute *attr, char *buf)
1572 {
1573     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1574     struct qla_hw_data *ha = vha->hw;
1575 
1576     return scnprintf(buf, PAGE_SIZE, "0x%x\n", ha->fdt_block_size);
1577 }
1578 
1579 static ssize_t
1580 qla2x00_vlan_id_show(struct device *dev, struct device_attribute *attr,
1581     char *buf)
1582 {
1583     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1584 
1585     if (!IS_CNA_CAPABLE(vha->hw))
1586         return scnprintf(buf, PAGE_SIZE, "\n");
1587 
1588     return scnprintf(buf, PAGE_SIZE, "%d\n", vha->fcoe_vlan_id);
1589 }
1590 
1591 static ssize_t
1592 qla2x00_vn_port_mac_address_show(struct device *dev,
1593     struct device_attribute *attr, char *buf)
1594 {
1595     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1596 
1597     if (!IS_CNA_CAPABLE(vha->hw))
1598         return scnprintf(buf, PAGE_SIZE, "\n");
1599 
1600     return scnprintf(buf, PAGE_SIZE, "%pMR\n", vha->fcoe_vn_port_mac);
1601 }
1602 
1603 static ssize_t
1604 qla2x00_fabric_param_show(struct device *dev, struct device_attribute *attr,
1605     char *buf)
1606 {
1607     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1608 
1609     return scnprintf(buf, PAGE_SIZE, "%d\n", vha->hw->switch_cap);
1610 }
1611 
1612 static ssize_t
1613 qla2x00_thermal_temp_show(struct device *dev,
1614     struct device_attribute *attr, char *buf)
1615 {
1616     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1617     uint16_t temp = 0;
1618     int rc;
1619 
1620     mutex_lock(&vha->hw->optrom_mutex);
1621     if (qla2x00_chip_is_down(vha)) {
1622         mutex_unlock(&vha->hw->optrom_mutex);
1623         ql_log(ql_log_warn, vha, 0x70dc, "ISP reset active.\n");
1624         goto done;
1625     }
1626 
1627     if (vha->hw->flags.eeh_busy) {
1628         mutex_unlock(&vha->hw->optrom_mutex);
1629         ql_log(ql_log_warn, vha, 0x70dd, "PCI EEH busy.\n");
1630         goto done;
1631     }
1632 
1633     rc = qla2x00_get_thermal_temp(vha, &temp);
1634     mutex_unlock(&vha->hw->optrom_mutex);
1635     if (rc == QLA_SUCCESS)
1636         return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
1637 
1638 done:
1639     return scnprintf(buf, PAGE_SIZE, "\n");
1640 }
1641 
1642 static ssize_t
1643 qla2x00_fw_state_show(struct device *dev, struct device_attribute *attr,
1644     char *buf)
1645 {
1646     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1647     int rval = QLA_FUNCTION_FAILED;
1648     uint16_t state[6];
1649     uint32_t pstate;
1650 
1651     if (IS_QLAFX00(vha->hw)) {
1652         pstate = qlafx00_fw_state_show(dev, attr, buf);
1653         return scnprintf(buf, PAGE_SIZE, "0x%x\n", pstate);
1654     }
1655 
1656     mutex_lock(&vha->hw->optrom_mutex);
1657     if (qla2x00_chip_is_down(vha)) {
1658         mutex_unlock(&vha->hw->optrom_mutex);
1659         ql_log(ql_log_warn, vha, 0x707c,
1660             "ISP reset active.\n");
1661         goto out;
1662     } else if (vha->hw->flags.eeh_busy) {
1663         mutex_unlock(&vha->hw->optrom_mutex);
1664         goto out;
1665     }
1666 
1667     rval = qla2x00_get_firmware_state(vha, state);
1668     mutex_unlock(&vha->hw->optrom_mutex);
1669 out:
1670     if (rval != QLA_SUCCESS) {
1671         memset(state, -1, sizeof(state));
1672         rval = qla2x00_get_firmware_state(vha, state);
1673     }
1674 
1675     return scnprintf(buf, PAGE_SIZE, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1676         state[0], state[1], state[2], state[3], state[4], state[5]);
1677 }
1678 
1679 static ssize_t
1680 qla2x00_diag_requests_show(struct device *dev,
1681     struct device_attribute *attr, char *buf)
1682 {
1683     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1684 
1685     if (!IS_BIDI_CAPABLE(vha->hw))
1686         return scnprintf(buf, PAGE_SIZE, "\n");
1687 
1688     return scnprintf(buf, PAGE_SIZE, "%llu\n", vha->bidi_stats.io_count);
1689 }
1690 
1691 static ssize_t
1692 qla2x00_diag_megabytes_show(struct device *dev,
1693     struct device_attribute *attr, char *buf)
1694 {
1695     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1696 
1697     if (!IS_BIDI_CAPABLE(vha->hw))
1698         return scnprintf(buf, PAGE_SIZE, "\n");
1699 
1700     return scnprintf(buf, PAGE_SIZE, "%llu\n",
1701         vha->bidi_stats.transfer_bytes >> 20);
1702 }
1703 
1704 static ssize_t
1705 qla2x00_fw_dump_size_show(struct device *dev, struct device_attribute *attr,
1706     char *buf)
1707 {
1708     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1709     struct qla_hw_data *ha = vha->hw;
1710     uint32_t size;
1711 
1712     if (!ha->fw_dumped)
1713         size = 0;
1714     else if (IS_P3P_TYPE(ha))
1715         size = ha->md_template_size + ha->md_dump_size;
1716     else
1717         size = ha->fw_dump_len;
1718 
1719     return scnprintf(buf, PAGE_SIZE, "%d\n", size);
1720 }
1721 
1722 static ssize_t
1723 qla2x00_allow_cna_fw_dump_show(struct device *dev,
1724     struct device_attribute *attr, char *buf)
1725 {
1726     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1727 
1728     if (!IS_P3P_TYPE(vha->hw))
1729         return scnprintf(buf, PAGE_SIZE, "\n");
1730     else
1731         return scnprintf(buf, PAGE_SIZE, "%s\n",
1732             vha->hw->allow_cna_fw_dump ? "true" : "false");
1733 }
1734 
1735 static ssize_t
1736 qla2x00_allow_cna_fw_dump_store(struct device *dev,
1737     struct device_attribute *attr, const char *buf, size_t count)
1738 {
1739     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1740     int val = 0;
1741 
1742     if (!IS_P3P_TYPE(vha->hw))
1743         return -EINVAL;
1744 
1745     if (sscanf(buf, "%d", &val) != 1)
1746         return -EINVAL;
1747 
1748     vha->hw->allow_cna_fw_dump = val != 0;
1749 
1750     return strlen(buf);
1751 }
1752 
1753 static ssize_t
1754 qla2x00_pep_version_show(struct device *dev, struct device_attribute *attr,
1755     char *buf)
1756 {
1757     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1758     struct qla_hw_data *ha = vha->hw;
1759 
1760     if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1761         return scnprintf(buf, PAGE_SIZE, "\n");
1762 
1763     return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1764         ha->pep_version[0], ha->pep_version[1], ha->pep_version[2]);
1765 }
1766 
1767 static ssize_t
1768 qla2x00_min_supported_speed_show(struct device *dev,
1769     struct device_attribute *attr, char *buf)
1770 {
1771     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1772     struct qla_hw_data *ha = vha->hw;
1773 
1774     if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1775         return scnprintf(buf, PAGE_SIZE, "\n");
1776 
1777     return scnprintf(buf, PAGE_SIZE, "%s\n",
1778         ha->min_supported_speed == 6 ? "64Gps" :
1779         ha->min_supported_speed == 5 ? "32Gps" :
1780         ha->min_supported_speed == 4 ? "16Gps" :
1781         ha->min_supported_speed == 3 ? "8Gps" :
1782         ha->min_supported_speed == 2 ? "4Gps" :
1783         ha->min_supported_speed != 0 ? "unknown" : "");
1784 }
1785 
1786 static ssize_t
1787 qla2x00_max_supported_speed_show(struct device *dev,
1788     struct device_attribute *attr, char *buf)
1789 {
1790     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1791     struct qla_hw_data *ha = vha->hw;
1792 
1793     if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1794         return scnprintf(buf, PAGE_SIZE, "\n");
1795 
1796     return scnprintf(buf, PAGE_SIZE, "%s\n",
1797         ha->max_supported_speed  == 2 ? "64Gps" :
1798         ha->max_supported_speed  == 1 ? "32Gps" :
1799         ha->max_supported_speed  == 0 ? "16Gps" : "unknown");
1800 }
1801 
1802 static ssize_t
1803 qla2x00_port_speed_store(struct device *dev, struct device_attribute *attr,
1804     const char *buf, size_t count)
1805 {
1806     struct scsi_qla_host *vha = shost_priv(dev_to_shost(dev));
1807     ulong type, speed;
1808     int oldspeed, rval;
1809     int mode = QLA_SET_DATA_RATE_LR;
1810     struct qla_hw_data *ha = vha->hw;
1811 
1812     if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha)) {
1813         ql_log(ql_log_warn, vha, 0x70d8,
1814             "Speed setting not supported \n");
1815         return -EINVAL;
1816     }
1817 
1818     rval = kstrtol(buf, 10, &type);
1819     if (rval)
1820         return rval;
1821     speed = type;
1822     if (type == 40 || type == 80 || type == 160 ||
1823         type == 320) {
1824         ql_dbg(ql_dbg_user, vha, 0x70d9,
1825             "Setting will be affected after a loss of sync\n");
1826         type = type/10;
1827         mode = QLA_SET_DATA_RATE_NOLR;
1828     }
1829 
1830     oldspeed = ha->set_data_rate;
1831 
1832     switch (type) {
1833     case 0:
1834         ha->set_data_rate = PORT_SPEED_AUTO;
1835         break;
1836     case 4:
1837         ha->set_data_rate = PORT_SPEED_4GB;
1838         break;
1839     case 8:
1840         ha->set_data_rate = PORT_SPEED_8GB;
1841         break;
1842     case 16:
1843         ha->set_data_rate = PORT_SPEED_16GB;
1844         break;
1845     case 32:
1846         ha->set_data_rate = PORT_SPEED_32GB;
1847         break;
1848     default:
1849         ql_log(ql_log_warn, vha, 0x1199,
1850             "Unrecognized speed setting:%lx. Setting Autoneg\n",
1851             speed);
1852         ha->set_data_rate = PORT_SPEED_AUTO;
1853     }
1854 
1855     if (qla2x00_chip_is_down(vha) || (oldspeed == ha->set_data_rate))
1856         return -EINVAL;
1857 
1858     ql_log(ql_log_info, vha, 0x70da,
1859         "Setting speed to %lx Gbps \n", type);
1860 
1861     rval = qla2x00_set_data_rate(vha, mode);
1862     if (rval != QLA_SUCCESS)
1863         return -EIO;
1864 
1865     return strlen(buf);
1866 }
1867 
1868 static const struct {
1869     u16 rate;
1870     char *str;
1871 } port_speed_str[] = {
1872     { PORT_SPEED_4GB, "4" },
1873     { PORT_SPEED_8GB, "8" },
1874     { PORT_SPEED_16GB, "16" },
1875     { PORT_SPEED_32GB, "32" },
1876     { PORT_SPEED_64GB, "64" },
1877     { PORT_SPEED_10GB, "10" },
1878 };
1879 
1880 static ssize_t
1881 qla2x00_port_speed_show(struct device *dev, struct device_attribute *attr,
1882     char *buf)
1883 {
1884     struct scsi_qla_host *vha = shost_priv(dev_to_shost(dev));
1885     struct qla_hw_data *ha = vha->hw;
1886     ssize_t rval;
1887     u16 i;
1888     char *speed = "Unknown";
1889 
1890     rval = qla2x00_get_data_rate(vha);
1891     if (rval != QLA_SUCCESS) {
1892         ql_log(ql_log_warn, vha, 0x70db,
1893             "Unable to get port speed rval:%zd\n", rval);
1894         return -EINVAL;
1895     }
1896 
1897     for (i = 0; i < ARRAY_SIZE(port_speed_str); i++) {
1898         if (port_speed_str[i].rate != ha->link_data_rate)
1899             continue;
1900         speed = port_speed_str[i].str;
1901         break;
1902     }
1903 
1904     return scnprintf(buf, PAGE_SIZE, "%s\n", speed);
1905 }
1906 
1907 static ssize_t
1908 qla2x00_mpi_pause_store(struct device *dev,
1909     struct device_attribute *attr, const char *buf, size_t count)
1910 {
1911     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1912     int rval = 0;
1913 
1914     if (sscanf(buf, "%d", &rval) != 1)
1915         return -EINVAL;
1916 
1917     ql_log(ql_log_warn, vha, 0x7089, "Pausing MPI...\n");
1918 
1919     rval = qla83xx_wr_reg(vha, 0x002012d4, 0x30000001);
1920 
1921     if (rval != QLA_SUCCESS) {
1922         ql_log(ql_log_warn, vha, 0x708a, "Unable to pause MPI.\n");
1923         count = 0;
1924     }
1925 
1926     return count;
1927 }
1928 
1929 static DEVICE_ATTR(mpi_pause, S_IWUSR, NULL, qla2x00_mpi_pause_store);
1930 
1931 /* ----- */
1932 
1933 static ssize_t
1934 qlini_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1935 {
1936     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1937     int len = 0;
1938 
1939     len += scnprintf(buf + len, PAGE_SIZE-len,
1940         "Supported options: enabled | disabled | dual | exclusive\n");
1941 
1942     /* --- */
1943     len += scnprintf(buf + len, PAGE_SIZE-len, "Current selection: ");
1944 
1945     switch (vha->qlini_mode) {
1946     case QLA2XXX_INI_MODE_EXCLUSIVE:
1947         len += scnprintf(buf + len, PAGE_SIZE-len,
1948             QLA2XXX_INI_MODE_STR_EXCLUSIVE);
1949         break;
1950     case QLA2XXX_INI_MODE_DISABLED:
1951         len += scnprintf(buf + len, PAGE_SIZE-len,
1952             QLA2XXX_INI_MODE_STR_DISABLED);
1953         break;
1954     case QLA2XXX_INI_MODE_ENABLED:
1955         len += scnprintf(buf + len, PAGE_SIZE-len,
1956             QLA2XXX_INI_MODE_STR_ENABLED);
1957         break;
1958     case QLA2XXX_INI_MODE_DUAL:
1959         len += scnprintf(buf + len, PAGE_SIZE-len,
1960             QLA2XXX_INI_MODE_STR_DUAL);
1961         break;
1962     }
1963     len += scnprintf(buf + len, PAGE_SIZE-len, "\n");
1964 
1965     return len;
1966 }
1967 
1968 static char *mode_to_str[] = {
1969     "exclusive",
1970     "disabled",
1971     "enabled",
1972     "dual",
1973 };
1974 
1975 #define NEED_EXCH_OFFLOAD(_exchg) ((_exchg) > FW_DEF_EXCHANGES_CNT)
1976 static void qla_set_ini_mode(scsi_qla_host_t *vha, int op)
1977 {
1978     enum {
1979         NO_ACTION,
1980         MODE_CHANGE_ACCEPT,
1981         MODE_CHANGE_NO_ACTION,
1982         TARGET_STILL_ACTIVE,
1983     };
1984     int action = NO_ACTION;
1985     int set_mode = 0;
1986     u8  eo_toggle = 0;  /* exchange offload flipped */
1987 
1988     switch (vha->qlini_mode) {
1989     case QLA2XXX_INI_MODE_DISABLED:
1990         switch (op) {
1991         case QLA2XXX_INI_MODE_DISABLED:
1992             if (qla_tgt_mode_enabled(vha)) {
1993                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
1994                     vha->hw->flags.exchoffld_enabled)
1995                     eo_toggle = 1;
1996                 if (((vha->ql2xexchoffld !=
1997                     vha->u_ql2xexchoffld) &&
1998                     NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
1999                     eo_toggle) {
2000                     /*
2001                      * The number of exchange to be offload
2002                      * was tweaked or offload option was
2003                      * flipped
2004                      */
2005                     action = MODE_CHANGE_ACCEPT;
2006                 } else {
2007                     action = MODE_CHANGE_NO_ACTION;
2008                 }
2009             } else {
2010                 action = MODE_CHANGE_NO_ACTION;
2011             }
2012             break;
2013         case QLA2XXX_INI_MODE_EXCLUSIVE:
2014             if (qla_tgt_mode_enabled(vha)) {
2015                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
2016                     vha->hw->flags.exchoffld_enabled)
2017                     eo_toggle = 1;
2018                 if (((vha->ql2xexchoffld !=
2019                     vha->u_ql2xexchoffld) &&
2020                     NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
2021                     eo_toggle) {
2022                     /*
2023                      * The number of exchange to be offload
2024                      * was tweaked or offload option was
2025                      * flipped
2026                      */
2027                     action = MODE_CHANGE_ACCEPT;
2028                 } else {
2029                     action = MODE_CHANGE_NO_ACTION;
2030                 }
2031             } else {
2032                 action = MODE_CHANGE_ACCEPT;
2033             }
2034             break;
2035         case QLA2XXX_INI_MODE_DUAL:
2036             action = MODE_CHANGE_ACCEPT;
2037             /* active_mode is target only, reset it to dual */
2038             if (qla_tgt_mode_enabled(vha)) {
2039                 set_mode = 1;
2040                 action = MODE_CHANGE_ACCEPT;
2041             } else {
2042                 action = MODE_CHANGE_NO_ACTION;
2043             }
2044             break;
2045 
2046         case QLA2XXX_INI_MODE_ENABLED:
2047             if (qla_tgt_mode_enabled(vha))
2048                 action = TARGET_STILL_ACTIVE;
2049             else {
2050                 action = MODE_CHANGE_ACCEPT;
2051                 set_mode = 1;
2052             }
2053             break;
2054         }
2055         break;
2056 
2057     case QLA2XXX_INI_MODE_EXCLUSIVE:
2058         switch (op) {
2059         case QLA2XXX_INI_MODE_EXCLUSIVE:
2060             if (qla_tgt_mode_enabled(vha)) {
2061                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
2062                     vha->hw->flags.exchoffld_enabled)
2063                     eo_toggle = 1;
2064                 if (((vha->ql2xexchoffld !=
2065                     vha->u_ql2xexchoffld) &&
2066                     NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
2067                     eo_toggle)
2068                     /*
2069                      * The number of exchange to be offload
2070                      * was tweaked or offload option was
2071                      * flipped
2072                      */
2073                     action = MODE_CHANGE_ACCEPT;
2074                 else
2075                     action = NO_ACTION;
2076             } else
2077                 action = NO_ACTION;
2078 
2079             break;
2080 
2081         case QLA2XXX_INI_MODE_DISABLED:
2082             if (qla_tgt_mode_enabled(vha)) {
2083                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
2084                     vha->hw->flags.exchoffld_enabled)
2085                     eo_toggle = 1;
2086                 if (((vha->ql2xexchoffld !=
2087                       vha->u_ql2xexchoffld) &&
2088                     NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
2089                     eo_toggle)
2090                     action = MODE_CHANGE_ACCEPT;
2091                 else
2092                     action = MODE_CHANGE_NO_ACTION;
2093             } else
2094                 action = MODE_CHANGE_NO_ACTION;
2095             break;
2096 
2097         case QLA2XXX_INI_MODE_DUAL: /* exclusive -> dual */
2098             if (qla_tgt_mode_enabled(vha)) {
2099                 action = MODE_CHANGE_ACCEPT;
2100                 set_mode = 1;
2101             } else
2102                 action = MODE_CHANGE_ACCEPT;
2103             break;
2104 
2105         case QLA2XXX_INI_MODE_ENABLED:
2106             if (qla_tgt_mode_enabled(vha))
2107                 action = TARGET_STILL_ACTIVE;
2108             else {
2109                 if (vha->hw->flags.fw_started)
2110                     action = MODE_CHANGE_NO_ACTION;
2111                 else
2112                     action = MODE_CHANGE_ACCEPT;
2113             }
2114             break;
2115         }
2116         break;
2117 
2118     case QLA2XXX_INI_MODE_ENABLED:
2119         switch (op) {
2120         case QLA2XXX_INI_MODE_ENABLED:
2121             if (NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg) !=
2122                 vha->hw->flags.exchoffld_enabled)
2123                 eo_toggle = 1;
2124             if (((vha->ql2xiniexchg != vha->u_ql2xiniexchg) &&
2125                 NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg)) ||
2126                 eo_toggle)
2127                 action = MODE_CHANGE_ACCEPT;
2128             else
2129                 action = NO_ACTION;
2130             break;
2131         case QLA2XXX_INI_MODE_DUAL:
2132         case QLA2XXX_INI_MODE_DISABLED:
2133             action = MODE_CHANGE_ACCEPT;
2134             break;
2135         default:
2136             action = MODE_CHANGE_NO_ACTION;
2137             break;
2138         }
2139         break;
2140 
2141     case QLA2XXX_INI_MODE_DUAL:
2142         switch (op) {
2143         case QLA2XXX_INI_MODE_DUAL:
2144             if (qla_tgt_mode_enabled(vha) ||
2145                 qla_dual_mode_enabled(vha)) {
2146                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld +
2147                     vha->u_ql2xiniexchg) !=
2148                     vha->hw->flags.exchoffld_enabled)
2149                     eo_toggle = 1;
2150 
2151                 if ((((vha->ql2xexchoffld +
2152                        vha->ql2xiniexchg) !=
2153                     (vha->u_ql2xiniexchg +
2154                      vha->u_ql2xexchoffld)) &&
2155                     NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg +
2156                     vha->u_ql2xexchoffld)) || eo_toggle)
2157                     action = MODE_CHANGE_ACCEPT;
2158                 else
2159                     action = NO_ACTION;
2160             } else {
2161                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld +
2162                     vha->u_ql2xiniexchg) !=
2163                     vha->hw->flags.exchoffld_enabled)
2164                     eo_toggle = 1;
2165 
2166                 if ((((vha->ql2xexchoffld + vha->ql2xiniexchg)
2167                     != (vha->u_ql2xiniexchg +
2168                     vha->u_ql2xexchoffld)) &&
2169                     NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg +
2170                     vha->u_ql2xexchoffld)) || eo_toggle)
2171                     action = MODE_CHANGE_NO_ACTION;
2172                 else
2173                     action = NO_ACTION;
2174             }
2175             break;
2176 
2177         case QLA2XXX_INI_MODE_DISABLED:
2178             if (qla_tgt_mode_enabled(vha) ||
2179                 qla_dual_mode_enabled(vha)) {
2180                 /* turning off initiator mode */
2181                 set_mode = 1;
2182                 action = MODE_CHANGE_ACCEPT;
2183             } else {
2184                 action = MODE_CHANGE_NO_ACTION;
2185             }
2186             break;
2187 
2188         case QLA2XXX_INI_MODE_EXCLUSIVE:
2189             if (qla_tgt_mode_enabled(vha) ||
2190                 qla_dual_mode_enabled(vha)) {
2191                 set_mode = 1;
2192                 action = MODE_CHANGE_ACCEPT;
2193             } else {
2194                 action = MODE_CHANGE_ACCEPT;
2195             }
2196             break;
2197 
2198         case QLA2XXX_INI_MODE_ENABLED:
2199             if (qla_tgt_mode_enabled(vha) ||
2200                 qla_dual_mode_enabled(vha)) {
2201                 action = TARGET_STILL_ACTIVE;
2202             } else {
2203                 action = MODE_CHANGE_ACCEPT;
2204             }
2205         }
2206         break;
2207     }
2208 
2209     switch (action) {
2210     case MODE_CHANGE_ACCEPT:
2211         ql_log(ql_log_warn, vha, 0xffff,
2212             "Mode change accepted. From %s to %s, Tgt exchg %d|%d. ini exchg %d|%d\n",
2213             mode_to_str[vha->qlini_mode], mode_to_str[op],
2214             vha->ql2xexchoffld, vha->u_ql2xexchoffld,
2215             vha->ql2xiniexchg, vha->u_ql2xiniexchg);
2216 
2217         vha->qlini_mode = op;
2218         vha->ql2xexchoffld = vha->u_ql2xexchoffld;
2219         vha->ql2xiniexchg = vha->u_ql2xiniexchg;
2220         if (set_mode)
2221             qlt_set_mode(vha);
2222         vha->flags.online = 1;
2223         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2224         break;
2225 
2226     case MODE_CHANGE_NO_ACTION:
2227         ql_log(ql_log_warn, vha, 0xffff,
2228             "Mode is set. No action taken. From %s to %s, Tgt exchg %d|%d. ini exchg %d|%d\n",
2229             mode_to_str[vha->qlini_mode], mode_to_str[op],
2230             vha->ql2xexchoffld, vha->u_ql2xexchoffld,
2231             vha->ql2xiniexchg, vha->u_ql2xiniexchg);
2232         vha->qlini_mode = op;
2233         vha->ql2xexchoffld = vha->u_ql2xexchoffld;
2234         vha->ql2xiniexchg = vha->u_ql2xiniexchg;
2235         break;
2236 
2237     case TARGET_STILL_ACTIVE:
2238         ql_log(ql_log_warn, vha, 0xffff,
2239             "Target Mode is active. Unable to change Mode.\n");
2240         break;
2241 
2242     case NO_ACTION:
2243     default:
2244         ql_log(ql_log_warn, vha, 0xffff,
2245             "Mode unchange. No action taken. %d|%d pct %d|%d.\n",
2246             vha->qlini_mode, op,
2247             vha->ql2xexchoffld, vha->u_ql2xexchoffld);
2248         break;
2249     }
2250 }
2251 
2252 static ssize_t
2253 qlini_mode_store(struct device *dev, struct device_attribute *attr,
2254     const char *buf, size_t count)
2255 {
2256     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2257     int ini;
2258 
2259     if (!buf)
2260         return -EINVAL;
2261 
2262     if (strncasecmp(QLA2XXX_INI_MODE_STR_EXCLUSIVE, buf,
2263         strlen(QLA2XXX_INI_MODE_STR_EXCLUSIVE)) == 0)
2264         ini = QLA2XXX_INI_MODE_EXCLUSIVE;
2265     else if (strncasecmp(QLA2XXX_INI_MODE_STR_DISABLED, buf,
2266         strlen(QLA2XXX_INI_MODE_STR_DISABLED)) == 0)
2267         ini = QLA2XXX_INI_MODE_DISABLED;
2268     else if (strncasecmp(QLA2XXX_INI_MODE_STR_ENABLED, buf,
2269           strlen(QLA2XXX_INI_MODE_STR_ENABLED)) == 0)
2270         ini = QLA2XXX_INI_MODE_ENABLED;
2271     else if (strncasecmp(QLA2XXX_INI_MODE_STR_DUAL, buf,
2272         strlen(QLA2XXX_INI_MODE_STR_DUAL)) == 0)
2273         ini = QLA2XXX_INI_MODE_DUAL;
2274     else
2275         return -EINVAL;
2276 
2277     qla_set_ini_mode(vha, ini);
2278     return strlen(buf);
2279 }
2280 
2281 static ssize_t
2282 ql2xexchoffld_show(struct device *dev, struct device_attribute *attr,
2283     char *buf)
2284 {
2285     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2286     int len = 0;
2287 
2288     len += scnprintf(buf + len, PAGE_SIZE-len,
2289         "target exchange: new %d : current: %d\n\n",
2290         vha->u_ql2xexchoffld, vha->ql2xexchoffld);
2291 
2292     len += scnprintf(buf + len, PAGE_SIZE-len,
2293         "Please (re)set operating mode via \"/sys/class/scsi_host/host%ld/qlini_mode\" to load new setting.\n",
2294         vha->host_no);
2295 
2296     return len;
2297 }
2298 
2299 static ssize_t
2300 ql2xexchoffld_store(struct device *dev, struct device_attribute *attr,
2301     const char *buf, size_t count)
2302 {
2303     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2304     int val = 0;
2305 
2306     if (sscanf(buf, "%d", &val) != 1)
2307         return -EINVAL;
2308 
2309     if (val > FW_MAX_EXCHANGES_CNT)
2310         val = FW_MAX_EXCHANGES_CNT;
2311     else if (val < 0)
2312         val = 0;
2313 
2314     vha->u_ql2xexchoffld = val;
2315     return strlen(buf);
2316 }
2317 
2318 static ssize_t
2319 ql2xiniexchg_show(struct device *dev, struct device_attribute *attr,
2320     char *buf)
2321 {
2322     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2323     int len = 0;
2324 
2325     len += scnprintf(buf + len, PAGE_SIZE-len,
2326         "target exchange: new %d : current: %d\n\n",
2327         vha->u_ql2xiniexchg, vha->ql2xiniexchg);
2328 
2329     len += scnprintf(buf + len, PAGE_SIZE-len,
2330         "Please (re)set operating mode via \"/sys/class/scsi_host/host%ld/qlini_mode\" to load new setting.\n",
2331         vha->host_no);
2332 
2333     return len;
2334 }
2335 
2336 static ssize_t
2337 ql2xiniexchg_store(struct device *dev, struct device_attribute *attr,
2338     const char *buf, size_t count)
2339 {
2340     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2341     int val = 0;
2342 
2343     if (sscanf(buf, "%d", &val) != 1)
2344         return -EINVAL;
2345 
2346     if (val > FW_MAX_EXCHANGES_CNT)
2347         val = FW_MAX_EXCHANGES_CNT;
2348     else if (val < 0)
2349         val = 0;
2350 
2351     vha->u_ql2xiniexchg = val;
2352     return strlen(buf);
2353 }
2354 
2355 static ssize_t
2356 qla2x00_dif_bundle_statistics_show(struct device *dev,
2357     struct device_attribute *attr, char *buf)
2358 {
2359     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2360     struct qla_hw_data *ha = vha->hw;
2361 
2362     return scnprintf(buf, PAGE_SIZE,
2363         "cross=%llu read=%llu write=%llu kalloc=%llu dma_alloc=%llu unusable=%u\n",
2364         ha->dif_bundle_crossed_pages, ha->dif_bundle_reads,
2365         ha->dif_bundle_writes, ha->dif_bundle_kallocs,
2366         ha->dif_bundle_dma_allocs, ha->pool.unusable.count);
2367 }
2368 
2369 static ssize_t
2370 qla2x00_fw_attr_show(struct device *dev,
2371     struct device_attribute *attr, char *buf)
2372 {
2373     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2374     struct qla_hw_data *ha = vha->hw;
2375 
2376     if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
2377         return scnprintf(buf, PAGE_SIZE, "\n");
2378 
2379     return scnprintf(buf, PAGE_SIZE, "%llx\n",
2380         (uint64_t)ha->fw_attributes_ext[1] << 48 |
2381         (uint64_t)ha->fw_attributes_ext[0] << 32 |
2382         (uint64_t)ha->fw_attributes_h << 16 |
2383         (uint64_t)ha->fw_attributes);
2384 }
2385 
2386 static ssize_t
2387 qla2x00_port_no_show(struct device *dev, struct device_attribute *attr,
2388     char *buf)
2389 {
2390     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2391 
2392     return scnprintf(buf, PAGE_SIZE, "%u\n", vha->hw->port_no);
2393 }
2394 
2395 static ssize_t
2396 qla2x00_dport_diagnostics_show(struct device *dev,
2397     struct device_attribute *attr, char *buf)
2398 {
2399     scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2400 
2401     if (!IS_QLA83XX(vha->hw) && !IS_QLA27XX(vha->hw) &&
2402         !IS_QLA28XX(vha->hw))
2403         return scnprintf(buf, PAGE_SIZE, "\n");
2404 
2405     if (!*vha->dport_data)
2406         return scnprintf(buf, PAGE_SIZE, "\n");
2407 
2408     return scnprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
2409         vha->dport_data[0], vha->dport_data[1],
2410         vha->dport_data[2], vha->dport_data[3]);
2411 }
2412 static DEVICE_ATTR(dport_diagnostics, 0444,
2413        qla2x00_dport_diagnostics_show, NULL);
2414 
2415 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_driver_version_show, NULL);
2416 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
2417 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
2418 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
2419 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
2420 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
2421 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
2422 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
2423 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
2424 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
2425 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
2426            qla2x00_zio_timer_store);
2427 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
2428            qla2x00_beacon_store);
2429 static DEVICE_ATTR(beacon_config, 0644, qla2x00_beacon_config_show,
2430            qla2x00_beacon_config_store);
2431 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
2432            qla2x00_optrom_bios_version_show, NULL);
2433 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
2434            qla2x00_optrom_efi_version_show, NULL);
2435 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
2436            qla2x00_optrom_fcode_version_show, NULL);
2437 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
2438            NULL);
2439 static DEVICE_ATTR(optrom_gold_fw_version, S_IRUGO,
2440     qla2x00_optrom_gold_fw_version_show, NULL);
2441 static DEVICE_ATTR(84xx_fw_version, S_IRUGO, qla24xx_84xx_fw_version_show,
2442            NULL);
2443 static DEVICE_ATTR(total_isp_aborts, S_IRUGO, qla2x00_total_isp_aborts_show,
2444            NULL);
2445 static DEVICE_ATTR(serdes_version, 0444, qla2x00_serdes_version_show, NULL);
2446 static DEVICE_ATTR(mpi_version, S_IRUGO, qla2x00_mpi_version_show, NULL);
2447 static DEVICE_ATTR(phy_version, S_IRUGO, qla2x00_phy_version_show, NULL);
2448 static DEVICE_ATTR(flash_block_size, S_IRUGO, qla2x00_flash_block_size_show,
2449            NULL);
2450 static DEVICE_ATTR(vlan_id, S_IRUGO, qla2x00_vlan_id_show, NULL);
2451 static DEVICE_ATTR(vn_port_mac_address, S_IRUGO,
2452            qla2x00_vn_port_mac_address_show, NULL);
2453 static DEVICE_ATTR(fabric_param, S_IRUGO, qla2x00_fabric_param_show, NULL);
2454 static DEVICE_ATTR(fw_state, S_IRUGO, qla2x00_fw_state_show, NULL);
2455 static DEVICE_ATTR(thermal_temp, S_IRUGO, qla2x00_thermal_temp_show, NULL);
2456 static DEVICE_ATTR(diag_requests, S_IRUGO, qla2x00_diag_requests_show, NULL);
2457 static DEVICE_ATTR(diag_megabytes, S_IRUGO, qla2x00_diag_megabytes_show, NULL);
2458 static DEVICE_ATTR(fw_dump_size, S_IRUGO, qla2x00_fw_dump_size_show, NULL);
2459 static DEVICE_ATTR(allow_cna_fw_dump, S_IRUGO | S_IWUSR,
2460            qla2x00_allow_cna_fw_dump_show,
2461            qla2x00_allow_cna_fw_dump_store);
2462 static DEVICE_ATTR(pep_version, S_IRUGO, qla2x00_pep_version_show, NULL);
2463 static DEVICE_ATTR(min_supported_speed, 0444,
2464            qla2x00_min_supported_speed_show, NULL);
2465 static DEVICE_ATTR(max_supported_speed, 0444,
2466            qla2x00_max_supported_speed_show, NULL);
2467 static DEVICE_ATTR(zio_threshold, 0644,
2468     qla_zio_threshold_show,
2469     qla_zio_threshold_store);
2470 static DEVICE_ATTR_RW(qlini_mode);
2471 static DEVICE_ATTR_RW(ql2xexchoffld);
2472 static DEVICE_ATTR_RW(ql2xiniexchg);
2473 static DEVICE_ATTR(dif_bundle_statistics, 0444,
2474     qla2x00_dif_bundle_statistics_show, NULL);
2475 static DEVICE_ATTR(port_speed, 0644, qla2x00_port_speed_show,
2476     qla2x00_port_speed_store);
2477 static DEVICE_ATTR(port_no, 0444, qla2x00_port_no_show, NULL);
2478 static DEVICE_ATTR(fw_attr, 0444, qla2x00_fw_attr_show, NULL);
2479 
2480 static struct attribute *qla2x00_host_attrs[] = {
2481     &dev_attr_driver_version.attr,
2482     &dev_attr_fw_version.attr,
2483     &dev_attr_serial_num.attr,
2484     &dev_attr_isp_name.attr,
2485     &dev_attr_isp_id.attr,
2486     &dev_attr_model_name.attr,
2487     &dev_attr_model_desc.attr,
2488     &dev_attr_pci_info.attr,
2489     &dev_attr_link_state.attr,
2490     &dev_attr_zio.attr,
2491     &dev_attr_zio_timer.attr,
2492     &dev_attr_beacon.attr,
2493     &dev_attr_beacon_config.attr,
2494     &dev_attr_optrom_bios_version.attr,
2495     &dev_attr_optrom_efi_version.attr,
2496     &dev_attr_optrom_fcode_version.attr,
2497     &dev_attr_optrom_fw_version.attr,
2498     &dev_attr_84xx_fw_version.attr,
2499     &dev_attr_total_isp_aborts.attr,
2500     &dev_attr_serdes_version.attr,
2501     &dev_attr_mpi_version.attr,
2502     &dev_attr_phy_version.attr,
2503     &dev_attr_flash_block_size.attr,
2504     &dev_attr_vlan_id.attr,
2505     &dev_attr_vn_port_mac_address.attr,
2506     &dev_attr_fabric_param.attr,
2507     &dev_attr_fw_state.attr,
2508     &dev_attr_optrom_gold_fw_version.attr,
2509     &dev_attr_thermal_temp.attr,
2510     &dev_attr_diag_requests.attr,
2511     &dev_attr_diag_megabytes.attr,
2512     &dev_attr_fw_dump_size.attr,
2513     &dev_attr_allow_cna_fw_dump.attr,
2514     &dev_attr_pep_version.attr,
2515     &dev_attr_min_supported_speed.attr,
2516     &dev_attr_max_supported_speed.attr,
2517     &dev_attr_zio_threshold.attr,
2518     &dev_attr_dif_bundle_statistics.attr,
2519     &dev_attr_port_speed.attr,
2520     &dev_attr_port_no.attr,
2521     &dev_attr_fw_attr.attr,
2522     &dev_attr_dport_diagnostics.attr,
2523     &dev_attr_mpi_pause.attr,
2524     &dev_attr_qlini_mode.attr,
2525     &dev_attr_ql2xiniexchg.attr,
2526     &dev_attr_ql2xexchoffld.attr,
2527     NULL,
2528 };
2529 
2530 static umode_t qla_host_attr_is_visible(struct kobject *kobj,
2531                     struct attribute *attr, int i)
2532 {
2533     if (ql2x_ini_mode != QLA2XXX_INI_MODE_DUAL &&
2534         (attr == &dev_attr_qlini_mode.attr ||
2535          attr == &dev_attr_ql2xiniexchg.attr ||
2536          attr == &dev_attr_ql2xexchoffld.attr))
2537         return 0;
2538     return attr->mode;
2539 }
2540 
2541 static const struct attribute_group qla2x00_host_attr_group = {
2542     .is_visible = qla_host_attr_is_visible,
2543     .attrs = qla2x00_host_attrs
2544 };
2545 
2546 const struct attribute_group *qla2x00_host_groups[] = {
2547     &qla2x00_host_attr_group,
2548     NULL
2549 };
2550 
2551 /* Host attributes. */
2552 
2553 static void
2554 qla2x00_get_host_port_id(struct Scsi_Host *shost)
2555 {
2556     scsi_qla_host_t *vha = shost_priv(shost);
2557 
2558     fc_host_port_id(shost) = vha->d_id.b.domain << 16 |
2559         vha->d_id.b.area << 8 | vha->d_id.b.al_pa;
2560 }
2561 
2562 static void
2563 qla2x00_get_host_speed(struct Scsi_Host *shost)
2564 {
2565     scsi_qla_host_t *vha = shost_priv(shost);
2566     u32 speed;
2567 
2568     if (IS_QLAFX00(vha->hw)) {
2569         qlafx00_get_host_speed(shost);
2570         return;
2571     }
2572 
2573     switch (vha->hw->link_data_rate) {
2574     case PORT_SPEED_1GB:
2575         speed = FC_PORTSPEED_1GBIT;
2576         break;
2577     case PORT_SPEED_2GB:
2578         speed = FC_PORTSPEED_2GBIT;
2579         break;
2580     case PORT_SPEED_4GB:
2581         speed = FC_PORTSPEED_4GBIT;
2582         break;
2583     case PORT_SPEED_8GB:
2584         speed = FC_PORTSPEED_8GBIT;
2585         break;
2586     case PORT_SPEED_10GB:
2587         speed = FC_PORTSPEED_10GBIT;
2588         break;
2589     case PORT_SPEED_16GB:
2590         speed = FC_PORTSPEED_16GBIT;
2591         break;
2592     case PORT_SPEED_32GB:
2593         speed = FC_PORTSPEED_32GBIT;
2594         break;
2595     case PORT_SPEED_64GB:
2596         speed = FC_PORTSPEED_64GBIT;
2597         break;
2598     default:
2599         speed = FC_PORTSPEED_UNKNOWN;
2600         break;
2601     }
2602 
2603     fc_host_speed(shost) = speed;
2604 }
2605 
2606 static void
2607 qla2x00_get_host_port_type(struct Scsi_Host *shost)
2608 {
2609     scsi_qla_host_t *vha = shost_priv(shost);
2610     uint32_t port_type;
2611 
2612     if (vha->vp_idx) {
2613         fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
2614         return;
2615     }
2616     switch (vha->hw->current_topology) {
2617     case ISP_CFG_NL:
2618         port_type = FC_PORTTYPE_LPORT;
2619         break;
2620     case ISP_CFG_FL:
2621         port_type = FC_PORTTYPE_NLPORT;
2622         break;
2623     case ISP_CFG_N:
2624         port_type = FC_PORTTYPE_PTP;
2625         break;
2626     case ISP_CFG_F:
2627         port_type = FC_PORTTYPE_NPORT;
2628         break;
2629     default:
2630         port_type = FC_PORTTYPE_UNKNOWN;
2631         break;
2632     }
2633 
2634     fc_host_port_type(shost) = port_type;
2635 }
2636 
2637 static void
2638 qla2x00_get_starget_node_name(struct scsi_target *starget)
2639 {
2640     struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2641     scsi_qla_host_t *vha = shost_priv(host);
2642     fc_port_t *fcport;
2643     u64 node_name = 0;
2644 
2645     list_for_each_entry(fcport, &vha->vp_fcports, list) {
2646         if (fcport->rport &&
2647             starget->id == fcport->rport->scsi_target_id) {
2648             node_name = wwn_to_u64(fcport->node_name);
2649             break;
2650         }
2651     }
2652 
2653     fc_starget_node_name(starget) = node_name;
2654 }
2655 
2656 static void
2657 qla2x00_get_starget_port_name(struct scsi_target *starget)
2658 {
2659     struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2660     scsi_qla_host_t *vha = shost_priv(host);
2661     fc_port_t *fcport;
2662     u64 port_name = 0;
2663 
2664     list_for_each_entry(fcport, &vha->vp_fcports, list) {
2665         if (fcport->rport &&
2666             starget->id == fcport->rport->scsi_target_id) {
2667             port_name = wwn_to_u64(fcport->port_name);
2668             break;
2669         }
2670     }
2671 
2672     fc_starget_port_name(starget) = port_name;
2673 }
2674 
2675 static void
2676 qla2x00_get_starget_port_id(struct scsi_target *starget)
2677 {
2678     struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2679     scsi_qla_host_t *vha = shost_priv(host);
2680     fc_port_t *fcport;
2681     uint32_t port_id = ~0U;
2682 
2683     list_for_each_entry(fcport, &vha->vp_fcports, list) {
2684         if (fcport->rport &&
2685             starget->id == fcport->rport->scsi_target_id) {
2686             port_id = fcport->d_id.b.domain << 16 |
2687                 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2688             break;
2689         }
2690     }
2691 
2692     fc_starget_port_id(starget) = port_id;
2693 }
2694 
2695 static inline void
2696 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
2697 {
2698     fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
2699 
2700     rport->dev_loss_tmo = timeout ? timeout : 1;
2701 
2702     if (IS_ENABLED(CONFIG_NVME_FC) && fcport && fcport->nvme_remote_port)
2703         nvme_fc_set_remoteport_devloss(fcport->nvme_remote_port,
2704                            rport->dev_loss_tmo);
2705 }
2706 
2707 static void
2708 qla2x00_dev_loss_tmo_callbk(struct fc_rport *rport)
2709 {
2710     struct Scsi_Host *host = rport_to_shost(rport);
2711     fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
2712     unsigned long flags;
2713 
2714     if (!fcport)
2715         return;
2716 
2717     ql_dbg(ql_dbg_async, fcport->vha, 0x5101,
2718            DBG_FCPORT_PRFMT(fcport, "dev_loss_tmo expiry, rport_state=%d",
2719                 rport->port_state));
2720 
2721     /*
2722      * Now that the rport has been deleted, set the fcport state to
2723      * FCS_DEVICE_DEAD, if the fcport is still lost.
2724      */
2725     if (fcport->scan_state != QLA_FCPORT_FOUND)
2726         qla2x00_set_fcport_state(fcport, FCS_DEVICE_DEAD);
2727 
2728     /*
2729      * Transport has effectively 'deleted' the rport, clear
2730      * all local references.
2731      */
2732     spin_lock_irqsave(host->host_lock, flags);
2733     /* Confirm port has not reappeared before clearing pointers. */
2734     if (rport->port_state != FC_PORTSTATE_ONLINE) {
2735         fcport->rport = fcport->drport = NULL;
2736         *((fc_port_t **)rport->dd_data) = NULL;
2737     }
2738     spin_unlock_irqrestore(host->host_lock, flags);
2739 
2740     if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
2741         return;
2742 
2743     if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
2744         qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
2745         return;
2746     }
2747 }
2748 
2749 static void
2750 qla2x00_terminate_rport_io(struct fc_rport *rport)
2751 {
2752     fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
2753 
2754     if (!fcport)
2755         return;
2756 
2757     if (test_bit(UNLOADING, &fcport->vha->dpc_flags))
2758         return;
2759 
2760     if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
2761         return;
2762 
2763     if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
2764         qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
2765         return;
2766     }
2767     /*
2768      * At this point all fcport's software-states are cleared.  Perform any
2769      * final cleanup of firmware resources (PCBs and XCBs).
2770      *
2771      * Attempt to cleanup only lost devices.
2772      */
2773     if (fcport->loop_id != FC_NO_LOOP_ID) {
2774         if (IS_FWI2_CAPABLE(fcport->vha->hw) &&
2775             fcport->scan_state != QLA_FCPORT_FOUND) {
2776             if (fcport->loop_id != FC_NO_LOOP_ID)
2777                 fcport->logout_on_delete = 1;
2778 
2779             if (!EDIF_NEGOTIATION_PENDING(fcport)) {
2780                 ql_dbg(ql_dbg_disc, fcport->vha, 0x911e,
2781                        "%s %d schedule session deletion\n", __func__,
2782                        __LINE__);
2783                 qlt_schedule_sess_for_deletion(fcport);
2784             }
2785         } else if (!IS_FWI2_CAPABLE(fcport->vha->hw)) {
2786             qla2x00_port_logout(fcport->vha, fcport);
2787         }
2788     }
2789 }
2790 
2791 static int
2792 qla2x00_issue_lip(struct Scsi_Host *shost)
2793 {
2794     scsi_qla_host_t *vha = shost_priv(shost);
2795 
2796     if (IS_QLAFX00(vha->hw))
2797         return 0;
2798 
2799     if (vha->hw->flags.port_isolated)
2800         return 0;
2801 
2802     qla2x00_loop_reset(vha);
2803     return 0;
2804 }
2805 
2806 static struct fc_host_statistics *
2807 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
2808 {
2809     scsi_qla_host_t *vha = shost_priv(shost);
2810     struct qla_hw_data *ha = vha->hw;
2811     struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2812     int rval;
2813     struct link_statistics *stats;
2814     dma_addr_t stats_dma;
2815     struct fc_host_statistics *p = &vha->fc_host_stat;
2816     struct qla_qpair *qpair;
2817     int i;
2818     u64 ib = 0, ob = 0, ir = 0, or = 0;
2819 
2820     memset(p, -1, sizeof(*p));
2821 
2822     if (IS_QLAFX00(vha->hw))
2823         goto done;
2824 
2825     if (test_bit(UNLOADING, &vha->dpc_flags))
2826         goto done;
2827 
2828     if (unlikely(pci_channel_offline(ha->pdev)))
2829         goto done;
2830 
2831     if (qla2x00_chip_is_down(vha))
2832         goto done;
2833 
2834     stats = dma_alloc_coherent(&ha->pdev->dev, sizeof(*stats), &stats_dma,
2835                    GFP_KERNEL);
2836     if (!stats) {
2837         ql_log(ql_log_warn, vha, 0x707d,
2838             "Failed to allocate memory for stats.\n");
2839         goto done;
2840     }
2841 
2842     rval = QLA_FUNCTION_FAILED;
2843     if (IS_FWI2_CAPABLE(ha)) {
2844         rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, 0);
2845     } else if (atomic_read(&base_vha->loop_state) == LOOP_READY &&
2846         !ha->dpc_active) {
2847         /* Must be in a 'READY' state for statistics retrieval. */
2848         rval = qla2x00_get_link_status(base_vha, base_vha->loop_id,
2849                         stats, stats_dma);
2850     }
2851 
2852     if (rval != QLA_SUCCESS)
2853         goto done_free;
2854 
2855     /* --- */
2856     for (i = 0; i < vha->hw->max_qpairs; i++) {
2857         qpair = vha->hw->queue_pair_map[i];
2858         if (!qpair)
2859             continue;
2860         ir += qpair->counters.input_requests;
2861         or += qpair->counters.output_requests;
2862         ib += qpair->counters.input_bytes;
2863         ob += qpair->counters.output_bytes;
2864     }
2865     ir += ha->base_qpair->counters.input_requests;
2866     or += ha->base_qpair->counters.output_requests;
2867     ib += ha->base_qpair->counters.input_bytes;
2868     ob += ha->base_qpair->counters.output_bytes;
2869 
2870     ir += vha->qla_stats.input_requests;
2871     or += vha->qla_stats.output_requests;
2872     ib += vha->qla_stats.input_bytes;
2873     ob += vha->qla_stats.output_bytes;
2874     /* --- */
2875 
2876     p->link_failure_count = le32_to_cpu(stats->link_fail_cnt);
2877     p->loss_of_sync_count = le32_to_cpu(stats->loss_sync_cnt);
2878     p->loss_of_signal_count = le32_to_cpu(stats->loss_sig_cnt);
2879     p->prim_seq_protocol_err_count = le32_to_cpu(stats->prim_seq_err_cnt);
2880     p->invalid_tx_word_count = le32_to_cpu(stats->inval_xmit_word_cnt);
2881     p->invalid_crc_count = le32_to_cpu(stats->inval_crc_cnt);
2882     if (IS_FWI2_CAPABLE(ha)) {
2883         p->lip_count = le32_to_cpu(stats->lip_cnt);
2884         p->tx_frames = le32_to_cpu(stats->tx_frames);
2885         p->rx_frames = le32_to_cpu(stats->rx_frames);
2886         p->dumped_frames = le32_to_cpu(stats->discarded_frames);
2887         p->nos_count = le32_to_cpu(stats->nos_rcvd);
2888         p->error_frames =
2889             le32_to_cpu(stats->dropped_frames) +
2890             le32_to_cpu(stats->discarded_frames);
2891         if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
2892             p->rx_words = le64_to_cpu(stats->fpm_recv_word_cnt);
2893             p->tx_words = le64_to_cpu(stats->fpm_xmit_word_cnt);
2894         } else {
2895             p->rx_words = ib >> 2;
2896             p->tx_words = ob >> 2;
2897         }
2898     }
2899 
2900     p->fcp_control_requests = vha->qla_stats.control_requests;
2901     p->fcp_input_requests = ir;
2902     p->fcp_output_requests = or;
2903     p->fcp_input_megabytes  = ib >> 20;
2904     p->fcp_output_megabytes = ob >> 20;
2905     p->seconds_since_last_reset =
2906         get_jiffies_64() - vha->qla_stats.jiffies_at_last_reset;
2907     do_div(p->seconds_since_last_reset, HZ);
2908 
2909 done_free:
2910     dma_free_coherent(&ha->pdev->dev, sizeof(struct link_statistics),
2911         stats, stats_dma);
2912 done:
2913     return p;
2914 }
2915 
2916 static void
2917 qla2x00_reset_host_stats(struct Scsi_Host *shost)
2918 {
2919     scsi_qla_host_t *vha = shost_priv(shost);
2920     struct qla_hw_data *ha = vha->hw;
2921     struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2922     struct link_statistics *stats;
2923     dma_addr_t stats_dma;
2924     int i;
2925     struct qla_qpair *qpair;
2926 
2927     memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
2928     memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
2929     for (i = 0; i < vha->hw->max_qpairs; i++) {
2930         qpair = vha->hw->queue_pair_map[i];
2931         if (!qpair)
2932             continue;
2933         memset(&qpair->counters, 0, sizeof(qpair->counters));
2934     }
2935     memset(&ha->base_qpair->counters, 0, sizeof(qpair->counters));
2936 
2937     vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
2938 
2939     if (IS_FWI2_CAPABLE(ha)) {
2940         int rval;
2941 
2942         stats = dma_alloc_coherent(&ha->pdev->dev,
2943             sizeof(*stats), &stats_dma, GFP_KERNEL);
2944         if (!stats) {
2945             ql_log(ql_log_warn, vha, 0x70d7,
2946                 "Failed to allocate memory for stats.\n");
2947             return;
2948         }
2949 
2950         /* reset firmware statistics */
2951         rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, BIT_0);
2952         if (rval != QLA_SUCCESS)
2953             ql_log(ql_log_warn, vha, 0x70de,
2954                    "Resetting ISP statistics failed: rval = %d\n",
2955                    rval);
2956 
2957         dma_free_coherent(&ha->pdev->dev, sizeof(*stats),
2958             stats, stats_dma);
2959     }
2960 }
2961 
2962 static void
2963 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
2964 {
2965     scsi_qla_host_t *vha = shost_priv(shost);
2966 
2967     qla2x00_get_sym_node_name(vha, fc_host_symbolic_name(shost),
2968         sizeof(fc_host_symbolic_name(shost)));
2969 }
2970 
2971 static void
2972 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
2973 {
2974     scsi_qla_host_t *vha = shost_priv(shost);
2975 
2976     set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
2977 }
2978 
2979 static void
2980 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
2981 {
2982     scsi_qla_host_t *vha = shost_priv(shost);
2983     static const uint8_t node_name[WWN_SIZE] = {
2984         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
2985     };
2986     u64 fabric_name = wwn_to_u64(node_name);
2987 
2988     if (vha->device_flags & SWITCH_FOUND)
2989         fabric_name = wwn_to_u64(vha->fabric_node_name);
2990 
2991     fc_host_fabric_name(shost) = fabric_name;
2992 }
2993 
2994 static void
2995 qla2x00_get_host_port_state(struct Scsi_Host *shost)
2996 {
2997     scsi_qla_host_t *vha = shost_priv(shost);
2998     struct scsi_qla_host *base_vha = pci_get_drvdata(vha->hw->pdev);
2999 
3000     if (!base_vha->flags.online) {
3001         fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
3002         return;
3003     }
3004 
3005     switch (atomic_read(&base_vha->loop_state)) {
3006     case LOOP_UPDATE:
3007         fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
3008         break;
3009     case LOOP_DOWN:
3010         if (test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags))
3011             fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
3012         else
3013             fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
3014         break;
3015     case LOOP_DEAD:
3016         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
3017         break;
3018     case LOOP_READY:
3019         fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
3020         break;
3021     default:
3022         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
3023         break;
3024     }
3025 }
3026 
3027 static int
3028 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
3029 {
3030     int ret = 0;
3031     uint8_t qos = 0;
3032     scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
3033     scsi_qla_host_t *vha = NULL;
3034     struct qla_hw_data *ha = base_vha->hw;
3035     int cnt;
3036     struct req_que *req = ha->req_q_map[0];
3037     struct qla_qpair *qpair;
3038 
3039     ret = qla24xx_vport_create_req_sanity_check(fc_vport);
3040     if (ret) {
3041         ql_log(ql_log_warn, vha, 0x707e,
3042             "Vport sanity check failed, status %x\n", ret);
3043         return (ret);
3044     }
3045 
3046     vha = qla24xx_create_vhost(fc_vport);
3047     if (vha == NULL) {
3048         ql_log(ql_log_warn, vha, 0x707f, "Vport create host failed.\n");
3049         return FC_VPORT_FAILED;
3050     }
3051     if (disable) {
3052         atomic_set(&vha->vp_state, VP_OFFLINE);
3053         fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
3054     } else
3055         atomic_set(&vha->vp_state, VP_FAILED);
3056 
3057     /* ready to create vport */
3058     ql_log(ql_log_info, vha, 0x7080,
3059         "VP entry id %d assigned.\n", vha->vp_idx);
3060 
3061     /* initialized vport states */
3062     atomic_set(&vha->loop_state, LOOP_DOWN);
3063     vha->vp_err_state = VP_ERR_PORTDWN;
3064     vha->vp_prev_err_state = VP_ERR_UNKWN;
3065     /* Check if physical ha port is Up */
3066     if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
3067         atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
3068         /* Don't retry or attempt login of this virtual port */
3069         ql_dbg(ql_dbg_user, vha, 0x7081,
3070             "Vport loop state is not UP.\n");
3071         atomic_set(&vha->loop_state, LOOP_DEAD);
3072         if (!disable)
3073             fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
3074     }
3075 
3076     if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
3077         if (ha->fw_attributes & BIT_4) {
3078             int prot = 0, guard;
3079 
3080             vha->flags.difdix_supported = 1;
3081             ql_dbg(ql_dbg_user, vha, 0x7082,
3082                 "Registered for DIF/DIX type 1 and 3 protection.\n");
3083             if (ql2xenabledif == 1)
3084                 prot = SHOST_DIX_TYPE0_PROTECTION;
3085             scsi_host_set_prot(vha->host,
3086                 prot | SHOST_DIF_TYPE1_PROTECTION
3087                 | SHOST_DIF_TYPE2_PROTECTION
3088                 | SHOST_DIF_TYPE3_PROTECTION
3089                 | SHOST_DIX_TYPE1_PROTECTION
3090                 | SHOST_DIX_TYPE2_PROTECTION
3091                 | SHOST_DIX_TYPE3_PROTECTION);
3092 
3093             guard = SHOST_DIX_GUARD_CRC;
3094 
3095             if (IS_PI_IPGUARD_CAPABLE(ha) &&
3096                 (ql2xenabledif > 1 || IS_PI_DIFB_DIX0_CAPABLE(ha)))
3097                 guard |= SHOST_DIX_GUARD_IP;
3098 
3099             scsi_host_set_guard(vha->host, guard);
3100         } else
3101             vha->flags.difdix_supported = 0;
3102     }
3103 
3104     if (scsi_add_host_with_dma(vha->host, &fc_vport->dev,
3105                    &ha->pdev->dev)) {
3106         ql_dbg(ql_dbg_user, vha, 0x7083,
3107             "scsi_add_host failure for VP[%d].\n", vha->vp_idx);
3108         goto vport_create_failed_2;
3109     }
3110 
3111     /* initialize attributes */
3112     fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
3113     fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
3114     fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
3115     fc_host_supported_classes(vha->host) =
3116         fc_host_supported_classes(base_vha->host);
3117     fc_host_supported_speeds(vha->host) =
3118         fc_host_supported_speeds(base_vha->host);
3119 
3120     qlt_vport_create(vha, ha);
3121     qla24xx_vport_disable(fc_vport, disable);
3122 
3123     if (!ql2xmqsupport || !ha->npiv_info)
3124         goto vport_queue;
3125 
3126     /* Create a request queue in QoS mode for the vport */
3127     for (cnt = 0; cnt < ha->nvram_npiv_size; cnt++) {
3128         if (memcmp(ha->npiv_info[cnt].port_name, vha->port_name, 8) == 0
3129             && memcmp(ha->npiv_info[cnt].node_name, vha->node_name,
3130                     8) == 0) {
3131             qos = ha->npiv_info[cnt].q_qos;
3132             break;
3133         }
3134     }
3135 
3136     if (qos) {
3137         qpair = qla2xxx_create_qpair(vha, qos, vha->vp_idx, true);
3138         if (!qpair)
3139             ql_log(ql_log_warn, vha, 0x7084,
3140                 "Can't create qpair for VP[%d]\n",
3141                 vha->vp_idx);
3142         else {
3143             ql_dbg(ql_dbg_multiq, vha, 0xc001,
3144                 "Queue pair: %d Qos: %d) created for VP[%d]\n",
3145                 qpair->id, qos, vha->vp_idx);
3146             ql_dbg(ql_dbg_user, vha, 0x7085,
3147                 "Queue Pair: %d Qos: %d) created for VP[%d]\n",
3148                 qpair->id, qos, vha->vp_idx);
3149             req = qpair->req;
3150             vha->qpair = qpair;
3151         }
3152     }
3153 
3154 vport_queue:
3155     vha->req = req;
3156     return 0;
3157 
3158 vport_create_failed_2:
3159     qla24xx_disable_vp(vha);
3160     qla24xx_deallocate_vp_id(vha);
3161     scsi_host_put(vha->host);
3162     return FC_VPORT_FAILED;
3163 }
3164 
3165 static int
3166 qla24xx_vport_delete(struct fc_vport *fc_vport)
3167 {
3168     scsi_qla_host_t *vha = fc_vport->dd_data;
3169     struct qla_hw_data *ha = vha->hw;
3170     uint16_t id = vha->vp_idx;
3171 
3172     set_bit(VPORT_DELETE, &vha->dpc_flags);
3173 
3174     while (test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags) ||
3175         test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags))
3176         msleep(1000);
3177 
3178 
3179     qla24xx_disable_vp(vha);
3180     qla2x00_wait_for_sess_deletion(vha);
3181 
3182     qla_nvme_delete(vha);
3183     qla_enode_stop(vha);
3184     qla_edb_stop(vha);
3185 
3186     vha->flags.delete_progress = 1;
3187 
3188     qlt_remove_target(ha, vha);
3189 
3190     fc_remove_host(vha->host);
3191 
3192     scsi_remove_host(vha->host);
3193 
3194     /* Allow timer to run to drain queued items, when removing vp */
3195     qla24xx_deallocate_vp_id(vha);
3196 
3197     if (vha->timer_active) {
3198         qla2x00_vp_stop_timer(vha);
3199         ql_dbg(ql_dbg_user, vha, 0x7086,
3200             "Timer for the VP[%d] has stopped\n", vha->vp_idx);
3201     }
3202 
3203     qla2x00_free_fcports(vha);
3204 
3205     mutex_lock(&ha->vport_lock);
3206     ha->cur_vport_count--;
3207     clear_bit(vha->vp_idx, ha->vp_idx_map);
3208     mutex_unlock(&ha->vport_lock);
3209 
3210     dma_free_coherent(&ha->pdev->dev, vha->gnl.size, vha->gnl.l,
3211         vha->gnl.ldma);
3212 
3213     vha->gnl.l = NULL;
3214 
3215     vfree(vha->scan.l);
3216 
3217     if (vha->qpair && vha->qpair->vp_idx == vha->vp_idx) {
3218         if (qla2xxx_delete_qpair(vha, vha->qpair) != QLA_SUCCESS)
3219             ql_log(ql_log_warn, vha, 0x7087,
3220                 "Queue Pair delete failed.\n");
3221     }
3222 
3223     ql_log(ql_log_info, vha, 0x7088, "VP[%d] deleted.\n", id);
3224     scsi_host_put(vha->host);
3225     return 0;
3226 }
3227 
3228 static int
3229 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
3230 {
3231     scsi_qla_host_t *vha = fc_vport->dd_data;
3232 
3233     if (disable)
3234         qla24xx_disable_vp(vha);
3235     else
3236         qla24xx_enable_vp(vha);
3237 
3238     return 0;
3239 }
3240 
3241 struct fc_function_template qla2xxx_transport_functions = {
3242 
3243     .show_host_node_name = 1,
3244     .show_host_port_name = 1,
3245     .show_host_supported_classes = 1,
3246     .show_host_supported_speeds = 1,
3247 
3248     .get_host_port_id = qla2x00_get_host_port_id,
3249     .show_host_port_id = 1,
3250     .get_host_speed = qla2x00_get_host_speed,
3251     .show_host_speed = 1,
3252     .get_host_port_type = qla2x00_get_host_port_type,
3253     .show_host_port_type = 1,
3254     .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
3255     .show_host_symbolic_name = 1,
3256     .set_host_system_hostname = qla2x00_set_host_system_hostname,
3257     .show_host_system_hostname = 1,
3258     .get_host_fabric_name = qla2x00_get_host_fabric_name,
3259     .show_host_fabric_name = 1,
3260     .get_host_port_state = qla2x00_get_host_port_state,
3261     .show_host_port_state = 1,
3262 
3263     .dd_fcrport_size = sizeof(struct fc_port *),
3264     .show_rport_supported_classes = 1,
3265 
3266     .get_starget_node_name = qla2x00_get_starget_node_name,
3267     .show_starget_node_name = 1,
3268     .get_starget_port_name = qla2x00_get_starget_port_name,
3269     .show_starget_port_name = 1,
3270     .get_starget_port_id  = qla2x00_get_starget_port_id,
3271     .show_starget_port_id = 1,
3272 
3273     .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
3274     .show_rport_dev_loss_tmo = 1,
3275 
3276     .issue_fc_host_lip = qla2x00_issue_lip,
3277     .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
3278     .terminate_rport_io = qla2x00_terminate_rport_io,
3279     .get_fc_host_stats = qla2x00_get_fc_host_stats,
3280     .reset_fc_host_stats = qla2x00_reset_host_stats,
3281 
3282     .vport_create = qla24xx_vport_create,
3283     .vport_disable = qla24xx_vport_disable,
3284     .vport_delete = qla24xx_vport_delete,
3285     .bsg_request = qla24xx_bsg_request,
3286     .bsg_timeout = qla24xx_bsg_timeout,
3287 };
3288 
3289 struct fc_function_template qla2xxx_transport_vport_functions = {
3290 
3291     .show_host_node_name = 1,
3292     .show_host_port_name = 1,
3293     .show_host_supported_classes = 1,
3294 
3295     .get_host_port_id = qla2x00_get_host_port_id,
3296     .show_host_port_id = 1,
3297     .get_host_speed = qla2x00_get_host_speed,
3298     .show_host_speed = 1,
3299     .get_host_port_type = qla2x00_get_host_port_type,
3300     .show_host_port_type = 1,
3301     .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
3302     .show_host_symbolic_name = 1,
3303     .set_host_system_hostname = qla2x00_set_host_system_hostname,
3304     .show_host_system_hostname = 1,
3305     .get_host_fabric_name = qla2x00_get_host_fabric_name,
3306     .show_host_fabric_name = 1,
3307     .get_host_port_state = qla2x00_get_host_port_state,
3308     .show_host_port_state = 1,
3309 
3310     .dd_fcrport_size = sizeof(struct fc_port *),
3311     .show_rport_supported_classes = 1,
3312 
3313     .get_starget_node_name = qla2x00_get_starget_node_name,
3314     .show_starget_node_name = 1,
3315     .get_starget_port_name = qla2x00_get_starget_port_name,
3316     .show_starget_port_name = 1,
3317     .get_starget_port_id  = qla2x00_get_starget_port_id,
3318     .show_starget_port_id = 1,
3319 
3320     .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
3321     .show_rport_dev_loss_tmo = 1,
3322 
3323     .issue_fc_host_lip = qla2x00_issue_lip,
3324     .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
3325     .terminate_rport_io = qla2x00_terminate_rport_io,
3326     .get_fc_host_stats = qla2x00_get_fc_host_stats,
3327     .reset_fc_host_stats = qla2x00_reset_host_stats,
3328 
3329     .bsg_request = qla24xx_bsg_request,
3330     .bsg_timeout = qla24xx_bsg_timeout,
3331 };
3332 
3333 void
3334 qla2x00_init_host_attr(scsi_qla_host_t *vha)
3335 {
3336     struct qla_hw_data *ha = vha->hw;
3337     u32 speeds = FC_PORTSPEED_UNKNOWN;
3338 
3339     fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
3340     fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
3341     fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
3342     fc_host_supported_classes(vha->host) = ha->base_qpair->enable_class_2 ?
3343             (FC_COS_CLASS2|FC_COS_CLASS3) : FC_COS_CLASS3;
3344     fc_host_max_npiv_vports(vha->host) = ha->max_npiv_vports;
3345     fc_host_npiv_vports_inuse(vha->host) = ha->cur_vport_count;
3346 
3347     speeds = qla25xx_fdmi_port_speed_capability(ha);
3348 
3349     fc_host_supported_speeds(vha->host) = speeds;
3350 }